fix missing CPU frequency (#16732)
Ilya Mashchenko committed
Jan 6, 2024 at 17:40 UTC
08c38340b3f81ccbcf376b03e5e48133515937d4
1 file changed
+64
-53
collectors/proc.plugin/proc_stat.c
+64
-53
@@ -46,6 +46,7 @@ struct cpu_chart {
46
RRDDIM *rd_guest;
47
RRDDIM *rd_guest_nice;
48
49
+ bool per_core_files_found;
50
struct per_core_single_number_file files[PER_CORE_FILES];
51
52
struct per_core_time_in_state_file time_in_state_files;
@@ -589,14 +590,71 @@ int do_proc_stat(int update_every, usec_t dt) {
590
continue;
591
}
592
592
- size_t core = (row_key[3] == '\0') ? 0 : str2ul(&row_key[3]) + 1;
593
- if(likely(core > 0)) cores_found = core;
593
+ size_t core = (row_key[3] == '\0') ? 0 : str2ul(&row_key[3]) + 1;
594
+ if (likely(core > 0))
595
+ cores_found = core;
596
+
597
+ bool do_any_core_metric = do_cpu_cores || do_core_throttle_count || do_cpu_freq || do_cpuidle;
598
+
599
+ if (likely((core == 0 && do_cpu) || (core > 0 && do_any_core_metric))) {
600
+ if (unlikely(core >= all_cpu_charts_size)) {
601
+ size_t old_cpu_charts_size = all_cpu_charts_size;
602
+ all_cpu_charts_size = core + 1;
603
+ all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * all_cpu_charts_size);
604
+ memset(&all_cpu_charts[old_cpu_charts_size], 0, sizeof(struct cpu_chart) * (all_cpu_charts_size - old_cpu_charts_size));
605
+ }
606
+
607
+ struct cpu_chart *cpu_chart = &all_cpu_charts[core];
608
+
609
+ if (unlikely(!cpu_chart->id))
610
+ cpu_chart->id = strdupz(row_key);
611
+
612
+ if (core > 0 && !cpu_chart->per_core_files_found) {
613
+ cpu_chart->per_core_files_found = true;
614
+
615
+ char filename[FILENAME_MAX + 1];
616
+ struct stat stbuf;
617
+
618
+ if (do_core_throttle_count != CONFIG_BOOLEAN_NO) {
619
+ snprintfz(filename, FILENAME_MAX, core_throttle_count_filename, cpu_chart->id);
620
+ if (stat(filename, &stbuf) == 0) {
621
+ cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
622
+ cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].fd = -1;
623
+ do_core_throttle_count = CONFIG_BOOLEAN_YES;
624
+ }
625
+ }
626
+
627
+ if (do_package_throttle_count != CONFIG_BOOLEAN_NO) {
628
+ snprintfz(filename, FILENAME_MAX, package_throttle_count_filename, cpu_chart->id);
629
+ if (stat(filename, &stbuf) == 0) {
630
+ cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
631
+ cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].fd = -1;
632
+ do_package_throttle_count = CONFIG_BOOLEAN_YES;
633
+ }
634
+ }
635
+
636
+ if (do_cpu_freq != CONFIG_BOOLEAN_NO) {
637
+ snprintfz(filename, FILENAME_MAX, scaling_cur_freq_filename, cpu_chart->id);
638
+ if (stat(filename, &stbuf) == 0) {
639
+ cpu_chart->files[CPU_FREQ_INDEX].filename = strdupz(filename);
640
+ cpu_chart->files[CPU_FREQ_INDEX].fd = -1;
641
+ do_cpu_freq = CONFIG_BOOLEAN_YES;
642
+ }
643
+
644
+ snprintfz(filename, FILENAME_MAX, time_in_state_filename, cpu_chart->id);
645
+ if (stat(filename, &stbuf) == 0) {
646
+ cpu_chart->time_in_state_files.filename = strdupz(filename);
647
+ cpu_chart->time_in_state_files.ff = NULL;
648
+ do_cpu_freq = CONFIG_BOOLEAN_YES;
649
+ accurate_freq_avail = 1;
650
+ }
651
+ }
652
+ }
653
+ }
654
655
if(likely((core == 0 && do_cpu) || (core > 0 && do_cpu_cores))) {
596
- char *id;
656
unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
657
599
- id = row_key;
658
user = str2ull(procfile_lineword(ff, l, 1), NULL);
659
nice = str2ull(procfile_lineword(ff, l, 2), NULL);
660
system = str2ull(procfile_lineword(ff, l, 3), NULL);
@@ -615,17 +673,11 @@ int do_proc_stat(int update_every, usec_t dt) {
673
char *title, *type, *context, *family;
674
long priority;
675
618
- if(unlikely(core >= all_cpu_charts_size)) {
619
- size_t old_cpu_charts_size = all_cpu_charts_size;
620
- all_cpu_charts_size = core + 1;
621
- all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * all_cpu_charts_size);
622
- memset(&all_cpu_charts[old_cpu_charts_size], 0, sizeof(struct cpu_chart) * (all_cpu_charts_size - old_cpu_charts_size));
623
- }
676
struct cpu_chart *cpu_chart = &all_cpu_charts[core];
677
626
- if(unlikely(!cpu_chart->st)) {
627
- cpu_chart->id = strdupz(id);
678
+ char *id = row_key;
679
680
+ if(unlikely(!cpu_chart->st)) {
681
if(unlikely(core == 0)) {
682
title = "Total CPU utilization";
683
type = "system";
@@ -639,47 +691,6 @@ int do_proc_stat(int update_every, usec_t dt) {
691
context = "cpu.cpu";
692
family = "utilization";
693
priority = NETDATA_CHART_PRIO_CPU_PER_CORE;
642
-
643
- char filename[FILENAME_MAX + 1];
644
- struct stat stbuf;
645
-
646
- if(do_core_throttle_count != CONFIG_BOOLEAN_NO) {
647
- snprintfz(filename, FILENAME_MAX, core_throttle_count_filename, id);
648
- if (stat(filename, &stbuf) == 0) {
649
- cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
650
- cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].fd = -1;
651
- do_core_throttle_count = CONFIG_BOOLEAN_YES;
652
- }
653
- }
654
-
655
- if(do_package_throttle_count != CONFIG_BOOLEAN_NO) {
656
- snprintfz(filename, FILENAME_MAX, package_throttle_count_filename, id);
657
- if (stat(filename, &stbuf) == 0) {
658
- cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
659
- cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].fd = -1;
660
- do_package_throttle_count = CONFIG_BOOLEAN_YES;
661
- }
662
- }
663
-
664
- if(do_cpu_freq != CONFIG_BOOLEAN_NO) {
665
-
666
- snprintfz(filename, FILENAME_MAX, scaling_cur_freq_filename, id);
667
-
668
- if (stat(filename, &stbuf) == 0) {
669
- cpu_chart->files[CPU_FREQ_INDEX].filename = strdupz(filename);
670
- cpu_chart->files[CPU_FREQ_INDEX].fd = -1;
671
- do_cpu_freq = CONFIG_BOOLEAN_YES;
672
- }
673
-
674
- snprintfz(filename, FILENAME_MAX, time_in_state_filename, id);
675
-
676
- if (stat(filename, &stbuf) == 0) {
677
- cpu_chart->time_in_state_files.filename = strdupz(filename);
678
- cpu_chart->time_in_state_files.ff = NULL;
679
- do_cpu_freq = CONFIG_BOOLEAN_YES;
680
- accurate_freq_avail = 1;
681
- }
682
- }
694
}
695
696
cpu_chart->st = rrdset_create_localhost(