proc_stat.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "plugin_proc.h"
  3. #define PLUGIN_PROC_MODULE_STAT_NAME "/proc/stat"
  4. struct per_core_single_number_file {
  5. unsigned char found:1;
  6. const char *filename;
  7. int fd;
  8. collected_number value;
  9. RRDDIM *rd;
  10. };
  11. struct last_ticks {
  12. collected_number frequency;
  13. collected_number ticks;
  14. };
  15. // This is an extension of struct per_core_single_number_file at CPU_FREQ_INDEX.
  16. // Either scaling_cur_freq or time_in_state file is used at one time.
  17. struct per_core_time_in_state_file {
  18. const char *filename;
  19. procfile *ff;
  20. size_t last_ticks_len;
  21. struct last_ticks *last_ticks;
  22. };
  23. #define CORE_THROTTLE_COUNT_INDEX 0
  24. #define PACKAGE_THROTTLE_COUNT_INDEX 1
  25. #define CPU_FREQ_INDEX 2
  26. #define PER_CORE_FILES 3
  27. struct cpu_chart {
  28. const char *id;
  29. RRDSET *st;
  30. RRDDIM *rd_user;
  31. RRDDIM *rd_nice;
  32. RRDDIM *rd_system;
  33. RRDDIM *rd_idle;
  34. RRDDIM *rd_iowait;
  35. RRDDIM *rd_irq;
  36. RRDDIM *rd_softirq;
  37. RRDDIM *rd_steal;
  38. RRDDIM *rd_guest;
  39. RRDDIM *rd_guest_nice;
  40. struct per_core_single_number_file files[PER_CORE_FILES];
  41. struct per_core_time_in_state_file time_in_state_files;
  42. };
  43. static int keep_per_core_fds_open = CONFIG_BOOLEAN_YES;
  44. static int keep_cpuidle_fds_open = CONFIG_BOOLEAN_YES;
  45. static int read_per_core_files(struct cpu_chart *all_cpu_charts, size_t len, size_t index) {
  46. char buf[50 + 1];
  47. size_t x, files_read = 0, files_nonzero = 0;
  48. for(x = 0; x < len ; x++) {
  49. struct per_core_single_number_file *f = &all_cpu_charts[x].files[index];
  50. f->found = 0;
  51. if(unlikely(!f->filename))
  52. continue;
  53. if(unlikely(f->fd == -1)) {
  54. f->fd = open(f->filename, O_RDONLY);
  55. if (unlikely(f->fd == -1)) {
  56. error("Cannot open file '%s'", f->filename);
  57. continue;
  58. }
  59. }
  60. ssize_t ret = read(f->fd, buf, 50);
  61. if(unlikely(ret < 0)) {
  62. // cannot read that file
  63. error("Cannot read file '%s'", f->filename);
  64. close(f->fd);
  65. f->fd = -1;
  66. continue;
  67. }
  68. else {
  69. // successful read
  70. // terminate the buffer
  71. buf[ret] = '\0';
  72. if(unlikely(keep_per_core_fds_open != CONFIG_BOOLEAN_YES)) {
  73. close(f->fd);
  74. f->fd = -1;
  75. }
  76. else if(lseek(f->fd, 0, SEEK_SET) == -1) {
  77. error("Cannot seek in file '%s'", f->filename);
  78. close(f->fd);
  79. f->fd = -1;
  80. }
  81. }
  82. files_read++;
  83. f->found = 1;
  84. f->value = str2ll(buf, NULL);
  85. if(likely(f->value != 0))
  86. files_nonzero++;
  87. }
  88. if(files_read == 0)
  89. return -1;
  90. if(files_nonzero == 0)
  91. return 0;
  92. return (int)files_nonzero;
  93. }
  94. static int read_per_core_time_in_state_files(struct cpu_chart *all_cpu_charts, size_t len, size_t index) {
  95. size_t x, files_read = 0, files_nonzero = 0;
  96. for(x = 0; x < len ; x++) {
  97. struct per_core_single_number_file *f = &all_cpu_charts[x].files[index];
  98. struct per_core_time_in_state_file *tsf = &all_cpu_charts[x].time_in_state_files;
  99. f->found = 0;
  100. if(unlikely(!tsf->filename))
  101. continue;
  102. if(unlikely(!tsf->ff)) {
  103. tsf->ff = procfile_open(tsf->filename, " \t:", PROCFILE_FLAG_DEFAULT);
  104. if(unlikely(!tsf->ff))
  105. {
  106. error("Cannot open file '%s'", tsf->filename);
  107. continue;
  108. }
  109. }
  110. tsf->ff = procfile_readall(tsf->ff);
  111. if(unlikely(!tsf->ff)) {
  112. error("Cannot read file '%s'", tsf->filename);
  113. procfile_close(tsf->ff);
  114. tsf->ff = NULL;
  115. continue;
  116. }
  117. else {
  118. // successful read
  119. size_t lines = procfile_lines(tsf->ff), l;
  120. size_t words;
  121. unsigned long long total_ticks_since_last = 0, avg_freq = 0;
  122. // Check if there is at least one frequency in time_in_state
  123. if (procfile_word(tsf->ff, 0)[0] == '\0') {
  124. if(unlikely(keep_per_core_fds_open != CONFIG_BOOLEAN_YES)) {
  125. procfile_close(tsf->ff);
  126. tsf->ff = NULL;
  127. }
  128. // TODO: Is there a better way to avoid spikes than calculating the average over
  129. // the whole period under schedutil governor?
  130. // freez(tsf->last_ticks);
  131. // tsf->last_ticks = NULL;
  132. // tsf->last_ticks_len = 0;
  133. continue;
  134. }
  135. if (unlikely(tsf->last_ticks_len < lines || tsf->last_ticks == NULL)) {
  136. tsf->last_ticks = reallocz(tsf->last_ticks, sizeof(struct last_ticks) * lines);
  137. memset(tsf->last_ticks, 0, sizeof(struct last_ticks) * lines);
  138. tsf->last_ticks_len = lines;
  139. }
  140. f->value = 0;
  141. for(l = 0; l < lines - 1 ;l++) {
  142. unsigned long long frequency = 0, ticks = 0, ticks_since_last = 0;
  143. words = procfile_linewords(tsf->ff, l);
  144. if(unlikely(words < 2)) {
  145. error("Cannot read time_in_state line. Expected 2 params, read %zu.", words);
  146. continue;
  147. }
  148. frequency = str2ull(procfile_lineword(tsf->ff, l, 0));
  149. ticks = str2ull(procfile_lineword(tsf->ff, l, 1));
  150. // It is assumed that frequencies are static and sorted
  151. ticks_since_last = ticks - tsf->last_ticks[l].ticks;
  152. tsf->last_ticks[l].frequency = frequency;
  153. tsf->last_ticks[l].ticks = ticks;
  154. total_ticks_since_last += ticks_since_last;
  155. avg_freq += frequency * ticks_since_last;
  156. }
  157. if (likely(total_ticks_since_last)) {
  158. avg_freq /= total_ticks_since_last;
  159. f->value = avg_freq;
  160. }
  161. if(unlikely(keep_per_core_fds_open != CONFIG_BOOLEAN_YES)) {
  162. procfile_close(tsf->ff);
  163. tsf->ff = NULL;
  164. }
  165. }
  166. files_read++;
  167. f->found = 1;
  168. if(likely(f->value != 0))
  169. files_nonzero++;
  170. }
  171. if(unlikely(files_read == 0))
  172. return -1;
  173. if(unlikely(files_nonzero == 0))
  174. return 0;
  175. return (int)files_nonzero;
  176. }
  177. static void chart_per_core_files(struct cpu_chart *all_cpu_charts, size_t len, size_t index, RRDSET *st, collected_number multiplier, collected_number divisor, RRD_ALGORITHM algorithm) {
  178. size_t x;
  179. for(x = 0; x < len ; x++) {
  180. struct per_core_single_number_file *f = &all_cpu_charts[x].files[index];
  181. if(unlikely(!f->found))
  182. continue;
  183. if(unlikely(!f->rd))
  184. f->rd = rrddim_add(st, all_cpu_charts[x].id, NULL, multiplier, divisor, algorithm);
  185. rrddim_set_by_pointer(st, f->rd, f->value);
  186. }
  187. }
  188. struct cpuidle_state {
  189. char *name;
  190. char *time_filename;
  191. int time_fd;
  192. collected_number value;
  193. RRDDIM *rd;
  194. };
  195. struct per_core_cpuidle_chart {
  196. RRDSET *st;
  197. RRDDIM *active_time_rd;
  198. collected_number active_time;
  199. collected_number last_active_time;
  200. struct cpuidle_state *cpuidle_state;
  201. size_t cpuidle_state_len;
  202. int rescan_cpu_states;
  203. };
  204. static void* wake_cpu_thread(void* core) {
  205. pthread_t thread;
  206. cpu_set_t cpu_set;
  207. static size_t cpu_wakeups = 0;
  208. static int errors = 0;
  209. CPU_ZERO(&cpu_set);
  210. CPU_SET(*(int*)core, &cpu_set);
  211. thread = pthread_self();
  212. if(unlikely(pthread_setaffinity_np(thread, sizeof(cpu_set_t), &cpu_set))) {
  213. if(unlikely(errors < 8)) {
  214. error("Cannot set CPU affinity for core %d", *(int*)core);
  215. errors++;
  216. }
  217. else if(unlikely(errors < 9)) {
  218. error("CPU affinity errors are disabled");
  219. errors++;
  220. }
  221. }
  222. // Make the CPU core do something to force it to update its idle counters
  223. cpu_wakeups++;
  224. return 0;
  225. }
  226. static int read_schedstat(char *schedstat_filename, struct per_core_cpuidle_chart **cpuidle_charts_address, size_t *schedstat_cores_found) {
  227. static size_t cpuidle_charts_len = 0;
  228. static procfile *ff = NULL;
  229. struct per_core_cpuidle_chart *cpuidle_charts = *cpuidle_charts_address;
  230. size_t cores_found = 0;
  231. if(unlikely(!ff)) {
  232. ff = procfile_open(schedstat_filename, " \t:", PROCFILE_FLAG_DEFAULT);
  233. if(unlikely(!ff)) return 1;
  234. }
  235. ff = procfile_readall(ff);
  236. if(unlikely(!ff)) return 1;
  237. size_t lines = procfile_lines(ff), l;
  238. size_t words;
  239. for(l = 0; l < lines ;l++) {
  240. char *row_key = procfile_lineword(ff, l, 0);
  241. // faster strncmp(row_key, "cpu", 3) == 0
  242. if(likely(row_key[0] == 'c' && row_key[1] == 'p' && row_key[2] == 'u')) {
  243. words = procfile_linewords(ff, l);
  244. if(unlikely(words < 10)) {
  245. error("Cannot read /proc/schedstat cpu line. Expected 9 params, read %zu.", words);
  246. return 1;
  247. }
  248. cores_found++;
  249. size_t core = str2ul(&row_key[3]);
  250. if(unlikely(core >= cores_found)) {
  251. error("Core %zu found but no more than %zu cores were expected.", core, cores_found);
  252. return 1;
  253. }
  254. if(unlikely(cpuidle_charts_len < cores_found)) {
  255. cpuidle_charts = reallocz(cpuidle_charts, sizeof(struct per_core_cpuidle_chart) * cores_found);
  256. *cpuidle_charts_address = cpuidle_charts;
  257. memset(cpuidle_charts + cpuidle_charts_len, 0, sizeof(struct per_core_cpuidle_chart) * (cores_found - cpuidle_charts_len));
  258. cpuidle_charts_len = cores_found;
  259. }
  260. cpuidle_charts[core].active_time = str2ull(procfile_lineword(ff, l, 7)) / 1000;
  261. }
  262. }
  263. *schedstat_cores_found = cores_found;
  264. return 0;
  265. }
  266. static int read_one_state(char *buf, const char *filename, int *fd) {
  267. ssize_t ret = read(*fd, buf, 50);
  268. if(unlikely(ret <= 0)) {
  269. // cannot read that file
  270. error("Cannot read file '%s'", filename);
  271. close(*fd);
  272. *fd = -1;
  273. return 0;
  274. }
  275. else {
  276. // successful read
  277. // terminate the buffer
  278. buf[ret - 1] = '\0';
  279. if(unlikely(keep_cpuidle_fds_open != CONFIG_BOOLEAN_YES)) {
  280. close(*fd);
  281. *fd = -1;
  282. }
  283. else if(lseek(*fd, 0, SEEK_SET) == -1) {
  284. error("Cannot seek in file '%s'", filename);
  285. close(*fd);
  286. *fd = -1;
  287. }
  288. }
  289. return 1;
  290. }
  291. static int read_cpuidle_states(char *cpuidle_name_filename , char *cpuidle_time_filename, struct per_core_cpuidle_chart *cpuidle_charts, size_t core) {
  292. char filename[FILENAME_MAX + 1];
  293. static char next_state_filename[FILENAME_MAX + 1];
  294. struct stat stbuf;
  295. struct per_core_cpuidle_chart *cc = &cpuidle_charts[core];
  296. size_t state;
  297. if(unlikely(!cc->cpuidle_state_len || cc->rescan_cpu_states)) {
  298. int state_file_found = 1; // check at least one state
  299. if(cc->cpuidle_state_len) {
  300. for(state = 0; state < cc->cpuidle_state_len; state++) {
  301. freez(cc->cpuidle_state[state].name);
  302. freez(cc->cpuidle_state[state].time_filename);
  303. close(cc->cpuidle_state[state].time_fd);
  304. cc->cpuidle_state[state].time_fd = -1;
  305. }
  306. freez(cc->cpuidle_state);
  307. cc->cpuidle_state = NULL;
  308. cc->cpuidle_state_len = 0;
  309. cc->active_time_rd = NULL;
  310. cc->st = NULL;
  311. }
  312. while(likely(state_file_found)) {
  313. snprintfz(filename, FILENAME_MAX, cpuidle_name_filename, core, cc->cpuidle_state_len);
  314. if (stat(filename, &stbuf) == 0)
  315. cc->cpuidle_state_len++;
  316. else
  317. state_file_found = 0;
  318. }
  319. snprintfz(next_state_filename, FILENAME_MAX, cpuidle_name_filename, core, cc->cpuidle_state_len);
  320. if(likely(cc->cpuidle_state_len))
  321. cc->cpuidle_state = callocz(cc->cpuidle_state_len, sizeof(struct cpuidle_state));
  322. for(state = 0; state < cc->cpuidle_state_len; state++) {
  323. char name_buf[50 + 1];
  324. snprintfz(filename, FILENAME_MAX, cpuidle_name_filename, core, state);
  325. int fd = open(filename, O_RDONLY, 0666);
  326. if(unlikely(fd == -1)) {
  327. error("Cannot open file '%s'", filename);
  328. cc->rescan_cpu_states = 1;
  329. return 1;
  330. }
  331. ssize_t r = read(fd, name_buf, 50);
  332. if(unlikely(r < 1)) {
  333. error("Cannot read file '%s'", filename);
  334. close(fd);
  335. cc->rescan_cpu_states = 1;
  336. return 1;
  337. }
  338. name_buf[r - 1] = '\0'; // erase extra character
  339. cc->cpuidle_state[state].name = strdupz(trim(name_buf));
  340. close(fd);
  341. snprintfz(filename, FILENAME_MAX, cpuidle_time_filename, core, state);
  342. cc->cpuidle_state[state].time_filename = strdupz(filename);
  343. cc->cpuidle_state[state].time_fd = -1;
  344. }
  345. cc->rescan_cpu_states = 0;
  346. }
  347. for(state = 0; state < cc->cpuidle_state_len; state++) {
  348. struct cpuidle_state *cs = &cc->cpuidle_state[state];
  349. if(unlikely(cs->time_fd == -1)) {
  350. cs->time_fd = open(cs->time_filename, O_RDONLY);
  351. if (unlikely(cs->time_fd == -1)) {
  352. error("Cannot open file '%s'", cs->time_filename);
  353. cc->rescan_cpu_states = 1;
  354. return 1;
  355. }
  356. }
  357. char time_buf[50 + 1];
  358. if(likely(read_one_state(time_buf, cs->time_filename, &cs->time_fd))) {
  359. cs->value = str2ll(time_buf, NULL);
  360. }
  361. else {
  362. cc->rescan_cpu_states = 1;
  363. return 1;
  364. }
  365. }
  366. // check if the number of states was increased
  367. if(unlikely(stat(next_state_filename, &stbuf) == 0)) {
  368. cc->rescan_cpu_states = 1;
  369. return 1;
  370. }
  371. return 0;
  372. }
  373. int do_proc_stat(int update_every, usec_t dt) {
  374. (void)dt;
  375. static struct cpu_chart *all_cpu_charts = NULL;
  376. static size_t all_cpu_charts_size = 0;
  377. static procfile *ff = NULL;
  378. static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1,
  379. do_core_throttle_count = -1, do_package_throttle_count = -1, do_cpu_freq = -1, do_cpuidle = -1;
  380. static uint32_t hash_intr, hash_ctxt, hash_processes, hash_procs_running, hash_procs_blocked;
  381. static char *core_throttle_count_filename = NULL, *package_throttle_count_filename = NULL, *scaling_cur_freq_filename = NULL,
  382. *time_in_state_filename = NULL, *schedstat_filename = NULL, *cpuidle_name_filename = NULL, *cpuidle_time_filename = NULL;
  383. static RRDVAR *cpus_var = NULL;
  384. static int accurate_freq_avail = 0, accurate_freq_is_used = 0;
  385. size_t cores_found = (size_t)processors;
  386. if(unlikely(do_cpu == -1)) {
  387. do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", CONFIG_BOOLEAN_YES);
  388. do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", CONFIG_BOOLEAN_YES);
  389. do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", CONFIG_BOOLEAN_YES);
  390. do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", CONFIG_BOOLEAN_YES);
  391. do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", CONFIG_BOOLEAN_YES);
  392. do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", CONFIG_BOOLEAN_YES);
  393. // give sane defaults based on the number of processors
  394. if(unlikely(processors > 50)) {
  395. // the system has too many processors
  396. keep_per_core_fds_open = CONFIG_BOOLEAN_NO;
  397. do_core_throttle_count = CONFIG_BOOLEAN_NO;
  398. do_package_throttle_count = CONFIG_BOOLEAN_NO;
  399. do_cpu_freq = CONFIG_BOOLEAN_NO;
  400. do_cpuidle = CONFIG_BOOLEAN_NO;
  401. }
  402. else {
  403. // the system has a reasonable number of processors
  404. keep_per_core_fds_open = CONFIG_BOOLEAN_YES;
  405. do_core_throttle_count = CONFIG_BOOLEAN_AUTO;
  406. do_package_throttle_count = CONFIG_BOOLEAN_NO;
  407. do_cpu_freq = CONFIG_BOOLEAN_YES;
  408. do_cpuidle = CONFIG_BOOLEAN_YES;
  409. }
  410. if(unlikely(processors > 24)) {
  411. // the system has too many processors
  412. keep_cpuidle_fds_open = CONFIG_BOOLEAN_NO;
  413. }
  414. else {
  415. // the system has a reasonable number of processors
  416. keep_cpuidle_fds_open = CONFIG_BOOLEAN_YES;
  417. }
  418. keep_per_core_fds_open = config_get_boolean("plugin:proc:/proc/stat", "keep per core files open", keep_per_core_fds_open);
  419. keep_cpuidle_fds_open = config_get_boolean("plugin:proc:/proc/stat", "keep cpuidle files open", keep_cpuidle_fds_open);
  420. do_core_throttle_count = config_get_boolean_ondemand("plugin:proc:/proc/stat", "core_throttle_count", do_core_throttle_count);
  421. do_package_throttle_count = config_get_boolean_ondemand("plugin:proc:/proc/stat", "package_throttle_count", do_package_throttle_count);
  422. do_cpu_freq = config_get_boolean_ondemand("plugin:proc:/proc/stat", "cpu frequency", do_cpu_freq);
  423. do_cpuidle = config_get_boolean_ondemand("plugin:proc:/proc/stat", "cpu idle states", do_cpuidle);
  424. hash_intr = simple_hash("intr");
  425. hash_ctxt = simple_hash("ctxt");
  426. hash_processes = simple_hash("processes");
  427. hash_procs_running = simple_hash("procs_running");
  428. hash_procs_blocked = simple_hash("procs_blocked");
  429. char filename[FILENAME_MAX + 1];
  430. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/cpu/%s/thermal_throttle/core_throttle_count");
  431. core_throttle_count_filename = config_get("plugin:proc:/proc/stat", "core_throttle_count filename to monitor", filename);
  432. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/cpu/%s/thermal_throttle/package_throttle_count");
  433. package_throttle_count_filename = config_get("plugin:proc:/proc/stat", "package_throttle_count filename to monitor", filename);
  434. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/cpu/%s/cpufreq/scaling_cur_freq");
  435. scaling_cur_freq_filename = config_get("plugin:proc:/proc/stat", "scaling_cur_freq filename to monitor", filename);
  436. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/cpu/%s/cpufreq/stats/time_in_state");
  437. time_in_state_filename = config_get("plugin:proc:/proc/stat", "time_in_state filename to monitor", filename);
  438. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/schedstat");
  439. schedstat_filename = config_get("plugin:proc:/proc/stat", "schedstat filename to monitor", filename);
  440. if(do_cpuidle != CONFIG_BOOLEAN_NO) {
  441. struct stat stbuf;
  442. if (stat(schedstat_filename, &stbuf))
  443. do_cpuidle = CONFIG_BOOLEAN_NO;
  444. }
  445. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/cpu/cpu%zu/cpuidle/state%zu/name");
  446. cpuidle_name_filename = config_get("plugin:proc:/proc/stat", "cpuidle name filename to monitor", filename);
  447. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/sys/devices/system/cpu/cpu%zu/cpuidle/state%zu/time");
  448. cpuidle_time_filename = config_get("plugin:proc:/proc/stat", "cpuidle time filename to monitor", filename);
  449. }
  450. if(unlikely(!ff)) {
  451. char filename[FILENAME_MAX + 1];
  452. snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/stat");
  453. ff = procfile_open(config_get("plugin:proc:/proc/stat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
  454. if(unlikely(!ff)) return 1;
  455. }
  456. ff = procfile_readall(ff);
  457. if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
  458. size_t lines = procfile_lines(ff), l;
  459. size_t words;
  460. unsigned long long processes = 0, running = 0 , blocked = 0;
  461. for(l = 0; l < lines ;l++) {
  462. char *row_key = procfile_lineword(ff, l, 0);
  463. uint32_t hash = simple_hash(row_key);
  464. // faster strncmp(row_key, "cpu", 3) == 0
  465. if(likely(row_key[0] == 'c' && row_key[1] == 'p' && row_key[2] == 'u')) {
  466. words = procfile_linewords(ff, l);
  467. if(unlikely(words < 9)) {
  468. error("Cannot read /proc/stat cpu line. Expected 9 params, read %zu.", words);
  469. continue;
  470. }
  471. size_t core = (row_key[3] == '\0') ? 0 : str2ul(&row_key[3]) + 1;
  472. if(likely(core > 0)) cores_found = core;
  473. if(likely((core == 0 && do_cpu) || (core > 0 && do_cpu_cores))) {
  474. char *id;
  475. unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
  476. id = row_key;
  477. user = str2ull(procfile_lineword(ff, l, 1));
  478. nice = str2ull(procfile_lineword(ff, l, 2));
  479. system = str2ull(procfile_lineword(ff, l, 3));
  480. idle = str2ull(procfile_lineword(ff, l, 4));
  481. iowait = str2ull(procfile_lineword(ff, l, 5));
  482. irq = str2ull(procfile_lineword(ff, l, 6));
  483. softirq = str2ull(procfile_lineword(ff, l, 7));
  484. steal = str2ull(procfile_lineword(ff, l, 8));
  485. guest = str2ull(procfile_lineword(ff, l, 9));
  486. user -= guest;
  487. guest_nice = str2ull(procfile_lineword(ff, l, 10));
  488. nice -= guest_nice;
  489. char *title, *type, *context, *family;
  490. long priority;
  491. if(unlikely(core >= all_cpu_charts_size)) {
  492. size_t old_cpu_charts_size = all_cpu_charts_size;
  493. all_cpu_charts_size = core + 1;
  494. all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * all_cpu_charts_size);
  495. memset(&all_cpu_charts[old_cpu_charts_size], 0, sizeof(struct cpu_chart) * (all_cpu_charts_size - old_cpu_charts_size));
  496. }
  497. struct cpu_chart *cpu_chart = &all_cpu_charts[core];
  498. if(unlikely(!cpu_chart->st)) {
  499. cpu_chart->id = strdupz(id);
  500. if(unlikely(core == 0)) {
  501. title = "Total CPU utilization";
  502. type = "system";
  503. context = "system.cpu";
  504. family = id;
  505. priority = NETDATA_CHART_PRIO_SYSTEM_CPU;
  506. }
  507. else {
  508. title = "Core utilization";
  509. type = "cpu";
  510. context = "cpu.cpu";
  511. family = "utilization";
  512. priority = NETDATA_CHART_PRIO_CPU_PER_CORE;
  513. char filename[FILENAME_MAX + 1];
  514. struct stat stbuf;
  515. if(do_core_throttle_count != CONFIG_BOOLEAN_NO) {
  516. snprintfz(filename, FILENAME_MAX, core_throttle_count_filename, id);
  517. if (stat(filename, &stbuf) == 0) {
  518. cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
  519. cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].fd = -1;
  520. do_core_throttle_count = CONFIG_BOOLEAN_YES;
  521. }
  522. }
  523. if(do_package_throttle_count != CONFIG_BOOLEAN_NO) {
  524. snprintfz(filename, FILENAME_MAX, package_throttle_count_filename, id);
  525. if (stat(filename, &stbuf) == 0) {
  526. cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
  527. cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].fd = -1;
  528. do_package_throttle_count = CONFIG_BOOLEAN_YES;
  529. }
  530. }
  531. if(do_cpu_freq != CONFIG_BOOLEAN_NO) {
  532. snprintfz(filename, FILENAME_MAX, scaling_cur_freq_filename, id);
  533. if (stat(filename, &stbuf) == 0) {
  534. cpu_chart->files[CPU_FREQ_INDEX].filename = strdupz(filename);
  535. cpu_chart->files[CPU_FREQ_INDEX].fd = -1;
  536. do_cpu_freq = CONFIG_BOOLEAN_YES;
  537. }
  538. snprintfz(filename, FILENAME_MAX, time_in_state_filename, id);
  539. if (stat(filename, &stbuf) == 0) {
  540. cpu_chart->time_in_state_files.filename = strdupz(filename);
  541. cpu_chart->time_in_state_files.ff = NULL;
  542. do_cpu_freq = CONFIG_BOOLEAN_YES;
  543. accurate_freq_avail = 1;
  544. }
  545. }
  546. }
  547. cpu_chart->st = rrdset_create_localhost(
  548. type
  549. , id
  550. , NULL
  551. , family
  552. , context
  553. , title
  554. , "percentage"
  555. , PLUGIN_PROC_NAME
  556. , PLUGIN_PROC_MODULE_STAT_NAME
  557. , priority + core
  558. , update_every
  559. , RRDSET_TYPE_STACKED
  560. );
  561. long multiplier = 1;
  562. long divisor = 1; // sysconf(_SC_CLK_TCK);
  563. cpu_chart->rd_guest_nice = rrddim_add(cpu_chart->st, "guest_nice", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  564. cpu_chart->rd_guest = rrddim_add(cpu_chart->st, "guest", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  565. cpu_chart->rd_steal = rrddim_add(cpu_chart->st, "steal", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  566. cpu_chart->rd_softirq = rrddim_add(cpu_chart->st, "softirq", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  567. cpu_chart->rd_irq = rrddim_add(cpu_chart->st, "irq", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  568. cpu_chart->rd_user = rrddim_add(cpu_chart->st, "user", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  569. cpu_chart->rd_system = rrddim_add(cpu_chart->st, "system", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  570. cpu_chart->rd_nice = rrddim_add(cpu_chart->st, "nice", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  571. cpu_chart->rd_iowait = rrddim_add(cpu_chart->st, "iowait", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  572. cpu_chart->rd_idle = rrddim_add(cpu_chart->st, "idle", NULL, multiplier, divisor, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  573. rrddim_hide(cpu_chart->st, "idle");
  574. if(unlikely(core == 0 && cpus_var == NULL))
  575. cpus_var = rrdvar_custom_host_variable_create(localhost, "active_processors");
  576. }
  577. else rrdset_next(cpu_chart->st);
  578. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_user, user);
  579. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_nice, nice);
  580. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_system, system);
  581. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_idle, idle);
  582. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_iowait, iowait);
  583. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_irq, irq);
  584. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_softirq, softirq);
  585. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_steal, steal);
  586. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_guest, guest);
  587. rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_guest_nice, guest_nice);
  588. rrdset_done(cpu_chart->st);
  589. }
  590. }
  591. else if(unlikely(hash == hash_intr && strcmp(row_key, "intr") == 0)) {
  592. if(likely(do_interrupts)) {
  593. static RRDSET *st_intr = NULL;
  594. static RRDDIM *rd_interrupts = NULL;
  595. unsigned long long value = str2ull(procfile_lineword(ff, l, 1));
  596. if(unlikely(!st_intr)) {
  597. st_intr = rrdset_create_localhost(
  598. "system"
  599. , "intr"
  600. , NULL
  601. , "interrupts"
  602. , NULL
  603. , "CPU Interrupts"
  604. , "interrupts/s"
  605. , PLUGIN_PROC_NAME
  606. , PLUGIN_PROC_MODULE_STAT_NAME
  607. , NETDATA_CHART_PRIO_SYSTEM_INTR
  608. , update_every
  609. , RRDSET_TYPE_LINE
  610. );
  611. rrdset_flag_set(st_intr, RRDSET_FLAG_DETAIL);
  612. rd_interrupts = rrddim_add(st_intr, "interrupts", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  613. }
  614. else rrdset_next(st_intr);
  615. rrddim_set_by_pointer(st_intr, rd_interrupts, value);
  616. rrdset_done(st_intr);
  617. }
  618. }
  619. else if(unlikely(hash == hash_ctxt && strcmp(row_key, "ctxt") == 0)) {
  620. if(likely(do_context)) {
  621. static RRDSET *st_ctxt = NULL;
  622. static RRDDIM *rd_switches = NULL;
  623. unsigned long long value = str2ull(procfile_lineword(ff, l, 1));
  624. if(unlikely(!st_ctxt)) {
  625. st_ctxt = rrdset_create_localhost(
  626. "system"
  627. , "ctxt"
  628. , NULL
  629. , "processes"
  630. , NULL
  631. , "CPU Context Switches"
  632. , "context switches/s"
  633. , PLUGIN_PROC_NAME
  634. , PLUGIN_PROC_MODULE_STAT_NAME
  635. , NETDATA_CHART_PRIO_SYSTEM_CTXT
  636. , update_every
  637. , RRDSET_TYPE_LINE
  638. );
  639. rd_switches = rrddim_add(st_ctxt, "switches", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  640. }
  641. else rrdset_next(st_ctxt);
  642. rrddim_set_by_pointer(st_ctxt, rd_switches, value);
  643. rrdset_done(st_ctxt);
  644. }
  645. }
  646. else if(unlikely(hash == hash_processes && !processes && strcmp(row_key, "processes") == 0)) {
  647. processes = str2ull(procfile_lineword(ff, l, 1));
  648. }
  649. else if(unlikely(hash == hash_procs_running && !running && strcmp(row_key, "procs_running") == 0)) {
  650. running = str2ull(procfile_lineword(ff, l, 1));
  651. }
  652. else if(unlikely(hash == hash_procs_blocked && !blocked && strcmp(row_key, "procs_blocked") == 0)) {
  653. blocked = str2ull(procfile_lineword(ff, l, 1));
  654. }
  655. }
  656. // --------------------------------------------------------------------
  657. if(likely(do_forks)) {
  658. static RRDSET *st_forks = NULL;
  659. static RRDDIM *rd_started = NULL;
  660. if(unlikely(!st_forks)) {
  661. st_forks = rrdset_create_localhost(
  662. "system"
  663. , "forks"
  664. , NULL
  665. , "processes"
  666. , NULL
  667. , "Started Processes"
  668. , "processes/s"
  669. , PLUGIN_PROC_NAME
  670. , PLUGIN_PROC_MODULE_STAT_NAME
  671. , NETDATA_CHART_PRIO_SYSTEM_FORKS
  672. , update_every
  673. , RRDSET_TYPE_LINE
  674. );
  675. rrdset_flag_set(st_forks, RRDSET_FLAG_DETAIL);
  676. rd_started = rrddim_add(st_forks, "started", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  677. }
  678. else rrdset_next(st_forks);
  679. rrddim_set_by_pointer(st_forks, rd_started, processes);
  680. rrdset_done(st_forks);
  681. }
  682. // --------------------------------------------------------------------
  683. if(likely(do_processes)) {
  684. static RRDSET *st_processes = NULL;
  685. static RRDDIM *rd_running = NULL;
  686. static RRDDIM *rd_blocked = NULL;
  687. if(unlikely(!st_processes)) {
  688. st_processes = rrdset_create_localhost(
  689. "system"
  690. , "processes"
  691. , NULL
  692. , "processes"
  693. , NULL
  694. , "System Processes"
  695. , "processes"
  696. , PLUGIN_PROC_NAME
  697. , PLUGIN_PROC_MODULE_STAT_NAME
  698. , NETDATA_CHART_PRIO_SYSTEM_PROCESSES
  699. , update_every
  700. , RRDSET_TYPE_LINE
  701. );
  702. rd_running = rrddim_add(st_processes, "running", NULL, 1, 1, RRD_ALGORITHM_ABSOLUTE);
  703. rd_blocked = rrddim_add(st_processes, "blocked", NULL, -1, 1, RRD_ALGORITHM_ABSOLUTE);
  704. }
  705. else rrdset_next(st_processes);
  706. rrddim_set_by_pointer(st_processes, rd_running, running);
  707. rrddim_set_by_pointer(st_processes, rd_blocked, blocked);
  708. rrdset_done(st_processes);
  709. }
  710. if(likely(all_cpu_charts_size > 1)) {
  711. if(likely(do_core_throttle_count != CONFIG_BOOLEAN_NO)) {
  712. int r = read_per_core_files(&all_cpu_charts[1], all_cpu_charts_size - 1, CORE_THROTTLE_COUNT_INDEX);
  713. if(likely(r != -1 && (do_core_throttle_count == CONFIG_BOOLEAN_YES || r > 0))) {
  714. do_core_throttle_count = CONFIG_BOOLEAN_YES;
  715. static RRDSET *st_core_throttle_count = NULL;
  716. if (unlikely(!st_core_throttle_count))
  717. st_core_throttle_count = rrdset_create_localhost(
  718. "cpu"
  719. , "core_throttling"
  720. , NULL
  721. , "throttling"
  722. , "cpu.core_throttling"
  723. , "Core Thermal Throttling Events"
  724. , "events/s"
  725. , PLUGIN_PROC_NAME
  726. , PLUGIN_PROC_MODULE_STAT_NAME
  727. , NETDATA_CHART_PRIO_CORE_THROTTLING
  728. , update_every
  729. , RRDSET_TYPE_LINE
  730. );
  731. else
  732. rrdset_next(st_core_throttle_count);
  733. chart_per_core_files(&all_cpu_charts[1], all_cpu_charts_size - 1, CORE_THROTTLE_COUNT_INDEX, st_core_throttle_count, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  734. rrdset_done(st_core_throttle_count);
  735. }
  736. }
  737. if(likely(do_package_throttle_count != CONFIG_BOOLEAN_NO)) {
  738. int r = read_per_core_files(&all_cpu_charts[1], all_cpu_charts_size - 1, PACKAGE_THROTTLE_COUNT_INDEX);
  739. if(likely(r != -1 && (do_package_throttle_count == CONFIG_BOOLEAN_YES || r > 0))) {
  740. do_package_throttle_count = CONFIG_BOOLEAN_YES;
  741. static RRDSET *st_package_throttle_count = NULL;
  742. if(unlikely(!st_package_throttle_count))
  743. st_package_throttle_count = rrdset_create_localhost(
  744. "cpu"
  745. , "package_throttling"
  746. , NULL
  747. , "throttling"
  748. , "cpu.package_throttling"
  749. , "Package Thermal Throttling Events"
  750. , "events/s"
  751. , PLUGIN_PROC_NAME
  752. , PLUGIN_PROC_MODULE_STAT_NAME
  753. , NETDATA_CHART_PRIO_PACKAGE_THROTTLING
  754. , update_every
  755. , RRDSET_TYPE_LINE
  756. );
  757. else
  758. rrdset_next(st_package_throttle_count);
  759. chart_per_core_files(&all_cpu_charts[1], all_cpu_charts_size - 1, PACKAGE_THROTTLE_COUNT_INDEX, st_package_throttle_count, 1, 1, RRD_ALGORITHM_INCREMENTAL);
  760. rrdset_done(st_package_throttle_count);
  761. }
  762. }
  763. if(likely(do_cpu_freq != CONFIG_BOOLEAN_NO)) {
  764. char filename[FILENAME_MAX + 1];
  765. int r = 0;
  766. if (accurate_freq_avail) {
  767. r = read_per_core_time_in_state_files(&all_cpu_charts[1], all_cpu_charts_size - 1, CPU_FREQ_INDEX);
  768. if(r > 0 && !accurate_freq_is_used) {
  769. accurate_freq_is_used = 1;
  770. snprintfz(filename, FILENAME_MAX, time_in_state_filename, "cpu*");
  771. info("cpufreq is using %s", filename);
  772. }
  773. }
  774. if (r < 1) {
  775. r = read_per_core_files(&all_cpu_charts[1], all_cpu_charts_size - 1, CPU_FREQ_INDEX);
  776. if(accurate_freq_is_used) {
  777. accurate_freq_is_used = 0;
  778. snprintfz(filename, FILENAME_MAX, scaling_cur_freq_filename, "cpu*");
  779. info("cpufreq fell back to %s", filename);
  780. }
  781. }
  782. if(likely(r != -1 && (do_cpu_freq == CONFIG_BOOLEAN_YES || r > 0))) {
  783. do_cpu_freq = CONFIG_BOOLEAN_YES;
  784. static RRDSET *st_scaling_cur_freq = NULL;
  785. if(unlikely(!st_scaling_cur_freq))
  786. st_scaling_cur_freq = rrdset_create_localhost(
  787. "cpu"
  788. , "cpufreq"
  789. , NULL
  790. , "cpufreq"
  791. , "cpufreq.cpufreq"
  792. , "Current CPU Frequency"
  793. , "MHz"
  794. , PLUGIN_PROC_NAME
  795. , PLUGIN_PROC_MODULE_STAT_NAME
  796. , NETDATA_CHART_PRIO_CPUFREQ_SCALING_CUR_FREQ
  797. , update_every
  798. , RRDSET_TYPE_LINE
  799. );
  800. else
  801. rrdset_next(st_scaling_cur_freq);
  802. chart_per_core_files(&all_cpu_charts[1], all_cpu_charts_size - 1, CPU_FREQ_INDEX, st_scaling_cur_freq, 1, 1000, RRD_ALGORITHM_ABSOLUTE);
  803. rrdset_done(st_scaling_cur_freq);
  804. }
  805. }
  806. }
  807. // --------------------------------------------------------------------
  808. static struct per_core_cpuidle_chart *cpuidle_charts = NULL;
  809. size_t schedstat_cores_found = 0;
  810. if(likely(do_cpuidle != CONFIG_BOOLEAN_NO && !read_schedstat(schedstat_filename, &cpuidle_charts, &schedstat_cores_found))) {
  811. int cpu_states_updated = 0;
  812. size_t core, state;
  813. // proc.plugin runs on Linux systems only. Multi-platform compatibility is not needed here,
  814. // so bare pthread functions are used to avoid unneeded overheads.
  815. for(core = 0; core < schedstat_cores_found; core++) {
  816. if(unlikely(!(cpuidle_charts[core].active_time - cpuidle_charts[core].last_active_time))) {
  817. pthread_t thread;
  818. cpu_set_t global_cpu_set;
  819. if (likely(!pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &global_cpu_set))) {
  820. if (unlikely(!CPU_ISSET(core, &global_cpu_set))) {
  821. continue;
  822. }
  823. }
  824. else
  825. error("Cannot read current process affinity");
  826. // These threads are very ephemeral and don't need to have a specific name
  827. if(unlikely(pthread_create(&thread, NULL, wake_cpu_thread, (void *)&core)))
  828. error("Cannot create wake_cpu_thread");
  829. else if(unlikely(pthread_join(thread, NULL)))
  830. error("Cannot join wake_cpu_thread");
  831. cpu_states_updated = 1;
  832. }
  833. }
  834. if(unlikely(!cpu_states_updated || !read_schedstat(schedstat_filename, &cpuidle_charts, &schedstat_cores_found))) {
  835. for(core = 0; core < schedstat_cores_found; core++) {
  836. cpuidle_charts[core].last_active_time = cpuidle_charts[core].active_time;
  837. int r = read_cpuidle_states(cpuidle_name_filename, cpuidle_time_filename, cpuidle_charts, core);
  838. if(likely(r != -1 && (do_cpuidle == CONFIG_BOOLEAN_YES || r > 0))) {
  839. do_cpuidle = CONFIG_BOOLEAN_YES;
  840. char cpuidle_chart_id[RRD_ID_LENGTH_MAX + 1];
  841. snprintfz(cpuidle_chart_id, RRD_ID_LENGTH_MAX, "cpu%zu_cpuidle", core);
  842. if(unlikely(!cpuidle_charts[core].st)) {
  843. cpuidle_charts[core].st = rrdset_create_localhost(
  844. "cpu"
  845. , cpuidle_chart_id
  846. , NULL
  847. , "cpuidle"
  848. , "cpuidle.cpuidle"
  849. , "C-state residency time"
  850. , "percentage"
  851. , PLUGIN_PROC_NAME
  852. , PLUGIN_PROC_MODULE_STAT_NAME
  853. , NETDATA_CHART_PRIO_CPUIDLE + core
  854. , update_every
  855. , RRDSET_TYPE_STACKED
  856. );
  857. char cpuidle_dim_id[RRD_ID_LENGTH_MAX + 1];
  858. snprintfz(cpuidle_dim_id, RRD_ID_LENGTH_MAX, "cpu%zu_active_time", core);
  859. cpuidle_charts[core].active_time_rd = rrddim_add(cpuidle_charts[core].st, cpuidle_dim_id, "C0 (active)", 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  860. for(state = 0; state < cpuidle_charts[core].cpuidle_state_len; state++) {
  861. snprintfz(cpuidle_dim_id, RRD_ID_LENGTH_MAX, "cpu%zu_cpuidle_state%zu_time", core, state);
  862. cpuidle_charts[core].cpuidle_state[state].rd = rrddim_add(cpuidle_charts[core].st, cpuidle_dim_id,
  863. cpuidle_charts[core].cpuidle_state[state].name,
  864. 1, 1, RRD_ALGORITHM_PCENT_OVER_DIFF_TOTAL);
  865. }
  866. }
  867. else
  868. rrdset_next(cpuidle_charts[core].st);
  869. rrddim_set_by_pointer(cpuidle_charts[core].st, cpuidle_charts[core].active_time_rd, cpuidle_charts[core].active_time);
  870. for(state = 0; state < cpuidle_charts[core].cpuidle_state_len; state++) {
  871. rrddim_set_by_pointer(cpuidle_charts[core].st, cpuidle_charts[core].cpuidle_state[state].rd, cpuidle_charts[core].cpuidle_state[state].value);
  872. }
  873. rrdset_done(cpuidle_charts[core].st);
  874. }
  875. }
  876. }
  877. }
  878. if(cpus_var)
  879. rrdvar_custom_host_variable_set(localhost, cpus_var, cores_found);
  880. return 0;
  881. }