@cryptotaxi247 / netdata-1 / commits / 408425209

Fix kern.cp_times bug in FreeBSD plugin

Vladimir Kobal committed Feb 5, 2017 at 13:05 UTC 4084252091b0715f2dd5f24b9f484a6219558593
1 file changed +31 -39
src/freebsd_sysctl.c
+31 -39
@@ -42,6 +42,8 @@
42 #define MEGA_FACTOR 1048576 // 1024 * 1024
43 #define GIGA_FACTOR 1073741824 // 1024 * 1024 * 1024
44
45 +#define MAX_INT_DIGITS 10 // maximum number of digits for int
46 +
47 // NEEDED BY: do_disk_io
48 #define RRD_TYPE_DISK "disk"
49
@@ -141,7 +143,7 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
143
144 // NEEDED BY: do_cpu_cores
145 static long *pcpu_cp_time = NULL;
144 - char cpuid[8]; // no more than 4 digits expected
146 + char cpuid[MAX_INT_DIGITS + 1];
147
148 // NEEDED BY: do_all_processes, do_processes
149 struct vmtotal vmtotal_data;
@@ -161,7 +163,7 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
163 int numdevs;
164 static void *devstat_data = NULL;
165 struct devstat *dstat;
164 - char disk[DEVSTAT_NAME_LEN + 10 + 1]; // 10 - maximum number of digits for int
166 + char disk[DEVSTAT_NAME_LEN + MAX_INT_DIGITS + 1];
167 struct cur_dstat {
168 collected_number duration_read_ms;
169 collected_number duration_write_ms;
@@ -412,40 +414,33 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
414 error("DISABLED: cpu.cpuXX");
415 } else {
416 pcpu_cp_time = reallocz(pcpu_cp_time, sizeof(cp_time) * ncpus);
415 -
416 - for (i = 0; i < ncpus; i++) {
417 - if (unlikely(getsysctl("kern.cp_times", pcpu_cp_time, sizeof(cp_time) * ncpus))) {
418 - do_cpu_cores = 0;
419 - error("DISABLED: cpu.cpuXX");
420 - break;
421 - }
422 - if (unlikely(ncpus > 9999)) {
423 - error("FREEBSD: There are more than 4 digits in cpu cores number");
424 - do_cpu_cores = 0;
425 - error("DISABLED: cpu.cpuXX");
426 - break;
427 - }
428 - snprintfz(cpuid, 8, "cpu%d", i);
429 -
430 - st = rrdset_find_bytype("cpu", cpuid);
431 - if (unlikely(!st)) {
432 - st = rrdset_create("cpu", cpuid, NULL, "utilization", "cpu.cpu", "Core utilization", "percentage", 1000, update_every, RRDSET_TYPE_STACKED);
433 -
434 - rrddim_add(st, "user", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
435 - rrddim_add(st, "nice", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
436 - rrddim_add(st, "system", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
437 - rrddim_add(st, "interrupt", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
438 - rrddim_add(st, "idle", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
439 - rrddim_hide(st, "idle");
440 - }
441 - else rrdset_next(st);
442 -
443 - rrddim_set(st, "user", pcpu_cp_time[i * 5 + 0]);
444 - rrddim_set(st, "nice", pcpu_cp_time[i * 5 + 1]);
445 - rrddim_set(st, "system", pcpu_cp_time[i * 5 + 2]);
446 - rrddim_set(st, "interrupt", pcpu_cp_time[i * 5 + 3]);
447 - rrddim_set(st, "idle", pcpu_cp_time[i * 5 + 4]);
448 - rrdset_done(st);
417 + if (unlikely(getsysctl("kern.cp_times", pcpu_cp_time, sizeof(cp_time) * ncpus))) {
418 + do_cpu_cores = 0;
419 + error("DISABLED: cpu.cpuXX");
420 + } else {
421 + for (i = 0; i < ncpus; i++) {
422 + snprintfz(cpuid, MAX_INT_DIGITS, "cpu%d", i);
423 + st = rrdset_find_bytype("cpu", cpuid);
424 + if (unlikely(!st)) {
425 + st = rrdset_create("cpu", cpuid, NULL, "utilization", "cpu.cpu", "Core utilization",
426 + "percentage", 1000, update_every, RRDSET_TYPE_STACKED);
427 +
428 + rrddim_add(st, "user", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
429 + rrddim_add(st, "nice", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
430 + rrddim_add(st, "system", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
431 + rrddim_add(st, "interrupt", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
432 + rrddim_add(st, "idle", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
433 + rrddim_hide(st, "idle");
434 + } else
435 + rrdset_next(st);
436 +
437 + rrddim_set(st, "user", pcpu_cp_time[i * 5 + 0]);
438 + rrddim_set(st, "nice", pcpu_cp_time[i * 5 + 1]);
439 + rrddim_set(st, "system", pcpu_cp_time[i * 5 + 2]);
440 + rrddim_set(st, "interrupt", pcpu_cp_time[i * 5 + 3]);
441 + rrddim_set(st, "idle", pcpu_cp_time[i * 5 + 4]);
442 + rrdset_done(st);
443 + }
444 }
445 }
446 }
@@ -1084,9 +1079,6 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
1079 if (likely(do_netisr || do_netisr_per_core)) {
1080 if (unlikely(GETSYSCTL("kern.smp.cpus", ncpus))) {
1081 common_error = 1;
1087 - } else if (unlikely(ncpus > 9999)) {
1088 - error("FREEBSD: There are more than 4 digits in cpu cores number");
1089 - common_error = 1;
1082 } else if (unlikely(sysctlbyname("net.isr.workstream", NULL, &netisr_workstream_size, NULL, 0) == -1)) {
1083 error("FREEBSD: sysctl(net.isr.workstream...) failed: %s", strerror(errno));
1084 common_error = 1;