X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fprocesses.c;h=8275022f1120df9bb2c55374eb97d6d96bc4aebc;hb=47faf00c18402cefd0e195d63ad48d5e7e6a4a92;hp=f57f2d3134bfb237da47f85b89eff544ad42a533;hpb=dc80c73c20ef0e69c3850fd9679a827ad79e61a1;p=collectd.git diff --git a/src/processes.c b/src/processes.c index f57f2d31..8275022f 100644 --- a/src/processes.c +++ b/src/processes.c @@ -80,84 +80,20 @@ # ifndef CONFIG_HZ # define CONFIG_HZ 100 # endif -#endif /* KERNEL_LINUX */ - -#define MODULE_NAME "processes" +/* #endif KERNEL_LINUX */ -#if HAVE_THREAD_INFO || KERNEL_LINUX -# define PROCESSES_HAVE_READ 1 #else -# define PROCESSES_HAVE_READ 0 +# error "No applicable input method." #endif #define BUFSIZE 256 -static data_source_t state_dsrc[1] = -{ - {"value", DS_TYPE_GAUGE, 0.0, 65535.0} -}; - -static data_set_t state_ds = -{ - "ps_state", 1, state_dsrc -}; - -static data_source_t rss_dsrc[1] = -{ - /* max = 2^63 - 1 */ - {"value", DS_TYPE_GAUGE, 0.0, 9223372036854775807.0} -}; - -static data_set_t rss_ds = -{ - "ps_rss", 1, rss_dsrc -}; - -static data_source_t time_dsrc[2] = -{ - /* 1 second in user-mode per second ought to be enough.. */ - {"user", DS_TYPE_COUNTER, 0.0, 1000000.0}, - {"syst", DS_TYPE_COUNTER, 0.0, 1000000.0} -}; - -static data_set_t time_ds = -{ - "ps_cputime", 2, time_dsrc -}; - -static data_source_t count_dsrc[2] = -{ - /* 1 second in user-mode per second ought to be enough.. */ - {"processes", DS_TYPE_GAUGE, 0.0, 1000000.0}, - {"threads", DS_TYPE_GAUGE, 0.0, 1000000.0} -}; - -static data_set_t count_ds = -{ - "ps_count", 2, count_dsrc -}; - -static data_source_t pagefaults_dsrc[2] = -{ - /* max = 2^63 - 1 */ - {"minflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0}, - {"majflt", DS_TYPE_COUNTER, 0.0, 9223372036854775807.0} -}; - -static data_set_t pagefaults_ds = -{ - "ps_pagefaults", 2, pagefaults_dsrc -}; - -#if PROCESSES_HAVE_READ -#if HAVE_THREAD_INFO | KERNEL_LINUX static const char *config_keys[] = { "Process", NULL }; static int config_keys_num = 1; -#endif typedef struct procstat_entry_s { @@ -200,9 +136,7 @@ typedef struct procstat struct procstat_entry_s *instances; } procstat_t; -#if HAVE_THREAD_INFO | KERNEL_LINUX static procstat_t *list_head_g = NULL; -#endif #if HAVE_THREAD_INFO static mach_port_t port_host_self; @@ -216,7 +150,6 @@ static mach_msg_type_number_t pset_list_len; static long pagesize_g; #endif /* KERNEL_LINUX */ -#if HAVE_THREAD_INFO | KERNEL_LINUX static void ps_list_register (const char *name) { procstat_t *new; @@ -225,7 +158,7 @@ static void ps_list_register (const char *name) if ((new = (procstat_t *) malloc (sizeof (procstat_t))) == NULL) return; memset (new, 0, sizeof (procstat_t)); - strncpy (new->name, name, PROCSTAT_NAME_LEN); + sstrncpy (new->name, name, sizeof (new->name)); for (ptr = list_head_g; ptr != NULL; ptr = ptr->next) { @@ -427,7 +360,6 @@ static int ps_config (const char *key, const char *value) return (0); } -#endif /* HAVE_THREAD_INFO | KERNEL_LINUX */ static int ps_init (void) { @@ -480,9 +412,10 @@ static void ps_submit_state (const char *state, double value) strcpy (vl.host, hostname_g); strcpy (vl.plugin, "processes"); strcpy (vl.plugin_instance, ""); - strncpy (vl.type_instance, state, sizeof (vl.type_instance)); + strcpy (vl.type, "ps_state"); + sstrncpy (vl.type_instance, state, sizeof (vl.type_instance)); - plugin_dispatch_values ("ps_state", &vl); + plugin_dispatch_values (&vl); } static void ps_submit_proc_list (procstat_t *ps) @@ -495,26 +428,30 @@ static void ps_submit_proc_list (procstat_t *ps) vl.time = time (NULL); strcpy (vl.host, hostname_g); strcpy (vl.plugin, "processes"); - strncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance)); + sstrncpy (vl.plugin_instance, ps->name, sizeof (vl.plugin_instance)); + strcpy (vl.type, "ps_rss"); vl.values[0].gauge = ps->vmem_rss; vl.values_len = 1; - plugin_dispatch_values ("ps_rss", &vl); + plugin_dispatch_values (&vl); + strcpy (vl.type, "ps_cputime"); vl.values[0].counter = ps->cpu_user_counter; vl.values[1].counter = ps->cpu_system_counter; vl.values_len = 2; - plugin_dispatch_values ("ps_cputime", &vl); + plugin_dispatch_values (&vl); + strcpy (vl.type, "ps_count"); vl.values[0].gauge = ps->num_proc; vl.values[1].gauge = ps->num_lwp; vl.values_len = 2; - plugin_dispatch_values ("ps_count", &vl); + plugin_dispatch_values (&vl); + strcpy (vl.type, "ps_pagefaults"); vl.values[0].counter = ps->vmem_minflt_counter; vl.values[1].counter = ps->vmem_majflt_counter; vl.values_len = 2; - plugin_dispatch_values ("ps_pagefaults", &vl); + plugin_dispatch_values (&vl); DEBUG ("name = %s; num_proc = %lu; num_lwp = %lu; vmem_rss = %lu; " "vmem_minflt_counter = %lu; vmem_majflt_counter = %lu; " @@ -535,8 +472,7 @@ static int *ps_read_tasks (int pid) DIR *dh; struct dirent *ent; - snprintf (dirname, 64, "/proc/%i/task", pid); - dirname[63] = '\0'; + ssnprintf (dirname, sizeof (dirname), "/proc/%i/task", pid); if ((dh = opendir (dirname)) == NULL) { @@ -578,11 +514,14 @@ static int *ps_read_tasks (int pid) closedir (dh); + if (list_len == 0) + return (NULL); + assert (list_len < list_size); assert (list[list_len] == 0); return (list); -} +} /* int *ps_read_tasks */ int ps_read_process (int pid, procstat_t *ps, char *state) { @@ -605,8 +544,7 @@ int ps_read_process (int pid, procstat_t *ps, char *state) memset (ps, 0, sizeof (procstat_t)); - snprintf (filename, 64, "/proc/%i/stat", pid); - filename[63] = '\0'; + ssnprintf (filename, sizeof (filename), "/proc/%i/stat", pid); if ((fh = fopen (filename, "r")) == NULL) return (-1); @@ -641,17 +579,21 @@ int ps_read_process (int pid, procstat_t *ps, char *state) ppid = atoi (fields[3]); - if ((tasks = ps_read_tasks (pid)) == NULL) + *state = fields[2][0]; + + if (*state == 'Z') { - /* This happends for zombied, e.g. */ - DEBUG ("ps_read_tasks (%i) failed.", pid); - *state = 'Z'; ps->num_lwp = 0; ps->num_proc = 0; } + else if ((tasks = ps_read_tasks (pid)) == NULL) + { + /* Kernel 2.4 or so */ + ps->num_lwp = 1; + ps->num_proc = 1; + } else { - *state = '\0'; ps->num_lwp = 0; ps->num_proc = 1; for (i = 0; tasks[i] != 0; i++) @@ -661,11 +603,11 @@ int ps_read_process (int pid, procstat_t *ps, char *state) tasks = NULL; } - /* Leave the rest at zero if this is only an LWP */ + /* Leave the rest at zero if this is only a zombi */ if (ps->num_proc == 0) { - DEBUG ("This is only an LWP: pid = %i; name = %s;", - pid, ps->name); + DEBUG ("processes plugin: This is only a zombi: pid = %i; " + "name = %s;", pid, ps->name); return (0); } @@ -684,8 +626,6 @@ int ps_read_process (int pid, procstat_t *ps, char *state) ps->cpu_system_counter = (unsigned long) cpu_system_counter; ps->vmem_rss = (unsigned long) vmem_rss; - *state = fields[2][0]; - /* success */ return (0); } /* int ps_read_process (...) */ @@ -1073,28 +1013,11 @@ static int ps_read (void) return (0); } /* int ps_read */ -#endif /* PROCESSES_HAVE_READ */ -void module_register (modreg_e load) +void module_register (void) { - if (load & MR_DATASETS) - { - plugin_register_data_set (&state_ds); - plugin_register_data_set (&rss_ds); - plugin_register_data_set (&time_ds); - plugin_register_data_set (&count_ds); - plugin_register_data_set (&pagefaults_ds); - } - -#if PROCESSES_HAVE_READ - if (load & MR_READ) - { -#if HAVE_THREAD_INFO | KERNEL_LINUX - plugin_register_config ("processes", ps_config, - config_keys, config_keys_num); -#endif - plugin_register_init ("processes", ps_init); - plugin_register_read ("processes", ps_read); - } -#endif /* PROCESSES_HAVE_READ */ + plugin_register_config ("processes", ps_config, + config_keys, config_keys_num); + plugin_register_init ("processes", ps_init); + plugin_register_read ("processes", ps_read); } /* void module_register */