4 * (C) Copyright 2016-2017 Costa Tsaousis <costa@tsaousis.gr>
5 * Released under GPL v3+
11 // ----------------------------------------------------------------------------
14 #define MAX_COMPARE_NAME 100
16 #define MAX_CMDLINE 1024
19 // ----------------------------------------------------------------------------
20 // the rates we are going to send to netdata will have this detail a value of:
21 // - 1 will send just integer parts to netdata
22 // - 100 will send 2 decimal points
23 // - 1000 will send 3 decimal points
25 #define RATES_DETAIL 10000ULL
28 // ----------------------------------------------------------------------------
29 // to avoid reallocating too frequently, we can increase the number of spare
30 // file descriptors used by processes.
32 // having a lot of spares, increases the CPU utilization of the plugin.
33 #define MAX_SPARE_FDS 1
36 // ----------------------------------------------------------------------------
37 // command line options
42 enable_guest_charts = 0,
43 enable_file_charts = 1,
44 enable_users_charts = 1,
45 enable_groups_charts = 1,
46 include_exited_childs = 1;
49 // will be changed to getenv(NETDATA_CONFIG_DIR) if it exists
50 static char *config_dir = CONFIG_DIR;
52 // ----------------------------------------------------------------------------
54 // handled in code (automatically set)
57 show_guest_time = 0, // 1 when guest values are collected
58 show_guest_time_old = 0,
59 proc_pid_cmdline_is_needed = 0; // 1 when we need to read /proc/cmdline
62 // ----------------------------------------------------------------------------
66 global_iterations_counter = 1,
70 // ----------------------------------------------------------------------------
73 // With normalization we lower the collected metrics by a factor to make them
74 // match the total utilization of the system.
75 // The discrepancy exists because apps.plugin needs some time to collect all
76 // the metrics. This results in utilization that exceeds the total utilization
79 // With normalization we align the per-process utilization, to the total of
80 // the system. We first consume the exited children utilization and it the
81 // collected values is above the total, we proportionally scale each reported
84 // the total system time, as reported by /proc/stat
91 // the normalization ratios, as calculated by normalize_utilization()
92 double utime_fix_ratio = 1.0,
93 stime_fix_ratio = 1.0,
94 gtime_fix_ratio = 1.0,
95 minflt_fix_ratio = 1.0,
96 majflt_fix_ratio = 1.0,
97 cutime_fix_ratio = 1.0,
98 cstime_fix_ratio = 1.0,
99 cgtime_fix_ratio = 1.0,
100 cminflt_fix_ratio = 1.0,
101 cmajflt_fix_ratio = 1.0;
104 // ----------------------------------------------------------------------------
107 // target is the structure that processes are aggregated to be reported
110 // - Each entry in /etc/apps_groups.conf creates a target.
111 // - Each user and group used by a process in the system, creates a target.
114 char compare[MAX_COMPARE_NAME + 1];
115 uint32_t comparehash;
118 char id[MAX_NAME + 1];
121 char name[MAX_NAME + 1];
126 kernel_uint_t minflt;
127 kernel_uint_t cminflt;
128 kernel_uint_t majflt;
129 kernel_uint_t cmajflt;
133 kernel_uint_t cutime;
134 kernel_uint_t cstime;
135 kernel_uint_t cgtime;
136 kernel_uint_t num_threads;
137 // kernel_uint_t rss;
139 kernel_uint_t statm_size;
140 kernel_uint_t statm_resident;
141 kernel_uint_t statm_share;
142 // kernel_uint_t statm_text;
143 // kernel_uint_t statm_lib;
144 // kernel_uint_t statm_data;
145 // kernel_uint_t statm_dirty;
147 kernel_uint_t io_logical_bytes_read;
148 kernel_uint_t io_logical_bytes_written;
149 // kernel_uint_t io_read_calls;
150 // kernel_uint_t io_write_calls;
151 kernel_uint_t io_storage_bytes_read;
152 kernel_uint_t io_storage_bytes_written;
153 // kernel_uint_t io_cancelled_write_bytes;
158 kernel_uint_t openfiles;
159 kernel_uint_t openpipes;
160 kernel_uint_t opensockets;
161 kernel_uint_t openinotifies;
162 kernel_uint_t openeventfds;
163 kernel_uint_t opentimerfds;
164 kernel_uint_t opensignalfds;
165 kernel_uint_t openeventpolls;
166 kernel_uint_t openother;
168 unsigned int processes; // how many processes have been merged to this
169 int exposed; // if set, we have sent this to netdata
170 int hidden; // if set, we set the hidden flag on the dimension
173 int starts_with; // if set, the compare string matches only the
174 // beginning of the command
176 struct target *target; // the one that will be reported to netdata
181 *apps_groups_default_target = NULL, // the default target
182 *apps_groups_root_target = NULL, // apps_groups.conf defined
183 *users_root_target = NULL, // users
184 *groups_root_target = NULL; // user groups
187 apps_groups_targets_count = 0; // # of apps_groups.conf targets
190 // ----------------------------------------------------------------------------
193 // structure to store data for each process running
194 // see: man proc for the description of the fields
198 char comm[MAX_COMPARE_NAME + 1];
199 char cmdline[MAX_CMDLINE + 1];
211 // these are raw values collected
212 kernel_uint_t minflt_raw;
213 kernel_uint_t cminflt_raw;
214 kernel_uint_t majflt_raw;
215 kernel_uint_t cmajflt_raw;
216 kernel_uint_t utime_raw;
217 kernel_uint_t stime_raw;
218 kernel_uint_t gtime_raw; // guest_time
219 kernel_uint_t cutime_raw;
220 kernel_uint_t cstime_raw;
221 kernel_uint_t cgtime_raw; // cguest_time
224 kernel_uint_t minflt;
225 kernel_uint_t cminflt;
226 kernel_uint_t majflt;
227 kernel_uint_t cmajflt;
231 kernel_uint_t cutime;
232 kernel_uint_t cstime;
233 kernel_uint_t cgtime;
238 // int64_t itrealvalue;
239 // kernel_uint_t starttime;
240 // kernel_uint_t vsize;
241 // kernel_uint_t rss;
242 // kernel_uint_t rsslim;
243 // kernel_uint_t starcode;
244 // kernel_uint_t endcode;
245 // kernel_uint_t startstack;
246 // kernel_uint_t kstkesp;
247 // kernel_uint_t kstkeip;
250 // uint64_t sigignore;
251 // uint64_t sigcatch;
255 // int32_t exit_signal;
256 // int32_t processor;
257 // uint32_t rt_priority;
259 // kernel_uint_t delayacct_blkio_ticks;
264 kernel_uint_t statm_size;
265 kernel_uint_t statm_resident;
266 kernel_uint_t statm_share;
267 // kernel_uint_t statm_text;
268 // kernel_uint_t statm_lib;
269 // kernel_uint_t statm_data;
270 // kernel_uint_t statm_dirty;
272 kernel_uint_t io_logical_bytes_read_raw;
273 kernel_uint_t io_logical_bytes_written_raw;
274 // kernel_uint_t io_read_calls_raw;
275 // kernel_uint_t io_write_calls_raw;
276 kernel_uint_t io_storage_bytes_read_raw;
277 kernel_uint_t io_storage_bytes_written_raw;
278 // kernel_uint_t io_cancelled_write_bytes_raw;
280 kernel_uint_t io_logical_bytes_read;
281 kernel_uint_t io_logical_bytes_written;
282 // kernel_uint_t io_read_calls;
283 // kernel_uint_t io_write_calls;
284 kernel_uint_t io_storage_bytes_read;
285 kernel_uint_t io_storage_bytes_written;
286 // kernel_uint_t io_cancelled_write_bytes;
288 int *fds; // array of fds it uses
289 int fds_size; // the size of the fds array
291 int children_count; // number of processes directly referencing this
292 char keep:1; // 1 when we need to keep this process in memory even after it exited
293 int keeploops; // increases by 1 every time keep is 1 and updated 0
294 char updated:1; // 1 when the process is currently running
295 char merged:1; // 1 when it has been merged to its parent
296 char new_entry:1; // 1 when this is a new process, just saw for the first time
297 char read:1; // 1 when we have already read this process for this iteration
299 int sortlist; // higher numbers = top on the process tree
300 // each process gets a unique number
302 struct target *target; // app_groups.conf targets
303 struct target *user_target; // uid based targets
304 struct target *group_target; // gid based targets
306 usec_t stat_collected_usec;
307 usec_t last_stat_collected_usec;
309 usec_t io_collected_usec;
310 usec_t last_io_collected_usec;
312 char *fds_dirname; // the full directory name in /proc/PID/fd
315 char *statm_filename;
317 char *cmdline_filename;
319 struct pid_stat *parent;
320 struct pid_stat *prev;
321 struct pid_stat *next;
324 // log each problem once per process
325 // log flood protection flags (log_thrown)
326 #define PID_LOG_IO 0x00000001
327 #define PID_LOG_STATM 0x00000002
328 #define PID_LOG_CMDLINE 0x00000004
329 #define PID_LOG_FDS 0x00000008
330 #define PID_LOG_STAT 0x00000010
332 static struct pid_stat
333 *root_of_pids = NULL, // global list of all processes running
334 **all_pids = NULL; // to avoid allocations, we pre-allocate the
335 // the entire pid space.
338 all_pids_count = 0; // the number of processes running
340 // Another pre-allocated list of all possible pids.
341 // We need it to pids and assign them a unique sortlist id, so that we
342 // read parents before children. This is needed to prevent a situation where
343 // a child is found running, but until we read its parent, it has exited and
344 // its parent has accumulated its resources.
346 *all_pids_sortlist = NULL;
349 // ----------------------------------------------------------------------------
352 // this is used to keep a global list of all open files of the system.
353 // it is needed in order to calculate the unique files processes have open.
355 #define FILE_DESCRIPTORS_INCREASE_STEP 100
357 struct file_descriptor {
360 #ifdef NETDATA_INTERNAL_CHECKS
362 #endif /* NETDATA_INTERNAL_CHECKS */
376 // types for struct file_descriptor->type
377 #define FILETYPE_OTHER 0
378 #define FILETYPE_FILE 1
379 #define FILETYPE_PIPE 2
380 #define FILETYPE_SOCKET 3
381 #define FILETYPE_INOTIFY 4
382 #define FILETYPE_EVENTFD 5
383 #define FILETYPE_EVENTPOLL 6
384 #define FILETYPE_TIMERFD 7
385 #define FILETYPE_SIGNALFD 8
388 // ----------------------------------------------------------------------------
389 // callback required by fatal()
391 void netdata_cleanup_and_exit(int ret) {
395 // ----------------------------------------------------------------------------
397 // aggregate all processes in groups, to have a limited number of dimensions
399 static struct target *get_users_target(uid_t uid) {
401 for(w = users_root_target ; w ; w = w->next)
402 if(w->uid == uid) return w;
404 w = callocz(sizeof(struct target), 1);
405 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
406 w->comparehash = simple_hash(w->compare);
407 w->comparelen = strlen(w->compare);
409 snprintfz(w->id, MAX_NAME, "%u", uid);
410 w->idhash = simple_hash(w->id);
412 struct passwd *pw = getpwuid(uid);
414 snprintfz(w->name, MAX_NAME, "%u", uid);
416 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
418 netdata_fix_chart_name(w->name);
422 w->next = users_root_target;
423 users_root_target = w;
426 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
431 struct target *get_groups_target(gid_t gid)
434 for(w = groups_root_target ; w ; w = w->next)
435 if(w->gid == gid) return w;
437 w = callocz(sizeof(struct target), 1);
438 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
439 w->comparehash = simple_hash(w->compare);
440 w->comparelen = strlen(w->compare);
442 snprintfz(w->id, MAX_NAME, "%u", gid);
443 w->idhash = simple_hash(w->id);
445 struct group *gr = getgrgid(gid);
447 snprintfz(w->name, MAX_NAME, "%u", gid);
449 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
451 netdata_fix_chart_name(w->name);
455 w->next = groups_root_target;
456 groups_root_target = w;
459 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
464 // find or create a new target
465 // there are targets that are just aggregated to other target (the second argument)
466 static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
467 int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
468 const char *nid = id;
470 // extract the options
471 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
472 if(nid[0] == '-') thidden = 1;
473 if(nid[0] == '+') tdebug = 1;
474 if(nid[0] == '*') ends_with = 1;
477 uint32_t hash = simple_hash(id);
479 // find if it already exists
480 struct target *w, *last = apps_groups_root_target;
481 for(w = apps_groups_root_target ; w ; w = w->next) {
482 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
488 // find an existing target
489 if(unlikely(!target)) {
490 while(*name == '-') {
491 if(*name == '-') thidden = 1;
495 for(target = apps_groups_root_target ; target != NULL ; target = target->next) {
496 if(!target->target && strcmp(name, target->name) == 0)
500 if(unlikely(debug)) {
502 fprintf(stderr, "apps.plugin: REUSING TARGET NAME '%s' on ID '%s'\n", target->name, target->id);
504 fprintf(stderr, "apps.plugin: NEW TARGET NAME '%s' on ID '%s'\n", name, id);
508 if(target && target->target)
509 fatal("Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id, target->id, target->target->id);
511 w = callocz(sizeof(struct target), 1);
512 strncpyz(w->id, nid, MAX_NAME);
513 w->idhash = simple_hash(w->id);
515 if(unlikely(!target))
517 strncpyz(w->name, name, MAX_NAME);
520 strncpyz(w->name, nid, MAX_NAME);
522 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
523 size_t len = strlen(w->compare);
524 if(w->compare[len - 1] == '*') {
525 w->compare[len - 1] = '\0';
528 w->ends_with = ends_with;
530 if(w->starts_with && w->ends_with)
531 proc_pid_cmdline_is_needed = 1;
533 w->comparehash = simple_hash(w->compare);
534 w->comparelen = strlen(w->compare);
540 // append it, to maintain the order in apps_groups.conf
541 if(last) last->next = w;
542 else apps_groups_root_target = w;
545 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
547 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
548 , w->target?w->target->name:w->name
549 , (w->hidden)?"hidden":"-"
550 , (w->debug)?"debug":"-"
556 // read the apps_groups.conf file
557 static int read_apps_groups_conf(const char *file)
559 char filename[FILENAME_MAX + 1];
561 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, file);
564 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
566 // ----------------------------------------
568 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
571 procfile_set_quotes(ff, "'\"");
573 ff = procfile_readall(ff);
577 size_t line, lines = procfile_lines(ff);
579 for(line = 0; line < lines ;line++) {
580 size_t word, words = procfile_linewords(ff, line);
583 char *name = procfile_lineword(ff, line, 0);
584 if(!name || !*name) continue;
586 // find a possibly existing target
587 struct target *w = NULL;
589 // loop through all words, skipping the first one (the name)
590 for(word = 0; word < words ;word++) {
591 char *s = procfile_lineword(ff, line, word);
592 if(!s || !*s) continue;
595 // is this the first word? skip it
596 if(s == name) continue;
599 struct target *n = get_apps_groups_target(s, w, name);
601 error("Cannot create target '%s' (line %zu, word %zu)", s, line, word);
605 // just some optimization
606 // to avoid searching for a target for each process
607 if(!w) w = n->target?n->target:n;
613 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
614 if(!apps_groups_default_target)
615 fatal("Cannot create default target");
617 // allow the user to override group 'other'
618 if(apps_groups_default_target->target)
619 apps_groups_default_target = apps_groups_default_target->target;
625 // ----------------------------------------------------------------------------
626 // struct pid_stat management
628 static inline struct pid_stat *get_pid_entry(pid_t pid) {
629 if(unlikely(all_pids[pid])) {
630 all_pids[pid]->new_entry = 0;
631 return all_pids[pid];
634 all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
635 all_pids[pid]->fds = callocz(sizeof(int), MAX_SPARE_FDS);
636 all_pids[pid]->fds_size = MAX_SPARE_FDS;
638 if(likely(root_of_pids))
639 root_of_pids->prev = all_pids[pid];
641 all_pids[pid]->next = root_of_pids;
642 root_of_pids = all_pids[pid];
644 all_pids[pid]->pid = pid;
645 all_pids[pid]->new_entry = 1;
649 return all_pids[pid];
652 static inline void del_pid_entry(pid_t pid) {
653 if(unlikely(!all_pids[pid])) {
654 error("attempted to free pid %d that is not allocated.", pid);
659 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
661 if(root_of_pids == all_pids[pid])
662 root_of_pids = all_pids[pid]->next;
664 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
665 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
667 freez(all_pids[pid]->fds);
668 freez(all_pids[pid]->fds_dirname);
669 freez(all_pids[pid]->stat_filename);
670 freez(all_pids[pid]->statm_filename);
671 freez(all_pids[pid]->io_filename);
672 freez(all_pids[pid]->cmdline_filename);
673 freez(all_pids[pid]);
675 all_pids[pid] = NULL;
680 // ----------------------------------------------------------------------------
681 // update pids from proc
683 static inline int read_proc_pid_cmdline(struct pid_stat *p) {
685 if(unlikely(!p->cmdline_filename)) {
686 char filename[FILENAME_MAX + 1];
687 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", global_host_prefix, p->pid);
688 p->cmdline_filename = strdupz(filename);
691 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
692 if(unlikely(fd == -1)) goto cleanup;
694 ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
697 if(unlikely(bytes < 0)) goto cleanup;
699 p->cmdline[bytes] = '\0';
700 for(i = 0; i < bytes ; i++)
701 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
704 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
709 // copy the command to the command line
710 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
714 static inline int read_proc_pid_ownership(struct pid_stat *p) {
715 if(unlikely(!p->stat_filename)) {
716 error("pid %d does not have a stat_filename", p->pid);
720 // ----------------------------------------
724 if(stat(p->stat_filename, &st) != 0) {
725 error("Cannot stat file '%s'", p->stat_filename);
735 static inline int read_proc_pid_stat(struct pid_stat *p) {
736 static procfile *ff = NULL;
738 if(unlikely(!p->stat_filename)) {
739 char filename[FILENAME_MAX + 1];
740 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", global_host_prefix, p->pid);
741 p->stat_filename = strdupz(filename);
744 int set_quotes = (!ff)?1:0;
746 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
747 if(unlikely(!ff)) goto cleanup;
749 // if(set_quotes) procfile_set_quotes(ff, "()");
750 if(unlikely(set_quotes))
751 procfile_set_open_close(ff, "(", ")");
753 ff = procfile_readall(ff);
754 if(unlikely(!ff)) goto cleanup;
756 p->last_stat_collected_usec = p->stat_collected_usec;
757 p->stat_collected_usec = now_monotonic_usec();
760 // p->pid = str2pid_t(procfile_lineword(ff, 0, 0+i));
762 if(unlikely(!p->comm[0]))
763 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
765 // p->state = *(procfile_lineword(ff, 0, 2));
766 p->ppid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 3));
767 // p->pgrp = str2ul(procfile_lineword(ff, 0, 4));
768 // p->session = str2ul(procfile_lineword(ff, 0, 5));
769 // p->tty_nr = str2ul(procfile_lineword(ff, 0, 6));
770 // p->tpgid = str2ul(procfile_lineword(ff, 0, 7));
771 // p->flags = str2ull(procfile_lineword(ff, 0, 8));
775 last = p->minflt_raw;
776 p->minflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 9));
777 p->minflt = (p->minflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
779 last = p->cminflt_raw;
780 p->cminflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 10));
781 p->cminflt = (p->cminflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
783 last = p->majflt_raw;
784 p->majflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 11));
785 p->majflt = (p->majflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
787 last = p->cmajflt_raw;
788 p->cmajflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 12));
789 p->cmajflt = (p->cmajflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
792 p->utime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 13));
793 p->utime = (p->utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
796 p->stime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 14));
797 p->stime = (p->stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
799 last = p->cutime_raw;
800 p->cutime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 15));
801 p->cutime = (p->cutime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
803 last = p->cstime_raw;
804 p->cstime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 16));
805 p->cstime = (p->cstime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
807 // p->priority = str2kernel_unit_t(procfile_lineword(ff, 0, 17));
808 // p->nice = str2kernel_unit_t(procfile_lineword(ff, 0, 18));
809 p->num_threads = (int32_t)str2uint32_t(procfile_lineword(ff, 0, 19));
810 // p->itrealvalue = str2kernel_unit_t(procfile_lineword(ff, 0, 20));
811 // p->starttime = str2kernel_unit_t(procfile_lineword(ff, 0, 21));
812 // p->vsize = str2kernel_unit_t(procfile_lineword(ff, 0, 22));
813 // p->rss = str2kernel_unit_t(procfile_lineword(ff, 0, 23));
814 // p->rsslim = str2kernel_unit_t(procfile_lineword(ff, 0, 24));
815 // p->starcode = str2kernel_unit_t(procfile_lineword(ff, 0, 25));
816 // p->endcode = str2kernel_unit_t(procfile_lineword(ff, 0, 26));
817 // p->startstack = str2kernel_unit_t(procfile_lineword(ff, 0, 27));
818 // p->kstkesp = str2kernel_unit_t(procfile_lineword(ff, 0, 28));
819 // p->kstkeip = str2kernel_unit_t(procfile_lineword(ff, 0, 29));
820 // p->signal = str2kernel_unit_t(procfile_lineword(ff, 0, 30));
821 // p->blocked = str2kernel_unit_t(procfile_lineword(ff, 0, 31));
822 // p->sigignore = str2kernel_unit_t(procfile_lineword(ff, 0, 32));
823 // p->sigcatch = str2kernel_unit_t(procfile_lineword(ff, 0, 33));
824 // p->wchan = str2kernel_unit_t(procfile_lineword(ff, 0, 34));
825 // p->nswap = str2kernel_unit_t(procfile_lineword(ff, 0, 35));
826 // p->cnswap = str2kernel_unit_t(procfile_lineword(ff, 0, 36));
827 // p->exit_signal = str2kernel_unit_t(procfile_lineword(ff, 0, 37));
828 // p->processor = str2kernel_unit_t(procfile_lineword(ff, 0, 38));
829 // p->rt_priority = str2kernel_unit_t(procfile_lineword(ff, 0, 39));
830 // p->policy = str2kernel_unit_t(procfile_lineword(ff, 0, 40));
831 // p->delayacct_blkio_ticks = str2kernel_unit_t(procfile_lineword(ff, 0, 41));
833 if(enable_guest_charts) {
835 p->gtime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 42));
836 p->gtime = (p->gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
838 last = p->cgtime_raw;
839 p->cgtime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 43));
840 p->cgtime = (p->cgtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
842 if (show_guest_time || p->gtime || p->cgtime) {
843 p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
844 p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
849 if(unlikely(debug || (p->target && p->target->debug)))
850 fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d\n", global_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
852 if(unlikely(global_iterations_counter == 1)) {
883 static inline int read_proc_pid_statm(struct pid_stat *p) {
884 static procfile *ff = NULL;
886 if(unlikely(!p->statm_filename)) {
887 char filename[FILENAME_MAX + 1];
888 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", global_host_prefix, p->pid);
889 p->statm_filename = strdupz(filename);
892 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
893 if(unlikely(!ff)) goto cleanup;
895 ff = procfile_readall(ff);
896 if(unlikely(!ff)) goto cleanup;
900 p->statm_size = str2ull(procfile_lineword(ff, 0, 0));
901 p->statm_resident = str2ull(procfile_lineword(ff, 0, 1));
902 p->statm_share = str2ull(procfile_lineword(ff, 0, 2));
903 // p->statm_text = str2ull(procfile_lineword(ff, 0, 3));
904 // p->statm_lib = str2ull(procfile_lineword(ff, 0, 4));
905 // p->statm_data = str2ull(procfile_lineword(ff, 0, 5));
906 // p->statm_dirty = str2ull(procfile_lineword(ff, 0, 6));
912 p->statm_resident = 0;
914 // p->statm_text = 0;
916 // p->statm_data = 0;
917 // p->statm_dirty = 0;
921 static inline int read_proc_pid_io(struct pid_stat *p) {
922 static procfile *ff = NULL;
924 if(unlikely(!p->io_filename)) {
925 char filename[FILENAME_MAX + 1];
926 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", global_host_prefix, p->pid);
927 p->io_filename = strdupz(filename);
931 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
932 if(unlikely(!ff)) goto cleanup;
934 ff = procfile_readall(ff);
935 if(unlikely(!ff)) goto cleanup;
939 p->last_io_collected_usec = p->io_collected_usec;
940 p->io_collected_usec = now_monotonic_usec();
944 last = p->io_logical_bytes_read_raw;
945 p->io_logical_bytes_read_raw = str2ull(procfile_lineword(ff, 0, 1));
946 p->io_logical_bytes_read = (p->io_logical_bytes_read_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
948 last = p->io_logical_bytes_written_raw;
949 p->io_logical_bytes_written_raw = str2ull(procfile_lineword(ff, 1, 1));
950 p->io_logical_bytes_written = (p->io_logical_bytes_written_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
952 // last = p->io_read_calls_raw;
953 // p->io_read_calls_raw = str2ull(procfile_lineword(ff, 2, 1));
954 // p->io_read_calls = (p->io_read_calls_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
956 // last = p->io_write_calls_raw;
957 // p->io_write_calls_raw = str2ull(procfile_lineword(ff, 3, 1));
958 // p->io_write_calls = (p->io_write_calls_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
960 last = p->io_storage_bytes_read_raw;
961 p->io_storage_bytes_read_raw = str2ull(procfile_lineword(ff, 4, 1));
962 p->io_storage_bytes_read = (p->io_storage_bytes_read_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
964 last = p->io_storage_bytes_written_raw;
965 p->io_storage_bytes_written_raw = str2ull(procfile_lineword(ff, 5, 1));
966 p->io_storage_bytes_written = (p->io_storage_bytes_written_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
968 // last = p->io_cancelled_write_bytes_raw;
969 // p->io_cancelled_write_bytes_raw = str2ull(procfile_lineword(ff, 6, 1));
970 // p->io_cancelled_write_bytes = (p->io_cancelled_write_bytes_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
972 if(unlikely(global_iterations_counter == 1)) {
973 p->io_logical_bytes_read = 0;
974 p->io_logical_bytes_written = 0;
975 // p->io_read_calls = 0;
976 // p->io_write_calls = 0;
977 p->io_storage_bytes_read = 0;
978 p->io_storage_bytes_written = 0;
979 // p->io_cancelled_write_bytes = 0;
985 p->io_logical_bytes_read = 0;
986 p->io_logical_bytes_written = 0;
987 // p->io_read_calls = 0;
988 // p->io_write_calls = 0;
989 p->io_storage_bytes_read = 0;
990 p->io_storage_bytes_written = 0;
991 // p->io_cancelled_write_bytes = 0;
995 static inline int read_proc_stat() {
996 static char filename[FILENAME_MAX + 1] = "";
997 static procfile *ff = NULL;
998 static kernel_uint_t utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
999 static usec_t collected_usec = 0, last_collected_usec = 0;
1002 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
1003 ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
1004 if(unlikely(!ff)) goto cleanup;
1007 ff = procfile_readall(ff);
1008 if(unlikely(!ff)) goto cleanup;
1010 last_collected_usec = collected_usec;
1011 collected_usec = now_monotonic_usec();
1018 utime_raw = str2ull(procfile_lineword(ff, 0, 1));
1019 global_utime = (utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
1021 // nice time, on user time
1023 ntime_raw = str2ull(procfile_lineword(ff, 0, 2));
1024 global_utime += (ntime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
1027 stime_raw = str2ull(procfile_lineword(ff, 0, 3));
1028 global_stime = (stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
1031 gtime_raw = str2ull(procfile_lineword(ff, 0, 10));
1032 global_gtime = (gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
1034 if(enable_guest_charts) {
1035 // guest nice time, on guest time
1037 gntime_raw = str2ull(procfile_lineword(ff, 0, 11));
1038 global_gtime += (gntime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
1040 // remove guest time from user time
1041 global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
1044 if(unlikely(global_iterations_counter == 1)) {
1060 // ----------------------------------------------------------------------------
1062 int file_descriptor_compare(void* a, void* b) {
1063 #ifdef NETDATA_INTERNAL_CHECKS
1064 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
1065 error("Corrupted index data detected. Please report this.");
1066 #endif /* NETDATA_INTERNAL_CHECKS */
1068 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
1071 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
1075 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
1078 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
1080 avl_tree all_files_index = {
1082 file_descriptor_compare
1085 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
1086 struct file_descriptor tmp;
1087 tmp.hash = (hash)?hash:simple_hash(name);
1091 #ifdef NETDATA_INTERNAL_CHECKS
1092 tmp.magic = 0x0BADCAFE;
1093 #endif /* NETDATA_INTERNAL_CHECKS */
1095 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
1098 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1099 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1101 // ----------------------------------------------------------------------------
1103 static inline void file_descriptor_not_used(int id)
1105 if(id > 0 && id < all_files_size) {
1107 #ifdef NETDATA_INTERNAL_CHECKS
1108 if(all_files[id].magic != 0x0BADCAFE) {
1109 error("Ignoring request to remove empty file id %d.", id);
1112 #endif /* NETDATA_INTERNAL_CHECKS */
1115 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1117 if(all_files[id].count > 0) {
1118 all_files[id].count--;
1120 if(!all_files[id].count) {
1122 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1124 if(unlikely(file_descriptor_remove(&all_files[id]) != (void *)&all_files[id]))
1125 error("INTERNAL ERROR: removal of unused fd from index, removed a different fd");
1127 #ifdef NETDATA_INTERNAL_CHECKS
1128 all_files[id].magic = 0x00000000;
1129 #endif /* NETDATA_INTERNAL_CHECKS */
1134 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1136 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1139 static inline void all_files_grow() {
1140 void *old = all_files;
1143 // there is no empty slot
1145 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1147 all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1149 // if the address changed, we have to rebuild the index
1150 // since all pointers are now invalid
1152 if(unlikely(old && old != (void *)all_files)) {
1154 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1156 all_files_index.root = NULL;
1157 for(i = 0; i < all_files_size; i++) {
1158 if(!all_files[i].count) continue;
1159 if(unlikely(file_descriptor_add(&all_files[i]) != (void *)&all_files[i]))
1160 error("INTERNAL ERROR: duplicate indexing of fd during realloc.");
1164 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1167 // initialize the newly added entries
1169 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1170 all_files[i].count = 0;
1171 all_files[i].name = NULL;
1172 #ifdef NETDATA_INTERNAL_CHECKS
1173 all_files[i].magic = 0x00000000;
1174 #endif /* NETDATA_INTERNAL_CHECKS */
1175 all_files[i].pos = i;
1178 if(unlikely(!all_files_size)) all_files_len = 1;
1179 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1182 static inline int file_descriptor_set_on_empty_slot(const char *name, uint32_t hash, int type) {
1183 // check we have enough memory to add it
1184 if(!all_files || all_files_len == all_files_size)
1188 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1190 // search for an empty slot
1192 static int last_pos = 0;
1194 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1195 if(c >= all_files_size) c = 0;
1196 if(c == 0) continue;
1198 if(!all_files[c].count) {
1200 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1202 #ifdef NETDATA_INTERNAL_CHECKS
1203 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1204 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1205 #endif /* NETDATA_INTERNAL_CHECKS */
1208 fprintf(stderr, "apps.plugin: >> %s fd position %d for %s (last name: %s)\n", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
1210 freez((void *)all_files[c].name);
1211 all_files[c].name = NULL;
1219 if(i == all_files_size) {
1220 fatal("We should find an empty slot, but there isn't any");
1223 // else we have an empty slot in 'c'
1226 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1228 all_files[c].name = strdupz(name);
1229 all_files[c].hash = hash;
1230 all_files[c].type = type;
1231 all_files[c].pos = c;
1232 all_files[c].count = 1;
1233 #ifdef NETDATA_INTERNAL_CHECKS
1234 all_files[c].magic = 0x0BADCAFE;
1235 #endif /* NETDATA_INTERNAL_CHECKS */
1236 if(unlikely(file_descriptor_add(&all_files[c]) != (void *)&all_files[c]))
1237 error("INTERNAL ERROR: duplicate indexing of fd.");
1240 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1245 static inline int file_descriptor_find_or_add(const char *name)
1247 uint32_t hash = simple_hash(name);
1250 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1252 struct file_descriptor *fd = file_descriptor_find(name, hash);
1256 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1264 if(name[0] == '/') type = FILETYPE_FILE;
1265 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1266 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1267 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1268 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1269 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1270 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1271 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1272 else if(strncmp(name, "anon_inode:", 11) == 0) {
1274 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1276 type = FILETYPE_OTHER;
1280 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1282 type = FILETYPE_OTHER;
1285 return file_descriptor_set_on_empty_slot(name, hash, type);
1288 static inline void make_all_pid_fds_negative(struct pid_stat *p) {
1289 int *fd = p->fds, *end = &p->fds[p->fds_size];
1296 static inline void cleanup_negative_pid_fds(struct pid_stat *p) {
1297 int *fd = p->fds, *end = &p->fds[p->fds_size];
1299 if(unlikely(*fd < 0)) {
1300 file_descriptor_not_used(-(*fd));
1308 static inline void zero_pid_fds(struct pid_stat *p, int first, int size) {
1309 int *fd = &p->fds[first], *end = &p->fds[first + size];
1310 while(fd < end) *fd++ = 0;
1313 static inline int read_pid_file_descriptors(struct pid_stat *p) {
1314 if(unlikely(!p->fds_dirname)) {
1315 char dirname[FILENAME_MAX+1];
1316 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", global_host_prefix, p->pid);
1317 p->fds_dirname = strdupz(dirname);
1320 DIR *fds = opendir(p->fds_dirname);
1321 if(unlikely(!fds)) return 0;
1324 char fdname[FILENAME_MAX + 1];
1325 char linkname[FILENAME_MAX + 1];
1327 // we make all pid fds negative, so that
1328 // we can detect unused file descriptors
1329 // at the end, to free them
1330 make_all_pid_fds_negative(p);
1332 while((de = readdir(fds))) {
1333 // we need only files with numeric names
1335 if(unlikely(de->d_name[0] < '0' || de->d_name[0] > '9'))
1339 int fdid = (int)str2l(de->d_name);
1340 if(unlikely(fdid < 0)) continue;
1342 // check if the fds array is small
1343 if(unlikely(fdid >= p->fds_size)) {
1344 // it is small, extend it
1347 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
1349 p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(int));
1351 // and initialize it
1352 zero_pid_fds(p, p->fds_size, (fdid + MAX_SPARE_FDS) - p->fds_size);
1353 p->fds_size = fdid + MAX_SPARE_FDS;
1356 if(unlikely(p->fds[fdid] == 0)) {
1357 // we don't know this fd, get it
1359 sprintf(fdname, "%s/proc/%d/fd/%s", global_host_prefix, p->pid, de->d_name);
1360 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1361 if(unlikely(l == -1)) {
1362 if(debug || (p->target && p->target->debug)) {
1363 if(debug || (p->target && p->target->debug))
1364 error("Cannot read link %s", fdname);
1373 // if another process already has this, we will get
1375 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1378 // else make it positive again, we need it
1379 // of course, the actual file may have changed, but we don't care so much
1380 // FIXME: we could compare the inode as returned by readdir dirent structure
1383 p->fds[fdid] = -p->fds[fdid];
1387 cleanup_negative_pid_fds(p);
1392 // ----------------------------------------------------------------------------
1394 static inline int print_process_and_parents(struct pid_stat *p, usec_t time) {
1395 char *prefix = "\\_ ";
1399 indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1403 char buffer[indent + 1];
1406 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1409 fprintf(stderr, " %s %s%s (%d %s %llu"
1414 , p->updated?"running":"exited"
1415 , p->stat_collected_usec - time
1418 if(p->utime) fprintf(stderr, " utime=" KERNEL_UINT_FORMAT, p->utime);
1419 if(p->stime) fprintf(stderr, " stime=" KERNEL_UINT_FORMAT, p->stime);
1420 if(p->gtime) fprintf(stderr, " gtime=" KERNEL_UINT_FORMAT, p->gtime);
1421 if(p->cutime) fprintf(stderr, " cutime=" KERNEL_UINT_FORMAT, p->cutime);
1422 if(p->cstime) fprintf(stderr, " cstime=" KERNEL_UINT_FORMAT, p->cstime);
1423 if(p->cgtime) fprintf(stderr, " cgtime=" KERNEL_UINT_FORMAT, p->cgtime);
1424 if(p->minflt) fprintf(stderr, " minflt=" KERNEL_UINT_FORMAT, p->minflt);
1425 if(p->cminflt) fprintf(stderr, " cminflt=" KERNEL_UINT_FORMAT, p->cminflt);
1426 if(p->majflt) fprintf(stderr, " majflt=" KERNEL_UINT_FORMAT, p->majflt);
1427 if(p->cmajflt) fprintf(stderr, " cmajflt=" KERNEL_UINT_FORMAT, p->cmajflt);
1428 fprintf(stderr, ")\n");
1433 static inline void print_process_tree(struct pid_stat *p, char *msg) {
1435 fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1436 print_process_and_parents(p, p->stat_collected_usec);
1439 static inline void find_lost_child_debug(struct pid_stat *pe, kernel_uint_t lost, int type) {
1441 struct pid_stat *p = NULL;
1443 for(p = root_of_pids; p ; p = p->next) {
1444 if(p == pe) continue;
1448 if(p->cminflt > lost) {
1449 fprintf(stderr, " > process %d (%s) could use the lost exited child minflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1455 if(p->cmajflt > lost) {
1456 fprintf(stderr, " > process %d (%s) could use the lost exited child majflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1462 if(p->cutime > lost) {
1463 fprintf(stderr, " > process %d (%s) could use the lost exited child utime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1469 if(p->cstime > lost) {
1470 fprintf(stderr, " > process %d (%s) could use the lost exited child stime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1476 if(p->cgtime > lost) {
1477 fprintf(stderr, " > process %d (%s) could use the lost exited child gtime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1487 fprintf(stderr, " > cannot find any process to use the lost exited child minflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1491 fprintf(stderr, " > cannot find any process to use the lost exited child majflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1495 fprintf(stderr, " > cannot find any process to use the lost exited child utime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1499 fprintf(stderr, " > cannot find any process to use the lost exited child stime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1503 fprintf(stderr, " > cannot find any process to use the lost exited child gtime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1509 static inline kernel_uint_t remove_exited_child_from_parent(kernel_uint_t *field, kernel_uint_t *pfield) {
1510 kernel_uint_t absorbed = 0;
1512 if(*field > *pfield) {
1513 absorbed += *pfield;
1526 static inline void process_exited_processes() {
1529 for(p = root_of_pids; p ; p = p->next) {
1530 if(p->updated || !p->stat_collected_usec)
1533 kernel_uint_t utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1534 kernel_uint_t stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1535 kernel_uint_t gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1536 kernel_uint_t minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1537 kernel_uint_t majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1539 if(utime + stime + gtime + minflt + majflt == 0)
1542 if(unlikely(debug)) {
1544 fprintf(stderr, "Absorb %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")\n"
1547 , p->updated?"running":"exited"
1554 print_process_tree(p, "Searching parents");
1557 struct pid_stat *pp;
1558 for(pp = p->parent; pp ; pp = pp->parent) {
1559 if(!pp->updated) continue;
1561 kernel_uint_t absorbed;
1562 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1563 if(unlikely(debug && absorbed))
1564 fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " utime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1566 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1567 if(unlikely(debug && absorbed))
1568 fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " stime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1570 absorbed = remove_exited_child_from_parent(>ime, &pp->cgtime);
1571 if(unlikely(debug && absorbed))
1572 fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " gtime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1574 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1575 if(unlikely(debug && absorbed))
1576 fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " minflt (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1578 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1579 if(unlikely(debug && absorbed))
1580 fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " majflt (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1583 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1584 if(unlikely(debug)) {
1585 if(utime) find_lost_child_debug(p, utime, 3);
1586 if(stime) find_lost_child_debug(p, stime, 4);
1587 if(gtime) find_lost_child_debug(p, gtime, 5);
1588 if(minflt) find_lost_child_debug(p, minflt, 1);
1589 if(majflt) find_lost_child_debug(p, majflt, 2);
1595 fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")\n"
1598 , p->updated?"running":"exited"
1606 for(pp = p->parent; pp ; pp = pp->parent) {
1607 if(pp->updated) break;
1611 fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
1614 , pp->updated?"running":"exited"
1618 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1619 p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1620 p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1621 p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1622 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1623 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
1626 fprintf(stderr, "\n");
1628 else if(unlikely(debug)) {
1629 fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
1632 , p->updated?"running":"exited"
1638 static inline void link_all_processes_to_their_parents(void) {
1639 struct pid_stat *p, *pp;
1641 // link all children to their parents
1642 // and update children count on parents
1643 for(p = root_of_pids; p ; p = p->next) {
1644 // for each process found
1649 if(unlikely(!p->ppid)) {
1654 pp = all_pids[p->ppid];
1657 pp->children_count++;
1659 if(unlikely(debug || (p->target && p->target->debug)))
1660 fprintf(stderr, "apps.plugin: \tchild %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "\n", p->pid, p->comm, p->updated?"running":"exited", (p->target)?p->target->name:"UNSET", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->gtime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cgtime, pp->cminflt, pp->cmajflt);
1664 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1669 // ----------------------------------------------------------------------------
1671 // 1. read all files in /proc
1672 // 2. for each numeric directory:
1673 // i. read /proc/pid/stat
1674 // ii. read /proc/pid/statm
1675 // iii. read /proc/pid/io (requires root access)
1676 // iii. read the entries in directory /proc/pid/fd (requires root access)
1678 // a. find or create a struct file_descriptor
1679 // b. cleanup any old/unused file_descriptors
1681 // after all these, some pids may be linked to targets, while others may not
1683 // in case of errors, only 1 every 1000 errors is printed
1684 // to avoid filling up all disk space
1685 // if debug is enabled, all errors are printed
1687 static int compar_pid(const void *pid1, const void *pid2) {
1689 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1690 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1692 if(p1->sortlist > p2->sortlist)
1698 static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
1699 if(unlikely(!status)) {
1700 // error("command failed log %u, errno %d", log, errno);
1702 if(unlikely(debug || errno != ENOENT)) {
1703 if(unlikely(debug || !(p->log_thrown & log))) {
1704 p->log_thrown |= log;
1707 error("Cannot process %s/proc/%d/io (command '%s')", global_host_prefix, p->pid, p->comm);
1711 error("Cannot process %s/proc/%d/statm (command '%s')", global_host_prefix, p->pid, p->comm);
1714 case PID_LOG_CMDLINE:
1715 error("Cannot process %s/proc/%d/cmdline (command '%s')", global_host_prefix, p->pid, p->comm);
1719 error("Cannot process entries in %s/proc/%d/fd (command '%s')", global_host_prefix, p->pid, p->comm);
1726 error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
1733 else if(unlikely(p->log_thrown & log)) {
1734 // error("unsetting log %u on pid %d", log, p->pid);
1735 p->log_thrown &= ~log;
1741 static inline void assign_target_to_pid(struct pid_stat *p) {
1742 uint32_t hash = simple_hash(p->comm);
1743 size_t pclen = strlen(p->comm);
1746 for(w = apps_groups_root_target; w ; w = w->next) {
1747 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1749 // find it - 4 cases:
1750 // 1. the target is not a pattern
1751 // 2. the target has the prefix
1752 // 3. the target has the suffix
1753 // 4. the target is something inside cmdline
1755 if(unlikely(( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1756 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1757 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1758 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1761 if(w->target) p->target = w->target;
1764 if(debug || (p->target && p->target->debug))
1765 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1772 static inline int collect_data_for_pid(pid_t pid) {
1773 if(unlikely(pid <= 0 || pid > pid_max)) {
1774 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1778 struct pid_stat *p = get_pid_entry(pid);
1779 if(unlikely(!p || p->read)) return 0;
1782 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1784 // --------------------------------------------------------------------
1787 if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p))))
1788 // there is no reason to proceed if we cannot get its status
1791 read_proc_pid_ownership(p);
1793 // check its parent pid
1794 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1795 error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
1799 // --------------------------------------------------------------------
1802 managed_log(p, PID_LOG_IO, read_proc_pid_io(p));
1804 // --------------------------------------------------------------------
1805 // /proc/<pid>/statm
1807 if(unlikely(!managed_log(p, PID_LOG_STATM, read_proc_pid_statm(p))))
1808 // there is no reason to proceed if we cannot get its memory status
1811 // --------------------------------------------------------------------
1814 // check if it is target
1815 // we do this only once, the first time this pid is loaded
1816 if(unlikely(p->new_entry)) {
1817 // /proc/<pid>/cmdline
1818 if(likely(proc_pid_cmdline_is_needed))
1819 managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
1822 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1824 assign_target_to_pid(p);
1827 // --------------------------------------------------------------------
1830 if(enable_file_charts)
1831 managed_log(p, PID_LOG_FDS, read_pid_file_descriptors(p));
1833 // --------------------------------------------------------------------
1836 if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1837 fprintf(stderr, "Read process %d (%s) sortlisted %d, but its parent %d (%s) sortlisted %d, is not read\n", p->pid, p->comm, p->sortlist, all_pids[p->ppid]->pid, all_pids[p->ppid]->comm, all_pids[p->ppid]->sortlist);
1839 // mark it as updated
1847 static int collect_data_for_all_processes(void) {
1848 struct pid_stat *p = NULL;
1850 if(all_pids_count) {
1852 for(p = root_of_pids; p ; p = p->next) {
1853 p->read = 0; // mark it as not read, so that collect_data_for_pid() will read it
1857 p->children_count = 0;
1860 all_pids_sortlist[slc++] = p->pid;
1863 if(unlikely(slc != all_pids_count)) {
1864 error("Internal error: I was thinking I had %zu processes in my arrays, but it seems there are more.", all_pids_count);
1865 all_pids_count = slc;
1868 if(include_exited_childs) {
1869 // Read parents before childs
1870 // This is needed to prevent a situation where
1871 // a child is found running, but until we read
1872 // its parent, it has exited and its parent
1873 // has accumulated its resources.
1875 qsort((void *)all_pids_sortlist, (size_t)all_pids_count, sizeof(pid_t), compar_pid);
1877 // we forward read all running processes
1878 // collect_data_for_pid() is smart enough,
1879 // not to read the same pid twice per iterations
1880 for(slc = 0; slc < all_pids_count; slc++)
1881 collect_data_for_pid(all_pids_sortlist[slc]);
1885 char dirname[FILENAME_MAX + 1];
1887 snprintfz(dirname, FILENAME_MAX, "%s/proc", global_host_prefix);
1888 DIR *dir = opendir(dirname);
1891 struct dirent *de = NULL;
1893 while((de = readdir(dir))) {
1894 char *endptr = de->d_name;
1896 if(unlikely(de->d_type != DT_DIR || de->d_name[0] < '0' || de->d_name[0] > '9'))
1899 pid_t pid = (pid_t) strtoul(de->d_name, &endptr, 10);
1901 // make sure we read a valid number
1902 if(unlikely(endptr == de->d_name || *endptr != '\0'))
1905 collect_data_for_pid(pid);
1912 // we need /proc/stat to normalize the cpu consumption of the exited childs
1915 // build the process tree
1916 link_all_processes_to_their_parents();
1918 // normally this is done
1919 // however we may have processes exited while we collected values
1920 // so let's find the exited ones
1921 // we do this by collecting the ownership of process
1922 // if we manage to get the ownership, the process still runs
1923 process_exited_processes();
1928 // ----------------------------------------------------------------------------
1929 // update statistics on the targets
1931 // 1. link all childs to their parents
1932 // 2. go from bottom to top, marking as merged all childs to their parents
1933 // this step links all parents without a target to the child target, if any
1934 // 3. link all top level processes (the ones not merged) to the default target
1935 // 4. go from top to bottom, linking all childs without a target, to their parent target
1936 // after this step, all processes have a target
1937 // [5. for each killed pid (updated = 0), remove its usage from its target]
1938 // 6. zero all apps_groups_targets
1939 // 7. concentrate all values on the apps_groups_targets
1940 // 8. remove all killed processes
1941 // 9. find the unique file count for each target
1942 // check: update_apps_groups_statistics()
1944 static void cleanup_exited_pids(void) {
1946 struct pid_stat *p = NULL;
1948 for(p = root_of_pids; p ;) {
1949 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1950 if(unlikely(debug && (p->keep || p->keeploops)))
1951 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1953 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1954 file_descriptor_not_used(p->fds[c]);
1963 if(unlikely(p->keep)) p->keeploops++;
1970 static void apply_apps_groups_targets_inheritance(void) {
1971 struct pid_stat *p = NULL;
1973 // children that do not have a target
1974 // inherit their target from their parent
1975 int found = 1, loops = 0;
1977 if(unlikely(debug)) loops++;
1979 for(p = root_of_pids; p ; p = p->next) {
1980 // if this process does not have a target
1981 // and it has a parent
1982 // and its parent has a target
1983 // then, set the parent's target to this process
1984 if(unlikely(!p->target && p->parent && p->parent->target)) {
1985 p->target = p->parent->target;
1988 if(debug || (p->target && p->target->debug))
1989 fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s).\n", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
1994 // find all the procs with 0 childs and merge them to their parents
1995 // repeat, until nothing more can be done.
1999 if(unlikely(debug)) loops++;
2002 for(p = root_of_pids; p ; p = p->next) {
2003 if(unlikely(!p->sortlist && !p->children_count))
2004 p->sortlist = sortlist++;
2006 // if this process does not have any children
2007 // and is not already merged
2009 // and its parent has children
2010 // and the target of this process and its parent is the same, or the parent does not have a target
2011 // and its parent is not init
2012 // then, mark them as merged.
2017 && p->parent->children_count
2018 && (p->target == p->parent->target || !p->parent->target)
2021 p->parent->children_count--;
2024 // the parent inherits the child's target, if it does not have a target itself
2025 if(unlikely(p->target && !p->parent->target)) {
2026 p->parent->target = p->target;
2028 if(debug || (p->target && p->target->debug))
2029 fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its child %d (%s).\n", p->target->name, p->parent->pid, p->parent->comm, p->pid, p->comm);
2037 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
2040 // init goes always to default target
2042 all_pids[1]->target = apps_groups_default_target;
2044 // give a default target on all top level processes
2045 if(unlikely(debug)) loops++;
2046 for(p = root_of_pids; p ; p = p->next) {
2047 // if the process is not merged itself
2048 // then is is a top level process
2049 if(unlikely(!p->merged && !p->target))
2050 p->target = apps_groups_default_target;
2052 // make sure all processes have a sortlist
2053 if(unlikely(!p->sortlist))
2054 p->sortlist = sortlist++;
2058 all_pids[1]->sortlist = sortlist++;
2060 // give a target to all merged child processes
2063 if(unlikely(debug)) loops++;
2065 for(p = root_of_pids; p ; p = p->next) {
2066 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
2067 p->target = p->parent->target;
2070 if(debug || (p->target && p->target->debug))
2071 fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s) at phase 2.\n", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
2077 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
2080 static size_t zero_all_targets(struct target *root) {
2084 for (w = root; w ; w = w->next) {
2102 w->statm_resident = 0;
2104 // w->statm_text = 0;
2105 // w->statm_lib = 0;
2106 // w->statm_data = 0;
2107 // w->statm_dirty = 0;
2109 w->io_logical_bytes_read = 0;
2110 w->io_logical_bytes_written = 0;
2111 // w->io_read_calls = 0;
2112 // w->io_write_calls = 0;
2113 w->io_storage_bytes_read = 0;
2114 w->io_storage_bytes_written = 0;
2115 // w->io_cancelled_write_bytes = 0;
2117 // zero file counters
2119 memset(w->target_fds, 0, sizeof(int) * w->target_fds_size);
2123 w->openinotifies = 0;
2124 w->openeventfds = 0;
2125 w->opentimerfds = 0;
2126 w->opensignalfds = 0;
2127 w->openeventpolls = 0;
2135 static inline void reallocate_target_fds(struct target *w) {
2139 if(unlikely(!w->target_fds || w->target_fds_size < all_files_size)) {
2140 w->target_fds = reallocz(w->target_fds, sizeof(int) * all_files_size);
2141 memset(&w->target_fds[w->target_fds_size], 0, sizeof(int) * (all_files_size - w->target_fds_size));
2142 w->target_fds_size = all_files_size;
2146 static inline void aggregate_fd_on_target(int fd, struct target *w) {
2150 if(unlikely(w->target_fds[fd])) {
2151 // it is already aggregated
2152 // just increase its usage counter
2153 w->target_fds[fd]++;
2157 // increase its usage counter
2158 // so that we will not add it again
2159 w->target_fds[fd]++;
2161 switch(all_files[fd].type) {
2170 case FILETYPE_SOCKET:
2174 case FILETYPE_INOTIFY:
2178 case FILETYPE_EVENTFD:
2182 case FILETYPE_TIMERFD:
2186 case FILETYPE_SIGNALFD:
2190 case FILETYPE_EVENTPOLL:
2191 w->openeventpolls++;
2200 static inline void aggregate_pid_fds_on_targets(struct pid_stat *p) {
2202 if(unlikely(!p->updated)) {
2203 // the process is not running
2207 struct target *w = p->target, *u = p->user_target, *g = p->group_target;
2209 reallocate_target_fds(w);
2210 reallocate_target_fds(u);
2211 reallocate_target_fds(g);
2213 int c, size = p->fds_size, *fds = p->fds;
2214 for(c = 0; c < size ;c++) {
2217 if(likely(fd <= 0 || fd >= all_files_size))
2220 aggregate_fd_on_target(fd, w);
2221 aggregate_fd_on_target(fd, u);
2222 aggregate_fd_on_target(fd, g);
2226 static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
2229 if(unlikely(!p->updated)) {
2230 // the process is not running
2235 error("pid %d %s was left without a target!", p->pid, p->comm);
2239 w->cutime += p->cutime;
2240 w->cstime += p->cstime;
2241 w->cgtime += p->cgtime;
2242 w->cminflt += p->cminflt;
2243 w->cmajflt += p->cmajflt;
2245 w->utime += p->utime;
2246 w->stime += p->stime;
2247 w->gtime += p->gtime;
2248 w->minflt += p->minflt;
2249 w->majflt += p->majflt;
2251 // w->rss += p->rss;
2253 w->statm_size += p->statm_size;
2254 w->statm_resident += p->statm_resident;
2255 w->statm_share += p->statm_share;
2256 // w->statm_text += p->statm_text;
2257 // w->statm_lib += p->statm_lib;
2258 // w->statm_data += p->statm_data;
2259 // w->statm_dirty += p->statm_dirty;
2261 w->io_logical_bytes_read += p->io_logical_bytes_read;
2262 w->io_logical_bytes_written += p->io_logical_bytes_written;
2263 // w->io_read_calls += p->io_read_calls;
2264 // w->io_write_calls += p->io_write_calls;
2265 w->io_storage_bytes_read += p->io_storage_bytes_read;
2266 w->io_storage_bytes_written += p->io_storage_bytes_written;
2267 // w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
2270 w->num_threads += p->num_threads;
2272 if(unlikely(debug || w->debug))
2273 fprintf(stderr, "apps.plugin: \taggregating '%s' pid %d on target '%s' utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "\n", p->comm, p->pid, w->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
2276 static void calculate_netdata_statistics(void) {
2278 apply_apps_groups_targets_inheritance();
2280 zero_all_targets(users_root_target);
2281 zero_all_targets(groups_root_target);
2282 apps_groups_targets_count = zero_all_targets(apps_groups_root_target);
2284 // this has to be done, before the cleanup
2285 struct pid_stat *p = NULL;
2286 struct target *w = NULL, *o = NULL;
2288 // concentrate everything on the targets
2289 for(p = root_of_pids; p ; p = p->next) {
2291 // --------------------------------------------------------------------
2292 // apps_groups target
2294 aggregate_pid_on_target(p->target, p, NULL);
2297 // --------------------------------------------------------------------
2301 if(likely(p->user_target && p->user_target->uid == p->uid))
2304 if(unlikely(debug && p->user_target))
2305 fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched user from %u (%s) to %u.\n", p->pid, p->comm, p->user_target->uid, p->user_target->name, p->uid);
2307 w = p->user_target = get_users_target(p->uid);
2310 aggregate_pid_on_target(w, p, o);
2313 // --------------------------------------------------------------------
2314 // user group target
2316 o = p->group_target;
2317 if(likely(p->group_target && p->group_target->gid == p->gid))
2318 w = p->group_target;
2320 if(unlikely(debug && p->group_target))
2321 fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched group from %u (%s) to %u.\n", p->pid, p->comm, p->group_target->gid, p->group_target->name, p->gid);
2323 w = p->group_target = get_groups_target(p->gid);
2326 aggregate_pid_on_target(w, p, o);
2329 // --------------------------------------------------------------------
2330 // aggregate all file descriptors
2332 if(enable_file_charts)
2333 aggregate_pid_fds_on_targets(p);
2336 cleanup_exited_pids();
2339 // ----------------------------------------------------------------------------
2340 // update chart dimensions
2342 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2344 static inline void send_BEGIN(const char *type, const char *id, usec_t usec) {
2345 fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2348 static inline void send_SET(const char *name, kernel_uint_t value) {
2349 fprintf(stdout, "SET %s = " KERNEL_UINT_FORMAT "\n", name, value);
2352 static inline void send_END(void) {
2353 fprintf(stdout, "END\n");
2356 static usec_t send_resource_usage_to_netdata() {
2357 static struct timeval last = { 0, 0 };
2358 static struct rusage me_last;
2368 now_monotonic_timeval(&last);
2369 getrusage(RUSAGE_SELF, &me_last);
2371 // the first time, give a zero to allow
2372 // netdata calibrate to the current time
2373 // usec = update_every * USEC_PER_SEC;
2379 now_monotonic_timeval(&now);
2380 getrusage(RUSAGE_SELF, &me);
2382 usec = dt_usec(&now, &last);
2383 cpuuser = me.ru_utime.tv_sec * USEC_PER_SEC + me.ru_utime.tv_usec;
2384 cpusyst = me.ru_stime.tv_sec * USEC_PER_SEC + me.ru_stime.tv_usec;
2386 memmove(&last, &now, sizeof(struct timeval));
2387 memmove(&me_last, &me, sizeof(struct rusage));
2391 "BEGIN netdata.apps_cpu %llu\n"
2393 "SET system = %llu\n"
2395 "BEGIN netdata.apps_files %llu\n"
2399 "SET targets = %zu\n"
2401 "BEGIN netdata.apps_fix %llu\n"
2415 , apps_groups_targets_count
2417 , (unsigned int)(utime_fix_ratio * 100 * RATES_DETAIL)
2418 , (unsigned int)(stime_fix_ratio * 100 * RATES_DETAIL)
2419 , (unsigned int)(gtime_fix_ratio * 100 * RATES_DETAIL)
2420 , (unsigned int)(minflt_fix_ratio * 100 * RATES_DETAIL)
2421 , (unsigned int)(majflt_fix_ratio * 100 * RATES_DETAIL)
2424 if(include_exited_childs)
2426 "BEGIN netdata.apps_children_fix %llu\n"
2430 "SET cminflt = %u\n"
2431 "SET cmajflt = %u\n"
2434 , (unsigned int)(cutime_fix_ratio * 100 * RATES_DETAIL)
2435 , (unsigned int)(cstime_fix_ratio * 100 * RATES_DETAIL)
2436 , (unsigned int)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2437 , (unsigned int)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2438 , (unsigned int)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2444 static void normalize_utilization(struct target *root) {
2447 // childs processing introduces spikes
2448 // here we try to eliminate them by disabling childs processing either for specific dimensions
2449 // or entirely. Of course, either way, we disable it just a single iteration.
2451 kernel_uint_t max_time = processors * hz * RATES_DETAIL;
2452 kernel_uint_t utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2454 if(global_utime > max_time) global_utime = max_time;
2455 if(global_stime > max_time) global_stime = max_time;
2456 if(global_gtime > max_time) global_gtime = max_time;
2458 for(w = root; w ; w = w->next) {
2459 if(w->target || (!w->processes && !w->exposed)) continue;
2464 cutime += w->cutime;
2465 cstime += w->cstime;
2466 cgtime += w->cgtime;
2468 minflt += w->minflt;
2469 majflt += w->majflt;
2470 cminflt += w->cminflt;
2471 cmajflt += w->cmajflt;
2474 if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
2475 if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
2476 // everything we collected fits
2482 cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
2484 else if(global_utime + global_stime > utime + stime) {
2485 // childrens resources are too high
2486 // lower only the children resources
2489 gtime_fix_ratio = 1.0;
2492 cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
2495 // even running processes are unrealistic
2496 // zero the children resources
2497 // lower the running processes resources
2500 gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
2503 cgtime_fix_ratio = 0.0;
2512 cgtime_fix_ratio = 0.0;
2515 if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
2516 if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
2517 if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
2518 if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
2519 if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
2520 if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
2522 // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
2523 // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
2524 // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
2525 // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
2526 // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
2527 // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
2530 // we use cpu time to normalize page faults
2531 // the problem is that to find the proper max values
2532 // for page faults we have to parse /proc/vmstat
2533 // which is quite big to do it again (netdata does it already)
2535 // a better solution could be to somehow have netdata
2536 // do this normalization for us
2538 if(utime || stime || gtime)
2540 minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
2543 majflt_fix_ratio = 1.0;
2545 if(cutime || cstime || cgtime)
2547 cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
2550 cmajflt_fix_ratio = 1.0;
2554 if(unlikely(debug)) {
2556 "SYSTEM: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
2557 "COLLECTED: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
2558 "DELTA: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
2559 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2560 "FINALLY: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
2571 , utime + cutime - global_utime
2572 , stime + cstime - global_stime
2573 , gtime + cgtime - global_gtime
2580 , (kernel_uint_t)(utime * utime_fix_ratio)
2581 , (kernel_uint_t)(stime * stime_fix_ratio)
2582 , (kernel_uint_t)(gtime * gtime_fix_ratio)
2583 , (kernel_uint_t)(cutime * cutime_fix_ratio)
2584 , (kernel_uint_t)(cstime * cstime_fix_ratio)
2585 , (kernel_uint_t)(cgtime * cgtime_fix_ratio)
2590 static void send_collected_data_to_netdata(struct target *root, const char *type, usec_t usec) {
2593 send_BEGIN(type, "cpu", usec);
2594 for (w = root; w ; w = w->next) {
2595 if(unlikely(w->exposed))
2596 send_SET(w->name, (kernel_uint_t)(w->utime * utime_fix_ratio) + (kernel_uint_t)(w->stime * stime_fix_ratio) + (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cutime * cutime_fix_ratio) + (kernel_uint_t)(w->cstime * cstime_fix_ratio) + (kernel_uint_t)(w->cgtime * cgtime_fix_ratio)):0ULL));
2600 send_BEGIN(type, "cpu_user", usec);
2601 for (w = root; w ; w = w->next) {
2602 if(unlikely(w->exposed))
2603 send_SET(w->name, (kernel_uint_t)(w->utime * utime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cutime * cutime_fix_ratio)):0ULL));
2607 send_BEGIN(type, "cpu_system", usec);
2608 for (w = root; w ; w = w->next) {
2609 if(unlikely(w->exposed))
2610 send_SET(w->name, (kernel_uint_t)(w->stime * stime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cstime * cstime_fix_ratio)):0ULL));
2614 if(show_guest_time) {
2615 send_BEGIN(type, "cpu_guest", usec);
2616 for (w = root; w ; w = w->next) {
2617 if(unlikely(w->exposed))
2618 send_SET(w->name, (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cgtime * cgtime_fix_ratio)):0ULL));
2623 send_BEGIN(type, "threads", usec);
2624 for (w = root; w ; w = w->next) {
2625 if(unlikely(w->exposed))
2626 send_SET(w->name, w->num_threads);
2630 send_BEGIN(type, "processes", usec);
2631 for (w = root; w ; w = w->next) {
2632 if(unlikely(w->exposed))
2633 send_SET(w->name, w->processes);
2637 send_BEGIN(type, "mem", usec);
2638 for (w = root; w ; w = w->next) {
2639 if(unlikely(w->exposed))
2640 send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2644 send_BEGIN(type, "vmem", usec);
2645 for (w = root; w ; w = w->next) {
2646 if(unlikely(w->exposed))
2647 send_SET(w->name, w->statm_size);
2651 send_BEGIN(type, "minor_faults", usec);
2652 for (w = root; w ; w = w->next) {
2653 if(unlikely(w->exposed))
2654 send_SET(w->name, (kernel_uint_t)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cminflt * cminflt_fix_ratio)):0ULL));
2658 send_BEGIN(type, "major_faults", usec);
2659 for (w = root; w ; w = w->next) {
2660 if(unlikely(w->exposed))
2661 send_SET(w->name, (kernel_uint_t)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2665 send_BEGIN(type, "lreads", usec);
2666 for (w = root; w ; w = w->next) {
2667 if(unlikely(w->exposed))
2668 send_SET(w->name, w->io_logical_bytes_read);
2672 send_BEGIN(type, "lwrites", usec);
2673 for (w = root; w ; w = w->next) {
2674 if(unlikely(w->exposed))
2675 send_SET(w->name, w->io_logical_bytes_written);
2679 send_BEGIN(type, "preads", usec);
2680 for (w = root; w ; w = w->next) {
2681 if(unlikely(w->exposed))
2682 send_SET(w->name, w->io_storage_bytes_read);
2686 send_BEGIN(type, "pwrites", usec);
2687 for (w = root; w ; w = w->next) {
2688 if(unlikely(w->exposed))
2689 send_SET(w->name, w->io_storage_bytes_written);
2693 if(enable_file_charts) {
2694 send_BEGIN(type, "files", usec);
2695 for (w = root; w; w = w->next) {
2696 if (unlikely(w->exposed))
2697 send_SET(w->name, w->openfiles);
2701 send_BEGIN(type, "sockets", usec);
2702 for (w = root; w; w = w->next) {
2703 if (unlikely(w->exposed))
2704 send_SET(w->name, w->opensockets);
2708 send_BEGIN(type, "pipes", usec);
2709 for (w = root; w; w = w->next) {
2710 if (unlikely(w->exposed))
2711 send_SET(w->name, w->openpipes);
2718 // ----------------------------------------------------------------------------
2719 // generate the charts
2721 static void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2724 int newly_added = 0;
2726 for(w = root ; w ; w = w->next) {
2727 if (w->target) continue;
2729 if (!w->exposed && w->processes) {
2732 if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2736 // nothing more to show
2737 if(!newly_added && show_guest_time == show_guest_time_old) return;
2739 // we have something new to show
2740 // update the charts
2741 fprintf(stdout, "CHART %s.cpu '' '%s CPU Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu stacked 20001 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2742 for (w = root; w ; w = w->next) {
2743 if(unlikely(w->exposed))
2744 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2747 fprintf(stdout, "CHART %s.mem '' '%s Real Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2748 for (w = root; w ; w = w->next) {
2749 if(unlikely(w->exposed))
2750 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2753 fprintf(stdout, "CHART %s.vmem '' '%s Virtual Memory Size' 'MB' mem %s.vmem stacked 20004 %d\n", type, title, type, update_every);
2754 for (w = root; w ; w = w->next) {
2755 if(unlikely(w->exposed))
2756 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2759 fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2760 for (w = root; w ; w = w->next) {
2761 if(unlikely(w->exposed))
2762 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2765 fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2766 for (w = root; w ; w = w->next) {
2767 if(unlikely(w->exposed))
2768 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2771 fprintf(stdout, "CHART %s.cpu_user '' '%s CPU User Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_user stacked 20020 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2772 for (w = root; w ; w = w->next) {
2773 if(unlikely(w->exposed))
2774 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2777 fprintf(stdout, "CHART %s.cpu_system '' '%s CPU System Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20021 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2778 for (w = root; w ; w = w->next) {
2779 if(unlikely(w->exposed))
2780 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2783 if(show_guest_time) {
2784 fprintf(stdout, "CHART %s.cpu_guest '' '%s CPU Guest Time (%d%% = %d core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20022 %d\n", type, title, (processors * 100), processors, (processors > 1) ? "s" : "", type, update_every);
2785 for (w = root; w; w = w->next) {
2786 if(unlikely(w->exposed))
2787 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2791 fprintf(stdout, "CHART %s.major_faults '' '%s Major Page Faults (swap read)' 'page faults/s' swap %s.major_faults stacked 20010 %d\n", type, title, type, update_every);
2792 for (w = root; w ; w = w->next) {
2793 if(unlikely(w->exposed))
2794 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2797 fprintf(stdout, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
2798 for (w = root; w ; w = w->next) {
2799 if(unlikely(w->exposed))
2800 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2803 fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2804 for (w = root; w ; w = w->next) {
2805 if(unlikely(w->exposed))
2806 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2809 fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2810 for (w = root; w ; w = w->next) {
2811 if(unlikely(w->exposed))
2812 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2815 fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2816 for (w = root; w ; w = w->next) {
2817 if(unlikely(w->exposed))
2818 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2821 fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2822 for (w = root; w ; w = w->next) {
2823 if(unlikely(w->exposed))
2824 fprintf(stdout, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2827 if(enable_file_charts) {
2828 fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type,
2829 title, type, update_every);
2830 for (w = root; w; w = w->next) {
2831 if (unlikely(w->exposed))
2832 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2835 fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n",
2836 type, title, type, update_every);
2837 for (w = root; w; w = w->next) {
2838 if (unlikely(w->exposed))
2839 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2842 fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type,
2843 title, type, update_every);
2844 for (w = root; w; w = w->next) {
2845 if (unlikely(w->exposed))
2846 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
2852 // ----------------------------------------------------------------------------
2853 // parse command line arguments
2855 static void parse_args(int argc, char **argv)
2860 for(i = 1; i < argc; i++) {
2862 int n = (int)str2l(argv[i]);
2869 if(strcmp("version", argv[i]) == 0 || strcmp("-v", argv[i]) == 0) {
2870 printf("apps.plugin %s\n", VERSION);
2874 if(strcmp("debug", argv[i]) == 0) {
2876 // debug_flags = 0xffffffff;
2880 if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
2881 include_exited_childs = 0;
2885 if(strcmp("with-childs", argv[i]) == 0) {
2886 include_exited_childs = 1;
2890 if(strcmp("with-guest", argv[i]) == 0) {
2891 enable_guest_charts = 1;
2895 if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
2896 enable_guest_charts = 0;
2900 if(strcmp("with-files", argv[i]) == 0) {
2901 enable_file_charts = 1;
2905 if(strcmp("no-files", argv[i]) == 0 || strcmp("without-files", argv[i]) == 0) {
2906 enable_file_charts = 0;
2910 if(strcmp("no-users", argv[i]) == 0 || strcmp("without-users", argv[i]) == 0) {
2911 enable_users_charts = 0;
2915 if(strcmp("no-groups", argv[i]) == 0 || strcmp("without-groups", argv[i]) == 0) {
2916 enable_groups_charts = 0;
2920 if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
2923 " netdata apps.plugin %s\n"
2924 " Copyright (C) 2016-2017 Costa Tsaousis <costa@tsaousis.gr>\n"
2925 " Released under GNU Public License v3 or later.\n"
2926 " All rights reserved.\n"
2928 " This program is a data collector plugin for netdata.\n"
2930 " Valid command line options:\n"
2932 " SECONDS set the data collection frequency\n"
2934 " debug enable debugging (lot of output)\n"
2937 " without-childs enable / disable aggregating exited\n"
2938 " children resources into parents\n"
2939 " (default is enabled)\n"
2942 " without-guest enable / disable reporting guest charts\n"
2943 " (default is disabled)\n"
2946 " without-files enable / disable reporting files, sockets, pipes\n"
2947 " (default is enabled)\n"
2949 " NAME read apps_NAME.conf instead of\n"
2950 " apps_groups.conf\n"
2951 " (default NAME=groups)\n"
2953 " version print program version and exit\n"
2965 error("Cannot understand option %s", argv[i]);
2969 if(freq > 0) update_every = freq;
2970 if(!name) name = "groups";
2972 if(read_apps_groups_conf(name)) {
2973 error("Cannot read process groups '%s/apps_%s.conf'. There are no internal defaults. Failing.", config_dir, name);
2978 int main(int argc, char **argv)
2980 // debug_flags = D_PROCFILE;
2982 // set the name for logging
2983 program_name = "apps.plugin";
2985 info("started on pid %d", getpid());
2987 // disable syslog for apps.plugin
2988 error_log_syslog = 0;
2990 // set errors flood protection to 100 logs per hour
2991 error_log_errors_per_period = 100;
2992 error_log_throttle_period = 3600;
2994 global_host_prefix = getenv("NETDATA_HOST_PREFIX");
2995 if(global_host_prefix == NULL) {
2996 // info("NETDATA_HOST_PREFIX is not passed from netdata");
2997 global_host_prefix = "";
2999 // else info("Found NETDATA_HOST_PREFIX='%s'", global_host_prefix);
3001 config_dir = getenv("NETDATA_CONFIG_DIR");
3002 if(config_dir == NULL) {
3003 // info("NETDATA_CONFIG_DIR is not passed from netdata");
3004 config_dir = CONFIG_DIR;
3006 // else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
3008 #ifdef NETDATA_INTERNAL_CHECKS
3009 if(debug_flags != 0) {
3010 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
3011 if(setrlimit(RLIMIT_CORE, &rl) != 0)
3012 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
3013 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
3015 #endif /* NETDATA_INTERNAL_CHECKS */
3017 procfile_adaptive_initial_allocation = 1;
3019 time_t started_t = now_monotonic_sec();
3021 get_system_pid_max();
3024 parse_args(argc, argv);
3026 all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
3027 all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
3030 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
3031 "DIMENSION user '' incremental 1 1000\n"
3032 "DIMENSION system '' incremental 1 1000\n"
3033 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
3034 "DIMENSION files '' incremental 1 1\n"
3035 "DIMENSION pids '' absolute 1 1\n"
3036 "DIMENSION fds '' absolute 1 1\n"
3037 "DIMENSION targets '' absolute 1 1\n"
3038 "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
3039 "DIMENSION utime '' absolute 1 %2$llu\n"
3040 "DIMENSION stime '' absolute 1 %2$llu\n"
3041 "DIMENSION gtime '' absolute 1 %2$llu\n"
3042 "DIMENSION minflt '' absolute 1 %2$llu\n"
3043 "DIMENSION majflt '' absolute 1 %2$llu\n"
3048 if(include_exited_childs)
3050 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
3051 "DIMENSION cutime '' absolute 1 %2$llu\n"
3052 "DIMENSION cstime '' absolute 1 %2$llu\n"
3053 "DIMENSION cgtime '' absolute 1 %2$llu\n"
3054 "DIMENSION cminflt '' absolute 1 %2$llu\n"
3055 "DIMENSION cmajflt '' absolute 1 %2$llu\n"
3060 usec_t step = update_every * USEC_PER_SEC;
3061 global_iterations_counter = 1;
3063 heartbeat_init(&hb);
3064 for(;1; global_iterations_counter++) {
3066 #ifdef NETDATA_PROFILING
3067 #warning "compiling for profiling"
3068 static int profiling_count=0;
3070 if(unlikely(profiling_count > 1000)) exit(0);
3072 heartbeat_next(&hb, step);
3075 if(!collect_data_for_all_processes()) {
3076 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
3077 printf("DISABLE\n");
3081 calculate_netdata_statistics();
3082 normalize_utilization(apps_groups_root_target);
3084 usec_t dt = send_resource_usage_to_netdata();
3086 // this is smart enough to show only newly added apps, when needed
3087 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
3089 if(likely(enable_users_charts))
3090 send_charts_updates_to_netdata(users_root_target, "users", "Users");
3092 if(likely(enable_groups_charts))
3093 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
3095 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
3097 if(likely(enable_users_charts))
3098 send_collected_data_to_netdata(users_root_target, "users", dt);
3100 if(likely(enable_groups_charts))
3101 send_collected_data_to_netdata(groups_root_target, "groups", dt);
3105 show_guest_time_old = show_guest_time;
3108 fprintf(stderr, "apps.plugin: done Loop No %zu\n", global_iterations_counter);
3110 // restart check (14400 seconds)
3111 if(now_monotonic_sec() - started_t > 14400) exit(0);