proc_vmstat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define PLUGIN_PROC_MODULE_VMSTAT_NAME "/proc/vmstat"
  4. #define OOM_KILL_STRING "oom_kill"
  5. int do_proc_vmstat(int update_every, usec_t dt) {
  6. (void)dt;
  7. static procfile *ff = NULL;
  8. static int do_swapio = -1, do_io = -1, do_pgfaults = -1, do_oom_kill = -1, do_numa = -1;
  9. static int has_numa = -1;
  10. static ARL_BASE *arl_base = NULL;
  11. static unsigned long long numa_foreign = 0ULL;
  12. static unsigned long long numa_hint_faults = 0ULL;
  13. static unsigned long long numa_hint_faults_local = 0ULL;
  14. static unsigned long long numa_huge_pte_updates = 0ULL;
  15. static unsigned long long numa_interleave = 0ULL;
  16. static unsigned long long numa_local = 0ULL;
  17. static unsigned long long numa_other = 0ULL;
  18. static unsigned long long numa_pages_migrated = 0ULL;
  19. static unsigned long long numa_pte_updates = 0ULL;
  20. static unsigned long long pgfault = 0ULL;
  21. static unsigned long long pgmajfault = 0ULL;
  22. static unsigned long long pgpgin = 0ULL;
  23. static unsigned long long pgpgout = 0ULL;
  24. static unsigned long long pswpin = 0ULL;
  25. static unsigned long long pswpout = 0ULL;
  26. static unsigned long long oom_kill = 0ULL;
  27. if(unlikely(!ff)) {
  28. char filename[FILENAME_MAX + 1];
  29. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/vmstat");
  30. ff = procfile_open(config_get("plugin:proc:/proc/vmstat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  31. if(unlikely(!ff)) return 1;
  32. }
  33. ff = procfile_readall(ff);
  34. if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
  35. size_t lines = procfile_lines(ff), l;
  36. if(unlikely(!arl_base)) {
  37. do_swapio = config_get_boolean_ondemand("plugin:proc:/proc/vmstat", "swap i/o", CONFIG_BOOLEAN_AUTO);
  38. do_io = config_get_boolean("plugin:proc:/proc/vmstat", "disk i/o", CONFIG_BOOLEAN_YES);
  39. do_pgfaults = config_get_boolean("plugin:proc:/proc/vmstat", "memory page faults", CONFIG_BOOLEAN_YES);
  40. do_oom_kill = config_get_boolean("plugin:proc:/proc/vmstat", "out of memory kills", CONFIG_BOOLEAN_AUTO);
  41. do_numa = config_get_boolean_ondemand("plugin:proc:/proc/vmstat", "system-wide numa metric summary", CONFIG_BOOLEAN_AUTO);
  42. arl_base = arl_create("vmstat", NULL, 60);
  43. arl_expect(arl_base, "pgfault", &pgfault);
  44. arl_expect(arl_base, "pgmajfault", &pgmajfault);
  45. arl_expect(arl_base, "pgpgin", &pgpgin);
  46. arl_expect(arl_base, "pgpgout", &pgpgout);
  47. arl_expect(arl_base, "pswpin", &pswpin);
  48. arl_expect(arl_base, "pswpout", &pswpout);
  49. int has_oom_kill = 0;
  50. for (l = 0; l < lines; l++) {
  51. if (!strcmp(procfile_lineword(ff, l, 0), OOM_KILL_STRING)) {
  52. has_oom_kill = 1;
  53. break;
  54. }
  55. }
  56. if (has_oom_kill)
  57. arl_expect(arl_base, OOM_KILL_STRING, &oom_kill);
  58. else
  59. do_oom_kill = CONFIG_BOOLEAN_NO;
  60. if(do_numa == CONFIG_BOOLEAN_YES || (do_numa == CONFIG_BOOLEAN_AUTO &&
  61. (get_numa_node_count() >= 2 ||
  62. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  63. arl_expect(arl_base, "numa_foreign", &numa_foreign);
  64. arl_expect(arl_base, "numa_hint_faults_local", &numa_hint_faults_local);
  65. arl_expect(arl_base, "numa_hint_faults", &numa_hint_faults);
  66. arl_expect(arl_base, "numa_huge_pte_updates", &numa_huge_pte_updates);
  67. arl_expect(arl_base, "numa_interleave", &numa_interleave);
  68. arl_expect(arl_base, "numa_local", &numa_local);
  69. arl_expect(arl_base, "numa_other", &numa_other);
  70. arl_expect(arl_base, "numa_pages_migrated", &numa_pages_migrated);
  71. arl_expect(arl_base, "numa_pte_updates", &numa_pte_updates);
  72. }
  73. else {
  74. // Do not expect numa metrics when they are not needed.
  75. // By not adding them, the ARL will stop processing the file
  76. // when all the expected metrics are collected.
  77. // Also ARL will not parse their values.
  78. has_numa = 0;
  79. do_numa = CONFIG_BOOLEAN_NO;
  80. }
  81. }
  82. arl_begin(arl_base);
  83. for(l = 0; l < lines ;l++) {
  84. size_t words = procfile_linewords(ff, l);
  85. if(unlikely(words < 2)) {
  86. if(unlikely(words)) error("Cannot read /proc/vmstat line %zu. Expected 2 params, read %zu.", l, words);
  87. continue;
  88. }
  89. if(unlikely(arl_check(arl_base,
  90. procfile_lineword(ff, l, 0),
  91. procfile_lineword(ff, l, 1)))) break;
  92. }
  93. // --------------------------------------------------------------------
  94. if(do_swapio == CONFIG_BOOLEAN_YES || (do_swapio == CONFIG_BOOLEAN_AUTO &&
  95. (pswpin || pswpout ||
  96. netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  97. do_swapio = CONFIG_BOOLEAN_YES;
  98. static RRDSET *st_swapio = NULL;
  99. static RRDDIM *rd_in = NULL, *rd_out = NULL;
  100. if(unlikely(!st_swapio)) {
  101. st_swapio = rrdset_create_localhost(
  102. "system"
  103. , "swapio"
  104. , NULL
  105. , "swap"
  106. , NULL
  107. , "Swap I/O"
  108. , "KiB/s"
  109. , PLUGIN_PROC_NAME
  110. , PLUGIN_PROC_MODULE_VMSTAT_NAME
  111. , NETDATA_CHART_PRIO_SYSTEM_SWAPIO
  112. , update_every
  113. , RRDSET_TYPE_AREA
  114. );
  115. rd_in = rrddim_add(st_swapio, "in", NULL, sysconf(_SC_PAGESIZE), 1024, RRD_ALGORITHM_INCREMENTAL);
  116. rd_out = rrddim_add(st_swapio, "out", NULL, -sysconf(_SC_PAGESIZE), 1024, RRD_ALGORITHM_INCREMENTAL);
  117. }
  118. else rrdset_next(st_swapio);
  119. rrddim_set_by_pointer(st_swapio, rd_in, pswpin);
  120. rrddim_set_by_pointer(st_swapio, rd_out, pswpout);
  121. rrdset_done(st_swapio);
  122. }
  123. // --------------------------------------------------------------------
  124. if(do_io) {
  125. static RRDSET *st_io = NULL;
  126. static RRDDIM *rd_in = NULL, *rd_out = NULL;
  127. if(unlikely(!st_io)) {
  128. st_io = rrdset_create_localhost(
  129. "system"
  130. , "pgpgio"
  131. , NULL
  132. , "disk"
  133. , NULL
  134. , "Memory Paged from/to disk"
  135. , "KiB/s"
  136. , PLUGIN_PROC_NAME
  137. , PLUGIN_PROC_MODULE_VMSTAT_NAME
  138. , NETDATA_CHART_PRIO_SYSTEM_PGPGIO
  139. , update_every
  140. , RRDSET_TYPE_AREA
  141. );
  142. rd_in = rrddim_add(st_io, "in", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  143. rd_out = rrddim_add(st_io, "out", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  144. }
  145. else rrdset_next(st_io);
  146. rrddim_set_by_pointer(st_io, rd_in, pgpgin);
  147. rrddim_set_by_pointer(st_io, rd_out, pgpgout);
  148. rrdset_done(st_io);
  149. }
  150. // --------------------------------------------------------------------
  151. if(do_pgfaults) {
  152. static RRDSET *st_pgfaults = NULL;
  153. static RRDDIM *rd_minor = NULL, *rd_major = NULL;
  154. if(unlikely(!st_pgfaults)) {
  155. st_pgfaults = rrdset_create_localhost(
  156. "mem"
  157. , "pgfaults"
  158. , NULL
  159. , "system"
  160. , NULL
  161. , "Memory Page Faults"
  162. , "faults/s"
  163. , PLUGIN_PROC_NAME
  164. , PLUGIN_PROC_MODULE_VMSTAT_NAME
  165. , NETDATA_CHART_PRIO_MEM_SYSTEM_PGFAULTS
  166. , update_every
  167. , RRDSET_TYPE_LINE
  168. );
  169. rrdset_flag_set(st_pgfaults, RRDSET_FLAG_DETAIL);
  170. rd_minor = rrddim_add(st_pgfaults, "minor", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  171. rd_major = rrddim_add(st_pgfaults, "major", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
  172. }
  173. else rrdset_next(st_pgfaults);
  174. rrddim_set_by_pointer(st_pgfaults, rd_minor, pgfault);
  175. rrddim_set_by_pointer(st_pgfaults, rd_major, pgmajfault);
  176. rrdset_done(st_pgfaults);
  177. }
  178. // --------------------------------------------------------------------
  179. if (do_oom_kill == CONFIG_BOOLEAN_YES ||
  180. (do_oom_kill == CONFIG_BOOLEAN_AUTO && (oom_kill || netdata_zero_metrics_enabled == CONFIG_BOOLEAN_YES))) {
  181. static RRDSET *st_oom_kill = NULL;
  182. static RRDDIM *rd_oom_kill = NULL;
  183. do_oom_kill = CONFIG_BOOLEAN_YES;
  184. if(unlikely(!st_oom_kill)) {
  185. st_oom_kill = rrdset_create_localhost(
  186. "mem"
  187. , "oom_kill"
  188. , NULL
  189. , "system"
  190. , NULL
  191. , "Out of Memory Kills"
  192. , "kills/s"
  193. , PLUGIN_PROC_NAME
  194. , PLUGIN_PROC_MODULE_VMSTAT_NAME
  195. , NETDATA_CHART_PRIO_MEM_SYSTEM_OOM_KILL
  196. , update_every
  197. , RRDSET_TYPE_LINE
  198. );
  199. rrdset_flag_set(st_oom_kill, RRDSET_FLAG_DETAIL);
  200. rd_oom_kill = rrddim_add(st_oom_kill, "kills", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  201. }
  202. else rrdset_next(st_oom_kill);
  203. rrddim_set_by_pointer(st_oom_kill, rd_oom_kill, oom_kill);
  204. rrdset_done(st_oom_kill);
  205. }
  206. // --------------------------------------------------------------------
  207. // Ondemand criteria for NUMA. Since this won't change at run time, we
  208. // check it only once. We check whether the node count is >= 2 because
  209. // single-node systems have uninteresting statistics (since all accesses
  210. // are local).
  211. if(unlikely(has_numa == -1))
  212. has_numa = (numa_local || numa_foreign || numa_interleave || numa_other || numa_pte_updates ||
  213. numa_huge_pte_updates || numa_hint_faults || numa_hint_faults_local || numa_pages_migrated) ? 1 : 0;
  214. if(do_numa == CONFIG_BOOLEAN_YES || (do_numa == CONFIG_BOOLEAN_AUTO && has_numa)) {
  215. do_numa = CONFIG_BOOLEAN_YES;
  216. static RRDSET *st_numa = NULL;
  217. static RRDDIM *rd_local = NULL, *rd_foreign = NULL, *rd_interleave = NULL, *rd_other = NULL, *rd_pte_updates = NULL, *rd_huge_pte_updates = NULL, *rd_hint_faults = NULL, *rd_hint_faults_local = NULL, *rd_pages_migrated = NULL;
  218. if(unlikely(!st_numa)) {
  219. st_numa = rrdset_create_localhost(
  220. "mem"
  221. , "numa"
  222. , NULL
  223. , "numa"
  224. , NULL
  225. , "NUMA events"
  226. , "events/s"
  227. , PLUGIN_PROC_NAME
  228. , PLUGIN_PROC_MODULE_VMSTAT_NAME
  229. , NETDATA_CHART_PRIO_MEM_NUMA
  230. , update_every
  231. , RRDSET_TYPE_LINE
  232. );
  233. rrdset_flag_set(st_numa, RRDSET_FLAG_DETAIL);
  234. // These depend on CONFIG_NUMA in the kernel.
  235. rd_local = rrddim_add(st_numa, "local", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  236. rd_foreign = rrddim_add(st_numa, "foreign", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  237. rd_interleave = rrddim_add(st_numa, "interleave", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  238. rd_other = rrddim_add(st_numa, "other", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  239. // The following stats depend on CONFIG_NUMA_BALANCING in the
  240. // kernel.
  241. rd_pte_updates = rrddim_add(st_numa, "pte_updates", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  242. rd_huge_pte_updates = rrddim_add(st_numa, "huge_pte_updates", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  243. rd_hint_faults = rrddim_add(st_numa, "hint_faults", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  244. rd_hint_faults_local = rrddim_add(st_numa, "hint_faults_local", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  245. rd_pages_migrated = rrddim_add(st_numa, "pages_migrated", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  246. }
  247. else rrdset_next(st_numa);
  248. rrddim_set_by_pointer(st_numa, rd_local, numa_local);
  249. rrddim_set_by_pointer(st_numa, rd_foreign, numa_foreign);
  250. rrddim_set_by_pointer(st_numa, rd_interleave, numa_interleave);
  251. rrddim_set_by_pointer(st_numa, rd_other, numa_other);
  252. rrddim_set_by_pointer(st_numa, rd_pte_updates, numa_pte_updates);
  253. rrddim_set_by_pointer(st_numa, rd_huge_pte_updates, numa_huge_pte_updates);
  254. rrddim_set_by_pointer(st_numa, rd_hint_faults, numa_hint_faults);
  255. rrddim_set_by_pointer(st_numa, rd_hint_faults_local, numa_hint_faults_local);
  256. rrddim_set_by_pointer(st_numa, rd_pages_migrated, numa_pages_migrated);
  257. rrdset_done(st_numa);
  258. }
  259. return 0;
  260. }