14 #include <sys/resource.h>
25 #include <arpa/inet.h>
27 #include <sys/types.h>
36 #include "../config.h"
38 #ifdef NETDATA_INTERNAL_CHECKS
39 #include <sys/prctl.h>
42 #define MAX_COMPARE_NAME 100
44 #define MAX_CMDLINE 1024
47 pid_t pid_max = 32768;
51 unsigned long long global_iterations_counter = 1;
52 unsigned long long file_counter = 0;
53 int proc_pid_cmdline_is_needed = 0;
54 int include_exited_childs = 1;
55 char *host_prefix = "";
56 char *config_dir = CONFIG_DIR;
58 pid_t *all_pids_sortlist = NULL;
60 // ----------------------------------------------------------------------------
62 void netdata_cleanup_and_exit(int ret) {
67 // ----------------------------------------------------------------------------
69 // to retrieve settings of the system
71 long get_system_cpus(void) {
76 char filename[FILENAME_MAX + 1];
77 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
79 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
82 ff = procfile_readall(ff);
89 for(i = 0; i < procfile_lines(ff); i++) {
90 if(!procfile_linewords(ff, i)) continue;
92 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
95 if(processors < 1) processors = 1;
101 pid_t get_system_pid_max(void) {
105 char filename[FILENAME_MAX + 1];
106 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
107 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
110 ff = procfile_readall(ff);
116 mpid = (pid_t)atoi(procfile_lineword(ff, 0, 0));
117 if(!mpid) mpid = 32768;
123 // ----------------------------------------------------------------------------
125 // target is the structure that process data are aggregated
128 char compare[MAX_COMPARE_NAME + 1];
129 uint32_t comparehash;
132 char id[MAX_NAME + 1];
135 char name[MAX_NAME + 1];
140 unsigned long long minflt;
141 unsigned long long cminflt;
142 unsigned long long majflt;
143 unsigned long long cmajflt;
144 unsigned long long utime;
145 unsigned long long stime;
146 unsigned long long cutime;
147 unsigned long long cstime;
148 unsigned long long num_threads;
149 unsigned long long rss;
151 unsigned long long statm_size;
152 unsigned long long statm_resident;
153 unsigned long long statm_share;
154 unsigned long long statm_text;
155 unsigned long long statm_lib;
156 unsigned long long statm_data;
157 unsigned long long statm_dirty;
159 unsigned long long io_logical_bytes_read;
160 unsigned long long io_logical_bytes_written;
161 unsigned long long io_read_calls;
162 unsigned long long io_write_calls;
163 unsigned long long io_storage_bytes_read;
164 unsigned long long io_storage_bytes_written;
165 unsigned long long io_cancelled_write_bytes;
168 unsigned long long openfiles;
169 unsigned long long openpipes;
170 unsigned long long opensockets;
171 unsigned long long openinotifies;
172 unsigned long long openeventfds;
173 unsigned long long opentimerfds;
174 unsigned long long opensignalfds;
175 unsigned long long openeventpolls;
176 unsigned long long openother;
178 unsigned long processes; // how many processes have been merged to this
179 int exposed; // if set, we have sent this to netdata
180 int hidden; // if set, we set the hidden flag on the dimension
183 int starts_with; // if set, the compare string matches only the
184 // beginning of the command
186 struct target *target; // the one that will be reported to netdata
191 // ----------------------------------------------------------------------------
193 // aggregate all processes in groups, to have a limited number of dimensions
195 struct target *apps_groups_root_target = NULL;
196 struct target *apps_groups_default_target = NULL;
197 long apps_groups_targets = 0;
199 struct target *users_root_target = NULL;
200 struct target *groups_root_target = NULL;
202 struct target *get_users_target(uid_t uid)
205 for(w = users_root_target ; w ; w = w->next)
206 if(w->uid == uid) return w;
208 w = calloc(sizeof(struct target), 1);
210 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
214 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", uid);
215 w->comparehash = simple_hash(w->compare);
216 w->comparelen = strlen(w->compare);
218 snprintfz(w->id, MAX_NAME, "%u", uid);
219 w->idhash = simple_hash(w->id);
221 struct passwd *pw = getpwuid(uid);
223 snprintfz(w->name, MAX_NAME, "%u", uid);
225 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
227 netdata_fix_chart_name(w->name);
231 w->next = users_root_target;
232 users_root_target = w;
235 fprintf(stderr, "apps.plugin: added uid %u ('%s') target\n", w->uid, w->name);
240 struct target *get_groups_target(gid_t gid)
243 for(w = groups_root_target ; w ; w = w->next)
244 if(w->gid == gid) return w;
246 w = calloc(sizeof(struct target), 1);
248 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
252 snprintfz(w->compare, MAX_COMPARE_NAME, "%u", gid);
253 w->comparehash = simple_hash(w->compare);
254 w->comparelen = strlen(w->compare);
256 snprintfz(w->id, MAX_NAME, "%u", gid);
257 w->idhash = simple_hash(w->id);
259 struct group *gr = getgrgid(gid);
261 snprintfz(w->name, MAX_NAME, "%u", gid);
263 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
265 netdata_fix_chart_name(w->name);
269 w->next = groups_root_target;
270 groups_root_target = w;
273 fprintf(stderr, "apps.plugin: added gid %u ('%s') target\n", w->gid, w->name);
278 // find or create a new target
279 // there are targets that are just aggregated to other target (the second argument)
280 struct target *get_apps_groups_target(const char *id, struct target *target)
282 int tdebug = 0, thidden = 0, ends_with = 0;
283 const char *nid = id;
285 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
286 if(nid[0] == '-') thidden = 1;
287 if(nid[0] == '+') tdebug = 1;
288 if(nid[0] == '*') ends_with = 1;
291 uint32_t hash = simple_hash(id);
293 struct target *w, *last = apps_groups_root_target;
294 for(w = apps_groups_root_target ; w ; w = w->next) {
295 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
301 w = calloc(sizeof(struct target), 1);
303 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
307 strncpyz(w->id, nid, MAX_NAME);
308 w->idhash = simple_hash(w->id);
310 strncpyz(w->name, nid, MAX_NAME);
312 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
313 int len = strlen(w->compare);
314 if(w->compare[len - 1] == '*') {
315 w->compare[len - 1] = '\0';
318 w->ends_with = ends_with;
320 if(w->starts_with && w->ends_with)
321 proc_pid_cmdline_is_needed = 1;
323 w->comparehash = simple_hash(w->compare);
324 w->comparelen = strlen(w->compare);
330 // append it, to maintain the order in apps_groups.conf
331 if(last) last->next = w;
332 else apps_groups_root_target = w;
335 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
337 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
338 , w->target?w->target->id:w->id
339 , (w->hidden)?"hidden":"-"
340 , (w->debug)?"debug":"-"
346 // read the apps_groups.conf file
347 int read_apps_groups_conf(const char *name)
349 char filename[FILENAME_MAX + 1];
351 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
354 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
356 // ----------------------------------------
358 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
361 procfile_set_quotes(ff, "'\"");
363 ff = procfile_readall(ff);
369 unsigned long line, lines = procfile_lines(ff);
371 for(line = 0; line < lines ;line++) {
372 unsigned long word, words = procfile_linewords(ff, line);
373 struct target *w = NULL;
375 char *t = procfile_lineword(ff, line, 0);
376 if(!t || !*t) continue;
378 for(word = 0; word < words ;word++) {
379 char *s = procfile_lineword(ff, line, word);
380 if(!s || !*s) continue;
385 struct target *n = get_apps_groups_target(s, w);
387 error("Cannot create target '%s' (line %lu, word %lu)", s, line, word);
395 int tdebug = 0, thidden = 0;
397 while(t[0] == '-' || t[0] == '+') {
398 if(t[0] == '-') thidden = 1;
399 if(t[0] == '+') tdebug = 1;
403 strncpyz(w->name, t, MAX_NAME);
408 fprintf(stderr, "apps.plugin: AGGREGATION TARGET NAME '%s' on ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
411 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
412 , w->target?w->target->id:w->id
413 , (w->hidden)?"hidden":"-"
414 , (w->debug)?"debug":"-"
421 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
422 if(!apps_groups_default_target)
423 error("Cannot create default target");
425 strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
431 // ----------------------------------------------------------------------------
432 // data to store for each pid
437 char comm[MAX_COMPARE_NAME + 1];
438 char cmdline[MAX_CMDLINE + 1];
448 // these are raw values collected
449 unsigned long long minflt_raw;
450 unsigned long long cminflt_raw;
451 unsigned long long majflt_raw;
452 unsigned long long cmajflt_raw;
453 unsigned long long utime_raw;
454 unsigned long long stime_raw;
455 unsigned long long cutime_raw;
456 unsigned long long cstime_raw;
459 unsigned long long minflt;
460 unsigned long long cminflt;
461 unsigned long long majflt;
462 unsigned long long cmajflt;
463 unsigned long long utime;
464 unsigned long long stime;
465 unsigned long long cutime;
466 unsigned long long cstime;
471 // int64_t itrealvalue;
472 // unsigned long long starttime;
473 // unsigned long long vsize;
474 unsigned long long rss;
475 // unsigned long long rsslim;
476 // unsigned long long starcode;
477 // unsigned long long endcode;
478 // unsigned long long startstack;
479 // unsigned long long kstkesp;
480 // unsigned long long kstkeip;
483 // uint64_t sigignore;
484 // uint64_t sigcatch;
488 // int32_t exit_signal;
489 // int32_t processor;
490 // uint32_t rt_priority;
492 // unsigned long long delayacct_blkio_ticks;
493 // uint64_t guest_time;
494 // int64_t cguest_time;
499 unsigned long long statm_size;
500 unsigned long long statm_resident;
501 unsigned long long statm_share;
502 unsigned long long statm_text;
503 unsigned long long statm_lib;
504 unsigned long long statm_data;
505 unsigned long long statm_dirty;
507 unsigned long long io_logical_bytes_read_raw;
508 unsigned long long io_logical_bytes_written_raw;
509 unsigned long long io_read_calls_raw;
510 unsigned long long io_write_calls_raw;
511 unsigned long long io_storage_bytes_read_raw;
512 unsigned long long io_storage_bytes_written_raw;
513 unsigned long long io_cancelled_write_bytes_raw;
515 unsigned long long io_logical_bytes_read;
516 unsigned long long io_logical_bytes_written;
517 unsigned long long io_read_calls;
518 unsigned long long io_write_calls;
519 unsigned long long io_storage_bytes_read;
520 unsigned long long io_storage_bytes_written;
521 unsigned long long io_cancelled_write_bytes;
523 int *fds; // array of fds it uses
524 int fds_size; // the size of the fds array
526 int children_count; // number of processes directly referencing this
527 int keep; // 1 when we need to keep this process in memory even after it exited
528 int keeploops; // increases by 1 every time keep is 1 and updated 0
529 int updated; // 1 when the process is currently running
530 int merged; // 1 when it has been merged to its parent
531 int new_entry; // 1 when this is a new process, just saw for the first time
532 int read; // 1 when we have already read this process for this iteration
533 int sortlist; // higher numbers = top on the process tree
534 // each process gets a unique number
536 struct target *target; // app_groups.conf targets
537 struct target *user_target; // uid based targets
538 struct target *group_target; // gid based targets
540 unsigned long long stat_collected_usec;
541 unsigned long long last_stat_collected_usec;
543 unsigned long long io_collected_usec;
544 unsigned long long last_io_collected_usec;
547 char *statm_filename;
549 char *cmdline_filename;
551 struct pid_stat *parent;
552 struct pid_stat *prev;
553 struct pid_stat *next;
554 } *root_of_pids = NULL, **all_pids;
556 long all_pids_count = 0;
558 struct pid_stat *get_pid_entry(pid_t pid) {
560 all_pids[pid]->new_entry = 0;
561 return all_pids[pid];
564 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
566 error("Cannot allocate %zu bytes of memory", (size_t)sizeof(struct pid_stat));
570 all_pids[pid]->fds = calloc(sizeof(int), 100);
571 if(!all_pids[pid]->fds)
572 error("Cannot allocate %zu bytes of memory", (size_t)(sizeof(int) * 100));
573 else all_pids[pid]->fds_size = 100;
575 if(root_of_pids) root_of_pids->prev = all_pids[pid];
576 all_pids[pid]->next = root_of_pids;
577 root_of_pids = all_pids[pid];
579 all_pids[pid]->pid = pid;
580 all_pids[pid]->new_entry = 1;
584 return all_pids[pid];
587 void del_pid_entry(pid_t pid) {
589 error("attempted to free pid %d that is not allocated.", pid);
594 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
596 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
597 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
598 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
600 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
601 if(all_pids[pid]->stat_filename) free(all_pids[pid]->stat_filename);
602 if(all_pids[pid]->statm_filename) free(all_pids[pid]->statm_filename);
603 if(all_pids[pid]->io_filename) free(all_pids[pid]->io_filename);
604 if(all_pids[pid]->cmdline_filename) free(all_pids[pid]->cmdline_filename);
607 all_pids[pid] = NULL;
612 // ----------------------------------------------------------------------------
613 // update pids from proc
615 int read_proc_pid_cmdline(struct pid_stat *p) {
617 if(unlikely(!p->cmdline_filename)) {
618 char filename[FILENAME_MAX + 1];
619 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
620 if(!(p->cmdline_filename = strdup(filename)))
621 fatal("Cannot allocate memory for filename '%s'", filename);
624 int fd = open(p->cmdline_filename, O_RDONLY, 0666);
625 if(unlikely(fd == -1)) goto cleanup;
627 int i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
630 if(unlikely(bytes <= 0)) goto cleanup;
632 p->cmdline[bytes] = '\0';
633 for(i = 0; i < bytes ; i++)
634 if(unlikely(!p->cmdline[i])) p->cmdline[i] = ' ';
637 fprintf(stderr, "Read file '%s' contents: %s\n", p->cmdline_filename, p->cmdline);
642 // copy the command to the command line
643 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
647 int read_proc_pid_ownership(struct pid_stat *p) {
648 if(unlikely(!p->stat_filename)) {
649 error("pid %d does not have a stat_filename", p->pid);
653 // ----------------------------------------
657 if(stat(p->stat_filename, &st) != 0) {
658 error("Cannot stat file '%s'", p->stat_filename);
668 int read_proc_pid_stat(struct pid_stat *p) {
669 static procfile *ff = NULL;
671 if(unlikely(!p->stat_filename)) {
672 char filename[FILENAME_MAX + 1];
673 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
674 if(!(p->stat_filename = strdup(filename)))
675 fatal("Cannot allocate memory for filename '%s'", filename);
678 int set_quotes = (!ff)?1:0;
680 ff = procfile_reopen(ff, p->stat_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
681 if(unlikely(!ff)) goto cleanup;
683 // if(set_quotes) procfile_set_quotes(ff, "()");
684 if(set_quotes) procfile_set_open_close(ff, "(", ")");
686 ff = procfile_readall(ff);
687 if(unlikely(!ff)) goto cleanup;
689 p->last_stat_collected_usec = p->stat_collected_usec;
690 p->stat_collected_usec = timems();
693 // parse the process name
695 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
697 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
699 // p->state = *(procfile_lineword(ff, 0, 2+i));
700 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3 + i));
701 // p->pgrp = atol(procfile_lineword(ff, 0, 4+i));
702 // p->session = atol(procfile_lineword(ff, 0, 5+i));
703 // p->tty_nr = atol(procfile_lineword(ff, 0, 6+i));
704 // p->tpgid = atol(procfile_lineword(ff, 0, 7+i));
705 // p->flags = strtoull(procfile_lineword(ff, 0, 8+i), NULL, 10);
707 unsigned long long last;
709 last = p->minflt_raw;
710 p->minflt_raw = strtoull(procfile_lineword(ff, 0, 9+i), NULL, 10);
711 p->minflt = (p->minflt_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
713 last = p->cminflt_raw;
714 p->cminflt_raw = strtoull(procfile_lineword(ff, 0, 10+i), NULL, 10);
715 p->cminflt = (p->cminflt_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
717 last = p->majflt_raw;
718 p->majflt_raw = strtoull(procfile_lineword(ff, 0, 11+i), NULL, 10);
719 p->majflt = (p->majflt_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
721 last = p->cmajflt_raw;
722 p->cmajflt_raw = strtoull(procfile_lineword(ff, 0, 12+i), NULL, 10);
723 p->cmajflt = (p->cmajflt_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
726 p->utime_raw = strtoull(procfile_lineword(ff, 0, 13+i), NULL, 10);
727 p->utime = (p->utime_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
730 p->stime_raw = strtoull(procfile_lineword(ff, 0, 14+i), NULL, 10);
731 p->stime = (p->stime_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
733 last = p->cutime_raw;
734 p->cutime_raw = strtoull(procfile_lineword(ff, 0, 15+i), NULL, 10);
735 p->cutime = (p->cutime_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
737 last = p->cstime_raw;
738 p->cstime_raw = strtoull(procfile_lineword(ff, 0, 16+i), NULL, 10);
739 p->cstime = (p->cstime_raw - last) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
741 // p->priority = strtoull(procfile_lineword(ff, 0, 17+i), NULL, 10);
742 // p->nice = strtoull(procfile_lineword(ff, 0, 18+i), NULL, 10);
743 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19 + i));
744 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20+i), NULL, 10);
745 // p->starttime = strtoull(procfile_lineword(ff, 0, 21+i), NULL, 10);
746 // p->vsize = strtoull(procfile_lineword(ff, 0, 22+i), NULL, 10);
747 p->rss = strtoull(procfile_lineword(ff, 0, 23+i), NULL, 10);
748 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24+i), NULL, 10);
749 // p->starcode = strtoull(procfile_lineword(ff, 0, 25+i), NULL, 10);
750 // p->endcode = strtoull(procfile_lineword(ff, 0, 26+i), NULL, 10);
751 // p->startstack = strtoull(procfile_lineword(ff, 0, 27+i), NULL, 10);
752 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28+i), NULL, 10);
753 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29+i), NULL, 10);
754 // p->signal = strtoull(procfile_lineword(ff, 0, 30+i), NULL, 10);
755 // p->blocked = strtoull(procfile_lineword(ff, 0, 31+i), NULL, 10);
756 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32+i), NULL, 10);
757 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33+i), NULL, 10);
758 // p->wchan = strtoull(procfile_lineword(ff, 0, 34+i), NULL, 10);
759 // p->nswap = strtoull(procfile_lineword(ff, 0, 35+i), NULL, 10);
760 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36+i), NULL, 10);
761 // p->exit_signal = atol(procfile_lineword(ff, 0, 37+i));
762 // p->processor = atol(procfile_lineword(ff, 0, 38+i));
763 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39+i), NULL, 10);
764 // p->policy = strtoul(procfile_lineword(ff, 0, 40+i), NULL, 10);
765 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41+i), NULL, 10);
766 // p->guest_time = strtoull(procfile_lineword(ff, 0, 42+i), NULL, 10);
767 // p->cguest_time = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
769 if(unlikely(debug || (p->target && p->target->debug)))
770 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);
772 if(unlikely(global_iterations_counter == 1)) {
799 int read_proc_pid_statm(struct pid_stat *p) {
800 static procfile *ff = NULL;
802 if(unlikely(!p->statm_filename)) {
803 char filename[FILENAME_MAX + 1];
804 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
805 if(!(p->statm_filename = strdup(filename)))
806 fatal("Cannot allocate memory for filename '%s'", filename);
809 ff = procfile_reopen(ff, p->statm_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
810 if(unlikely(!ff)) goto cleanup;
812 ff = procfile_readall(ff);
813 if(unlikely(!ff)) goto cleanup;
817 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
818 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
819 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
820 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
821 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
822 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
823 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
829 p->statm_resident = 0;
838 int read_proc_pid_io(struct pid_stat *p) {
839 static procfile *ff = NULL;
841 if(unlikely(!p->io_filename)) {
842 char filename[FILENAME_MAX + 1];
843 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
844 if(!(p->io_filename = strdup(filename)))
845 fatal("Cannot allocate memory for filename '%s'", filename);
849 ff = procfile_reopen(ff, p->io_filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
850 if(unlikely(!ff)) goto cleanup;
852 ff = procfile_readall(ff);
853 if(unlikely(!ff)) goto cleanup;
857 p->last_io_collected_usec = p->io_collected_usec;
858 p->io_collected_usec = timems();
860 unsigned long long last;
862 last = p->io_logical_bytes_read_raw;
863 p->io_logical_bytes_read_raw = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
864 p->io_logical_bytes_read = (p->io_logical_bytes_read_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
866 last = p->io_logical_bytes_written_raw;
867 p->io_logical_bytes_written_raw = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
868 p->io_logical_bytes_written = (p->io_logical_bytes_written_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
870 last = p->io_read_calls_raw;
871 p->io_read_calls_raw = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
872 p->io_read_calls = (p->io_read_calls_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
874 last = p->io_write_calls_raw;
875 p->io_write_calls_raw = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
876 p->io_write_calls = (p->io_write_calls_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
878 last = p->io_storage_bytes_read_raw;
879 p->io_storage_bytes_read_raw = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
880 p->io_storage_bytes_read = (p->io_storage_bytes_read_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
882 last = p->io_storage_bytes_written_raw;
883 p->io_storage_bytes_written_raw = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
884 p->io_storage_bytes_written = (p->io_storage_bytes_written_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
886 last = p->io_cancelled_write_bytes_raw;
887 p->io_cancelled_write_bytes_raw = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
888 p->io_cancelled_write_bytes = (p->io_cancelled_write_bytes_raw - last) * (1000000 * 100) / (p->io_collected_usec - p->last_io_collected_usec);
890 if(unlikely(global_iterations_counter == 1)) {
891 p->io_logical_bytes_read = 0;
892 p->io_logical_bytes_written = 0;
893 p->io_read_calls = 0;
894 p->io_write_calls = 0;
895 p->io_storage_bytes_read = 0;
896 p->io_storage_bytes_written = 0;
897 p->io_cancelled_write_bytes = 0;
903 p->io_logical_bytes_read = 0;
904 p->io_logical_bytes_written = 0;
905 p->io_read_calls = 0;
906 p->io_write_calls = 0;
907 p->io_storage_bytes_read = 0;
908 p->io_storage_bytes_written = 0;
909 p->io_cancelled_write_bytes = 0;
914 // ----------------------------------------------------------------------------
916 // this is used to keep a global list of all open files of the system
917 // it is needed in order to calculate the unique files processes have open
919 #define FILE_DESCRIPTORS_INCREASE_STEP 100
921 struct file_descriptor {
923 #ifdef NETDATA_INTERNAL_CHECKS
925 #endif /* NETDATA_INTERNAL_CHECKS */
933 int all_files_len = 0;
934 int all_files_size = 0;
936 int file_descriptor_compare(void* a, void* b) {
937 #ifdef NETDATA_INTERNAL_CHECKS
938 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
939 error("Corrupted index data detected. Please report this.");
940 #endif /* NETDATA_INTERNAL_CHECKS */
942 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
945 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
949 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
952 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
954 avl_tree all_files_index = {
956 file_descriptor_compare
959 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
960 struct file_descriptor tmp;
961 tmp.hash = (hash)?hash:simple_hash(name);
965 #ifdef NETDATA_INTERNAL_CHECKS
966 tmp.magic = 0x0BADCAFE;
967 #endif /* NETDATA_INTERNAL_CHECKS */
969 return (struct file_descriptor *)avl_search(&all_files_index, (avl *) &tmp);
972 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
973 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
975 #define FILETYPE_OTHER 0
976 #define FILETYPE_FILE 1
977 #define FILETYPE_PIPE 2
978 #define FILETYPE_SOCKET 3
979 #define FILETYPE_INOTIFY 4
980 #define FILETYPE_EVENTFD 5
981 #define FILETYPE_EVENTPOLL 6
982 #define FILETYPE_TIMERFD 7
983 #define FILETYPE_SIGNALFD 8
985 void file_descriptor_not_used(int id)
987 if(id > 0 && id < all_files_size) {
989 #ifdef NETDATA_INTERNAL_CHECKS
990 if(all_files[id].magic != 0x0BADCAFE) {
991 error("Ignoring request to remove empty file id %d.", id);
994 #endif /* NETDATA_INTERNAL_CHECKS */
997 fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
999 if(all_files[id].count > 0) {
1000 all_files[id].count--;
1002 if(!all_files[id].count) {
1004 fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1006 file_descriptor_remove(&all_files[id]);
1007 #ifdef NETDATA_INTERNAL_CHECKS
1008 all_files[id].magic = 0x00000000;
1009 #endif /* NETDATA_INTERNAL_CHECKS */
1014 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1016 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1019 int file_descriptor_find_or_add(const char *name)
1021 static int last_pos = 0;
1022 uint32_t hash = simple_hash(name);
1025 fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1027 struct file_descriptor *fd = file_descriptor_find(name, hash);
1031 fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1038 // check we have enough memory to add it
1039 if(!all_files || all_files_len == all_files_size) {
1040 void *old = all_files;
1043 // there is no empty slot
1045 fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1047 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1049 // if the address changed, we have to rebuild the index
1050 // since all pointers are now invalid
1051 if(old && old != (void *)all_files) {
1053 fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1055 all_files_index.root = NULL;
1056 for(i = 0; i < all_files_size; i++) {
1057 if(!all_files[i].count) continue;
1058 file_descriptor_add(&all_files[i]);
1062 fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1065 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1066 all_files[i].count = 0;
1067 all_files[i].name = NULL;
1068 #ifdef NETDATA_INTERNAL_CHECKS
1069 all_files[i].magic = 0x00000000;
1070 #endif /* NETDATA_INTERNAL_CHECKS */
1071 all_files[i].pos = i;
1074 if(!all_files_size) all_files_len = 1;
1075 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1079 fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1081 // search for an empty slot
1083 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1084 if(c >= all_files_size) c = 0;
1085 if(c == 0) continue;
1087 if(!all_files[c].count) {
1089 fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1091 #ifdef NETDATA_INTERNAL_CHECKS
1092 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1093 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1094 #endif /* NETDATA_INTERNAL_CHECKS */
1097 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);
1099 if(all_files[c].name) free((void *)all_files[c].name);
1100 all_files[c].name = NULL;
1105 if(i == all_files_size) {
1106 fatal("We should find an empty slot, but there isn't any");
1111 fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1115 // else we have an empty slot in 'c'
1118 if(name[0] == '/') type = FILETYPE_FILE;
1119 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1120 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1121 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1122 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1123 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1124 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1125 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1126 else if(strncmp(name, "anon_inode:", 11) == 0) {
1128 fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1130 type = FILETYPE_OTHER;
1134 fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1136 type = FILETYPE_OTHER;
1139 all_files[c].name = strdup(name);
1140 all_files[c].hash = hash;
1141 all_files[c].type = type;
1142 all_files[c].pos = c;
1143 all_files[c].count = 1;
1144 #ifdef NETDATA_INTERNAL_CHECKS
1145 all_files[c].magic = 0x0BADCAFE;
1146 #endif /* NETDATA_INTERNAL_CHECKS */
1147 file_descriptor_add(&all_files[c]);
1150 fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1155 int read_pid_file_descriptors(struct pid_stat *p) {
1156 char dirname[FILENAME_MAX+1];
1158 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
1159 DIR *fds = opendir(dirname);
1163 char fdname[FILENAME_MAX + 1];
1164 char linkname[FILENAME_MAX + 1];
1166 // make the array negative
1167 for(c = 0 ; c < p->fds_size ; c++)
1168 p->fds[c] = -p->fds[c];
1170 while((de = readdir(fds))) {
1171 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1174 // check if the fds array is small
1175 int fdid = atoi(de->d_name);
1176 if(fdid < 0) continue;
1177 if(fdid >= p->fds_size) {
1178 // it is small, extend it
1180 fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1182 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1184 fatal("Cannot re-allocate fds for %s", p->comm);
1188 // and initialize it
1189 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1190 p->fds_size = fdid + 100;
1193 if(p->fds[fdid] == 0) {
1194 // we don't know this fd, get it
1196 sprintf(fdname, "%s/proc/%d/fd/%s", host_prefix, p->pid, de->d_name);
1197 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1199 if(debug || (p->target && p->target->debug)) {
1200 if(debug || (p->target && p->target->debug))
1201 error("Cannot read link %s", fdname);
1208 // if another process already has this, we will get
1210 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1213 // else make it positive again, we need it
1214 // of course, the actual file may have changed, but we don't care so much
1215 // FIXME: we could compare the inode as returned by readdir direct structure
1216 else p->fds[fdid] = -p->fds[fdid];
1220 // remove all the negative file descriptors
1221 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1222 file_descriptor_not_used(-p->fds[c]);
1231 // ----------------------------------------------------------------------------
1233 #ifdef NETDATA_INTERNAL_CHECKS
1234 void find_lost_child_debug(struct pid_stat *pe, struct pid_stat *ppe, unsigned long long lost, int type) {
1236 struct pid_stat *p = NULL, *pp = pe->parent;
1239 fprintf(stderr, "Searching for candidate of lost resources of process %d (%s, %s) which is aggregated on %d (%s, %s)\n", pe->pid, pe->comm, pe->updated?"running":"exited", ppe->pid, ppe->comm, ppe->updated?"running":"exited");
1241 fprintf(stderr, " >> parent %d (%s, %s)\n", pp->pid, pp->comm, pp->updated?"running":"exited");
1245 for(p = root_of_pids; p ; p = p->next) {
1246 if(p == pe) continue;
1250 if(p->cminflt > lost) {
1251 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);
1257 if(p->cmajflt > lost) {
1258 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);
1264 if(p->cutime > lost) {
1265 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);
1271 if(p->cstime > lost) {
1272 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);
1282 fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1286 fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1290 fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1294 fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1299 #endif /* NETDATA_INTERNAL_CHECKS */
1301 void remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield, unsigned long long *ifield, struct pid_stat *pe, struct pid_stat *ppe, int type) {
1303 if(*field > *pfield) {
1314 if(ifield && ifield != pfield) {
1315 if(*field > *ifield) {
1327 #ifdef NETDATA_INTERNAL_CHECKS
1328 find_lost_child_debug(pe, ppe, *field, type);
1330 while(pe && !pe->updated) {
1337 void process_exited_processes() {
1338 struct pid_stat *init = all_pids[1];
1341 for(p = root_of_pids; p ; p = p->next) {
1342 if(p->updated || !p->stat_collected_usec) continue;
1344 struct pid_stat *pp = p->parent;
1346 // find the first parent that is running
1347 while(pp && !pp->updated)
1350 unsigned long long rate;
1352 rate = (p->utime_raw + p->cutime_raw) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
1353 remove_exited_child_from_parent(&rate, (pp)?&pp->cutime:NULL, (init)?&init->cutime:NULL, p, pp, 3);
1355 p->utime_raw = rate * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000 * 100);
1357 rate = (p->stime_raw + p->cstime_raw) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
1358 remove_exited_child_from_parent(&rate, (pp)?&pp->cstime:NULL, (init)?&init->cstime:NULL, p, pp, 4);
1360 p->stime_raw = rate * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000 * 100);
1362 rate = (p->minflt_raw + p->cminflt_raw) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
1363 remove_exited_child_from_parent(&rate, (pp)?&pp->cminflt:NULL, (init)?&init->cminflt:NULL, p, pp, 1);
1365 p->minflt_raw = rate * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000 * 100);
1367 rate = (p->majflt_raw + p->cmajflt_raw) * (1000000 * 100) / (p->stat_collected_usec - p->last_stat_collected_usec);
1368 remove_exited_child_from_parent(&rate, (pp)?&pp->cmajflt:NULL, (init)?&init->cmajflt:NULL, p, pp, 2);
1370 p->majflt_raw = rate * (p->stat_collected_usec - p->last_stat_collected_usec) / (1000000 * 100);
1374 void link_all_processes_to_their_parents(void) {
1375 struct pid_stat *p = NULL;
1377 // link all children to their parents
1378 // and update children count on parents
1379 for(p = root_of_pids; p ; p = p->next) {
1380 // for each process found running
1382 if(likely(p->ppid > 0 && all_pids[p->ppid])) {
1383 // valid parent processes
1385 struct pid_stat *pp;
1387 p->parent = pp = all_pids[p->ppid];
1388 p->parent->children_count++;
1390 if(unlikely(debug || (p->target && p->target->debug)))
1391 fprintf(stderr, "apps.plugin: \tchild %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=%llu, stime=%llu, minflt=%llu, majflt=%llu, cutime=%llu, cstime=%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->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cminflt, pp->cmajflt);
1393 else if(unlikely(p->ppid != 0))
1394 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1400 // ----------------------------------------------------------------------------
1402 // 1. read all files in /proc
1403 // 2. for each numeric directory:
1404 // i. read /proc/pid/stat
1405 // ii. read /proc/pid/statm
1406 // iii. read /proc/pid/io (requires root access)
1407 // iii. read the entries in directory /proc/pid/fd (requires root access)
1409 // a. find or create a struct file_descriptor
1410 // b. cleanup any old/unused file_descriptors
1412 // after all these, some pids may be linked to targets, while others may not
1414 // in case of errors, only 1 every 1000 errors is printed
1415 // to avoid filling up all disk space
1416 // if debug is enabled, all errors are printed
1418 static int compar_pid(const void *pid1, const void *pid2) {
1420 struct pid_stat *p1 = all_pids[*((pid_t *)pid1)];
1421 struct pid_stat *p2 = all_pids[*((pid_t *)pid2)];
1423 if(p1->sortlist > p2->sortlist)
1429 void collect_data_for_pid(pid_t pid) {
1430 if(unlikely(pid <= 0 || pid > pid_max)) {
1431 error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1435 struct pid_stat *p = get_pid_entry(pid);
1436 if(unlikely(!p || p->read)) return;
1439 // fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1441 // --------------------------------------------------------------------
1444 if(unlikely(read_proc_pid_stat(p))) {
1445 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1446 // there is no reason to proceed if we cannot get its status
1450 read_proc_pid_ownership(p);
1452 // check its parent pid
1453 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1454 error("Pid %d states invalid parent pid %d. Using 0.", pid, p->ppid);
1458 // --------------------------------------------------------------------
1461 if(unlikely(read_proc_pid_io(p)))
1462 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1464 // --------------------------------------------------------------------
1465 // /proc/<pid>/statm
1467 if(unlikely(read_proc_pid_statm(p))) {
1468 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1469 // there is no reason to proceed if we cannot get its memory status
1473 // --------------------------------------------------------------------
1476 // check if it is target
1477 // we do this only once, the first time this pid is loaded
1478 if(unlikely(p->new_entry)) {
1479 // /proc/<pid>/cmdline
1480 if(likely(proc_pid_cmdline_is_needed)) {
1481 if(unlikely(read_proc_pid_cmdline(p)))
1482 error("Cannot process %s/proc/%d/cmdline", host_prefix, pid);
1486 fprintf(stderr, "apps.plugin: \tJust added %d (%s)\n", pid, p->comm);
1488 uint32_t hash = simple_hash(p->comm);
1489 size_t pclen = strlen(p->comm);
1492 for(w = apps_groups_root_target; w ; w = w->next) {
1493 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1495 // find it - 4 cases:
1496 // 1. the target is not a pattern
1497 // 2. the target has the prefix
1498 // 3. the target has the suffix
1499 // 4. the target is something inside cmdline
1500 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1501 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1502 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1503 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1505 if(w->target) p->target = w->target;
1508 if(debug || (p->target && p->target->debug))
1509 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1516 // --------------------------------------------------------------------
1519 if(unlikely(read_pid_file_descriptors(p))) {
1520 error("Cannot process entries in %s/proc/%d/fd", host_prefix, pid);
1523 // --------------------------------------------------------------------
1526 #ifdef NETDATA_INTERNAL_CHECKS
1527 if(unlikely(all_pids_count && p->ppid && all_pids[p->ppid] && !all_pids[p->ppid]->read))
1528 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);
1531 // mark it as updated
1537 int collect_data_for_all_processes_from_proc(void) {
1538 struct pid_stat *p = NULL;
1540 if(all_pids_count) {
1541 // read parents before childs
1542 // this is needed to prevent a situation where
1543 // a child is found running, but until we read
1544 // its parent, it has exited and its parent
1545 // has accumulated its resources
1548 for(p = root_of_pids; p ; p = p->next) {
1553 p->children_count = 0;
1556 #ifdef NETDATA_INTERNAL_CHECKS
1557 if(unlikely(slc >= all_pids_count))
1558 error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1560 all_pids_sortlist[slc++] = p->pid;
1563 qsort((void *)all_pids_sortlist, all_pids_count, sizeof(pid_t), compar_pid);
1565 for(slc = 0; slc < all_pids_count; slc++)
1566 collect_data_for_pid(all_pids_sortlist[slc]);
1569 char dirname[FILENAME_MAX + 1];
1571 snprintfz(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1572 DIR *dir = opendir(dirname);
1575 struct dirent *file = NULL;
1577 while((file = readdir(dir))) {
1578 char *endptr = file->d_name;
1579 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1581 // make sure we read a valid number
1582 if(unlikely(endptr == file->d_name || *endptr != '\0'))
1585 collect_data_for_pid(pid);
1589 // normally this is done
1590 // however we may have processes exited while we collected values
1591 // so let's find the exited ones
1592 // we do this by collecting the ownership of process
1593 // if we manage to get the ownership, the process still runs
1595 link_all_processes_to_their_parents();
1596 process_exited_processes();
1601 // ----------------------------------------------------------------------------
1602 // update statistics on the targets
1604 // 1. link all childs to their parents
1605 // 2. go from bottom to top, marking as merged all childs to their parents
1606 // this step links all parents without a target to the child target, if any
1607 // 3. link all top level processes (the ones not merged) to the default target
1608 // 4. go from top to bottom, linking all childs without a target, to their parent target
1609 // after this step, all processes have a target
1610 // [5. for each killed pid (updated = 0), remove its usage from its target]
1611 // 6. zero all apps_groups_targets
1612 // 7. concentrate all values on the apps_groups_targets
1613 // 8. remove all killed processes
1614 // 9. find the unique file count for each target
1615 // check: update_apps_groups_statistics()
1617 void cleanup_exited_pids(void) {
1619 struct pid_stat *p = NULL;
1621 for(p = root_of_pids; p ;) {
1622 if(!p->updated && (!p->keep || p->keeploops > 1)) {
1623 // fprintf(stderr, "\tEXITED %d %s [parent %d %s, target %s] utime=%llu, stime=%llu, cutime=%llu, cstime=%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->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1625 #ifdef NETDATA_INTERNAL_CHECKS
1627 fprintf(stderr, " > cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1630 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1631 file_descriptor_not_used(p->fds[c]);
1640 if(unlikely(p->keep)) p->keeploops++;
1647 void apply_apps_groups_targets_inheritance(void) {
1648 struct pid_stat *p = NULL;
1650 // children that do not have a target
1651 // inherit their target from their parent
1652 int found = 1, loops = 0;
1654 if(unlikely(debug)) loops++;
1656 for(p = root_of_pids; p ; p = p->next) {
1657 // if this process does not have a target
1658 // and it has a parent
1659 // and its parent has a target
1660 // then, set the parent's target to this process
1661 if(unlikely(!p->target && p->parent && p->parent->target)) {
1662 p->target = p->parent->target;
1665 if(debug || (p->target && p->target->debug))
1666 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);
1671 // find all the procs with 0 childs and merge them to their parents
1672 // repeat, until nothing more can be done.
1676 if(unlikely(debug)) loops++;
1679 for(p = root_of_pids; p ; p = p->next) {
1680 // if this process does not have any children
1681 // and is not already merged
1683 // and its parent has children
1684 // and the target of this process and its parent is the same, or the parent does not have a target
1685 // and its parent is not init
1686 // then, mark them as merged.
1691 && p->parent->children_count
1692 && (p->target == p->parent->target || !p->parent->target)
1695 p->parent->children_count--;
1698 // the parent inherits the child's target, if it does not have a target itself
1699 if(unlikely(p->target && !p->parent->target)) {
1700 p->parent->target = p->target;
1702 if(debug || (p->target && p->target->debug))
1703 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);
1709 // since this process does not have any childs
1710 // assign it to the current sortlist
1711 if(unlikely(!p->sortlist && !p->children_count))
1712 p->sortlist = sortlist++;
1716 fprintf(stderr, "apps.plugin: TARGET INHERITANCE: merged %d processes\n", found);
1719 // init goes always to default target
1721 all_pids[1]->target = apps_groups_default_target;
1723 // give a default target on all top level processes
1724 if(unlikely(debug)) loops++;
1725 for(p = root_of_pids; p ; p = p->next) {
1726 // if the process is not merged itself
1727 // then is is a top level process
1728 if(unlikely(!p->merged && !p->target))
1729 p->target = apps_groups_default_target;
1731 // make sure all processes have a sortlist
1732 if(unlikely(!p->sortlist))
1733 p->sortlist = sortlist++;
1736 // give a target to all merged child processes
1739 if(unlikely(debug)) loops++;
1741 for(p = root_of_pids; p ; p = p->next) {
1742 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1743 p->target = p->parent->target;
1746 if(debug || (p->target && p->target->debug))
1747 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);
1753 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1756 long zero_all_targets(struct target *root) {
1760 for (w = root; w ; w = w->next) {
1763 if(w->fds) free(w->fds);
1779 w->statm_resident = 0;
1786 w->io_logical_bytes_read = 0;
1787 w->io_logical_bytes_written = 0;
1788 w->io_read_calls = 0;
1789 w->io_write_calls = 0;
1790 w->io_storage_bytes_read = 0;
1791 w->io_storage_bytes_written = 0;
1792 w->io_cancelled_write_bytes = 0;
1798 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1801 if(unlikely(!w->fds)) {
1802 w->fds = calloc(sizeof(int), (size_t) all_files_size);
1803 if(unlikely(!w->fds))
1804 error("Cannot allocate memory for fds in %s", w->name);
1807 if(likely(p->updated)) {
1808 w->cutime += p->cutime;
1809 w->cstime += p->cstime;
1810 w->cminflt += p->cminflt;
1811 w->cmajflt += p->cmajflt;
1813 w->utime += p->utime;
1814 w->stime += p->stime;
1815 w->minflt += p->minflt;
1816 w->majflt += p->majflt;
1820 w->statm_size += p->statm_size;
1821 w->statm_resident += p->statm_resident;
1822 w->statm_share += p->statm_share;
1823 w->statm_text += p->statm_text;
1824 w->statm_lib += p->statm_lib;
1825 w->statm_data += p->statm_data;
1826 w->statm_dirty += p->statm_dirty;
1828 w->io_logical_bytes_read += p->io_logical_bytes_read;
1829 w->io_logical_bytes_written += p->io_logical_bytes_written;
1830 w->io_read_calls += p->io_read_calls;
1831 w->io_write_calls += p->io_write_calls;
1832 w->io_storage_bytes_read += p->io_storage_bytes_read;
1833 w->io_storage_bytes_written += p->io_storage_bytes_written;
1834 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1837 w->num_threads += p->num_threads;
1839 if(likely(w->fds)) {
1841 for(c = 0; c < p->fds_size ;c++) {
1842 if(p->fds[c] == 0) continue;
1844 if(likely(p->fds[c] < all_files_size)) {
1845 if(w->fds) w->fds[p->fds[c]]++;
1848 error("Invalid fd number %d", p->fds[c]);
1852 if(unlikely(debug || w->debug))
1853 fprintf(stderr, "apps.plugin: \taggregating '%s' pid %d on target '%s' utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->comm, p->pid, w->name, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1857 void count_targets_fds(struct target *root) {
1861 for (w = root; w ; w = w->next) {
1862 if(!w->fds) continue;
1867 w->openinotifies = 0;
1868 w->openeventfds = 0;
1869 w->opentimerfds = 0;
1870 w->opensignalfds = 0;
1871 w->openeventpolls = 0;
1874 for(c = 1; c < all_files_size ;c++) {
1876 switch(all_files[c].type) {
1885 case FILETYPE_SOCKET:
1889 case FILETYPE_INOTIFY:
1893 case FILETYPE_EVENTFD:
1897 case FILETYPE_TIMERFD:
1901 case FILETYPE_SIGNALFD:
1905 case FILETYPE_EVENTPOLL:
1906 w->openeventpolls++;
1919 void calculate_netdata_statistics(void) {
1920 apply_apps_groups_targets_inheritance();
1922 zero_all_targets(users_root_target);
1923 zero_all_targets(groups_root_target);
1924 apps_groups_targets = zero_all_targets(apps_groups_root_target);
1926 // this has to be done, before the cleanup
1927 struct pid_stat *p = NULL;
1928 struct target *w = NULL, *o = NULL;
1930 // concentrate everything on the apps_groups_targets
1931 for(p = root_of_pids; p ; p = p->next) {
1933 // --------------------------------------------------------------------
1934 // apps_groups targets
1935 if(likely(p->target))
1936 aggregate_pid_on_target(p->target, p, NULL);
1938 error("pid %d %s was left without a target!", p->pid, p->comm);
1941 // --------------------------------------------------------------------
1944 if(likely(p->user_target && p->user_target->uid == p->uid))
1947 if(unlikely(debug && p->user_target))
1948 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);
1950 w = p->user_target = get_users_target(p->uid);
1954 aggregate_pid_on_target(w, p, o);
1956 error("pid %d %s was left without a user target!", p->pid, p->comm);
1959 // --------------------------------------------------------------------
1961 o = p->group_target;
1962 if(likely(p->group_target && p->group_target->gid == p->gid))
1963 w = p->group_target;
1965 if(unlikely(debug && p->group_target))
1966 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);
1968 w = p->group_target = get_groups_target(p->gid);
1972 aggregate_pid_on_target(w, p, o);
1974 error("pid %d %s was left without a group target!", p->pid, p->comm);
1978 count_targets_fds(apps_groups_root_target);
1979 count_targets_fds(users_root_target);
1980 count_targets_fds(groups_root_target);
1982 cleanup_exited_pids();
1985 // ----------------------------------------------------------------------------
1986 // update chart dimensions
1988 unsigned long long send_resource_usage_to_netdata() {
1989 static struct timeval last = { 0, 0 };
1990 static struct rusage me_last;
1995 unsigned long long usec;
1996 unsigned long long cpuuser;
1997 unsigned long long cpusyst;
2000 gettimeofday(&last, NULL);
2001 getrusage(RUSAGE_SELF, &me_last);
2003 // the first time, give a zero to allow
2004 // netdata calibrate to the current time
2005 // usec = update_every * 1000000ULL;
2011 gettimeofday(&now, NULL);
2012 getrusage(RUSAGE_SELF, &me);
2014 usec = usecdiff(&now, &last);
2015 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2016 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2018 bcopy(&now, &last, sizeof(struct timeval));
2019 bcopy(&me, &me_last, sizeof(struct rusage));
2022 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
2023 fprintf(stdout, "SET user = %llu\n", cpuuser);
2024 fprintf(stdout, "SET system = %llu\n", cpusyst);
2025 fprintf(stdout, "END\n");
2027 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
2028 fprintf(stdout, "SET files = %llu\n", file_counter);
2029 fprintf(stdout, "SET pids = %ld\n", all_pids_count);
2030 fprintf(stdout, "SET fds = %d\n", all_files_len);
2031 fprintf(stdout, "SET targets = %ld\n", apps_groups_targets);
2032 fprintf(stdout, "END\n");
2037 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec)
2040 int childs = include_exited_childs;
2043 // childs processing introduces spikes
2044 // here we try to eliminate them by disabling childs processing either for specific dimensions
2045 // or entirely. Of course, either way, we disable it just a single iteration.
2047 unsigned long long max = processors * hz * 100;
2048 unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2050 for (w = root; w ; w = w->next) {
2051 if(w->target || (!w->processes && !w->exposed)) continue;
2053 if((w->utime + w->stime + w->cutime + w->cstime) > max) {
2054 #ifdef NETDATA_INTERNAL_CHECKS
2056 fprintf(stderr, "Prevented a spike on target '%s', reported CPU time = %llu (without childs = %llu)\n", w->name, (w->utime + w->stime + w->cutime + w->cstime) / 100, (w->utime + w->stime) / 100);
2058 w->cutime = w->cstime = w->cminflt = w->majflt = 0;
2062 cutime += w->cutime;
2064 cstime += w->cstime;
2065 minflt += w->minflt;
2066 cminflt += w->cminflt;
2067 majflt += w->majflt;
2068 cmajflt += w->cmajflt;
2071 if((utime + stime + cutime + cstime) > max) {
2073 #ifdef NETDATA_INTERNAL_CHECKS
2075 fprintf(stderr, "Prevented a spike because the total CPU of all dimensions = %llu (without childs = %llu)\n", (utime + stime + cutime + cstime) / 100, (utime + stime) / 100);
2079 if((utime + stime) > max) {
2081 unsigned long long multiplier = max, divider = utime + stime;
2082 for (w = root; w ; w = w->next) {
2083 w->utime = w->utime * multiplier / divider;
2084 w->stime = w->stime * multiplier / divider;
2085 w->minflt = w->minflt * multiplier / divider;
2086 w->majflt = w->majflt * multiplier / divider;
2089 #ifdef NETDATA_INTERNAL_CHECKS
2091 fprintf(stderr, "Reduced processes utilization (without childs) by %0.2f%% (CPU was %llu)\n", (float)(((utime + stime - max) * 100.0)/(float)max), (utime + stime) / 100);
2097 fprintf(stdout, "BEGIN %s.cpu %llu\n", type, usec);
2098 for (w = root; w ; w = w->next) {
2099 if(w->target || (!w->processes && !w->exposed)) continue;
2101 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + (childs?(w->cutime + w->cstime):0));
2103 fprintf(stdout, "END\n");
2105 fprintf(stdout, "BEGIN %s.cpu_user %llu\n", type, usec);
2106 for (w = root; w ; w = w->next) {
2107 if(w->target || (!w->processes && !w->exposed)) continue;
2109 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + (childs?(w->cutime):0));
2111 fprintf(stdout, "END\n");
2113 fprintf(stdout, "BEGIN %s.cpu_system %llu\n", type, usec);
2114 for (w = root; w ; w = w->next) {
2115 if(w->target || (!w->processes && !w->exposed)) continue;
2117 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + (childs?(w->cstime):0));
2119 fprintf(stdout, "END\n");
2121 fprintf(stdout, "BEGIN %s.threads %llu\n", type, usec);
2122 for (w = root; w ; w = w->next) {
2123 if(w->target || (!w->processes && !w->exposed)) continue;
2125 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
2127 fprintf(stdout, "END\n");
2129 fprintf(stdout, "BEGIN %s.processes %llu\n", type, usec);
2130 for (w = root; w ; w = w->next) {
2131 if(w->target || (!w->processes && !w->exposed)) continue;
2133 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
2135 fprintf(stdout, "END\n");
2137 fprintf(stdout, "BEGIN %s.mem %llu\n", type, usec);
2138 for (w = root; w ; w = w->next) {
2139 if(w->target || (!w->processes && !w->exposed)) continue;
2141 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
2143 fprintf(stdout, "END\n");
2145 fprintf(stdout, "BEGIN %s.minor_faults %llu\n", type, usec);
2146 for (w = root; w ; w = w->next) {
2147 if(w->target || (!w->processes && !w->exposed)) continue;
2149 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + (childs?(w->cminflt):0));
2151 fprintf(stdout, "END\n");
2153 fprintf(stdout, "BEGIN %s.major_faults %llu\n", type, usec);
2154 for (w = root; w ; w = w->next) {
2155 if(w->target || (!w->processes && !w->exposed)) continue;
2157 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + (childs?(w->cmajflt):0));
2159 fprintf(stdout, "END\n");
2161 fprintf(stdout, "BEGIN %s.lreads %llu\n", type, usec);
2162 for (w = root; w ; w = w->next) {
2163 if(w->target || (!w->processes && !w->exposed)) continue;
2165 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read);
2167 fprintf(stdout, "END\n");
2169 fprintf(stdout, "BEGIN %s.lwrites %llu\n", type, usec);
2170 for (w = root; w ; w = w->next) {
2171 if(w->target || (!w->processes && !w->exposed)) continue;
2173 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written);
2175 fprintf(stdout, "END\n");
2177 fprintf(stdout, "BEGIN %s.preads %llu\n", type, usec);
2178 for (w = root; w ; w = w->next) {
2179 if(w->target || (!w->processes && !w->exposed)) continue;
2181 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read);
2183 fprintf(stdout, "END\n");
2185 fprintf(stdout, "BEGIN %s.pwrites %llu\n", type, usec);
2186 for (w = root; w ; w = w->next) {
2187 if(w->target || (!w->processes && !w->exposed)) continue;
2189 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written);
2191 fprintf(stdout, "END\n");
2193 fprintf(stdout, "BEGIN %s.files %llu\n", type, usec);
2194 for (w = root; w ; w = w->next) {
2195 if(w->target || (!w->processes && !w->exposed)) continue;
2197 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
2199 fprintf(stdout, "END\n");
2201 fprintf(stdout, "BEGIN %s.sockets %llu\n", type, usec);
2202 for (w = root; w ; w = w->next) {
2203 if(w->target || (!w->processes && !w->exposed)) continue;
2205 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
2207 fprintf(stdout, "END\n");
2209 fprintf(stdout, "BEGIN %s.pipes %llu\n", type, usec);
2210 for (w = root; w ; w = w->next) {
2211 if(w->target || (!w->processes && !w->exposed)) continue;
2213 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
2215 fprintf(stdout, "END\n");
2221 // ----------------------------------------------------------------------------
2222 // generate the charts
2224 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2227 int newly_added = 0;
2229 for(w = root ; w ; w = w->next)
2230 if(!w->exposed && w->processes) {
2233 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2236 // nothing more to show
2237 if(!newly_added) return;
2239 // we have something new to show
2240 // update the charts
2241 fprintf(stdout, "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);
2242 for (w = root; w ; w = w->next) {
2243 if(w->target || (!w->processes && !w->exposed)) continue;
2245 fprintf(stdout, "DIMENSION %s '' absolute 1 %u %s\n", w->name, hz, w->hidden ? "hidden,noreset" : "noreset");
2248 fprintf(stdout, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2249 for (w = root; w ; w = w->next) {
2250 if(w->target || (!w->processes && !w->exposed)) continue;
2252 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld noreset\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2255 fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2256 for (w = root; w ; w = w->next) {
2257 if(w->target || (!w->processes && !w->exposed)) continue;
2259 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2262 fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2263 for (w = root; w ; w = w->next) {
2264 if(w->target || (!w->processes && !w->exposed)) continue;
2266 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2269 fprintf(stdout, "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);
2270 for (w = root; w ; w = w->next) {
2271 if(w->target || (!w->processes && !w->exposed)) continue;
2273 fprintf(stdout, "DIMENSION %s '' absolute 1 %u noreset\n", w->name, hz);
2276 fprintf(stdout, "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);
2277 for (w = root; w ; w = w->next) {
2278 if(w->target || (!w->processes && !w->exposed)) continue;
2280 fprintf(stdout, "DIMENSION %s '' absolute 1 %u noreset\n", w->name, hz);
2283 fprintf(stdout, "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);
2284 for (w = root; w ; w = w->next) {
2285 if(w->target || (!w->processes && !w->exposed)) continue;
2287 fprintf(stdout, "DIMENSION %s '' absolute 1 100 noreset\n", w->name);
2290 fprintf(stdout, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
2291 for (w = root; w ; w = w->next) {
2292 if(w->target || (!w->processes && !w->exposed)) continue;
2294 fprintf(stdout, "DIMENSION %s '' absolute 1 100 noreset\n", w->name);
2297 fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2298 for (w = root; w ; w = w->next) {
2299 if(w->target || (!w->processes && !w->exposed)) continue;
2301 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024*100);
2304 fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2305 for (w = root; w ; w = w->next) {
2306 if(w->target || (!w->processes && !w->exposed)) continue;
2308 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024*100);
2311 fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2312 for (w = root; w ; w = w->next) {
2313 if(w->target || (!w->processes && !w->exposed)) continue;
2315 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024*100);
2318 fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2319 for (w = root; w ; w = w->next) {
2320 if(w->target || (!w->processes && !w->exposed)) continue;
2322 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024*100);
2325 fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2326 for (w = root; w ; w = w->next) {
2327 if(w->target || (!w->processes && !w->exposed)) continue;
2329 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2332 fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2333 for (w = root; w ; w = w->next) {
2334 if(w->target || (!w->processes && !w->exposed)) continue;
2336 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2339 fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2340 for (w = root; w ; w = w->next) {
2341 if(w->target || (!w->processes && !w->exposed)) continue;
2343 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2348 // ----------------------------------------------------------------------------
2349 // parse command line arguments
2351 void parse_args(int argc, char **argv)
2356 for(i = 1; i < argc; i++) {
2358 int n = atoi(argv[i]);
2365 if(strcmp("debug", argv[i]) == 0) {
2367 // debug_flags = 0xffffffff;
2371 if(strcmp("no-childs", argv[i]) == 0) {
2372 include_exited_childs = 0;
2376 if(strcmp("with-childs", argv[i]) == 0) {
2377 include_exited_childs = 1;
2386 error("Cannot understand option %s", argv[i]);
2390 if(freq > 0) update_every = freq;
2391 if(!name) name = "groups";
2393 if(read_apps_groups_conf(name)) {
2394 error("Cannot read process groups %s", name);
2399 int main(int argc, char **argv)
2401 // debug_flags = D_PROCFILE;
2403 // set the name for logging
2404 program_name = "apps.plugin";
2406 // disable syslog for apps.plugin
2407 error_log_syslog = 0;
2409 // set errors flood protection to 100 logs per hour
2410 error_log_errors_per_period = 100;
2411 error_log_throttle_period = 3600;
2413 host_prefix = getenv("NETDATA_HOST_PREFIX");
2414 if(host_prefix == NULL) {
2415 info("NETDATA_HOST_PREFIX is not passed from netdata");
2418 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2420 config_dir = getenv("NETDATA_CONFIG_DIR");
2421 if(config_dir == NULL) {
2422 info("NETDATA_CONFIG_DIR is not passed from netdata");
2423 config_dir = CONFIG_DIR;
2425 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2427 #ifdef NETDATA_INTERNAL_CHECKS
2428 if(debug_flags != 0) {
2429 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2430 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2431 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2432 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2434 #endif /* NETDATA_INTERNAL_CHECKS */
2436 procfile_adaptive_initial_allocation = 1;
2438 time_t started_t = time(NULL);
2441 pid_max = get_system_pid_max();
2442 processors = get_system_cpus();
2444 parse_args(argc, argv);
2446 all_pids_sortlist = calloc(sizeof(pid_t), (size_t)pid_max);
2447 if(!all_pids_sortlist) {
2448 error("Cannot allocate %zu bytes of memory.", sizeof(pid_t) * pid_max);
2449 printf("DISABLE\n");
2453 all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2455 error("Cannot allocate %zu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2456 printf("DISABLE\n");
2460 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2461 "DIMENSION user '' incremental 1 1000\n"
2462 "DIMENSION system '' incremental 1 1000\n"
2463 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2464 "DIMENSION files '' incremental 1 1\n"
2465 "DIMENSION pids '' absolute 1 1\n"
2466 "DIMENSION fds '' absolute 1 1\n"
2467 "DIMENSION targets '' absolute 1 1\n", update_every);
2469 #ifndef PROFILING_MODE
2470 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2471 unsigned long long sunow;
2472 #endif /* PROFILING_MODE */
2474 global_iterations_counter = 1;
2475 for(;1; global_iterations_counter++) {
2476 #ifndef PROFILING_MODE
2477 // delay until it is our time to run
2478 while((sunow = timems()) < sunext)
2479 usecsleep(sunext - sunow);
2481 // find the next time we need to run
2482 while(timems() > sunext)
2483 sunext += update_every * 1000000ULL;
2484 #endif /* PROFILING_MODE */
2486 if(!collect_data_for_all_processes_from_proc()) {
2487 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2488 printf("DISABLE\n");
2492 calculate_netdata_statistics();
2494 unsigned long long dt = send_resource_usage_to_netdata();
2496 // this is smart enough to show only newly added apps, when needed
2497 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2498 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2499 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2501 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2502 send_collected_data_to_netdata(users_root_target, "users", dt);
2503 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2506 fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
2508 current_t = time(NULL);
2510 #ifndef PROFILING_MODE
2511 // restart check (14400 seconds)
2512 if(current_t - started_t > 14400) exit(0);
2514 if(current_t - started_t > 10) exit(0);
2515 #endif /* PROFILING_MODE */