]> arthur.barton.de Git - netdata.git/blob - src/macos_mach_smi.c
dns_query_time plugin: replace "." with "_" in dimensions
[netdata.git] / src / macos_mach_smi.c
1 #include "common.h"
2 #include <mach/mach.h>
3
4 int do_macos_mach_smi(int update_every, usec_t dt) {
5     (void)dt;
6
7     static int do_cpu = -1, do_ram = - 1, do_swapio = -1, do_pgfaults = -1;
8
9     if (unlikely(do_cpu == -1)) {
10         do_cpu                  = config_get_boolean("plugin:macos:mach_smi", "cpu utilization", 1);
11         do_ram                  = config_get_boolean("plugin:macos:mach_smi", "system ram", 1);
12         do_swapio               = config_get_boolean("plugin:macos:mach_smi", "swap i/o", 1);
13         do_pgfaults             = config_get_boolean("plugin:macos:mach_smi", "memory page faults", 1);
14     }
15
16     RRDSET *st;
17
18         kern_return_t kr;
19         mach_msg_type_number_t count;
20     host_t host;
21     vm_size_t system_pagesize;
22
23
24     // NEEDED BY: do_cpu
25     natural_t cp_time[CPU_STATE_MAX];
26
27     // NEEDED BY: do_ram, do_swapio, do_pgfaults
28     vm_statistics64_data_t vm_statistics;
29
30     host = mach_host_self();
31     kr = host_page_size(host, &system_pagesize);
32     if (unlikely(kr != KERN_SUCCESS))
33         return -1;
34
35     // --------------------------------------------------------------------
36
37     if (likely(do_cpu)) {
38         if (unlikely(HOST_CPU_LOAD_INFO_COUNT != 4)) {
39             error("MACOS: There are %d CPU states (4 was expected)", HOST_CPU_LOAD_INFO_COUNT);
40             do_cpu = 0;
41             error("DISABLED: system.cpu");
42         } else {
43             count = HOST_CPU_LOAD_INFO_COUNT;
44             kr = host_statistics(host, HOST_CPU_LOAD_INFO, (host_info_t)cp_time, &count);
45             if (unlikely(kr != KERN_SUCCESS)) {
46                 error("MACOS: host_statistics() failed: %s", mach_error_string(kr));
47                 do_cpu = 0;
48                 error("DISABLED: system.cpu");
49             } else {
50
51                 st = rrdset_find_bytype_localhost("system", "cpu");
52                 if (unlikely(!st)) {
53                     st = rrdset_create_localhost("system", "cpu", NULL, "cpu", "system.cpu", "Total CPU utilization"
54                                                  , "percentage", 100, update_every, RRDSET_TYPE_STACKED);
55
56                     rrddim_add(st, "user", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
57                     rrddim_add(st, "nice", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
58                     rrddim_add(st, "system", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
59                     rrddim_add(st, "idle", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
60                     rrddim_hide(st, "idle");
61                 }
62                 else rrdset_next(st);
63
64                 rrddim_set(st, "user", cp_time[CPU_STATE_USER]);
65                 rrddim_set(st, "nice", cp_time[CPU_STATE_NICE]);
66                 rrddim_set(st, "system", cp_time[CPU_STATE_SYSTEM]);
67                 rrddim_set(st, "idle", cp_time[CPU_STATE_IDLE]);
68                 rrdset_done(st);
69             }
70         }
71      }
72
73     // --------------------------------------------------------------------
74     
75     if (likely(do_ram || do_swapio || do_pgfaults)) {
76         count = sizeof(vm_statistics64_data_t);
77         kr = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_statistics, &count);
78         if (unlikely(kr != KERN_SUCCESS)) {
79             error("MACOS: host_statistics64() failed: %s", mach_error_string(kr));
80             do_ram = 0;
81             error("DISABLED: system.ram");
82             do_swapio = 0;
83             error("DISABLED: system.swapio");
84             do_pgfaults = 0;
85             error("DISABLED: mem.pgfaults");
86         } else {
87             if (likely(do_ram)) {
88                 st = rrdset_find_localhost("system.ram");
89                 if (unlikely(!st)) {
90                     st = rrdset_create_localhost("system", "ram", NULL, "ram", NULL, "System RAM", "MB", 200
91                                                  , update_every, RRDSET_TYPE_STACKED);
92
93                     rrddim_add(st, "active",    NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
94                     rrddim_add(st, "wired",     NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
95                     rrddim_add(st, "throttled", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
96                     rrddim_add(st, "compressor", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
97                     rrddim_add(st, "inactive",  NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
98                     rrddim_add(st, "purgeable", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
99                     rrddim_add(st, "speculative", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
100                     rrddim_add(st, "free",      NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE);
101                 }
102                 else rrdset_next(st);
103
104                 rrddim_set(st, "active",    vm_statistics.active_count);
105                 rrddim_set(st, "wired",     vm_statistics.wire_count);
106                 rrddim_set(st, "throttled", vm_statistics.throttled_count);
107                 rrddim_set(st, "compressor", vm_statistics.compressor_page_count);
108                 rrddim_set(st, "inactive",  vm_statistics.inactive_count);
109                 rrddim_set(st, "purgeable", vm_statistics.purgeable_count);
110                 rrddim_set(st, "speculative", vm_statistics.speculative_count);
111                 rrddim_set(st, "free",      (vm_statistics.free_count - vm_statistics.speculative_count));
112                 rrdset_done(st);
113             }
114
115             // --------------------------------------------------------------------
116
117             if (likely(do_swapio)) {
118                 st = rrdset_find_localhost("system.swapio");
119                 if (unlikely(!st)) {
120                     st = rrdset_create_localhost("system", "swapio", NULL, "swap", NULL, "Swap I/O", "kilobytes/s", 250
121                                                  , update_every, RRDSET_TYPE_AREA);
122
123                     rrddim_add(st, "in",  NULL, system_pagesize, 1024, RRD_ALGORITHM_INCREMENTAL);
124                     rrddim_add(st, "out", NULL, -system_pagesize, 1024, RRD_ALGORITHM_INCREMENTAL);
125                 }
126                 else rrdset_next(st);
127
128                 rrddim_set(st, "in", vm_statistics.swapins);
129                 rrddim_set(st, "out", vm_statistics.swapouts);
130                 rrdset_done(st);
131             }
132
133             // --------------------------------------------------------------------
134
135             if (likely(do_pgfaults)) {
136                 st = rrdset_find_localhost("mem.pgfaults");
137                 if (unlikely(!st)) {
138                     st = rrdset_create_localhost("mem", "pgfaults", NULL, "system", NULL, "Memory Page Faults"
139                                                  , "page faults/s", 500, update_every, RRDSET_TYPE_LINE);
140                     rrdset_flag_set(st, RRDSET_FLAG_DETAIL);
141
142                     rrddim_add(st, "memory",    NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
143                     rrddim_add(st, "cow",       NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
144                     rrddim_add(st, "pagein",    NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
145                     rrddim_add(st, "pageout",   NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
146                     rrddim_add(st, "compress",  NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
147                     rrddim_add(st, "decompress", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
148                     rrddim_add(st, "zero_fill", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
149                     rrddim_add(st, "reactivate", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
150                     rrddim_add(st, "purge",     NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
151                 }
152                 else rrdset_next(st);
153
154                 rrddim_set(st, "memory", vm_statistics.faults);
155                 rrddim_set(st, "cow", vm_statistics.cow_faults);
156                 rrddim_set(st, "pagein", vm_statistics.pageins);
157                 rrddim_set(st, "pageout", vm_statistics.pageouts);
158                 rrddim_set(st, "compress", vm_statistics.compressions);
159                 rrddim_set(st, "decompress", vm_statistics.decompressions);
160                 rrddim_set(st, "zero_fill", vm_statistics.zero_fill_count);
161                 rrddim_set(st, "reactivate", vm_statistics.reactivations);
162                 rrddim_set(st, "purge", vm_statistics.purges);
163                 rrdset_done(st);
164             }
165         }
166     } 
167  
168     // --------------------------------------------------------------------
169
170     return 0;
171 }