3 #define MAX_COMPARE_NAME 100
5 #define MAX_CMDLINE 1024
7 // the rates we are going to send to netdata
8 // will have this detail
10 // 1 will send just integer parts to netdata
11 // 100 will send 2 decimal points
12 // 1000 will send 3 decimal points
14 #define RATES_DETAIL 10000ULL
19 unsigned long long global_iterations_counter = 1;
20 unsigned long long file_counter = 0;
21 int proc_pid_cmdline_is_needed = 0;
22 int include_exited_childs = 1;
23 char *config_dir = CONFIG_DIR;
25 pid_t *all_pids_sortlist = NULL;
27 // will be automatically set to 1, if guest values are collected
28 int show_guest_time = 0;
29 int show_guest_time_old = 0;
31 int enable_guest_charts = 0;
32 int enable_file_charts = 1;
33 int enable_users_charts = 1;
34 int enable_groups_charts = 1;
36 // ----------------------------------------------------------------------------
38 void netdata_cleanup_and_exit(int ret) {
43 // ----------------------------------------------------------------------------
45 // target is the structure that process data are aggregated
48 char compare[MAX_COMPARE_NAME + 1];
52 char id[MAX_NAME + 1];
55 char name[MAX_NAME + 1];
60 unsigned long long minflt;
61 unsigned long long cminflt;
62 unsigned long long majflt;
63 unsigned long long cmajflt;
64 unsigned long long utime;
65 unsigned long long stime;
66 unsigned long long gtime;
67 unsigned long long cutime;
68 unsigned long long cstime;
69 unsigned long long cgtime;
70 unsigned long long num_threads;
71 // unsigned long long rss;
73 unsigned long long statm_size;
74 unsigned long long statm_resident;
75 unsigned long long statm_share;
76 // unsigned long long statm_text;
77 // unsigned long long statm_lib;
78 // unsigned long long statm_data;
79 // unsigned long long statm_dirty;
81 unsigned long long io_logical_bytes_read;
82 unsigned long long io_logical_bytes_written;
83 // unsigned long long io_read_calls;
84 // unsigned long long io_write_calls;
85 unsigned long long io_storage_bytes_read;
86 unsigned long long io_storage_bytes_written;
87 // unsigned long long io_cancelled_write_bytes;
90 unsigned long long openfiles;
91 unsigned long long openpipes;
92 unsigned long long opensockets;
93 unsigned long long openinotifies;
94 unsigned long long openeventfds;
95 unsigned long long opentimerfds;
96 unsigned long long opensignalfds;
97 unsigned long long openeventpolls;
98 unsigned long long openother;
100 unsigned long processes; // how many processes have been merged to this
101 int exposed; // if set, we have sent this to netdata
102 int hidden; // if set, we set the hidden flag on the dimension
105 int starts_with; // if set, the compare string matches only the
106 // beginning of the command
108 struct target *target; // the one that will be reported to netdata
113 // ----------------------------------------------------------------------------
115 // aggregate all processes in groups, to have a limited number of dimensions
117 struct target *apps_groups_root_target = NULL;
118 struct target *apps_groups_default_target = NULL;
119 long apps_groups_targets = 0;
121 struct target *users_root_target = NULL;
122 struct target *groups_root_target = NULL;
124 static struct target *get_users_target(uid_t uid)
127 for(w = users_root_target ; w ; w = w->next)
128 if(w->uid == uid) return w;
130 w = callocz(sizeof(struct target), 1);
131 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
132 w->comparehash = simple_hash(w->compare);
133 w->comparelen = strlen(w->compare);
135 snprintfz(w->id, MAX_NAME, "%u", uid);
136 w->idhash = simple_hash(w->id);
138 struct passwd *pw = getpwuid(uid);
140 snprintfz(w->name, MAX_NAME, "%u", uid);
142 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
144 netdata_fix_chart_name(w->name);
148 w->next = users_root_target;
149 users_root_target = w;
152 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
157 struct target *get_groups_target(gid_t gid)
160 for(w = groups_root_target ; w ; w = w->next)
161 if(w->gid == gid) return w;
163 w = callocz(sizeof(struct target), 1);
164 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
165 w->comparehash = simple_hash(w->compare);
166 w->comparelen = strlen(w->compare);
168 snprintfz(w->id, MAX_NAME, "%u", gid);
169 w->idhash = simple_hash(w->id);
171 struct group *gr = getgrgid(gid);
173 snprintfz(w->name, MAX_NAME, "%u", gid);
175 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
177 netdata_fix_chart_name(w->name);
181 w->next = groups_root_target;
182 groups_root_target = w;
185 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
190 // find or create a new target
191 // there are targets that are just aggregated to other target (the second argument)
192 static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
193 int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
194 const char *nid = id;
196 // extract the options
197 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
198 if(nid[0] == '-') thidden = 1;
199 if(nid[0] == '+') tdebug = 1;
200 if(nid[0] == '*') ends_with = 1;
203 uint32_t hash = simple_hash(id);
205 // find if it already exists
206 struct target *w, *last = apps_groups_root_target;
207 for(w = apps_groups_root_target ; w ; w = w->next) {
208 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
214 // find an existing target
215 if(unlikely(!target)) {
216 while(*name == '-') {
217 if(*name == '-') thidden = 1;
220 for(target = apps_groups_root_target ; target ; target = target->next) {
221 if(!target->target && strcmp(name, target->name) == 0)
224 if(unlikely(debug)) {
226 fprintf(stderr, "apps.plugin: REUSING TARGET NAME '%s' on ID '%s'\n", target->name, target->id);
228 fprintf(stderr, "apps.plugin: NEW TARGET NAME '%s' on ID '%s'\n", name, id);
232 if(target && target->target)
233 fatal("Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id, target->id, target->target->id);
235 w = callocz(sizeof(struct target), 1);
236 strncpyz(w->id, nid, MAX_NAME);
237 w->idhash = simple_hash(w->id);
239 if(unlikely(!target))
241 strncpyz(w->name, name, MAX_NAME);
244 strncpyz(w->name, nid, MAX_NAME);
246 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
247 size_t len = strlen(w->compare);
248 if(w->compare[len - 1] == '*') {
249 w->compare[len - 1] = '\0';
252 w->ends_with = ends_with;
254 if(w->starts_with && w->ends_with)
255 proc_pid_cmdline_is_needed = 1;
257 w->comparehash = simple_hash(w->compare);
258 w->comparelen = strlen(w->compare);
264 // append it, to maintain the order in apps_groups.conf
265 if(last) last->next = w;
266 else apps_groups_root_target = w;
269 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
271 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
272 , w->target?w->target->name:w->name
273 , (w->hidden)?"hidden":"-"
274 , (w->debug)?"debug":"-"
280 // read the apps_groups.conf file
281 static int read_apps_groups_conf(const char *file)
283 char filename[FILENAME_MAX + 1];
285 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, file);
288 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
290 // ----------------------------------------
292 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
295 procfile_set_quotes(ff, "'\"");
297 ff = procfile_readall(ff);
301 unsigned long line, lines = procfile_lines(ff);
303 for(line = 0; line < lines ;line++) {
304 unsigned long word, words = procfile_linewords(ff, line);
307 char *name = procfile_lineword(ff, line, 0);
308 if(!name || !*name) continue;
310 // find a possibly existing target
311 struct target *w = NULL;
313 // loop through all words, skipping the first one (the name)
314 for(word = 0; word < words ;word++) {
315 char *s = procfile_lineword(ff, line, word);
316 if(!s || !*s) continue;
319 // is this the first word? skip it
320 if(s == name) continue;
323 struct target *n = get_apps_groups_target(s, w, name);
325 error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
329 // just some optimization
330 // to avoid searching for a target for each process
331 if(!w) w = n->target?n->target:n;
337 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
338 if(!apps_groups_default_target)
339 fatal("Cannot create default target");
341 // allow the user to override group 'other'
342 if(apps_groups_default_target->target)
343 apps_groups_default_target = apps_groups_default_target->target;
349 // ----------------------------------------------------------------------------
350 // data to store for each pid
353 #define PID_LOG_IO 0x00000001
354 #define PID_LOG_STATM 0x00000002
355 #define PID_LOG_CMDLINE 0x00000004
356 #define PID_LOG_FDS 0x00000008
357 #define PID_LOG_STAT 0x00000010
361 char comm[MAX_COMPARE_NAME + 1];
362 char cmdline[MAX_CMDLINE + 1];
374 // these are raw values collected
375 unsigned long long minflt_raw;
376 unsigned long long cminflt_raw;
377 unsigned long long majflt_raw;
378 unsigned long long cmajflt_raw;
379 unsigned long long utime_raw;
380 unsigned long long stime_raw;
381 unsigned long long gtime_raw; // guest_time
382 unsigned long long cutime_raw;
383 unsigned long long cstime_raw;
384 unsigned long long cgtime_raw; // cguest_time
387 unsigned long long minflt;
388 unsigned long long cminflt;
389 unsigned long long majflt;
390 unsigned long long cmajflt;
391 unsigned long long utime;
392 unsigned long long stime;
393 unsigned long long gtime;
394 unsigned long long cutime;
395 unsigned long long cstime;
396 unsigned long long cgtime;
401 // int64_t itrealvalue;
402 // unsigned long long starttime;
403 // unsigned long long vsize;
404 // unsigned long long rss;
405 // unsigned long long rsslim;
406 // unsigned long long starcode;
407 // unsigned long long endcode;
408 // unsigned long long startstack;
409 // unsigned long long kstkesp;
410 // unsigned long long kstkeip;
413 // uint64_t sigignore;
414 // uint64_t sigcatch;
418 // int32_t exit_signal;
419 // int32_t processor;
420 // uint32_t rt_priority;
422 // unsigned long long delayacct_blkio_ticks;
427 unsigned long long statm_size;
428 unsigned long long statm_resident;
429 unsigned long long statm_share;
430 // unsigned long long statm_text;
431 // unsigned long long statm_lib;
432 // unsigned long long statm_data;
433 // unsigned long long statm_dirty;
435 unsigned long long io_logical_bytes_read_raw;
436 unsigned long long io_logical_bytes_written_raw;
437 // unsigned long long io_read_calls_raw;
438 // unsigned long long io_write_calls_raw;
439 unsigned long long io_storage_bytes_read_raw;
440 unsigned long long io_storage_bytes_written_raw;
441 // unsigned long long io_cancelled_write_bytes_raw;
443 unsigned long long io_logical_bytes_read;
444 unsigned long long io_logical_bytes_written;
445 // unsigned long long io_read_calls;
446 // unsigned long long io_write_calls;
447 unsigned long long io_storage_bytes_read;
448 unsigned long long io_storage_bytes_written;
449 // unsigned long long io_cancelled_write_bytes;
451 int *fds; // array of fds it uses
452 int fds_size; // the size of the fds array
454 int children_count; // number of processes directly referencing this
455 int keep; // 1 when we need to keep this process in memory even after it exited
456 int keeploops; // increases by 1 every time keep is 1 and updated 0
457 int updated; // 1 when the process is currently running
458 int merged; // 1 when it has been merged to its parent
459 int new_entry; // 1 when this is a new process, just saw for the first time
460 int read; // 1 when we have already read this process for this iteration
461 int sortlist; // higher numbers = top on the process tree
462 // each process gets a unique number
464 struct target *target; // app_groups.conf targets
465 struct target *user_target; // uid based targets
466 struct target *group_target; // gid based targets
468 unsigned long long stat_collected_usec;
469 unsigned long long last_stat_collected_usec;
471 unsigned long long io_collected_usec;
472 unsigned long long last_io_collected_usec;
475 char *statm_filename;
477 char *cmdline_filename;
479 struct pid_stat *parent;
480 struct pid_stat *prev;
481 struct pid_stat *next;
482 } *root_of_pids = NULL, **all_pids;
484 long all_pids_count = 0;
486 static inline struct pid_stat *get_pid_entry(pid_t pid) {
488 all_pids[pid]->new_entry = 0;
489 return all_pids[pid];
492 all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
493 all_pids[pid]->fds = callocz(sizeof(int), 100);
494 all_pids[pid]->fds_size = 100;
496 if(root_of_pids) root_of_pids->prev = all_pids[pid];
497 all_pids[pid]->next = root_of_pids;
498 root_of_pids = all_pids[pid];
500 all_pids[pid]->pid = pid;
501 all_pids[pid]->new_entry = 1;
505 return all_pids[pid];
508 static inline void del_pid_entry(pid_t pid) {
510 error("attempted to free pid %d that is not allocated.", pid);
515 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
517 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
518 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
519 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
521 if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
522 if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
523 if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
524 if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
525 if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
526 freez(all_pids[pid]);
528 all_pids[pid] = NULL;
533 // ----------------------------------------------------------------------------
534 // update pids from proc
536 static inline int read_proc_pid_cmdline(struct pid_stat *p) {
538 if(unlikely(!p->cmdline_filename)) {
539 char filename[FILENAME_MAX + 1];
540 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", global_host_prefix, p->pid);
541 p->cmdline_filename = strdupz(filename);
544 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
545 if(unlikely(fd == -1)) goto cleanup;
547 ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
550 if(unlikely(bytes < 0)) goto cleanup;
552 p->cmdline[bytes] = '\0';
553 for(i = 0; i < bytes ; i++)
554 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
557 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
562 // copy the command to the command line
563 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
567 static inline int read_proc_pid_ownership(struct pid_stat *p) {
568 if(unlikely(!p->stat_filename)) {
569 error("pid %d does not have a stat_filename", p->pid);
573 // ----------------------------------------
577 if(stat(p->stat_filename, &st) != 0) {
578 error("Cannot stat file '%s'", p->stat_filename);
588 static inline int read_proc_pid_stat(struct pid_stat *p) {
589 static procfile *ff = NULL;
591 if(unlikely(!p->stat_filename)) {
592 char filename[FILENAME_MAX + 1];
593 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", global_host_prefix, p->pid);
594 p->stat_filename = strdupz(filename);
597 int set_quotes = (!ff)?1:0;
599 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
600 if(unlikely(!ff)) goto cleanup;
602 // if(set_quotes) procfile_set_quotes(ff, "()");
603 if(set_quotes) procfile_set_open_close(ff, "(", ")");
605 ff = procfile_readall(ff);
606 if(unlikely(!ff)) goto cleanup;
608 p->last_stat_collected_usec = p->stat_collected_usec;
609 p->stat_collected_usec = now_realtime_usec();
612 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
614 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
616 // p->state = *(procfile_lineword(ff, 0, 2));
617 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3));
618 // p->pgrp = atol(procfile_lineword(ff, 0, 4));
619 // p->session = atol(procfile_lineword(ff, 0, 5));
620 // p->tty_nr = atol(procfile_lineword(ff, 0, 6));
621 // p->tpgid = atol(procfile_lineword(ff, 0, 7));
622 // p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
624 unsigned long long last;
626 last = p->minflt_raw;
627 p->minflt_raw = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
628 p->minflt = (p->minflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
630 last = p->cminflt_raw;
631 p->cminflt_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
632 p->cminflt = (p->cminflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
634 last = p->majflt_raw;
635 p->majflt_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
636 p->majflt = (p->majflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
638 last = p->cmajflt_raw;
639 p->cmajflt_raw = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
640 p->cmajflt = (p->cmajflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
643 p->utime_raw = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
644 p->utime = (p->utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
647 p->stime_raw = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
648 p->stime = (p->stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
650 last = p->cutime_raw;
651 p->cutime_raw = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
652 p->cutime = (p->cutime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
654 last = p->cstime_raw;
655 p->cstime_raw = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
656 p->cstime = (p->cstime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
658 // p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
659 // p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
660 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19));
661 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
662 // p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
663 // p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
664 // p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
665 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
666 // p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
667 // p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
668 // p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
669 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
670 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
671 // p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
672 // p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
673 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
674 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
675 // p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
676 // p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
677 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
678 // p->exit_signal = atol(procfile_lineword(ff, 0, 37));
679 // p->processor = atol(procfile_lineword(ff, 0, 38));
680 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
681 // p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
682 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
684 if(enable_guest_charts) {
686 p->gtime_raw = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
687 p->gtime = (p->gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
689 last = p->cgtime_raw;
690 p->cgtime_raw = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
691 p->cgtime = (p->cgtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
693 if (show_guest_time || p->gtime || p->cgtime) {
694 p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
695 p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
700 if(unlikely(debug || (p->target && p->target->debug)))
701 fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu, 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);
703 if(unlikely(global_iterations_counter == 1)) {
734 static inline int read_proc_pid_statm(struct pid_stat *p) {
735 static procfile *ff = NULL;
737 if(unlikely(!p->statm_filename)) {
738 char filename[FILENAME_MAX + 1];
739 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", global_host_prefix, p->pid);
740 p->statm_filename = strdupz(filename);
743 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
744 if(unlikely(!ff)) goto cleanup;
746 ff = procfile_readall(ff);
747 if(unlikely(!ff)) goto cleanup;
751 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
752 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
753 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
754 // p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
755 // p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
756 // p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
757 // p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
763 p->statm_resident = 0;
765 // p->statm_text = 0;
767 // p->statm_data = 0;
768 // p->statm_dirty = 0;
772 static inline int read_proc_pid_io(struct pid_stat *p) {
773 static procfile *ff = NULL;
775 if(unlikely(!p->io_filename)) {
776 char filename[FILENAME_MAX + 1];
777 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", global_host_prefix, p->pid);
778 p->io_filename = strdupz(filename);
782 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
783 if(unlikely(!ff)) goto cleanup;
785 ff = procfile_readall(ff);
786 if(unlikely(!ff)) goto cleanup;
790 p->last_io_collected_usec = p->io_collected_usec;
791 p->io_collected_usec = now_realtime_usec();
793 unsigned long long last;
795 last = p->io_logical_bytes_read_raw;
796 p->io_logical_bytes_read_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
797 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);
799 last = p->io_logical_bytes_written_raw;
800 p->io_logical_bytes_written_raw = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
801 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);
803 // last = p->io_read_calls_raw;
804 // p->io_read_calls_raw = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
805 // p->io_read_calls = (p->io_read_calls_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
807 // last = p->io_write_calls_raw;
808 // p->io_write_calls_raw = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
809 // p->io_write_calls = (p->io_write_calls_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
811 last = p->io_storage_bytes_read_raw;
812 p->io_storage_bytes_read_raw = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
813 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);
815 last = p->io_storage_bytes_written_raw;
816 p->io_storage_bytes_written_raw = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
817 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);
819 // last = p->io_cancelled_write_bytes_raw;
820 // p->io_cancelled_write_bytes_raw = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
821 // 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);
823 if(unlikely(global_iterations_counter == 1)) {
824 p->io_logical_bytes_read = 0;
825 p->io_logical_bytes_written = 0;
826 // p->io_read_calls = 0;
827 // p->io_write_calls = 0;
828 p->io_storage_bytes_read = 0;
829 p->io_storage_bytes_written = 0;
830 // p->io_cancelled_write_bytes = 0;
836 p->io_logical_bytes_read = 0;
837 p->io_logical_bytes_written = 0;
838 // p->io_read_calls = 0;
839 // p->io_write_calls = 0;
840 p->io_storage_bytes_read = 0;
841 p->io_storage_bytes_written = 0;
842 // p->io_cancelled_write_bytes = 0;
846 unsigned long long global_utime = 0;
847 unsigned long long global_stime = 0;
848 unsigned long long global_gtime = 0;
850 static inline int read_proc_stat() {
851 static char filename[FILENAME_MAX + 1] = "";
852 static procfile *ff = NULL;
853 static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
854 static usec_t collected_usec = 0, last_collected_usec = 0;
857 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
858 ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
859 if(unlikely(!ff)) goto cleanup;
862 ff = procfile_readall(ff);
863 if(unlikely(!ff)) goto cleanup;
865 last_collected_usec = collected_usec;
866 collected_usec = now_realtime_usec();
870 unsigned long long last;
873 utime_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
874 global_utime = (utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
876 // nice time, on user time
878 ntime_raw = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
879 global_utime += (ntime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
882 stime_raw = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
883 global_stime = (stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
886 gtime_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
887 global_gtime = (gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
889 if(enable_guest_charts) {
890 // guest nice time, on guest time
892 gntime_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
893 global_gtime += (gntime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
895 // remove guest time from user time
896 global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
899 if(unlikely(global_iterations_counter == 1)) {
915 // ----------------------------------------------------------------------------
917 // this is used to keep a global list of all open files of the system
918 // it is needed in order to calculate the unique files processes have open
920 #define FILE_DESCRIPTORS_INCREASE_STEP 100
922 struct file_descriptor {
924 #ifdef NETDATA_INTERNAL_CHECKS
926 #endif /* NETDATA_INTERNAL_CHECKS */
934 int all_files_len = 0;
935 int all_files_size = 0;
937 int file_descriptor_compare(void* a, void* b) {
938 #ifdef NETDATA_INTERNAL_CHECKS
939 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
940 error("Corrupted index data detected. Please report this.");
941 #endif /* NETDATA_INTERNAL_CHECKS */
943 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
946 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
950 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
953 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
955 avl_tree all_files_index = {
957 file_descriptor_compare
960 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
961 struct file_descriptor tmp;
962 tmp.hash = (hash)?hash:simple_hash(name);
966 #ifdef NETDATA_INTERNAL_CHECKS
967 tmp.magic = 0x0BADCAFE;
968 #endif /* NETDATA_INTERNAL_CHECKS */
970 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
973 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
974 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
976 #define FILETYPE_OTHER 0
977 #define FILETYPE_FILE 1
978 #define FILETYPE_PIPE 2
979 #define FILETYPE_SOCKET 3
980 #define FILETYPE_INOTIFY 4
981 #define FILETYPE_EVENTFD 5
982 #define FILETYPE_EVENTPOLL 6
983 #define FILETYPE_TIMERFD 7
984 #define FILETYPE_SIGNALFD 8
986 static inline void file_descriptor_not_used(int id)
988 if(id > 0 && id < all_files_size) {
990 #ifdef NETDATA_INTERNAL_CHECKS
991 if(all_files[id].magic != 0x0BADCAFE) {
992 error("Ignoring request to remove empty file id %d.", id);
995 #endif /* NETDATA_INTERNAL_CHECKS */
998 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1000 if(all_files[id].count > 0) {
1001 all_files[id].count--;
1003 if(!all_files[id].count) {
1005 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1007 file_descriptor_remove(&all_files[id]);
1008 #ifdef NETDATA_INTERNAL_CHECKS
1009 all_files[id].magic = 0x00000000;
1010 #endif /* NETDATA_INTERNAL_CHECKS */
1015 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1017 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1020 static inline int file_descriptor_find_or_add(const char *name)
1022 static int last_pos = 0;
1023 uint32_t hash = simple_hash(name);
1026 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1028 struct file_descriptor *fd = file_descriptor_find(name, hash);
1032 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1039 // check we have enough memory to add it
1040 if(!all_files || all_files_len == all_files_size) {
1041 void *old = all_files;
1044 // there is no empty slot
1046 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1048 all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1050 // if the address changed, we have to rebuild the index
1051 // since all pointers are now invalid
1052 if(old && old != (void *)all_files) {
1054 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1056 all_files_index.root = NULL;
1057 for(i = 0; i < all_files_size; i++) {
1058 if(!all_files[i].count) continue;
1059 file_descriptor_add(&all_files[i]);
1063 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1066 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1067 all_files[i].count = 0;
1068 all_files[i].name = NULL;
1069 #ifdef NETDATA_INTERNAL_CHECKS
1070 all_files[i].magic = 0x00000000;
1071 #endif /* NETDATA_INTERNAL_CHECKS */
1072 all_files[i].pos = i;
1075 if(!all_files_size) all_files_len = 1;
1076 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1080 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1082 // search for an empty slot
1084 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1085 if(c >= all_files_size) c = 0;
1086 if(c == 0) continue;
1088 if(!all_files[c].count) {
1090 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1092 #ifdef NETDATA_INTERNAL_CHECKS
1093 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1094 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1095 #endif /* NETDATA_INTERNAL_CHECKS */
1098 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);
1100 if(all_files[c].name) freez((void *)all_files[c].name);
1101 all_files[c].name = NULL;
1106 if(i == all_files_size) {
1107 fatal("We should find an empty slot, but there isn't any");
1112 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1116 // else we have an empty slot in 'c'
1119 if(name[0] == '/') type = FILETYPE_FILE;
1120 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1121 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1122 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1123 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1124 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1125 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1126 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1127 else if(strncmp(name, "anon_inode:", 11) == 0) {
1129 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1131 type = FILETYPE_OTHER;
1135 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1137 type = FILETYPE_OTHER;
1140 all_files[c].name = strdupz(name);
1141 all_files[c].hash = hash;
1142 all_files[c].type = type;
1143 all_files[c].pos = c;
1144 all_files[c].count = 1;
1145 #ifdef NETDATA_INTERNAL_CHECKS
1146 all_files[c].magic = 0x0BADCAFE;
1147 #endif /* NETDATA_INTERNAL_CHECKS */
1148 file_descriptor_add(&all_files[c]);
1151 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1156 static inline int read_pid_file_descriptors(struct pid_stat *p) {
1157 char dirname[FILENAME_MAX+1];
1159 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", global_host_prefix, p->pid);
1160 DIR *fds = opendir(dirname);
1164 char fdname[FILENAME_MAX + 1];
1165 char linkname[FILENAME_MAX + 1];
1167 // make the array negative
1168 for(c = 0 ; c < p->fds_size ; c++)
1169 p->fds[c] = -p->fds[c];
1171 while((de = readdir(fds))) {
1172 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1175 // check if the fds array is small
1176 int fdid = atoi(de->d_name);
1177 if(fdid < 0) continue;
1178 if(fdid >= p->fds_size) {
1179 // it is small, extend it
1181 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1183 p->fds = reallocz(p->fds, (fdid + 100) * sizeof(int));
1185 fatal("Cannot re-allocate fds for %s", p->comm);
1189 // and initialize it
1190 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1191 p->fds_size = fdid + 100;
1194 if(p->fds[fdid] == 0) {
1195 // we don't know this fd, get it
1197 sprintf(fdname, "%s/proc/%d/fd/%s", global_host_prefix, p->pid, de->d_name);
1198 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1200 if(debug || (p->target && p->target->debug)) {
1201 if(debug || (p->target && p->target->debug))
1202 error("Cannot read link %s", fdname);
1209 // if another process already has this, we will get
1211 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1214 // else make it positive again, we need it
1215 // of course, the actual file may have changed, but we don't care so much
1216 // FIXME: we could compare the inode as returned by readdir dirent structure
1217 else p->fds[fdid] = -p->fds[fdid];
1221 // remove all the negative file descriptors
1222 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1223 file_descriptor_not_used(-p->fds[c]);
1232 // ----------------------------------------------------------------------------
1234 static inline int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1235 char *prefix = "\\_ ";
1239 indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1243 char buffer[indent + 1];
1246 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1249 fprintf(stderr, " %s %s%s (%d %s %lld"
1254 , p->updated?"running":"exited"
1255 , (long long)p->stat_collected_usec - (long long)time
1258 if(p->utime) fprintf(stderr, " utime=%llu", p->utime);
1259 if(p->stime) fprintf(stderr, " stime=%llu", p->stime);
1260 if(p->gtime) fprintf(stderr, " gtime=%llu", p->gtime);
1261 if(p->cutime) fprintf(stderr, " cutime=%llu", p->cutime);
1262 if(p->cstime) fprintf(stderr, " cstime=%llu", p->cstime);
1263 if(p->cgtime) fprintf(stderr, " cgtime=%llu", p->cgtime);
1264 if(p->minflt) fprintf(stderr, " minflt=%llu", p->minflt);
1265 if(p->cminflt) fprintf(stderr, " cminflt=%llu", p->cminflt);
1266 if(p->majflt) fprintf(stderr, " majflt=%llu", p->majflt);
1267 if(p->cmajflt) fprintf(stderr, " cmajflt=%llu", p->cmajflt);
1268 fprintf(stderr, ")\n");
1273 static inline void print_process_tree(struct pid_stat *p, char *msg) {
1275 fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1276 print_process_and_parents(p, p->stat_collected_usec);
1279 static inline void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1281 struct pid_stat *p = NULL;
1283 for(p = root_of_pids; p ; p = p->next) {
1284 if(p == pe) continue;
1288 if(p->cminflt > lost) {
1289 fprintf(stderr, " > process %d (%s) could use the lost exited child minflt %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1295 if(p->cmajflt > lost) {
1296 fprintf(stderr, " > process %d (%s) could use the lost exited child majflt %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1302 if(p->cutime > lost) {
1303 fprintf(stderr, " > process %d (%s) could use the lost exited child utime %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1309 if(p->cstime > lost) {
1310 fprintf(stderr, " > process %d (%s) could use the lost exited child stime %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1316 if(p->cgtime > lost) {
1317 fprintf(stderr, " > process %d (%s) could use the lost exited child gtime %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1327 fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1331 fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1335 fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1339 fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1343 fprintf(stderr, " > cannot find any process to use the lost exited child gtime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1349 static inline unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1350 unsigned long long absorbed = 0;
1352 if(*field > *pfield) {
1353 absorbed += *pfield;
1366 static inline void process_exited_processes() {
1369 for(p = root_of_pids; p ; p = p->next) {
1370 if(p->updated || !p->stat_collected_usec)
1373 struct pid_stat *pp = p->parent;
1375 unsigned long long utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1376 unsigned long long stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1377 unsigned long long gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1378 unsigned long long minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1379 unsigned long long majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1381 if(utime + stime + gtime + minflt + majflt == 0)
1384 if(unlikely(debug)) {
1386 fprintf(stderr, "Absorb %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1389 , p->updated?"running":"exited"
1396 print_process_tree(p, "Searching parents");
1399 for(pp = p->parent; pp ; pp = pp->parent) {
1400 if(!pp->updated) continue;
1402 unsigned long long absorbed;
1403 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1404 if(unlikely(debug && absorbed))
1405 fprintf(stderr, " > process %s (%d %s) absorbed %llu utime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1407 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1408 if(unlikely(debug && absorbed))
1409 fprintf(stderr, " > process %s (%d %s) absorbed %llu stime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1411 absorbed = remove_exited_child_from_parent(>ime, &pp->cgtime);
1412 if(unlikely(debug && absorbed))
1413 fprintf(stderr, " > process %s (%d %s) absorbed %llu gtime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1415 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1416 if(unlikely(debug && absorbed))
1417 fprintf(stderr, " > process %s (%d %s) absorbed %llu minflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1419 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1420 if(unlikely(debug && absorbed))
1421 fprintf(stderr, " > process %s (%d %s) absorbed %llu majflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1424 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1425 if(unlikely(debug)) {
1426 if(utime) find_lost_child_debug(p, utime, 3);
1427 if(stime) find_lost_child_debug(p, stime, 4);
1428 if(gtime) find_lost_child_debug(p, gtime, 5);
1429 if(minflt) find_lost_child_debug(p, minflt, 1);
1430 if(majflt) find_lost_child_debug(p, majflt, 2);
1436 fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1439 , p->updated?"running":"exited"
1447 for(pp = p->parent; pp ; pp = pp->parent) {
1448 if(pp->updated) break;
1452 fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
1455 , pp->updated?"running":"exited"
1459 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1460 p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1461 p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1462 p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1463 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1464 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
1467 fprintf(stderr, "\n");
1469 else if(unlikely(debug)) {
1470 fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
1473 , p->updated?"running":"exited"
1479 static inline void link_all_processes_to_their_parents(void) {
1480 struct pid_stat *p, *pp;
1482 // link all children to their parents
1483 // and update children count on parents
1484 for(p = root_of_pids; p ; p = p->next) {
1485 // for each process found
1490 if(unlikely(!p->ppid)) {
1495 pp = all_pids[p->ppid];
1498 pp->children_count++;
1500 if(unlikely(debug || (p->target && p->target->debug)))
1501 fprintf(stderr, "apps.plugin: \tchild %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=%llu, stime=%llu, gtime=%llu, minflt=%llu, majflt=%llu, cutime=%llu, cstime=%llu, cgtime=%llu, cminflt=%llu, cmajflt=%llu\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);
1505 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1510 // ----------------------------------------------------------------------------
1512 // 1. read all files in /proc
1513 // 2. for each numeric directory:
1514 // i. read /proc/pid/stat
1515 // ii. read /proc/pid/statm
1516 // iii. read /proc/pid/io (requires root access)
1517 // iii. read the entries in directory /proc/pid/fd (requires root access)
1519 // a. find or create a struct file_descriptor
1520 // b. cleanup any old/unused file_descriptors
1522 // after all these, some pids may be linked to targets, while others may not
1524 // in case of errors, only 1 every 1000 errors is printed
1525 // to avoid filling up all disk space
1526 // if debug is enabled, all errors are printed
1528 static int compar_pid(const void *pid1, const void *pid2) {
1530 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1531 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1533 if(p1->sortlist > p2->sortlist)
1539 static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
1540 if(unlikely(!status)) {
1541 // error("command failed log %u, errno %d", log, errno);
1543 if(unlikely(debug || errno != ENOENT)) {
1544 if(unlikely(debug || !(p->log_thrown & log))) {
1545 p->log_thrown |= log;
1548 error("Cannot process %s/proc/%d/io (command '%s')", global_host_prefix, p->pid, p->comm);
1552 error("Cannot process %s/proc/%d/statm (command '%s')", global_host_prefix, p->pid, p->comm);
1555 case PID_LOG_CMDLINE:
1556 error("Cannot process %s/proc/%d/cmdline (command '%s')", global_host_prefix, p->pid, p->comm);
1560 error("Cannot process entries in %s/proc/%d/fd (command '%s')", global_host_prefix, p->pid, p->comm);
1567 error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
1574 else if(unlikely(p->log_thrown & log)) {
1575 // error("unsetting log %u on pid %d", log, p->pid);
1576 p->log_thrown &= ~log;
1582 static inline int collect_data_for_pid(pid_t pid) {
1583 if(unlikely(pid <= 0 || pid > pid_max)) {
1584 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1588 struct pid_stat *p = get_pid_entry(pid);
1589 if(unlikely(!p || p->read)) return 0;
1592 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1594 // --------------------------------------------------------------------
1597 if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p))))
1598 // there is no reason to proceed if we cannot get its status
1601 read_proc_pid_ownership(p);
1603 // check its parent pid
1604 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1605 error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
1609 // --------------------------------------------------------------------
1612 managed_log(p, PID_LOG_IO, read_proc_pid_io(p));
1614 // --------------------------------------------------------------------
1615 // /proc/<pid>/statm
1617 if(unlikely(!managed_log(p, PID_LOG_STATM, read_proc_pid_statm(p))))
1618 // there is no reason to proceed if we cannot get its memory status
1621 // --------------------------------------------------------------------
1624 // check if it is target
1625 // we do this only once, the first time this pid is loaded
1626 if(unlikely(p->new_entry)) {
1627 // /proc/<pid>/cmdline
1628 if(likely(proc_pid_cmdline_is_needed))
1629 managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
1632 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1634 uint32_t hash = simple_hash(p->comm);
1635 size_t pclen = strlen(p->comm);
1638 for(w = apps_groups_root_target; w ; w = w->next) {
1639 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1641 // find it - 4 cases:
1642 // 1. the target is not a pattern
1643 // 2. the target has the prefix
1644 // 3. the target has the suffix
1645 // 4. the target is something inside cmdline
1646 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1647 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1648 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1649 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1651 if(w->target) p->target = w->target;
1654 if(debug || (p->target && p->target->debug))
1655 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1662 // --------------------------------------------------------------------
1665 if(enable_file_charts)
1666 managed_log(p, PID_LOG_FDS, read_pid_file_descriptors(p));
1668 // --------------------------------------------------------------------
1671 if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1672 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);
1674 // mark it as updated
1682 static int collect_data_for_all_processes_from_proc(void) {
1683 struct pid_stat *p = NULL;
1685 if(all_pids_count) {
1686 // read parents before childs
1687 // this is needed to prevent a situation where
1688 // a child is found running, but until we read
1689 // its parent, it has exited and its parent
1690 // has accumulated its resources
1693 for(p = root_of_pids; p ; p = p->next) {
1698 p->children_count = 0;
1701 all_pids_sortlist[slc++] = p->pid;
1704 if(unlikely(slc != all_pids_count)) {
1705 error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1706 all_pids_count = slc;
1709 if(include_exited_childs) {
1710 qsort((void *)all_pids_sortlist, all_pids_count, sizeof(pid_t), compar_pid);
1711 for(slc = 0; slc < all_pids_count; slc++)
1712 collect_data_for_pid(all_pids_sortlist[slc]);
1716 char dirname[FILENAME_MAX + 1];
1718 snprintfz(dirname, FILENAME_MAX, "%s/proc", global_host_prefix);
1719 DIR *dir = opendir(dirname);
1722 struct dirent *file = NULL;
1724 while((file = readdir(dir))) {
1725 char *endptr = file->d_name;
1726 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1728 // make sure we read a valid number
1729 if(unlikely(endptr == file->d_name || *endptr != '\0'))
1732 collect_data_for_pid(pid);
1739 // normally this is done
1740 // however we may have processes exited while we collected values
1741 // so let's find the exited ones
1742 // we do this by collecting the ownership of process
1743 // if we manage to get the ownership, the process still runs
1746 link_all_processes_to_their_parents();
1747 process_exited_processes();
1752 // ----------------------------------------------------------------------------
1753 // update statistics on the targets
1755 // 1. link all childs to their parents
1756 // 2. go from bottom to top, marking as merged all childs to their parents
1757 // this step links all parents without a target to the child target, if any
1758 // 3. link all top level processes (the ones not merged) to the default target
1759 // 4. go from top to bottom, linking all childs without a target, to their parent target
1760 // after this step, all processes have a target
1761 // [5. for each killed pid (updated = 0), remove its usage from its target]
1762 // 6. zero all apps_groups_targets
1763 // 7. concentrate all values on the apps_groups_targets
1764 // 8. remove all killed processes
1765 // 9. find the unique file count for each target
1766 // check: update_apps_groups_statistics()
1768 static void cleanup_exited_pids(void) {
1770 struct pid_stat *p = NULL;
1772 for(p = root_of_pids; p ;) {
1773 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1774 // fprintf(stderr, "\tEXITED %d %s [parent %d %s, target %s] utime=%llu, stime=%llu, gtime=%llu, cutime=%llu, cstime=%llu, cgtime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->pid, p->comm, p->parent->pid, p->parent->comm, p->target->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1776 if(unlikely(debug && (p->keep || p->keeploops)))
1777 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1779 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1780 file_descriptor_not_used(p->fds[c]);
1789 if(unlikely(p->keep)) p->keeploops++;
1796 static void apply_apps_groups_targets_inheritance(void) {
1797 struct pid_stat *p = NULL;
1799 // children that do not have a target
1800 // inherit their target from their parent
1801 int found = 1, loops = 0;
1803 if(unlikely(debug)) loops++;
1805 for(p = root_of_pids; p ; p = p->next) {
1806 // if this process does not have a target
1807 // and it has a parent
1808 // and its parent has a target
1809 // then, set the parent's target to this process
1810 if(unlikely(!p->target && p->parent && p->parent->target)) {
1811 p->target = p->parent->target;
1814 if(debug || (p->target && p->target->debug))
1815 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);
1820 // find all the procs with 0 childs and merge them to their parents
1821 // repeat, until nothing more can be done.
1825 if(unlikely(debug)) loops++;
1828 for(p = root_of_pids; p ; p = p->next) {
1829 if(unlikely(!p->sortlist && !p->children_count))
1830 p->sortlist = sortlist++;
1832 // if this process does not have any children
1833 // and is not already merged
1835 // and its parent has children
1836 // and the target of this process and its parent is the same, or the parent does not have a target
1837 // and its parent is not init
1838 // then, mark them as merged.
1843 && p->parent->children_count
1844 && (p->target == p->parent->target || !p->parent->target)
1847 p->parent->children_count--;
1850 // the parent inherits the child's target, if it does not have a target itself
1851 if(unlikely(p->target && !p->parent->target)) {
1852 p->parent->target = p->target;
1854 if(debug || (p->target && p->target->debug))
1855 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);
1863 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
1866 // init goes always to default target
1868 all_pids[1]->target = apps_groups_default_target;
1870 // give a default target on all top level processes
1871 if(unlikely(debug)) loops++;
1872 for(p = root_of_pids; p ; p = p->next) {
1873 // if the process is not merged itself
1874 // then is is a top level process
1875 if(unlikely(!p->merged && !p->target))
1876 p->target = apps_groups_default_target;
1878 // make sure all processes have a sortlist
1879 if(unlikely(!p->sortlist))
1880 p->sortlist = sortlist++;
1884 all_pids[1]->sortlist = sortlist++;
1886 // give a target to all merged child processes
1889 if(unlikely(debug)) loops++;
1891 for(p = root_of_pids; p ; p = p->next) {
1892 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1893 p->target = p->parent->target;
1896 if(debug || (p->target && p->target->debug))
1897 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);
1903 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1906 static long zero_all_targets(struct target *root) {
1910 for (w = root; w ; w = w->next) {
1913 if(w->fds) freez(w->fds);
1931 w->statm_resident = 0;
1933 // w->statm_text = 0;
1934 // w->statm_lib = 0;
1935 // w->statm_data = 0;
1936 // w->statm_dirty = 0;
1938 w->io_logical_bytes_read = 0;
1939 w->io_logical_bytes_written = 0;
1940 // w->io_read_calls = 0;
1941 // w->io_write_calls = 0;
1942 w->io_storage_bytes_read = 0;
1943 w->io_storage_bytes_written = 0;
1944 // w->io_cancelled_write_bytes = 0;
1950 static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1953 if(unlikely(!w->fds))
1954 w->fds = callocz(sizeof(int), (size_t) all_files_size);
1956 if(likely(p->updated)) {
1957 w->cutime += p->cutime;
1958 w->cstime += p->cstime;
1959 w->cgtime += p->cgtime;
1960 w->cminflt += p->cminflt;
1961 w->cmajflt += p->cmajflt;
1963 w->utime += p->utime;
1964 w->stime += p->stime;
1965 w->gtime += p->gtime;
1966 w->minflt += p->minflt;
1967 w->majflt += p->majflt;
1969 // w->rss += p->rss;
1971 w->statm_size += p->statm_size;
1972 w->statm_resident += p->statm_resident;
1973 w->statm_share += p->statm_share;
1974 // w->statm_text += p->statm_text;
1975 // w->statm_lib += p->statm_lib;
1976 // w->statm_data += p->statm_data;
1977 // w->statm_dirty += p->statm_dirty;
1979 w->io_logical_bytes_read += p->io_logical_bytes_read;
1980 w->io_logical_bytes_written += p->io_logical_bytes_written;
1981 // w->io_read_calls += p->io_read_calls;
1982 // w->io_write_calls += p->io_write_calls;
1983 w->io_storage_bytes_read += p->io_storage_bytes_read;
1984 w->io_storage_bytes_written += p->io_storage_bytes_written;
1985 // w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1988 w->num_threads += p->num_threads;
1990 if(likely(w->fds)) {
1992 for(c = 0; c < p->fds_size ;c++) {
1993 if(p->fds[c] == 0) continue;
1995 if(likely(p->fds[c] < all_files_size)) {
1996 if(w->fds) w->fds[p->fds[c]]++;
1999 error("Invalid fd number %d", p->fds[c]);
2003 if(unlikely(debug || w->debug))
2004 fprintf(stderr, "apps.plugin: \taggregating '%s' pid %d on target '%s' utime=%llu, stime=%llu, gtime=%llu, cutime=%llu, cstime=%llu, cgtime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\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);
2008 static inline void count_targets_fds(struct target *root) {
2012 for (w = root; w ; w = w->next) {
2013 if(!w->fds) continue;
2018 w->openinotifies = 0;
2019 w->openeventfds = 0;
2020 w->opentimerfds = 0;
2021 w->opensignalfds = 0;
2022 w->openeventpolls = 0;
2025 for(c = 1; c < all_files_size ;c++) {
2027 switch(all_files[c].type) {
2036 case FILETYPE_SOCKET:
2040 case FILETYPE_INOTIFY:
2044 case FILETYPE_EVENTFD:
2048 case FILETYPE_TIMERFD:
2052 case FILETYPE_SIGNALFD:
2056 case FILETYPE_EVENTPOLL:
2057 w->openeventpolls++;
2070 static void calculate_netdata_statistics(void) {
2071 apply_apps_groups_targets_inheritance();
2073 zero_all_targets(users_root_target);
2074 zero_all_targets(groups_root_target);
2075 apps_groups_targets = zero_all_targets(apps_groups_root_target);
2077 // this has to be done, before the cleanup
2078 struct pid_stat *p = NULL;
2079 struct target *w = NULL, *o = NULL;
2081 // concentrate everything on the apps_groups_targets
2082 for(p = root_of_pids; p ; p = p->next) {
2084 // --------------------------------------------------------------------
2085 // apps_groups targets
2086 if(likely(p->target))
2087 aggregate_pid_on_target(p->target, p, NULL);
2089 error("pid %d %s was left without a target!", p->pid, p->comm);
2092 // --------------------------------------------------------------------
2095 if(likely(p->user_target && p->user_target->uid == p->uid))
2098 if(unlikely(debug && p->user_target))
2099 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);
2101 w = p->user_target = get_users_target(p->uid);
2105 aggregate_pid_on_target(w, p, o);
2107 error("pid %d %s was left without a user target!", p->pid, p->comm);
2110 // --------------------------------------------------------------------
2112 o = p->group_target;
2113 if(likely(p->group_target && p->group_target->gid == p->gid))
2114 w = p->group_target;
2116 if(unlikely(debug && p->group_target))
2117 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);
2119 w = p->group_target = get_groups_target(p->gid);
2123 aggregate_pid_on_target(w, p, o);
2125 error("pid %d %s was left without a group target!", p->pid, p->comm);
2129 count_targets_fds(apps_groups_root_target);
2130 count_targets_fds(users_root_target);
2131 count_targets_fds(groups_root_target);
2133 cleanup_exited_pids();
2136 // ----------------------------------------------------------------------------
2137 // update chart dimensions
2139 BUFFER *output = NULL;
2140 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2142 static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2143 // fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2144 buffer_strcat(output, "BEGIN ");
2145 buffer_strcat(output, type);
2146 buffer_strcat(output, ".");
2147 buffer_strcat(output, id);
2148 buffer_strcat(output, " ");
2149 buffer_print_llu(output, usec);
2150 buffer_strcat(output, "\n");
2153 static inline void send_SET(const char *name, unsigned long long value) {
2154 // fprintf(stdout, "SET %s = %llu\n", name, value);
2155 buffer_strcat(output, "SET ");
2156 buffer_strcat(output, name);
2157 buffer_strcat(output, " = ");
2158 buffer_print_llu(output, value);
2159 buffer_strcat(output, "\n");
2162 static inline void send_END(void) {
2163 // fprintf(stdout, "END\n");
2164 buffer_strcat(output, "END\n");
2167 double utime_fix_ratio = 1.0, stime_fix_ratio = 1.0, gtime_fix_ratio = 1.0, cutime_fix_ratio = 1.0, cstime_fix_ratio = 1.0, cgtime_fix_ratio = 1.0;
2168 double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2170 static usec_t send_resource_usage_to_netdata() {
2171 static struct timeval last = { 0, 0 };
2172 static struct rusage me_last;
2182 now_realtime_timeval(&last);
2183 getrusage(RUSAGE_SELF, &me_last);
2185 // the first time, give a zero to allow
2186 // netdata calibrate to the current time
2187 // usec = update_every * USEC_PER_SEC;
2193 now_realtime_timeval(&now);
2194 getrusage(RUSAGE_SELF, &me);
2196 usec = dt_usec(&now, &last);
2197 cpuuser = me.ru_utime.tv_sec * USEC_PER_SEC + me.ru_utime.tv_usec;
2198 cpusyst = me.ru_stime.tv_sec * USEC_PER_SEC + me.ru_stime.tv_usec;
2200 memmove(&last, &now, sizeof(struct timeval));
2201 memmove(&me_last, &me, sizeof(struct rusage));
2204 buffer_sprintf(output,
2205 "BEGIN netdata.apps_cpu %llu\n"
2207 "SET system = %llu\n"
2209 "BEGIN netdata.apps_files %llu\n"
2210 "SET files = %llu\n"
2213 "SET targets = %ld\n"
2215 "BEGIN netdata.apps_fix %llu\n"
2216 "SET utime = %llu\n"
2217 "SET stime = %llu\n"
2218 "SET gtime = %llu\n"
2219 "SET minflt = %llu\n"
2220 "SET majflt = %llu\n"
2229 , apps_groups_targets
2231 , (unsigned long long)(utime_fix_ratio * 100 * RATES_DETAIL)
2232 , (unsigned long long)(stime_fix_ratio * 100 * RATES_DETAIL)
2233 , (unsigned long long)(gtime_fix_ratio * 100 * RATES_DETAIL)
2234 , (unsigned long long)(minflt_fix_ratio * 100 * RATES_DETAIL)
2235 , (unsigned long long)(majflt_fix_ratio * 100 * RATES_DETAIL)
2238 if(include_exited_childs)
2239 buffer_sprintf(output,
2240 "BEGIN netdata.apps_children_fix %llu\n"
2241 "SET cutime = %llu\n"
2242 "SET cstime = %llu\n"
2243 "SET cgtime = %llu\n"
2244 "SET cminflt = %llu\n"
2245 "SET cmajflt = %llu\n"
2248 , (unsigned long long)(cutime_fix_ratio * 100 * RATES_DETAIL)
2249 , (unsigned long long)(cstime_fix_ratio * 100 * RATES_DETAIL)
2250 , (unsigned long long)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2251 , (unsigned long long)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2252 , (unsigned long long)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2258 static void normalize_data(struct target *root) {
2261 // childs processing introduces spikes
2262 // here we try to eliminate them by disabling childs processing either for specific dimensions
2263 // or entirely. Of course, either way, we disable it just a single iteration.
2265 unsigned long long max = processors * hz * RATES_DETAIL;
2266 unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2268 if(global_utime > max) global_utime = max;
2269 if(global_stime > max) global_stime = max;
2270 if(global_gtime > max) global_gtime = max;
2272 for(w = root; w ; w = w->next) {
2273 if(w->target || (!w->processes && !w->exposed)) continue;
2278 cutime += w->cutime;
2279 cstime += w->cstime;
2280 cgtime += w->cgtime;
2282 minflt += w->minflt;
2283 majflt += w->majflt;
2284 cminflt += w->cminflt;
2285 cmajflt += w->cmajflt;
2288 if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
2289 if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
2290 // everything we collected fits
2296 cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
2298 else if(global_utime + global_stime > utime + stime) {
2299 // childrens resources are too high
2300 // lower only the children resources
2303 gtime_fix_ratio = 1.0;
2306 cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
2309 // even running processes are unrealistic
2310 // zero the children resources
2311 // lower the running processes resources
2314 gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
2317 cgtime_fix_ratio = 0.0;
2326 cgtime_fix_ratio = 0.0;
2329 if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
2330 if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
2331 if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
2332 if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
2333 if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
2334 if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
2336 // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
2337 // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
2338 // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
2339 // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
2340 // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
2341 // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
2344 // we use cpu time to normalize page faults
2345 // the problem is that to find the proper max values
2346 // for page faults we have to parse /proc/vmstat
2347 // which is quite big to do it again (netdata does it already)
2349 // a better solution could be to somehow have netdata
2350 // do this normalization for us
2352 if(utime || stime || gtime)
2354 minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
2357 majflt_fix_ratio = 1.0;
2359 if(cutime || cstime || cgtime)
2361 cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
2364 cmajflt_fix_ratio = 1.0;
2368 if(unlikely(debug)) {
2370 "SYSTEM: u=%llu s=%llu g=%llu "
2371 "COLLECTED: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2372 "DELTA: u=%lld s=%lld g=%lld "
2373 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2374 "FINALLY: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2385 , (long long)utime + (long long)cutime - (long long)global_utime
2386 , (long long)stime + (long long)cstime - (long long)global_stime
2387 , (long long)gtime + (long long)cgtime - (long long)global_gtime
2394 , (unsigned long long)(utime * utime_fix_ratio)
2395 , (unsigned long long)(stime * stime_fix_ratio)
2396 , (unsigned long long)(gtime * gtime_fix_ratio)
2397 , (unsigned long long)(cutime * cutime_fix_ratio)
2398 , (unsigned long long)(cstime * cstime_fix_ratio)
2399 , (unsigned long long)(cgtime * cgtime_fix_ratio)
2404 static void send_collected_data_to_netdata(struct target *root, const char *type, usec_t usec) {
2407 send_BEGIN(type, "cpu", usec);
2408 for (w = root; w ; w = w->next) {
2409 if(unlikely(w->exposed))
2410 send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (unsigned long long)(w->stime * stime_fix_ratio) + (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio) + (unsigned long long)(w->cstime * cstime_fix_ratio) + (unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2414 send_BEGIN(type, "cpu_user", usec);
2415 for (w = root; w ; w = w->next) {
2416 if(unlikely(w->exposed))
2417 send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2421 send_BEGIN(type, "cpu_system", usec);
2422 for (w = root; w ; w = w->next) {
2423 if(unlikely(w->exposed))
2424 send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2428 if(show_guest_time) {
2429 send_BEGIN(type, "cpu_guest", usec);
2430 for (w = root; w ; w = w->next) {
2431 if(unlikely(w->exposed))
2432 send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2437 send_BEGIN(type, "threads", usec);
2438 for (w = root; w ; w = w->next) {
2439 if(unlikely(w->exposed))
2440 send_SET(w->name, w->num_threads);
2444 send_BEGIN(type, "processes", usec);
2445 for (w = root; w ; w = w->next) {
2446 if(unlikely(w->exposed))
2447 send_SET(w->name, w->processes);
2451 send_BEGIN(type, "mem", usec);
2452 for (w = root; w ; w = w->next) {
2453 if(unlikely(w->exposed))
2454 send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2458 send_BEGIN(type, "vmem", usec);
2459 for (w = root; w ; w = w->next) {
2460 if(unlikely(w->exposed))
2461 send_SET(w->name, w->statm_size);
2465 send_BEGIN(type, "minor_faults", usec);
2466 for (w = root; w ; w = w->next) {
2467 if(unlikely(w->exposed))
2468 send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2472 send_BEGIN(type, "major_faults", usec);
2473 for (w = root; w ; w = w->next) {
2474 if(unlikely(w->exposed))
2475 send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2479 send_BEGIN(type, "lreads", usec);
2480 for (w = root; w ; w = w->next) {
2481 if(unlikely(w->exposed))
2482 send_SET(w->name, w->io_logical_bytes_read);
2486 send_BEGIN(type, "lwrites", usec);
2487 for (w = root; w ; w = w->next) {
2488 if(unlikely(w->exposed))
2489 send_SET(w->name, w->io_logical_bytes_written);
2493 send_BEGIN(type, "preads", usec);
2494 for (w = root; w ; w = w->next) {
2495 if(unlikely(w->exposed))
2496 send_SET(w->name, w->io_storage_bytes_read);
2500 send_BEGIN(type, "pwrites", usec);
2501 for (w = root; w ; w = w->next) {
2502 if(unlikely(w->exposed))
2503 send_SET(w->name, w->io_storage_bytes_written);
2507 if(enable_file_charts) {
2508 send_BEGIN(type, "files", usec);
2509 for (w = root; w; w = w->next) {
2510 if (unlikely(w->exposed))
2511 send_SET(w->name, w->openfiles);
2515 send_BEGIN(type, "sockets", usec);
2516 for (w = root; w; w = w->next) {
2517 if (unlikely(w->exposed))
2518 send_SET(w->name, w->opensockets);
2522 send_BEGIN(type, "pipes", usec);
2523 for (w = root; w; w = w->next) {
2524 if (unlikely(w->exposed))
2525 send_SET(w->name, w->openpipes);
2532 // ----------------------------------------------------------------------------
2533 // generate the charts
2535 static void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2538 int newly_added = 0;
2540 for(w = root ; w ; w = w->next) {
2541 if (w->target) continue;
2543 if (!w->exposed && w->processes) {
2546 if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2550 // nothing more to show
2551 if(!newly_added && show_guest_time == show_guest_time_old) return;
2553 // we have something new to show
2554 // update the charts
2555 buffer_sprintf(output, "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);
2556 for (w = root; w ; w = w->next) {
2557 if(unlikely(w->exposed))
2558 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2561 buffer_sprintf(output, "CHART %s.mem '' '%s Real Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2562 for (w = root; w ; w = w->next) {
2563 if(unlikely(w->exposed))
2564 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2567 buffer_sprintf(output, "CHART %s.vmem '' '%s Virtual Memory Size' 'MB' mem %s.vmem stacked 20004 %d\n", type, title, type, update_every);
2568 for (w = root; w ; w = w->next) {
2569 if(unlikely(w->exposed))
2570 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2573 buffer_sprintf(output, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2574 for (w = root; w ; w = w->next) {
2575 if(unlikely(w->exposed))
2576 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2579 buffer_sprintf(output, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2580 for (w = root; w ; w = w->next) {
2581 if(unlikely(w->exposed))
2582 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2585 buffer_sprintf(output, "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);
2586 for (w = root; w ; w = w->next) {
2587 if(unlikely(w->exposed))
2588 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2591 buffer_sprintf(output, "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);
2592 for (w = root; w ; w = w->next) {
2593 if(unlikely(w->exposed))
2594 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2597 if(show_guest_time) {
2598 buffer_sprintf(output, "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);
2599 for (w = root; w; w = w->next) {
2600 if(unlikely(w->exposed))
2601 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2605 buffer_sprintf(output, "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);
2606 for (w = root; w ; w = w->next) {
2607 if(unlikely(w->exposed))
2608 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2611 buffer_sprintf(output, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
2612 for (w = root; w ; w = w->next) {
2613 if(unlikely(w->exposed))
2614 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2617 buffer_sprintf(output, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2618 for (w = root; w ; w = w->next) {
2619 if(unlikely(w->exposed))
2620 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2623 buffer_sprintf(output, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2624 for (w = root; w ; w = w->next) {
2625 if(unlikely(w->exposed))
2626 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2629 buffer_sprintf(output, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2630 for (w = root; w ; w = w->next) {
2631 if(unlikely(w->exposed))
2632 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2635 buffer_sprintf(output, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2636 for (w = root; w ; w = w->next) {
2637 if(unlikely(w->exposed))
2638 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2641 if(enable_file_charts) {
2642 buffer_sprintf(output, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type,
2643 title, type, update_every);
2644 for (w = root; w; w = w->next) {
2645 if (unlikely(w->exposed))
2646 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2649 buffer_sprintf(output, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n",
2650 type, title, type, update_every);
2651 for (w = root; w; w = w->next) {
2652 if (unlikely(w->exposed))
2653 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2656 buffer_sprintf(output, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type,
2657 title, type, update_every);
2658 for (w = root; w; w = w->next) {
2659 if (unlikely(w->exposed))
2660 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2666 // ----------------------------------------------------------------------------
2667 // parse command line arguments
2669 static void parse_args(int argc, char **argv)
2674 for(i = 1; i < argc; i++) {
2676 int n = atoi(argv[i]);
2683 if(strcmp("debug", argv[i]) == 0) {
2685 // debug_flags = 0xffffffff;
2689 if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
2690 include_exited_childs = 0;
2694 if(strcmp("with-childs", argv[i]) == 0) {
2695 include_exited_childs = 1;
2699 if(strcmp("with-guest", argv[i]) == 0) {
2700 enable_guest_charts = 1;
2704 if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
2705 enable_guest_charts = 0;
2709 if(strcmp("with-files", argv[i]) == 0) {
2710 enable_file_charts = 1;
2714 if(strcmp("no-files", argv[i]) == 0 || strcmp("without-files", argv[i]) == 0) {
2715 enable_file_charts = 0;
2719 if(strcmp("no-users", argv[i]) == 0 || strcmp("without-users", argv[i]) == 0) {
2720 enable_users_charts = 0;
2724 if(strcmp("no-groups", argv[i]) == 0 || strcmp("without-groups", argv[i]) == 0) {
2725 enable_groups_charts = 0;
2729 if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
2732 "(C) 2016 Costa Tsaousis"
2734 "This program is a data collector plugin for netdata.\n"
2736 "Valid command line options:\n"
2738 "SECONDS set the data collection frequency\n"
2740 "debug enable debugging (lot of output)\n"
2743 "without-childs enable / disable aggregating exited\n"
2744 " children resources into parents\n"
2745 " (default is enabled)\n"
2748 "without-guest enable / disable reporting guest charts\n"
2749 " (default is disabled)\n"
2752 "without-files enable / disable reporting files, sockets, pipes\n"
2753 " (default is enabled)\n"
2755 "NAME read apps_NAME.conf instead of\n"
2756 " apps_groups.conf\n"
2757 " (default NAME=groups)\n"
2767 error("Cannot understand option %s", argv[i]);
2771 if(freq > 0) update_every = freq;
2772 if(!name) name = "groups";
2774 if(read_apps_groups_conf(name)) {
2775 error("Cannot read process groups '%s/apps_%s.conf'. There are no internal defaults. Failing.", config_dir, name);
2780 int main(int argc, char **argv)
2782 // debug_flags = D_PROCFILE;
2784 // set the name for logging
2785 program_name = "apps.plugin";
2787 info("started on pid %d", getpid());
2789 // disable syslog for apps.plugin
2790 error_log_syslog = 0;
2792 // set errors flood protection to 100 logs per hour
2793 error_log_errors_per_period = 100;
2794 error_log_throttle_period = 3600;
2796 global_host_prefix = getenv("NETDATA_HOST_PREFIX");
2797 if(global_host_prefix == NULL) {
2798 // info("NETDATA_HOST_PREFIX is not passed from netdata");
2799 global_host_prefix = "";
2801 // else info("Found NETDATA_HOST_PREFIX='%s'", global_host_prefix);
2803 config_dir = getenv("NETDATA_CONFIG_DIR");
2804 if(config_dir == NULL) {
2805 // info("NETDATA_CONFIG_DIR is not passed from netdata");
2806 config_dir = CONFIG_DIR;
2808 // else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2810 #ifdef NETDATA_INTERNAL_CHECKS
2811 if(debug_flags != 0) {
2812 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2813 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2814 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2815 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2817 #endif /* NETDATA_INTERNAL_CHECKS */
2819 procfile_adaptive_initial_allocation = 1;
2821 time_t started_t = now_realtime_sec();
2823 get_system_pid_max();
2826 parse_args(argc, argv);
2828 all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
2829 all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
2831 output = buffer_create(1024);
2832 buffer_sprintf(output,
2833 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2834 "DIMENSION user '' incremental 1 1000\n"
2835 "DIMENSION system '' incremental 1 1000\n"
2836 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2837 "DIMENSION files '' incremental 1 1\n"
2838 "DIMENSION pids '' absolute 1 1\n"
2839 "DIMENSION fds '' absolute 1 1\n"
2840 "DIMENSION targets '' absolute 1 1\n"
2841 "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
2842 "DIMENSION utime '' absolute 1 %2$llu\n"
2843 "DIMENSION stime '' absolute 1 %2$llu\n"
2844 "DIMENSION gtime '' absolute 1 %2$llu\n"
2845 "DIMENSION minflt '' absolute 1 %2$llu\n"
2846 "DIMENSION majflt '' absolute 1 %2$llu\n"
2851 if(include_exited_childs)
2852 buffer_sprintf(output,
2853 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
2854 "DIMENSION cutime '' absolute 1 %2$llu\n"
2855 "DIMENSION cstime '' absolute 1 %2$llu\n"
2856 "DIMENSION cgtime '' absolute 1 %2$llu\n"
2857 "DIMENSION cminflt '' absolute 1 %2$llu\n"
2858 "DIMENSION cmajflt '' absolute 1 %2$llu\n"
2863 usec_t step = update_every * USEC_PER_SEC;
2864 global_iterations_counter = 1;
2865 for(;1; global_iterations_counter++) {
2866 usec_t now = now_realtime_usec();
2867 usec_t next = now - (now % step) + step;
2870 sleep_usec(next - now);
2871 now = now_realtime_usec();
2874 if(!collect_data_for_all_processes_from_proc()) {
2875 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2876 printf("DISABLE\n");
2880 calculate_netdata_statistics();
2881 normalize_data(apps_groups_root_target);
2883 usec_t dt = send_resource_usage_to_netdata();
2885 // this is smart enough to show only newly added apps, when needed
2886 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2888 if(likely(enable_users_charts))
2889 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2891 if(likely(enable_groups_charts))
2892 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2894 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2896 if(likely(enable_users_charts))
2897 send_collected_data_to_netdata(users_root_target, "users", dt);
2899 if(likely(enable_groups_charts))
2900 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2902 show_guest_time_old = show_guest_time;
2904 if(write(STDOUT_FILENO, buffer_tostring(output), buffer_strlen(output)) == -1)
2905 fatal("Cannot send chart values to netdata.");
2908 buffer_flush(output);
2911 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2913 time_t current_t = now_realtime_sec();
2915 // restart check (14400 seconds)
2916 if(current_t - started_t > 14400) exit(0);