X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fcontextswitch.c;h=35ac5a397b03b1a095ac6199d5af48096546f0eb;hp=834fbd74cd9182c7931c6a2ce898cb98650c01cc;hb=a9e50e9e30ecde17e167e271060c8183bfcbf407;hpb=41288c6a9ed050b41ad47184aa1b53668c3588cc diff --git a/src/contextswitch.c b/src/contextswitch.c index 834fbd74..35ac5a39 100644 --- a/src/contextswitch.c +++ b/src/contextswitch.c @@ -27,7 +27,7 @@ #include "plugin.h" #ifdef HAVE_SYS_SYSCTL_H -# include +#include #endif #if HAVE_SYSCTLBYNAME @@ -39,113 +39,103 @@ /* #endif KERNEL_LINUX */ #elif HAVE_PERFSTAT -# include -# include +#include +#include /* #endif HAVE_PERFSTAT */ #else -# error "No applicable input method." +#error "No applicable input method." #endif -static void cs_submit (derive_t context_switches) -{ - value_list_t vl = VALUE_LIST_INIT; +static void cs_submit(derive_t context_switches) { + value_list_t vl = VALUE_LIST_INIT; - vl.values = &(value_t) { .derive = context_switches }; - vl.values_len = 1; - sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin)); - sstrncpy (vl.type, "contextswitch", sizeof (vl.type)); + vl.values = &(value_t){.derive = context_switches}; + vl.values_len = 1; + sstrncpy(vl.plugin, "contextswitch", sizeof(vl.plugin)); + sstrncpy(vl.type, "contextswitch", sizeof(vl.type)); - plugin_dispatch_values (&vl); + plugin_dispatch_values(&vl); } -static int cs_read (void) -{ +static int cs_read(void) { #if HAVE_SYSCTLBYNAME - int value = 0; - size_t value_len = sizeof (value); - int status; - - status = sysctlbyname ("vm.stats.sys.v_swtch", - &value, &value_len, - /* new pointer = */ NULL, /* new length = */ 0); - if (status != 0) - { - ERROR("contextswitch plugin: sysctlbyname " - "(vm.stats.sys.v_swtch) failed"); - return (-1); - } - - cs_submit (value); + int value = 0; + size_t value_len = sizeof(value); + int status; + + status = sysctlbyname("vm.stats.sys.v_swtch", &value, &value_len, + /* new pointer = */ NULL, /* new length = */ 0); + if (status != 0) { + ERROR("contextswitch plugin: sysctlbyname " + "(vm.stats.sys.v_swtch) failed"); + return -1; + } + + cs_submit(value); /* #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) { - ERROR ("contextswitch plugin: unable to open /proc/stat: %s", - sstrerror (errno, buffer, sizeof (buffer))); - return (-1); - } - - while (fgets(buffer, sizeof(buffer), fh) != NULL) - { - char *endptr; - - numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields)); - if (numfields != 2) - continue; - - if (strcmp("ctxt", fields[0]) != 0) - continue; - - errno = 0; - endptr = NULL; - result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10); - if ((endptr == fields[1]) || (errno != 0)) { - ERROR ("contextswitch plugin: Cannot parse ctxt value: %s", - fields[1]); - status = -1; - break; - } - - cs_submit(result); - status = 0; - break; - } - fclose(fh); - - if (status == -2) - ERROR ("contextswitch plugin: Unable to find context switch value."); + 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) { + ERROR("contextswitch plugin: unable to open /proc/stat: %s", STRERRNO); + return -1; + } + + while (fgets(buffer, sizeof(buffer), fh) != NULL) { + char *endptr; + + numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); + if (numfields != 2) + continue; + + if (strcmp("ctxt", fields[0]) != 0) + continue; + + errno = 0; + endptr = NULL; + result = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10); + if ((endptr == fields[1]) || (errno != 0)) { + ERROR("contextswitch plugin: Cannot parse ctxt value: %s", fields[1]); + status = -1; + break; + } + + cs_submit(result); + status = 0; + break; + } + fclose(fh); + + if (status == -2) + ERROR("contextswitch plugin: Unable to find context switch value."); /* #endif KERNEL_LINUX */ #elif HAVE_PERFSTAT - int status = 0; - perfstat_cpu_total_t perfcputotal; - - status = perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1); - if (status < 0) - { - char errbuf[1024]; - ERROR ("contextswitch plugin: perfstat_cpu_total: %s", - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); - } - - cs_submit(perfcputotal.pswitch); - status = 0; + int status = 0; + perfstat_cpu_total_t perfcputotal; + + status = + perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1); + if (status < 0) { + ERROR("contextswitch plugin: perfstat_cpu_total: %s", STRERRNO); + return -1; + } + + cs_submit(perfcputotal.pswitch); + status = 0; #endif /* defined(HAVE_PERFSTAT) */ - return status; + return status; } -void module_register (void) -{ - plugin_register_read ("contextswitch", cs_read); +void module_register(void) { + plugin_register_read("contextswitch", cs_read); } /* void module_register */