plugin_macos.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_macos.h"
  3. static struct macos_module {
  4. const char *name;
  5. const char *dim;
  6. int enabled;
  7. int (*func)(int update_every, usec_t dt);
  8. usec_t duration;
  9. RRDDIM *rd;
  10. } macos_modules[] = {
  11. {.name = "sysctl", .dim = "sysctl", .enabled = 1, .func = do_macos_sysctl},
  12. {.name = "mach system management interface", .dim = "mach_smi", .enabled = 1, .func = do_macos_mach_smi},
  13. {.name = "iokit", .dim = "iokit", .enabled = 1, .func = do_macos_iokit},
  14. // the terminator of this array
  15. {.name = NULL, .dim = NULL, .enabled = 0, .func = NULL}
  16. };
  17. static void macos_main_cleanup(void *ptr)
  18. {
  19. struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
  20. static_thread->enabled = NETDATA_MAIN_THREAD_EXITING;
  21. info("cleaning up...");
  22. static_thread->enabled = NETDATA_MAIN_THREAD_EXITED;
  23. }
  24. void *macos_main(void *ptr)
  25. {
  26. netdata_thread_cleanup_push(macos_main_cleanup, ptr);
  27. int vdo_cpu_netdata = config_get_boolean("plugin:macos", "netdata server resources", CONFIG_BOOLEAN_YES);
  28. // check the enabled status for each module
  29. for (int i = 0; macos_modules[i].name; i++) {
  30. struct macos_module *pm = &macos_modules[i];
  31. pm->enabled = config_get_boolean("plugin:macos", pm->name, pm->enabled);
  32. pm->duration = 0ULL;
  33. pm->rd = NULL;
  34. }
  35. usec_t step = localhost->rrd_update_every * USEC_PER_SEC;
  36. heartbeat_t hb;
  37. heartbeat_init(&hb);
  38. while (!netdata_exit) {
  39. usec_t hb_dt = heartbeat_next(&hb, step);
  40. usec_t duration = 0ULL;
  41. // BEGIN -- the job to be done
  42. for (int i = 0; macos_modules[i].name; i++) {
  43. struct macos_module *pm = &macos_modules[i];
  44. if (unlikely(!pm->enabled))
  45. continue;
  46. debug(D_PROCNETDEV_LOOP, "macos calling %s.", pm->name);
  47. pm->enabled = !pm->func(localhost->rrd_update_every, hb_dt);
  48. pm->duration = heartbeat_monotonic_dt_to_now_usec(&hb) - duration;
  49. duration += pm->duration;
  50. if (unlikely(netdata_exit))
  51. break;
  52. }
  53. // END -- the job is done
  54. if (vdo_cpu_netdata) {
  55. static RRDSET *st_cpu_thread = NULL, *st_duration = NULL;
  56. static RRDDIM *rd_user = NULL, *rd_system = NULL;
  57. // ----------------------------------------------------------------
  58. struct rusage thread;
  59. getrusage(RUSAGE_THREAD, &thread);
  60. if (unlikely(!st_cpu_thread)) {
  61. st_cpu_thread = rrdset_create_localhost(
  62. "netdata",
  63. "plugin_macos_cpu",
  64. NULL,
  65. "macos",
  66. NULL,
  67. "Netdata macOS plugin CPU usage",
  68. "milliseconds/s",
  69. "macos.plugin",
  70. "stats",
  71. 132000,
  72. localhost->rrd_update_every,
  73. RRDSET_TYPE_STACKED);
  74. rd_user = rrddim_add(st_cpu_thread, "user", NULL, 1, USEC_PER_MS, RRD_ALGORITHM_INCREMENTAL);
  75. rd_system = rrddim_add(st_cpu_thread, "system", NULL, 1, USEC_PER_MS, RRD_ALGORITHM_INCREMENTAL);
  76. } else {
  77. rrdset_next(st_cpu_thread);
  78. }
  79. rrddim_set_by_pointer(
  80. st_cpu_thread, rd_user, thread.ru_utime.tv_sec * USEC_PER_SEC + thread.ru_utime.tv_usec);
  81. rrddim_set_by_pointer(
  82. st_cpu_thread, rd_system, thread.ru_stime.tv_sec * USEC_PER_SEC + thread.ru_stime.tv_usec);
  83. rrdset_done(st_cpu_thread);
  84. // ----------------------------------------------------------------
  85. if (unlikely(!st_duration)) {
  86. st_duration = rrdset_find_active_bytype_localhost("netdata", "plugin_macos_modules");
  87. if (!st_duration) {
  88. st_duration = rrdset_create_localhost(
  89. "netdata",
  90. "plugin_macos_modules",
  91. NULL,
  92. "macos",
  93. NULL,
  94. "Netdata macOS plugin modules durations",
  95. "milliseconds/run",
  96. "macos.plugin",
  97. "stats",
  98. 132001,
  99. localhost->rrd_update_every,
  100. RRDSET_TYPE_STACKED);
  101. for (int i = 0; macos_modules[i].name; i++) {
  102. struct macos_module *pm = &macos_modules[i];
  103. if (unlikely(!pm->enabled))
  104. continue;
  105. pm->rd = rrddim_add(st_duration, pm->dim, NULL, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  106. }
  107. }
  108. } else
  109. rrdset_next(st_duration);
  110. for (int i = 0; macos_modules[i].name; i++) {
  111. struct macos_module *pm = &macos_modules[i];
  112. if (unlikely(!pm->enabled))
  113. continue;
  114. rrddim_set_by_pointer(st_duration, pm->rd, pm->duration);
  115. }
  116. rrdset_done(st_duration);
  117. }
  118. }
  119. netdata_thread_cleanup_pop(1);
  120. return NULL;
  121. }