proc_vmstat.c 11 KB

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