14 #include <sys/resource.h>
25 #include <arpa/inet.h>
27 #include <sys/types.h>
36 #include "../config.h"
37 #include "web_buffer.h"
39 #ifdef NETDATA_INTERNAL_CHECKS
40 #include <sys/prctl.h>
43 #define MAX_COMPARE_NAME 100
45 #define MAX_CMDLINE 1024
47 // the rates we are going to send to netdata
48 // will have this detail
50 // 1 will send just integer parts to netdata
51 // 100 will send 2 decimal points
52 // 1000 will send 3 decimal points
54 #define RATES_DETAIL 10000ULL
57 pid_t pid_max = 32768;
61 unsigned long long global_iterations_counter = 1;
62 unsigned long long file_counter = 0;
63 int proc_pid_cmdline_is_needed = 0;
64 int include_exited_childs = 1;
65 char *host_prefix = "";
66 char *config_dir = CONFIG_DIR;
68 pid_t *all_pids_sortlist = NULL;
70 // will be automatically set to 1, if guest values are collected
71 int show_guest_time = 0;
72 int show_guest_time_old = 0;
74 // ----------------------------------------------------------------------------
76 void netdata_cleanup_and_exit(int ret) {
81 // ----------------------------------------------------------------------------
83 // to retrieve settings of the system
85 long get_system_cpus(void) {
90 char filename[FILENAME_MAX + 1];
91 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
93 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
96 ff = procfile_readall(ff);
103 for(i = 0; i < procfile_lines(ff); i++) {
104 if(!procfile_linewords(ff, i)) continue;
106 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
109 if(processors < 1) processors = 1;
115 pid_t get_system_pid_max(void) {
119 char filename[FILENAME_MAX + 1];
120 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
121 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
124 ff = procfile_readall(ff);
130 mpid = (pid_t)atoi(procfile_lineword(ff, 0, 0));
131 if(!mpid) mpid = 32768;
137 // ----------------------------------------------------------------------------
139 // target is the structure that process data are aggregated
142 char compare[MAX_COMPARE_NAME + 1];
143 uint32_t comparehash;
146 char id[MAX_NAME + 1];
149 char name[MAX_NAME + 1];
154 unsigned long long minflt;
155 unsigned long long cminflt;
156 unsigned long long majflt;
157 unsigned long long cmajflt;
158 unsigned long long utime;
159 unsigned long long stime;
160 unsigned long long gtime;
161 unsigned long long cutime;
162 unsigned long long cstime;
163 unsigned long long cgtime;
164 unsigned long long num_threads;
165 unsigned long long rss;
167 unsigned long long statm_size;
168 unsigned long long statm_resident;
169 unsigned long long statm_share;
170 unsigned long long statm_text;
171 unsigned long long statm_lib;
172 unsigned long long statm_data;
173 unsigned long long statm_dirty;
175 unsigned long long io_logical_bytes_read;
176 unsigned long long io_logical_bytes_written;
177 unsigned long long io_read_calls;
178 unsigned long long io_write_calls;
179 unsigned long long io_storage_bytes_read;
180 unsigned long long io_storage_bytes_written;
181 unsigned long long io_cancelled_write_bytes;
184 unsigned long long openfiles;
185 unsigned long long openpipes;
186 unsigned long long opensockets;
187 unsigned long long openinotifies;
188 unsigned long long openeventfds;
189 unsigned long long opentimerfds;
190 unsigned long long opensignalfds;
191 unsigned long long openeventpolls;
192 unsigned long long openother;
194 unsigned long processes; // how many processes have been merged to this
195 int exposed; // if set, we have sent this to netdata
196 int hidden; // if set, we set the hidden flag on the dimension
199 int starts_with; // if set, the compare string matches only the
200 // beginning of the command
202 struct target *target; // the one that will be reported to netdata
207 // ----------------------------------------------------------------------------
209 // aggregate all processes in groups, to have a limited number of dimensions
211 struct target *apps_groups_root_target = NULL;
212 struct target *apps_groups_default_target = NULL;
213 long apps_groups_targets = 0;
215 struct target *users_root_target = NULL;
216 struct target *groups_root_target = NULL;
218 struct target *get_users_target(uid_t uid)
221 for(w = users_root_target ; w ; w = w->next)
222 if(w->uid == uid) return w;
224 w = calloc(sizeof(struct target), 1);
226 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
230 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
231 w->comparehash = simple_hash(w->compare);
232 w->comparelen = strlen(w->compare);
234 snprintfz(w->id, MAX_NAME, "%u", uid);
235 w->idhash = simple_hash(w->id);
237 struct passwd *pw = getpwuid(uid);
239 snprintfz(w->name, MAX_NAME, "%u", uid);
241 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
243 netdata_fix_chart_name(w->name);
247 w->next = users_root_target;
248 users_root_target = w;
251 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
256 struct target *get_groups_target(gid_t gid)
259 for(w = groups_root_target ; w ; w = w->next)
260 if(w->gid == gid) return w;
262 w = calloc(sizeof(struct target), 1);
264 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
268 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
269 w->comparehash = simple_hash(w->compare);
270 w->comparelen = strlen(w->compare);
272 snprintfz(w->id, MAX_NAME, "%u", gid);
273 w->idhash = simple_hash(w->id);
275 struct group *gr = getgrgid(gid);
277 snprintfz(w->name, MAX_NAME, "%u", gid);
279 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
281 netdata_fix_chart_name(w->name);
285 w->next = groups_root_target;
286 groups_root_target = w;
289 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
294 // find or create a new target
295 // there are targets that are just aggregated to other target (the second argument)
296 struct target *get_apps_groups_target(const char *id, struct target *target)
298 int tdebug = 0, thidden = 0, ends_with = 0;
299 const char *nid = id;
301 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
302 if(nid[0] == '-') thidden = 1;
303 if(nid[0] == '+') tdebug = 1;
304 if(nid[0] == '*') ends_with = 1;
307 uint32_t hash = simple_hash(id);
309 struct target *w, *last = apps_groups_root_target;
310 for(w = apps_groups_root_target ; w ; w = w->next) {
311 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
317 w = calloc(sizeof(struct target), 1);
319 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
323 strncpyz(w->id, nid, MAX_NAME);
324 w->idhash = simple_hash(w->id);
326 strncpyz(w->name, nid, MAX_NAME);
328 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
329 int len = strlen(w->compare);
330 if(w->compare[len - 1] == '*') {
331 w->compare[len - 1] = '\0';
334 w->ends_with = ends_with;
336 if(w->starts_with && w->ends_with)
337 proc_pid_cmdline_is_needed = 1;
339 w->comparehash = simple_hash(w->compare);
340 w->comparelen = strlen(w->compare);
346 // append it, to maintain the order in apps_groups.conf
347 if(last) last->next = w;
348 else apps_groups_root_target = w;
351 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
353 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
354 , w->target?w->target->id:w->id
355 , (w->hidden)?"hidden":"-"
356 , (w->debug)?"debug":"-"
362 // read the apps_groups.conf file
363 int read_apps_groups_conf(const char *name)
365 char filename[FILENAME_MAX + 1];
367 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
370 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
372 // ----------------------------------------
374 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
377 procfile_set_quotes(ff, "'\"");
379 ff = procfile_readall(ff);
385 unsigned long line, lines = procfile_lines(ff);
387 for(line = 0; line < lines ;line++) {
388 unsigned long word, words = procfile_linewords(ff, line);
389 struct target *w = NULL;
391 char *t = procfile_lineword(ff, line, 0);
392 if(!t || !*t) continue;
394 for(word = 0; word < words ;word++) {
395 char *s = procfile_lineword(ff, line, word);
396 if(!s || !*s) continue;
401 struct target *n = get_apps_groups_target(s, w);
403 error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
411 int tdebug = 0, thidden = 0;
413 while(t[0] == '-' || t[0] == '+') {
414 if(t[0] == '-') thidden = 1;
415 if(t[0] == '+') tdebug = 1;
419 strncpyz(w->name, t, MAX_NAME);
424 fprintf(stderr, "apps.plugin: AGGREGATION TARGET NAME '%s' on ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
427 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
428 , w->target?w->target->id:w->id
429 , (w->hidden)?"hidden":"-"
430 , (w->debug)?"debug":"-"
437 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
438 if(!apps_groups_default_target)
439 error("Cannot create default target");
441 strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
447 // ----------------------------------------------------------------------------
448 // data to store for each pid
453 char comm[MAX_COMPARE_NAME + 1];
454 char cmdline[MAX_CMDLINE + 1];
464 // these are raw values collected
465 unsigned long long minflt_raw;
466 unsigned long long cminflt_raw;
467 unsigned long long majflt_raw;
468 unsigned long long cmajflt_raw;
469 unsigned long long utime_raw;
470 unsigned long long stime_raw;
471 unsigned long long gtime_raw; // guest_time
472 unsigned long long cutime_raw;
473 unsigned long long cstime_raw;
474 unsigned long long cgtime_raw; // cguest_time
477 unsigned long long minflt;
478 unsigned long long cminflt;
479 unsigned long long majflt;
480 unsigned long long cmajflt;
481 unsigned long long utime;
482 unsigned long long stime;
483 unsigned long long gtime;
484 unsigned long long cutime;
485 unsigned long long cstime;
486 unsigned long long cgtime;
491 // int64_t itrealvalue;
492 // unsigned long long starttime;
493 // unsigned long long vsize;
494 unsigned long long rss;
495 // unsigned long long rsslim;
496 // unsigned long long starcode;
497 // unsigned long long endcode;
498 // unsigned long long startstack;
499 // unsigned long long kstkesp;
500 // unsigned long long kstkeip;
503 // uint64_t sigignore;
504 // uint64_t sigcatch;
508 // int32_t exit_signal;
509 // int32_t processor;
510 // uint32_t rt_priority;
512 // unsigned long long delayacct_blkio_ticks;
517 unsigned long long statm_size;
518 unsigned long long statm_resident;
519 unsigned long long statm_share;
520 unsigned long long statm_text;
521 unsigned long long statm_lib;
522 unsigned long long statm_data;
523 unsigned long long statm_dirty;
525 unsigned long long io_logical_bytes_read_raw;
526 unsigned long long io_logical_bytes_written_raw;
527 unsigned long long io_read_calls_raw;
528 unsigned long long io_write_calls_raw;
529 unsigned long long io_storage_bytes_read_raw;
530 unsigned long long io_storage_bytes_written_raw;
531 unsigned long long io_cancelled_write_bytes_raw;
533 unsigned long long io_logical_bytes_read;
534 unsigned long long io_logical_bytes_written;
535 unsigned long long io_read_calls;
536 unsigned long long io_write_calls;
537 unsigned long long io_storage_bytes_read;
538 unsigned long long io_storage_bytes_written;
539 unsigned long long io_cancelled_write_bytes;
541 int *fds; // array of fds it uses
542 int fds_size; // the size of the fds array
544 int children_count; // number of processes directly referencing this
545 int keep; // 1 when we need to keep this process in memory even after it exited
546 int keeploops; // increases by 1 every time keep is 1 and updated 0
547 int updated; // 1 when the process is currently running
548 int merged; // 1 when it has been merged to its parent
549 int new_entry; // 1 when this is a new process, just saw for the first time
550 int read; // 1 when we have already read this process for this iteration
551 int sortlist; // higher numbers = top on the process tree
552 // each process gets a unique number
554 struct target *target; // app_groups.conf targets
555 struct target *user_target; // uid based targets
556 struct target *group_target; // gid based targets
558 unsigned long long stat_collected_usec;
559 unsigned long long last_stat_collected_usec;
561 unsigned long long io_collected_usec;
562 unsigned long long last_io_collected_usec;
565 char *statm_filename;
567 char *cmdline_filename;
569 struct pid_stat *parent;
570 struct pid_stat *prev;
571 struct pid_stat *next;
572 } *root_of_pids = NULL, **all_pids;
574 long all_pids_count = 0;
576 struct pid_stat *get_pid_entry(pid_t pid) {
578 all_pids[pid]->new_entry = 0;
579 return all_pids[pid];
582 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
584 error("Cannot allocate %zu bytes of memory", (size_t)sizeof(struct pid_stat));
588 all_pids[pid]->fds = calloc(sizeof(int), 100);
589 if(!all_pids[pid]->fds)
590 error("Cannot allocate %zu bytes of memory", (size_t)(sizeof(int) * 100));
591 else all_pids[pid]->fds_size = 100;
593 if(root_of_pids) root_of_pids->prev = all_pids[pid];
594 all_pids[pid]->next = root_of_pids;
595 root_of_pids = all_pids[pid];
597 all_pids[pid]->pid = pid;
598 all_pids[pid]->new_entry = 1;
602 return all_pids[pid];
605 void del_pid_entry(pid_t pid) {
607 error("attempted to free pid %d that is not allocated.", pid);
612 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
614 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
615 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
616 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
618 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
619 if(all_pids[pid]->stat_filename) free(all_pids[pid]->stat_filename);
620 if(all_pids[pid]->statm_filename) free(all_pids[pid]->statm_filename);
621 if(all_pids[pid]->io_filename) free(all_pids[pid]->io_filename);
622 if(all_pids[pid]->cmdline_filename) free(all_pids[pid]->cmdline_filename);
625 all_pids[pid] = NULL;
630 // ----------------------------------------------------------------------------
631 // update pids from proc
633 int read_proc_pid_cmdline(struct pid_stat *p) {
635 if(unlikely(!p->cmdline_filename)) {
636 char filename[FILENAME_MAX + 1];
637 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
638 if(!(p->cmdline_filename = strdup(filename)))
639 fatal("Cannot allocate memory for filename '%s'", filename);
642 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
643 if(unlikely(fd == -1)) goto cleanup;
645 int i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
648 if(unlikely(bytes <= 0)) goto cleanup;
650 p->cmdline[bytes] = '\0';
651 for(i = 0; i < bytes ; i++)
652 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
655 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
660 // copy the command to the command line
661 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
665 int read_proc_pid_ownership(struct pid_stat *p) {
666 if(unlikely(!p->stat_filename)) {
667 error("pid %d does not have a stat_filename", p->pid);
671 // ----------------------------------------
675 if(stat(p->stat_filename, &st) != 0) {
676 error("Cannot stat file '%s'", p->stat_filename);
686 int read_proc_pid_stat(struct pid_stat *p) {
687 static procfile *ff = NULL;
689 if(unlikely(!p->stat_filename)) {
690 char filename[FILENAME_MAX + 1];
691 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
692 if(!(p->stat_filename = strdup(filename)))
693 fatal("Cannot allocate memory for filename '%s'", filename);
696 int set_quotes = (!ff)?1:0;
698 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
699 if(unlikely(!ff)) goto cleanup;
701 // if(set_quotes) procfile_set_quotes(ff, "()");
702 if(set_quotes) procfile_set_open_close(ff, "(", ")");
704 ff = procfile_readall(ff);
705 if(unlikely(!ff)) goto cleanup;
707 p->last_stat_collected_usec = p->stat_collected_usec;
708 p->stat_collected_usec = timems();
711 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
713 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
715 // p->state = *(procfile_lineword(ff, 0, 2));
716 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3));
717 // p->pgrp = atol(procfile_lineword(ff, 0, 4));
718 // p->session = atol(procfile_lineword(ff, 0, 5));
719 // p->tty_nr = atol(procfile_lineword(ff, 0, 6));
720 // p->tpgid = atol(procfile_lineword(ff, 0, 7));
721 // p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
723 unsigned long long last;
725 last = p->minflt_raw;
726 p->minflt_raw = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
727 p->minflt = (p->minflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
729 last = p->cminflt_raw;
730 p->cminflt_raw = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
731 p->cminflt = (p->cminflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
733 last = p->majflt_raw;
734 p->majflt_raw = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
735 p->majflt = (p->majflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
737 last = p->cmajflt_raw;
738 p->cmajflt_raw = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
739 p->cmajflt = (p->cmajflt_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
742 p->utime_raw = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
743 p->utime = (p->utime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
746 p->stime_raw = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
747 p->stime = (p->stime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
749 last = p->cutime_raw;
750 p->cutime_raw = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
751 p->cutime = (p->cutime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
753 last = p->cstime_raw;
754 p->cstime_raw = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
755 p->cstime = (p->cstime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
757 // p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
758 // p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
759 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19));
760 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
761 // p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
762 // p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
763 p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
764 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
765 // p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
766 // p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
767 // p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
768 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
769 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
770 // p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
771 // p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
772 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
773 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
774 // p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
775 // p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
776 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
777 // p->exit_signal = atol(procfile_lineword(ff, 0, 37));
778 // p->processor = atol(procfile_lineword(ff, 0, 38));
779 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
780 // p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
781 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
784 p->gtime_raw = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
785 p->gtime = (p->gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
787 last = p->cgtime_raw;
788 p->cgtime_raw = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
789 p->cgtime = (p->cgtime_raw - last) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
791 if(show_guest_time || p->gtime || p->cgtime) {
792 p->utime -= (p->utime >= p->gtime )?p->gtime :p->utime;
793 p->cutime -= (p->cutime >= p->cgtime)?p->cgtime:p->cutime;
797 if(unlikely(debug || (p->target && p->target->debug)))
798 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);
800 if(unlikely(global_iterations_counter == 1)) {
831 int read_proc_pid_statm(struct pid_stat *p) {
832 static procfile *ff = NULL;
834 if(unlikely(!p->statm_filename)) {
835 char filename[FILENAME_MAX + 1];
836 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
837 if(!(p->statm_filename = strdup(filename)))
838 fatal("Cannot allocate memory for filename '%s'", filename);
841 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
842 if(unlikely(!ff)) goto cleanup;
844 ff = procfile_readall(ff);
845 if(unlikely(!ff)) goto cleanup;
849 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
850 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
851 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
852 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
853 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
854 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
855 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
861 p->statm_resident = 0;
870 int read_proc_pid_io(struct pid_stat *p) {
871 static procfile *ff = NULL;
873 if(unlikely(!p->io_filename)) {
874 char filename[FILENAME_MAX + 1];
875 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
876 if(!(p->io_filename = strdup(filename)))
877 fatal("Cannot allocate memory for filename '%s'", filename);
881 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
882 if(unlikely(!ff)) goto cleanup;
884 ff = procfile_readall(ff);
885 if(unlikely(!ff)) goto cleanup;
889 p->last_io_collected_usec = p->io_collected_usec;
890 p->io_collected_usec = timems();
892 unsigned long long last;
894 last = p->io_logical_bytes_read_raw;
895 p->io_logical_bytes_read_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
896 p->io_logical_bytes_read = (p->io_logical_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
898 last = p->io_logical_bytes_written_raw;
899 p->io_logical_bytes_written_raw = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
900 p->io_logical_bytes_written = (p->io_logical_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
902 last = p->io_read_calls_raw;
903 p->io_read_calls_raw = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
904 p->io_read_calls = (p->io_read_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
906 last = p->io_write_calls_raw;
907 p->io_write_calls_raw = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
908 p->io_write_calls = (p->io_write_calls_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
910 last = p->io_storage_bytes_read_raw;
911 p->io_storage_bytes_read_raw = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
912 p->io_storage_bytes_read = (p->io_storage_bytes_read_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
914 last = p->io_storage_bytes_written_raw;
915 p->io_storage_bytes_written_raw = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
916 p->io_storage_bytes_written = (p->io_storage_bytes_written_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
918 last = p->io_cancelled_write_bytes_raw;
919 p->io_cancelled_write_bytes_raw = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
920 p->io_cancelled_write_bytes = (p->io_cancelled_write_bytes_raw - last) * (1000000ULL * RATES_DETAIL) / (p->io_collected_usec - p->last_io_collected_usec);
922 if(unlikely(global_iterations_counter == 1)) {
923 p->io_logical_bytes_read = 0;
924 p->io_logical_bytes_written = 0;
925 p->io_read_calls = 0;
926 p->io_write_calls = 0;
927 p->io_storage_bytes_read = 0;
928 p->io_storage_bytes_written = 0;
929 p->io_cancelled_write_bytes = 0;
935 p->io_logical_bytes_read = 0;
936 p->io_logical_bytes_written = 0;
937 p->io_read_calls = 0;
938 p->io_write_calls = 0;
939 p->io_storage_bytes_read = 0;
940 p->io_storage_bytes_written = 0;
941 p->io_cancelled_write_bytes = 0;
945 unsigned long long global_utime = 0;
946 unsigned long long global_stime = 0;
947 unsigned long long global_gtime = 0;
949 int read_proc_stat() {
950 static char filename[FILENAME_MAX + 1] = "";
951 static procfile *ff = NULL;
952 static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, ntime_raw = 0, collected_usec = 0, last_collected_usec = 0;
955 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
956 ff = procfile_open(filename, " \t:", PROCFILE_FLAG_DEFAULT);
957 if(unlikely(!ff)) goto cleanup;
960 ff = procfile_readall(ff);
961 if(unlikely(!ff)) goto cleanup;
963 last_collected_usec = collected_usec;
964 collected_usec = timems();
968 unsigned long long last;
971 utime_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
972 global_utime = (utime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
975 ntime_raw = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
976 global_utime += (ntime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
979 stime_raw = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
980 global_stime = (stime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
983 gtime_raw = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
984 global_gtime = (gtime_raw - last) * (1000000ULL * RATES_DETAIL) / (collected_usec - last_collected_usec);
986 if(unlikely(global_iterations_counter == 1)) {
1002 // ----------------------------------------------------------------------------
1004 // this is used to keep a global list of all open files of the system
1005 // it is needed in order to calculate the unique files processes have open
1007 #define FILE_DESCRIPTORS_INCREASE_STEP 100
1009 struct file_descriptor {
1011 #ifdef NETDATA_INTERNAL_CHECKS
1013 #endif /* NETDATA_INTERNAL_CHECKS */
1019 } *all_files = NULL;
1021 int all_files_len = 0;
1022 int all_files_size = 0;
1024 int file_descriptor_compare(void* a, void* b) {
1025 #ifdef NETDATA_INTERNAL_CHECKS
1026 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
1027 error("Corrupted index data detected. Please report this.");
1028 #endif /* NETDATA_INTERNAL_CHECKS */
1030 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
1033 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
1037 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
1040 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
1042 avl_tree all_files_index = {
1044 file_descriptor_compare
1047 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
1048 struct file_descriptor tmp;
1049 tmp.hash = (hash)?hash:simple_hash(name);
1053 #ifdef NETDATA_INTERNAL_CHECKS
1054 tmp.magic = 0x0BADCAFE;
1055 #endif /* NETDATA_INTERNAL_CHECKS */
1057 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
1060 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1061 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1063 #define FILETYPE_OTHER 0
1064 #define FILETYPE_FILE 1
1065 #define FILETYPE_PIPE 2
1066 #define FILETYPE_SOCKET 3
1067 #define FILETYPE_INOTIFY 4
1068 #define FILETYPE_EVENTFD 5
1069 #define FILETYPE_EVENTPOLL 6
1070 #define FILETYPE_TIMERFD 7
1071 #define FILETYPE_SIGNALFD 8
1073 void file_descriptor_not_used(int id)
1075 if(id > 0 && id < all_files_size) {
1077 #ifdef NETDATA_INTERNAL_CHECKS
1078 if(all_files[id].magic != 0x0BADCAFE) {
1079 error("Ignoring request to remove empty file id %d.", id);
1082 #endif /* NETDATA_INTERNAL_CHECKS */
1085 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1087 if(all_files[id].count > 0) {
1088 all_files[id].count--;
1090 if(!all_files[id].count) {
1092 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1094 file_descriptor_remove(&all_files[id]);
1095 #ifdef NETDATA_INTERNAL_CHECKS
1096 all_files[id].magic = 0x00000000;
1097 #endif /* NETDATA_INTERNAL_CHECKS */
1102 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1104 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1107 int file_descriptor_find_or_add(const char *name)
1109 static int last_pos = 0;
1110 uint32_t hash = simple_hash(name);
1113 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1115 struct file_descriptor *fd = file_descriptor_find(name, hash);
1119 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1126 // check we have enough memory to add it
1127 if(!all_files || all_files_len == all_files_size) {
1128 void *old = all_files;
1131 // there is no empty slot
1133 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1135 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1137 // if the address changed, we have to rebuild the index
1138 // since all pointers are now invalid
1139 if(old && old != (void *)all_files) {
1141 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1143 all_files_index.root = NULL;
1144 for(i = 0; i < all_files_size; i++) {
1145 if(!all_files[i].count) continue;
1146 file_descriptor_add(&all_files[i]);
1150 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1153 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1154 all_files[i].count = 0;
1155 all_files[i].name = NULL;
1156 #ifdef NETDATA_INTERNAL_CHECKS
1157 all_files[i].magic = 0x00000000;
1158 #endif /* NETDATA_INTERNAL_CHECKS */
1159 all_files[i].pos = i;
1162 if(!all_files_size) all_files_len = 1;
1163 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1167 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1169 // search for an empty slot
1171 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1172 if(c >= all_files_size) c = 0;
1173 if(c == 0) continue;
1175 if(!all_files[c].count) {
1177 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1179 #ifdef NETDATA_INTERNAL_CHECKS
1180 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1181 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1182 #endif /* NETDATA_INTERNAL_CHECKS */
1185 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);
1187 if(all_files[c].name) free((void *)all_files[c].name);
1188 all_files[c].name = NULL;
1193 if(i == all_files_size) {
1194 fatal("We should find an empty slot, but there isn't any");
1199 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1203 // else we have an empty slot in 'c'
1206 if(name[0] == '/') type = FILETYPE_FILE;
1207 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1208 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1209 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1210 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1211 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1212 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1213 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1214 else if(strncmp(name, "anon_inode:", 11) == 0) {
1216 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1218 type = FILETYPE_OTHER;
1222 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1224 type = FILETYPE_OTHER;
1227 all_files[c].name = strdup(name);
1228 all_files[c].hash = hash;
1229 all_files[c].type = type;
1230 all_files[c].pos = c;
1231 all_files[c].count = 1;
1232 #ifdef NETDATA_INTERNAL_CHECKS
1233 all_files[c].magic = 0x0BADCAFE;
1234 #endif /* NETDATA_INTERNAL_CHECKS */
1235 file_descriptor_add(&all_files[c]);
1238 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1243 int read_pid_file_descriptors(struct pid_stat *p) {
1244 char dirname[FILENAME_MAX+1];
1246 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
1247 DIR *fds = opendir(dirname);
1251 char fdname[FILENAME_MAX + 1];
1252 char linkname[FILENAME_MAX + 1];
1254 // make the array negative
1255 for(c = 0 ; c < p->fds_size ; c++)
1256 p->fds[c] = -p->fds[c];
1258 while((de = readdir(fds))) {
1259 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1262 // check if the fds array is small
1263 int fdid = atoi(de->d_name);
1264 if(fdid < 0) continue;
1265 if(fdid >= p->fds_size) {
1266 // it is small, extend it
1268 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1270 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1272 fatal("Cannot re-allocate fds for %s", p->comm);
1276 // and initialize it
1277 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1278 p->fds_size = fdid + 100;
1281 if(p->fds[fdid] == 0) {
1282 // we don't know this fd, get it
1284 sprintf(fdname, "%s/proc/%d/fd/%s", host_prefix, p->pid, de->d_name);
1285 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1287 if(debug || (p->target && p->target->debug)) {
1288 if(debug || (p->target && p->target->debug))
1289 error("Cannot read link %s", fdname);
1296 // if another process already has this, we will get
1298 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1301 // else make it positive again, we need it
1302 // of course, the actual file may have changed, but we don't care so much
1303 // FIXME: we could compare the inode as returned by readdir direct structure
1304 else p->fds[fdid] = -p->fds[fdid];
1308 // remove all the negative file descriptors
1309 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1310 file_descriptor_not_used(-p->fds[c]);
1319 // ----------------------------------------------------------------------------
1321 int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1322 char *prefix = "\\_ ";
1326 indent = print_process_and_parents(p->parent, p->stat_collected_usec);
1330 char buffer[indent + 1];
1333 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1336 fprintf(stderr, " %s %s%s (%d %s %lld"
1341 , p->updated?"running":"exited"
1342 , (long long)p->stat_collected_usec - (long long)time
1345 if(p->utime) fprintf(stderr, " utime=%llu", p->utime);
1346 if(p->stime) fprintf(stderr, " stime=%llu", p->stime);
1347 if(p->gtime) fprintf(stderr, " gtime=%llu", p->gtime);
1348 if(p->cutime) fprintf(stderr, " cutime=%llu", p->cutime);
1349 if(p->cstime) fprintf(stderr, " cstime=%llu", p->cstime);
1350 if(p->cgtime) fprintf(stderr, " cgtime=%llu", p->cgtime);
1351 if(p->minflt) fprintf(stderr, " minflt=%llu", p->minflt);
1352 if(p->cminflt) fprintf(stderr, " cminflt=%llu", p->cminflt);
1353 if(p->majflt) fprintf(stderr, " majflt=%llu", p->majflt);
1354 if(p->cmajflt) fprintf(stderr, " cmajflt=%llu", p->cmajflt);
1355 fprintf(stderr, ")\n");
1360 void print_process_tree(struct pid_stat *p, char *msg) {
1362 fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1363 print_process_and_parents(p, p->stat_collected_usec);
1366 void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1368 struct pid_stat *p = NULL;
1370 for(p = root_of_pids; p ; p = p->next) {
1371 if(p == pe) continue;
1375 if(p->cminflt > lost) {
1376 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);
1382 if(p->cmajflt > lost) {
1383 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);
1389 if(p->cutime > lost) {
1390 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);
1396 if(p->cstime > lost) {
1397 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);
1403 if(p->cgtime > lost) {
1404 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);
1414 fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1418 fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1422 fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1426 fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1430 fprintf(stderr, " > cannot find any process to use the lost exited child gtime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1436 unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1437 unsigned long long absorbed = 0;
1439 if(*field > *pfield) {
1440 absorbed += *pfield;
1453 void process_exited_processes() {
1456 for(p = root_of_pids; p ; p = p->next) {
1457 if(p->updated || !p->stat_collected_usec)
1460 struct pid_stat *pp = p->parent;
1462 unsigned long long utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1463 unsigned long long stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1464 unsigned long long gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1465 unsigned long long minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1466 unsigned long long majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1468 if(utime + stime + gtime + minflt + majflt == 0)
1471 if(unlikely(debug)) {
1473 fprintf(stderr, "Absorb %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1476 , p->updated?"running":"exited"
1483 print_process_tree(p, "Searching parents");
1486 for(pp = p->parent; pp ; pp = pp->parent) {
1487 if(!pp->updated) continue;
1489 unsigned long long absorbed;
1490 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1491 if(unlikely(debug && absorbed))
1492 fprintf(stderr, " > process %s (%d %s) absorbed %llu utime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1494 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1495 if(unlikely(debug && absorbed))
1496 fprintf(stderr, " > process %s (%d %s) absorbed %llu stime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1498 absorbed = remove_exited_child_from_parent(>ime, &pp->cgtime);
1499 if(unlikely(debug && absorbed))
1500 fprintf(stderr, " > process %s (%d %s) absorbed %llu gtime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1502 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1503 if(unlikely(debug && absorbed))
1504 fprintf(stderr, " > process %s (%d %s) absorbed %llu minflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1506 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1507 if(unlikely(debug && absorbed))
1508 fprintf(stderr, " > process %s (%d %s) absorbed %llu majflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1511 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
1512 if(unlikely(debug)) {
1513 if(utime) find_lost_child_debug(p, utime, 3);
1514 if(stime) find_lost_child_debug(p, stime, 4);
1515 if(gtime) find_lost_child_debug(p, gtime, 5);
1516 if(minflt) find_lost_child_debug(p, minflt, 1);
1517 if(majflt) find_lost_child_debug(p, majflt, 2);
1523 fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1526 , p->updated?"running":"exited"
1534 for(pp = p->parent; pp ; pp = pp->parent) {
1535 if(pp->updated) break;
1539 fprintf(stderr, " > - KEEP - parent for another loop: %s (%d %s)\n"
1542 , pp->updated?"running":"exited"
1546 p->utime_raw = utime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1547 p->stime_raw = stime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1548 p->gtime_raw = gtime * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1549 p->minflt_raw = minflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1550 p->majflt_raw = majflt * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000ULL * RATES_DETAIL);
1551 p->cutime_raw = p->cstime_raw = p->cgtime_raw = p->cminflt_raw = p->cmajflt_raw = 0;
1554 fprintf(stderr, "\n");
1556 else if(unlikely(debug)) {
1557 fprintf(stderr, " > totally absorbed - DONE - %s (%d %s)\n"
1560 , p->updated?"running":"exited"
1566 void link_all_processes_to_their_parents(void) {
1567 struct pid_stat *p, *pp;
1569 // link all children to their parents
1570 // and update children count on parents
1571 for(p = root_of_pids; p ; p = p->next) {
1572 // for each process found
1577 if(unlikely(!p->ppid)) {
1582 pp = all_pids[p->ppid];
1585 pp->children_count++;
1587 if(unlikely(debug || (p->target && p->target->debug)))
1588 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);
1592 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1597 // ----------------------------------------------------------------------------
1599 // 1. read all files in /proc
1600 // 2. for each numeric directory:
1601 // i. read /proc/pid/stat
1602 // ii. read /proc/pid/statm
1603 // iii. read /proc/pid/io (requires root access)
1604 // iii. read the entries in directory /proc/pid/fd (requires root access)
1606 // a. find or create a struct file_descriptor
1607 // b. cleanup any old/unused file_descriptors
1609 // after all these, some pids may be linked to targets, while others may not
1611 // in case of errors, only 1 every 1000 errors is printed
1612 // to avoid filling up all disk space
1613 // if debug is enabled, all errors are printed
1615 static int compar_pid(const void *pid1, const void *pid2) {
1617 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1618 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1620 if(p1->sortlist > p2->sortlist)
1626 void collect_data_for_pid(pid_t pid) {
1627 if(unlikely(pid <= 0 || pid > pid_max)) {
1628 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1632 struct pid_stat *p = get_pid_entry(pid);
1633 if(unlikely(!p || p->read)) return;
1636 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1638 // --------------------------------------------------------------------
1641 if(unlikely(read_proc_pid_stat(p))) {
1642 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1643 // there is no reason to proceed if we cannot get its status
1647 read_proc_pid_ownership(p);
1649 // check its parent pid
1650 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1651 error("Pid %d states invalid parent pid %d. Using 0.", pid, p->ppid);
1655 // --------------------------------------------------------------------
1658 if(unlikely(read_proc_pid_io(p)))
1659 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1661 // --------------------------------------------------------------------
1662 // /proc/<pid>/statm
1664 if(unlikely(read_proc_pid_statm(p))) {
1665 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1666 // there is no reason to proceed if we cannot get its memory status
1670 // --------------------------------------------------------------------
1673 // check if it is target
1674 // we do this only once, the first time this pid is loaded
1675 if(unlikely(p->new_entry)) {
1676 // /proc/<pid>/cmdline
1677 if(likely(proc_pid_cmdline_is_needed)) {
1678 if(unlikely(read_proc_pid_cmdline(p)))
1679 error("Cannot process %s/proc/%d/cmdline", host_prefix, pid);
1683 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1685 uint32_t hash = simple_hash(p->comm);
1686 size_t pclen = strlen(p->comm);
1689 for(w = apps_groups_root_target; w ; w = w->next) {
1690 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1692 // find it - 4 cases:
1693 // 1. the target is not a pattern
1694 // 2. the target has the prefix
1695 // 3. the target has the suffix
1696 // 4. the target is something inside cmdline
1697 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1698 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1699 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1700 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1702 if(w->target) p->target = w->target;
1705 if(debug || (p->target && p->target->debug))
1706 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1713 // --------------------------------------------------------------------
1716 if(unlikely(read_pid_file_descriptors(p))) {
1717 error("Cannot process entries in %s/proc/%d/fd", host_prefix, pid);
1720 // --------------------------------------------------------------------
1723 if(unlikely(debug && include_exited_childs && all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1724 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);
1726 // mark it as updated
1732 int collect_data_for_all_processes_from_proc(void) {
1733 struct pid_stat *p = NULL;
1735 if(all_pids_count) {
1736 // read parents before childs
1737 // this is needed to prevent a situation where
1738 // a child is found running, but until we read
1739 // its parent, it has exited and its parent
1740 // has accumulated its resources
1743 for(p = root_of_pids; p ; p = p->next) {
1748 p->children_count = 0;
1751 all_pids_sortlist[slc++] = p->pid;
1754 if(unlikely(slc != all_pids_count)) {
1755 error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1756 all_pids_count = slc;
1759 if(include_exited_childs) {
1760 qsort((void *)all_pids_sortlist, all_pids_count, sizeof(pid_t), compar_pid);
1761 for(slc = 0; slc < all_pids_count; slc++)
1762 collect_data_for_pid(all_pids_sortlist[slc]);
1766 char dirname[FILENAME_MAX + 1];
1768 snprintfz(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1769 DIR *dir = opendir(dirname);
1772 struct dirent *file = NULL;
1774 while((file = readdir(dir))) {
1775 char *endptr = file->d_name;
1776 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1778 // make sure we read a valid number
1779 if(unlikely(endptr == file->d_name || *endptr != '\0'))
1782 collect_data_for_pid(pid);
1786 // normally this is done
1787 // however we may have processes exited while we collected values
1788 // so let's find the exited ones
1789 // we do this by collecting the ownership of process
1790 // if we manage to get the ownership, the process still runs
1793 link_all_processes_to_their_parents();
1794 process_exited_processes();
1799 // ----------------------------------------------------------------------------
1800 // update statistics on the targets
1802 // 1. link all childs to their parents
1803 // 2. go from bottom to top, marking as merged all childs to their parents
1804 // this step links all parents without a target to the child target, if any
1805 // 3. link all top level processes (the ones not merged) to the default target
1806 // 4. go from top to bottom, linking all childs without a target, to their parent target
1807 // after this step, all processes have a target
1808 // [5. for each killed pid (updated = 0), remove its usage from its target]
1809 // 6. zero all apps_groups_targets
1810 // 7. concentrate all values on the apps_groups_targets
1811 // 8. remove all killed processes
1812 // 9. find the unique file count for each target
1813 // check: update_apps_groups_statistics()
1815 void cleanup_exited_pids(void) {
1817 struct pid_stat *p = NULL;
1819 for(p = root_of_pids; p ;) {
1820 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1821 // 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);
1823 if(unlikely(debug && (p->keep || p->keeploops)))
1824 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1826 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1827 file_descriptor_not_used(p->fds[c]);
1836 if(unlikely(p->keep)) p->keeploops++;
1843 void apply_apps_groups_targets_inheritance(void) {
1844 struct pid_stat *p = NULL;
1846 // children that do not have a target
1847 // inherit their target from their parent
1848 int found = 1, loops = 0;
1850 if(unlikely(debug)) loops++;
1852 for(p = root_of_pids; p ; p = p->next) {
1853 // if this process does not have a target
1854 // and it has a parent
1855 // and its parent has a target
1856 // then, set the parent's target to this process
1857 if(unlikely(!p->target && p->parent && p->parent->target)) {
1858 p->target = p->parent->target;
1861 if(debug || (p->target && p->target->debug))
1862 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);
1867 // find all the procs with 0 childs and merge them to their parents
1868 // repeat, until nothing more can be done.
1872 if(unlikely(debug)) loops++;
1875 for(p = root_of_pids; p ; p = p->next) {
1876 if(unlikely(!p->sortlist && !p->children_count))
1877 p->sortlist = sortlist++;
1879 // if this process does not have any children
1880 // and is not already merged
1882 // and its parent has children
1883 // and the target of this process and its parent is the same, or the parent does not have a target
1884 // and its parent is not init
1885 // then, mark them as merged.
1890 && p->parent->children_count
1891 && (p->target == p->parent->target || !p->parent->target)
1894 p->parent->children_count--;
1897 // the parent inherits the child's target, if it does not have a target itself
1898 if(unlikely(p->target && !p->parent->target)) {
1899 p->parent->target = p->target;
1901 if(debug || (p->target && p->target->debug))
1902 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);
1910 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
1913 // init goes always to default target
1915 all_pids[1]->target = apps_groups_default_target;
1917 // give a default target on all top level processes
1918 if(unlikely(debug)) loops++;
1919 for(p = root_of_pids; p ; p = p->next) {
1920 // if the process is not merged itself
1921 // then is is a top level process
1922 if(unlikely(!p->merged && !p->target))
1923 p->target = apps_groups_default_target;
1925 // make sure all processes have a sortlist
1926 if(unlikely(!p->sortlist))
1927 p->sortlist = sortlist++;
1931 all_pids[1]->sortlist = sortlist++;
1933 // give a target to all merged child processes
1936 if(unlikely(debug)) loops++;
1938 for(p = root_of_pids; p ; p = p->next) {
1939 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1940 p->target = p->parent->target;
1943 if(debug || (p->target && p->target->debug))
1944 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);
1950 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1953 long zero_all_targets(struct target *root) {
1957 for (w = root; w ; w = w->next) {
1960 if(w->fds) free(w->fds);
1978 w->statm_resident = 0;
1985 w->io_logical_bytes_read = 0;
1986 w->io_logical_bytes_written = 0;
1987 w->io_read_calls = 0;
1988 w->io_write_calls = 0;
1989 w->io_storage_bytes_read = 0;
1990 w->io_storage_bytes_written = 0;
1991 w->io_cancelled_write_bytes = 0;
1997 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
2000 if(unlikely(!w->fds)) {
2001 w->fds = calloc(sizeof(int), (size_t) all_files_size);
2002 if(unlikely(!w->fds))
2003 error("Cannot allocate memory for fds in %s", w->name);
2006 if(likely(p->updated)) {
2007 w->cutime += p->cutime;
2008 w->cstime += p->cstime;
2009 w->cgtime += p->cgtime;
2010 w->cminflt += p->cminflt;
2011 w->cmajflt += p->cmajflt;
2013 w->utime += p->utime;
2014 w->stime += p->stime;
2015 w->gtime += p->gtime;
2016 w->minflt += p->minflt;
2017 w->majflt += p->majflt;
2021 w->statm_size += p->statm_size;
2022 w->statm_resident += p->statm_resident;
2023 w->statm_share += p->statm_share;
2024 w->statm_text += p->statm_text;
2025 w->statm_lib += p->statm_lib;
2026 w->statm_data += p->statm_data;
2027 w->statm_dirty += p->statm_dirty;
2029 w->io_logical_bytes_read += p->io_logical_bytes_read;
2030 w->io_logical_bytes_written += p->io_logical_bytes_written;
2031 w->io_read_calls += p->io_read_calls;
2032 w->io_write_calls += p->io_write_calls;
2033 w->io_storage_bytes_read += p->io_storage_bytes_read;
2034 w->io_storage_bytes_written += p->io_storage_bytes_written;
2035 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
2038 w->num_threads += p->num_threads;
2040 if(likely(w->fds)) {
2042 for(c = 0; c < p->fds_size ;c++) {
2043 if(p->fds[c] == 0) continue;
2045 if(likely(p->fds[c] < all_files_size)) {
2046 if(w->fds) w->fds[p->fds[c]]++;
2049 error("Invalid fd number %d", p->fds[c]);
2053 if(unlikely(debug || w->debug))
2054 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);
2058 void count_targets_fds(struct target *root) {
2062 for (w = root; w ; w = w->next) {
2063 if(!w->fds) continue;
2068 w->openinotifies = 0;
2069 w->openeventfds = 0;
2070 w->opentimerfds = 0;
2071 w->opensignalfds = 0;
2072 w->openeventpolls = 0;
2075 for(c = 1; c < all_files_size ;c++) {
2077 switch(all_files[c].type) {
2086 case FILETYPE_SOCKET:
2090 case FILETYPE_INOTIFY:
2094 case FILETYPE_EVENTFD:
2098 case FILETYPE_TIMERFD:
2102 case FILETYPE_SIGNALFD:
2106 case FILETYPE_EVENTPOLL:
2107 w->openeventpolls++;
2120 void calculate_netdata_statistics(void) {
2121 apply_apps_groups_targets_inheritance();
2123 zero_all_targets(users_root_target);
2124 zero_all_targets(groups_root_target);
2125 apps_groups_targets = zero_all_targets(apps_groups_root_target);
2127 // this has to be done, before the cleanup
2128 struct pid_stat *p = NULL;
2129 struct target *w = NULL, *o = NULL;
2131 // concentrate everything on the apps_groups_targets
2132 for(p = root_of_pids; p ; p = p->next) {
2134 // --------------------------------------------------------------------
2135 // apps_groups targets
2136 if(likely(p->target))
2137 aggregate_pid_on_target(p->target, p, NULL);
2139 error("pid %d %s was left without a target!", p->pid, p->comm);
2142 // --------------------------------------------------------------------
2145 if(likely(p->user_target && p->user_target->uid == p->uid))
2148 if(unlikely(debug && p->user_target))
2149 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);
2151 w = p->user_target = get_users_target(p->uid);
2155 aggregate_pid_on_target(w, p, o);
2157 error("pid %d %s was left without a user target!", p->pid, p->comm);
2160 // --------------------------------------------------------------------
2162 o = p->group_target;
2163 if(likely(p->group_target && p->group_target->gid == p->gid))
2164 w = p->group_target;
2166 if(unlikely(debug && p->group_target))
2167 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);
2169 w = p->group_target = get_groups_target(p->gid);
2173 aggregate_pid_on_target(w, p, o);
2175 error("pid %d %s was left without a group target!", p->pid, p->comm);
2179 count_targets_fds(apps_groups_root_target);
2180 count_targets_fds(users_root_target);
2181 count_targets_fds(groups_root_target);
2183 cleanup_exited_pids();
2186 // ----------------------------------------------------------------------------
2187 // update chart dimensions
2189 BUFFER *output = NULL;
2190 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2192 static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2193 // fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2194 buffer_strcat(output, "BEGIN ");
2195 buffer_strcat(output, type);
2196 buffer_strcat(output, ".");
2197 buffer_strcat(output, id);
2198 buffer_strcat(output, " ");
2199 buffer_print_llu(output, usec);
2200 buffer_strcat(output, "\n");
2203 static inline void send_SET(const char *name, unsigned long long value) {
2204 // fprintf(stdout, "SET %s = %llu\n", name, value);
2205 buffer_strcat(output, "SET ");
2206 buffer_strcat(output, name);
2207 buffer_strcat(output, " = ");
2208 buffer_print_llu(output, value);
2209 buffer_strcat(output, "\n");
2212 static inline void send_END(void) {
2213 // fprintf(stdout, "END\n");
2214 buffer_strcat(output, "END\n");
2217 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;
2218 double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2220 unsigned long long send_resource_usage_to_netdata() {
2221 static struct timeval last = { 0, 0 };
2222 static struct rusage me_last;
2227 unsigned long long usec;
2228 unsigned long long cpuuser;
2229 unsigned long long cpusyst;
2232 gettimeofday(&last, NULL);
2233 getrusage(RUSAGE_SELF, &me_last);
2235 // the first time, give a zero to allow
2236 // netdata calibrate to the current time
2237 // usec = update_every * 1000000ULL;
2243 gettimeofday(&now, NULL);
2244 getrusage(RUSAGE_SELF, &me);
2246 usec = usecdiff(&now, &last);
2247 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2248 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2250 bcopy(&now, &last, sizeof(struct timeval));
2251 bcopy(&me, &me_last, sizeof(struct rusage));
2254 buffer_sprintf(output,
2255 "BEGIN netdata.apps_cpu %llu\n"
2257 "SET system = %llu\n"
2259 "BEGIN netdata.apps_files %llu\n"
2260 "SET files = %llu\n"
2263 "SET targets = %ld\n"
2265 "BEGIN netdata.apps_fix %llu\n"
2266 "SET utime = %llu\n"
2267 "SET stime = %llu\n"
2268 "SET gtime = %llu\n"
2269 "SET minflt = %llu\n"
2270 "SET majflt = %llu\n"
2279 , apps_groups_targets
2281 , (unsigned long long)(utime_fix_ratio * 100 * RATES_DETAIL)
2282 , (unsigned long long)(stime_fix_ratio * 100 * RATES_DETAIL)
2283 , (unsigned long long)(gtime_fix_ratio * 100 * RATES_DETAIL)
2284 , (unsigned long long)(minflt_fix_ratio * 100 * RATES_DETAIL)
2285 , (unsigned long long)(majflt_fix_ratio * 100 * RATES_DETAIL)
2288 if(include_exited_childs)
2289 buffer_sprintf(output,
2290 "BEGIN netdata.apps_children_fix %llu\n"
2291 "SET cutime = %llu\n"
2292 "SET cstime = %llu\n"
2293 "SET cgtime = %llu\n"
2294 "SET cminflt = %llu\n"
2295 "SET cmajflt = %llu\n"
2298 , (unsigned long long)(cutime_fix_ratio * 100 * RATES_DETAIL)
2299 , (unsigned long long)(cstime_fix_ratio * 100 * RATES_DETAIL)
2300 , (unsigned long long)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2301 , (unsigned long long)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2302 , (unsigned long long)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2308 void normalize_data(struct target *root) {
2311 // childs processing introduces spikes
2312 // here we try to eliminate them by disabling childs processing either for specific dimensions
2313 // or entirely. Of course, either way, we disable it just a single iteration.
2315 unsigned long long max = processors * hz * RATES_DETAIL;
2316 unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2318 if(global_utime > max) global_utime = max;
2319 if(global_stime > max) global_stime = max;
2320 if(global_gtime > max) global_gtime = max;
2322 for(w = root; w ; w = w->next) {
2323 if(w->target || (!w->processes && !w->exposed)) continue;
2328 cutime += w->cutime;
2329 cstime += w->cstime;
2330 cgtime += w->cgtime;
2332 minflt += w->minflt;
2333 majflt += w->majflt;
2334 cminflt += w->cminflt;
2335 cmajflt += w->cmajflt;
2338 if((global_utime || global_stime || global_gtime) && (utime || stime || gtime)) {
2339 if(global_utime + global_stime + global_gtime > utime + cutime + stime + cstime + gtime + cgtime) {
2340 // everything we collected fits
2346 cgtime_fix_ratio = 1.0; //(double)(global_utime + global_stime) / (double)(utime + cutime + stime + cstime);
2348 else if(global_utime + global_stime > utime + stime) {
2349 // childrens resources are too high
2350 // lower only the children resources
2353 gtime_fix_ratio = 1.0;
2356 cgtime_fix_ratio = (double)((global_utime + global_stime) - (utime + stime)) / (double)(cutime + cstime);
2359 // even running processes are unrealistic
2360 // zero the children resources
2361 // lower the running processes resources
2364 gtime_fix_ratio = (double)(global_utime + global_stime) / (double)(utime + stime);
2367 cgtime_fix_ratio = 0.0;
2376 cgtime_fix_ratio = 0.0;
2379 if(utime_fix_ratio > 1.0) utime_fix_ratio = 1.0;
2380 if(cutime_fix_ratio > 1.0) cutime_fix_ratio = 1.0;
2381 if(stime_fix_ratio > 1.0) stime_fix_ratio = 1.0;
2382 if(cstime_fix_ratio > 1.0) cstime_fix_ratio = 1.0;
2383 if(gtime_fix_ratio > 1.0) gtime_fix_ratio = 1.0;
2384 if(cgtime_fix_ratio > 1.0) cgtime_fix_ratio = 1.0;
2386 // if(utime_fix_ratio < 0.0) utime_fix_ratio = 0.0;
2387 // if(cutime_fix_ratio < 0.0) cutime_fix_ratio = 0.0;
2388 // if(stime_fix_ratio < 0.0) stime_fix_ratio = 0.0;
2389 // if(cstime_fix_ratio < 0.0) cstime_fix_ratio = 0.0;
2390 // if(gtime_fix_ratio < 0.0) gtime_fix_ratio = 0.0;
2391 // if(cgtime_fix_ratio < 0.0) cgtime_fix_ratio = 0.0;
2394 // we use cpu time to normalize page faults
2395 // the problem is that to find the proper max values
2396 // for page faults we have to parse /proc/vmstat
2397 // which is quite big to do it again (netdata does it already)
2399 // a better solution could be to somehow have netdata
2400 // do this normalization for us
2402 if(utime || stime || gtime)
2404 minflt_fix_ratio = (double)(utime * utime_fix_ratio + stime * stime_fix_ratio + gtime * gtime_fix_ratio) / (double)(utime + stime + gtime);
2407 majflt_fix_ratio = 1.0;
2409 if(cutime || cstime || cgtime)
2411 cminflt_fix_ratio = (double)(cutime * cutime_fix_ratio + cstime * cstime_fix_ratio + cgtime * cgtime_fix_ratio) / (double)(cutime + cstime + cgtime);
2414 cmajflt_fix_ratio = 1.0;
2418 if(unlikely(debug)) {
2420 "SYSTEM: u=%llu s=%llu g=%llu "
2421 "COLLECTED: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2422 "DELTA: u=%lld s=%lld g=%lld "
2423 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2424 "FINALLY: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2435 , (long long)utime + (long long)cutime - (long long)global_utime
2436 , (long long)stime + (long long)cstime - (long long)global_stime
2437 , (long long)gtime + (long long)cgtime - (long long)global_gtime
2444 , (unsigned long long)(utime * utime_fix_ratio)
2445 , (unsigned long long)(stime * stime_fix_ratio)
2446 , (unsigned long long)(gtime * gtime_fix_ratio)
2447 , (unsigned long long)(cutime * cutime_fix_ratio)
2448 , (unsigned long long)(cstime * cstime_fix_ratio)
2449 , (unsigned long long)(cgtime * cgtime_fix_ratio)
2454 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec) {
2457 send_BEGIN(type, "cpu", usec);
2458 for (w = root; w ; w = w->next) {
2459 if(unlikely(w->exposed))
2460 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));
2464 send_BEGIN(type, "cpu_user", usec);
2465 for (w = root; w ; w = w->next) {
2466 if(unlikely(w->exposed))
2467 send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2471 send_BEGIN(type, "cpu_system", usec);
2472 for (w = root; w ; w = w->next) {
2473 if(unlikely(w->exposed))
2474 send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2478 if(show_guest_time) {
2479 send_BEGIN(type, "cpu_guest", usec);
2480 for (w = root; w ; w = w->next) {
2481 if(unlikely(w->exposed))
2482 send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2487 send_BEGIN(type, "threads", usec);
2488 for (w = root; w ; w = w->next) {
2489 if(unlikely(w->exposed))
2490 send_SET(w->name, w->num_threads);
2494 send_BEGIN(type, "processes", usec);
2495 for (w = root; w ; w = w->next) {
2496 if(unlikely(w->exposed))
2497 send_SET(w->name, w->processes);
2501 send_BEGIN(type, "mem", usec);
2502 for (w = root; w ; w = w->next) {
2503 if(unlikely(w->exposed))
2504 send_SET(w->name, (w->statm_resident > w->statm_share)?(w->statm_resident - w->statm_share):0ULL);
2508 send_BEGIN(type, "minor_faults", usec);
2509 for (w = root; w ; w = w->next) {
2510 if(unlikely(w->exposed))
2511 send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2515 send_BEGIN(type, "major_faults", usec);
2516 for (w = root; w ; w = w->next) {
2517 if(unlikely(w->exposed))
2518 send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2522 send_BEGIN(type, "lreads", usec);
2523 for (w = root; w ; w = w->next) {
2524 if(unlikely(w->exposed))
2525 send_SET(w->name, w->io_logical_bytes_read);
2529 send_BEGIN(type, "lwrites", usec);
2530 for (w = root; w ; w = w->next) {
2531 if(unlikely(w->exposed))
2532 send_SET(w->name, w->io_logical_bytes_written);
2536 send_BEGIN(type, "preads", usec);
2537 for (w = root; w ; w = w->next) {
2538 if(unlikely(w->exposed))
2539 send_SET(w->name, w->io_storage_bytes_read);
2543 send_BEGIN(type, "pwrites", usec);
2544 for (w = root; w ; w = w->next) {
2545 if(unlikely(w->exposed))
2546 send_SET(w->name, w->io_storage_bytes_written);
2550 send_BEGIN(type, "files", usec);
2551 for (w = root; w ; w = w->next) {
2552 if(unlikely(w->exposed))
2553 send_SET(w->name, w->openfiles);
2557 send_BEGIN(type, "sockets", usec);
2558 for (w = root; w ; w = w->next) {
2559 if(unlikely(w->exposed))
2560 send_SET(w->name, w->opensockets);
2564 send_BEGIN(type, "pipes", usec);
2565 for (w = root; w ; w = w->next) {
2566 if(unlikely(w->exposed))
2567 send_SET(w->name, w->openpipes);
2573 // ----------------------------------------------------------------------------
2574 // generate the charts
2576 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2579 int newly_added = 0;
2581 for(w = root ; w ; w = w->next) {
2582 if (w->target) continue;
2584 if (!w->exposed && w->processes) {
2587 if (debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2591 // nothing more to show
2592 if(!newly_added && show_guest_time == show_guest_time_old) return;
2594 // we have something new to show
2595 // update the charts
2596 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);
2597 for (w = root; w ; w = w->next) {
2598 if(unlikely(w->exposed))
2599 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu %s\n", w->name, hz * RATES_DETAIL / 100, w->hidden ? "hidden" : "");
2602 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);
2603 for (w = root; w ; w = w->next) {
2604 if(unlikely(w->exposed))
2605 buffer_sprintf(output, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2608 buffer_sprintf(output, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %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 1\n", w->name);
2614 buffer_sprintf(output, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %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 1\n", w->name);
2620 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);
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, hz * RATES_DETAIL / 100LLU);
2626 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);
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, hz * RATES_DETAIL / 100LLU);
2632 if(show_guest_time) {
2633 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);
2634 for (w = root; w; w = w->next) {
2635 if(unlikely(w->exposed))
2636 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, hz * RATES_DETAIL / 100LLU);
2640 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);
2641 for (w = root; w ; w = w->next) {
2642 if(unlikely(w->exposed))
2643 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2646 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);
2647 for (w = root; w ; w = w->next) {
2648 if(unlikely(w->exposed))
2649 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, RATES_DETAIL);
2652 buffer_sprintf(output, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2653 for (w = root; w ; w = w->next) {
2654 if(unlikely(w->exposed))
2655 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2658 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);
2659 for (w = root; w ; w = w->next) {
2660 if(unlikely(w->exposed))
2661 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2664 buffer_sprintf(output, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2665 for (w = root; w ; w = w->next) {
2666 if(unlikely(w->exposed))
2667 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2670 buffer_sprintf(output, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2671 for (w = root; w ; w = w->next) {
2672 if(unlikely(w->exposed))
2673 buffer_sprintf(output, "DIMENSION %s '' absolute 1 %llu\n", w->name, 1024LLU * RATES_DETAIL);
2676 buffer_sprintf(output, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2677 for (w = root; w ; w = w->next) {
2678 if(unlikely(w->exposed))
2679 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2682 buffer_sprintf(output, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2683 for (w = root; w ; w = w->next) {
2684 if(unlikely(w->exposed))
2685 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2688 buffer_sprintf(output, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2689 for (w = root; w ; w = w->next) {
2690 if(unlikely(w->exposed))
2691 buffer_sprintf(output, "DIMENSION %s '' absolute 1 1\n", w->name);
2696 // ----------------------------------------------------------------------------
2697 // parse command line arguments
2699 void parse_args(int argc, char **argv)
2704 for(i = 1; i < argc; i++) {
2706 int n = atoi(argv[i]);
2713 if(strcmp("debug", argv[i]) == 0) {
2715 // debug_flags = 0xffffffff;
2719 if(strcmp("no-childs", argv[i]) == 0) {
2720 include_exited_childs = 0;
2724 if(strcmp("with-childs", argv[i]) == 0) {
2725 include_exited_childs = 1;
2734 error("Cannot understand option %s", argv[i]);
2738 if(freq > 0) update_every = freq;
2739 if(!name) name = "groups";
2741 if(read_apps_groups_conf(name)) {
2742 error("Cannot read process groups %s", name);
2747 int main(int argc, char **argv)
2749 // debug_flags = D_PROCFILE;
2751 // set the name for logging
2752 program_name = "apps.plugin";
2754 // disable syslog for apps.plugin
2755 error_log_syslog = 0;
2757 // set errors flood protection to 100 logs per hour
2758 error_log_errors_per_period = 100;
2759 error_log_throttle_period = 3600;
2761 host_prefix = getenv("NETDATA_HOST_PREFIX");
2762 if(host_prefix == NULL) {
2763 info("NETDATA_HOST_PREFIX is not passed from netdata");
2766 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2768 config_dir = getenv("NETDATA_CONFIG_DIR");
2769 if(config_dir == NULL) {
2770 info("NETDATA_CONFIG_DIR is not passed from netdata");
2771 config_dir = CONFIG_DIR;
2773 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2775 #ifdef NETDATA_INTERNAL_CHECKS
2776 if(debug_flags != 0) {
2777 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2778 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2779 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2780 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2782 #endif /* NETDATA_INTERNAL_CHECKS */
2784 procfile_adaptive_initial_allocation = 1;
2786 time_t started_t = time(NULL);
2789 pid_max = get_system_pid_max();
2790 processors = get_system_cpus();
2792 parse_args(argc, argv);
2794 all_pids_sortlist = calloc(sizeof(pid_t), (size_t)pid_max);
2795 if(!all_pids_sortlist) {
2796 error("Cannot allocate %zu bytes of memory.", sizeof(pid_t) * pid_max);
2797 printf("DISABLE\n");
2801 all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2803 error("Cannot allocate %zu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2804 printf("DISABLE\n");
2808 output = buffer_create(1024);
2810 fatal("Cannot create BUFFER.");
2812 buffer_sprintf(output,
2813 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2814 "DIMENSION user '' incremental 1 1000\n"
2815 "DIMENSION system '' incremental 1 1000\n"
2816 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2817 "DIMENSION files '' incremental 1 1\n"
2818 "DIMENSION pids '' absolute 1 1\n"
2819 "DIMENSION fds '' absolute 1 1\n"
2820 "DIMENSION targets '' absolute 1 1\n"
2821 "CHART netdata.apps_fix '' 'Apps Plugin Normalization Ratios' 'percentage' apps.plugin netdata.apps_fix line 140002 %1$d\n"
2822 "DIMENSION utime '' absolute 1 %2$llu\n"
2823 "DIMENSION stime '' absolute 1 %2$llu\n"
2824 "DIMENSION gtime '' absolute 1 %2$llu\n"
2825 "DIMENSION minflt '' absolute 1 %2$llu\n"
2826 "DIMENSION majflt '' absolute 1 %2$llu\n"
2831 if(include_exited_childs)
2832 buffer_sprintf(output,
2833 "CHART netdata.apps_children_fix '' 'Apps Plugin Exited Children Normalization Ratios' 'percentage' apps.plugin netdata.apps_children_fix line 140003 %1$d\n"
2834 "DIMENSION cutime '' absolute 1 %2$llu\n"
2835 "DIMENSION cstime '' absolute 1 %2$llu\n"
2836 "DIMENSION cgtime '' absolute 1 %2$llu\n"
2837 "DIMENSION cminflt '' absolute 1 %2$llu\n"
2838 "DIMENSION cmajflt '' absolute 1 %2$llu\n"
2843 #ifndef PROFILING_MODE
2844 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2845 unsigned long long sunow;
2846 #endif /* PROFILING_MODE */
2848 global_iterations_counter = 1;
2849 for(;1; global_iterations_counter++) {
2850 #ifndef PROFILING_MODE
2851 // delay until it is our time to run
2852 while((sunow = timems()) < sunext)
2853 usecsleep(sunext - sunow);
2855 // find the next time we need to run
2856 while(timems() > sunext)
2857 sunext += update_every * 1000000ULL;
2858 #endif /* PROFILING_MODE */
2860 if(!collect_data_for_all_processes_from_proc()) {
2861 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2862 printf("DISABLE\n");
2866 calculate_netdata_statistics();
2867 normalize_data(apps_groups_root_target);
2869 unsigned long long dt = send_resource_usage_to_netdata();
2871 // this is smart enough to show only newly added apps, when needed
2872 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2873 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2874 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2876 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2877 send_collected_data_to_netdata(users_root_target, "users", dt);
2878 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2880 show_guest_time_old = show_guest_time;
2882 //if(puts(buffer_tostring(output)) == EOF)
2883 if(write(STDOUT_FILENO, buffer_tostring(output), buffer_strlen(output)) == -1)
2884 fatal("Cannot send chart values to netdata.");
2887 buffer_flush(output);
2890 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2892 current_t = time(NULL);
2894 #ifndef PROFILING_MODE
2895 // restart check (14400 seconds)
2896 if(current_t - started_t > 14400) exit(0);
2898 if(current_t - started_t > 10) exit(0);
2899 #endif /* PROFILING_MODE */