[Pkg-wmaker-commits] [wmbubble] 181/207: Further FreeBSD updates.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Mon Aug 24 04:18:31 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch master
in repository wmbubble.

commit 3e74615e7cab9bb8e5ce95faece08f1175c217cb
Author: Doug Torrance <dtorrance at monmouthcollege.edu>
Date:   Sun Nov 30 07:46:23 2014 -0600

    Further FreeBSD updates.
    
    Remove the dependency on libkvm and the need for a setgid binary by using
    sysctl to determine memory and swap usage.
    
    Based on similar patches for wmmemload.  See:
    http://repo.or.cz/w/dockapps.git/commitdiff/a473280ab9850b93ba2da950cb549c62a2225351?hp=ba72863015547316aaed5e9c42fd179faeeaa911
    http://sources.debian.net/src/wmmemload/0.1.7-2/debian/patches/sysctl_swap.patch/
---
 Makefile              |   6 +-
 bubblemon.c           |   8 ---
 include/sys_include.h |   3 -
 sys_freebsd.c         | 161 +++++++++++++++++++-------------------------------
 4 files changed, 63 insertions(+), 115 deletions(-)

diff --git a/Makefile b/Makefile
index fbb1d69..eb77280 100644
--- a/Makefile
+++ b/Makefile
@@ -32,14 +32,12 @@ endif
 # special things for FreeBSD
 ifeq ($(OS), FreeBSD)
 	OBJS += sys_freebsd.o
-	LIBS = -lX11 -lkvm -lm
-	INSTALL = -c -g kmem -m 2755 -o root
+	LIBS = -lX11 -lm
 endif
 
 ifeq ($(OS), GNU/kFreeBSD)
 	OBJS += sys_freebsd.o
-	LIBS = -lX11 -lkvm -lm
-	INSTALL = -c -g kmem -m 2755 -o root
+	LIBS = -lX11 -lm
 	CFLAGS += -D_BSD_SOURCE
 endif
 
diff --git a/bubblemon.c b/bubblemon.c
index 3f9a66f..0e40fbd 100644
--- a/bubblemon.c
+++ b/bubblemon.c
@@ -124,9 +124,6 @@ int animate_correctly(void);
 void draw_duck(int x, int y, int frame_no, int flipx, int flipy);
 void duck_swimmer(void);
 
-#ifdef __FreeBSD__
-extern int init_stuff();	/* defined in sys_{freebsd,linux}.c */
-#endif
 /* local prototypes end *INDENT-ON* */
 
 extern char * optarg;
