Separate kern.cp_times module
Vladimir Kobal committed
Mar 7, 2017 at 23:20 UTC
1c5e878d6d711b6f362b1089975d85f63625bfb0
3 files changed
+98
-63
src/freebsd_sysctl.c
+95
-63
@@ -26,6 +26,9 @@
26
#include <netinet/udp.h>
27
#include <netinet/udp_var.h>
28
29
+// --------------------------------------------------------------------------------------------------------------------
30
+// common definitions and variables
31
+
32
#define KILO_FACTOR 1024
33
#define MEGA_FACTOR 1048576 // 1024 * 1024
34
#define GIGA_FACTOR 1073741824 // 1024 * 1024 * 1024
@@ -33,6 +36,7 @@
36
#define MAX_INT_DIGITS 10 // maximum number of digits for int
37
38
int system_pagesize = PAGE_SIZE;
39
+int number_of_cpus = 1;
40
41
// --------------------------------------------------------------------------------------------------------------------
42
// FreeBSD plugin initialization
@@ -45,6 +49,16 @@ int freebsd_plugin_init()
49
return 1;
50
}
51
52
+ if (unlikely(GETSYSCTL_BY_NAME("kern.smp.cpus", number_of_cpus))) {
53
+ error("FREEBSD: can't get number of cpus");
54
+ return 1;
55
+ }
56
+
57
+ if (unlikely(!number_of_cpus)) {
58
+ error("FREEBSD: wrong number of cpus");
59
+ return 1;
60
+ }
61
+
62
return 0;
63
}
64
@@ -111,9 +125,9 @@ int do_vm_vmtotal(int update_every, usec_t dt) {
125
static int do_all_processes = -1, do_processes = -1, do_committed = -1;
126
127
if (unlikely(do_all_processes == -1)) {
114
- do_all_processes = config_get_boolean("plugin:freebsd:vm.vmtotal", "enable total processes", 1);
115
- do_processes = config_get_boolean("plugin:freebsd:vm.vmtotal", "processes running", 1);
116
- do_committed = config_get_boolean("plugin:freebsd:vm.vmtotal", "committed memory", 1);
128
+ do_all_processes = config_get_boolean("plugin:freebsd:vm.vmtotal", "enable total processes", 1);
129
+ do_processes = config_get_boolean("plugin:freebsd:vm.vmtotal", "processes running", 1);
130
+ do_committed = config_get_boolean("plugin:freebsd:vm.vmtotal", "committed memory", 1);
131
}
132
133
if (likely(do_all_processes | do_processes | do_committed)) {
@@ -256,11 +270,11 @@ int do_kern_cp_time(int update_every, usec_t dt) {
270
RRDSET_TYPE_STACKED
271
);
272
259
- rd_nice = rrddim_add(st, "nice", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
260
- rd_system = rrddim_add(st, "system", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
261
- rd_user = rrddim_add(st, "user", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
262
- rd_interrupt = rrddim_add(st, "interrupt", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
263
- rd_idle = rrddim_add(st, "idle", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
273
+ rd_nice = rrddim_add(st, "nice", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
274
+ rd_system = rrddim_add(st, "system", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
275
+ rd_user = rrddim_add(st, "user", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
276
+ rd_interrupt = rrddim_add(st, "interrupt", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
277
+ rd_idle = rrddim_add(st, "idle", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
278
rrddim_hide(st, "idle");
279
}
280
else rrdset_next(st);
@@ -275,6 +289,78 @@ int do_kern_cp_time(int update_every, usec_t dt) {
289
}
290
}
291
292
+// --------------------------------------------------------------------------------------------------------------------
293
+// kern.cp_times
294
+
295
+int do_kern_cp_times(int update_every, usec_t dt) {
296
+ if (unlikely(CPUSTATES != 5)) {
297
+ error("FREEBSD: There are %d CPU states (5 was expected)", CPUSTATES);
298
+ error("DISABLED: cpu.cpuXX charts");
299
+ error("DISABLED: kern.cp_times module");
300
+ return 1;
301
+ } else {
302
+ static int mib[2] = {0, 0};
303
+ long cp_time[CPUSTATES];
304
+ static long *pcpu_cp_time = NULL;
305
+
306
+ pcpu_cp_time = reallocz(pcpu_cp_time, sizeof(cp_time) * number_of_cpus);
307
+ if (unlikely(GETSYSCTL_WSIZE("kern.cp_times", mib, pcpu_cp_time, sizeof(cp_time) * number_of_cpus))) {
308
+ error("DISABLED: cpu.cpuXX charts");
309
+ error("DISABLED: kern.cp_times module");
310
+ return 1;
311
+ } else {
312
+ int i;
313
+ static struct cpu_chart {
314
+ char cpuid[MAX_INT_DIGITS + 4];
315
+ RRDSET *st;
316
+ RRDDIM *rd_user;
317
+ RRDDIM *rd_nice;
318
+ RRDDIM *rd_system;
319
+ RRDDIM *rd_interrupt;
320
+ RRDDIM *rd_idle;
321
+ } *all_cpu_charts = NULL;
322
+
323
+ all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * number_of_cpus);
324
+
325
+ for (i = 0; i < number_of_cpus; i++) {
326
+ if (unlikely(!all_cpu_charts[i].st)) {
327
+ snprintfz(all_cpu_charts[i].cpuid, MAX_INT_DIGITS, "cpu%d", i);
328
+ all_cpu_charts[i].st = rrdset_create_localhost("cpu",
329
+ all_cpu_charts[i].cpuid,
330
+ NULL,
331
+ "utilization",
332
+ "cpu.cpu",
333
+ "Core utilization",
334
+ "percentage",
335
+ 1000,
336
+ update_every,
337
+ RRDSET_TYPE_STACKED
338
+ );
339
+
340
+ all_cpu_charts[i].rd_nice = rrddim_add(all_cpu_charts[i].st, "nice", NULL, 1, 1,
341
+ RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
342
+ all_cpu_charts[i].rd_system = rrddim_add(all_cpu_charts[i].st, "system", NULL, 1, 1,
343
+ RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
344
+ all_cpu_charts[i].rd_user = rrddim_add(all_cpu_charts[i].st, "user", NULL, 1, 1,
345
+ RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
346
+ all_cpu_charts[i].rd_interrupt = rrddim_add(all_cpu_charts[i].st, "interrupt", NULL, 1, 1,
347
+ RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
348
+ all_cpu_charts[i].rd_idle = rrddim_add(all_cpu_charts[i].st, "idle", NULL, 1, 1,
349
+ RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
350
+ rrddim_hide(all_cpu_charts[i].st, "idle");
351
+ } else rrdset_next(all_cpu_charts[i].st);
352
+
353
+ rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_nice, pcpu_cp_time[i * 5 + 1]);
354
+ rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_system, pcpu_cp_time[i * 5 + 2]);
355
+ rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_user, pcpu_cp_time[i * 5 + 0]);
356
+ rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_interrupt, pcpu_cp_time[i * 5 + 3]);
357
+ rrddim_set_by_pointer(all_cpu_charts[i].st, all_cpu_charts[i].rd_idle, pcpu_cp_time[i * 5 + 4]);
358
+ rrdset_done(all_cpu_charts[i].st);
359
+ }
360
+ }
361
+ }
362
+}
363
+
364
// --------------------------------------------------------------------------------------------------------------------
365
// old sources
366
@@ -349,22 +435,13 @@ int do_freebsd_sysctl_old(int update_every, usec_t dt) {
435
RRDSET *st;
436
RRDDIM *rd;
437
438
+ int ncpus;
439
int i, n;
440
void *p;
441
int common_error = 0;
442
size_t size;
443
char title[4096 + 1];
444
358
- // NEEDED BY: do_cpu_cores
359
- long cp_time[CPUSTATES];
360
-
361
- // NEEDED BY: du_cpu_cores, do_netisr, do_netisr_per_core
362
- int ncpus;
363
-
364
- // NEEDED BY: do_cpu_cores
365
- static long *pcpu_cp_time = NULL;
366
- char cpuid[MAX_INT_DIGITS + 1];
367
-
445
// NEEDED BY: do_context, do_forks
446
u_int u_int_data;
447
@@ -495,51 +572,6 @@ int do_freebsd_sysctl_old(int update_every, usec_t dt) {
572
573
// --------------------------------------------------------------------
574
498
- if (likely(do_cpu_cores)) {
499
- if (unlikely(CPUSTATES != 5)) {
500
- error("FREEBSD: There are %d CPU states (5 was expected)", CPUSTATES);
501
- do_cpu_cores = 0;
502
- error("DISABLED: cpu.cpuXX");
503
- } else {
504
- if (unlikely(GETSYSCTL_BY_NAME("kern.smp.cpus", ncpus))) {
505
- do_cpu_cores = 0;
506
- error("DISABLED: cpu.cpuXX");
507
- } else {
508
- pcpu_cp_time = reallocz(pcpu_cp_time, sizeof(cp_time) * ncpus);
509
- if (unlikely(getsysctl_by_name("kern.cp_times", pcpu_cp_time, sizeof(cp_time) * ncpus))) {
510
- do_cpu_cores = 0;
511
- error("DISABLED: cpu.cpuXX");
512
- } else {
513
- for (i = 0; i < ncpus; i++) {
514
- snprintfz(cpuid, MAX_INT_DIGITS, "cpu%d", i);
515
- st = rrdset_find_bytype_localhost("cpu", cpuid);
516
- if (unlikely(!st)) {
517
- st = rrdset_create_localhost("cpu", cpuid, NULL, "utilization", "cpu.cpu", "Core utilization",
518
- "percentage", 1000, update_every, RRDSET_TYPE_STACKED);
519
-
520
- rrddim_add(st, "nice", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
521
- rrddim_add(st, "system", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
522
- rrddim_add(st, "user", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
523
- rrddim_add(st, "interrupt", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
524
- rrddim_add(st, "idle", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
525
- rrddim_hide(st, "idle");
526
- } else
527
- rrdset_next(st);
528
-
529
- rrddim_set(st, "nice", pcpu_cp_time[i * 5 + 1]);
530
- rrddim_set(st, "system", pcpu_cp_time[i * 5 + 2]);
531
- rrddim_set(st, "user", pcpu_cp_time[i * 5 + 0]);
532
- rrddim_set(st, "interrupt", pcpu_cp_time[i * 5 + 3]);
533
- rrddim_set(st, "idle", pcpu_cp_time[i * 5 + 4]);
534
- rrdset_done(st);
535
- }
536
- }
537
- }
538
- }
539
- }
540
-
541
- // --------------------------------------------------------------------
542
-
575
if (likely(do_interrupts)) {
576
if (unlikely(sysctlbyname("hw.intrcnt", NULL, &intrcnt_size, NULL, 0) == -1)) {
577
error("FREEBSD: sysctl(hw.intrcnt...) failed: %s", strerror(errno));
src/plugin_freebsd.c
+1
@@ -19,6 +19,7 @@ static struct freebsd_module {
19
{ .name = "vm.loadavg", .dim = "loadavg", .enabled = 1, .func = do_vm_loadavg },
20
{ .name = "vm.vmtotal", .dim = "vmtotal", .enabled = 1, .func = do_vm_vmtotal },
21
{ .name = "kern.cp_time", .dim = "cp_time", .enabled = 1, .func = do_kern_cp_time },
22
+ { .name = "kern.cp_times", .dim = "cp_times", .enabled = 1, .func = do_kern_cp_times },
23
24
// the terminator of this array
25
{ .name = NULL, .dim = NULL, .enabled = 0, .func = NULL }
src/plugin_freebsd.h
+2
@@ -10,6 +10,7 @@ extern int freebsd_plugin_init();
10
extern int do_vm_loadavg(int update_every, usec_t dt);
11
extern int do_vm_vmtotal(int update_every, usec_t dt);
12
extern int do_kern_cp_time(int update_every, usec_t dt);
13
+extern int do_kern_cp_times(int update_every, usec_t dt);
14
extern int do_freebsd_sysctl_old(int update_every, usec_t dt);
15
16
#define GETSYSCTL_MIB(name, mib) getsysctl_mib(name, mib, sizeof(mib)/sizeof(int))
@@ -30,6 +31,7 @@ static inline int getsysctl_mib(const char *name, int *mib, size_t len)
31
}
32
33
#define GETSYSCTL_SIMPLE(name, mib, var) getsysctl_simple(name, mib, sizeof(mib)/sizeof(int), &(var), sizeof(var))
34
+#define GETSYSCTL_WSIZE(name, mib, var, size) getsysctl_simple(name, mib, sizeof(mib)/sizeof(int), var, size)
35
36
static inline int getsysctl_simple(const char *name, int *mib, size_t miblen, void *ptr, size_t len)
37
{