proc_interrupts.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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_AUTO);
  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. 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. 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)));
  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. // --------------------------------------------------------------------
  119. static RRDSET *st_system_interrupts = NULL;
  120. if(unlikely(!st_system_interrupts))
  121. st_system_interrupts = rrdset_create_localhost(
  122. "system"
  123. , "interrupts"
  124. , NULL
  125. , "interrupts"
  126. , NULL
  127. , "System interrupts"
  128. , "interrupts/s"
  129. , PLUGIN_PROC_NAME
  130. , PLUGIN_PROC_MODULE_INTERRUPTS_NAME
  131. , NETDATA_CHART_PRIO_SYSTEM_INTERRUPTS
  132. , update_every
  133. , RRDSET_TYPE_STACKED
  134. );
  135. else
  136. rrdset_next(st_system_interrupts);
  137. for(l = 0; l < lines ;l++) {
  138. struct interrupt *irr = irrindex(irrs, l, cpus);
  139. if(irr->used && irr->total) {
  140. // some interrupt may have changed without changing the total number of lines
  141. // if the same number of interrupts have been added and removed between two
  142. // calls of this function.
  143. if(unlikely(!irr->rd || strncmp(irr->rd->name, irr->name, MAX_INTERRUPT_NAME) != 0)) {
  144. irr->rd = rrddim_add(st_system_interrupts, irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  145. rrddim_set_name(st_system_interrupts, irr->rd, irr->name);
  146. // also reset per cpu RRDDIMs to avoid repeating strncmp() in the per core loop
  147. if(likely(do_per_core != CONFIG_BOOLEAN_NO)) {
  148. int c;
  149. for(c = 0; c < cpus; c++) irr->cpu[c].rd = NULL;
  150. }
  151. }
  152. rrddim_set_by_pointer(st_system_interrupts, irr->rd, irr->total);
  153. }
  154. }
  155. rrdset_done(st_system_interrupts);
  156. // --------------------------------------------------------------------
  157. if(likely(do_per_core != CONFIG_BOOLEAN_NO)) {
  158. static RRDSET **core_st = NULL;
  159. static int old_cpus = 0;
  160. if(old_cpus < cpus) {
  161. core_st = reallocz(core_st, sizeof(RRDSET *) * cpus);
  162. memset(&core_st[old_cpus], 0, sizeof(RRDSET *) * (cpus - old_cpus));
  163. old_cpus = cpus;
  164. }
  165. int c;
  166. for(c = 0; c < cpus ;c++) {
  167. if(unlikely(!core_st[c])) {
  168. char id[50+1];
  169. snprintfz(id, 50, "cpu%d_interrupts", c);
  170. char title[100+1];
  171. snprintfz(title, 100, "CPU Interrupts");
  172. core_st[c] = rrdset_create_localhost(
  173. "cpu"
  174. , id
  175. , NULL
  176. , "interrupts"
  177. , "cpu.interrupts"
  178. , title
  179. , "interrupts/s"
  180. , PLUGIN_PROC_NAME
  181. , PLUGIN_PROC_MODULE_INTERRUPTS_NAME
  182. , NETDATA_CHART_PRIO_INTERRUPTS_PER_CORE + c
  183. , update_every
  184. , RRDSET_TYPE_STACKED
  185. );
  186. }
  187. else rrdset_next(core_st[c]);
  188. for(l = 0; l < lines ;l++) {
  189. struct interrupt *irr = irrindex(irrs, l, cpus);
  190. if(irr->used && (do_per_core == CONFIG_BOOLEAN_YES || irr->cpu[c].value)) {
  191. if(unlikely(!irr->cpu[c].rd)) {
  192. irr->cpu[c].rd = rrddim_add(core_st[c], irr->id, irr->name, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  193. rrddim_set_name(core_st[c], irr->cpu[c].rd, irr->name);
  194. }
  195. rrddim_set_by_pointer(core_st[c], irr->cpu[c].rd, irr->cpu[c].value);
  196. }
  197. }
  198. rrdset_done(core_st[c]);
  199. }
  200. }
  201. return 0;
  202. }