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
16 #define MAX_SPARE_FDS 10
21 unsigned long long global_iterations_counter = 1;
22 unsigned long long file_counter = 0;
23 int proc_pid_cmdline_is_needed = 0;
24 int include_exited_childs = 1;
25 char *config_dir = CONFIG_DIR;
27 pid_t *all_pids_sortlist = NULL;
29 // will be automatically set to 1, if guest values are collected
30 int show_guest_time = 0;
31 int show_guest_time_old = 0;
33 int enable_guest_charts = 0;
34 int enable_file_charts = 1;
35 int enable_users_charts = 1;
36 int enable_groups_charts = 1;
38 // ----------------------------------------------------------------------------
40 void netdata_cleanup_and_exit(int ret) {
45 // ----------------------------------------------------------------------------
47 // target is the structure that process data are aggregated
50 char compare[MAX_COMPARE_NAME + 1];
54 char id[MAX_NAME + 1];
57 char name[MAX_NAME + 1];
62 unsigned long long minflt;
63 unsigned long long cminflt;
64 unsigned long long majflt;
65 unsigned long long cmajflt;
66 unsigned long long utime;
67 unsigned long long stime;
68 unsigned long long gtime;
69 unsigned long long cutime;
70 unsigned long long cstime;
71 unsigned long long cgtime;
72 unsigned long long num_threads;
73 // unsigned long long rss;
75 unsigned long long statm_size;
76 unsigned long long statm_resident;
77 unsigned long long statm_share;
78 // unsigned long long statm_text;
79 // unsigned long long statm_lib;
80 // unsigned long long statm_data;
81 // unsigned long long statm_dirty;
83 unsigned long long io_logical_bytes_read;
84 unsigned long long io_logical_bytes_written;
85 // unsigned long long io_read_calls;
86 // unsigned long long io_write_calls;
87 unsigned long long io_storage_bytes_read;
88 unsigned long long io_storage_bytes_written;
89 // unsigned long long io_cancelled_write_bytes;
94 unsigned long long openfiles;
95 unsigned long long openpipes;
96 unsigned long long opensockets;
97 unsigned long long openinotifies;
98 unsigned long long openeventfds;
99 unsigned long long opentimerfds;
100 unsigned long long opensignalfds;
101 unsigned long long openeventpolls;
102 unsigned long long openother;
104 unsigned long processes; // how many processes have been merged to this
105 int exposed; // if set, we have sent this to netdata
106 int hidden; // if set, we set the hidden flag on the dimension
109 int starts_with; // if set, the compare string matches only the
110 // beginning of the command
112 struct target *target; // the one that will be reported to netdata
117 // ----------------------------------------------------------------------------
119 // aggregate all processes in groups, to have a limited number of dimensions
121 struct target *apps_groups_root_target = NULL;
122 struct target *apps_groups_default_target = NULL;
123 long apps_groups_targets = 0;
125 struct target *users_root_target = NULL;
126 struct target *groups_root_target = NULL;
128 static struct target *get_users_target(uid_t uid)
131 for(w = users_root_target ; w ; w = w->next)
132 if(w->uid == uid) return w;
134 w = callocz(sizeof(struct target), 1);
135 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
136 w->comparehash = simple_hash(w->compare);
137 w->comparelen = strlen(w->compare);
139 snprintfz(w->id, MAX_NAME, "%u", uid);
140 w->idhash = simple_hash(w->id);
142 struct passwd *pw = getpwuid(uid);
144 snprintfz(w->name, MAX_NAME, "%u", uid);
146 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
148 netdata_fix_chart_name(w->name);
152 w->next = users_root_target;
153 users_root_target = w;
156 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
161 struct target *get_groups_target(gid_t gid)
164 for(w = groups_root_target ; w ; w = w->next)
165 if(w->gid == gid) return w;
167 w = callocz(sizeof(struct target), 1);
168 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
169 w->comparehash = simple_hash(w->compare);
170 w->comparelen = strlen(w->compare);
172 snprintfz(w->id, MAX_NAME, "%u", gid);
173 w->idhash = simple_hash(w->id);
175 struct group *gr = getgrgid(gid);
177 snprintfz(w->name, MAX_NAME, "%u", gid);
179 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
181 netdata_fix_chart_name(w->name);
185 w->next = groups_root_target;
186 groups_root_target = w;
189 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
194 // find or create a new target
195 // there are targets that are just aggregated to other target (the second argument)
196 static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
197 int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
198 const char *nid = id;
200 // extract the options
201 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
202 if(nid[0] == '-') thidden = 1;
203 if(nid[0] == '+') tdebug = 1;
204 if(nid[0] == '*') ends_with = 1;
207 uint32_t hash = simple_hash(id);
209 // find if it already exists
210 struct target *w, *last = apps_groups_root_target;
211 for(w = apps_groups_root_target ; w ; w = w->next) {
212 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
218 // find an existing target
219 if(unlikely(!target)) {
220 while(*name == '-') {
221 if(*name == '-') thidden = 1;
224 for(target = apps_groups_root_target ; target ; target = target->next) {
225 if(!target->target && strcmp(name, target->name) == 0)
228 if(unlikely(debug)) {
230 fprintf(stderr, "apps.plugin: REUSING TARGET NAME '%s' on ID '%s'\n", target->name, target->id);
232 fprintf(stderr, "apps.plugin: NEW TARGET NAME '%s' on ID '%s'\n", name, id);
236 if(target && target->target)
237 fatal("Internal Error: request to link process '%s' to target '%s' which is linked to target '%s'", id, target->id, target->target->id);
239 w = callocz(sizeof(struct target), 1);
240 strncpyz(w->id, nid, MAX_NAME);
241 w->idhash = simple_hash(w->id);
243 if(unlikely(!target))
245 strncpyz(w->name, name, MAX_NAME);
248 strncpyz(w->name, nid, MAX_NAME);
250 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
251 size_t len = strlen(w->compare);
252 if(w->compare[len - 1] == '*') {
253 w->compare[len - 1] = '\0';
256 w->ends_with = ends_with;
258 if(w->starts_with && w->ends_with)
259 proc_pid_cmdline_is_needed = 1;
261 w->comparehash = simple_hash(w->compare);
262 w->comparelen = strlen(w->compare);
268 // append it, to maintain the order in apps_groups.conf
269 if(last) last->next = w;
270 else apps_groups_root_target = w;
273 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
275 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
276 , w->target?w->target->name:w->name
277 , (w->hidden)?"hidden":"-"
278 , (w->debug)?"debug":"-"
284 // read the apps_groups.conf file
285 static int read_apps_groups_conf(const char *file)
287 char filename[FILENAME_MAX + 1];
289 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, file);
292 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
294 // ----------------------------------------
296 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
299 procfile_set_quotes(ff, "'\"");
301 ff = procfile_readall(ff);
305 unsigned long line, lines = procfile_lines(ff);
307 for(line = 0; line < lines ;line++) {
308 unsigned long word, words = procfile_linewords(ff, line);
311 char *name = procfile_lineword(ff, line, 0);
312 if(!name || !*name) continue;
314 // find a possibly existing target
315 struct target *w = NULL;
317 // loop through all words, skipping the first one (the name)
318 for(word = 0; word < words ;word++) {
319 char *s = procfile_lineword(ff, line, word);
320 if(!s || !*s) continue;
323 // is this the first word? skip it
324 if(s == name) continue;
327 struct target *n = get_apps_groups_target(s, w, name);
329 error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
333 // just some optimization
334 // to avoid searching for a target for each process
335 if(!w) w = n->target?n->target:n;
341 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL, "other"); // match nothing
342 if(!apps_groups_default_target)
343 fatal("Cannot create default target");
345 // allow the user to override group 'other'
346 if(apps_groups_default_target->target)
347 apps_groups_default_target = apps_groups_default_target->target;
353 // ----------------------------------------------------------------------------
354 // data to store for each pid
357 #define PID_LOG_IO 0x00000001
358 #define PID_LOG_STATM 0x00000002
359 #define PID_LOG_CMDLINE 0x00000004
360 #define PID_LOG_FDS 0x00000008
361 #define PID_LOG_STAT 0x00000010
365 char comm[MAX_COMPARE_NAME + 1];
366 char cmdline[MAX_CMDLINE + 1];
378 // these are raw values collected
379 unsigned long long minflt_raw;
380 unsigned long long cminflt_raw;
381 unsigned long long majflt_raw;
382 unsigned long long cmajflt_raw;
383 unsigned long long utime_raw;
384 unsigned long long stime_raw;
385 unsigned long long gtime_raw; // guest_time
386 unsigned long long cutime_raw;
387 unsigned long long cstime_raw;
388 unsigned long long cgtime_raw; // cguest_time
391 unsigned long long minflt;
392 unsigned long long cminflt;
393 unsigned long long majflt;
394 unsigned long long cmajflt;
395 unsigned long long utime;
396 unsigned long long stime;
397 unsigned long long gtime;
398 unsigned long long cutime;
399 unsigned long long cstime;
400 unsigned long long cgtime;
405 // int64_t itrealvalue;
406 // unsigned long long starttime;
407 // unsigned long long vsize;
408 // unsigned long long rss;
409 // unsigned long long rsslim;
410 // unsigned long long starcode;
411 // unsigned long long endcode;
412 // unsigned long long startstack;
413 // unsigned long long kstkesp;
414 // unsigned long long kstkeip;
417 // uint64_t sigignore;
418 // uint64_t sigcatch;
422 // int32_t exit_signal;
423 // int32_t processor;
424 // uint32_t rt_priority;
426 // unsigned long long delayacct_blkio_ticks;
431 unsigned long long statm_size;
432 unsigned long long statm_resident;
433 unsigned long long statm_share;
434 // unsigned long long statm_text;
435 // unsigned long long statm_lib;
436 // unsigned long long statm_data;
437 // unsigned long long statm_dirty;
439 unsigned long long io_logical_bytes_read_raw;
440 unsigned long long io_logical_bytes_written_raw;
441 // unsigned long long io_read_calls_raw;
442 // unsigned long long io_write_calls_raw;
443 unsigned long long io_storage_bytes_read_raw;
444 unsigned long long io_storage_bytes_written_raw;
445 // unsigned long long io_cancelled_write_bytes_raw;
447 unsigned long long io_logical_bytes_read;
448 unsigned long long io_logical_bytes_written;
449 // unsigned long long io_read_calls;
450 // unsigned long long io_write_calls;
451 unsigned long long io_storage_bytes_read;
452 unsigned long long io_storage_bytes_written;
453 // unsigned long long io_cancelled_write_bytes;
455 int *fds; // array of fds it uses
456 int fds_size; // the size of the fds array
458 int children_count; // number of processes directly referencing this
459 int keep; // 1 when we need to keep this process in memory even after it exited
460 int keeploops; // increases by 1 every time keep is 1 and updated 0
461 int updated; // 1 when the process is currently running
462 int merged; // 1 when it has been merged to its parent
463 int new_entry; // 1 when this is a new process, just saw for the first time
464 int read; // 1 when we have already read this process for this iteration
465 int sortlist; // higher numbers = top on the process tree
466 // each process gets a unique number
468 struct target *target; // app_groups.conf targets
469 struct target *user_target; // uid based targets
470 struct target *group_target; // gid based targets
472 unsigned long long stat_collected_usec;
473 unsigned long long last_stat_collected_usec;
475 unsigned long long io_collected_usec;
476 unsigned long long last_io_collected_usec;
479 char *statm_filename;
481 char *cmdline_filename;
483 struct pid_stat *parent;
484 struct pid_stat *prev;
485 struct pid_stat *next;
486 } *root_of_pids = NULL, **all_pids;
488 long all_pids_count = 0;
490 static inline struct pid_stat *get_pid_entry(pid_t pid) {
492 all_pids[pid]->new_entry = 0;
493 return all_pids[pid];
496 all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
497 all_pids[pid]->fds = callocz(sizeof(int), MAX_SPARE_FDS);
498 all_pids[pid]->fds_size = MAX_SPARE_FDS;
500 if(root_of_pids) root_of_pids->prev = all_pids[pid];
501 all_pids[pid]->next = root_of_pids;
502 root_of_pids = all_pids[pid];
504 all_pids[pid]->pid = pid;
505 all_pids[pid]->new_entry = 1;
509 return all_pids[pid];
512 static inline void del_pid_entry(pid_t pid) {
514 error("attempted to free pid %d that is not allocated.", pid);
519 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
521 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
522 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
523 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
525 if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
526 if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
527 if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
528 if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
529 if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
530 freez(all_pids[pid]);
532 all_pids[pid] = NULL;
537 // ----------------------------------------------------------------------------
538 // update pids from proc
540 static inline int read_proc_pid_cmdline(struct pid_stat *p) {
542 if(unlikely(!p->cmdline_filename)) {
543 char filename[FILENAME_MAX + 1];
544 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", global_host_prefix, p->pid);
545 p->cmdline_filename = strdupz(filename);
548 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
549 if(unlikely(fd == -1)) goto cleanup;
551 ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
554 if(unlikely(bytes < 0)) goto cleanup;
556 p->cmdline[bytes] = '\0';
557 for(i = 0; i < bytes ; i++)
558 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
561 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
566 // copy the command to the command line
567 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
571 static inline int read_proc_pid_ownership(struct pid_stat *p) {
572 if(unlikely(!p->stat_filename)) {
573 error("pid %d does not have a stat_filename", p->pid);
577 // ----------------------------------------
581 if(stat(p->stat_filename, &st) != 0) {
582 error("Cannot stat file '%s'", p->stat_filename);
592 static inline int read_proc_pid_stat(struct pid_stat *p) {
593 static procfile *ff = NULL;
595 if(unlikely(!p->stat_filename)) {
596 char filename[FILENAME_MAX + 1];
597 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", global_host_prefix, p->pid);
598 p->stat_filename = strdupz(filename);
601 int set_quotes = (!ff)?1:0;
603 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
604 if(unlikely(!ff)) goto cleanup;
606 // if(set_quotes) procfile_set_quotes(ff, "()");
607 if(set_quotes) procfile_set_open_close(ff, "(", ")");
609 ff = procfile_readall(ff);
610 if(unlikely(!ff)) goto cleanup;
612 p->last_stat_collected_usec = p->stat_collected_usec;
613 p->stat_collected_usec = now_realtime_usec();
616 // p->pid = str2ul(procfile_lineword(ff, 0, 0+i));
618 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
620 // p->state = *(procfile_lineword(ff, 0, 2));
621 p->ppid = (int32_t)str2ul(procfile_lineword(ff, 0, 3));
622 // p->pgrp = str2ul(procfile_lineword(ff, 0, 4));
623 // p->session = str2ul(procfile_lineword(ff, 0, 5));
624 // p->tty_nr = str2ul(procfile_lineword(ff, 0, 6));
625 // p->tpgid = str2ul(procfile_lineword(ff, 0, 7));
626 // p->flags = str2ull(procfile_lineword(ff, 0, 8));
628 unsigned long long last;
630 last = p->minflt_raw;
631 p->minflt_raw = str2ull(procfile_lineword(ff, 0, 9));
632 p->minflt = (p->minflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
634 last = p->cminflt_raw;
635 p->cminflt_raw = str2ull(procfile_lineword(ff, 0, 10));
636 p->cminflt = (p->cminflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
638 last = p->majflt_raw;
639 p->majflt_raw = str2ull(procfile_lineword(ff, 0, 11));
640 p->majflt = (p->majflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
642 last = p->cmajflt_raw;
643 p->cmajflt_raw = str2ull(procfile_lineword(ff, 0, 12));
644 p->cmajflt = (p->cmajflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
647 p->utime_raw = str2ull(procfile_lineword(ff, 0, 13));
648 p->utime = (p->utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
651 p->stime_raw = str2ull(procfile_lineword(ff, 0, 14));
652 p->stime = (p->stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
654 last = p->cutime_raw;
655 p->cutime_raw = str2ull(procfile_lineword(ff, 0, 15));
656 p->cutime = (p->cutime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
658 last = p->cstime_raw;
659 p->cstime_raw = str2ull(procfile_lineword(ff, 0, 16));
660 p->cstime = (p->cstime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
662 // p->priority = str2ull(procfile_lineword(ff, 0, 17));
663 // p->nice = str2ull(procfile_lineword(ff, 0, 18));
664 p->num_threads = (int32_t)str2ul(procfile_lineword(ff, 0, 19));
665 // p->itrealvalue = str2ull(procfile_lineword(ff, 0, 20));
666 // p->starttime = str2ull(procfile_lineword(ff, 0, 21));
667 // p->vsize = str2ull(procfile_lineword(ff, 0, 22));
668 // p->rss = str2ull(procfile_lineword(ff, 0, 23));
669 // p->rsslim = str2ull(procfile_lineword(ff, 0, 24));
670 // p->starcode = str2ull(procfile_lineword(ff, 0, 25));
671 // p->endcode = str2ull(procfile_lineword(ff, 0, 26));
672 // p->startstack = str2ull(procfile_lineword(ff, 0, 27));
673 // p->kstkesp = str2ull(procfile_lineword(ff, 0, 28));
674 // p->kstkeip = str2ull(procfile_lineword(ff, 0, 29));
675 // p->signal = str2ull(procfile_lineword(ff, 0, 30));
676 // p->blocked = str2ull(procfile_lineword(ff, 0, 31));
677 // p->sigignore = str2ull(procfile_lineword(ff, 0, 32));
678 // p->sigcatch = str2ull(procfile_lineword(ff, 0, 33));
679 // p->wchan = str2ull(procfile_lineword(ff, 0, 34));
680 // p->nswap = str2ull(procfile_lineword(ff, 0, 35));
681 // p->cnswap = str2ull(procfile_lineword(ff, 0, 36));
682 // p->exit_signal = str2ul(procfile_lineword(ff, 0, 37));
683 // p->processor = str2ul(procfile_lineword(ff, 0, 38));
684 // p->rt_priority = str2ul(procfile_lineword(ff, 0, 39));
685 // p->policy = str2ul(procfile_lineword(ff, 0, 40));
686 // p->delayacct_blkio_ticks = str2ull(procfile_lineword(ff, 0, 41));
688 if(enable_guest_charts) {
690 p->gtime_raw = str2ull(procfile_lineword(ff, 0, 42));
691 p->gtime = (p->gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
693 last = p->cgtime_raw;
694 p->cgtime_raw = str2ull(procfile_lineword(ff, 0, 43));
695 p->cgtime = (p->cgtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
697 if (show_guest_time || p->gtime || p->cgtime) {
698 p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
699 p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
704 if(unlikely(debug || (p->target && p->target->debug)))
705 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);
707 if(unlikely(global_iterations_counter == 1)) {
738 static inline int read_proc_pid_statm(struct pid_stat *p) {
739 static procfile *ff = NULL;
741 if(unlikely(!p->statm_filename)) {
742 char filename[FILENAME_MAX + 1];
743 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", global_host_prefix, p->pid);
744 p->statm_filename = strdupz(filename);
747 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
748 if(unlikely(!ff)) goto cleanup;
750 ff = procfile_readall(ff);
751 if(unlikely(!ff)) goto cleanup;
755 p->statm_size = str2ull(procfile_lineword(ff, 0, 0));
756 p->statm_resident = str2ull(procfile_lineword(ff, 0, 1));
757 p->statm_share = str2ull(procfile_lineword(ff, 0, 2));
758 // p->statm_text = str2ull(procfile_lineword(ff, 0, 3));
759 // p->statm_lib = str2ull(procfile_lineword(ff, 0, 4));
760 // p->statm_data = str2ull(procfile_lineword(ff, 0, 5));
761 // p->statm_dirty = str2ull(procfile_lineword(ff, 0, 6));
767 p->statm_resident = 0;
769 // p->statm_text = 0;
771 // p->statm_data = 0;
772 // p->statm_dirty = 0;
776 static inline int read_proc_pid_io(struct pid_stat *p) {
777 static procfile *ff = NULL;
779 if(unlikely(!p->io_filename)) {
780 char filename[FILENAME_MAX + 1];
781 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", global_host_prefix, p->pid);
782 p->io_filename = strdupz(filename);
786 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
787 if(unlikely(!ff)) goto cleanup;
789 ff = procfile_readall(ff);
790 if(unlikely(!ff)) goto cleanup;
794 p->last_io_collected_usec = p->io_collected_usec;
795 p->io_collected_usec = now_realtime_usec();
797 unsigned long long last;
799 last = p->io_logical_bytes_read_raw;
800 p->io_logical_bytes_read_raw = str2ull(procfile_lineword(ff, 0, 1));
801 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);
803 last = p->io_logical_bytes_written_raw;
804 p->io_logical_bytes_written_raw = str2ull(procfile_lineword(ff, 1, 1));
805 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);
807 // last = p->io_read_calls_raw;
808 // p->io_read_calls_raw = str2ull(procfile_lineword(ff, 2, 1));
809 // p->io_read_calls = (p->io_read_calls_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
811 // last = p->io_write_calls_raw;
812 // p->io_write_calls_raw = str2ull(procfile_lineword(ff, 3, 1));
813 // p->io_write_calls = (p->io_write_calls_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
815 last = p->io_storage_bytes_read_raw;
816 p->io_storage_bytes_read_raw = str2ull(procfile_lineword(ff, 4, 1));
817 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);
819 last = p->io_storage_bytes_written_raw;
820 p->io_storage_bytes_written_raw = str2ull(procfile_lineword(ff, 5, 1));
821 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);
823 // last = p->io_cancelled_write_bytes_raw;
824 // p->io_cancelled_write_bytes_raw = str2ull(procfile_lineword(ff, 6, 1));
825 // 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);
827 if(unlikely(global_iterations_counter == 1)) {
828 p->io_logical_bytes_read = 0;
829 p->io_logical_bytes_written = 0;
830 // p->io_read_calls = 0;
831 // p->io_write_calls = 0;
832 p->io_storage_bytes_read = 0;
833 p->io_storage_bytes_written = 0;
834 // p->io_cancelled_write_bytes = 0;
840 p->io_logical_bytes_read = 0;
841 p->io_logical_bytes_written = 0;
842 // p->io_read_calls = 0;
843 // p->io_write_calls = 0;
844 p->io_storage_bytes_read = 0;
845 p->io_storage_bytes_written = 0;
846 // p->io_cancelled_write_bytes = 0;
850 unsigned long long global_utime = 0;
851 unsigned long long global_stime = 0;
852 unsigned long long global_gtime = 0;
854 static inline int read_proc_stat() {
855 static char filename[FILENAME_MAX + 1] = "";
856 static procfile *ff = NULL;
857 static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
858 static usec_t collected_usec = 0, last_collected_usec = 0;
861 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
862 ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
863 if(unlikely(!ff)) goto cleanup;
866 ff = procfile_readall(ff);
867 if(unlikely(!ff)) goto cleanup;
869 last_collected_usec = collected_usec;
870 collected_usec = now_realtime_usec();
874 unsigned long long last;
877 utime_raw = str2ull(procfile_lineword(ff, 0, 1));
878 global_utime = (utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
880 // nice time, on user time
882 ntime_raw = str2ull(procfile_lineword(ff, 0, 2));
883 global_utime += (ntime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
886 stime_raw = str2ull(procfile_lineword(ff, 0, 3));
887 global_stime = (stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
890 gtime_raw = str2ull(procfile_lineword(ff, 0, 10));
891 global_gtime = (gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
893 if(enable_guest_charts) {
894 // guest nice time, on guest time
896 gntime_raw = str2ull(procfile_lineword(ff, 0, 11));
897 global_gtime += (gntime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (collected_usec - last_collected_usec);
899 // remove guest time from user time
900 global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
903 if(unlikely(global_iterations_counter == 1)) {
919 // ----------------------------------------------------------------------------
921 // this is used to keep a global list of all open files of the system
922 // it is needed in order to calculate the unique files processes have open
924 #define FILE_DESCRIPTORS_INCREASE_STEP 100
926 struct file_descriptor {
928 #ifdef NETDATA_INTERNAL_CHECKS
930 #endif /* NETDATA_INTERNAL_CHECKS */
938 int all_files_len = 0;
939 int all_files_size = 0;
941 int file_descriptor_compare(void* a, void* b) {
942 #ifdef NETDATA_INTERNAL_CHECKS
943 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
944 error("Corrupted index data detected. Please report this.");
945 #endif /* NETDATA_INTERNAL_CHECKS */
947 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
950 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
954 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
957 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
959 avl_tree all_files_index = {
961 file_descriptor_compare
964 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
965 struct file_descriptor tmp;
966 tmp.hash = (hash)?hash:simple_hash(name);
970 #ifdef NETDATA_INTERNAL_CHECKS
971 tmp.magic = 0x0BADCAFE;
972 #endif /* NETDATA_INTERNAL_CHECKS */
974 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
977 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
978 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
980 #define FILETYPE_OTHER 0
981 #define FILETYPE_FILE 1
982 #define FILETYPE_PIPE 2
983 #define FILETYPE_SOCKET 3
984 #define FILETYPE_INOTIFY 4
985 #define FILETYPE_EVENTFD 5
986 #define FILETYPE_EVENTPOLL 6
987 #define FILETYPE_TIMERFD 7
988 #define FILETYPE_SIGNALFD 8
990 static inline void file_descriptor_not_used(int id)
992 if(id > 0 && id < all_files_size) {
994 #ifdef NETDATA_INTERNAL_CHECKS
995 if(all_files[id].magic != 0x0BADCAFE) {
996 error("Ignoring request to remove empty file id %d.", id);
999 #endif /* NETDATA_INTERNAL_CHECKS */
1002 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1004 if(all_files[id].count > 0) {
1005 all_files[id].count--;
1007 if(!all_files[id].count) {
1009 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1011 if(unlikely(file_descriptor_remove(&all_files[id]) != (void *)&all_files[id]))
1012 error("INTERNAL ERROR: removal of unused fd from index, removed a different fd");
1014 #ifdef NETDATA_INTERNAL_CHECKS
1015 all_files[id].magic = 0x00000000;
1016 #endif /* NETDATA_INTERNAL_CHECKS */
1021 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1023 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1026 static inline int file_descriptor_find_or_add(const char *name)
1028 static int last_pos = 0;
1029 uint32_t hash = simple_hash(name);
1032 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1034 struct file_descriptor *fd = file_descriptor_find(name, hash);
1038 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1045 // check we have enough memory to add it
1046 if(!all_files || all_files_len == all_files_size) {
1047 void *old = all_files;
1050 // there is no empty slot
1052 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1054 all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1056 // if the address changed, we have to rebuild the index
1057 // since all pointers are now invalid
1058 if(old && old != (void *)all_files) {
1060 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1062 all_files_index.root = NULL;
1063 for(i = 0; i < all_files_size; i++) {
1064 if(!all_files[i].count) continue;
1065 if(unlikely(file_descriptor_add(&all_files[i]) != (void *)&all_files[i]))
1066 error("INTERNAL ERROR: duplicate indexing of fd during realloc.");
1070 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1073 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1074 all_files[i].count = 0;
1075 all_files[i].name = NULL;
1076 #ifdef NETDATA_INTERNAL_CHECKS
1077 all_files[i].magic = 0x00000000;
1078 #endif /* NETDATA_INTERNAL_CHECKS */
1079 all_files[i].pos = i;
1082 if(!all_files_size) all_files_len = 1;
1083 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1087 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1089 // search for an empty slot
1091 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1092 if(c >= all_files_size) c = 0;
1093 if(c == 0) continue;
1095 if(!all_files[c].count) {
1097 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1099 #ifdef NETDATA_INTERNAL_CHECKS
1100 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1101 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1102 #endif /* NETDATA_INTERNAL_CHECKS */
1105 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);
1107 if(all_files[c].name) freez((void *)all_files[c].name);
1108 all_files[c].name = NULL;
1113 if(i == all_files_size) {
1114 fatal("We should find an empty slot, but there isn't any");
1119 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1123 // else we have an empty slot in 'c'
1126 if(name[0] == '/') type = FILETYPE_FILE;
1127 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1128 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1129 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1130 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1131 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1132 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1133 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1134 else if(strncmp(name, "anon_inode:", 11) == 0) {
1136 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1138 type = FILETYPE_OTHER;
1142 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1144 type = FILETYPE_OTHER;
1147 all_files[c].name = strdupz(name);
1148 all_files[c].hash = hash;
1149 all_files[c].type = type;
1150 all_files[c].pos = c;
1151 all_files[c].count = 1;
1152 #ifdef NETDATA_INTERNAL_CHECKS
1153 all_files[c].magic = 0x0BADCAFE;
1154 #endif /* NETDATA_INTERNAL_CHECKS */
1155 if(unlikely(file_descriptor_add(&all_files[c]) != (void *)&all_files[c]))
1156 error("INTERNAL ERROR: duplicate indexing of fd.");
1159 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1164 static inline int read_pid_file_descriptors(struct pid_stat *p) {
1165 char dirname[FILENAME_MAX+1];
1167 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", global_host_prefix, p->pid);
1168 DIR *fds = opendir(dirname);
1172 char fdname[FILENAME_MAX + 1];
1173 char linkname[FILENAME_MAX + 1];
1175 // make the array negative
1176 for(c = 0 ; c < p->fds_size ; c++)
1177 p->fds[c] = -p->fds[c];
1179 while((de = readdir(fds))) {
1180 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1183 // check if the fds array is small
1184 int fdid = (int)str2l(de->d_name);
1185 if(fdid < 0) continue;
1186 if(fdid >= p->fds_size) {
1187 // it is small, extend it
1189 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + MAX_SPARE_FDS);
1191 p->fds = reallocz(p->fds, (fdid + MAX_SPARE_FDS) * sizeof(int));
1193 fatal("Cannot re-allocate fds for %s", p->comm);
1197 // and initialize it
1198 for(c = p->fds_size ; c < (fdid + MAX_SPARE_FDS) ; c++) p->fds[c] = 0;
1199 p->fds_size = fdid + MAX_SPARE_FDS;
1202 if(p->fds[fdid] == 0) {
1203 // we don't know this fd, get it
1205 sprintf(fdname, "%s/proc/%d/fd/%s", global_host_prefix, p->pid, de->d_name);
1206 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1208 if(debug || (p->target && p->target->debug)) {
1209 if(debug || (p->target && p->target->debug))
1210 error("Cannot read link %s", fdname);
1217 // if another process already has this, we will get
1219 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1222 // else make it positive again, we need it
1223 // of course, the actual file may have changed, but we don't care so much
1224 // FIXME: we could compare the inode as returned by readdir dirent structure
1225 else p->fds[fdid] = -p->fds[fdid];
1229 // remove all the negative file descriptors
1230 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1231 file_descriptor_not_used(-p->fds[c]);
1240 // ----------------------------------------------------------------------------
1242 static inline int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1243 char *prefix = "\\_ ";
1247 indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1251 char buffer[indent + 1];
1254 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1257 fprintf(stderr, " %s %s%s (%d %s %lld"
1262 , p->updated?"running":"exited"
1263 , (long long)p->stat_collected_usec - (long long)time
1266 if(p->utime) fprintf(stderr, " utime=%llu", p->utime);
1267 if(p->stime) fprintf(stderr, " stime=%llu", p->stime);
1268 if(p->gtime) fprintf(stderr, " gtime=%llu", p->gtime);
1269 if(p->cutime) fprintf(stderr, " cutime=%llu", p->cutime);
1270 if(p->cstime) fprintf(stderr, " cstime=%llu", p->cstime);
1271 if(p->cgtime) fprintf(stderr, " cgtime=%llu", p->cgtime);
1272 if(p->minflt) fprintf(stderr, " minflt=%llu", p->minflt);
1273 if(p->cminflt) fprintf(stderr, " cminflt=%llu", p->cminflt);
1274 if(p->majflt) fprintf(stderr, " majflt=%llu", p->majflt);
1275 if(p->cmajflt) fprintf(stderr, " cmajflt=%llu", p->cmajflt);
1276 fprintf(stderr, ")\n");
1281 static inline void print_process_tree(struct pid_stat *p, char *msg) {
1283 fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1284 print_process_and_parents(p, p->stat_collected_usec);
1287 static inline void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1289 struct pid_stat *p = NULL;
1291 for(p = root_of_pids; p ; p = p->next) {
1292 if(p == pe) continue;
1296 if(p->cminflt > lost) {
1297 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);
1303 if(p->cmajflt > lost) {
1304 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);
1310 if(p->cutime > lost) {
1311 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);
1317 if(p->cstime > lost) {
1318 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);
1324 if(p->cgtime > lost) {
1325 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);
1335 fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1339 fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1343 fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1347 fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1351 fprintf(stderr, " > cannot find any process to use the lost exited child gtime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1357 static inline unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1358 unsigned long long absorbed = 0;
1360 if(*field > *pfield) {
1361 absorbed += *pfield;
1374 static inline void process_exited_processes() {
1377 for(p = root_of_pids; p ; p = p->next) {
1378 if(p->updated || !p->stat_collected_usec)
1381 struct pid_stat *pp = p->parent;
1383 unsigned long long utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1384 unsigned long long stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1385 unsigned long long gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1386 unsigned long long minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1387 unsigned long long majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1389 if(utime + stime + gtime + minflt + majflt == 0)
1392 if(unlikely(debug)) {
1394 fprintf(stderr, "Absorb %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1397 , p->updated?"running":"exited"
1404 print_process_tree(p, "Searching parents");
1407 for(pp = p->parent; pp ; pp = pp->parent) {
1408 if(!pp->updated) continue;
1410 unsigned long long absorbed;
1411 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1412 if(unlikely(debug && absorbed))
1413 fprintf(stderr, " > process %s (%d %s) absorbed %llu utime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1415 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1416 if(unlikely(debug && absorbed))
1417 fprintf(stderr, " > process %s (%d %s) absorbed %llu stime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1419 absorbed = remove_exited_child_from_parent(>ime, &pp->cgtime);
1420 if(unlikely(debug && absorbed))
1421 fprintf(stderr, " > process %s (%d %s) absorbed %llu gtime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1423 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1424 if(unlikely(debug && absorbed))
1425 fprintf(stderr, " > process %s (%d %s) absorbed %llu minflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1427 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1428 if(unlikely(debug && absorbed))
1429 fprintf(stderr, " > process %s (%d %s) absorbed %llu majflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1432 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1433 if(unlikely(debug)) {
1434 if(utime) find_lost_child_debug(p, utime, 3);
1435 if(stime) find_lost_child_debug(p, stime, 4);
1436 if(gtime) find_lost_child_debug(p, gtime, 5);
1437 if(minflt) find_lost_child_debug(p, minflt, 1);
1438 if(majflt) find_lost_child_debug(p, majflt, 2);
1444 fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1447 , p->updated?"running":"exited"
1455 for(pp = p->parent; pp ; pp = pp->parent) {
1456 if(pp->updated) break;
1460 fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
1463 , pp->updated?"running":"exited"
1467 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1468 p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1469 p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1470 p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1471 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (USEC_PER_SEC * RATES_DETAIL);
1472 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
1475 fprintf(stderr, "\n");
1477 else if(unlikely(debug)) {
1478 fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
1481 , p->updated?"running":"exited"
1487 static inline void link_all_processes_to_their_parents(void) {
1488 struct pid_stat *p, *pp;
1490 // link all children to their parents
1491 // and update children count on parents
1492 for(p = root_of_pids; p ; p = p->next) {
1493 // for each process found
1498 if(unlikely(!p->ppid)) {
1503 pp = all_pids[p->ppid];
1506 pp->children_count++;
1508 if(unlikely(debug || (p->target && p->target->debug)))
1509 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);
1513 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1518 // ----------------------------------------------------------------------------
1520 // 1. read all files in /proc
1521 // 2. for each numeric directory:
1522 // i. read /proc/pid/stat
1523 // ii. read /proc/pid/statm
1524 // iii. read /proc/pid/io (requires root access)
1525 // iii. read the entries in directory /proc/pid/fd (requires root access)
1527 // a. find or create a struct file_descriptor
1528 // b. cleanup any old/unused file_descriptors
1530 // after all these, some pids may be linked to targets, while others may not
1532 // in case of errors, only 1 every 1000 errors is printed
1533 // to avoid filling up all disk space
1534 // if debug is enabled, all errors are printed
1536 static int compar_pid(const void *pid1, const void *pid2) {
1538 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1539 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1541 if(p1->sortlist > p2->sortlist)
1547 static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
1548 if(unlikely(!status)) {
1549 // error("command failed log %u, errno %d", log, errno);
1551 if(unlikely(debug || errno != ENOENT)) {
1552 if(unlikely(debug || !(p->log_thrown & log))) {
1553 p->log_thrown |= log;
1556 error("Cannot process %s/proc/%d/io (command '%s')", global_host_prefix, p->pid, p->comm);
1560 error("Cannot process %s/proc/%d/statm (command '%s')", global_host_prefix, p->pid, p->comm);
1563 case PID_LOG_CMDLINE:
1564 error("Cannot process %s/proc/%d/cmdline (command '%s')", global_host_prefix, p->pid, p->comm);
1568 error("Cannot process entries in %s/proc/%d/fd (command '%s')", global_host_prefix, p->pid, p->comm);
1575 error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
1582 else if(unlikely(p->log_thrown & log)) {
1583 // error("unsetting log %u on pid %d", log, p->pid);
1584 p->log_thrown &= ~log;
1590 static inline int collect_data_for_pid(pid_t pid) {
1591 if(unlikely(pid <= 0 || pid > pid_max)) {
1592 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1596 struct pid_stat *p = get_pid_entry(pid);
1597 if(unlikely(!p || p->read)) return 0;
1600 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1602 // --------------------------------------------------------------------
1605 if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p))))
1606 // there is no reason to proceed if we cannot get its status
1609 read_proc_pid_ownership(p);
1611 // check its parent pid
1612 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1613 error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
1617 // --------------------------------------------------------------------
1620 managed_log(p, PID_LOG_IO, read_proc_pid_io(p));
1622 // --------------------------------------------------------------------
1623 // /proc/<pid>/statm
1625 if(unlikely(!managed_log(p, PID_LOG_STATM, read_proc_pid_statm(p))))
1626 // there is no reason to proceed if we cannot get its memory status
1629 // --------------------------------------------------------------------
1632 // check if it is target
1633 // we do this only once, the first time this pid is loaded
1634 if(unlikely(p->new_entry)) {
1635 // /proc/<pid>/cmdline
1636 if(likely(proc_pid_cmdline_is_needed))
1637 managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
1640 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1642 uint32_t hash = simple_hash(p->comm);
1643 size_t pclen = strlen(p->comm);
1646 for(w = apps_groups_root_target; w ; w = w->next) {
1647 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1649 // find it - 4 cases:
1650 // 1. the target is not a pattern
1651 // 2. the target has the prefix
1652 // 3. the target has the suffix
1653 // 4. the target is something inside cmdline
1654 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1655 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1656 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1657 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1659 if(w->target) p->target = w->target;
1662 if(debug || (p->target && p->target->debug))
1663 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1670 // --------------------------------------------------------------------
1673 if(enable_file_charts)
1674 managed_log(p, PID_LOG_FDS, read_pid_file_descriptors(p));
1676 // --------------------------------------------------------------------
1679 if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1680 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);
1682 // mark it as updated
1690 static int collect_data_for_all_processes_from_proc(void) {
1691 struct pid_stat *p = NULL;
1693 if(all_pids_count) {
1694 // read parents before childs
1695 // this is needed to prevent a situation where
1696 // a child is found running, but until we read
1697 // its parent, it has exited and its parent
1698 // has accumulated its resources
1701 for(p = root_of_pids; p ; p = p->next) {
1706 p->children_count = 0;
1709 all_pids_sortlist[slc++] = p->pid;
1712 if(unlikely(slc != all_pids_count)) {
1713 error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1714 all_pids_count = slc;
1717 if(include_exited_childs) {
1718 qsort((void *)all_pids_sortlist, all_pids_count, sizeof(pid_t), compar_pid);
1719 for(slc = 0; slc < all_pids_count; slc++)
1720 collect_data_for_pid(all_pids_sortlist[slc]);
1724 char dirname[FILENAME_MAX + 1];
1726 snprintfz(dirname, FILENAME_MAX, "%s/proc", global_host_prefix);
1727 DIR *dir = opendir(dirname);
1730 struct dirent *file = NULL;
1732 while((file = readdir(dir))) {
1733 char *endptr = file->d_name;
1734 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1736 // make sure we read a valid number
1737 if(unlikely(endptr == file->d_name || *endptr != '\0'))
1740 collect_data_for_pid(pid);
1747 // normally this is done
1748 // however we may have processes exited while we collected values
1749 // so let's find the exited ones
1750 // we do this by collecting the ownership of process
1751 // if we manage to get the ownership, the process still runs
1754 link_all_processes_to_their_parents();
1755 process_exited_processes();
1760 // ----------------------------------------------------------------------------
1761 // update statistics on the targets
1763 // 1. link all childs to their parents
1764 // 2. go from bottom to top, marking as merged all childs to their parents
1765 // this step links all parents without a target to the child target, if any
1766 // 3. link all top level processes (the ones not merged) to the default target
1767 // 4. go from top to bottom, linking all childs without a target, to their parent target
1768 // after this step, all processes have a target
1769 // [5. for each killed pid (updated = 0), remove its usage from its target]
1770 // 6. zero all apps_groups_targets
1771 // 7. concentrate all values on the apps_groups_targets
1772 // 8. remove all killed processes
1773 // 9. find the unique file count for each target
1774 // check: update_apps_groups_statistics()
1776 static void cleanup_exited_pids(void) {
1778 struct pid_stat *p = NULL;
1780 for(p = root_of_pids; p ;) {
1781 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1782 // 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);
1784 if(unlikely(debug && (p->keep || p->keeploops)))
1785 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1787 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1788 file_descriptor_not_used(p->fds[c]);
1797 if(unlikely(p->keep)) p->keeploops++;
1804 static void apply_apps_groups_targets_inheritance(void) {
1805 struct pid_stat *p = NULL;
1807 // children that do not have a target
1808 // inherit their target from their parent
1809 int found = 1, loops = 0;
1811 if(unlikely(debug)) loops++;
1813 for(p = root_of_pids; p ; p = p->next) {
1814 // if this process does not have a target
1815 // and it has a parent
1816 // and its parent has a target
1817 // then, set the parent's target to this process
1818 if(unlikely(!p->target && p->parent && p->parent->target)) {
1819 p->target = p->parent->target;
1822 if(debug || (p->target && p->target->debug))
1823 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);
1828 // find all the procs with 0 childs and merge them to their parents
1829 // repeat, until nothing more can be done.
1833 if(unlikely(debug)) loops++;
1836 for(p = root_of_pids; p ; p = p->next) {
1837 if(unlikely(!p->sortlist && !p->children_count))
1838 p->sortlist = sortlist++;
1840 // if this process does not have any children
1841 // and is not already merged
1843 // and its parent has children
1844 // and the target of this process and its parent is the same, or the parent does not have a target
1845 // and its parent is not init
1846 // then, mark them as merged.
1851 && p->parent->children_count
1852 && (p->target == p->parent->target || !p->parent->target)
1855 p->parent->children_count--;
1858 // the parent inherits the child's target, if it does not have a target itself
1859 if(unlikely(p->target && !p->parent->target)) {
1860 p->parent->target = p->target;
1862 if(debug || (p->target && p->target->debug))
1863 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);
1871 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
1874 // init goes always to default target
1876 all_pids[1]->target = apps_groups_default_target;
1878 // give a default target on all top level processes
1879 if(unlikely(debug)) loops++;
1880 for(p = root_of_pids; p ; p = p->next) {
1881 // if the process is not merged itself
1882 // then is is a top level process
1883 if(unlikely(!p->merged && !p->target))
1884 p->target = apps_groups_default_target;
1886 // make sure all processes have a sortlist
1887 if(unlikely(!p->sortlist))
1888 p->sortlist = sortlist++;
1892 all_pids[1]->sortlist = sortlist++;
1894 // give a target to all merged child processes
1897 if(unlikely(debug)) loops++;
1899 for(p = root_of_pids; p ; p = p->next) {
1900 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1901 p->target = p->parent->target;
1904 if(debug || (p->target && p->target->debug))
1905 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);
1911 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1914 static long zero_all_targets(struct target *root) {
1918 for (w = root; w ; w = w->next) {
1936 w->statm_resident = 0;
1938 // w->statm_text = 0;
1939 // w->statm_lib = 0;
1940 // w->statm_data = 0;
1941 // w->statm_dirty = 0;
1943 w->io_logical_bytes_read = 0;
1944 w->io_logical_bytes_written = 0;
1945 // w->io_read_calls = 0;
1946 // w->io_write_calls = 0;
1947 w->io_storage_bytes_read = 0;
1948 w->io_storage_bytes_written = 0;
1949 // w->io_cancelled_write_bytes = 0;
1951 // zero file counters
1953 memset(w->target_fds, 0, sizeof(int) * w->target_fds_size);
1957 w->openinotifies = 0;
1958 w->openeventfds = 0;
1959 w->opentimerfds = 0;
1960 w->opensignalfds = 0;
1961 w->openeventpolls = 0;
1969 static inline void reallocate_target_fds(struct target *w) {
1970 w->target_fds = reallocz(w->target_fds, sizeof(int) * all_files_size);
1971 memset(&w->target_fds[w->target_fds_size], 0, sizeof(int) * (all_files_size - w->target_fds_size));
1972 w->target_fds_size = all_files_size;
1975 static inline void aggregate_pid_fds_on_target(struct target *w, struct pid_stat *p) {
1976 if(unlikely(!w->target_fds || w->target_fds_size < all_files_size))
1977 reallocate_target_fds(w);
1979 int c, size = p->fds_size, *fds = p->fds;
1980 for(c = 0; c < size ;c++) {
1983 if(likely(fd <= 0 || fd >= all_files_size))
1986 if(unlikely(!w->target_fds[fd])) {
1987 switch(all_files[fd].type) {
1996 case FILETYPE_SOCKET:
2000 case FILETYPE_INOTIFY:
2004 case FILETYPE_EVENTFD:
2008 case FILETYPE_TIMERFD:
2012 case FILETYPE_SIGNALFD:
2016 case FILETYPE_EVENTPOLL:
2017 w->openeventpolls++;
2024 w->target_fds[fd]++;
2028 static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
2031 if(likely(p->updated)) {
2032 w->cutime += p->cutime;
2033 w->cstime += p->cstime;
2034 w->cgtime += p->cgtime;
2035 w->cminflt += p->cminflt;
2036 w->cmajflt += p->cmajflt;
2038 w->utime += p->utime;
2039 w->stime += p->stime;
2040 w->gtime += p->gtime;
2041 w->minflt += p->minflt;
2042 w->majflt += p->majflt;
2044 // w->rss += p->rss;
2046 w->statm_size += p->statm_size;
2047 w->statm_resident += p->statm_resident;
2048 w->statm_share += p->statm_share;
2049 // w->statm_text += p->statm_text;
2050 // w->statm_lib += p->statm_lib;
2051 // w->statm_data += p->statm_data;
2052 // w->statm_dirty += p->statm_dirty;
2054 w->io_logical_bytes_read += p->io_logical_bytes_read;
2055 w->io_logical_bytes_written += p->io_logical_bytes_written;
2056 // w->io_read_calls += p->io_read_calls;
2057 // w->io_write_calls += p->io_write_calls;
2058 w->io_storage_bytes_read += p->io_storage_bytes_read;
2059 w->io_storage_bytes_written += p->io_storage_bytes_written;
2060 // w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
2063 w->num_threads += p->num_threads;
2065 aggregate_pid_fds_on_target(w, p);
2067 if(unlikely(debug || w->debug))
2068 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);
2072 static void calculate_netdata_statistics(void) {
2073 apply_apps_groups_targets_inheritance();
2075 zero_all_targets(users_root_target);
2076 zero_all_targets(groups_root_target);
2077 apps_groups_targets = zero_all_targets(apps_groups_root_target);
2079 // this has to be done, before the cleanup
2080 struct pid_stat *p = NULL;
2081 struct target *w = NULL, *o = NULL;
2083 // concentrate everything on the apps_groups_targets
2084 for(p = root_of_pids; p ; p = p->next) {
2086 // --------------------------------------------------------------------
2087 // apps_groups targets
2088 if(likely(p->target))
2089 aggregate_pid_on_target(p->target, p, NULL);
2091 error("pid %d %s was left without a target!", p->pid, p->comm);
2094 // --------------------------------------------------------------------
2097 if(likely(p->user_target && p->user_target->uid == p->uid))
2100 if(unlikely(debug && p->user_target))
2101 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);
2103 w = p->user_target = get_users_target(p->uid);
2107 aggregate_pid_on_target(w, p, o);
2109 error("pid %d %s was left without a user target!", p->pid, p->comm);
2112 // --------------------------------------------------------------------
2114 o = p->group_target;
2115 if(likely(p->group_target && p->group_target->gid == p->gid))
2116 w = p->group_target;
2118 if(unlikely(debug && p->group_target))
2119 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);
2121 w = p->group_target = get_groups_target(p->gid);
2125 aggregate_pid_on_target(w, p, o);
2127 error("pid %d %s was left without a group target!", p->pid, p->comm);
2131 cleanup_exited_pids();
2134 // ----------------------------------------------------------------------------
2135 // update chart dimensions
2137 BUFFER *output = NULL;
2138 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2140 static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2141 // fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2142 buffer_strcat(output, "BEGIN ");
2143 buffer_strcat(output, type);
2144 buffer_strcat(output, ".");
2145 buffer_strcat(output, id);
2146 buffer_strcat(output, " ");
2147 buffer_print_llu(output, usec);
2148 buffer_strcat(output, "\n");
2151 static inline void send_SET(const char *name, unsigned long long value) {
2152 // fprintf(stdout, "SET %s = %llu\n", name, value);
2153 buffer_strcat(output, "SET ");
2154 buffer_strcat(output, name);
2155 buffer_strcat(output, " = ");
2156 buffer_print_llu(output, value);
2157 buffer_strcat(output, "\n");
2160 static inline void send_END(void) {
2161 // fprintf(stdout, "END\n");
2162 buffer_strcat(output, "END\n");
2165 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;
2166 double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2168 static usec_t send_resource_usage_to_netdata() {
2169 static struct timeval last = { 0, 0 };
2170 static struct rusage me_last;
2180 now_realtime_timeval(&last);
2181 getrusage(RUSAGE_SELF, &me_last);
2183 // the first time, give a zero to allow
2184 // netdata calibrate to the current time
2185 // usec = update_every * USEC_PER_SEC;
2191 now_realtime_timeval(&now);
2192 getrusage(RUSAGE_SELF, &me);
2194 usec = dt_usec(&now, &last);
2195 cpuuser = me.ru_utime.tv_sec * USEC_PER_SEC + me.ru_utime.tv_usec;
2196 cpusyst = me.ru_stime.tv_sec * USEC_PER_SEC + me.ru_stime.tv_usec;
2198 memmove(&last, &now, sizeof(struct timeval));
2199 memmove(&me_last, &me, sizeof(struct rusage));
2202 buffer_sprintf(output,
2203 "BEGIN netdata.apps_cpu %llu\n"
2205 "SET system = %llu\n"
2207 "BEGIN netdata.apps_files %llu\n"
2208 "SET files = %llu\n"
2211 "SET targets = %ld\n"
2213 "BEGIN netdata.apps_fix %llu\n"
2214 "SET utime = %llu\n"
2215 "SET stime = %llu\n"
2216 "SET gtime = %llu\n"
2217 "SET minflt = %llu\n"
2218 "SET majflt = %llu\n"
2227 , apps_groups_targets
2229 , (unsigned long long)(utime_fix_ratio * 100 * RATES_DETAIL)
2230 , (unsigned long long)(stime_fix_ratio * 100 * RATES_DETAIL)
2231 , (unsigned long long)(gtime_fix_ratio * 100 * RATES_DETAIL)
2232 , (unsigned long long)(minflt_fix_ratio * 100 * RATES_DETAIL)
2233 , (unsigned long long)(majflt_fix_ratio * 100 * RATES_DETAIL)
2236 if(include_exited_childs)
2237 buffer_sprintf(output,
2238 "BEGIN netdata.apps_children_fix %llu\n"
2239 "SET cutime = %llu\n"
2240 "SET cstime = %llu\n"
2241 "SET cgtime = %llu\n"
2242 "SET cminflt = %llu\n"
2243 "SET cmajflt = %llu\n"
2246 , (unsigned long long)(cutime_fix_ratio * 100 * RATES_DETAIL)
2247 , (unsigned long long)(cstime_fix_ratio * 100 * RATES_DETAIL)
2248 , (unsigned long long)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2249 , (unsigned long long)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2250 , (unsigned long long)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2256 static void normalize_data(struct target *root) {
2259 // childs processing introduces spikes
2260 // here we try to eliminate them by disabling childs processing either for specific dimensions
2261 // or entirely. Of course, either way, we disable it just a single iteration.
2263 unsigned long long max = processors * hz * RATES_DETAIL;
2264 unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2266 if(global_utime > max) global_utime = max;
2267 if(global_stime > max) global_stime = max;
2268 if(global_gtime > max) global_gtime = max;
2270 for(w = root; w ; w = w->next) {
2271 if(w->target || (!w->processes && !w->exposed)) continue;
2276 cutime += w->cutime;
2277 cstime += w->cstime;
2278 cgtime += w->cgtime;
2280 minflt += w->minflt;
2281 majflt += w->majflt;
2282 cminflt += w->cminflt;
2283 cmajflt += w->cmajflt;
2286 if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
2287 if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
2288 // everything we collected fits
2294 cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
2296 else if(global_utime + global_stime > utime + stime) {
2297 // childrens resources are too high
2298 // lower only the children resources
2301 gtime_fix_ratio = 1.0;
2304 cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
2307 // even running processes are unrealistic
2308 // zero the children resources
2309 // lower the running processes resources
2312 gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
2315 cgtime_fix_ratio = 0.0;
2324 cgtime_fix_ratio = 0.0;
2327 if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
2328 if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
2329 if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
2330 if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
2331 if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
2332 if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
2334 // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
2335 // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
2336 // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
2337 // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
2338 // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
2339 // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
2342 // we use cpu time to normalize page faults
2343 // the problem is that to find the proper max values
2344 // for page faults we have to parse /proc/vmstat
2345 // which is quite big to do it again (netdata does it already)
2347 // a better solution could be to somehow have netdata
2348 // do this normalization for us
2350 if(utime || stime || gtime)
2352 minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
2355 majflt_fix_ratio = 1.0;
2357 if(cutime || cstime || cgtime)
2359 cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
2362 cmajflt_fix_ratio = 1.0;
2366 if(unlikely(debug)) {
2368 "SYSTEM: u=%llu s=%llu g=%llu "
2369 "COLLECTED: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2370 "DELTA: u=%lld s=%lld g=%lld "
2371 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2372 "FINALLY: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2383 , (long long)utime + (long long)cutime - (long long)global_utime
2384 , (long long)stime + (long long)cstime - (long long)global_stime
2385 , (long long)gtime + (long long)cgtime - (long long)global_gtime
2392 , (unsigned long long)(utime * utime_fix_ratio)
2393 , (unsigned long long)(stime * stime_fix_ratio)
2394 , (unsigned long long)(gtime * gtime_fix_ratio)
2395 , (unsigned long long)(cutime * cutime_fix_ratio)
2396 , (unsigned long long)(cstime * cstime_fix_ratio)
2397 , (unsigned long long)(cgtime * cgtime_fix_ratio)
2402 static void send_collected_data_to_netdata(struct target *root, const char *type, usec_t usec) {
2405 send_BEGIN(type, "cpu", usec);
2406 for (w = root; w ; w = w->next) {
2407 if(unlikely(w->exposed))
2408 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));
2412 send_BEGIN(type, "cpu_user", usec);
2413 for (w = root; w ; w = w->next) {
2414 if(unlikely(w->exposed))
2415 send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2419 send_BEGIN(type, "cpu_system", usec);
2420 for (w = root; w ; w = w->next) {
2421 if(unlikely(w->exposed))
2422 send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2426 if(show_guest_time) {
2427 send_BEGIN(type, "cpu_guest", usec);
2428 for (w = root; w ; w = w->next) {
2429 if(unlikely(w->exposed))
2430 send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2435 send_BEGIN(type, "threads", usec);
2436 for (w = root; w ; w = w->next) {
2437 if(unlikely(w->exposed))
2438 send_SET(w->name, w->num_threads);
2442 send_BEGIN(type, "processes", usec);
2443 for (w = root; w ; w = w->next) {
2444 if(unlikely(w->exposed))
2445 send_SET(w->name, w->processes);
2449 send_BEGIN(type, "mem", usec);
2450 for (w = root; w ; w = w->next) {
2451 if(unlikely(w->exposed))
2452 send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2456 send_BEGIN(type, "vmem", usec);
2457 for (w = root; w ; w = w->next) {
2458 if(unlikely(w->exposed))
2459 send_SET(w->name, w->statm_size);
2463 send_BEGIN(type, "minor_faults", usec);
2464 for (w = root; w ; w = w->next) {
2465 if(unlikely(w->exposed))
2466 send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2470 send_BEGIN(type, "major_faults", usec);
2471 for (w = root; w ; w = w->next) {
2472 if(unlikely(w->exposed))
2473 send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2477 send_BEGIN(type, "lreads", usec);
2478 for (w = root; w ; w = w->next) {
2479 if(unlikely(w->exposed))
2480 send_SET(w->name, w->io_logical_bytes_read);
2484 send_BEGIN(type, "lwrites", usec);
2485 for (w = root; w ; w = w->next) {
2486 if(unlikely(w->exposed))
2487 send_SET(w->name, w->io_logical_bytes_written);
2491 send_BEGIN(type, "preads", usec);
2492 for (w = root; w ; w = w->next) {
2493 if(unlikely(w->exposed))
2494 send_SET(w->name, w->io_storage_bytes_read);
2498 send_BEGIN(type, "pwrites", usec);
2499 for (w = root; w ; w = w->next) {
2500 if(unlikely(w->exposed))
2501 send_SET(w->name, w->io_storage_bytes_written);
2505 if(enable_file_charts) {
2506 send_BEGIN(type, "files", usec);
2507 for (w = root; w; w = w->next) {
2508 if (unlikely(w->exposed))
2509 send_SET(w->name, w->openfiles);
2513 send_BEGIN(type, "sockets", usec);
2514 for (w = root; w; w = w->next) {
2515 if (unlikely(w->exposed))
2516 send_SET(w->name, w->opensockets);
2520 send_BEGIN(type, "pipes", usec);
2521 for (w = root; w; w = w->next) {
2522 if (unlikely(w->exposed))
2523 send_SET(w->name, w->openpipes);
2530 // ----------------------------------------------------------------------------
2531 // generate the charts
2533 static void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2536 int newly_added = 0;
2538 for(w = root ; w ; w = w->next) {
2539 if (w->target) continue;
2541 if (!w->exposed && w->processes) {
2544 if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2548 // nothing more to show
2549 if(!newly_added && show_guest_time == show_guest_time_old) return;
2551 // we have something new to show
2552 // update the charts
2553 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);
2554 for (w = root; w ; w = w->next) {
2555 if(unlikely(w->exposed))
2556 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2559 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);
2560 for (w = root; w ; w = w->next) {
2561 if(unlikely(w->exposed))
2562 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2565 buffer_sprintf(output, "CHART %s.vmem '' '%s Virtual Memory Size' 'MB' mem %s.vmem stacked 20004 %d\n", type, title, type, update_every);
2566 for (w = root; w ; w = w->next) {
2567 if(unlikely(w->exposed))
2568 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2571 buffer_sprintf(output, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2572 for (w = root; w ; w = w->next) {
2573 if(unlikely(w->exposed))
2574 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2577 buffer_sprintf(output, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2578 for (w = root; w ; w = w->next) {
2579 if(unlikely(w->exposed))
2580 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2583 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);
2584 for (w = root; w ; w = w->next) {
2585 if(unlikely(w->exposed))
2586 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2589 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);
2590 for (w = root; w ; w = w->next) {
2591 if(unlikely(w->exposed))
2592 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2595 if(show_guest_time) {
2596 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);
2597 for (w = root; w; w = w->next) {
2598 if(unlikely(w->exposed))
2599 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2603 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);
2604 for (w = root; w ; w = w->next) {
2605 if(unlikely(w->exposed))
2606 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2609 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);
2610 for (w = root; w ; w = w->next) {
2611 if(unlikely(w->exposed))
2612 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2615 buffer_sprintf(output, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2616 for (w = root; w ; w = w->next) {
2617 if(unlikely(w->exposed))
2618 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2621 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);
2622 for (w = root; w ; w = w->next) {
2623 if(unlikely(w->exposed))
2624 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2627 buffer_sprintf(output, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2628 for (w = root; w ; w = w->next) {
2629 if(unlikely(w->exposed))
2630 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2633 buffer_sprintf(output, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2634 for (w = root; w ; w = w->next) {
2635 if(unlikely(w->exposed))
2636 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2639 if(enable_file_charts) {
2640 buffer_sprintf(output, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type,
2641 title, type, update_every);
2642 for (w = root; w; w = w->next) {
2643 if (unlikely(w->exposed))
2644 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2647 buffer_sprintf(output, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n",
2648 type, title, type, update_every);
2649 for (w = root; w; w = w->next) {
2650 if (unlikely(w->exposed))
2651 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2654 buffer_sprintf(output, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type,
2655 title, type, update_every);
2656 for (w = root; w; w = w->next) {
2657 if (unlikely(w->exposed))
2658 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2664 // ----------------------------------------------------------------------------
2665 // parse command line arguments
2667 static void parse_args(int argc, char **argv)
2672 for(i = 1; i < argc; i++) {
2674 int n = (int)str2l(argv[i]);
2681 if(strcmp("version", argv[i]) == 0 || strcmp("-v", argv[i]) == 0) {
2682 printf("apps.plugin %s\n", VERSION);
2686 if(strcmp("debug", argv[i]) == 0) {
2688 // debug_flags = 0xffffffff;
2692 if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
2693 include_exited_childs = 0;
2697 if(strcmp("with-childs", argv[i]) == 0) {
2698 include_exited_childs = 1;
2702 if(strcmp("with-guest", argv[i]) == 0) {
2703 enable_guest_charts = 1;
2707 if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
2708 enable_guest_charts = 0;
2712 if(strcmp("with-files", argv[i]) == 0) {
2713 enable_file_charts = 1;
2717 if(strcmp("no-files", argv[i]) == 0 || strcmp("without-files", argv[i]) == 0) {
2718 enable_file_charts = 0;
2722 if(strcmp("no-users", argv[i]) == 0 || strcmp("without-users", argv[i]) == 0) {
2723 enable_users_charts = 0;
2727 if(strcmp("no-groups", argv[i]) == 0 || strcmp("without-groups", argv[i]) == 0) {
2728 enable_groups_charts = 0;
2732 if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
2735 " netdata apps.plugin %s\n"
2736 " Copyright (C) 2016-2017 Costa Tsaousis <costa@tsaousis.gr>\n"
2737 " Released under GNU Public License v3 or later.\n"
2738 " All rights reserved.\n"
2740 " This program is a data collector plugin for netdata.\n"
2742 " Valid command line options:\n"
2744 " SECONDS set the data collection frequency\n"
2746 " debug enable debugging (lot of output)\n"
2749 " without-childs enable / disable aggregating exited\n"
2750 " children resources into parents\n"
2751 " (default is enabled)\n"
2754 " without-guest enable / disable reporting guest charts\n"
2755 " (default is disabled)\n"
2758 " without-files enable / disable reporting files, sockets, pipes\n"
2759 " (default is enabled)\n"
2761 " NAME read apps_NAME.conf instead of\n"
2762 " apps_groups.conf\n"
2763 " (default NAME=groups)\n"
2765 " version print program version and exit\n"
2777 error("Cannot understand option %s", argv[i]);
2781 if(freq > 0) update_every = freq;
2782 if(!name) name = "groups";
2784 if(read_apps_groups_conf(name)) {
2785 error("Cannot read process groups '%s/apps_%s.conf'. There are no internal defaults. Failing.", config_dir, name);
2790 int main(int argc, char **argv)
2792 // debug_flags = D_PROCFILE;
2794 // set the name for logging
2795 program_name = "apps.plugin";
2797 info("started on pid %d", getpid());
2799 // disable syslog for apps.plugin
2800 error_log_syslog = 0;
2802 // set errors flood protection to 100 logs per hour
2803 error_log_errors_per_period = 100;
2804 error_log_throttle_period = 3600;
2806 global_host_prefix = getenv("NETDATA_HOST_PREFIX");
2807 if(global_host_prefix == NULL) {
2808 // info("NETDATA_HOST_PREFIX is not passed from netdata");
2809 global_host_prefix = "";
2811 // else info("Found NETDATA_HOST_PREFIX='%s'", global_host_prefix);
2813 config_dir = getenv("NETDATA_CONFIG_DIR");
2814 if(config_dir == NULL) {
2815 // info("NETDATA_CONFIG_DIR is not passed from netdata");
2816 config_dir = CONFIG_DIR;
2818 // else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2820 #ifdef NETDATA_INTERNAL_CHECKS
2821 if(debug_flags != 0) {
2822 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2823 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2824 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2825 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2827 #endif /* NETDATA_INTERNAL_CHECKS */
2829 procfile_adaptive_initial_allocation = 1;
2831 time_t started_t = now_realtime_sec();
2833 get_system_pid_max();
2836 parse_args(argc, argv);
2838 all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
2839 all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
2841 output = buffer_create(1024);
2842 buffer_sprintf(output,
2843 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2844 "DIMENSION user '' incremental 1 1000\n"
2845 "DIMENSION system '' incremental 1 1000\n"
2846 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2847 "DIMENSION files '' incremental 1 1\n"
2848 "DIMENSION pids '' absolute 1 1\n"
2849 "DIMENSION fds '' absolute 1 1\n"
2850 "DIMENSION targets '' absolute 1 1\n"
2851 "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
2852 "DIMENSION utime '' absolute 1 %2$llu\n"
2853 "DIMENSION stime '' absolute 1 %2$llu\n"
2854 "DIMENSION gtime '' absolute 1 %2$llu\n"
2855 "DIMENSION minflt '' absolute 1 %2$llu\n"
2856 "DIMENSION majflt '' absolute 1 %2$llu\n"
2861 if(include_exited_childs)
2862 buffer_sprintf(output,
2863 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
2864 "DIMENSION cutime '' absolute 1 %2$llu\n"
2865 "DIMENSION cstime '' absolute 1 %2$llu\n"
2866 "DIMENSION cgtime '' absolute 1 %2$llu\n"
2867 "DIMENSION cminflt '' absolute 1 %2$llu\n"
2868 "DIMENSION cmajflt '' absolute 1 %2$llu\n"
2873 usec_t step = update_every * USEC_PER_SEC;
2874 global_iterations_counter = 1;
2875 for(;1; global_iterations_counter++) {
2876 usec_t now = now_realtime_usec();
2877 usec_t next = now - (now % step) + step;
2879 #ifdef NETDATA_PROFILING
2880 #warning "compiling for profiling"
2881 static int profiling_count=0;
2883 if(unlikely(profiling_count > 1000)) exit(0);
2886 sleep_usec(next - now);
2887 now = now_realtime_usec();
2891 if(!collect_data_for_all_processes_from_proc()) {
2892 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2893 printf("DISABLE\n");
2897 calculate_netdata_statistics();
2898 normalize_data(apps_groups_root_target);
2900 usec_t dt = send_resource_usage_to_netdata();
2902 // this is smart enough to show only newly added apps, when needed
2903 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2905 if(likely(enable_users_charts))
2906 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2908 if(likely(enable_groups_charts))
2909 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2911 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2913 if(likely(enable_users_charts))
2914 send_collected_data_to_netdata(users_root_target, "users", dt);
2916 if(likely(enable_groups_charts))
2917 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2919 show_guest_time_old = show_guest_time;
2921 if(write(STDOUT_FILENO, buffer_tostring(output), buffer_strlen(output)) == -1)
2922 fatal("Cannot send chart values to netdata.");
2925 buffer_flush(output);
2928 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2930 time_t current_t = now_realtime_sec();
2932 // restart check (14400 seconds)
2933 if(current_t - started_t > 14400) exit(0);