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 <sys/protosw.h>
43 # include <libperfstat.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 {
52 value_list_t vl = VALUE_LIST_INIT;
54 vl.values = &(value_t) { .derive = context_switches };
55 vl.values_len = 1;
56 sstrncpy (vl.plugin, "contextswitch", sizeof (vl.plugin));
57 sstrncpy (vl.type, "contextswitch", sizeof (vl.type));
59 plugin_dispatch_values (&vl);
60 }
62 static int cs_read (void)
63 {
64 #if HAVE_SYSCTLBYNAME
65 int value = 0;
66 size_t value_len = sizeof (value);
67 int status;
69 status = sysctlbyname ("vm.stats.sys.v_swtch",
70 &value, &value_len,
71 /* new pointer = */ NULL, /* new length = */ 0);
72 if (status != 0)
73 {
74 ERROR("contextswitch plugin: sysctlbyname "
75 "(vm.stats.sys.v_swtch) failed");
76 return (-1);
77 }
79 cs_submit (value);
80 /* #endif HAVE_SYSCTLBYNAME */
82 #elif KERNEL_LINUX
83 FILE *fh;
84 char buffer[64];
85 int numfields;
86 char *fields[3];
87 derive_t result = 0;
88 int status = -2;
90 fh = fopen ("/proc/stat", "r");
91 if (fh == NULL) {
92 ERROR ("contextswitch plugin: unable to open /proc/stat: %s",
93 sstrerror (errno, buffer, sizeof (buffer)));
94 return (-1);
95 }
97 while (fgets(buffer, sizeof(buffer), fh) != NULL)
98 {
99 char *endptr;
101 numfields = strsplit(buffer, fields, STATIC_ARRAY_SIZE (fields));
102 if (numfields != 2)
103 continue;
105 if (strcmp("ctxt", fields[0]) != 0)
106 continue;
108 errno = 0;
109 endptr = NULL;
110 result = (derive_t) strtoll (fields[1], &endptr, /* base = */ 10);
111 if ((endptr == fields[1]) || (errno != 0)) {
112 ERROR ("contextswitch plugin: Cannot parse ctxt value: %s",
113 fields[1]);
114 status = -1;
115 break;
116 }
118 cs_submit(result);
119 status = 0;
120 break;
121 }
122 fclose(fh);
124 if (status == -2)
125 ERROR ("contextswitch plugin: Unable to find context switch value.");
126 /* #endif KERNEL_LINUX */
128 #elif HAVE_PERFSTAT
129 int status = 0;
130 perfstat_cpu_total_t perfcputotal;
132 status = perfstat_cpu_total(NULL, &perfcputotal, sizeof(perfstat_cpu_total_t), 1);
133 if (status < 0)
134 {
135 char errbuf[1024];
136 ERROR ("contextswitch plugin: perfstat_cpu_total: %s",
137 sstrerror (errno, errbuf, sizeof (errbuf)));
138 return (-1);
139 }
141 cs_submit(perfcputotal.pswitch);
142 status = 0;
143 #endif /* defined(HAVE_PERFSTAT) */
145 return status;
146 }
148 void module_register (void)
149 {
150 plugin_register_read ("contextswitch", cs_read);
151 } /* void module_register */