proc_interrupts.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define PLUGIN_PROC_MODULE_INTERRUPTS_NAME "/proc/interrupts"
  4. #define CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS "plugin:" PLUGIN_PROC_CONFIG_NAME ":" PLUGIN_PROC_MODULE_INTERRUPTS_NAME
  5. #define MAX_INTERRUPT_NAME 50
  6. struct cpu_interrupt {
  7. unsigned long long value;
  8. RRDDIM *rd;
  9. };
  10. struct interrupt {
  11. int used;
  12. char *id;
  13. char name[MAX_INTERRUPT_NAME + 1];
  14. RRDDIM *rd;
  15. unsigned long long total;
  16. struct cpu_interrupt cpu[];
  17. };
  18. // since each interrupt is variable in size
  19. // we use this to calculate its record size
  20. #define recordsize(cpus) (sizeof(struct interrupt) + ((cpus) * sizeof(struct cpu_interrupt)))
  21. // given a base, get a pointer to each record
  22. #define irrindex(base, line, cpus) ((struct interrupt *)&((char *)(base))[(line) * recordsize(cpus)])
  23. static inline struct interrupt *get_interrupts_array(size_t lines, int cpus) {
  24. static struct interrupt *irrs = NULL;
  25. static size_t allocated = 0;
  26. if(unlikely(lines != allocated)) {
  27. size_t l;
  28. int c;
  29. irrs = (struct interrupt *)reallocz(irrs, lines * recordsize(cpus));
  30. // reset all interrupt RRDDIM pointers as any line could have shifted
  31. for(l = 0; l < lines ;l++) {
  32. struct interrupt *irr = irrindex(irrs, l, cpus);
  33. irr->rd = NULL;
  34. irr->name[0] = '\0';
  35. for(c = 0; c < cpus ;c++)
  36. irr->cpu[c].rd = NULL;
  37. }
  38. allocated = lines;
  39. }
  40. return irrs;
  41. }
  42. int do_proc_interrupts(int update_every, usec_t dt) {
  43. (void)dt;
  44. static procfile *ff = NULL;
  45. static int cpus = -1, do_per_core = CONFIG_BOOLEAN_INVALID;
  46. struct interrupt *irrs = NULL;
  47. if(unlikely(do_per_core == CONFIG_BOOLEAN_INVALID))
  48. do_per_core = config_get_boolean_ondemand(CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS, "interrupts per core", CONFIG_BOOLEAN_NO);
  49. if(unlikely(!ff)) {
  50. char filename[FILENAME_MAX + 1];
  51. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/interrupts");
  52. ff = procfile_open(config_get(CONFIG_SECTION_PLUGIN_PROC_INTERRUPTS, "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  53. }
  54. if(unlikely(!ff))
  55. return 1;
  56. ff = procfile_readall(ff);
  57. if(unlikely(!ff))
  58. return 0; // we return 0, so that we will retry to open it next time
  59. size_t lines = procfile_lines(ff), l;
  60. size_t words = procfile_linewords(ff, 0);
  61. if(unlikely(!lines)) {
  62. collector_error("Cannot read /proc/interrupts, zero lines reported.");
  63. return 1;
  64. }
  65. // find how many CPUs are there
  66. if(unlikely(cpus == -1)) {
  67. uint32_t w;
  68. cpus = 0;
  69. for(w = 0; w < words ; w++) {
  70. if(likely(strncmp(procfile_lineword(ff, 0, w), "CPU", 3) == 0))
  71. cpus++;
  72. }
  73. }
  74. if(unlikely(!cpus)) {
  75. collector_error("PLUGIN: PROC_INTERRUPTS: Cannot find the number of CPUs in /proc/interrupts");
  76. return 1;
  77. }
  78. // allocate the size we need;
  79. irrs = get_interrupts_array(lines, cpus);
  80. irrs[0].used = 0;
  81. // loop through all lines
  82. for(l = 1; l < lines ;l++) {
  83. struct interrupt *irr = irrindex(irrs, l, cpus);
  84. irr->used = 0;
  85. irr->total = 0;
  86. words = procfile_linewords(ff, l);
  87. if(unlikely(!words)) continue;
  88. irr->id = procfile_lineword(ff, l, 0);
  89. if(unlikely(!irr->id || !irr->id[0])) continue;
  90. size_t idlen = strlen(irr->id);
  91. if(irr->id[idlen - 1] == ':')
  92. irr->id[--idlen] = '\0';
  93. int c;
  94. for(c = 0; c < cpus ;c++) {
  95. if(likely((c + 1) < (int)words))
  96. irr->cpu[c].value = str2ull(procfile_lineword(ff, l, (uint32_t) (c + 1)), NULL);
  97. else
  98. irr->cpu[c].value = 0;
  99. irr->total += irr->cpu[c].value;
  100. }
  101. if(unlikely(isdigit(irr->id[0]) && (uint32_t)(cpus + 2) < words)) {
  102. strncpyz(irr->name, procfile_lineword(ff, l, words - 1), MAX_INTERRUPT_NAME);
  103. size_t nlen = strlen(irr->name);
  104. if(likely(nlen + 1 + idlen <= MAX_INTERRUPT_NAME)) {
  105. irr->name[nlen] = '_';
  106. strncpyz(&irr->name[nlen + 1], irr->id, MAX_INTERRUPT_NAME - nlen - 1);
  107. }
  108. else {
  109. irr->name[MAX_INTERRUPT_NAME - idlen - 1] = '_';
  110. strncpyz(&irr->name[MAX_INTERRUPT_NAME - idlen], irr->id, idlen);
  111. }
  112. }
  113. else {
  114. strncpyz(irr->name, irr->id, MAX_INTERRUPT_NAME);
  115. }
  116. irr->used = 1;
  117. }
  118. static RRDSET *st_system_interrupts = NULL;
  119. if(unlikely(!st_system_interrupts))
  120. st_system_interrupts = rrdset_create_localhost(
  121. "system"
  122. , "interrupts"
  123. , NULL
  124. , "interrupts"
  125. , NULL
  126. , "System interrupts"
  127. , "interrupts/s"
  128. , PLUGIN_PROC_NAME
  129. , PLUGIN_PROC_MODULE_INTERRUPTS_NAME
  130. , NETDATA_CHART_PRIO_SYSTEM_INTERRUPTS
  131. , update_every
  132. , RRDSET_TYPE_STACKED
  133. );
  134. for(l = 0; l < lines ;l++) {
  135. struct interrupt *irr = irrindex(irrs, l, cpus);
  136. if(irr->used && irr->total) {
  137. // some interrupt may have changed without changing the total number of lines
  138. // if the same number of interrupts have been added and removed between two
  139. // calls of this function.
  140. if(unlikely(!irr->rd || strncmp(rrddim_name(irr->rd), irr->name, MAX_INTERRUPT_NAME) != 0)) {
  141. irr->rd = rrddim_add(st_system_interrupts, irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  142. rrddim_reset_name(st_system_interrupts, irr->rd, irr->name);
  143. // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
  144. if(likely(do_per_core != CONFIG_BOOLEAN_NO)) {
  145. int c;
  146. for(c = 0; c < cpus; c++) irr->cpu[c].rd = NULL;
  147. }
  148. }
  149. rrddim_set_by_pointer(st_system_interrupts, irr->rd, irr->total);
  150. }
  151. }
  152. rrdset_done(st_system_interrupts);
  153. if(likely(do_per_core != CONFIG_BOOLEAN_NO)) {
  154. static RRDSET **core_st = NULL;
  155. static int old_cpus = 0;
  156. if(old_cpus < cpus) {
  157. core_st = reallocz(core_st, sizeof(RRDSET *) * cpus);
  158. memset(&core_st[old_cpus], 0, sizeof(RRDSET *) * (cpus - old_cpus));
  159. old_cpus = cpus;
  160. }
  161. int c;
  162. for(c = 0; c < cpus ;c++) {
  163. if(unlikely(!core_st[c])) {
  164. char id[50+1];
  165. snprintfz(id, sizeof(id) - 1, "cpu%d_interrupts", c);
  166. char title[100+1];
  167. snprintfz(title, sizeof(title) - 1, "CPU Interrupts");
  168. core_st[c] = rrdset_create_localhost(
  169. "cpu"
  170. , id
  171. , NULL
  172. , "interrupts"
  173. , "cpu.interrupts"
  174. , title
  175. , "interrupts/s"
  176. , PLUGIN_PROC_NAME
  177. , PLUGIN_PROC_MODULE_INTERRUPTS_NAME
  178. , NETDATA_CHART_PRIO_INTERRUPTS_PER_CORE + c
  179. , update_every
  180. , RRDSET_TYPE_STACKED
  181. );
  182. char core[50+1];
  183. snprintfz(core, sizeof(core) - 1, "cpu%d", c);
  184. rrdlabels_add(core_st[c]->rrdlabels, "cpu", core, RRDLABEL_SRC_AUTO);
  185. }
  186. for(l = 0; l < lines ;l++) {
  187. struct interrupt *irr = irrindex(irrs, l, cpus);
  188. if(irr->used && (do_per_core == CONFIG_BOOLEAN_YES || irr->cpu[c].value)) {
  189. if(unlikely(!irr->cpu[c].rd)) {
  190. irr->cpu[c].rd = rrddim_add(core_st[c], irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  191. rrddim_reset_name(core_st[c], irr->cpu[c].rd, irr->name);
  192. }
  193. rrddim_set_by_pointer(core_st[c], irr->cpu[c].rd, irr->cpu[c].value);
  194. }
  195. }
  196. rrdset_done(core_st[c]);
  197. }
  198. }
  199. return 0;
  200. }