3 #define MAX_COMPARE_NAME 100
5 #define MAX_CMDLINE 1024
7 // the rates we are going to send to netdata
8 // will have this detail
10 // 1 will send just integer parts to netdata
11 // 100 will send 2 decimal points
12 // 1000 will send 3 decimal points
14 #define RATES_DETAIL 10000ULL
19 unsigned long long global_iterations_counter = 1;
20 unsigned long long file_counter = 0;
21 int proc_pid_cmdline_is_needed = 0;
22 int include_exited_childs = 1;
23 char *config_dir = CONFIG_DIR;
25 pid_t *all_pids_sortlist = NULL;
27 // will be automatically set to 1, if guest values are collected
28 int show_guest_time = 0;
29 int show_guest_time_old = 0;
31 int enable_guest_charts = 0;
32 int enable_file_charts = 1;
34 // ----------------------------------------------------------------------------
36 void netdata_cleanup_and_exit(int ret) {
41 // ----------------------------------------------------------------------------
43 // target is the structure that process data are aggregated
46 char compare[MAX_COMPARE_NAME + 1];
50 char id[MAX_NAME + 1];
53 char name[MAX_NAME + 1];
58 unsigned long long minflt;
59 unsigned long long cminflt;
60 unsigned long long majflt;
61 unsigned long long cmajflt;
62 unsigned long long utime;
63 unsigned long long stime;
64 unsigned long long gtime;
65 unsigned long long cutime;
66 unsigned long long cstime;
67 unsigned long long cgtime;
68 unsigned long long num_threads;
69 unsigned long long rss;
71 unsigned long long statm_size;
72 unsigned long long statm_resident;
73 unsigned long long statm_share;
74 unsigned long long statm_text;
75 unsigned long long statm_lib;
76 unsigned long long statm_data;
77 unsigned long long statm_dirty;
79 unsigned long long io_logical_bytes_read;
80 unsigned long long io_logical_bytes_written;
81 unsigned long long io_read_calls;
82 unsigned long long io_write_calls;
83 unsigned long long io_storage_bytes_read;
84 unsigned long long io_storage_bytes_written;
85 unsigned long long io_cancelled_write_bytes;
88 unsigned long long openfiles;
89 unsigned long long openpipes;
90 unsigned long long opensockets;
91 unsigned long long openinotifies;
92 unsigned long long openeventfds;
93 unsigned long long opentimerfds;
94 unsigned long long opensignalfds;
95 unsigned long long openeventpolls;
96 unsigned long long openother;
98 unsigned long processes; // how many processes have been merged to this
99 int exposed; // if set, we have sent this to netdata
100 int hidden; // if set, we set the hidden flag on the dimension
103 int starts_with; // if set, the compare string matches only the
104 // beginning of the command
106 struct target *target; // the one that will be reported to netdata
111 // ----------------------------------------------------------------------------
113 // aggregate all processes in groups, to have a limited number of dimensions
115 struct target *apps_groups_root_target = NULL;
116 struct target *apps_groups_default_target = NULL;
117 long apps_groups_targets = 0;
119 struct target *users_root_target = NULL;
120 struct target *groups_root_target = NULL;
122 struct target *get_users_target(uid_t uid)
125 for(w = users_root_target ; w ; w = w->next)
126 if(w->uid == uid) return w;
128 w = callocz(sizeof(struct target), 1);
129 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
130 w->comparehash = simple_hash(w->compare);
131 w->comparelen = strlen(w->compare);
133 snprintfz(w->id, MAX_NAME, "%u", uid);
134 w->idhash = simple_hash(w->id);
136 struct passwd *pw = getpwuid(uid);
138 snprintfz(w->name, MAX_NAME, "%u", uid);
140 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
142 netdata_fix_chart_name(w->name);
146 w->next = users_root_target;
147 users_root_target = w;
150 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
155 struct target *get_groups_target(gid_t gid)
158 for(w = groups_root_target ; w ; w = w->next)
159 if(w->gid == gid) return w;
161 w = callocz(sizeof(struct target), 1);
162 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
163 w->comparehash = simple_hash(w->compare);
164 w->comparelen = strlen(w->compare);
166 snprintfz(w->id, MAX_NAME, "%u", gid);
167 w->idhash = simple_hash(w->id);
169 struct group *gr = getgrgid(gid);
171 snprintfz(w->name, MAX_NAME, "%u", gid);
173 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
175 netdata_fix_chart_name(w->name);
179 w->next = groups_root_target;
180 groups_root_target = w;
183 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
188 // find or create a new target
189 // there are targets that are just aggregated to other target (the second argument)
190 struct target *get_apps_groups_target(const char *id, struct target *target) {
191 int tdebug = 0, thidden = 0, ends_with = 0;
192 const char *nid = id;
194 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
195 if(nid[0] == '-') thidden = 1;
196 if(nid[0] == '+') tdebug = 1;
197 if(nid[0] == '*') ends_with = 1;
200 uint32_t hash = simple_hash(id);
202 struct target *w, *last = apps_groups_root_target;
203 for(w = apps_groups_root_target ; w ; w = w->next) {
204 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
210 w = callocz(sizeof(struct target), 1);
211 strncpyz(w->id, nid, MAX_NAME);
212 w->idhash = simple_hash(w->id);
214 strncpyz(w->name, nid, MAX_NAME);
216 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
217 size_t len = strlen(w->compare);
218 if(w->compare[len - 1] == '*') {
219 w->compare[len - 1] = '\0';
222 w->ends_with = ends_with;
224 if(w->starts_with && w->ends_with)
225 proc_pid_cmdline_is_needed = 1;
227 w->comparehash = simple_hash(w->compare);
228 w->comparelen = strlen(w->compare);
234 // append it, to maintain the order in apps_groups.conf
235 if(last) last->next = w;
236 else apps_groups_root_target = w;
239 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
241 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
242 , w->target?w->target->id:w->id
243 , (w->hidden)?"hidden":"-"
244 , (w->debug)?"debug":"-"
250 // read the apps_groups.conf file
251 int read_apps_groups_conf(const char *name)
253 char filename[FILENAME_MAX + 1];
255 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
258 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
260 // ----------------------------------------
262 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
265 procfile_set_quotes(ff, "'\"");
267 ff = procfile_readall(ff);
271 unsigned long line, lines = procfile_lines(ff);
273 for(line = 0; line < lines ;line++) {
274 unsigned long word, words = procfile_linewords(ff, line);
275 struct target *w = NULL;
277 char *t = procfile_lineword(ff, line, 0);
278 if(!t || !*t) continue;
280 for(word = 0; word < words ;word++) {
281 char *s = procfile_lineword(ff, line, word);
282 if(!s || !*s) continue;
287 struct target *n = get_apps_groups_target(s, w);
289 error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
297 int tdebug = 0, thidden = 0;
299 while(t[0] == '-' || t[0] == '+') {
300 if(t[0] == '-') thidden = 1;
301 if(t[0] == '+') tdebug = 1;
305 strncpyz(w->name, t, MAX_NAME);
310 fprintf(stderr, "apps.plugin: AGGREGATION TARGET NAME '%s' on ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
313 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
314 , w->target?w->target->id:w->id
315 , (w->hidden)?"hidden":"-"
316 , (w->debug)?"debug":"-"
323 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
324 if(!apps_groups_default_target)
325 error("Cannot create default target");
327 strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
333 // ----------------------------------------------------------------------------
334 // data to store for each pid
337 #define PID_LOG_IO 0x00000001
338 #define PID_LOG_STATM 0x00000002
339 #define PID_LOG_CMDLINE 0x00000004
340 #define PID_LOG_FDS 0x00000008
341 #define PID_LOG_STAT 0x00000010
345 char comm[MAX_COMPARE_NAME + 1];
346 char cmdline[MAX_CMDLINE + 1];
358 // these are raw values collected
359 unsigned long long minflt_raw;
360 unsigned long long cminflt_raw;
361 unsigned long long majflt_raw;
362 unsigned long long cmajflt_raw;
363 unsigned long long utime_raw;
364 unsigned long long stime_raw;
365 unsigned long long gtime_raw; // guest_time
366 unsigned long long cutime_raw;
367 unsigned long long cstime_raw;
368 unsigned long long cgtime_raw; // cguest_time
371 unsigned long long minflt;
372 unsigned long long cminflt;
373 unsigned long long majflt;
374 unsigned long long cmajflt;
375 unsigned long long utime;
376 unsigned long long stime;
377 unsigned long long gtime;
378 unsigned long long cutime;
379 unsigned long long cstime;
380 unsigned long long cgtime;
385 // int64_t itrealvalue;
386 // unsigned long long starttime;
387 // unsigned long long vsize;
388 unsigned long long rss;
389 // unsigned long long rsslim;
390 // unsigned long long starcode;
391 // unsigned long long endcode;
392 // unsigned long long startstack;
393 // unsigned long long kstkesp;
394 // unsigned long long kstkeip;
397 // uint64_t sigignore;
398 // uint64_t sigcatch;
402 // int32_t exit_signal;
403 // int32_t processor;
404 // uint32_t rt_priority;
406 // unsigned long long delayacct_blkio_ticks;
411 unsigned long long statm_size;
412 unsigned long long statm_resident;
413 unsigned long long statm_share;
414 unsigned long long statm_text;
415 unsigned long long statm_lib;
416 unsigned long long statm_data;
417 unsigned long long statm_dirty;
419 unsigned long long io_logical_bytes_read_raw;
420 unsigned long long io_logical_bytes_written_raw;
421 unsigned long long io_read_calls_raw;
422 unsigned long long io_write_calls_raw;
423 unsigned long long io_storage_bytes_read_raw;
424 unsigned long long io_storage_bytes_written_raw;
425 unsigned long long io_cancelled_write_bytes_raw;
427 unsigned long long io_logical_bytes_read;
428 unsigned long long io_logical_bytes_written;
429 unsigned long long io_read_calls;
430 unsigned long long io_write_calls;
431 unsigned long long io_storage_bytes_read;
432 unsigned long long io_storage_bytes_written;
433 unsigned long long io_cancelled_write_bytes;
435 int *fds; // array of fds it uses
436 int fds_size; // the size of the fds array
438 int children_count; // number of processes directly referencing this
439 int keep; // 1 when we need to keep this process in memory even after it exited
440 int keeploops; // increases by 1 every time keep is 1 and updated 0
441 int updated; // 1 when the process is currently running
442 int merged; // 1 when it has been merged to its parent
443 int new_entry; // 1 when this is a new process, just saw for the first time
444 int read; // 1 when we have already read this process for this iteration
445 int sortlist; // higher numbers = top on the process tree
446 // each process gets a unique number
448 struct target *target; // app_groups.conf targets
449 struct target *user_target; // uid based targets
450 struct target *group_target; // gid based targets
452 unsigned long long stat_collected_usec;
453 unsigned long long last_stat_collected_usec;
455 unsigned long long io_collected_usec;
456 unsigned long long last_io_collected_usec;
459 char *statm_filename;
461 char *cmdline_filename;
463 struct pid_stat *parent;
464 struct pid_stat *prev;
465 struct pid_stat *next;
466 } *root_of_pids = NULL, **all_pids;
468 long all_pids_count = 0;
470 struct pid_stat *get_pid_entry(pid_t pid) {
472 all_pids[pid]->new_entry = 0;
473 return all_pids[pid];
476 all_pids[pid] = callocz(sizeof(struct pid_stat), 1);
477 all_pids[pid]->fds = callocz(sizeof(int), 100);
478 all_pids[pid]->fds_size = 100;
480 if(root_of_pids) root_of_pids->prev = all_pids[pid];
481 all_pids[pid]->next = root_of_pids;
482 root_of_pids = all_pids[pid];
484 all_pids[pid]->pid = pid;
485 all_pids[pid]->new_entry = 1;
489 return all_pids[pid];
492 void del_pid_entry(pid_t pid) {
494 error("attempted to free pid %d that is not allocated.", pid);
499 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
501 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
502 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
503 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
505 if(all_pids[pid]->fds) freez(all_pids[pid]->fds);
506 if(all_pids[pid]->stat_filename) freez(all_pids[pid]->stat_filename);
507 if(all_pids[pid]->statm_filename) freez(all_pids[pid]->statm_filename);
508 if(all_pids[pid]->io_filename) freez(all_pids[pid]->io_filename);
509 if(all_pids[pid]->cmdline_filename) freez(all_pids[pid]->cmdline_filename);
510 freez(all_pids[pid]);
512 all_pids[pid] = NULL;
517 // ----------------------------------------------------------------------------
518 // update pids from proc
520 int read_proc_pid_cmdline(struct pid_stat *p) {
522 if(unlikely(!p->cmdline_filename)) {
523 char filename[FILENAME_MAX + 1];
524 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", global_host_prefix, p->pid);
525 p->cmdline_filename = strdupz(filename);
528 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
529 if(unlikely(fd == -1)) goto cleanup;
531 ssize_t i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
534 if(unlikely(bytes < 0)) goto cleanup;
536 p->cmdline[bytes] = '\0';
537 for(i = 0; i < bytes ; i++)
538 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
541 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
546 // copy the command to the command line
547 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
551 int read_proc_pid_ownership(struct pid_stat *p) {
552 if(unlikely(!p->stat_filename)) {
553 error("pid %d does not have a stat_filename", p->pid);
557 // ----------------------------------------
561 if(stat(p->stat_filename, &st) != 0) {
562 error("Cannot stat file '%s'", p->stat_filename);
572 int read_proc_pid_stat(struct pid_stat *p) {
573 static procfile *ff = NULL;
575 if(unlikely(!p->stat_filename)) {
576 char filename[FILENAME_MAX + 1];
577 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", global_host_prefix, p->pid);
578 p->stat_filename = strdupz(filename);
581 int set_quotes = (!ff)?1:0;
583 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
584 if(unlikely(!ff)) goto cleanup;
586 // if(set_quotes) procfile_set_quotes(ff, "()");
587 if(set_quotes) procfile_set_open_close(ff, "(", ")");
589 ff = procfile_readall(ff);
590 if(unlikely(!ff)) goto cleanup;
592 p->last_stat_collected_usec = p->stat_collected_usec;
593 p->stat_collected_usec = time_usec();
596 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
598 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
600 // p->state = *(procfile_lineword(ff, 0, 2));
601 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3));
602 // p->pgrp = atol(procfile_lineword(ff, 0, 4));
603 // p->session = atol(procfile_lineword(ff, 0, 5));
604 // p->tty_nr = atol(procfile_lineword(ff, 0, 6));
605 // p->tpgid = atol(procfile_lineword(ff, 0, 7));
606 // p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
608 unsigned long long last;
610 last = p->minflt_raw;
611 p->minflt_raw = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
612 p->minflt = (p->minflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
614 last = p->cminflt_raw;
615 p->cminflt_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
616 p->cminflt = (p->cminflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
618 last = p->majflt_raw;
619 p->majflt_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
620 p->majflt = (p->majflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
622 last = p->cmajflt_raw;
623 p->cmajflt_raw = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
624 p->cmajflt = (p->cmajflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
627 p->utime_raw = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
628 p->utime = (p->utime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
631 p->stime_raw = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
632 p->stime = (p->stime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
634 last = p->cutime_raw;
635 p->cutime_raw = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
636 p->cutime = (p->cutime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
638 last = p->cstime_raw;
639 p->cstime_raw = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
640 p->cstime = (p->cstime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
642 // p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
643 // p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
644 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19));
645 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
646 // p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
647 // p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
648 p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
649 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
650 // p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
651 // p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
652 // p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
653 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
654 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
655 // p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
656 // p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
657 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
658 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
659 // p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
660 // p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
661 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
662 // p->exit_signal = atol(procfile_lineword(ff, 0, 37));
663 // p->processor = atol(procfile_lineword(ff, 0, 38));
664 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
665 // p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
666 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
668 if(enable_guest_charts) {
670 p->gtime_raw = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
671 p->gtime = (p->gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
673 last = p->cgtime_raw;
674 p->cgtime_raw = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
675 p->cgtime = (p->cgtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
677 if (show_guest_time || p->gtime || p->cgtime) {
678 p->utime -= (p->utime >= p->gtime) ? p->gtime : p->utime;
679 p->cutime -= (p->cutime >= p->cgtime) ? p->cgtime : p->cutime;
684 if(unlikely(debug || (p->target && p->target->debug)))
685 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);
687 if(unlikely(global_iterations_counter == 1)) {
718 int read_proc_pid_statm(struct pid_stat *p) {
719 static procfile *ff = NULL;
721 if(unlikely(!p->statm_filename)) {
722 char filename[FILENAME_MAX + 1];
723 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", global_host_prefix, p->pid);
724 p->statm_filename = strdupz(filename);
727 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
728 if(unlikely(!ff)) goto cleanup;
730 ff = procfile_readall(ff);
731 if(unlikely(!ff)) goto cleanup;
735 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
736 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
737 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
738 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
739 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
740 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
741 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
747 p->statm_resident = 0;
756 int read_proc_pid_io(struct pid_stat *p) {
757 static procfile *ff = NULL;
759 if(unlikely(!p->io_filename)) {
760 char filename[FILENAME_MAX + 1];
761 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", global_host_prefix, p->pid);
762 p->io_filename = strdupz(filename);
766 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
767 if(unlikely(!ff)) goto cleanup;
769 ff = procfile_readall(ff);
770 if(unlikely(!ff)) goto cleanup;
774 p->last_io_collected_usec = p->io_collected_usec;
775 p->io_collected_usec = time_usec();
777 unsigned long long last;
779 last = p->io_logical_bytes_read_raw;
780 p->io_logical_bytes_read_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
781 p->io_logical_bytes_read = (p->io_logical_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
783 last = p->io_logical_bytes_written_raw;
784 p->io_logical_bytes_written_raw = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
785 p->io_logical_bytes_written = (p->io_logical_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
787 last = p->io_read_calls_raw;
788 p->io_read_calls_raw = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
789 p->io_read_calls = (p->io_read_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
791 last = p->io_write_calls_raw;
792 p->io_write_calls_raw = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
793 p->io_write_calls = (p->io_write_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
795 last = p->io_storage_bytes_read_raw;
796 p->io_storage_bytes_read_raw = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
797 p->io_storage_bytes_read = (p->io_storage_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
799 last = p->io_storage_bytes_written_raw;
800 p->io_storage_bytes_written_raw = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
801 p->io_storage_bytes_written = (p->io_storage_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
803 last = p->io_cancelled_write_bytes_raw;
804 p->io_cancelled_write_bytes_raw = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
805 p->io_cancelled_write_bytes = (p->io_cancelled_write_bytes_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
807 if(unlikely(global_iterations_counter == 1)) {
808 p->io_logical_bytes_read = 0;
809 p->io_logical_bytes_written = 0;
810 p->io_read_calls = 0;
811 p->io_write_calls = 0;
812 p->io_storage_bytes_read = 0;
813 p->io_storage_bytes_written = 0;
814 p->io_cancelled_write_bytes = 0;
820 p->io_logical_bytes_read = 0;
821 p->io_logical_bytes_written = 0;
822 p->io_read_calls = 0;
823 p->io_write_calls = 0;
824 p->io_storage_bytes_read = 0;
825 p->io_storage_bytes_written = 0;
826 p->io_cancelled_write_bytes = 0;
830 unsigned long long global_utime = 0;
831 unsigned long long global_stime = 0;
832 unsigned long long global_gtime = 0;
834 int read_proc_stat() {
835 static char filename[FILENAME_MAX + 1] = "";
836 static procfile *ff = NULL;
837 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;
840 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", global_host_prefix);
841 ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
842 if(unlikely(!ff)) goto cleanup;
845 ff = procfile_readall(ff);
846 if(unlikely(!ff)) goto cleanup;
848 last_collected_usec = collected_usec;
849 collected_usec = time_usec();
853 unsigned long long last;
856 utime_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
857 global_utime = (utime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
859 // nice time, on user time
861 ntime_raw = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
862 global_utime += (ntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
865 stime_raw = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
866 global_stime = (stime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
869 gtime_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
870 global_gtime = (gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
872 if(enable_guest_charts) {
873 // guest nice time, on guest time
875 gntime_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
876 global_gtime += (gntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
878 // remove guest time from user time
879 global_utime -= (global_utime > global_gtime) ? global_gtime : global_utime;
882 if(unlikely(global_iterations_counter == 1)) {
898 // ----------------------------------------------------------------------------
900 // this is used to keep a global list of all open files of the system
901 // it is needed in order to calculate the unique files processes have open
903 #define FILE_DESCRIPTORS_INCREASE_STEP 100
905 struct file_descriptor {
907 #ifdef NETDATA_INTERNAL_CHECKS
909 #endif /* NETDATA_INTERNAL_CHECKS */
917 int all_files_len = 0;
918 int all_files_size = 0;
920 int file_descriptor_compare(void* a, void* b) {
921 #ifdef NETDATA_INTERNAL_CHECKS
922 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
923 error("Corrupted index data detected. Please report this.");
924 #endif /* NETDATA_INTERNAL_CHECKS */
926 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
929 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
933 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
936 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
938 avl_tree all_files_index = {
940 file_descriptor_compare
943 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
944 struct file_descriptor tmp;
945 tmp.hash = (hash)?hash:simple_hash(name);
949 #ifdef NETDATA_INTERNAL_CHECKS
950 tmp.magic = 0x0BADCAFE;
951 #endif /* NETDATA_INTERNAL_CHECKS */
953 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
956 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
957 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
959 #define FILETYPE_OTHER 0
960 #define FILETYPE_FILE 1
961 #define FILETYPE_PIPE 2
962 #define FILETYPE_SOCKET 3
963 #define FILETYPE_INOTIFY 4
964 #define FILETYPE_EVENTFD 5
965 #define FILETYPE_EVENTPOLL 6
966 #define FILETYPE_TIMERFD 7
967 #define FILETYPE_SIGNALFD 8
969 void file_descriptor_not_used(int id)
971 if(id > 0 && id < all_files_size) {
973 #ifdef NETDATA_INTERNAL_CHECKS
974 if(all_files[id].magic != 0x0BADCAFE) {
975 error("Ignoring request to remove empty file id %d.", id);
978 #endif /* NETDATA_INTERNAL_CHECKS */
981 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
983 if(all_files[id].count > 0) {
984 all_files[id].count--;
986 if(!all_files[id].count) {
988 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
990 file_descriptor_remove(&all_files[id]);
991 #ifdef NETDATA_INTERNAL_CHECKS
992 all_files[id].magic = 0x00000000;
993 #endif /* NETDATA_INTERNAL_CHECKS */
998 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1000 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1003 int file_descriptor_find_or_add(const char *name)
1005 static int last_pos = 0;
1006 uint32_t hash = simple_hash(name);
1009 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1011 struct file_descriptor *fd = file_descriptor_find(name, hash);
1015 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1022 // check we have enough memory to add it
1023 if(!all_files || all_files_len == all_files_size) {
1024 void *old = all_files;
1027 // there is no empty slot
1029 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1031 all_files = reallocz(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1033 // if the address changed, we have to rebuild the index
1034 // since all pointers are now invalid
1035 if(old && old != (void *)all_files) {
1037 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1039 all_files_index.root = NULL;
1040 for(i = 0; i < all_files_size; i++) {
1041 if(!all_files[i].count) continue;
1042 file_descriptor_add(&all_files[i]);
1046 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1049 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1050 all_files[i].count = 0;
1051 all_files[i].name = NULL;
1052 #ifdef NETDATA_INTERNAL_CHECKS
1053 all_files[i].magic = 0x00000000;
1054 #endif /* NETDATA_INTERNAL_CHECKS */
1055 all_files[i].pos = i;
1058 if(!all_files_size) all_files_len = 1;
1059 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1063 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1065 // search for an empty slot
1067 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1068 if(c >= all_files_size) c = 0;
1069 if(c == 0) continue;
1071 if(!all_files[c].count) {
1073 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1075 #ifdef NETDATA_INTERNAL_CHECKS
1076 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1077 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1078 #endif /* NETDATA_INTERNAL_CHECKS */
1081 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);
1083 if(all_files[c].name) freez((void *)all_files[c].name);
1084 all_files[c].name = NULL;
1089 if(i == all_files_size) {
1090 fatal("We should find an empty slot, but there isn't any");
1095 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1099 // else we have an empty slot in 'c'
1102 if(name[0] == '/') type = FILETYPE_FILE;
1103 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1104 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1105 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1106 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1107 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1108 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1109 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1110 else if(strncmp(name, "anon_inode:", 11) == 0) {
1112 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1114 type = FILETYPE_OTHER;
1118 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1120 type = FILETYPE_OTHER;
1123 all_files[c].name = strdupz(name);
1124 all_files[c].hash = hash;
1125 all_files[c].type = type;
1126 all_files[c].pos = c;
1127 all_files[c].count = 1;
1128 #ifdef NETDATA_INTERNAL_CHECKS
1129 all_files[c].magic = 0x0BADCAFE;
1130 #endif /* NETDATA_INTERNAL_CHECKS */
1131 file_descriptor_add(&all_files[c]);
1134 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1139 int read_pid_file_descriptors(struct pid_stat *p) {
1140 char dirname[FILENAME_MAX+1];
1142 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", global_host_prefix, p->pid);
1143 DIR *fds = opendir(dirname);
1147 char fdname[FILENAME_MAX + 1];
1148 char linkname[FILENAME_MAX + 1];
1150 // make the array negative
1151 for(c = 0 ; c < p->fds_size ; c++)
1152 p->fds[c] = -p->fds[c];
1154 while((de = readdir(fds))) {
1155 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1158 // check if the fds array is small
1159 int fdid = atoi(de->d_name);
1160 if(fdid < 0) continue;
1161 if(fdid >= p->fds_size) {
1162 // it is small, extend it
1164 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1166 p->fds = reallocz(p->fds, (fdid + 100) * sizeof(int));
1168 fatal("Cannot re-allocate fds for %s", p->comm);
1172 // and initialize it
1173 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1174 p->fds_size = fdid + 100;
1177 if(p->fds[fdid] == 0) {
1178 // we don't know this fd, get it
1180 sprintf(fdname, "%s/proc/%d/fd/%s", global_host_prefix, p->pid, de->d_name);
1181 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1183 if(debug || (p->target && p->target->debug)) {
1184 if(debug || (p->target && p->target->debug))
1185 error("Cannot read link %s", fdname);
1192 // if another process already has this, we will get
1194 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1197 // else make it positive again, we need it
1198 // of course, the actual file may have changed, but we don't care so much
1199 // FIXME: we could compare the inode as returned by readdir dirent structure
1200 else p->fds[fdid] = -p->fds[fdid];
1204 // remove all the negative file descriptors
1205 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1206 file_descriptor_not_used(-p->fds[c]);
1215 // ----------------------------------------------------------------------------
1217 int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1218 char *prefix = "\\_ ";
1222 indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1226 char buffer[indent + 1];
1229 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1232 fprintf(stderr, " %s %s%s (%d %s %lld"
1237 , p->updated?"running":"exited"
1238 , (long long)p->stat_collected_usec - (long long)time
1241 if(p->utime) fprintf(stderr, " utime=%llu", p->utime);
1242 if(p->stime) fprintf(stderr, " stime=%llu", p->stime);
1243 if(p->gtime) fprintf(stderr, " gtime=%llu", p->gtime);
1244 if(p->cutime) fprintf(stderr, " cutime=%llu", p->cutime);
1245 if(p->cstime) fprintf(stderr, " cstime=%llu", p->cstime);
1246 if(p->cgtime) fprintf(stderr, " cgtime=%llu", p->cgtime);
1247 if(p->minflt) fprintf(stderr, " minflt=%llu", p->minflt);
1248 if(p->cminflt) fprintf(stderr, " cminflt=%llu", p->cminflt);
1249 if(p->majflt) fprintf(stderr, " majflt=%llu", p->majflt);
1250 if(p->cmajflt) fprintf(stderr, " cmajflt=%llu", p->cmajflt);
1251 fprintf(stderr, ")\n");
1256 void print_process_tree(struct pid_stat *p, char *msg) {
1258 fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1259 print_process_and_parents(p, p->stat_collected_usec);
1262 void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1264 struct pid_stat *p = NULL;
1266 for(p = root_of_pids; p ; p = p->next) {
1267 if(p == pe) continue;
1271 if(p->cminflt > lost) {
1272 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);
1278 if(p->cmajflt > lost) {
1279 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);
1285 if(p->cutime > lost) {
1286 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);
1292 if(p->cstime > lost) {
1293 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);
1299 if(p->cgtime > lost) {
1300 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);
1310 fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1314 fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1318 fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1322 fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1326 fprintf(stderr, " > cannot find any process to use the lost exited child gtime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1332 unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1333 unsigned long long absorbed = 0;
1335 if(*field > *pfield) {
1336 absorbed += *pfield;
1349 void process_exited_processes() {
1352 for(p = root_of_pids; p ; p = p->next) {
1353 if(p->updated || !p->stat_collected_usec)
1356 struct pid_stat *pp = p->parent;
1358 unsigned long long utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1359 unsigned long long stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1360 unsigned long long gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1361 unsigned long long minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1362 unsigned long long majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1364 if(utime + stime + gtime + minflt + majflt == 0)
1367 if(unlikely(debug)) {
1369 fprintf(stderr, "Absorb %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1372 , p->updated?"running":"exited"
1379 print_process_tree(p, "Searching parents");
1382 for(pp = p->parent; pp ; pp = pp->parent) {
1383 if(!pp->updated) continue;
1385 unsigned long long absorbed;
1386 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1387 if(unlikely(debug && absorbed))
1388 fprintf(stderr, " > process %s (%d %s) absorbed %llu utime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1390 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1391 if(unlikely(debug && absorbed))
1392 fprintf(stderr, " > process %s (%d %s) absorbed %llu stime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1394 absorbed = remove_exited_child_from_parent(>ime, &pp->cgtime);
1395 if(unlikely(debug && absorbed))
1396 fprintf(stderr, " > process %s (%d %s) absorbed %llu gtime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1398 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1399 if(unlikely(debug && absorbed))
1400 fprintf(stderr, " > process %s (%d %s) absorbed %llu minflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1402 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1403 if(unlikely(debug && absorbed))
1404 fprintf(stderr, " > process %s (%d %s) absorbed %llu majflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1407 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1408 if(unlikely(debug)) {
1409 if(utime) find_lost_child_debug(p, utime, 3);
1410 if(stime) find_lost_child_debug(p, stime, 4);
1411 if(gtime) find_lost_child_debug(p, gtime, 5);
1412 if(minflt) find_lost_child_debug(p, minflt, 1);
1413 if(majflt) find_lost_child_debug(p, majflt, 2);
1419 fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1422 , p->updated?"running":"exited"
1430 for(pp = p->parent; pp ; pp = pp->parent) {
1431 if(pp->updated) break;
1435 fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
1438 , pp->updated?"running":"exited"
1442 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1443 p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1444 p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1445 p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1446 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1447 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
1450 fprintf(stderr, "\n");
1452 else if(unlikely(debug)) {
1453 fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
1456 , p->updated?"running":"exited"
1462 void link_all_processes_to_their_parents(void) {
1463 struct pid_stat *p, *pp;
1465 // link all children to their parents
1466 // and update children count on parents
1467 for(p = root_of_pids; p ; p = p->next) {
1468 // for each process found
1473 if(unlikely(!p->ppid)) {
1478 pp = all_pids[p->ppid];
1481 pp->children_count++;
1483 if(unlikely(debug || (p->target && p->target->debug)))
1484 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);
1488 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1493 // ----------------------------------------------------------------------------
1495 // 1. read all files in /proc
1496 // 2. for each numeric directory:
1497 // i. read /proc/pid/stat
1498 // ii. read /proc/pid/statm
1499 // iii. read /proc/pid/io (requires root access)
1500 // iii. read the entries in directory /proc/pid/fd (requires root access)
1502 // a. find or create a struct file_descriptor
1503 // b. cleanup any old/unused file_descriptors
1505 // after all these, some pids may be linked to targets, while others may not
1507 // in case of errors, only 1 every 1000 errors is printed
1508 // to avoid filling up all disk space
1509 // if debug is enabled, all errors are printed
1511 static int compar_pid(const void *pid1, const void *pid2) {
1513 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1514 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1516 if(p1->sortlist > p2->sortlist)
1522 static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
1523 if(unlikely(!status)) {
1524 // error("command failed log %u, errno %d", log, errno);
1526 if(unlikely(debug || errno != ENOENT)) {
1527 if(unlikely(debug || !(p->log_thrown & log))) {
1528 p->log_thrown |= log;
1531 error("Cannot process %s/proc/%d/io (command '%s')", global_host_prefix, p->pid, p->comm);
1535 error("Cannot process %s/proc/%d/statm (command '%s')", global_host_prefix, p->pid, p->comm);
1538 case PID_LOG_CMDLINE:
1539 error("Cannot process %s/proc/%d/cmdline (command '%s')", global_host_prefix, p->pid, p->comm);
1543 error("Cannot process entries in %s/proc/%d/fd (command '%s')", global_host_prefix, p->pid, p->comm);
1550 error("unhandled error for pid %d, command '%s'", p->pid, p->comm);
1557 else if(unlikely(p->log_thrown & log)) {
1558 // error("unsetting log %u on pid %d", log, p->pid);
1559 p->log_thrown &= ~log;
1565 void collect_data_for_pid(pid_t pid) {
1566 if(unlikely(pid <= 0 || pid > pid_max)) {
1567 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1571 struct pid_stat *p = get_pid_entry(pid);
1572 if(unlikely(!p || p->read)) return;
1575 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1577 // --------------------------------------------------------------------
1580 if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p))))
1581 // there is no reason to proceed if we cannot get its status
1584 read_proc_pid_ownership(p);
1586 // check its parent pid
1587 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1588 error("Pid %d (command '%s') states invalid parent pid %d. Using 0.", pid, p->comm, p->ppid);
1592 // --------------------------------------------------------------------
1595 managed_log(p, PID_LOG_IO, read_proc_pid_io(p));
1597 // --------------------------------------------------------------------
1598 // /proc/<pid>/statm
1600 if(unlikely(!managed_log(p, PID_LOG_STATM, read_proc_pid_statm(p))))
1601 // there is no reason to proceed if we cannot get its memory status
1604 // --------------------------------------------------------------------
1607 // check if it is target
1608 // we do this only once, the first time this pid is loaded
1609 if(unlikely(p->new_entry)) {
1610 // /proc/<pid>/cmdline
1611 if(likely(proc_pid_cmdline_is_needed))
1612 managed_log(p, PID_LOG_CMDLINE, read_proc_pid_cmdline(p));
1615 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1617 uint32_t hash = simple_hash(p->comm);
1618 size_t pclen = strlen(p->comm);
1621 for(w = apps_groups_root_target; w ; w = w->next) {
1622 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1624 // find it - 4 cases:
1625 // 1. the target is not a pattern
1626 // 2. the target has the prefix
1627 // 3. the target has the suffix
1628 // 4. the target is something inside cmdline
1629 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1630 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1631 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1632 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1634 if(w->target) p->target = w->target;
1637 if(debug || (p->target && p->target->debug))
1638 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1645 // --------------------------------------------------------------------
1648 if(enable_file_charts)
1649 managed_log(p, PID_LOG_FDS, read_pid_file_descriptors(p));
1651 // --------------------------------------------------------------------
1654 if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1655 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);
1657 // mark it as updated
1663 int collect_data_for_all_processes_from_proc(void) {
1664 struct pid_stat *p = NULL;
1666 if(all_pids_count) {
1667 // read parents before childs
1668 // this is needed to prevent a situation where
1669 // a child is found running, but until we read
1670 // its parent, it has exited and its parent
1671 // has accumulated its resources
1674 for(p = root_of_pids; p ; p = p->next) {
1679 p->children_count = 0;
1682 all_pids_sortlist[slc++] = p->pid;
1685 if(unlikely(slc != all_pids_count)) {
1686 error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1687 all_pids_count = slc;
1690 if(include_exited_childs) {
1691 qsort((void *)all_pids_sortlist, all_pids_count, sizeof(pid_t), compar_pid);
1692 for(slc = 0; slc < all_pids_count; slc++)
1693 collect_data_for_pid(all_pids_sortlist[slc]);
1697 char dirname[FILENAME_MAX + 1];
1699 snprintfz(dirname, FILENAME_MAX, "%s/proc", global_host_prefix);
1700 DIR *dir = opendir(dirname);
1703 struct dirent *file = NULL;
1705 while((file = readdir(dir))) {
1706 char *endptr = file->d_name;
1707 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1709 // make sure we read a valid number
1710 if(unlikely(endptr == file->d_name || *endptr != '\0'))
1713 collect_data_for_pid(pid);
1717 // normally this is done
1718 // however we may have processes exited while we collected values
1719 // so let's find the exited ones
1720 // we do this by collecting the ownership of process
1721 // if we manage to get the ownership, the process still runs
1724 link_all_processes_to_their_parents();
1725 process_exited_processes();
1730 // ----------------------------------------------------------------------------
1731 // update statistics on the targets
1733 // 1. link all childs to their parents
1734 // 2. go from bottom to top, marking as merged all childs to their parents
1735 // this step links all parents without a target to the child target, if any
1736 // 3. link all top level processes (the ones not merged) to the default target
1737 // 4. go from top to bottom, linking all childs without a target, to their parent target
1738 // after this step, all processes have a target
1739 // [5. for each killed pid (updated = 0), remove its usage from its target]
1740 // 6. zero all apps_groups_targets
1741 // 7. concentrate all values on the apps_groups_targets
1742 // 8. remove all killed processes
1743 // 9. find the unique file count for each target
1744 // check: update_apps_groups_statistics()
1746 void cleanup_exited_pids(void) {
1748 struct pid_stat *p = NULL;
1750 for(p = root_of_pids; p ;) {
1751 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1752 // 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);
1754 if(unlikely(debug && (p->keep || p->keeploops)))
1755 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1757 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1758 file_descriptor_not_used(p->fds[c]);
1767 if(unlikely(p->keep)) p->keeploops++;
1774 void apply_apps_groups_targets_inheritance(void) {
1775 struct pid_stat *p = NULL;
1777 // children that do not have a target
1778 // inherit their target from their parent
1779 int found = 1, loops = 0;
1781 if(unlikely(debug)) loops++;
1783 for(p = root_of_pids; p ; p = p->next) {
1784 // if this process does not have a target
1785 // and it has a parent
1786 // and its parent has a target
1787 // then, set the parent's target to this process
1788 if(unlikely(!p->target && p->parent && p->parent->target)) {
1789 p->target = p->parent->target;
1792 if(debug || (p->target && p->target->debug))
1793 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);
1798 // find all the procs with 0 childs and merge them to their parents
1799 // repeat, until nothing more can be done.
1803 if(unlikely(debug)) loops++;
1806 for(p = root_of_pids; p ; p = p->next) {
1807 if(unlikely(!p->sortlist && !p->children_count))
1808 p->sortlist = sortlist++;
1810 // if this process does not have any children
1811 // and is not already merged
1813 // and its parent has children
1814 // and the target of this process and its parent is the same, or the parent does not have a target
1815 // and its parent is not init
1816 // then, mark them as merged.
1821 && p->parent->children_count
1822 && (p->target == p->parent->target || !p->parent->target)
1825 p->parent->children_count--;
1828 // the parent inherits the child's target, if it does not have a target itself
1829 if(unlikely(p->target && !p->parent->target)) {
1830 p->parent->target = p->target;
1832 if(debug || (p->target && p->target->debug))
1833 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);
1841 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
1844 // init goes always to default target
1846 all_pids[1]->target = apps_groups_default_target;
1848 // give a default target on all top level processes
1849 if(unlikely(debug)) loops++;
1850 for(p = root_of_pids; p ; p = p->next) {
1851 // if the process is not merged itself
1852 // then is is a top level process
1853 if(unlikely(!p->merged && !p->target))
1854 p->target = apps_groups_default_target;
1856 // make sure all processes have a sortlist
1857 if(unlikely(!p->sortlist))
1858 p->sortlist = sortlist++;
1862 all_pids[1]->sortlist = sortlist++;
1864 // give a target to all merged child processes
1867 if(unlikely(debug)) loops++;
1869 for(p = root_of_pids; p ; p = p->next) {
1870 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1871 p->target = p->parent->target;
1874 if(debug || (p->target && p->target->debug))
1875 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);
1881 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1884 long zero_all_targets(struct target *root) {
1888 for (w = root; w ; w = w->next) {
1891 if(w->fds) freez(w->fds);
1909 w->statm_resident = 0;
1916 w->io_logical_bytes_read = 0;
1917 w->io_logical_bytes_written = 0;
1918 w->io_read_calls = 0;
1919 w->io_write_calls = 0;
1920 w->io_storage_bytes_read = 0;
1921 w->io_storage_bytes_written = 0;
1922 w->io_cancelled_write_bytes = 0;
1928 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1931 if(unlikely(!w->fds))
1932 w->fds = callocz(sizeof(int), (size_t) all_files_size);
1934 if(likely(p->updated)) {
1935 w->cutime += p->cutime;
1936 w->cstime += p->cstime;
1937 w->cgtime += p->cgtime;
1938 w->cminflt += p->cminflt;
1939 w->cmajflt += p->cmajflt;
1941 w->utime += p->utime;
1942 w->stime += p->stime;
1943 w->gtime += p->gtime;
1944 w->minflt += p->minflt;
1945 w->majflt += p->majflt;
1949 w->statm_size += p->statm_size;
1950 w->statm_resident += p->statm_resident;
1951 w->statm_share += p->statm_share;
1952 w->statm_text += p->statm_text;
1953 w->statm_lib += p->statm_lib;
1954 w->statm_data += p->statm_data;
1955 w->statm_dirty += p->statm_dirty;
1957 w->io_logical_bytes_read += p->io_logical_bytes_read;
1958 w->io_logical_bytes_written += p->io_logical_bytes_written;
1959 w->io_read_calls += p->io_read_calls;
1960 w->io_write_calls += p->io_write_calls;
1961 w->io_storage_bytes_read += p->io_storage_bytes_read;
1962 w->io_storage_bytes_written += p->io_storage_bytes_written;
1963 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1966 w->num_threads += p->num_threads;
1968 if(likely(w->fds)) {
1970 for(c = 0; c < p->fds_size ;c++) {
1971 if(p->fds[c] == 0) continue;
1973 if(likely(p->fds[c] < all_files_size)) {
1974 if(w->fds) w->fds[p->fds[c]]++;
1977 error("Invalid fd number %d", p->fds[c]);
1981 if(unlikely(debug || w->debug))
1982 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);
1986 void count_targets_fds(struct target *root) {
1990 for (w = root; w ; w = w->next) {
1991 if(!w->fds) continue;
1996 w->openinotifies = 0;
1997 w->openeventfds = 0;
1998 w->opentimerfds = 0;
1999 w->opensignalfds = 0;
2000 w->openeventpolls = 0;
2003 for(c = 1; c < all_files_size ;c++) {
2005 switch(all_files[c].type) {
2014 case FILETYPE_SOCKET:
2018 case FILETYPE_INOTIFY:
2022 case FILETYPE_EVENTFD:
2026 case FILETYPE_TIMERFD:
2030 case FILETYPE_SIGNALFD:
2034 case FILETYPE_EVENTPOLL:
2035 w->openeventpolls++;
2048 void calculate_netdata_statistics(void) {
2049 apply_apps_groups_targets_inheritance();
2051 zero_all_targets(users_root_target);
2052 zero_all_targets(groups_root_target);
2053 apps_groups_targets = zero_all_targets(apps_groups_root_target);
2055 // this has to be done, before the cleanup
2056 struct pid_stat *p = NULL;
2057 struct target *w = NULL, *o = NULL;
2059 // concentrate everything on the apps_groups_targets
2060 for(p = root_of_pids; p ; p = p->next) {
2062 // --------------------------------------------------------------------
2063 // apps_groups targets
2064 if(likely(p->target))
2065 aggregate_pid_on_target(p->target, p, NULL);
2067 error("pid %d %s was left without a target!", p->pid, p->comm);
2070 // --------------------------------------------------------------------
2073 if(likely(p->user_target && p->user_target->uid == p->uid))
2076 if(unlikely(debug && p->user_target))
2077 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);
2079 w = p->user_target = get_users_target(p->uid);
2083 aggregate_pid_on_target(w, p, o);
2085 error("pid %d %s was left without a user target!", p->pid, p->comm);
2088 // --------------------------------------------------------------------
2090 o = p->group_target;
2091 if(likely(p->group_target && p->group_target->gid == p->gid))
2092 w = p->group_target;
2094 if(unlikely(debug && p->group_target))
2095 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);
2097 w = p->group_target = get_groups_target(p->gid);
2101 aggregate_pid_on_target(w, p, o);
2103 error("pid %d %s was left without a group target!", p->pid, p->comm);
2107 count_targets_fds(apps_groups_root_target);
2108 count_targets_fds(users_root_target);
2109 count_targets_fds(groups_root_target);
2111 cleanup_exited_pids();
2114 // ----------------------------------------------------------------------------
2115 // update chart dimensions
2117 BUFFER *output = NULL;
2118 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2120 static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2121 // fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2122 buffer_strcat(output, "BEGIN ");
2123 buffer_strcat(output, type);
2124 buffer_strcat(output, ".");
2125 buffer_strcat(output, id);
2126 buffer_strcat(output, " ");
2127 buffer_print_llu(output, usec);
2128 buffer_strcat(output, "\n");
2131 static inline void send_SET(const char *name, unsigned long long value) {
2132 // fprintf(stdout, "SET %s = %llu\n", name, value);
2133 buffer_strcat(output, "SET ");
2134 buffer_strcat(output, name);
2135 buffer_strcat(output, " = ");
2136 buffer_print_llu(output, value);
2137 buffer_strcat(output, "\n");
2140 static inline void send_END(void) {
2141 // fprintf(stdout, "END\n");
2142 buffer_strcat(output, "END\n");
2145 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;
2146 double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2148 unsigned long long send_resource_usage_to_netdata() {
2149 static struct timeval last = { 0, 0 };
2150 static struct rusage me_last;
2155 unsigned long long usec;
2156 unsigned long long cpuuser;
2157 unsigned long long cpusyst;
2160 gettimeofday(&last, NULL);
2161 getrusage(RUSAGE_SELF, &me_last);
2163 // the first time, give a zero to allow
2164 // netdata calibrate to the current time
2165 // usec = update_every * 1000000ULL;
2171 gettimeofday(&now, NULL);
2172 getrusage(RUSAGE_SELF, &me);
2174 usec = usec_dt(&now, &last);
2175 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2176 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2178 memmove(&last, &now, sizeof(struct timeval));
2179 memmove(&me_last, &me, sizeof(struct rusage));
2182 buffer_sprintf(output,
2183 "BEGIN netdata.apps_cpu %llu\n"
2185 "SET system = %llu\n"
2187 "BEGIN netdata.apps_files %llu\n"
2188 "SET files = %llu\n"
2191 "SET targets = %ld\n"
2193 "BEGIN netdata.apps_fix %llu\n"
2194 "SET utime = %llu\n"
2195 "SET stime = %llu\n"
2196 "SET gtime = %llu\n"
2197 "SET minflt = %llu\n"
2198 "SET majflt = %llu\n"
2207 , apps_groups_targets
2209 , (unsigned long long)(utime_fix_ratio * 100 * RATES_DETAIL)
2210 , (unsigned long long)(stime_fix_ratio * 100 * RATES_DETAIL)
2211 , (unsigned long long)(gtime_fix_ratio * 100 * RATES_DETAIL)
2212 , (unsigned long long)(minflt_fix_ratio * 100 * RATES_DETAIL)
2213 , (unsigned long long)(majflt_fix_ratio * 100 * RATES_DETAIL)
2216 if(include_exited_childs)
2217 buffer_sprintf(output,
2218 "BEGIN netdata.apps_children_fix %llu\n"
2219 "SET cutime = %llu\n"
2220 "SET cstime = %llu\n"
2221 "SET cgtime = %llu\n"
2222 "SET cminflt = %llu\n"
2223 "SET cmajflt = %llu\n"
2226 , (unsigned long long)(cutime_fix_ratio * 100 * RATES_DETAIL)
2227 , (unsigned long long)(cstime_fix_ratio * 100 * RATES_DETAIL)
2228 , (unsigned long long)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2229 , (unsigned long long)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2230 , (unsigned long long)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2236 void normalize_data(struct target *root) {
2239 // childs processing introduces spikes
2240 // here we try to eliminate them by disabling childs processing either for specific dimensions
2241 // or entirely. Of course, either way, we disable it just a single iteration.
2243 unsigned long long max = processors * hz * RATES_DETAIL;
2244 unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2246 if(global_utime > max) global_utime = max;
2247 if(global_stime > max) global_stime = max;
2248 if(global_gtime > max) global_gtime = max;
2250 for(w = root; w ; w = w->next) {
2251 if(w->target || (!w->processes && !w->exposed)) continue;
2256 cutime += w->cutime;
2257 cstime += w->cstime;
2258 cgtime += w->cgtime;
2260 minflt += w->minflt;
2261 majflt += w->majflt;
2262 cminflt += w->cminflt;
2263 cmajflt += w->cmajflt;
2266 if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
2267 if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
2268 // everything we collected fits
2274 cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
2276 else if(global_utime + global_stime > utime + stime) {
2277 // childrens resources are too high
2278 // lower only the children resources
2281 gtime_fix_ratio = 1.0;
2284 cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
2287 // even running processes are unrealistic
2288 // zero the children resources
2289 // lower the running processes resources
2292 gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
2295 cgtime_fix_ratio = 0.0;
2304 cgtime_fix_ratio = 0.0;
2307 if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
2308 if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
2309 if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
2310 if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
2311 if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
2312 if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
2314 // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
2315 // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
2316 // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
2317 // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
2318 // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
2319 // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
2322 // we use cpu time to normalize page faults
2323 // the problem is that to find the proper max values
2324 // for page faults we have to parse /proc/vmstat
2325 // which is quite big to do it again (netdata does it already)
2327 // a better solution could be to somehow have netdata
2328 // do this normalization for us
2330 if(utime || stime || gtime)
2332 minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
2335 majflt_fix_ratio = 1.0;
2337 if(cutime || cstime || cgtime)
2339 cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
2342 cmajflt_fix_ratio = 1.0;
2346 if(unlikely(debug)) {
2348 "SYSTEM: u=%llu s=%llu g=%llu "
2349 "COLLECTED: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2350 "DELTA: u=%lld s=%lld g=%lld "
2351 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2352 "FINALLY: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2363 , (long long)utime + (long long)cutime - (long long)global_utime
2364 , (long long)stime + (long long)cstime - (long long)global_stime
2365 , (long long)gtime + (long long)cgtime - (long long)global_gtime
2372 , (unsigned long long)(utime * utime_fix_ratio)
2373 , (unsigned long long)(stime * stime_fix_ratio)
2374 , (unsigned long long)(gtime * gtime_fix_ratio)
2375 , (unsigned long long)(cutime * cutime_fix_ratio)
2376 , (unsigned long long)(cstime * cstime_fix_ratio)
2377 , (unsigned long long)(cgtime * cgtime_fix_ratio)
2382 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec) {
2385 send_BEGIN(type, "cpu", usec);
2386 for (w = root; w ; w = w->next) {
2387 if(unlikely(w->exposed))
2388 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));
2392 send_BEGIN(type, "cpu_user", usec);
2393 for (w = root; w ; w = w->next) {
2394 if(unlikely(w->exposed))
2395 send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2399 send_BEGIN(type, "cpu_system", usec);
2400 for (w = root; w ; w = w->next) {
2401 if(unlikely(w->exposed))
2402 send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2406 if(show_guest_time) {
2407 send_BEGIN(type, "cpu_guest", usec);
2408 for (w = root; w ; w = w->next) {
2409 if(unlikely(w->exposed))
2410 send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2415 send_BEGIN(type, "threads", usec);
2416 for (w = root; w ; w = w->next) {
2417 if(unlikely(w->exposed))
2418 send_SET(w->name, w->num_threads);
2422 send_BEGIN(type, "processes", usec);
2423 for (w = root; w ; w = w->next) {
2424 if(unlikely(w->exposed))
2425 send_SET(w->name, w->processes);
2429 send_BEGIN(type, "mem", usec);
2430 for (w = root; w ; w = w->next) {
2431 if(unlikely(w->exposed))
2432 send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2436 send_BEGIN(type, "minor_faults", usec);
2437 for (w = root; w ; w = w->next) {
2438 if(unlikely(w->exposed))
2439 send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2443 send_BEGIN(type, "major_faults", usec);
2444 for (w = root; w ; w = w->next) {
2445 if(unlikely(w->exposed))
2446 send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2450 send_BEGIN(type, "lreads", usec);
2451 for (w = root; w ; w = w->next) {
2452 if(unlikely(w->exposed))
2453 send_SET(w->name, w->io_logical_bytes_read);
2457 send_BEGIN(type, "lwrites", usec);
2458 for (w = root; w ; w = w->next) {
2459 if(unlikely(w->exposed))
2460 send_SET(w->name, w->io_logical_bytes_written);
2464 send_BEGIN(type, "preads", usec);
2465 for (w = root; w ; w = w->next) {
2466 if(unlikely(w->exposed))
2467 send_SET(w->name, w->io_storage_bytes_read);
2471 send_BEGIN(type, "pwrites", usec);
2472 for (w = root; w ; w = w->next) {
2473 if(unlikely(w->exposed))
2474 send_SET(w->name, w->io_storage_bytes_written);
2478 if(enable_file_charts) {
2479 send_BEGIN(type, "files", usec);
2480 for (w = root; w; w = w->next) {
2481 if (unlikely(w->exposed))
2482 send_SET(w->name, w->openfiles);
2486 send_BEGIN(type, "sockets", usec);
2487 for (w = root; w; w = w->next) {
2488 if (unlikely(w->exposed))
2489 send_SET(w->name, w->opensockets);
2493 send_BEGIN(type, "pipes", usec);
2494 for (w = root; w; w = w->next) {
2495 if (unlikely(w->exposed))
2496 send_SET(w->name, w->openpipes);
2503 // ----------------------------------------------------------------------------
2504 // generate the charts
2506 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2509 int newly_added = 0;
2511 for(w = root ; w ; w = w->next) {
2512 if (w->target) continue;
2514 if (!w->exposed && w->processes) {
2517 if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2521 // nothing more to show
2522 if(!newly_added && show_guest_time == show_guest_time_old) return;
2524 // we have something new to show
2525 // update the charts
2526 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);
2527 for (w = root; w ; w = w->next) {
2528 if(unlikely(w->exposed))
2529 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2532 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);
2533 for (w = root; w ; w = w->next) {
2534 if(unlikely(w->exposed))
2535 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2538 buffer_sprintf(output, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2539 for (w = root; w ; w = w->next) {
2540 if(unlikely(w->exposed))
2541 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2544 buffer_sprintf(output, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2545 for (w = root; w ; w = w->next) {
2546 if(unlikely(w->exposed))
2547 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2550 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);
2551 for (w = root; w ; w = w->next) {
2552 if(unlikely(w->exposed))
2553 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2556 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);
2557 for (w = root; w ; w = w->next) {
2558 if(unlikely(w->exposed))
2559 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2562 if(show_guest_time) {
2563 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);
2564 for (w = root; w; w = w->next) {
2565 if(unlikely(w->exposed))
2566 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2570 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);
2571 for (w = root; w ; w = w->next) {
2572 if(unlikely(w->exposed))
2573 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2576 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);
2577 for (w = root; w ; w = w->next) {
2578 if(unlikely(w->exposed))
2579 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2582 buffer_sprintf(output, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, 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, 1024LLU * RATES_DETAIL);
2588 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);
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, 1024LLU * RATES_DETAIL);
2594 buffer_sprintf(output, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2595 for (w = root; w ; w = w->next) {
2596 if(unlikely(w->exposed))
2597 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2600 buffer_sprintf(output, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2601 for (w = root; w ; w = w->next) {
2602 if(unlikely(w->exposed))
2603 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2606 if(enable_file_charts) {
2607 buffer_sprintf(output, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type,
2608 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 1\n", w->name);
2614 buffer_sprintf(output, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n",
2615 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 1\n", w->name);
2621 buffer_sprintf(output, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type,
2622 title, type, update_every);
2623 for (w = root; w; w = w->next) {
2624 if (unlikely(w->exposed))
2625 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2631 // ----------------------------------------------------------------------------
2632 // parse command line arguments
2634 void parse_args(int argc, char **argv)
2639 for(i = 1; i < argc; i++) {
2641 int n = atoi(argv[i]);
2648 if(strcmp("debug", argv[i]) == 0) {
2650 // debug_flags = 0xffffffff;
2654 if(strcmp("no-childs", argv[i]) == 0 || strcmp("without-childs", argv[i]) == 0) {
2655 include_exited_childs = 0;
2659 if(strcmp("with-childs", argv[i]) == 0) {
2660 include_exited_childs = 1;
2664 if(strcmp("with-guest", argv[i]) == 0) {
2665 enable_guest_charts = 1;
2669 if(strcmp("no-guest", argv[i]) == 0 || strcmp("without-guest", argv[i]) == 0) {
2670 enable_guest_charts = 0;
2674 if(strcmp("with-files", argv[i]) == 0) {
2675 enable_file_charts = 1;
2679 if(strcmp("no-files", argv[i]) == 0 || strcmp("without-files", argv[i]) == 0) {
2680 enable_file_charts = 0;
2684 if(strcmp("-h", argv[i]) == 0 || strcmp("--help", argv[i]) == 0) {
2687 "(C) 2016 Costa Tsaousis"
2689 "This program is a data collector plugin for netdata.\n"
2691 "Valid command line options:\n"
2693 "SECONDS set the data collection frequency\n"
2695 "debug enable debugging (lot of output)\n"
2698 "without-childs enable / disable aggregating exited\n"
2699 " children resources into parents\n"
2700 " (default is enabled)\n"
2703 "without-guest enable / disable reporting guest charts\n"
2704 " (default is disabled)\n"
2707 "without-files enable / disable reporting files, sockets, pipes\n"
2708 " (default is enabled)\n"
2710 "NAME read apps_NAME.conf instead of\n"
2711 " apps_groups.conf\n"
2712 " (default NAME=groups)\n"
2722 error("Cannot understand option %s", argv[i]);
2726 if(freq > 0) update_every = freq;
2727 if(!name) name = "groups";
2729 if(read_apps_groups_conf(name)) {
2730 error("Cannot read process groups %s", name);
2735 int main(int argc, char **argv)
2737 // debug_flags = D_PROCFILE;
2739 // set the name for logging
2740 program_name = "apps.plugin";
2742 info("started on pid %d", getpid());
2744 // disable syslog for apps.plugin
2745 error_log_syslog = 0;
2747 // set errors flood protection to 100 logs per hour
2748 error_log_errors_per_period = 100;
2749 error_log_throttle_period = 3600;
2751 global_host_prefix = getenv("NETDATA_HOST_PREFIX");
2752 if(global_host_prefix == NULL) {
2753 // info("NETDATA_HOST_PREFIX is not passed from netdata");
2754 global_host_prefix = "";
2756 // else info("Found NETDATA_HOST_PREFIX='%s'", global_host_prefix);
2758 config_dir = getenv("NETDATA_CONFIG_DIR");
2759 if(config_dir == NULL) {
2760 // info("NETDATA_CONFIG_DIR is not passed from netdata");
2761 config_dir = CONFIG_DIR;
2763 // else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2765 #ifdef NETDATA_INTERNAL_CHECKS
2766 if(debug_flags != 0) {
2767 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2768 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2769 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2770 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2772 #endif /* NETDATA_INTERNAL_CHECKS */
2774 procfile_adaptive_initial_allocation = 1;
2776 time_t started_t = time(NULL);
2779 get_system_pid_max();
2782 parse_args(argc, argv);
2784 all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
2785 all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
2787 output = buffer_create(1024);
2788 buffer_sprintf(output,
2789 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2790 "DIMENSION user '' incremental 1 1000\n"
2791 "DIMENSION system '' incremental 1 1000\n"
2792 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2793 "DIMENSION files '' incremental 1 1\n"
2794 "DIMENSION pids '' absolute 1 1\n"
2795 "DIMENSION fds '' absolute 1 1\n"
2796 "DIMENSION targets '' absolute 1 1\n"
2797 "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
2798 "DIMENSION utime '' absolute 1 %2$llu\n"
2799 "DIMENSION stime '' absolute 1 %2$llu\n"
2800 "DIMENSION gtime '' absolute 1 %2$llu\n"
2801 "DIMENSION minflt '' absolute 1 %2$llu\n"
2802 "DIMENSION majflt '' absolute 1 %2$llu\n"
2807 if(include_exited_childs)
2808 buffer_sprintf(output,
2809 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
2810 "DIMENSION cutime '' absolute 1 %2$llu\n"
2811 "DIMENSION cstime '' absolute 1 %2$llu\n"
2812 "DIMENSION cgtime '' absolute 1 %2$llu\n"
2813 "DIMENSION cminflt '' absolute 1 %2$llu\n"
2814 "DIMENSION cmajflt '' absolute 1 %2$llu\n"
2819 #ifndef PROFILING_MODE
2820 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2821 unsigned long long sunow;
2822 #endif /* PROFILING_MODE */
2824 global_iterations_counter = 1;
2825 for(;1; global_iterations_counter++) {
2826 #ifndef PROFILING_MODE
2827 // delay until it is our time to run
2828 while((sunow = time_usec()) < sunext)
2829 sleep_usec(sunext - sunow);
2831 // find the next time we need to run
2832 while(time_usec() > sunext)
2833 sunext += update_every * 1000000ULL;
2834 #endif /* PROFILING_MODE */
2836 if(!collect_data_for_all_processes_from_proc()) {
2837 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2838 printf("DISABLE\n");
2842 calculate_netdata_statistics();
2843 normalize_data(apps_groups_root_target);
2845 unsigned long long dt = send_resource_usage_to_netdata();
2847 // this is smart enough to show only newly added apps, when needed
2848 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2849 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2850 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2852 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2853 send_collected_data_to_netdata(users_root_target, "users", dt);
2854 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2856 show_guest_time_old = show_guest_time;
2858 //if(puts(buffer_tostring(output)) == EOF)
2859 if(write(STDOUT_FILENO, buffer_tostring(output), buffer_strlen(output)) == -1)
2860 fatal("Cannot send chart values to netdata.");
2863 buffer_flush(output);
2866 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2868 current_t = time(NULL);
2870 #ifndef PROFILING_MODE
2871 // restart check (14400 seconds)
2872 if(current_t - started_t > 14400) exit(0);
2874 if(current_t - started_t > 10) exit(0);
2875 #endif /* PROFILING_MODE */