1 /**
2 * collectd - src/contextswitch.c
3 * Copyright (C) 2009 Patrik Weiskircher
4 * Copyright (C) 2010 Kimo Rosenbaum
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Patrik Weiskircher <weiskircher at inqnet.at>
21 * Kimo Rosenbaum <http://github.com/kimor79>
22 **/
24 #include "collectd.h"
26 #include "common.h"
27 #include "plugin.h"
29 #ifdef HAVE_SYS_SYSCTL_H
30 #include <sys/sysctl.h>
31 #endif
33 #if HAVE_SYSCTLBYNAME
34 /* no global variables */
35 /* #endif HAVE_SYSCTLBYNAME */
37 #elif KERNEL_LINUX
38 /* no global variables */
39 /* #endif KERNEL_LINUX */
41 #elif HAVE_PERFSTAT
42 #include <libperfstat.h>
43 #include <sys/protosw.h>
44 /* #endif HAVE_PERFSTAT */
46 #else
47 #error "No applicable input method."
48 #endif
50 static void cs_submit(derive_t context_switches) {
51 value_list_t vl = VALUE_LIST_INIT;
53 vl.values = &(value_t){.derive = context_switches};
54 vl.values_len = 1;
55 sstrncpy(vl.plugin, "contextswitch", sizeof(vl.plugin));
56 sstrncpy(vl.type, "contextswitch", sizeof(vl.type));
58 plugin_dispatch_values(&vl);
59 }
61 static int cs_read(void) {
62 #if HAVE_SYSCTLBYNAME
63 int value = 0;
64 size_t value_len = sizeof(value);
65 int status;
67 status = sysctlbyname("vm.stats.sys.v_swtch", &value, &value_len,
68 /* new pointer = */ NULL, /* new length = */ 0);
69 if (status != 0) {
70 ERROR("contextswitch plugin: sysctlbyname "
71 "(vm.stats.sys.v_swtch) failed");
72 return (-1);
73 }
75 cs_submit(value);
76 /* #endif HAVE_SYSCTLBYNAME */
78 #elif KERNEL_LINUX
79 FILE *fh;
80 char buffer[64];
81 int numfields;
82 char *fields[3];
83 derive_t result = 0;
84 int status = -2;
86 fh = fopen("/proc/stat", "r");
87 if (fh == NULL) {
88 ERROR("contextswitch plugin: unable to open /proc/stat: %s",
89 sstrerror(errno, buffer, sizeof(buffer)));
90 return (-1);
91 }
93 while (fgets(buffer, sizeof(buffer), fh) != NULL) {
94 char *endptr;
96 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields));
97 if (numfields != 2)
98 continue;
100 if (strcmp("ctxt", fields[0]) != 0)
101 continue;
103 errno = 0;
104 endptr = NULL;
105 result = (derive_t)strtoll(fields[1], &endptr, /* base = */ 10);
106 if ((endptr == fields[1]) || (errno != 0)) {
107 ERROR("contextswitch plugin: Cannot parse ctxt value: %s", fields[1]);
108 status = -1;
109 break;
110 }
112 cs_submit(result);
113 status = 0;
114 break;
115 }
116 fclose(fh);
118 if (status == -2)
119 ERROR("contextswitch plugin: Unable to find context switch value.");
120 /* #endif KERNEL_LINUX */
122 #elif HAVE_PERFSTAT
123 int status = 0;
124 perfstat_cpu_total_t perfcputotal;
126 status =
127 perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1);
128 if (status < 0) {
129 char errbuf[1024];
130 ERROR("contextswitch plugin: perfstat_cpu_total: %s",
131 sstrerror(errno, errbuf, sizeof(errbuf)));
132 return (-1);
133 }
135 cs_submit(perfcputotal.pswitch);
136 status = 0;
137 #endif /* defined(HAVE_PERFSTAT) */
139 return status;
140 }
142 void module_register(void) {
143 plugin_register_read("contextswitch", cs_read);
144 } /* void module_register */