[collectd] [PATCH] contextswitch support in freebsd

Kimo Rosenbaum kimor79 at yahoo.com
Tue Aug 24 22:44:44 CEST 2010


Hello,

FreeBSD makes the number of context switches available via the 
vm.stats.sys.v_swtch sysctl. I am by no means a programmer so apologies if this 
isn't the right way to go about this. I used HAVE_SYSCTLBYNAME because there 
were other places in the code that I could copy/paste from.

Thanks
Kimo

$ diff -pur collectd-4.10.1 collectd-4.10.1-new 
diff -pur collectd-4.10.1/configure.in collectd-4.10.1-new/configure.in
--- collectd-4.10.1/configure.in    2010-07-09 03:01:59.000000000 -0700
+++ collectd-4.10.1-new/configure.in    2010-08-24 13:37:58.000000000 -0700
@@ -4131,6 +4131,7 @@ then
 fi
 if test "x$have_sysctlbyname" = "xyes"
 then
+    plugin_contextswitch="yes"
     plugin_cpu="yes"
     plugin_memory="yes"
     plugin_tcpconns="yes"
diff -pur collectd-4.10.1/src/contextswitch.c 
collectd-4.10.1-new/src/contextswitch.c
--- collectd-4.10.1/src/contextswitch.c    2010-07-09 03:01:59.000000000 -0700
+++ collectd-4.10.1-new/src/contextswitch.c    2010-08-24 13:38:13.000000000 
-0700
@@ -23,7 +23,19 @@
 #include "common.h"
 #include "plugin.h"
 
-#if !KERNEL_LINUX
+#ifdef HAVE_SYS_SYSCTL_H
+# include <sys/sysctl.h>
+#endif
+
+#if HAVE_SYSCTLBYNAME
+/* no global variables */
+/* #endif HAVE_SYSCTLBYNAME */
+
+#elif KERNEL_LINUX
+/* no global variables */
+/* #endif KERNEL_LINUX */
+
+#else
 # error "No applicable input method."
 #endif
 
@@ -45,12 +57,29 @@ static void cs_submit (derive_t context_
 
 static int cs_read (void)
 {
+    int status = -2;
+#if HAVE_SYSCTLBYNAME
+    int value;
+    size_t value_len = sizeof (value);
+
+    if (sysctlbyname ("vm.stats.sys.v_swtch", (void *) &value, &value_len,
+            NULL, 0) == 0)
+    {
+        cs_submit(value);
+        status = 0;
+    }
+    else
+    {
+        ERROR("contextswitch plugin: sysctlbyname failed");
+    }
+
+/* #endif HAVE_SYSCTLBYNAME */
+#elif KERNEL_LINUX
     FILE *fh;
     char buffer[64];
     int numfields;
     char *fields[3];
     derive_t result = 0;
-    int status = -2;
 
     fh = fopen ("/proc/stat", "r");
     if (fh == NULL) {
@@ -88,6 +117,7 @@ static int cs_read (void)
 
     if (status == -2)
         ERROR ("contextswitch plugin: Unable to find context switch value.");
+#endif /* KERNEL_LINUX */
 
     return status;
 }



More information about the collectd mailing list