X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fprocesses.c;h=ac5ec6041bd221ace5413d2ee88be44b57b50189;hb=c4439c9cb3e2348ad7013644731de27a55eca478;hp=2e9715ddc3083c697c96d6ecf98088998ab0d45d;hpb=22912eb530d3352544a450c149ae59d7f6ae0266;p=collectd.git diff --git a/src/processes.c b/src/processes.c index 2e9715dd..ac5ec604 100644 --- a/src/processes.c +++ b/src/processes.c @@ -38,12 +38,12 @@ #include "collectd.h" -#include "common.h" #include "plugin.h" +#include "utils/common/common.h" #if HAVE_LIBTASKSTATS +#include "utils/taskstats/taskstats.h" #include "utils_complain.h" -#include "utils_taskstats.h" #endif /* Include header files for the mach system, if they exist.. */ @@ -198,25 +198,25 @@ typedef struct process_entry_s { derive_t io_syscw; derive_t io_diskr; derive_t io_diskw; - _Bool has_io; + bool has_io; derive_t cswitch_vol; derive_t cswitch_invol; - _Bool has_cswitch; + bool has_cswitch; #if HAVE_LIBTASKSTATS ts_delay_t delay; #endif - _Bool has_delay; + bool has_delay; - _Bool has_fd; + bool has_fd; - _Bool has_maps; + bool has_maps; } process_entry_t; typedef struct procstat_entry_s { unsigned long id; - unsigned long age; + unsigned char age; derive_t vmem_minflt_counter; derive_t vmem_majflt_counter; @@ -284,22 +284,22 @@ typedef struct procstat { gauge_t delay_swapin; gauge_t delay_freepages; - _Bool report_fd_num; - _Bool report_maps_num; - _Bool report_ctx_switch; - _Bool report_delay; + bool report_fd_num; + bool report_maps_num; + bool report_ctx_switch; + bool report_delay; struct procstat *next; struct procstat_entry_s *instances; } procstat_t; -static procstat_t *list_head_g = NULL; +static procstat_t *list_head_g; -static _Bool want_init = 1; -static _Bool report_ctx_switch = 0; -static _Bool report_fd_num = 0; -static _Bool report_maps_num = 0; -static _Bool report_delay = 0; +static bool want_init = true; +static bool report_ctx_switch; +static bool report_fd_num; +static bool report_maps_num; +static bool report_delay; #if HAVE_THREAD_INFO static mach_port_t port_host_self; @@ -334,7 +334,7 @@ int getargs(void *processBuffer, int bufferLen, char *argsBuffer, int argsLen); #endif /* HAVE_PROCINFO_H */ #if HAVE_LIBTASKSTATS -static ts_t *taskstats_handle = NULL; +static ts_t *taskstats_handle; #endif /* put name of process from config to list_head_g tree @@ -587,7 +587,8 @@ static void ps_list_add(const char *name, const char *cmdline, entry->cpu_system_counter); #if HAVE_LIBTASKSTATS - ps_update_delay(ps, pse, entry); + if (entry->has_delay) + ps_update_delay(ps, pse, entry); #endif } } @@ -616,7 +617,7 @@ static void ps_list_reset(void) { pse_prev = NULL; pse = ps->instances; while (pse != NULL) { - if (pse->age > 10) { + if (pse->age > 0) { DEBUG("Removing this procstat entry cause it's too old: " "id = %lu; name = %s;", pse->id, ps->name); @@ -631,7 +632,7 @@ static void ps_list_reset(void) { pse = pse_prev->next; } } else { - pse->age++; + pse->age = 1; pse_prev = pse; pse = pse->next; } @@ -657,7 +658,7 @@ static void ps_tune_instance(oconfig_item_t *ci, procstat_t *ps) { "for the \"CollectDelayAccounting\" option."); #endif } else { - ERROR("processes plugin: Option `%s' not allowed heeere.", c->key); + ERROR("processes plugin: Option \"%s\" not allowed here.", c->key); } } /* for (ci->children) */ } /* void ps_tune_instance */ @@ -685,7 +686,8 @@ static int ps_config(oconfig_item_t *ci) { #if KERNEL_LINUX || KERNEL_SOLARIS || KERNEL_FREEBSD if (strlen(c->values[0].value.string) > max_procname_len) { - WARNING("processes plugin: this platform has a %zu character limit " + WARNING("processes plugin: this platform has a %" PRIsz + " character limit " "to process names. The `Process \"%s\"' option will " "not work as expected.", max_procname_len, c->values[0].value.string); @@ -903,38 +905,27 @@ static void ps_submit_proc_list(procstat_t *ps) { plugin_dispatch_values(&vl); } - /* The ps->delay_* metrics are in nanoseconds per second. This factor converts - * them to a percentage. */ - gauge_t const delay_factor = 100.0 / 1000000000.0; - - if (!isnan(ps->delay_cpu)) { - sstrncpy(vl.type, "percent", sizeof(vl.type)); - sstrncpy(vl.type_instance, "delay-cpu", sizeof(vl.type_instance)); - vl.values[0].gauge = ps->delay_cpu * delay_factor; - vl.values_len = 1; - plugin_dispatch_values(&vl); - } - - if (!isnan(ps->delay_blkio)) { - sstrncpy(vl.type, "percent", sizeof(vl.type)); - sstrncpy(vl.type_instance, "delay-blkio", sizeof(vl.type_instance)); - vl.values[0].gauge = ps->delay_blkio * delay_factor; - vl.values_len = 1; - plugin_dispatch_values(&vl); - } - - if (!isnan(ps->delay_swapin)) { - sstrncpy(vl.type, "percent", sizeof(vl.type)); - sstrncpy(vl.type_instance, "delay-swapin", sizeof(vl.type_instance)); - vl.values[0].gauge = ps->delay_swapin * delay_factor; - vl.values_len = 1; - plugin_dispatch_values(&vl); - } - - if (!isnan(ps->delay_freepages)) { - sstrncpy(vl.type, "percent", sizeof(vl.type)); - sstrncpy(vl.type_instance, "delay-freepages", sizeof(vl.type_instance)); - vl.values[0].gauge = ps->delay_freepages * delay_factor; + /* The ps->delay_* metrics are in nanoseconds per second. Convert to seconds + * per second. */ + gauge_t const delay_factor = 1000000000.0; + + struct { + const char *type_instance; + gauge_t rate_ns; + } delay_metrics[] = { + {"delay-cpu", ps->delay_cpu}, + {"delay-blkio", ps->delay_blkio}, + {"delay-swapin", ps->delay_swapin}, + {"delay-freepages", ps->delay_freepages}, + }; + for (size_t i = 0; i < STATIC_ARRAY_SIZE(delay_metrics); i++) { + if (isnan(delay_metrics[i].rate_ns)) { + continue; + } + sstrncpy(vl.type, "delay_rate", sizeof(vl.type)); + sstrncpy(vl.type_instance, delay_metrics[i].type_instance, + sizeof(vl.type_instance)); + vl.values[0].gauge = delay_metrics[i].rate_ns / delay_factor; vl.values_len = 1; plugin_dispatch_values(&vl); } @@ -1005,8 +996,9 @@ static int ps_read_tasks_status(process_entry_t *ps) { tpid = ent->d_name; - if (snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", ps->id, - tpid) >= sizeof(filename)) { + int r = snprintf(filename, sizeof(filename), "/proc/%li/task/%s/status", + ps->id, tpid); + if ((size_t)r >= sizeof(filename)) { DEBUG("Filename too long: `%s'", filename); continue; } @@ -1259,45 +1251,41 @@ static int ps_delay(process_entry_t *ps) { return 0; } -#else -static int ps_delay(__attribute__((unused)) process_entry_t *unused) { - return -1; -} #endif static void ps_fill_details(const procstat_t *ps, process_entry_t *entry) { - if (entry->has_io == 0) { + if (entry->has_io == false) { ps_read_io(entry); - entry->has_io = 1; + entry->has_io = true; } if (ps->report_ctx_switch) { - if (entry->has_cswitch == 0) { + if (entry->has_cswitch == false) { ps_read_tasks_status(entry); - entry->has_cswitch = 1; + entry->has_cswitch = true; } } if (ps->report_maps_num) { int num_maps; - if (entry->has_maps == 0 && (num_maps = ps_count_maps(entry->id)) > 0) { + if (entry->has_maps == false && (num_maps = ps_count_maps(entry->id)) > 0) { entry->num_maps = num_maps; } - entry->has_maps = 1; + entry->has_maps = true; } if (ps->report_fd_num) { int num_fd; - if (entry->has_fd == 0 && (num_fd = ps_count_fd(entry->id)) > 0) { + if (entry->has_fd == false && (num_fd = ps_count_fd(entry->id)) > 0) { entry->num_fd = num_fd; } - entry->has_fd = 1; + entry->has_fd = true; } #if HAVE_LIBTASKSTATS if (ps->report_delay && !entry->has_delay) { if (ps_delay(entry) == 0) { - entry->has_delay = 1; + entry->has_delay = true; } } #endif @@ -1350,7 +1338,8 @@ static int ps_read_process(long pid, process_entry_t *ps, char *state) { /* Either '(' or ')' is not found or they are in the wrong order. * Anyway, something weird that shouldn't happen ever. */ if (name_start_pos >= name_end_pos) { - ERROR("processes plugin: name_start_pos = %zu >= name_end_pos = %zu", + ERROR("processes plugin: name_start_pos = %" PRIsz + " >= name_end_pos = %" PRIsz, name_start_pos, name_end_pos); return -1; } @@ -1534,7 +1523,7 @@ static int read_fork_rate(void) { FILE *proc_stat; char buffer[1024]; value_t value; - _Bool value_valid = 0; + bool value_valid = 0; proc_stat = fopen("/proc/stat", "r"); if (proc_stat == NULL) { @@ -1584,12 +1573,11 @@ static char *ps_get_cmdline(long pid, if ((status < 0) || (((size_t)status) != sizeof(info))) { ERROR("processes plugin: Unexpected return value " "while reading \"%s\": " - "Returned %zd but expected %zu.", + "Returned %zd but expected %" PRIsz ".", path, status, buffer_size); return NULL; } - info.pr_psargs[sizeof(info.pr_psargs) - 1] = 0; sstrncpy(buffer, info.pr_psargs, buffer_size); return buffer; @@ -2150,7 +2138,7 @@ static int ps_read(void) { * filter out threads (duplicate PID entries). */ if ((proc_ptr == NULL) || (proc_ptr->ki_pid != procs[i].ki_pid)) { char cmdline[CMDLINE_BUFFER_SIZE] = ""; - _Bool have_cmdline = 0; + bool have_cmdline = 0; proc_ptr = &(procs[i]); /* Don't probe system processes and processes without arguments */ @@ -2305,7 +2293,7 @@ static int ps_read(void) { * filter out threads (duplicate PID entries). */ if ((proc_ptr == NULL) || (proc_ptr->p_pid != procs[i].p_pid)) { char cmdline[CMDLINE_BUFFER_SIZE] = ""; - _Bool have_cmdline = 0; + bool have_cmdline = 0; proc_ptr = &(procs[i]); /* Don't probe zombie processes */ @@ -2645,7 +2633,7 @@ static int ps_read(void) { read_fork_rate(); #endif /* KERNEL_SOLARIS */ - want_init = 0; + want_init = false; return 0; } /* int ps_read */