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