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
17 pid_t pid_max = 32768;
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 *host_prefix = "";
26 char *config_dir = CONFIG_DIR;
28 pid_t *all_pids_sortlist = NULL;
30 // will be automatically set to 1, if guest values are collected
31 int show_guest_time = 0;
32 int show_guest_time_old = 0;
34 int enable_guest_charts = 0;
36 // ----------------------------------------------------------------------------
38 void netdata_cleanup_and_exit(int ret) {
43 // ----------------------------------------------------------------------------
45 // to retrieve settings of the system
47 long get_system_cpus(void) {
52 char filename[FILENAME_MAX + 1];
53 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
55 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
58 ff = procfile_readall(ff);
65 for(i = 0; i < procfile_lines(ff); i++) {
66 if(!procfile_linewords(ff, i)) continue;
68 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
71 if(processors < 1) processors = 1;
77 pid_t get_system_pid_max(void) {
81 char filename[FILENAME_MAX + 1];
82 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
83 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
86 ff = procfile_readall(ff);
92 mpid = (pid_t)atoi(procfile_lineword(ff, 0, 0));
93 if(!mpid) mpid = 32768;
99 // ----------------------------------------------------------------------------
101 // target is the structure that process data are aggregated
104 char compare[MAX_COMPARE_NAME + 1];
105 uint32_t comparehash;
108 char id[MAX_NAME + 1];
111 char name[MAX_NAME + 1];
116 unsigned long long minflt;
117 unsigned long long cminflt;
118 unsigned long long majflt;
119 unsigned long long cmajflt;
120 unsigned long long utime;
121 unsigned long long stime;
122 unsigned long long gtime;
123 unsigned long long cutime;
124 unsigned long long cstime;
125 unsigned long long cgtime;
126 unsigned long long num_threads;
127 unsigned long long rss;
129 unsigned long long statm_size;
130 unsigned long long statm_resident;
131 unsigned long long statm_share;
132 unsigned long long statm_text;
133 unsigned long long statm_lib;
134 unsigned long long statm_data;
135 unsigned long long statm_dirty;
137 unsigned long long io_logical_bytes_read;
138 unsigned long long io_logical_bytes_written;
139 unsigned long long io_read_calls;
140 unsigned long long io_write_calls;
141 unsigned long long io_storage_bytes_read;
142 unsigned long long io_storage_bytes_written;
143 unsigned long long io_cancelled_write_bytes;
146 unsigned long long openfiles;
147 unsigned long long openpipes;
148 unsigned long long opensockets;
149 unsigned long long openinotifies;
150 unsigned long long openeventfds;
151 unsigned long long opentimerfds;
152 unsigned long long opensignalfds;
153 unsigned long long openeventpolls;
154 unsigned long long openother;
156 unsigned long processes; // how many processes have been merged to this
157 int exposed; // if set, we have sent this to netdata
158 int hidden; // if set, we set the hidden flag on the dimension
161 int starts_with; // if set, the compare string matches only the
162 // beginning of the command
164 struct target *target; // the one that will be reported to netdata
169 // ----------------------------------------------------------------------------
171 // aggregate all processes in groups, to have a limited number of dimensions
173 struct target *apps_groups_root_target = NULL;
174 struct target *apps_groups_default_target = NULL;
175 long apps_groups_targets = 0;
177 struct target *users_root_target = NULL;
178 struct target *groups_root_target = NULL;
180 struct target *get_users_target(uid_t uid)
183 for(w = users_root_target ; w ; w = w->next)
184 if(w->uid == uid) return w;
186 w = callocz(sizeof(struct target), 1);
187 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
188 w->comparehash = simple_hash(w->compare);
189 w->comparelen = strlen(w->compare);
191 snprintfz(w->id, MAX_NAME, "%u", uid);
192 w->idhash = simple_hash(w->id);
194 struct passwd *pw = getpwuid(uid);
196 snprintfz(w->name, MAX_NAME, "%u", uid);
198 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
200 netdata_fix_chart_name(w->name);
204 w->next = users_root_target;
205 users_root_target = w;
208 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
213 struct target *get_groups_target(gid_t gid)
216 for(w = groups_root_target ; w ; w = w->next)
217 if(w->gid == gid) return w;
219 w = callocz(sizeof(struct target), 1);
220 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
221 w->comparehash = simple_hash(w->compare);
222 w->comparelen = strlen(w->compare);
224 snprintfz(w->id, MAX_NAME, "%u", gid);
225 w->idhash = simple_hash(w->id);
227 struct group *gr = getgrgid(gid);
229 snprintfz(w->name, MAX_NAME, "%u", gid);
231 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
233 netdata_fix_chart_name(w->name);
237 w->next = groups_root_target;
238 groups_root_target = w;
241 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
246 // find or create a new target
247 // there are targets that are just aggregated to other target (the second argument)
248 struct target *get_apps_groups_target(const char *id, struct target *target) {
249 int tdebug = 0, thidden = 0, ends_with = 0;
250 const char *nid = id;
252 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
253 if(nid[0] == '-') thidden = 1;
254 if(nid[0] == '+') tdebug = 1;
255 if(nid[0] == '*') ends_with = 1;
258 uint32_t hash = simple_hash(id);
260 struct target *w, *last = apps_groups_root_target;
261 for(w = apps_groups_root_target ; w ; w = w->next) {
262 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
268 w = callocz(sizeof(struct target), 1);
269 strncpyz(w->id, nid, MAX_NAME);
270 w->idhash = simple_hash(w->id);
272 strncpyz(w->name, nid, MAX_NAME);
274 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
275 size_t len = strlen(w->compare);
276 if(w->compare[len - 1] == '*') {
277 w->compare[len - 1] = '\0';
280 w->ends_with = ends_with;
282 if(w->starts_with && w->ends_with)
283 proc_pid_cmdline_is_needed = 1;
285 w->comparehash = simple_hash(w->compare);
286 w->comparelen = strlen(w->compare);
292 // append it, to maintain the order in apps_groups.conf
293 if(last) last->next = w;
294 else apps_groups_root_target = w;
297 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
299 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
300 , w->target?w->target->id:w->id
301 , (w->hidden)?"hidden":"-"
302 , (w->debug)?"debug":"-"
308 // read the apps_groups.conf file
309 int read_apps_groups_conf(const char *name)
311 char filename[FILENAME_MAX + 1];
313 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
316 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
318 // ----------------------------------------
320 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
323 procfile_set_quotes(ff, "'\"");
325 ff = procfile_readall(ff);
331 unsigned long line, lines = procfile_lines(ff);
333 for(line = 0; line < lines ;line++) {
334 unsigned long word, words = procfile_linewords(ff, line);
335 struct target *w = NULL;
337 char *t = procfile_lineword(ff, line, 0);
338 if(!t || !*t) continue;
340 for(word = 0; word < words ;word++) {
341 char *s = procfile_lineword(ff, line, word);
342 if(!s || !*s) continue;
347 struct target *n = get_apps_groups_target(s, w);
349 error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
357 int tdebug = 0, thidden = 0;
359 while(t[0] == '-' || t[0] == '+') {
360 if(t[0] == '-') thidden = 1;
361 if(t[0] == '+') tdebug = 1;
365 strncpyz(w->name, t, MAX_NAME);
370 fprintf(stderr, "apps.plugin: AGGREGATION TARGET NAME '%s' on ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
373 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
374 , w->target?w->target->id:w->id
375 , (w->hidden)?"hidden":"-"
376 , (w->debug)?"debug":"-"
383 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
384 if(!apps_groups_default_target)
385 error("Cannot create default target");
387 strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
393 // ----------------------------------------------------------------------------
394 // data to store for each pid
399 char comm[MAX_COMPARE_NAME + 1];
400 char cmdline[MAX_CMDLINE + 1];
410 // these are raw values collected
411 unsigned long long minflt_raw;
412 unsigned long long cminflt_raw;
413 unsigned long long majflt_raw;
414 unsigned long long cmajflt_raw;
415 unsigned long long utime_raw;
416 unsigned long long stime_raw;
417 unsigned long long gtime_raw; // guest_time
418 unsigned long long cutime_raw;
419 unsigned long long cstime_raw;
420 unsigned long long cgtime_raw; // cguest_time
423 unsigned long long minflt;
424 unsigned long long cminflt;
425 unsigned long long majflt;
426 unsigned long long cmajflt;
427 unsigned long long utime;
428 unsigned long long stime;
429 unsigned long long gtime;
430 unsigned long long cutime;
431 unsigned long long cstime;
432 unsigned long long cgtime;
437 // int64_t itrealvalue;
438 // unsigned long long starttime;
439 // unsigned long long vsize;
440 unsigned long long rss;
441 // unsigned long long rsslim;
442 // unsigned long long starcode;
443 // unsigned long long endcode;
444 // unsigned long long startstack;
445 // unsigned long long kstkesp;
446 // unsigned long long kstkeip;
449 // uint64_t sigignore;
450 // uint64_t sigcatch;
454 // int32_t exit_signal;
455 // int32_t processor;
456 // uint32_t rt_priority;
458 // unsigned long long delayacct_blkio_ticks;
463 unsigned long long statm_size;
464 unsigned long long statm_resident;
465 unsigned long long statm_share;
466 unsigned long long statm_text;
467 unsigned long long statm_lib;
468 unsigned long long statm_data;
469 unsigned long long statm_dirty;
471 unsigned long long io_logical_bytes_read_raw;
472 unsigned long long io_logical_bytes_written_raw;
473 unsigned long long io_read_calls_raw;
474 unsigned long long io_write_calls_raw;
475 unsigned long long io_storage_bytes_read_raw;
476 unsigned long long io_storage_bytes_written_raw;
477 unsigned long long io_cancelled_write_bytes_raw;
479 unsigned long long io_logical_bytes_read;
480 unsigned long long io_logical_bytes_written;
481 unsigned long long io_read_calls;
482 unsigned long long io_write_calls;
483 unsigned long long io_storage_bytes_read;
484 unsigned long long io_storage_bytes_written;
485 unsigned long long io_cancelled_write_bytes;
487 int *fds; // array of fds it uses
488 int fds_size; // the size of the fds array
490 int children_count; // number of processes directly referencing this
491 int keep; // 1 when we need to keep this process in memory even after it exited
492 int keeploops; // increases by 1 every time keep is 1 and updated 0
493 int updated; // 1 when the process is currently running
494 int merged; // 1 when it has been merged to its parent
495 int new_entry; // 1 when this is a new process, just saw for the first time
496 int read; // 1 when we have already read this process for this iteration
497 int sortlist; // higher numbers = top on the process tree
498 // each process gets a unique number
500 struct target *target; // app_groups.conf targets
501 struct target *user_target; // uid based targets
502 struct target *group_target; // gid based targets
504 unsigned long long stat_collected_usec;
505 unsigned long long last_stat_collected_usec;
507 unsigned long long io_collected_usec;
508 unsigned long long last_io_collected_usec;
511 char *statm_filename;
513 char *cmdline_filename;
515 struct pid_stat *parent;
516 struct pid_stat *prev;
517 struct pid_stat *next;
518 } *root_of_pids = NULL, **all_pids;
520 long all_pids_count = 0;
522 struct pid_stat *get_pid_entry(pid_t pid) {
524 all_pids[pid]->new_entry = 0;
525 return all_pids[pid];
528 all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
529 all_pids[pid]->fds = callocz(sizeof(int), 100);
530 all_pids[pid]->fds_size = 100;
532 if(root_of_pids) root_of_pids->prev = all_pids[pid];
533 all_pids[pid]->next = root_of_pids;
534 root_of_pids = all_pids[pid];
536 all_pids[pid]->pid = pid;
537 all_pids[pid]->new_entry = 1;
541 return all_pids[pid];
544 void del_pid_entry(pid_t pid) {
546 error("attempted to free pid %d that is not allocated.", pid);
551 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
553 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
554 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
555 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
557 if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
558 if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
559 if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
560 if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
561 if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
562 freez(all_pids[pid]);
564 all_pids[pid] = NULL;
569 // ----------------------------------------------------------------------------
570 // update pids from proc
572 int read_proc_pid_cmdline(struct pid_stat *p) {
574 if(unlikely(!p->cmdline_filename)) {
575 char filename[FILENAME_MAX + 1];
576 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
577 p->cmdline_filename = strdupz(filename);
580 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
581 if(unlikely(fd == -1)) goto cleanup;
583 ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
586 if(unlikely(bytes <= 0)) goto cleanup;
588 p->cmdline[bytes] = '\0';
589 for(i = 0; i < bytes ; i++)
590 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
593 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
598 // copy the command to the command line
599 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
603 int read_proc_pid_ownership(struct pid_stat *p) {
604 if(unlikely(!p->stat_filename)) {
605 error("pid %d does not have a stat_filename", p->pid);
609 // ----------------------------------------
613 if(stat(p->stat_filename, &st) != 0) {
614 error("Cannot stat file '%s'", p->stat_filename);
624 int read_proc_pid_stat(struct pid_stat *p) {
625 static procfile *ff = NULL;
627 if(unlikely(!p->stat_filename)) {
628 char filename[FILENAME_MAX + 1];
629 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
630 p->stat_filename = strdupz(filename);
633 int set_quotes = (!ff)?1:0;
635 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
636 if(unlikely(!ff)) goto cleanup;
638 // if(set_quotes) procfile_set_quotes(ff, "()");
639 if(set_quotes) procfile_set_open_close(ff, "(", ")");
641 ff = procfile_readall(ff);
642 if(unlikely(!ff)) goto cleanup;
644 p->last_stat_collected_usec = p->stat_collected_usec;
645 p->stat_collected_usec = timems();
648 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
650 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
652 // p->state = *(procfile_lineword(ff, 0, 2));
653 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3));
654 // p->pgrp = atol(procfile_lineword(ff, 0, 4));
655 // p->session = atol(procfile_lineword(ff, 0, 5));
656 // p->tty_nr = atol(procfile_lineword(ff, 0, 6));
657 // p->tpgid = atol(procfile_lineword(ff, 0, 7));
658 // p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
660 unsigned long long last;
662 last = p->minflt_raw;
663 p->minflt_raw = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
664 p->minflt = (p->minflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
666 last = p->cminflt_raw;
667 p->cminflt_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
668 p->cminflt = (p->cminflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
670 last = p->majflt_raw;
671 p->majflt_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
672 p->majflt = (p->majflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
674 last = p->cmajflt_raw;
675 p->cmajflt_raw = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
676 p->cmajflt = (p->cmajflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
679 p->utime_raw = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
680 p->utime = (p->utime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
683 p->stime_raw = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
684 p->stime = (p->stime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
686 last = p->cutime_raw;
687 p->cutime_raw = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
688 p->cutime = (p->cutime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
690 last = p->cstime_raw;
691 p->cstime_raw = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
692 p->cstime = (p->cstime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
694 // p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
695 // p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
696 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19));
697 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
698 // p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
699 // p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
700 p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
701 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
702 // p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
703 // p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
704 // p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
705 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
706 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
707 // p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
708 // p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
709 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
710 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
711 // p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
712 // p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
713 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
714 // p->exit_signal = atol(procfile_lineword(ff, 0, 37));
715 // p->processor = atol(procfile_lineword(ff, 0, 38));
716 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
717 // p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
718 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
720 if(enable_guest_charts) {
722 p->gtime_raw = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
723 p->gtime = (p->gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
725 last = p->cgtime_raw;
726 p->cgtime_raw = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
727 p->cgtime = (p->cgtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
729 if (show_guest_time || p->gtime || p->cgtime) {
730 p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
731 p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
736 if(unlikely(debug || (p->target && p->target->debug)))
737 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", 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);
739 if(unlikely(global_iterations_counter == 1)) {
770 int read_proc_pid_statm(struct pid_stat *p) {
771 static procfile *ff = NULL;
773 if(unlikely(!p->statm_filename)) {
774 char filename[FILENAME_MAX + 1];
775 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
776 p->statm_filename = strdupz(filename);
779 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
780 if(unlikely(!ff)) goto cleanup;
782 ff = procfile_readall(ff);
783 if(unlikely(!ff)) goto cleanup;
787 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
788 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
789 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
790 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
791 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
792 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
793 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
799 p->statm_resident = 0;
808 int read_proc_pid_io(struct pid_stat *p) {
809 static procfile *ff = NULL;
811 if(unlikely(!p->io_filename)) {
812 char filename[FILENAME_MAX + 1];
813 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
814 p->io_filename = strdupz(filename);
818 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
819 if(unlikely(!ff)) goto cleanup;
821 ff = procfile_readall(ff);
822 if(unlikely(!ff)) goto cleanup;
826 p->last_io_collected_usec = p->io_collected_usec;
827 p->io_collected_usec = timems();
829 unsigned long long last;
831 last = p->io_logical_bytes_read_raw;
832 p->io_logical_bytes_read_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
833 p->io_logical_bytes_read = (p->io_logical_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
835 last = p->io_logical_bytes_written_raw;
836 p->io_logical_bytes_written_raw = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
837 p->io_logical_bytes_written = (p->io_logical_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
839 last = p->io_read_calls_raw;
840 p->io_read_calls_raw = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
841 p->io_read_calls = (p->io_read_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
843 last = p->io_write_calls_raw;
844 p->io_write_calls_raw = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
845 p->io_write_calls = (p->io_write_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
847 last = p->io_storage_bytes_read_raw;
848 p->io_storage_bytes_read_raw = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
849 p->io_storage_bytes_read = (p->io_storage_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
851 last = p->io_storage_bytes_written_raw;
852 p->io_storage_bytes_written_raw = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
853 p->io_storage_bytes_written = (p->io_storage_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
855 last = p->io_cancelled_write_bytes_raw;
856 p->io_cancelled_write_bytes_raw = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
857 p->io_cancelled_write_bytes = (p->io_cancelled_write_bytes_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
859 if(unlikely(global_iterations_counter == 1)) {
860 p->io_logical_bytes_read = 0;
861 p->io_logical_bytes_written = 0;
862 p->io_read_calls = 0;
863 p->io_write_calls = 0;
864 p->io_storage_bytes_read = 0;
865 p->io_storage_bytes_written = 0;
866 p->io_cancelled_write_bytes = 0;
872 p->io_logical_bytes_read = 0;
873 p->io_logical_bytes_written = 0;
874 p->io_read_calls = 0;
875 p->io_write_calls = 0;
876 p->io_storage_bytes_read = 0;
877 p->io_storage_bytes_written = 0;
878 p->io_cancelled_write_bytes = 0;
882 unsigned long long global_utime = 0;
883 unsigned long long global_stime = 0;
884 unsigned long long global_gtime = 0;
886 int read_proc_stat() {
887 static char filename[FILENAME_MAX + 1] = "";
888 static procfile *ff = NULL;
889 static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0, collected_usec = 0, last_collected_usec = 0;
892 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
893 ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
894 if(unlikely(!ff)) goto cleanup;
897 ff = procfile_readall(ff);
898 if(unlikely(!ff)) goto cleanup;
900 last_collected_usec = collected_usec;
901 collected_usec = timems();
905 unsigned long long last;
908 utime_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
909 global_utime = (utime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
911 // nice time, on user time
913 ntime_raw = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
914 global_utime += (ntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
917 stime_raw = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
918 global_stime = (stime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
921 gtime_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
922 global_gtime = (gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
924 if(enable_guest_charts) {
925 // guest nice time, on guest time
927 gntime_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
928 global_gtime += (gntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
930 // remove guest time from user time
931 global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
934 if(unlikely(global_iterations_counter == 1)) {
950 // ----------------------------------------------------------------------------
952 // this is used to keep a global list of all open files of the system
953 // it is needed in order to calculate the unique files processes have open
955 #define FILE_DESCRIPTORS_INCREASE_STEP 100
957 struct file_descriptor {
959 #ifdef NETDATA_INTERNAL_CHECKS
961 #endif /* NETDATA_INTERNAL_CHECKS */
969 int all_files_len = 0;
970 int all_files_size = 0;
972 int file_descriptor_compare(void* a, void* b) {
973 #ifdef NETDATA_INTERNAL_CHECKS
974 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
975 error("Corrupted index data detected. Please report this.");
976 #endif /* NETDATA_INTERNAL_CHECKS */
978 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
981 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
985 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
988 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
990 avl_tree all_files_index = {
992 file_descriptor_compare
995 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
996 struct file_descriptor tmp;
997 tmp.hash = (hash)?hash:simple_hash(name);
1001 #ifdef NETDATA_INTERNAL_CHECKS
1002 tmp.magic = 0x0BADCAFE;
1003 #endif /* NETDATA_INTERNAL_CHECKS */
1005 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
1008 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1009 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1011 #define FILETYPE_OTHER 0
1012 #define FILETYPE_FILE 1
1013 #define FILETYPE_PIPE 2
1014 #define FILETYPE_SOCKET 3
1015 #define FILETYPE_INOTIFY 4
1016 #define FILETYPE_EVENTFD 5
1017 #define FILETYPE_EVENTPOLL 6
1018 #define FILETYPE_TIMERFD 7
1019 #define FILETYPE_SIGNALFD 8
1021 void file_descriptor_not_used(int id)
1023 if(id > 0 && id < all_files_size) {
1025 #ifdef NETDATA_INTERNAL_CHECKS
1026 if(all_files[id].magic != 0x0BADCAFE) {
1027 error("Ignoring request to remove empty file id %d.", id);
1030 #endif /* NETDATA_INTERNAL_CHECKS */
1033 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1035 if(all_files[id].count > 0) {
1036 all_files[id].count--;
1038 if(!all_files[id].count) {
1040 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1042 file_descriptor_remove(&all_files[id]);
1043 #ifdef NETDATA_INTERNAL_CHECKS
1044 all_files[id].magic = 0x00000000;
1045 #endif /* NETDATA_INTERNAL_CHECKS */
1050 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1052 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1055 int file_descriptor_find_or_add(const char *name)
1057 static int last_pos = 0;
1058 uint32_t hash = simple_hash(name);
1061 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1063 struct file_descriptor *fd = file_descriptor_find(name, hash);
1067 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1074 // check we have enough memory to add it
1075 if(!all_files || all_files_len == all_files_size) {
1076 void *old = all_files;
1079 // there is no empty slot
1081 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1083 all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1085 // if the address changed, we have to rebuild the index
1086 // since all pointers are now invalid
1087 if(old && old != (void *)all_files) {
1089 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1091 all_files_index.root = NULL;
1092 for(i = 0; i < all_files_size; i++) {
1093 if(!all_files[i].count) continue;
1094 file_descriptor_add(&all_files[i]);
1098 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1101 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1102 all_files[i].count = 0;
1103 all_files[i].name = NULL;
1104 #ifdef NETDATA_INTERNAL_CHECKS
1105 all_files[i].magic = 0x00000000;
1106 #endif /* NETDATA_INTERNAL_CHECKS */
1107 all_files[i].pos = i;
1110 if(!all_files_size) all_files_len = 1;
1111 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1115 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1117 // search for an empty slot
1119 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1120 if(c >= all_files_size) c = 0;
1121 if(c == 0) continue;
1123 if(!all_files[c].count) {
1125 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1127 #ifdef NETDATA_INTERNAL_CHECKS
1128 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1129 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1130 #endif /* NETDATA_INTERNAL_CHECKS */
1133 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);
1135 if(all_files[c].name) freez((void *)all_files[c].name);
1136 all_files[c].name = NULL;
1141 if(i == all_files_size) {
1142 fatal("We should find an empty slot, but there isn't any");
1147 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1151 // else we have an empty slot in 'c'
1154 if(name[0] == '/') type = FILETYPE_FILE;
1155 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1156 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1157 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1158 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1159 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1160 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1161 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1162 else if(strncmp(name, "anon_inode:", 11) == 0) {
1164 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1166 type = FILETYPE_OTHER;
1170 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1172 type = FILETYPE_OTHER;
1175 all_files[c].name = strdupz(name);
1176 all_files[c].hash = hash;
1177 all_files[c].type = type;
1178 all_files[c].pos = c;
1179 all_files[c].count = 1;
1180 #ifdef NETDATA_INTERNAL_CHECKS
1181 all_files[c].magic = 0x0BADCAFE;
1182 #endif /* NETDATA_INTERNAL_CHECKS */
1183 file_descriptor_add(&all_files[c]);
1186 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1191 int read_pid_file_descriptors(struct pid_stat *p) {
1192 char dirname[FILENAME_MAX+1];
1194 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
1195 DIR *fds = opendir(dirname);
1199 char fdname[FILENAME_MAX + 1];
1200 char linkname[FILENAME_MAX + 1];
1202 // make the array negative
1203 for(c = 0 ; c < p->fds_size ; c++)
1204 p->fds[c] = -p->fds[c];
1206 while((de = readdir(fds))) {
1207 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1210 // check if the fds array is small
1211 int fdid = atoi(de->d_name);
1212 if(fdid < 0) continue;
1213 if(fdid >= p->fds_size) {
1214 // it is small, extend it
1216 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1218 p->fds = reallocz(p->fds, (fdid + 100) * sizeof(int));
1220 fatal("Cannot re-allocate fds for %s", p->comm);
1224 // and initialize it
1225 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1226 p->fds_size = fdid + 100;
1229 if(p->fds[fdid] == 0) {
1230 // we don't know this fd, get it
1232 sprintf(fdname, "%s/proc/%d/fd/%s", host_prefix, p->pid, de->d_name);
1233 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1235 if(debug || (p->target && p->target->debug)) {
1236 if(debug || (p->target && p->target->debug))
1237 error("Cannot read link %s", fdname);
1244 // if another process already has this, we will get
1246 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1249 // else make it positive again, we need it
1250 // of course, the actual file may have changed, but we don't care so much
1251 // FIXME: we could compare the inode as returned by readdir direct structure
1252 else p->fds[fdid] = -p->fds[fdid];
1256 // remove all the negative file descriptors
1257 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1258 file_descriptor_not_used(-p->fds[c]);
1267 // ----------------------------------------------------------------------------
1269 int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1270 char *prefix = "\\_ ";
1274 indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1278 char buffer[indent + 1];
1281 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1284 fprintf(stderr, " %s %s%s (%d %s %lld"
1289 , p->updated?"running":"exited"
1290 , (long long)p->stat_collected_usec - (long long)time
1293 if(p->utime) fprintf(stderr, " utime=%llu", p->utime);
1294 if(p->stime) fprintf(stderr, " stime=%llu", p->stime);
1295 if(p->gtime) fprintf(stderr, " gtime=%llu", p->gtime);
1296 if(p->cutime) fprintf(stderr, " cutime=%llu", p->cutime);
1297 if(p->cstime) fprintf(stderr, " cstime=%llu", p->cstime);
1298 if(p->cgtime) fprintf(stderr, " cgtime=%llu", p->cgtime);
1299 if(p->minflt) fprintf(stderr, " minflt=%llu", p->minflt);
1300 if(p->cminflt) fprintf(stderr, " cminflt=%llu", p->cminflt);
1301 if(p->majflt) fprintf(stderr, " majflt=%llu", p->majflt);
1302 if(p->cmajflt) fprintf(stderr, " cmajflt=%llu", p->cmajflt);
1303 fprintf(stderr, ")\n");
1308 void print_process_tree(struct pid_stat *p, char *msg) {
1310 fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1311 print_process_and_parents(p, p->stat_collected_usec);
1314 void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1316 struct pid_stat *p = NULL;
1318 for(p = root_of_pids; p ; p = p->next) {
1319 if(p == pe) continue;
1323 if(p->cminflt > lost) {
1324 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);
1330 if(p->cmajflt > lost) {
1331 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);
1337 if(p->cutime > lost) {
1338 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);
1344 if(p->cstime > lost) {
1345 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);
1351 if(p->cgtime > lost) {
1352 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);
1362 fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1366 fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1370 fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1374 fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1378 fprintf(stderr, " > cannot find any process to use the lost exited child gtime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1384 unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1385 unsigned long long absorbed = 0;
1387 if(*field > *pfield) {
1388 absorbed += *pfield;
1401 void process_exited_processes() {
1404 for(p = root_of_pids; p ; p = p->next) {
1405 if(p->updated || !p->stat_collected_usec)
1408 struct pid_stat *pp = p->parent;
1410 unsigned long long utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1411 unsigned long long stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1412 unsigned long long gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1413 unsigned long long minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1414 unsigned long long majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1416 if(utime + stime + gtime + minflt + majflt == 0)
1419 if(unlikely(debug)) {
1421 fprintf(stderr, "Absorb %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1424 , p->updated?"running":"exited"
1431 print_process_tree(p, "Searching parents");
1434 for(pp = p->parent; pp ; pp = pp->parent) {
1435 if(!pp->updated) continue;
1437 unsigned long long absorbed;
1438 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1439 if(unlikely(debug && absorbed))
1440 fprintf(stderr, " > process %s (%d %s) absorbed %llu utime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1442 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1443 if(unlikely(debug && absorbed))
1444 fprintf(stderr, " > process %s (%d %s) absorbed %llu stime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1446 absorbed = remove_exited_child_from_parent(>ime, &pp->cgtime);
1447 if(unlikely(debug && absorbed))
1448 fprintf(stderr, " > process %s (%d %s) absorbed %llu gtime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1450 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1451 if(unlikely(debug && absorbed))
1452 fprintf(stderr, " > process %s (%d %s) absorbed %llu minflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1454 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1455 if(unlikely(debug && absorbed))
1456 fprintf(stderr, " > process %s (%d %s) absorbed %llu majflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1459 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1460 if(unlikely(debug)) {
1461 if(utime) find_lost_child_debug(p, utime, 3);
1462 if(stime) find_lost_child_debug(p, stime, 4);
1463 if(gtime) find_lost_child_debug(p, gtime, 5);
1464 if(minflt) find_lost_child_debug(p, minflt, 1);
1465 if(majflt) find_lost_child_debug(p, majflt, 2);
1471 fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1474 , p->updated?"running":"exited"
1482 for(pp = p->parent; pp ; pp = pp->parent) {
1483 if(pp->updated) break;
1487 fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
1490 , pp->updated?"running":"exited"
1494 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1495 p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1496 p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1497 p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1498 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1499 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
1502 fprintf(stderr, "\n");
1504 else if(unlikely(debug)) {
1505 fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
1508 , p->updated?"running":"exited"
1514 void link_all_processes_to_their_parents(void) {
1515 struct pid_stat *p, *pp;
1517 // link all children to their parents
1518 // and update children count on parents
1519 for(p = root_of_pids; p ; p = p->next) {
1520 // for each process found
1525 if(unlikely(!p->ppid)) {
1530 pp = all_pids[p->ppid];
1533 pp->children_count++;
1535 if(unlikely(debug || (p->target && p->target->debug)))
1536 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);
1540 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1545 // ----------------------------------------------------------------------------
1547 // 1. read all files in /proc
1548 // 2. for each numeric directory:
1549 // i. read /proc/pid/stat
1550 // ii. read /proc/pid/statm
1551 // iii. read /proc/pid/io (requires root access)
1552 // iii. read the entries in directory /proc/pid/fd (requires root access)
1554 // a. find or create a struct file_descriptor
1555 // b. cleanup any old/unused file_descriptors
1557 // after all these, some pids may be linked to targets, while others may not
1559 // in case of errors, only 1 every 1000 errors is printed
1560 // to avoid filling up all disk space
1561 // if debug is enabled, all errors are printed
1563 static int compar_pid(const void *pid1, const void *pid2) {
1565 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1566 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1568 if(p1->sortlist > p2->sortlist)
1574 void collect_data_for_pid(pid_t pid) {
1575 if(unlikely(pid <= 0 || pid > pid_max)) {
1576 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1580 struct pid_stat *p = get_pid_entry(pid);
1581 if(unlikely(!p || p->read)) return;
1584 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1586 // --------------------------------------------------------------------
1589 if(unlikely(read_proc_pid_stat(p))) {
1590 if(errno != ENOENT || debug)
1591 error("Cannot process %s/proc/%d/stat (command '%s')", host_prefix, pid, p->comm);
1594 // there is no reason to proceed if we cannot get its status
1598 read_proc_pid_ownership(p);
1600 // check its parent pid
1601 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1602 error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
1606 // --------------------------------------------------------------------
1609 if(unlikely(read_proc_pid_io(p))) {
1610 if(errno != ENOENT || debug)
1611 error("Cannot process %s/proc/%d/io (command '%s')", host_prefix, pid, p->comm);
1616 // --------------------------------------------------------------------
1617 // /proc/<pid>/statm
1619 if(unlikely(read_proc_pid_statm(p))) {
1620 if(errno != ENOENT || debug)
1621 error("Cannot process %s/proc/%d/statm (command '%s')", host_prefix, pid, p->comm);
1624 // there is no reason to proceed if we cannot get its memory status
1628 // --------------------------------------------------------------------
1631 // check if it is target
1632 // we do this only once, the first time this pid is loaded
1633 if(unlikely(p->new_entry)) {
1634 // /proc/<pid>/cmdline
1635 if(likely(proc_pid_cmdline_is_needed)) {
1636 if(unlikely(read_proc_pid_cmdline(p))) {
1637 if(errno != ENOENT || debug)
1638 error("Cannot process %s/proc/%d/cmdline (command '%s')", host_prefix, pid, p->comm);
1645 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1647 uint32_t hash = simple_hash(p->comm);
1648 size_t pclen = strlen(p->comm);
1651 for(w = apps_groups_root_target; w ; w = w->next) {
1652 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1654 // find it - 4 cases:
1655 // 1. the target is not a pattern
1656 // 2. the target has the prefix
1657 // 3. the target has the suffix
1658 // 4. the target is something inside cmdline
1659 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1660 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1661 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1662 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1664 if(w->target) p->target = w->target;
1667 if(debug || (p->target && p->target->debug))
1668 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1675 // --------------------------------------------------------------------
1678 if(unlikely(read_pid_file_descriptors(p))) {
1679 if(errno != ENOENT || debug)
1680 error("Cannot process entries in %s/proc/%d/fd (command '%s')", host_prefix, pid, p->comm);
1685 // --------------------------------------------------------------------
1688 if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1689 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);
1691 // mark it as updated
1697 int collect_data_for_all_processes_from_proc(void) {
1698 struct pid_stat *p = NULL;
1700 if(all_pids_count) {
1701 // read parents before childs
1702 // this is needed to prevent a situation where
1703 // a child is found running, but until we read
1704 // its parent, it has exited and its parent
1705 // has accumulated its resources
1708 for(p = root_of_pids; p ; p = p->next) {
1713 p->children_count = 0;
1716 all_pids_sortlist[slc++] = p->pid;
1719 if(unlikely(slc != all_pids_count)) {
1720 error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1721 all_pids_count = slc;
1724 if(include_exited_childs) {
1725 qsort((void *)all_pids_sortlist, all_pids_count, sizeof(pid_t), compar_pid);
1726 for(slc = 0; slc < all_pids_count; slc++)
1727 collect_data_for_pid(all_pids_sortlist[slc]);
1731 char dirname[FILENAME_MAX + 1];
1733 snprintfz(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1734 DIR *dir = opendir(dirname);
1737 struct dirent *file = NULL;
1739 while((file = readdir(dir))) {
1740 char *endptr = file->d_name;
1741 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1743 // make sure we read a valid number
1744 if(unlikely(endptr == file->d_name || *endptr != '\0'))
1747 collect_data_for_pid(pid);
1751 // normally this is done
1752 // however we may have processes exited while we collected values
1753 // so let's find the exited ones
1754 // we do this by collecting the ownership of process
1755 // if we manage to get the ownership, the process still runs
1758 link_all_processes_to_their_parents();
1759 process_exited_processes();
1764 // ----------------------------------------------------------------------------
1765 // update statistics on the targets
1767 // 1. link all childs to their parents
1768 // 2. go from bottom to top, marking as merged all childs to their parents
1769 // this step links all parents without a target to the child target, if any
1770 // 3. link all top level processes (the ones not merged) to the default target
1771 // 4. go from top to bottom, linking all childs without a target, to their parent target
1772 // after this step, all processes have a target
1773 // [5. for each killed pid (updated = 0), remove its usage from its target]
1774 // 6. zero all apps_groups_targets
1775 // 7. concentrate all values on the apps_groups_targets
1776 // 8. remove all killed processes
1777 // 9. find the unique file count for each target
1778 // check: update_apps_groups_statistics()
1780 void cleanup_exited_pids(void) {
1782 struct pid_stat *p = NULL;
1784 for(p = root_of_pids; p ;) {
1785 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1786 // 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);
1788 if(unlikely(debug && (p->keep || p->keeploops)))
1789 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1791 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1792 file_descriptor_not_used(p->fds[c]);
1801 if(unlikely(p->keep)) p->keeploops++;
1808 void apply_apps_groups_targets_inheritance(void) {
1809 struct pid_stat *p = NULL;
1811 // children that do not have a target
1812 // inherit their target from their parent
1813 int found = 1, loops = 0;
1815 if(unlikely(debug)) loops++;
1817 for(p = root_of_pids; p ; p = p->next) {
1818 // if this process does not have a target
1819 // and it has a parent
1820 // and its parent has a target
1821 // then, set the parent's target to this process
1822 if(unlikely(!p->target && p->parent && p->parent->target)) {
1823 p->target = p->parent->target;
1826 if(debug || (p->target && p->target->debug))
1827 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);
1832 // find all the procs with 0 childs and merge them to their parents
1833 // repeat, until nothing more can be done.
1837 if(unlikely(debug)) loops++;
1840 for(p = root_of_pids; p ; p = p->next) {
1841 if(unlikely(!p->sortlist && !p->children_count))
1842 p->sortlist = sortlist++;
1844 // if this process does not have any children
1845 // and is not already merged
1847 // and its parent has children
1848 // and the target of this process and its parent is the same, or the parent does not have a target
1849 // and its parent is not init
1850 // then, mark them as merged.
1855 && p->parent->children_count
1856 && (p->target == p->parent->target || !p->parent->target)
1859 p->parent->children_count--;
1862 // the parent inherits the child's target, if it does not have a target itself
1863 if(unlikely(p->target && !p->parent->target)) {
1864 p->parent->target = p->target;
1866 if(debug || (p->target && p->target->debug))
1867 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);
1875 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
1878 // init goes always to default target
1880 all_pids[1]->target = apps_groups_default_target;
1882 // give a default target on all top level processes
1883 if(unlikely(debug)) loops++;
1884 for(p = root_of_pids; p ; p = p->next) {
1885 // if the process is not merged itself
1886 // then is is a top level process
1887 if(unlikely(!p->merged && !p->target))
1888 p->target = apps_groups_default_target;
1890 // make sure all processes have a sortlist
1891 if(unlikely(!p->sortlist))
1892 p->sortlist = sortlist++;
1896 all_pids[1]->sortlist = sortlist++;
1898 // give a target to all merged child processes
1901 if(unlikely(debug)) loops++;
1903 for(p = root_of_pids; p ; p = p->next) {
1904 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1905 p->target = p->parent->target;
1908 if(debug || (p->target && p->target->debug))
1909 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);
1915 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1918 long zero_all_targets(struct target *root) {
1922 for (w = root; w ; w = w->next) {
1925 if(w->fds) freez(w->fds);
1943 w->statm_resident = 0;
1950 w->io_logical_bytes_read = 0;
1951 w->io_logical_bytes_written = 0;
1952 w->io_read_calls = 0;
1953 w->io_write_calls = 0;
1954 w->io_storage_bytes_read = 0;
1955 w->io_storage_bytes_written = 0;
1956 w->io_cancelled_write_bytes = 0;
1962 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1965 if(unlikely(!w->fds))
1966 w->fds = callocz(sizeof(int), (size_t) all_files_size);
1968 if(likely(p->updated)) {
1969 w->cutime += p->cutime;
1970 w->cstime += p->cstime;
1971 w->cgtime += p->cgtime;
1972 w->cminflt += p->cminflt;
1973 w->cmajflt += p->cmajflt;
1975 w->utime += p->utime;
1976 w->stime += p->stime;
1977 w->gtime += p->gtime;
1978 w->minflt += p->minflt;
1979 w->majflt += p->majflt;
1983 w->statm_size += p->statm_size;
1984 w->statm_resident += p->statm_resident;
1985 w->statm_share += p->statm_share;
1986 w->statm_text += p->statm_text;
1987 w->statm_lib += p->statm_lib;
1988 w->statm_data += p->statm_data;
1989 w->statm_dirty += p->statm_dirty;
1991 w->io_logical_bytes_read += p->io_logical_bytes_read;
1992 w->io_logical_bytes_written += p->io_logical_bytes_written;
1993 w->io_read_calls += p->io_read_calls;
1994 w->io_write_calls += p->io_write_calls;
1995 w->io_storage_bytes_read += p->io_storage_bytes_read;
1996 w->io_storage_bytes_written += p->io_storage_bytes_written;
1997 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
2000 w->num_threads += p->num_threads;
2002 if(likely(w->fds)) {
2004 for(c = 0; c < p->fds_size ;c++) {
2005 if(p->fds[c] == 0) continue;
2007 if(likely(p->fds[c] < all_files_size)) {
2008 if(w->fds) w->fds[p->fds[c]]++;
2011 error("Invalid fd number %d", p->fds[c]);
2015 if(unlikely(debug || w->debug))
2016 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);
2020 void count_targets_fds(struct target *root) {
2024 for (w = root; w ; w = w->next) {
2025 if(!w->fds) continue;
2030 w->openinotifies = 0;
2031 w->openeventfds = 0;
2032 w->opentimerfds = 0;
2033 w->opensignalfds = 0;
2034 w->openeventpolls = 0;
2037 for(c = 1; c < all_files_size ;c++) {
2039 switch(all_files[c].type) {
2048 case FILETYPE_SOCKET:
2052 case FILETYPE_INOTIFY:
2056 case FILETYPE_EVENTFD:
2060 case FILETYPE_TIMERFD:
2064 case FILETYPE_SIGNALFD:
2068 case FILETYPE_EVENTPOLL:
2069 w->openeventpolls++;
2082 void calculate_netdata_statistics(void) {
2083 apply_apps_groups_targets_inheritance();
2085 zero_all_targets(users_root_target);
2086 zero_all_targets(groups_root_target);
2087 apps_groups_targets = zero_all_targets(apps_groups_root_target);
2089 // this has to be done, before the cleanup
2090 struct pid_stat *p = NULL;
2091 struct target *w = NULL, *o = NULL;
2093 // concentrate everything on the apps_groups_targets
2094 for(p = root_of_pids; p ; p = p->next) {
2096 // --------------------------------------------------------------------
2097 // apps_groups targets
2098 if(likely(p->target))
2099 aggregate_pid_on_target(p->target, p, NULL);
2101 error("pid %d %s was left without a target!", p->pid, p->comm);
2104 // --------------------------------------------------------------------
2107 if(likely(p->user_target && p->user_target->uid == p->uid))
2110 if(unlikely(debug && p->user_target))
2111 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);
2113 w = p->user_target = get_users_target(p->uid);
2117 aggregate_pid_on_target(w, p, o);
2119 error("pid %d %s was left without a user target!", p->pid, p->comm);
2122 // --------------------------------------------------------------------
2124 o = p->group_target;
2125 if(likely(p->group_target && p->group_target->gid == p->gid))
2126 w = p->group_target;
2128 if(unlikely(debug && p->group_target))
2129 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);
2131 w = p->group_target = get_groups_target(p->gid);
2135 aggregate_pid_on_target(w, p, o);
2137 error("pid %d %s was left without a group target!", p->pid, p->comm);
2141 count_targets_fds(apps_groups_root_target);
2142 count_targets_fds(users_root_target);
2143 count_targets_fds(groups_root_target);
2145 cleanup_exited_pids();
2148 // ----------------------------------------------------------------------------
2149 // update chart dimensions
2151 BUFFER *output = NULL;
2152 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2154 static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2155 // fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2156 buffer_strcat(output, "BEGIN ");
2157 buffer_strcat(output, type);
2158 buffer_strcat(output, ".");
2159 buffer_strcat(output, id);
2160 buffer_strcat(output, " ");
2161 buffer_print_llu(output, usec);
2162 buffer_strcat(output, "\n");
2165 static inline void send_SET(const char *name, unsigned long long value) {
2166 // fprintf(stdout, "SET %s = %llu\n", name, value);
2167 buffer_strcat(output, "SET ");
2168 buffer_strcat(output, name);
2169 buffer_strcat(output, " = ");
2170 buffer_print_llu(output, value);
2171 buffer_strcat(output, "\n");
2174 static inline void send_END(void) {
2175 // fprintf(stdout, "END\n");
2176 buffer_strcat(output, "END\n");
2179 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;
2180 double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2182 unsigned long long send_resource_usage_to_netdata() {
2183 static struct timeval last = { 0, 0 };
2184 static struct rusage me_last;
2189 unsigned long long usec;
2190 unsigned long long cpuuser;
2191 unsigned long long cpusyst;
2194 gettimeofday(&last, NULL);
2195 getrusage(RUSAGE_SELF, &me_last);
2197 // the first time, give a zero to allow
2198 // netdata calibrate to the current time
2199 // usec = update_every * 1000000ULL;
2205 gettimeofday(&now, NULL);
2206 getrusage(RUSAGE_SELF, &me);
2208 usec = usecdiff(&now, &last);
2209 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2210 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2212 bcopy(&now, &last, sizeof(struct timeval));
2213 bcopy(&me, &me_last, sizeof(struct rusage));
2216 buffer_sprintf(output,
2217 "BEGIN netdata.apps_cpu %llu\n"
2219 "SET system = %llu\n"
2221 "BEGIN netdata.apps_files %llu\n"
2222 "SET files = %llu\n"
2225 "SET targets = %ld\n"
2227 "BEGIN netdata.apps_fix %llu\n"
2228 "SET utime = %llu\n"
2229 "SET stime = %llu\n"
2230 "SET gtime = %llu\n"
2231 "SET minflt = %llu\n"
2232 "SET majflt = %llu\n"
2241 , apps_groups_targets
2243 , (unsigned long long)(utime_fix_ratio * 100 * RATES_DETAIL)
2244 , (unsigned long long)(stime_fix_ratio * 100 * RATES_DETAIL)
2245 , (unsigned long long)(gtime_fix_ratio * 100 * RATES_DETAIL)
2246 , (unsigned long long)(minflt_fix_ratio * 100 * RATES_DETAIL)
2247 , (unsigned long long)(majflt_fix_ratio * 100 * RATES_DETAIL)
2250 if(include_exited_childs)
2251 buffer_sprintf(output,
2252 "BEGIN netdata.apps_children_fix %llu\n"
2253 "SET cutime = %llu\n"
2254 "SET cstime = %llu\n"
2255 "SET cgtime = %llu\n"
2256 "SET cminflt = %llu\n"
2257 "SET cmajflt = %llu\n"
2260 , (unsigned long long)(cutime_fix_ratio * 100 * RATES_DETAIL)
2261 , (unsigned long long)(cstime_fix_ratio * 100 * RATES_DETAIL)
2262 , (unsigned long long)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2263 , (unsigned long long)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2264 , (unsigned long long)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2270 void normalize_data(struct target *root) {
2273 // childs processing introduces spikes
2274 // here we try to eliminate them by disabling childs processing either for specific dimensions
2275 // or entirely. Of course, either way, we disable it just a single iteration.
2277 unsigned long long max = processors * hz * RATES_DETAIL;
2278 unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2280 if(global_utime > max) global_utime = max;
2281 if(global_stime > max) global_stime = max;
2282 if(global_gtime > max) global_gtime = max;
2284 for(w = root; w ; w = w->next) {
2285 if(w->target || (!w->processes && !w->exposed)) continue;
2290 cutime += w->cutime;
2291 cstime += w->cstime;
2292 cgtime += w->cgtime;
2294 minflt += w->minflt;
2295 majflt += w->majflt;
2296 cminflt += w->cminflt;
2297 cmajflt += w->cmajflt;
2300 if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
2301 if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
2302 // everything we collected fits
2308 cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
2310 else if(global_utime + global_stime > utime + stime) {
2311 // childrens resources are too high
2312 // lower only the children resources
2315 gtime_fix_ratio = 1.0;
2318 cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
2321 // even running processes are unrealistic
2322 // zero the children resources
2323 // lower the running processes resources
2326 gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
2329 cgtime_fix_ratio = 0.0;
2338 cgtime_fix_ratio = 0.0;
2341 if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
2342 if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
2343 if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
2344 if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
2345 if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
2346 if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
2348 // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
2349 // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
2350 // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
2351 // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
2352 // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
2353 // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
2356 // we use cpu time to normalize page faults
2357 // the problem is that to find the proper max values
2358 // for page faults we have to parse /proc/vmstat
2359 // which is quite big to do it again (netdata does it already)
2361 // a better solution could be to somehow have netdata
2362 // do this normalization for us
2364 if(utime || stime || gtime)
2366 minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
2369 majflt_fix_ratio = 1.0;
2371 if(cutime || cstime || cgtime)
2373 cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
2376 cmajflt_fix_ratio = 1.0;
2380 if(unlikely(debug)) {
2382 "SYSTEM: u=%llu s=%llu g=%llu "
2383 "COLLECTED: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2384 "DELTA: u=%lld s=%lld g=%lld "
2385 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2386 "FINALLY: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2397 , (long long)utime + (long long)cutime - (long long)global_utime
2398 , (long long)stime + (long long)cstime - (long long)global_stime
2399 , (long long)gtime + (long long)cgtime - (long long)global_gtime
2406 , (unsigned long long)(utime * utime_fix_ratio)
2407 , (unsigned long long)(stime * stime_fix_ratio)
2408 , (unsigned long long)(gtime * gtime_fix_ratio)
2409 , (unsigned long long)(cutime * cutime_fix_ratio)
2410 , (unsigned long long)(cstime * cstime_fix_ratio)
2411 , (unsigned long long)(cgtime * cgtime_fix_ratio)
2416 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec) {
2419 send_BEGIN(type, "cpu", usec);
2420 for (w = root; w ; w = w->next) {
2421 if(unlikely(w->exposed))
2422 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));
2426 send_BEGIN(type, "cpu_user", usec);
2427 for (w = root; w ; w = w->next) {
2428 if(unlikely(w->exposed))
2429 send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2433 send_BEGIN(type, "cpu_system", usec);
2434 for (w = root; w ; w = w->next) {
2435 if(unlikely(w->exposed))
2436 send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2440 if(show_guest_time) {
2441 send_BEGIN(type, "cpu_guest", usec);
2442 for (w = root; w ; w = w->next) {
2443 if(unlikely(w->exposed))
2444 send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2449 send_BEGIN(type, "threads", usec);
2450 for (w = root; w ; w = w->next) {
2451 if(unlikely(w->exposed))
2452 send_SET(w->name, w->num_threads);
2456 send_BEGIN(type, "processes", usec);
2457 for (w = root; w ; w = w->next) {
2458 if(unlikely(w->exposed))
2459 send_SET(w->name, w->processes);
2463 send_BEGIN(type, "mem", usec);
2464 for (w = root; w ; w = w->next) {
2465 if(unlikely(w->exposed))
2466 send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2470 send_BEGIN(type, "minor_faults", usec);
2471 for (w = root; w ; w = w->next) {
2472 if(unlikely(w->exposed))
2473 send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2477 send_BEGIN(type, "major_faults", usec);
2478 for (w = root; w ; w = w->next) {
2479 if(unlikely(w->exposed))
2480 send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2484 send_BEGIN(type, "lreads", usec);
2485 for (w = root; w ; w = w->next) {
2486 if(unlikely(w->exposed))
2487 send_SET(w->name, w->io_logical_bytes_read);
2491 send_BEGIN(type, "lwrites", usec);
2492 for (w = root; w ; w = w->next) {
2493 if(unlikely(w->exposed))
2494 send_SET(w->name, w->io_logical_bytes_written);
2498 send_BEGIN(type, "preads", usec);
2499 for (w = root; w ; w = w->next) {
2500 if(unlikely(w->exposed))
2501 send_SET(w->name, w->io_storage_bytes_read);
2505 send_BEGIN(type, "pwrites", usec);
2506 for (w = root; w ; w = w->next) {
2507 if(unlikely(w->exposed))
2508 send_SET(w->name, w->io_storage_bytes_written);
2512 send_BEGIN(type, "files", usec);
2513 for (w = root; w ; w = w->next) {
2514 if(unlikely(w->exposed))
2515 send_SET(w->name, w->openfiles);
2519 send_BEGIN(type, "sockets", usec);
2520 for (w = root; w ; w = w->next) {
2521 if(unlikely(w->exposed))
2522 send_SET(w->name, w->opensockets);
2526 send_BEGIN(type, "pipes", usec);
2527 for (w = root; w ; w = w->next) {
2528 if(unlikely(w->exposed))
2529 send_SET(w->name, w->openpipes);
2535 // ----------------------------------------------------------------------------
2536 // generate the charts
2538 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2541 int newly_added = 0;
2543 for(w = root ; w ; w = w->next) {
2544 if (w->target) continue;
2546 if (!w->exposed && w->processes) {
2549 if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2553 // nothing more to show
2554 if(!newly_added && show_guest_time == show_guest_time_old) return;
2556 // we have something new to show
2557 // update the charts
2558 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);
2559 for (w = root; w ; w = w->next) {
2560 if(unlikely(w->exposed))
2561 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2564 buffer_sprintf(output, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2565 for (w = root; w ; w = w->next) {
2566 if(unlikely(w->exposed))
2567 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2570 buffer_sprintf(output, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2571 for (w = root; w ; w = w->next) {
2572 if(unlikely(w->exposed))
2573 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2576 buffer_sprintf(output, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2577 for (w = root; w ; w = w->next) {
2578 if(unlikely(w->exposed))
2579 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2582 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);
2583 for (w = root; w ; w = w->next) {
2584 if(unlikely(w->exposed))
2585 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2588 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);
2589 for (w = root; w ; w = w->next) {
2590 if(unlikely(w->exposed))
2591 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2594 if(show_guest_time) {
2595 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);
2596 for (w = root; w; w = w->next) {
2597 if(unlikely(w->exposed))
2598 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2602 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);
2603 for (w = root; w ; w = w->next) {
2604 if(unlikely(w->exposed))
2605 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2608 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);
2609 for (w = root; w ; w = w->next) {
2610 if(unlikely(w->exposed))
2611 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2614 buffer_sprintf(output, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2615 for (w = root; w ; w = w->next) {
2616 if(unlikely(w->exposed))
2617 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2620 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);
2621 for (w = root; w ; w = w->next) {
2622 if(unlikely(w->exposed))
2623 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2626 buffer_sprintf(output, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2627 for (w = root; w ; w = w->next) {
2628 if(unlikely(w->exposed))
2629 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2632 buffer_sprintf(output, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2633 for (w = root; w ; w = w->next) {
2634 if(unlikely(w->exposed))
2635 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2638 buffer_sprintf(output, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2639 for (w = root; w ; w = w->next) {
2640 if(unlikely(w->exposed))
2641 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2644 buffer_sprintf(output, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2645 for (w = root; w ; w = w->next) {
2646 if(unlikely(w->exposed))
2647 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2650 buffer_sprintf(output, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", 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);
2658 // ----------------------------------------------------------------------------
2659 // parse command line arguments
2661 void parse_args(int argc, char **argv)
2666 for(i = 1; i < argc; i++) {
2668 int n = atoi(argv[i]);
2675 if(strcmp("debug", argv[i]) == 0) {
2677 // debug_flags = 0xffffffff;
2681 if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
2682 include_exited_childs = 0;
2686 if(strcmp("with-childs", argv[i]) == 0) {
2687 include_exited_childs = 1;
2691 if(strcmp("with-guest", argv[i]) == 0) {
2692 enable_guest_charts = 1;
2696 if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
2697 enable_guest_charts = 0;
2701 if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
2704 "(C) 2016 Costa Tsaousis"
2706 "This program is a data collector plugin for netdata.\n"
2708 "Valid command line options:\n"
2710 "SECONDS set the data collection frequency\n"
2712 "debug enable debugging (lot of output)\n"
2715 "without-childs enable / disable aggregating exited\n"
2716 " children resources into parents\n"
2717 " (default is enabled)\n"
2720 "without-guest enable / disable reporting guest charts\n"
2721 " (default is disabled)\n"
2723 "NAME read apps_NAME.conf instead of\n"
2724 " apps_groups.conf\n"
2725 " (default NAME=groups)\n"
2735 error("Cannot understand option %s", argv[i]);
2739 if(freq > 0) update_every = freq;
2740 if(!name) name = "groups";
2742 if(read_apps_groups_conf(name)) {
2743 error("Cannot read process groups %s", name);
2748 int main(int argc, char **argv)
2750 // debug_flags = D_PROCFILE;
2752 // set the name for logging
2753 program_name = "apps.plugin";
2755 // disable syslog for apps.plugin
2756 error_log_syslog = 0;
2758 // set errors flood protection to 100 logs per hour
2759 error_log_errors_per_period = 100;
2760 error_log_throttle_period = 3600;
2762 host_prefix = getenv("NETDATA_HOST_PREFIX");
2763 if(host_prefix == NULL) {
2764 info("NETDATA_HOST_PREFIX is not passed from netdata");
2767 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2769 config_dir = getenv("NETDATA_CONFIG_DIR");
2770 if(config_dir == NULL) {
2771 info("NETDATA_CONFIG_DIR is not passed from netdata");
2772 config_dir = CONFIG_DIR;
2774 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2776 #ifdef NETDATA_INTERNAL_CHECKS
2777 if(debug_flags != 0) {
2778 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2779 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2780 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2781 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2783 #endif /* NETDATA_INTERNAL_CHECKS */
2785 procfile_adaptive_initial_allocation = 1;
2787 time_t started_t = time(NULL);
2790 pid_max = get_system_pid_max();
2791 processors = get_system_cpus();
2793 parse_args(argc, argv);
2795 all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
2796 all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
2798 output = buffer_create(1024);
2799 buffer_sprintf(output,
2800 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2801 "DIMENSION user '' incremental 1 1000\n"
2802 "DIMENSION system '' incremental 1 1000\n"
2803 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2804 "DIMENSION files '' incremental 1 1\n"
2805 "DIMENSION pids '' absolute 1 1\n"
2806 "DIMENSION fds '' absolute 1 1\n"
2807 "DIMENSION targets '' absolute 1 1\n"
2808 "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
2809 "DIMENSION utime '' absolute 1 %2$llu\n"
2810 "DIMENSION stime '' absolute 1 %2$llu\n"
2811 "DIMENSION gtime '' absolute 1 %2$llu\n"
2812 "DIMENSION minflt '' absolute 1 %2$llu\n"
2813 "DIMENSION majflt '' absolute 1 %2$llu\n"
2818 if(include_exited_childs)
2819 buffer_sprintf(output,
2820 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
2821 "DIMENSION cutime '' absolute 1 %2$llu\n"
2822 "DIMENSION cstime '' absolute 1 %2$llu\n"
2823 "DIMENSION cgtime '' absolute 1 %2$llu\n"
2824 "DIMENSION cminflt '' absolute 1 %2$llu\n"
2825 "DIMENSION cmajflt '' absolute 1 %2$llu\n"
2830 #ifndef PROFILING_MODE
2831 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2832 unsigned long long sunow;
2833 #endif /* PROFILING_MODE */
2835 global_iterations_counter = 1;
2836 for(;1; global_iterations_counter++) {
2837 #ifndef PROFILING_MODE
2838 // delay until it is our time to run
2839 while((sunow = timems()) < sunext)
2840 usecsleep(sunext - sunow);
2842 // find the next time we need to run
2843 while(timems() > sunext)
2844 sunext += update_every * 1000000ULL;
2845 #endif /* PROFILING_MODE */
2847 if(!collect_data_for_all_processes_from_proc()) {
2848 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2849 printf("DISABLE\n");
2853 calculate_netdata_statistics();
2854 normalize_data(apps_groups_root_target);
2856 unsigned long long dt = send_resource_usage_to_netdata();
2858 // this is smart enough to show only newly added apps, when needed
2859 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2860 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2861 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2863 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2864 send_collected_data_to_netdata(users_root_target, "users", dt);
2865 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2867 show_guest_time_old = show_guest_time;
2869 //if(puts(buffer_tostring(output)) == EOF)
2870 if(write(STDOUT_FILENO, buffer_tostring(output), buffer_strlen(output)) == -1)
2871 fatal("Cannot send chart values to netdata.");
2874 buffer_flush(output);
2877 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2879 current_t = time(NULL);
2881 #ifndef PROFILING_MODE
2882 // restart check (14400 seconds)
2883 if(current_t - started_t > 14400) exit(0);
2885 if(current_t - started_t > 10) exit(0);
2886 #endif /* PROFILING_MODE */