]> arthur.barton.de Git - netdata.git/blob - src/macos_mach_smi.c
Merge pull request #1451 from ktsaou/master
[netdata.git] / src / macos_mach_smi.c
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;
6
7     static int do_cpu = -1;
8
9     if (unlikely(do_cpu == -1)) {
10         do_cpu                  = config_get_boolean("plugin:macos:mach_smi", "cpu utilization", 1);
11     }
12
13     RRDSET *st;
14
15     // NEEDED BY: do_cpu, do_cpu_cores
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)) {
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 {
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             }
55         }
56      }
57
58     return 0;
59 }