]> arthur.barton.de Git - netdata.git/commitdiff
Add CPU usage chart to macOS plugin
authorVladimir Kobal <vlad@prokk.net>
Mon, 26 Dec 2016 19:23:40 +0000 (21:23 +0200)
committerVladimir Kobal <vlad@prokk.net>
Mon, 26 Dec 2016 19:23:40 +0000 (21:23 +0200)
src/macos_mach_smi.c

index a10e5add59c9db9c17f8a05c8a0c67b17d9172ed..a8d241e7ad55604d456e48260a7d1c2d2a116451 100644 (file)
@@ -1,4 +1,5 @@
 #include "common.h"
+#include <mach/mach_host.h>
 
 int do_macos_mach_smi(int update_every, usec_t dt) {
     (void)dt;
@@ -12,35 +13,45 @@ int do_macos_mach_smi(int update_every, usec_t dt) {
     RRDSET *st;
 
     // NEEDED BY: do_cpu, do_cpu_cores
-    long cp_time[5];
+    natural_t cp_time[CPU_STATE_MAX];
+       kern_return_t kr;
+       mach_msg_type_number_t count;
 
     // --------------------------------------------------------------------
 
     if (likely(do_cpu)) {
-        if (unlikely(1)) {
+        if (unlikely(HOST_CPU_LOAD_INFO_COUNT != 4)) {
+            error("FREEBSD: There are %d CPU states (4 was expected)", HOST_CPU_LOAD_INFO_COUNT);
             do_cpu = 0;
             error("DISABLED: system.cpu");
         } else {
-
-            st = rrdset_find_bytype("system", "cpu");
-            if (unlikely(!st)) {
-                st = rrdset_create("system", "cpu", NULL, "cpu", "system.cpu", "Total CPU utilization", "percentage", 100, update_every, RRDSET_TYPE_STACKED);
-
-                rrddim_add(st, "user", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
-                rrddim_add(st, "nice", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
-                rrddim_add(st, "system", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
-                rrddim_add(st, "interrupt", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
-                rrddim_add(st, "idle", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
-                rrddim_hide(st, "idle");
+            count = HOST_CPU_LOAD_INFO_COUNT;
+            if (unlikely(kr = host_statistics(mach_host_self(), HOST_CPU_LOAD_INFO, (host_info_t)cp_time, &count))) {
+                if (kr != KERN_SUCCESS) {
+                    error("MACOS: host_statistics() failed: %s", mach_error_string(kr));
+                    do_cpu = 0;
+                    error("DISABLED: system.cpu");
+                }
+            } else {
+
+                st = rrdset_find_bytype("system", "cpu");
+                if (unlikely(!st)) {
+                    st = rrdset_create("system", "cpu", NULL, "cpu", "system.cpu", "Total CPU utilization", "percentage", 100, update_every, RRDSET_TYPE_STACKED);
+
+                    rrddim_add(st, "user", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
+                    rrddim_add(st, "nice", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
+                    rrddim_add(st, "system", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
+                    rrddim_add(st, "idle", NULL, 1, 1, RRDDIM_PCENT_OVER_DIFF_TOTAL);
+                    rrddim_hide(st, "idle");
+                }
+                else rrdset_next(st);
+
+                rrddim_set(st, "user", cp_time[CPU_STATE_USER]);
+                rrddim_set(st, "nice", cp_time[CPU_STATE_NICE]);
+                rrddim_set(st, "system", cp_time[CPU_STATE_SYSTEM]);
+                rrddim_set(st, "idle", cp_time[CPU_STATE_IDLE]);
+                rrdset_done(st);
             }
-            else rrdset_next(st);
-
-            rrddim_set(st, "user", cp_time[0]);
-            rrddim_set(st, "nice", cp_time[1]);
-            rrddim_set(st, "system", cp_time[2]);
-            rrddim_set(st, "interrupt", cp_time[3]);
-            rrddim_set(st, "idle", cp_time[4]);
-            rrdset_done(st);
         }
      }