diff options
author | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2016-07-04 22:41:49 +0300 |
---|---|---|
committer | Costa Tsaousis (ktsaou) <costa@tsaousis.gr> | 2016-07-04 22:41:49 +0300 |
commit | 0f80f8b9f850cc5bbb6669aa7d71f8b43e4614d4 (patch) | |
tree | 0d19c042d57c2d59f9a2de2b8c7bcb36af23cfcc /src/proc_stat.c | |
parent | 76162e300a56bc06f7c3da1812f00814ef4f37ce (diff) |
cleanup propriatery charts.d charts; proc_stat optimization; minor fixes for not updated dimensions in rrd.c
Diffstat (limited to 'src/proc_stat.c')
-rw-r--r-- | src/proc_stat.c | 99 |
1 files changed, 57 insertions, 42 deletions
diff --git a/src/proc_stat.c b/src/proc_stat.c index 9272dd3c29..f96297941f 100644 --- a/src/proc_stat.c +++ b/src/proc_stat.c @@ -12,31 +12,37 @@ #include "rrd.h" #include "plugin_proc.h" -#define RRD_TYPE_STAT "cpu" -#define RRD_TYPE_STAT_LEN strlen(RRD_TYPE_STAT) - int do_proc_stat(int update_every, unsigned long long dt) { + (void)dt; + static procfile *ff = NULL; static int do_cpu = -1, do_cpu_cores = -1, do_interrupts = -1, do_context = -1, do_forks = -1, do_processes = -1; + static uint32_t hash_intr, hash_ctxt, hash_processes, hash_procs_running, hash_procs_blocked; + + if(unlikely(do_cpu == -1)) { + do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1); + do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1); + do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1); + do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1); + do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1); + do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1); + + hash_intr = simple_hash("intr"); + hash_ctxt = simple_hash("ctxt"); + hash_processes = simple_hash("processes"); + hash_procs_running = simple_hash("procs_running"); + hash_procs_blocked = simple_hash("procs_blocked"); + } - if(do_cpu == -1) do_cpu = config_get_boolean("plugin:proc:/proc/stat", "cpu utilization", 1); - if(do_cpu_cores == -1) do_cpu_cores = config_get_boolean("plugin:proc:/proc/stat", "per cpu core utilization", 1); - if(do_interrupts == -1) do_interrupts = config_get_boolean("plugin:proc:/proc/stat", "cpu interrupts", 1); - if(do_context == -1) do_context = config_get_boolean("plugin:proc:/proc/stat", "context switches", 1); - if(do_forks == -1) do_forks = config_get_boolean("plugin:proc:/proc/stat", "processes started", 1); - if(do_processes == -1) do_processes = config_get_boolean("plugin:proc:/proc/stat", "processes running", 1); - - if(dt) {}; - - if(!ff) { + if(unlikely(!ff)) { char filename[FILENAME_MAX + 1]; snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/stat"); ff = procfile_open(config_get("plugin:proc:/proc/stat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT); + if(unlikely(!ff)) return 1; } - if(!ff) return 1; ff = procfile_readall(ff); - if(!ff) return 0; // we return 0, so that we will retry to open it next time + if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time uint32_t lines = procfile_lines(ff), l; uint32_t words; @@ -45,9 +51,13 @@ int do_proc_stat(int update_every, unsigned long long dt) { RRDSET *st; for(l = 0; l < lines ;l++) { - if(strncmp(procfile_lineword(ff, l, 0), "cpu", 3) == 0) { + char *row_key = procfile_lineword(ff, l, 0); + uint32_t hash = simple_hash(row_key); + + // faster strncmp(row_key, "cpu", 3) == 0 + if(likely(row_key[0] == 'c' && row_key[1] == 'p' && row_key[2] == 'u')) { words = procfile_linewords(ff, l); - if(words < 9) { + if(unlikely(words < 9)) { error("Cannot read /proc/stat cpu line. Expected 9 params, read %u.", words); continue; } @@ -55,7 +65,7 @@ int do_proc_stat(int update_every, unsigned long long dt) { 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 = procfile_lineword(ff, l, 0); + id = row_key; user = strtoull(procfile_lineword(ff, l, 1), NULL, 10); nice = strtoull(procfile_lineword(ff, l, 2), NULL, 10); system = strtoull(procfile_lineword(ff, l, 3), NULL, 10); @@ -67,25 +77,30 @@ int do_proc_stat(int update_every, unsigned long long dt) { if(words >= 10) guest = strtoull(procfile_lineword(ff, l, 9), NULL, 10); if(words >= 11) guest_nice = strtoull(procfile_lineword(ff, l, 10), NULL, 10); - char *title = "Core utilization"; - char *type = RRD_TYPE_STAT; - char *context = "cpu.cpu"; - char *family = "utilization"; - long priority = 1000; - int isthistotal = 0; + char *title, *type, *context, *family; + long priority; + int isthistotal; - if(strcmp(id, "cpu") == 0) { - isthistotal = 1; - type = "system"; + if(unlikely(strcmp(id, "cpu")) == 0) { title = "Total CPU utilization"; + type = "system"; context = "system.cpu"; family = id; priority = 100; + isthistotal = 1; + } + else { + title = "Core utilization"; + type = "cpu"; + context = "cpu.cpu"; + family = "utilization"; + priority = 1000; + isthistotal = 0; } - if((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores)) { + if(likely((isthistotal && do_cpu) || (!isthistotal && do_cpu_cores))) { st = rrdset_find_bytype(type, id); - if(!st) { + if(unlikely(!st)) { st = rrdset_create(type, id, NULL, family, context, title, "percentage", priority, update_every, RRDSET_TYPE_STACKED); long multiplier = 1; @@ -119,14 +134,14 @@ int do_proc_stat(int update_every, unsigned long long dt) { rrdset_done(st); } } - else if(strcmp(procfile_lineword(ff, l, 0), "intr") == 0) { + else if(hash == hash_intr && strcmp(row_key, "intr") == 0) { unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10); // -------------------------------------------------------------------- - if(do_interrupts) { + if(likely(do_interrupts)) { st = rrdset_find_bytype("system", "intr"); - if(!st) { + if(unlikely(!st)) { st = rrdset_create("system", "intr", NULL, "interrupts", NULL, "CPU Interrupts", "interrupts/s", 900, update_every, RRDSET_TYPE_LINE); st->isdetail = 1; @@ -138,14 +153,14 @@ int do_proc_stat(int update_every, unsigned long long dt) { rrdset_done(st); } } - else if(strcmp(procfile_lineword(ff, l, 0), "ctxt") == 0) { + else if(hash == hash_ctxt && strcmp(row_key, "ctxt") == 0) { unsigned long long value = strtoull(procfile_lineword(ff, l, 1), NULL, 10); // -------------------------------------------------------------------- - if(do_context) { + if(likely(do_context)) { st = rrdset_find_bytype("system", "ctxt"); - if(!st) { + if(unlikely(!st)) { st = rrdset_create("system", "ctxt", NULL, "processes", NULL, "CPU Context Switches", "context switches/s", 800, update_every, RRDSET_TYPE_LINE); rrddim_add(st, "switches", NULL, 1, 1, RRDDIM_INCREMENTAL); @@ -156,22 +171,22 @@ int do_proc_stat(int update_every, unsigned long long dt) { rrdset_done(st); } } - else if(!processes && strcmp(procfile_lineword(ff, l, 0), "processes") == 0) { + else if(hash == hash_processes && !processes && strcmp(row_key, "processes") == 0) { processes = strtoull(procfile_lineword(ff, l, 1), NULL, 10); } - else if(!running && strcmp(procfile_lineword(ff, l, 0), "procs_running") == 0) { + else if(hash == hash_procs_running && !running && strcmp(row_key, "procs_running") == 0) { running = strtoull(procfile_lineword(ff, l, 1), NULL, 10); } - else if(!blocked && strcmp(procfile_lineword(ff, l, 0), "procs_blocked") == 0) { + else if(hash == hash_procs_blocked && !blocked && strcmp(row_key, "procs_blocked") == 0) { blocked = strtoull(procfile_lineword(ff, l, 1), NULL, 10); } } // -------------------------------------------------------------------- - if(do_forks) { + if(likely(do_forks)) { st = rrdset_find_bytype("system", "forks"); - if(!st) { + if(unlikely(!st)) { st = rrdset_create("system", "forks", NULL, "processes", NULL, "Started Processes", "processes/s", 700, update_every, RRDSET_TYPE_LINE); st->isdetail = 1; @@ -185,9 +200,9 @@ int do_proc_stat(int update_every, unsigned long long dt) { // -------------------------------------------------------------------- - if(do_processes) { + if(likely(do_processes)) { st = rrdset_find_bytype("system", "processes"); - if(!st) { + if(unlikely(!st)) { st = rrdset_create("system", "processes", NULL, "processes", NULL, "System Processes", "processes", 600, update_every, RRDSET_TYPE_LINE); rrddim_add(st, "running", NULL, 1, 1, RRDDIM_ABSOLUTE); |