summaryrefslogtreecommitdiffstats
path: root/collectors/proc.plugin/proc_stat.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/collectors/proc.plugin/proc_stat.c (renamed from collectors/proc.plugin/proc_stat.c)131
1 files changed, 71 insertions, 60 deletions
diff --git a/collectors/proc.plugin/proc_stat.c b/src/collectors/proc.plugin/proc_stat.c
index 84160f22f..481cb906a 100644
--- a/collectors/proc.plugin/proc_stat.c
+++ b/src/collectors/proc.plugin/proc_stat.c
@@ -46,6 +46,7 @@ struct cpu_chart {
RRDDIM *rd_guest;
RRDDIM *rd_guest_nice;
+ bool per_core_files_found;
struct per_core_single_number_file files[PER_CORE_FILES];
struct per_core_time_in_state_file time_in_state_files;
@@ -67,7 +68,7 @@ static int read_per_core_files(struct cpu_chart *all_cpu_charts, size_t len, siz
continue;
if(unlikely(f->fd == -1)) {
- f->fd = open(f->filename, O_RDONLY);
+ f->fd = open(f->filename, O_RDONLY | O_CLOEXEC);
if (unlikely(f->fd == -1)) {
collector_error("Cannot open file '%s'", f->filename);
continue;
@@ -411,7 +412,7 @@ static int read_cpuidle_states(char *cpuidle_name_filename , char *cpuidle_time_
char name_buf[50 + 1];
snprintfz(filename, FILENAME_MAX, cpuidle_name_filename, core, state);
- int fd = open(filename, O_RDONLY, 0666);
+ int fd = open(filename, O_RDONLY | O_CLOEXEC, 0666);
if(unlikely(fd == -1)) {
collector_error("Cannot open file '%s'", filename);
cc->rescan_cpu_states = 1;
@@ -443,7 +444,7 @@ static int read_cpuidle_states(char *cpuidle_name_filename , char *cpuidle_time_
struct cpuidle_state *cs = &cc->cpuidle_state[state];
if(unlikely(cs->time_fd == -1)) {
- cs->time_fd = open(cs->time_filename, O_RDONLY);
+ cs->time_fd = open(cs->time_filename, O_RDONLY | O_CLOEXEC);
if (unlikely(cs->time_fd == -1)) {
collector_error("Cannot open file '%s'", cs->time_filename);
cc->rescan_cpu_states = 1;
@@ -487,7 +488,7 @@ int do_proc_stat(int update_every, usec_t dt) {
if(unlikely(do_cpu == -1)) {
do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", CONFIG_BOOLEAN_YES);
- do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", CONFIG_BOOLEAN_YES);
+ do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", CONFIG_BOOLEAN_NO);
do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", CONFIG_BOOLEAN_YES);
do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", CONFIG_BOOLEAN_YES);
do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", CONFIG_BOOLEAN_YES);
@@ -508,7 +509,7 @@ int do_proc_stat(int update_every, usec_t dt) {
do_core_throttle_count = CONFIG_BOOLEAN_AUTO;
do_package_throttle_count = CONFIG_BOOLEAN_NO;
do_cpu_freq = CONFIG_BOOLEAN_YES;
- do_cpuidle = CONFIG_BOOLEAN_YES;
+ do_cpuidle = CONFIG_BOOLEAN_NO;
}
if(unlikely(get_system_cpus() > 24)) {
// the system has too many processors
@@ -589,14 +590,71 @@ int do_proc_stat(int update_every, usec_t dt) {
continue;
}
- size_t core = (row_key[3] == '\0') ? 0 : str2ul(&row_key[3]) + 1;
- if(likely(core > 0)) cores_found = core;
+ size_t core = (row_key[3] == '\0') ? 0 : str2ul(&row_key[3]) + 1;
+ if (likely(core > 0))
+ cores_found = core;
+
+ bool do_any_core_metric = do_cpu_cores || do_core_throttle_count || do_cpu_freq || do_cpuidle;
+
+ if (likely((core == 0 && do_cpu) || (core > 0 && do_any_core_metric))) {
+ if (unlikely(core >= all_cpu_charts_size)) {
+ size_t old_cpu_charts_size = all_cpu_charts_size;
+ all_cpu_charts_size = core + 1;
+ all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * all_cpu_charts_size);
+ memset(&all_cpu_charts[old_cpu_charts_size], 0, sizeof(struct cpu_chart) * (all_cpu_charts_size - old_cpu_charts_size));
+ }
+
+ struct cpu_chart *cpu_chart = &all_cpu_charts[core];
+
+ if (unlikely(!cpu_chart->id))
+ cpu_chart->id = strdupz(row_key);
+
+ if (core > 0 && !cpu_chart->per_core_files_found) {
+ cpu_chart->per_core_files_found = true;
+
+ char filename[FILENAME_MAX + 1];
+ struct stat stbuf;
+
+ if (do_core_throttle_count != CONFIG_BOOLEAN_NO) {
+ snprintfz(filename, FILENAME_MAX, core_throttle_count_filename, cpu_chart->id);
+ if (stat(filename, &stbuf) == 0) {
+ cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
+ cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].fd = -1;
+ do_core_throttle_count = CONFIG_BOOLEAN_YES;
+ }
+ }
+
+ if (do_package_throttle_count != CONFIG_BOOLEAN_NO) {
+ snprintfz(filename, FILENAME_MAX, package_throttle_count_filename, cpu_chart->id);
+ if (stat(filename, &stbuf) == 0) {
+ cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
+ cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].fd = -1;
+ do_package_throttle_count = CONFIG_BOOLEAN_YES;
+ }
+ }
+
+ if (do_cpu_freq != CONFIG_BOOLEAN_NO) {
+ snprintfz(filename, FILENAME_MAX, scaling_cur_freq_filename, cpu_chart->id);
+ if (stat(filename, &stbuf) == 0) {
+ cpu_chart->files[CPU_FREQ_INDEX].filename = strdupz(filename);
+ cpu_chart->files[CPU_FREQ_INDEX].fd = -1;
+ do_cpu_freq = CONFIG_BOOLEAN_YES;
+ }
+
+ snprintfz(filename, FILENAME_MAX, time_in_state_filename, cpu_chart->id);
+ if (stat(filename, &stbuf) == 0) {
+ cpu_chart->time_in_state_files.filename = strdupz(filename);
+ cpu_chart->time_in_state_files.ff = NULL;
+ do_cpu_freq = CONFIG_BOOLEAN_YES;
+ accurate_freq_avail = 1;
+ }
+ }
+ }
+ }
if(likely((core == 0 && do_cpu) || (core > 0 && do_cpu_cores))) {
- char *id;
unsigned long long user = 0, nice = 0, system = 0, idle = 0, iowait = 0, irq = 0, softirq = 0, steal = 0, guest = 0, guest_nice = 0;
- id = row_key;
user = str2ull(procfile_lineword(ff, l, 1), NULL);
nice = str2ull(procfile_lineword(ff, l, 2), NULL);
system = str2ull(procfile_lineword(ff, l, 3), NULL);
@@ -615,17 +673,11 @@ int do_proc_stat(int update_every, usec_t dt) {
char *title, *type, *context, *family;
long priority;
- if(unlikely(core >= all_cpu_charts_size)) {
- size_t old_cpu_charts_size = all_cpu_charts_size;
- all_cpu_charts_size = core + 1;
- all_cpu_charts = reallocz(all_cpu_charts, sizeof(struct cpu_chart) * all_cpu_charts_size);
- memset(&all_cpu_charts[old_cpu_charts_size], 0, sizeof(struct cpu_chart) * (all_cpu_charts_size - old_cpu_charts_size));
- }
struct cpu_chart *cpu_chart = &all_cpu_charts[core];
- if(unlikely(!cpu_chart->st)) {
- cpu_chart->id = strdupz(id);
+ char *id = row_key;
+ if(unlikely(!cpu_chart->st)) {
if(unlikely(core == 0)) {
title = "Total CPU utilization";
type = "system";
@@ -639,47 +691,6 @@ int do_proc_stat(int update_every, usec_t dt) {
context = "cpu.cpu";
family = "utilization";
priority = NETDATA_CHART_PRIO_CPU_PER_CORE;
-
- char filename[FILENAME_MAX + 1];
- struct stat stbuf;
-
- if(do_core_throttle_count != CONFIG_BOOLEAN_NO) {
- snprintfz(filename, FILENAME_MAX, core_throttle_count_filename, id);
- if (stat(filename, &stbuf) == 0) {
- cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
- cpu_chart->files[CORE_THROTTLE_COUNT_INDEX].fd = -1;
- do_core_throttle_count = CONFIG_BOOLEAN_YES;
- }
- }
-
- if(do_package_throttle_count != CONFIG_BOOLEAN_NO) {
- snprintfz(filename, FILENAME_MAX, package_throttle_count_filename, id);
- if (stat(filename, &stbuf) == 0) {
- cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].filename = strdupz(filename);
- cpu_chart->files[PACKAGE_THROTTLE_COUNT_INDEX].fd = -1;
- do_package_throttle_count = CONFIG_BOOLEAN_YES;
- }
- }
-
- if(do_cpu_freq != CONFIG_BOOLEAN_NO) {
-
- snprintfz(filename, FILENAME_MAX, scaling_cur_freq_filename, id);
-
- if (stat(filename, &stbuf) == 0) {
- cpu_chart->files[CPU_FREQ_INDEX].filename = strdupz(filename);
- cpu_chart->files[CPU_FREQ_INDEX].fd = -1;
- do_cpu_freq = CONFIG_BOOLEAN_YES;
- }
-
- snprintfz(filename, FILENAME_MAX, time_in_state_filename, id);
-
- if (stat(filename, &stbuf) == 0) {
- cpu_chart->time_in_state_files.filename = strdupz(filename);
- cpu_chart->time_in_state_files.ff = NULL;
- do_cpu_freq = CONFIG_BOOLEAN_YES;
- accurate_freq_avail = 1;
- }
- }
}
cpu_chart->st = rrdset_create_localhost(
@@ -719,7 +730,7 @@ int do_proc_stat(int update_every, usec_t dt) {
}
if(unlikely(core == 0 && cpus_var == NULL))
- cpus_var = rrdvar_custom_host_variable_add_and_acquire(localhost, "active_processors");
+ cpus_var = rrdvar_host_variable_add_and_acquire(localhost, "active_processors");
}
rrddim_set_by_pointer(cpu_chart->st, cpu_chart->rd_user, user);
@@ -1064,7 +1075,7 @@ int do_proc_stat(int update_every, usec_t dt) {
}
if(cpus_var)
- rrdvar_custom_host_variable_set(localhost, cpus_var, cores_found);
+ rrdvar_host_variable_set(localhost, cpus_var, cores_found);
return 0;
}