Add CPU usage chart to macOS plugin
Vladimir Kobal committed
Dec 26, 2016 at 21:23 UTC
100e150235290130f7c5a39f666dcb6e0604fc9e
1 file changed
+32
-21
src/macos_mach_smi.c
+32
-21
@@ -1,4 +1,5 @@
1
#include "common.h"
2
+#include <mach/mach_host.h>
3
4
int do_macos_mach_smi(int update_every, usec_t dt) {
5
(void)dt;
@@ -12,35 +13,45 @@ int do_macos_mach_smi(int update_every, usec_t dt) {
13
RRDSET *st;
14
15
// NEEDED BY: do_cpu, do_cpu_cores
15
- long cp_time[5];
16
+ natural_t cp_time[CPU_STATE_MAX];
17
+ kern_return_t kr;
18
+ mach_msg_type_number_t count;
19
20
// --------------------------------------------------------------------
21
22
if (likely(do_cpu)) {
20
- if (unlikely(1)) {
23
+ if (unlikely(HOST_CPU_LOAD_INFO_COUNT != 4)) {
24
+ error("FREEBSD: There are %d CPU states (4 was expected)", HOST_CPU_LOAD_INFO_COUNT);
25
do_cpu = 0;
26
error("DISABLED: system.cpu");
27
} else {
24
-
25
- st = rrdset_find_bytype("system", "cpu");
26
- if (unlikely(!st)) {
27
- st = rrdset_create("system", "cpu", NULL, "cpu", "system.cpu", "Total CPU utilization", "percentage", 100, update_every, RRDSET_TYPE_STACKED);
28
-
29
- rrddim_add(st, "user", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
30
- rrddim_add(st, "nice", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
31
- rrddim_add(st, "system", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
32
- rrddim_add(st, "interrupt", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
33
- rrddim_add(st, "idle", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
34
- rrddim_hide(st, "idle");
28
+ count = HOST_CPU_LOAD_INFO_COUNT;
29
+ if (unlikely(kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)cp_time, &count))) {
30
+ if (kr != KERN_SUCCESS) {
31
+ error("MACOS: host_statistics() failed: %s", mach_error_string(kr));
32
+ do_cpu = 0;
33
+ error("DISABLED: system.cpu");
34
+ }
35
+ } else {
36
+
37
+ st = rrdset_find_bytype("system", "cpu");
38
+ if (unlikely(!st)) {
39
+ st = rrdset_create("system", "cpu", NULL, "cpu", "system.cpu", "Total CPU utilization", "percentage", 100, update_every, RRDSET_TYPE_STACKED);
40
+
41
+ rrddim_add(st, "user", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
42
+ rrddim_add(st, "nice", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
43
+ rrddim_add(st, "system", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
44
+ rrddim_add(st, "idle", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
45
+ rrddim_hide(st, "idle");
46
+ }
47
+ else rrdset_next(st);
48
+
49
+ rrddim_set(st, "user", cp_time[CPU_STATE_USER]);
50
+ rrddim_set(st, "nice", cp_time[CPU_STATE_NICE]);
51
+ rrddim_set(st, "system", cp_time[CPU_STATE_SYSTEM]);
52
+ rrddim_set(st, "idle", cp_time[CPU_STATE_IDLE]);
53
+ rrdset_done(st);
54
}
36
- else rrdset_next(st);
37
-
38
- rrddim_set(st, "user", cp_time[0]);
39
- rrddim_set(st, "nice", cp_time[1]);
40
- rrddim_set(st, "system", cp_time[2]);
41
- rrddim_set(st, "interrupt", cp_time[3]);
42
- rrddim_set(st, "idle", cp_time[4]);
43
- rrdset_done(st);
55
}
56
}
57