X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fprocesses.c;h=e1660c55455e63c4fe29ce040d8a45ec1652681b;hb=c5ffa797883f238776e6294054f260df3cd0aea6;hp=8ac05a35074bda4744037c7d955087ad6875e430;hpb=14b4024e2e621b440f96e8f2ef1af04d6275ee34;p=collectd.git diff --git a/src/processes.c b/src/processes.c index 8ac05a35..e1660c55 100644 --- a/src/processes.c +++ b/src/processes.c @@ -181,9 +181,11 @@ typedef struct procstat_entry_s derive_t io_wchar; derive_t io_syscr; derive_t io_syscw; + _Bool has_io; derive_t cswitch_vol; derive_t cswitch_invol; + _Bool has_cswitch; struct procstat_entry_s *next; } procstat_entry_t; @@ -225,6 +227,7 @@ typedef struct procstat static procstat_t *list_head_g = NULL; +static _Bool want_init = 1; static _Bool report_ctx_switch = 0; #if HAVE_THREAD_INFO @@ -237,6 +240,7 @@ static mach_msg_type_number_t pset_list_len; #elif KERNEL_LINUX static long pagesize_g; +static void ps_fill_details (const procstat_t *ps, procstat_entry_t *entry); /* #endif KERNEL_LINUX */ #elif HAVE_LIBKVM_GETPROCS && (HAVE_STRUCT_KINFO_PROC_FREEBSD || HAVE_STRUCT_KINFO_PROC_OPENBSD) @@ -362,13 +366,16 @@ static int ps_list_match (const char *name, const char *cmdline, procstat_t *ps) return (0); } /* int ps_list_match */ -static void ps_update_counter (_Bool init, derive_t *group_counter, +static void ps_update_counter (derive_t *group_counter, derive_t *curr_counter, derive_t new_counter) { unsigned long curr_value; - if (init) + if (want_init) + { + *curr_counter = new_counter; return; + } if (new_counter < *curr_counter) curr_value = new_counter + (ULONG_MAX - *curr_counter); @@ -389,11 +396,13 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t for (procstat_t *ps = list_head_g; ps != NULL; ps = ps->next) { - _Bool want_init; - if ((ps_list_match (name, cmdline, ps)) == 0) continue; +#if KERNEL_LINUX + ps_fill_details(ps, entry); +#endif + for (pse = ps->instances; pse != NULL; pse = pse->next) if ((pse->id == entry->id) || (pse->next == NULL)) break; @@ -446,24 +455,20 @@ static void ps_list_add (const char *name, const char *cmdline, procstat_entry_t ps->cswitch_vol += ((pse->cswitch_vol == -1)?0:pse->cswitch_vol); ps->cswitch_invol += ((pse->cswitch_invol == -1)?0:pse->cswitch_invol); - want_init = (entry->vmem_minflt_counter == 0) - && (entry->vmem_majflt_counter == 0); - ps_update_counter (want_init, + ps_update_counter ( &ps->vmem_minflt_counter, &pse->vmem_minflt_counter, entry->vmem_minflt_counter); - ps_update_counter (want_init, + ps_update_counter ( &ps->vmem_majflt_counter, &pse->vmem_majflt_counter, entry->vmem_majflt_counter); - want_init = (entry->cpu_user_counter == 0) - && (entry->cpu_system_counter == 0); - ps_update_counter (want_init, + ps_update_counter ( &ps->cpu_user_counter, &pse->cpu_user_counter, entry->cpu_user_counter); - ps_update_counter (want_init, + ps_update_counter ( &ps->cpu_system_counter, &pse->cpu_system_counter, entry->cpu_system_counter); @@ -785,7 +790,7 @@ static void ps_submit_fork_rate (derive_t value) /* ------- additional functions for KERNEL_LINUX/HAVE_THREAD_INFO ------- */ #if KERNEL_LINUX -static procstat_t *ps_read_tasks_status (long pid, procstat_t *ps) +static int ps_read_tasks_status (procstat_entry_t *ps) { char dirname[64]; DIR *dh; @@ -798,12 +803,12 @@ static procstat_t *ps_read_tasks_status (long pid, procstat_t *ps) char *fields[8]; int numfields; - ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", pid); + ssnprintf (dirname, sizeof (dirname), "/proc/%li/task", ps->id); if ((dh = opendir (dirname)) == NULL) { DEBUG ("Failed to open directory `%s'", dirname); - return (NULL); + return (-1); } while ((ent = readdir (dh)) != NULL) @@ -815,7 +820,7 @@ static procstat_t *ps_read_tasks_status (long pid, procstat_t *ps) tpid = ent->d_name; - ssnprintf (filename, sizeof (filename), "/proc/%li/task/%s/status", pid, tpid); + ssnprintf (filename, sizeof (filename), "/proc/%li/task/%s/status", ps->id, tpid); if ((fh = fopen (filename, "r")) == NULL) { DEBUG ("Failed to open file `%s'", filename); @@ -865,7 +870,7 @@ static procstat_t *ps_read_tasks_status (long pid, procstat_t *ps) ps->cswitch_vol = cswitch_vol; ps->cswitch_invol = cswitch_invol; - return (ps); + return (0); } /* int *ps_read_tasks_status */ /* Read data from /proc/pid/status */ @@ -939,7 +944,7 @@ static procstat_t *ps_read_status (long pid, procstat_t *ps) return (ps); } /* procstat_t *ps_read_vmem */ -static procstat_t *ps_read_io (long pid, procstat_t *ps) +static int ps_read_io (procstat_entry_t *ps) { FILE *fh; char buffer[1024]; @@ -948,9 +953,9 @@ static procstat_t *ps_read_io (long pid, procstat_t *ps) char *fields[8]; int numfields; - ssnprintf (filename, sizeof (filename), "/proc/%li/io", pid); + ssnprintf (filename, sizeof (filename), "/proc/%li/io", ps->id); if ((fh = fopen (filename, "r")) == NULL) - return (NULL); + return (-1); while (fgets (buffer, sizeof (buffer), fh) != NULL) { @@ -990,9 +995,36 @@ static procstat_t *ps_read_io (long pid, procstat_t *ps) WARNING ("processes: fclose: %s", sstrerror (errno, errbuf, sizeof (errbuf))); } + return (0); +} /* int ps_read_io (...) */ - return (ps); -} /* procstat_t *ps_read_io */ +static void ps_fill_details (const procstat_t *ps, procstat_entry_t *entry) +{ + if ( entry->has_io == 0 && ps_read_io (entry) != 0 ) + { + /* no io data */ + entry->io_rchar = -1; + entry->io_wchar = -1; + entry->io_syscr = -1; + entry->io_syscw = -1; + + DEBUG("ps_read_process: not get io data for pid %li", entry->id); + } + entry->has_io = 1; + + if ( report_ctx_switch ) + { + if ( entry->has_cswitch == 0 && ps_read_tasks_status(entry) != 0 ) + { + entry->cswitch_vol = -1; + entry->cswitch_invol = -1; + + DEBUG("ps_read_tasks_status: not get context " + "switch data for pid %li", entry->id); + } + entry->has_cswitch = 1; + } +} /* void ps_fill_details (...) */ static int ps_read_process (long pid, procstat_t *ps, char *state) { @@ -1125,29 +1157,6 @@ static int ps_read_process (long pid, procstat_t *ps, char *state) ps->vmem_rss = (unsigned long) vmem_rss; ps->stack_size = (unsigned long) stack_size; - if ( (ps_read_io (pid, ps)) == NULL) - { - /* no io data */ - ps->io_rchar = -1; - ps->io_wchar = -1; - ps->io_syscr = -1; - ps->io_syscw = -1; - - DEBUG("ps_read_process: not get io data for pid %li", pid); - } - - if ( report_ctx_switch ) - { - if ( (ps_read_tasks_status(pid, ps)) == NULL) - { - ps->cswitch_vol = -1; - ps->cswitch_invol = -1; - - DEBUG("ps_read_tasks_status: not get context " - "switch data for pid %li", pid); - } - } - /* success */ return (0); } /* int ps_read_process (...) */ @@ -2395,6 +2404,8 @@ static int ps_read (void) read_fork_rate(); #endif /* KERNEL_SOLARIS */ + want_init = 0; + return (0); } /* int ps_read */