| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "plugin_macos.h" |
| 4 | |
| 5 | #include <mach/mach.h> |
| 6 | |
| 7 | int do_macos_mach_smi(int update_every, usec_t dt) { |
| 8 | (void)dt; |
| 9 | |
| 10 | static int do_cpu = -1, do_ram = - 1, do_swapio = -1, do_pgfaults = -1; |
| 11 | |
| 12 | if (unlikely(do_cpu == -1)) { |
| 13 | do_cpu = inicfg_get_boolean(&netdata_config, "plugin:macos:mach_smi", "cpu utilization", 1); |
| 14 | do_ram = inicfg_get_boolean(&netdata_config, "plugin:macos:mach_smi", "system ram", 1); |
| 15 | do_swapio = inicfg_get_boolean(&netdata_config, "plugin:macos:mach_smi", "swap i/o", 1); |
| 16 | do_pgfaults = inicfg_get_boolean(&netdata_config, "plugin:macos:mach_smi", "memory page faults", 1); |
| 17 | } |
| 18 | |
| 19 | RRDSET *st; |
| 20 | |
| 21 | kern_return_t kr; |
| 22 | mach_msg_type_number_t count; |
| 23 | host_t host; |
| 24 | vm_size_t system_pagesize; |
| 25 | |
| 26 | |
| 27 | // NEEDED BY: do_cpu |
| 28 | natural_t cp_time[CPU_STATE_MAX]; |
| 29 | |
| 30 | // NEEDED BY: do_ram, do_swapio, do_pgfaults |
| 31 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) |
| 32 | vm_statistics64_data_t vm_statistics; |
| 33 | #else |
| 34 | vm_statistics_data_t vm_statistics; |
| 35 | #endif |
| 36 | |
| 37 | host = mach_host_self(); |
| 38 | kr = host_page_size(host, &system_pagesize); |
| 39 | if (unlikely(kr != KERN_SUCCESS)) |
| 40 | return -1; |
| 41 | |
| 42 | if (likely(do_cpu)) { |
| 43 | if (unlikely(HOST_CPU_LOAD_INFO_COUNT != 4)) { |
| 44 | collector_error("MACOS: There are %d CPU states (4 was expected)", HOST_CPU_LOAD_INFO_COUNT); |
| 45 | do_cpu = 0; |
| 46 | collector_error("DISABLED: system.cpu"); |
| 47 | } else { |
| 48 | count = HOST_CPU_LOAD_INFO_COUNT; |
| 49 | kr = host_statistics(host, HOST_CPU_LOAD_INFO, (host_info_t)cp_time, &count); |
| 50 | if (unlikely(kr != KERN_SUCCESS)) { |
| 51 | collector_error("MACOS: host_statistics() failed: %s", mach_error_string(kr)); |
| 52 | do_cpu = 0; |
| 53 | collector_error("DISABLED: system.cpu"); |
| 54 | } else { |
| 55 | |
| 56 | st = rrdset_find_active_bytype_localhost("system", "cpu"); |
| 57 | if (unlikely(!st)) { |
| 58 | st = rrdset_create_localhost( |
| 59 | "system" |
| 60 | , "cpu" |
| 61 | , NULL |
| 62 | , "cpu" |
| 63 | , "system.cpu" |
| 64 | , "Total CPU utilization" |
| 65 | , "percentage" |
| 66 | , "macos.plugin" |
| 67 | , "mach_smi" |
| 68 | , 100 |
| 69 | , update_every |
| 70 | , RRDSET_TYPE_STACKED |
| 71 | ); |
| 72 | |
| 73 | rrddim_add(st, "user", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL); |
| 74 | rrddim_add(st, "nice", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL); |
| 75 | rrddim_add(st, "system", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL); |
| 76 | rrddim_add(st, "idle", NULL, 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL); |
| 77 | rrddim_hide(st, "idle"); |
| 78 | } |
| 79 | |
| 80 | rrddim_set(st, "user", cp_time[CPU_STATE_USER]); |
| 81 | rrddim_set(st, "nice", cp_time[CPU_STATE_NICE]); |
| 82 | rrddim_set(st, "system", cp_time[CPU_STATE_SYSTEM]); |
| 83 | rrddim_set(st, "idle", cp_time[CPU_STATE_IDLE]); |
| 84 | rrdset_done(st); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (likely(do_ram || do_swapio || do_pgfaults)) { |
| 90 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1060) |
| 91 | count = HOST_VM_INFO64_COUNT; |
| 92 | kr = host_statistics64(host, HOST_VM_INFO64, (host_info64_t)&vm_statistics, &count); |
| 93 | #else |
| 94 | count = HOST_VM_INFO_COUNT; |
| 95 | kr = host_statistics(host, HOST_VM_INFO, (host_info_t)&vm_statistics, &count); |
| 96 | #endif |
| 97 | if (unlikely(kr != KERN_SUCCESS)) { |
| 98 | collector_error("MACOS: host_statistics64() failed: %s", mach_error_string(kr)); |
| 99 | do_ram = 0; |
| 100 | collector_error("DISABLED: system.ram"); |
| 101 | do_swapio = 0; |
| 102 | collector_error("DISABLED: mem.swapio"); |
| 103 | do_pgfaults = 0; |
| 104 | collector_error("DISABLED: mem.pgfaults"); |
| 105 | } else { |
| 106 | if (likely(do_ram)) { |
| 107 | st = rrdset_find_active_localhost("system.ram"); |
| 108 | if (unlikely(!st)) { |
| 109 | st = rrdset_create_localhost( |
| 110 | "system" |
| 111 | , "ram" |
| 112 | , NULL |
| 113 | , "ram" |
| 114 | , NULL |
| 115 | , "System RAM" |
| 116 | , "MiB" |
| 117 | , "macos.plugin" |
| 118 | , "mach_smi" |
| 119 | , 200 |
| 120 | , update_every |
| 121 | , RRDSET_TYPE_STACKED |
| 122 | ); |
| 123 | |
| 124 | rrddim_add(st, "active", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 125 | rrddim_add(st, "wired", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 126 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) |
| 127 | rrddim_add(st, "throttled", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 128 | rrddim_add(st, "compressor", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 129 | #endif |
| 130 | rrddim_add(st, "inactive", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 131 | rrddim_add(st, "purgeable", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 132 | rrddim_add(st, "speculative", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 133 | rrddim_add(st, "free", NULL, system_pagesize, 1048576, RRD_ALGORITHM_ABSOLUTE); |
| 134 | } |
| 135 | |
| 136 | rrddim_set(st, "active", vm_statistics.active_count); |
| 137 | rrddim_set(st, "wired", vm_statistics.wire_count); |
| 138 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) |
| 139 | rrddim_set(st, "throttled", vm_statistics.throttled_count); |
| 140 | rrddim_set(st, "compressor", vm_statistics.compressor_page_count); |
| 141 | #endif |
| 142 | rrddim_set(st, "inactive", vm_statistics.inactive_count); |
| 143 | rrddim_set(st, "purgeable", vm_statistics.purgeable_count); |
| 144 | rrddim_set(st, "speculative", vm_statistics.speculative_count); |
| 145 | rrddim_set(st, "free", (vm_statistics.free_count - vm_statistics.speculative_count)); |
| 146 | rrdset_done(st); |
| 147 | } |
| 148 | |
| 149 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) |
| 150 | if (likely(do_swapio)) { |
| 151 | st = rrdset_find_active_localhost("mem.swapio"); |
| 152 | if (unlikely(!st)) { |
| 153 | st = rrdset_create_localhost( |
| 154 | "mem" |
| 155 | , "swapio" |
| 156 | , NULL |
| 157 | , "swap" |
| 158 | , NULL |
| 159 | , "Swap I/O" |
| 160 | , "KiB/s" |
| 161 | , "macos.plugin" |
| 162 | , "mach_smi" |
| 163 | , 250 |
| 164 | , update_every |
| 165 | , RRDSET_TYPE_AREA |
| 166 | ); |
| 167 | |
| 168 | rrddim_add(st, "in", NULL, system_pagesize, 1024, RRD_ALGORITHM_INCREMENTAL); |
| 169 | rrddim_add(st, "out", NULL, -system_pagesize, 1024, RRD_ALGORITHM_INCREMENTAL); |
| 170 | } |
| 171 | |
| 172 | rrddim_set(st, "in", vm_statistics.swapins); |
| 173 | rrddim_set(st, "out", vm_statistics.swapouts); |
| 174 | rrdset_done(st); |
| 175 | } |
| 176 | #endif |
| 177 | |
| 178 | if (likely(do_pgfaults)) { |
| 179 | st = rrdset_find_active_localhost("mem.pgfaults"); |
| 180 | if (unlikely(!st)) { |
| 181 | st = rrdset_create_localhost( |
| 182 | "mem" |
| 183 | , "pgfaults" |
| 184 | , NULL |
| 185 | , "system" |
| 186 | , NULL |
| 187 | , "Memory Page Faults" |
| 188 | , "faults/s" |
| 189 | , "macos.plugin" |
| 190 | , "mach_smi" |
| 191 | , NETDATA_CHART_PRIO_MEM_SYSTEM_PGFAULTS |
| 192 | , update_every |
| 193 | , RRDSET_TYPE_LINE |
| 194 | ); |
| 195 | |
| 196 | rrddim_add(st, "memory", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 197 | rrddim_add(st, "cow", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 198 | rrddim_add(st, "pagein", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 199 | rrddim_add(st, "pageout", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 200 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) |
| 201 | rrddim_add(st, "compress", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 202 | rrddim_add(st, "decompress", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 203 | #endif |
| 204 | rrddim_add(st, "zero_fill", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 205 | rrddim_add(st, "reactivate", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 206 | rrddim_add(st, "purge", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL); |
| 207 | } |
| 208 | |
| 209 | rrddim_set(st, "memory", vm_statistics.faults); |
| 210 | rrddim_set(st, "cow", vm_statistics.cow_faults); |
| 211 | rrddim_set(st, "pagein", vm_statistics.pageins); |
| 212 | rrddim_set(st, "pageout", vm_statistics.pageouts); |
| 213 | #if (defined __MAC_OS_X_VERSION_MIN_REQUIRED && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090) |
| 214 | rrddim_set(st, "compress", vm_statistics.compressions); |
| 215 | rrddim_set(st, "decompress", vm_statistics.decompressions); |
| 216 | #endif |
| 217 | rrddim_set(st, "zero_fill", vm_statistics.zero_fill_count); |
| 218 | rrddim_set(st, "reactivate", vm_statistics.reactivations); |
| 219 | rrddim_set(st, "purge", vm_statistics.purges); |
| 220 | rrdset_done(st); |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | return 0; |
| 226 | } |