@@ -405,11 +402,6 @@ int main(int argc, char **argv) {
 
 	argv++; argc--; /* Otherwise we'll make more of ourselves on a left click */
 
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-	if (init_stuff())
-		exit(-1);
-#endif
-
 	make_new_bubblemon_dockapp();
 
 	/* the math below makes the cpu gauge try to update at 5 Hz.
diff --git a/include/sys_include.h b/include/sys_include.h
index da211c2..0aa6bda 100644
--- a/include/sys_include.h
+++ b/include/sys_include.h
@@ -24,8 +24,5 @@ int system_cpu(void);		/* return total CPU load in percent */
 void system_memory(void);	/* set memory related values in BubbleMonData */
 void system_loadavg(void);	/* get current load average and put into
 				   bm->loadavg[].{i,f} */
-#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-int init_stuff();
-#endif
 
 #endif /* _SYS_INCLUDE_H_ */
diff --git a/sys_freebsd.c b/sys_freebsd.c
index 1cf68f7..3113f9d 100644
--- a/sys_freebsd.c
+++ b/sys_freebsd.c
@@ -17,70 +17,35 @@
  *
  */
 
-#include <kvm.h>
-#include <fcntl.h>
 #include <sys/types.h>
-#include <sys/dkstat.h>
-#include <sys/vmmeter.h>
 #include <stdlib.h>
 #include <stdio.h>
-#include <unistd.h>
-#include <sys/time.h>
 #include <sys/resource.h>
 #include <sys/sysctl.h>
-#include <vm/vm_param.h>
 #include <time.h>
+#include <sys/errno.h>
+#include <string.h>
 #include "include/bubblemon.h"
 #include "include/sys_include.h"
 
 extern BubbleMonData bm;
 
-static kvm_t *kd = NULL;
-static struct nlist nlst[] = {
-    {"_cnt", 0},
-    {"_bufspace", 0},
-    {0, 0}
-};
-static int pageshift;
+#define GETSYSCTL(name, var) getsysctl(name, &(var), sizeof(var))
 
-#define pagetob(size) ((size) << pageshift)
-
-int init_stuff()
+static void getsysctl(const char *name, void *ptr, size_t len)
 {
-    /* calculate page shift to convert pages into kilobytes */
-    int pagesize = getpagesize();
-    pageshift = 0;
-
-    while (pagesize > 1) {
-	pageshift++;
-	pagesize >>= 1;
-    }
+    size_t nlen = len;
 
-    /* open kernel memory */
-    kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
-
-    if (kd == NULL) {
-	puts("Could not open kernel virtual memory");
-	return 1;
+    if (sysctlbyname(name, ptr, &nlen, NULL, 0) == -1) {
+        fprintf(stderr, "sysctl(%s...) failed: %s\n", name,
+	        strerror(errno));
+	exit(1);
     }
-
-    kvm_nlist(kd, nlst);
-
-    if (nlst[0].n_type == 0 || nlst[1].n_type == 0) {
-	puts("Error extracting symbols");
-	return 2;
+    if (nlen != len) {
+	fprintf(stderr, "sysctl(%s...) expected %lu, got %lu\n",
+	        name, (unsigned long)len, (unsigned long)nlen);
+	exit(1);
     }
-
-    /* drop setgid & setuid (the latter should not be there really) */
-    seteuid(getuid());
-    setegid(getgid());
-
-    if (geteuid() != getuid() || getegid() != getgid()) {
-	puts("Unable to drop privileges");
-	return 3;
-    }
-
-    return 0;
 }
 
 /* Returns the current CPU load in percent */
@@ -92,19 +57,7 @@ int system_cpu(void)
     unsigned long int cpu_time[CPUSTATES];
     int i;
 
-    static int mib[2];
-    size_t len = 2;
-
-    size_t size;
-
-    if (sysctlnametomib("kern.cp_time", mib, &len) < 0)
-        return 0;
-
-    size = sizeof (cpu_time);
-
-
-    if (sysctl(mib, 2, &cpu_time, &size, NULL, 0) < 0)
-        return 0;
+    GETSYSCTL("kern.cp_time", cpu_time);
 
     load = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_NICE];
     total = load + cpu_time[CP_IDLE];
@@ -131,62 +84,70 @@ int system_cpu(void)
 
 void system_memory(void)
 {
-    u_int64_t my_mem_used, my_mem_max;
-    u_int64_t my_swap_used, my_swap_max;
-    struct vmmeter sum;
-    int bufspace;
+    u_int new_swappgsin, new_swappgsout;
+    u_int my_mem_used, my_mem_max;
+    u_int pagesize;
     static int swappgsin = -1;
     static int swappgsout = -1;
     static int swap_firsttime = 1;
     static int swapavail = 0, swapused = 0;
     static time_t last_time_swap = 0;
     time_t curr_time;
-	
-    if (kvm_read(kd, nlst[0].n_value, &sum, sizeof(sum)) != sizeof(sum))
-	return;		/* _cnt */
-
-    if (kvm_read(kd, nlst[1].n_value, &bufspace, sizeof(bufspace)) !=
-	sizeof(bufspace))
-	return;		/* _bufspace */
 
-    my_mem_max = pagetob((u_int64_t) sum.v_page_count);
-    my_mem_used = pagetob((u_int64_t) sum.v_active_count);
+    GETSYSCTL("vm.stats.vm.v_page_count", my_mem_max);
+    GETSYSCTL("vm.stats.vm.v_active_count", my_mem_used);
+    GETSYSCTL("hw.pagesize", pagesize);
 
     /* only calculate when first time or when changes took place */
     /* do not call it more than 1 time per 2 seconds */
     /* otherwise it can eat up to 50% of CPU time on heavy swap activity */
     curr_time = time(NULL);
-    
-    if (swap_firsttime ||
-	(((sum.v_swappgsin > swappgsin) || (sum.v_swappgsout > swappgsout)) &&
-	curr_time > last_time_swap + 1)) {
-	
-	struct kvm_swap swap;
-	int n;
-
-	swapavail = 0;
-	swapused = 0;
 
-	n = kvm_getswapinfo(kd, &swap, 1, 0);
-	if (n >= 0 && swap.ksw_total != 0) {
-	    swapavail = pagetob(swap.ksw_total);
-	    swapused = pagetob(swap.ksw_used);
-	}
+    GETSYSCTL("vm.stats.vm.v_swappgsin", new_swappgsin);
+    GETSYSCTL("vm.stats.vm.v_swappgsout", new_swappgsout);
 
-	swap_firsttime = 0;
-	last_time_swap = curr_time;
+    if (swap_firsttime ||
+        (((new_swappgsin > swappgsin) || (new_swappgsout > swappgsout)) &&
+        curr_time > last_time_swap + 1)) {
+        int mib[2], n;
+        size_t mibsize, size;
+        struct xswdev xsw;
+
+        mibsize = sizeof(mib) / sizeof(mib[0]);
+        if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) {
+            fprintf(stderr, "sysctlnametomib() failed %s\n",
+                    strerror(errno));
+            return;
+        }
+
+        swapavail = 0;
+        swapused = 0;
+
+        for (n = 0; ; n++) {
+            mib[mibsize] = n;
+            size = sizeof(xsw);
+            if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) {
+                if (errno == ENOENT)
+                    break;
+                fprintf(stderr, "sysctl() failed: %s\n",
+                        strerror(errno));
+                return;
+            }
+            swapavail += xsw.xsw_nblks;
+            swapused += xsw.xsw_used;
+        }
+
+        swap_firsttime = 0;
+        last_time_swap = curr_time;
     }
 
-    my_swap_used = swapused;
-    my_swap_max = swapavail;
-
-    swappgsin = sum.v_swappgsin;
-    swappgsout = sum.v_swappgsout;
+    swappgsin = new_swappgsin;
+    swappgsout = new_swappgsout;
 
-    bm.mem_used = my_mem_used;
-    bm.mem_max = my_mem_max;
-    bm.swap_used = my_swap_used;
-    bm.swap_max = my_swap_max;
+    bm.mem_used = my_mem_used * pagesize;
+    bm.mem_max = my_mem_max * pagesize;
+    bm.swap_used = swapused * pagesize;
+    bm.swap_max = swapavail * pagesize;
 }
 
 void system_loadavg(void)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-wmaker/wmbubble.git



More information about the Pkg-wmaker-commits mailing list