[Xfce4-commits] <xfce4-taskmanager:master> Implement fetching of swap stats

Landry Breuil noreply at xfce.org
Thu May 13 20:10:02 CEST 2010


Updating branch refs/heads/master
         to 10247249ebb5aec6041876151c53085ce20c6192 (commit)
       from e5ad1842bf9721567ad0c93e7081ba5c24496088 (commit)

commit 10247249ebb5aec6041876151c53085ce20c6192
Author: Landry Breuil <landry at rhaalovely.net>
Date:   Thu May 13 19:56:51 2010 +0200

    Implement fetching of swap stats

 src/task-manager-bsd.c |   27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diff --git a/src/task-manager-bsd.c b/src/task-manager-bsd.c
index 1c21974..5c73d91 100644
--- a/src/task-manager-bsd.c
+++ b/src/task-manager-bsd.c
@@ -163,6 +163,8 @@ gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64
 {
 	int mib[] = {CTL_VM, VM_METER};
 	struct vmtotal vmtotal;
+	struct swapent *swdev;
+	int nswap, i;
 	size_t size;
 	size = sizeof(vmtotal);
 	if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0)
@@ -172,9 +174,28 @@ gboolean get_memory_usage (guint64 *memory_total, guint64 *memory_free, guint64
 	*memory_free = pagetok(vmtotal.t_free);
 	*memory_cache = 0;
 	*memory_buffers = pagetok(vmtotal.t_rm - vmtotal.t_arm);
-	/* XXX:TODO */
-	*swap_total = 0;
-	*swap_free = 0;
+
+	/* get swap stats */
+	if ((nswap = swapctl(SWAP_NSWAP, 0, 0)) == 0)
+		errx(1,"failed to get swap device count");
+
+	if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL)
+		errx(1,"failed to allocate memory for swdev structures");
+
+	if (swapctl(SWAP_STATS, swdev, nswap) == -1) {
+		free(swdev);
+		errx(1,"failed to get swap stats");
+	}
+
+	/* Total things up */
+	*swap_total = *swap_free = 0;
+	for (i = 0; i < nswap; i++) {
+		if (swdev[i].se_flags & SWF_ENABLE) {
+			*swap_free += ((swdev[i].se_nblks - swdev[i].se_inuse) / (1024 / DEV_BSIZE));
+			*swap_total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
+		}
+	}
+	free(swdev);
 	return TRUE;
 }
 



More information about the Xfce4-commits mailing list