3 // 1. disable RESET_OR_OVERFLOW check in charts
13 #include <sys/types.h>
18 #include <sys/resource.h>
29 #include <arpa/inet.h>
31 #include <sys/types.h>
40 #include "../config.h"
42 #ifdef NETDATA_INTERNAL_CHECKS
43 #include <sys/prctl.h>
46 #define MAX_COMPARE_NAME 100
48 #define MAX_CMDLINE 1024
55 unsigned long long file_counter = 0;
56 int proc_pid_cmdline_is_needed = 0;
58 char *host_prefix = "";
59 char *config_dir = CONFIG_DIR;
61 #ifdef NETDATA_INTERNAL_CHECKS
62 // ----------------------------------------------------------------------------
64 // do not use in production systems - it mis-aligns allocated memory
70 } allocations = { 0, 0, 0 };
72 #define MALLOC_MARK (uint32_t)(0x0BADCAFE)
73 #define MALLOC_PREFIX (sizeof(uint32_t) * 2)
74 #define MALLOC_SUFFIX (sizeof(uint32_t))
75 #define MALLOC_OVERHEAD (MALLOC_PREFIX + MALLOC_SUFFIX)
77 void *mark_allocation(void *allocated_ptr, size_t size_without_overheads) {
78 uint32_t *real_ptr = (uint32_t *)allocated_ptr;
79 real_ptr[0] = MALLOC_MARK;
80 real_ptr[1] = (uint32_t) size_without_overheads;
82 uint32_t *end_ptr = (uint32_t *)(allocated_ptr + MALLOC_PREFIX + size_without_overheads);
83 end_ptr[0] = MALLOC_MARK;
85 // fprintf(stderr, "MEMORY_POINTER: Allocated at %p, returning %p.\n", allocated_ptr, (void *)(allocated_ptr + MALLOC_PREFIX));
87 return allocated_ptr + MALLOC_PREFIX;
90 void *check_allocation(const char *file, int line, const char *function, void *marked_ptr, size_t *size_without_overheads_ptr) {
91 uint32_t *real_ptr = (uint32_t *)(marked_ptr - MALLOC_PREFIX);
93 // fprintf(stderr, "MEMORY_POINTER: Checking pointer at %p, real %p for %s/%u@%s.\n", marked_ptr, (void *)(marked_ptr - MALLOC_PREFIX), function, line, file);
95 if(real_ptr[0] != MALLOC_MARK) fatal("MEMORY: prefix MARK is not valid for %s/%u@%s.", function, line, file);
97 size_t size = real_ptr[1];
99 uint32_t *end_ptr = (uint32_t *)(marked_ptr + size);
100 if(end_ptr[0] != MALLOC_MARK) fatal("MEMORY: suffix MARK of allocation with size %zu is not valid for %s/%u@%s.", size, function, line, file);
102 if(size_without_overheads_ptr) *size_without_overheads_ptr = size;
107 void *malloc_debug(const char *file, int line, const char *function, size_t size) {
108 void *ptr = malloc(size + MALLOC_OVERHEAD);
109 if(!ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
111 allocations.allocated += size;
112 allocations.allocations++;
114 debug(D_MEMORY, "MEMORY: Allocated %zu bytes for %s/%u@%s."
115 " Status: allocated %zu in %zu allocs."
117 , function, line, file
118 , allocations.allocated
119 , allocations.allocations
122 if(allocations.allocated > allocations.allocated_max) {
123 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
124 allocations.allocated_max = allocations.allocated;
128 check_allocation(file, line, function, mark_allocation(ptr, size), &csize);
130 fatal("Invalid size.");
133 return mark_allocation(ptr, size);
136 void *calloc_debug(const char *file, int line, const char *function, size_t nmemb, size_t size) {
137 void *ptr = malloc_debug(file, line, function, (nmemb * size));
138 bzero(ptr, nmemb * size);
142 void free_debug(const char *file, int line, const char *function, void *ptr) {
144 void *real_ptr = check_allocation(file, line, function, ptr, &size);
146 bzero(real_ptr, size + MALLOC_OVERHEAD);
149 allocations.allocated -= size;
150 allocations.allocations--;
152 debug(D_MEMORY, "MEMORY: freed %zu bytes for %s/%u@%s."
153 " Status: allocated %zu in %zu allocs."
155 , function, line, file
156 , allocations.allocated
157 , allocations.allocations
161 void *realloc_debug(const char *file, int line, const char *function, void *ptr, size_t size) {
162 if(!ptr) return malloc_debug(file, line, function, size);
163 if(!size) { free_debug(file, line, function, ptr); return NULL; }
166 void *real_ptr = check_allocation(file, line, function, ptr, &old_size);
168 void *new_ptr = realloc(real_ptr, size + MALLOC_OVERHEAD);
169 if(!new_ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
171 allocations.allocated += size;
172 allocations.allocated -= old_size;
174 debug(D_MEMORY, "MEMORY: Re-allocated from %zu to %zu bytes for %s/%u@%s."
175 " Status: allocated %z in %zu allocs."
177 , function, line, file
178 , allocations.allocated
179 , allocations.allocations
182 if(allocations.allocated > allocations.allocated_max) {
183 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
184 allocations.allocated_max = allocations.allocated;
187 return mark_allocation(new_ptr, size);
190 char *strdup_debug(const char *file, int line, const char *function, const char *ptr) {
197 char *p = malloc_debug(file, line, function, size);
198 if(!p) fatal("Cannot allocate %zu bytes.", size);
200 memcpy(p, ptr, size);
204 #define malloc(size) malloc_debug(__FILE__, __LINE__, __FUNCTION__, (size))
205 #define calloc(nmemb, size) calloc_debug(__FILE__, __LINE__, __FUNCTION__, (nmemb), (size))
206 #define realloc(ptr, size) realloc_debug(__FILE__, __LINE__, __FUNCTION__, (ptr), (size))
207 #define free(ptr) free_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
212 #define strdup(ptr) strdup_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
214 #endif /* NETDATA_INTERNAL_CHECKS */
216 // ----------------------------------------------------------------------------
218 // to retrieve settings of the system
220 long get_system_cpus(void) {
225 char filename[FILENAME_MAX + 1];
226 snprintfz(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
228 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
231 ff = procfile_readall(ff);
238 for(i = 0; i < procfile_lines(ff); i++) {
239 if(!procfile_linewords(ff, i)) continue;
241 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
244 if(processors < 1) processors = 1;
250 long get_system_pid_max(void) {
254 char filename[FILENAME_MAX + 1];
255 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
256 ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
259 ff = procfile_readall(ff);
265 mpid = atol(procfile_lineword(ff, 0, 0));
266 if(!mpid) mpid = 32768;
272 // ----------------------------------------------------------------------------
274 // target is the structure that process data are aggregated
277 char compare[MAX_COMPARE_NAME + 1];
278 uint32_t comparehash;
281 char id[MAX_NAME + 1];
284 char name[MAX_NAME + 1];
289 unsigned long long minflt;
290 unsigned long long cminflt;
291 unsigned long long majflt;
292 unsigned long long cmajflt;
293 unsigned long long utime;
294 unsigned long long stime;
295 unsigned long long cutime;
296 unsigned long long cstime;
297 unsigned long long num_threads;
298 unsigned long long rss;
300 unsigned long long fix_minflt;
301 unsigned long long fix_cminflt;
302 unsigned long long fix_majflt;
303 unsigned long long fix_cmajflt;
304 unsigned long long fix_utime;
305 unsigned long long fix_stime;
306 unsigned long long fix_cutime;
307 unsigned long long fix_cstime;
309 unsigned long long statm_size;
310 unsigned long long statm_resident;
311 unsigned long long statm_share;
312 unsigned long long statm_text;
313 unsigned long long statm_lib;
314 unsigned long long statm_data;
315 unsigned long long statm_dirty;
317 unsigned long long io_logical_bytes_read;
318 unsigned long long io_logical_bytes_written;
319 unsigned long long io_read_calls;
320 unsigned long long io_write_calls;
321 unsigned long long io_storage_bytes_read;
322 unsigned long long io_storage_bytes_written;
323 unsigned long long io_cancelled_write_bytes;
325 unsigned long long fix_io_logical_bytes_read;
326 unsigned long long fix_io_logical_bytes_written;
327 unsigned long long fix_io_read_calls;
328 unsigned long long fix_io_write_calls;
329 unsigned long long fix_io_storage_bytes_read;
330 unsigned long long fix_io_storage_bytes_written;
331 unsigned long long fix_io_cancelled_write_bytes;
334 unsigned long long openfiles;
335 unsigned long long openpipes;
336 unsigned long long opensockets;
337 unsigned long long openinotifies;
338 unsigned long long openeventfds;
339 unsigned long long opentimerfds;
340 unsigned long long opensignalfds;
341 unsigned long long openeventpolls;
342 unsigned long long openother;
344 unsigned long processes; // how many processes have been merged to this
345 int exposed; // if set, we have sent this to netdata
346 int hidden; // if set, we set the hidden flag on the dimension
349 int starts_with; // if set, the compare string matches only the
350 // beginning of the command
352 struct target *target; // the one that will be reported to netdata
357 // ----------------------------------------------------------------------------
359 // aggregate all processes in groups, to have a limited number of dimensions
361 struct target *apps_groups_root_target = NULL;
362 struct target *apps_groups_default_target = NULL;
363 long apps_groups_targets = 0;
365 struct target *users_root_target = NULL;
366 struct target *groups_root_target = NULL;
368 struct target *get_users_target(uid_t uid)
371 for(w = users_root_target ; w ; w = w->next)
372 if(w->uid == uid) return w;
374 w = calloc(sizeof(struct target), 1);
376 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
380 snprintfz(w->compare, MAX_COMPARE_NAME, "%d", uid);
381 w->comparehash = simple_hash(w->compare);
382 w->comparelen = strlen(w->compare);
384 snprintfz(w->id, MAX_NAME, "%d", uid);
385 w->idhash = simple_hash(w->id);
387 struct passwd *pw = getpwuid(uid);
389 snprintfz(w->name, MAX_NAME, "%d", uid);
391 snprintfz(w->name, MAX_NAME, "%s", pw->pw_name);
393 netdata_fix_chart_name(w->name);
397 w->next = users_root_target;
398 users_root_target = w;
401 fprintf(stderr, "apps.plugin: added uid %d ('%s') target\n", w->uid, w->name);
406 struct target *get_groups_target(gid_t gid)
409 for(w = groups_root_target ; w ; w = w->next)
410 if(w->gid == gid) return w;
412 w = calloc(sizeof(struct target), 1);
414 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
418 snprintfz(w->compare, MAX_COMPARE_NAME, "%d", gid);
419 w->comparehash = simple_hash(w->compare);
420 w->comparelen = strlen(w->compare);
422 snprintfz(w->id, MAX_NAME, "%d", gid);
423 w->idhash = simple_hash(w->id);
425 struct group *gr = getgrgid(gid);
427 snprintfz(w->name, MAX_NAME, "%d", gid);
429 snprintfz(w->name, MAX_NAME, "%s", gr->gr_name);
431 netdata_fix_chart_name(w->name);
435 w->next = groups_root_target;
436 groups_root_target = w;
439 fprintf(stderr, "apps.plugin: added gid %d ('%s') target\n", w->gid, w->name);
444 // find or create a new target
445 // there are targets that are just aggregated to other target (the second argument)
446 struct target *get_apps_groups_target(const char *id, struct target *target)
448 int tdebug = 0, thidden = 0, ends_with = 0;
449 const char *nid = id;
451 while(nid[0] == '-' || nid[0] == '+' || nid[0] == '*') {
452 if(nid[0] == '-') thidden = 1;
453 if(nid[0] == '+') tdebug = 1;
454 if(nid[0] == '*') ends_with = 1;
457 uint32_t hash = simple_hash(id);
460 for(w = apps_groups_root_target ; w ; w = w->next) {
461 if(w->idhash == hash && strncmp(nid, w->id, MAX_NAME) == 0)
465 w = calloc(sizeof(struct target), 1);
467 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
471 strncpyz(w->id, nid, MAX_NAME);
472 w->idhash = simple_hash(w->id);
474 strncpyz(w->name, nid, MAX_NAME);
476 strncpyz(w->compare, nid, MAX_COMPARE_NAME);
477 int len = strlen(w->compare);
478 if(w->compare[len - 1] == '*') {
479 w->compare[len - 1] = '\0';
482 w->ends_with = ends_with;
484 if(w->starts_with && w->ends_with)
485 proc_pid_cmdline_is_needed = 1;
487 w->comparehash = simple_hash(w->compare);
488 w->comparelen = strlen(w->compare);
494 w->next = apps_groups_root_target;
495 apps_groups_root_target = w;
498 fprintf(stderr, "apps.plugin: ADDING TARGET ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
500 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
501 , w->target?w->target->id:w->id
502 , (w->hidden)?"hidden":"-"
503 , (w->debug)?"debug":"-"
509 // read the apps_groups.conf file
510 int read_apps_groups_conf(const char *name)
512 char filename[FILENAME_MAX + 1];
514 snprintfz(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
517 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
519 // ----------------------------------------
521 procfile *ff = procfile_open(filename, " :\t", PROCFILE_FLAG_DEFAULT);
524 procfile_set_quotes(ff, "'\"");
526 ff = procfile_readall(ff);
532 unsigned long line, lines = procfile_lines(ff);
534 for(line = 0; line < lines ;line++) {
535 unsigned long word, words = procfile_linewords(ff, line);
536 struct target *w = NULL;
538 char *t = procfile_lineword(ff, line, 0);
539 if(!t || !*t) continue;
541 for(word = 0; word < words ;word++) {
542 char *s = procfile_lineword(ff, line, word);
543 if(!s || !*s) continue;
548 struct target *n = get_apps_groups_target(s, w);
550 error("Cannot create target '%s' (line %d, word %d)", s, line, word);
558 int tdebug = 0, thidden = 0;
560 while(t[0] == '-' || t[0] == '+') {
561 if(t[0] == '-') thidden = 1;
562 if(t[0] == '+') tdebug = 1;
566 strncpyz(w->name, t, MAX_NAME);
571 fprintf(stderr, "apps.plugin: AGGREGATION TARGET NAME '%s' on ID '%s', process name '%s' (%s), aggregated on target '%s', options: %s %s\n"
574 , w->compare, (w->starts_with && w->ends_with)?"substring":((w->starts_with)?"prefix":((w->ends_with)?"suffix":"exact"))
575 , w->target?w->target->id:w->id
576 , (w->hidden)?"hidden":"-"
577 , (w->debug)?"debug":"-"
584 apps_groups_default_target = get_apps_groups_target("p+!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
585 if(!apps_groups_default_target)
586 error("Cannot create default target");
588 strncpyz(apps_groups_default_target->name, "other", MAX_NAME);
594 // ----------------------------------------------------------------------------
595 // data to store for each pid
600 char comm[MAX_COMPARE_NAME + 1];
601 char cmdline[MAX_CMDLINE + 1];
610 unsigned long long minflt;
611 unsigned long long cminflt;
612 unsigned long long majflt;
613 unsigned long long cmajflt;
614 unsigned long long utime;
615 unsigned long long stime;
616 unsigned long long cutime;
617 unsigned long long cstime;
621 // int64_t itrealvalue;
622 // unsigned long long starttime;
623 // unsigned long long vsize;
624 unsigned long long rss;
625 // unsigned long long rsslim;
626 // unsigned long long starcode;
627 // unsigned long long endcode;
628 // unsigned long long startstack;
629 // unsigned long long kstkesp;
630 // unsigned long long kstkeip;
633 // uint64_t sigignore;
634 // uint64_t sigcatch;
638 // int32_t exit_signal;
639 // int32_t processor;
640 // uint32_t rt_priority;
642 // unsigned long long delayacct_blkio_ticks;
643 // uint64_t guest_time;
644 // int64_t cguest_time;
649 unsigned long long statm_size;
650 unsigned long long statm_resident;
651 unsigned long long statm_share;
652 unsigned long long statm_text;
653 unsigned long long statm_lib;
654 unsigned long long statm_data;
655 unsigned long long statm_dirty;
657 unsigned long long io_logical_bytes_read;
658 unsigned long long io_logical_bytes_written;
659 unsigned long long io_read_calls;
660 unsigned long long io_write_calls;
661 unsigned long long io_storage_bytes_read;
662 unsigned long long io_storage_bytes_written;
663 unsigned long long io_cancelled_write_bytes;
665 // we need the last values
666 // for all incremental counters
667 // so that when a process switches users/groups
668 // we will subtract these values from the old
670 unsigned long long last_minflt;
671 unsigned long long last_cminflt;
672 unsigned long long last_majflt;
673 unsigned long long last_cmajflt;
674 unsigned long long last_utime;
675 unsigned long long last_stime;
676 unsigned long long last_cutime;
677 unsigned long long last_cstime;
679 unsigned long long last_io_logical_bytes_read;
680 unsigned long long last_io_logical_bytes_written;
681 unsigned long long last_io_read_calls;
682 unsigned long long last_io_write_calls;
683 unsigned long long last_io_storage_bytes_read;
684 unsigned long long last_io_storage_bytes_written;
685 unsigned long long last_io_cancelled_write_bytes;
687 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
688 unsigned long long old_utime;
689 unsigned long long old_stime;
690 unsigned long long old_minflt;
691 unsigned long long old_majflt;
693 unsigned long long old_cutime;
694 unsigned long long old_cstime;
695 unsigned long long old_cminflt;
696 unsigned long long old_cmajflt;
698 unsigned long long fix_cutime;
699 unsigned long long fix_cstime;
700 unsigned long long fix_cminflt;
701 unsigned long long fix_cmajflt;
703 unsigned long long diff_cutime;
704 unsigned long long diff_cstime;
705 unsigned long long diff_cminflt;
706 unsigned long long diff_cmajflt;
707 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
709 int *fds; // array of fds it uses
710 int fds_size; // the size of the fds array
712 int children_count; // number of processes directly referencing this
713 int updated; // 1 when update
714 int merged; // 1 when it has been merged to its parent
717 struct target *target; // app_groups.conf targets
718 struct target *user_target; // uid based targets
719 struct target *group_target; // gid based targets
721 struct pid_stat *parent;
722 struct pid_stat *prev;
723 struct pid_stat *next;
725 } *root_of_pids = NULL, **all_pids;
727 long all_pids_count = 0;
729 struct pid_stat *get_pid_entry(pid_t pid)
732 all_pids[pid]->new_entry = 0;
733 return all_pids[pid];
736 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
738 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct pid_stat));
742 all_pids[pid]->fds = calloc(sizeof(int), 100);
743 if(!all_pids[pid]->fds)
744 error("Cannot allocate %ld bytes of memory", (unsigned long)(sizeof(int) * 100));
745 else all_pids[pid]->fds_size = 100;
747 if(root_of_pids) root_of_pids->prev = all_pids[pid];
748 all_pids[pid]->next = root_of_pids;
749 root_of_pids = all_pids[pid];
751 all_pids[pid]->pid = pid;
752 all_pids[pid]->new_entry = 1;
754 return all_pids[pid];
757 void del_pid_entry(pid_t pid)
759 if(!all_pids[pid]) return;
761 if(debug) fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
763 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
764 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
765 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
767 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
769 all_pids[pid] = NULL;
773 // ----------------------------------------------------------------------------
774 // update pids from proc
776 int read_proc_pid_cmdline(struct pid_stat *p) {
777 char filename[FILENAME_MAX + 1];
778 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/cmdline", host_prefix, p->pid);
780 int fd = open(filename, O_RDONLY, 0666);
781 if(unlikely(fd == -1)) return 1;
783 int i, bytes = read(fd, p->cmdline, MAX_CMDLINE);
787 // copy the command to the command line
788 strncpyz(p->cmdline, p->comm, MAX_CMDLINE);
792 p->cmdline[bytes] = '\0';
793 for(i = 0; i < bytes ; i++)
794 if(!p->cmdline[i]) p->cmdline[i] = ' ';
797 fprintf(stderr, "Read file '%s' contents: %s\n", filename, p->cmdline);
802 int read_proc_pid_ownership(struct pid_stat *p) {
803 char filename[FILENAME_MAX + 1];
805 snprintfz(filename, FILENAME_MAX, "%s/proc/%d", host_prefix, p->pid);
807 // ----------------------------------------
811 if(stat(filename, &st) != 0)
820 int read_proc_pid_stat(struct pid_stat *p) {
821 static procfile *ff = NULL;
823 char filename[FILENAME_MAX + 1];
825 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
827 // ----------------------------------------
829 int set_quotes = (!ff)?1:0;
831 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
834 // if(set_quotes) procfile_set_quotes(ff, "()");
835 if(set_quotes) procfile_set_open_close(ff, "(", ")");
837 ff = procfile_readall(ff);
839 // procfile_close(ff);
845 // parse the process name
847 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
849 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
851 // p->state = *(procfile_lineword(ff, 0, 2+i));
852 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3 + i));
853 // p->pgrp = atol(procfile_lineword(ff, 0, 4+i));
854 // p->session = atol(procfile_lineword(ff, 0, 5+i));
855 // p->tty_nr = atol(procfile_lineword(ff, 0, 6+i));
856 // p->tpgid = atol(procfile_lineword(ff, 0, 7+i));
857 // p->flags = strtoull(procfile_lineword(ff, 0, 8+i), NULL, 10);
858 p->minflt = strtoull(procfile_lineword(ff, 0, 9+i), NULL, 10);
859 p->cminflt = strtoull(procfile_lineword(ff, 0, 10+i), NULL, 10);
860 p->majflt = strtoull(procfile_lineword(ff, 0, 11+i), NULL, 10);
861 p->cmajflt = strtoull(procfile_lineword(ff, 0, 12+i), NULL, 10);
862 p->utime = strtoull(procfile_lineword(ff, 0, 13+i), NULL, 10);
863 p->stime = strtoull(procfile_lineword(ff, 0, 14+i), NULL, 10);
864 p->cutime = strtoull(procfile_lineword(ff, 0, 15+i), NULL, 10);
865 p->cstime = strtoull(procfile_lineword(ff, 0, 16+i), NULL, 10);
866 // p->priority = strtoull(procfile_lineword(ff, 0, 17+i), NULL, 10);
867 // p->nice = strtoull(procfile_lineword(ff, 0, 18+i), NULL, 10);
868 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19 + i));
869 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20+i), NULL, 10);
870 // p->starttime = strtoull(procfile_lineword(ff, 0, 21+i), NULL, 10);
871 // p->vsize = strtoull(procfile_lineword(ff, 0, 22+i), NULL, 10);
872 p->rss = strtoull(procfile_lineword(ff, 0, 23+i), NULL, 10);
873 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24+i), NULL, 10);
874 // p->starcode = strtoull(procfile_lineword(ff, 0, 25+i), NULL, 10);
875 // p->endcode = strtoull(procfile_lineword(ff, 0, 26+i), NULL, 10);
876 // p->startstack = strtoull(procfile_lineword(ff, 0, 27+i), NULL, 10);
877 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28+i), NULL, 10);
878 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29+i), NULL, 10);
879 // p->signal = strtoull(procfile_lineword(ff, 0, 30+i), NULL, 10);
880 // p->blocked = strtoull(procfile_lineword(ff, 0, 31+i), NULL, 10);
881 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32+i), NULL, 10);
882 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33+i), NULL, 10);
883 // p->wchan = strtoull(procfile_lineword(ff, 0, 34+i), NULL, 10);
884 // p->nswap = strtoull(procfile_lineword(ff, 0, 35+i), NULL, 10);
885 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36+i), NULL, 10);
886 // p->exit_signal = atol(procfile_lineword(ff, 0, 37+i));
887 // p->processor = atol(procfile_lineword(ff, 0, 38+i));
888 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39+i), NULL, 10);
889 // p->policy = strtoul(procfile_lineword(ff, 0, 40+i), NULL, 10);
890 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41+i), NULL, 10);
891 // p->guest_time = strtoull(procfile_lineword(ff, 0, 42+i), NULL, 10);
892 // p->cguest_time = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
894 if(debug || (p->target && p->target->debug))
895 fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' 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->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
897 // procfile_close(ff);
901 int read_proc_pid_statm(struct pid_stat *p) {
902 static procfile *ff = NULL;
904 char filename[FILENAME_MAX + 1];
906 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
908 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
911 ff = procfile_readall(ff);
913 // procfile_close(ff);
919 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
920 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
921 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
922 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
923 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
924 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
925 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
927 // procfile_close(ff);
931 int read_proc_pid_io(struct pid_stat *p) {
932 static procfile *ff = NULL;
934 char filename[FILENAME_MAX + 1];
936 snprintfz(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
938 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
941 ff = procfile_readall(ff);
943 // procfile_close(ff);
949 p->io_logical_bytes_read = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
950 p->io_logical_bytes_written = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
951 p->io_read_calls = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
952 p->io_write_calls = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
953 p->io_storage_bytes_read = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
954 p->io_storage_bytes_written = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
955 p->io_cancelled_write_bytes = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
957 // procfile_close(ff);
962 // ----------------------------------------------------------------------------
964 // this is used to keep a global list of all open files of the system
965 // it is needed in order to calculate the unique files processes have open
967 #define FILE_DESCRIPTORS_INCREASE_STEP 100
969 struct file_descriptor {
971 #ifdef NETDATA_INTERNAL_CHECKS
973 #endif /* NETDATA_INTERNAL_CHECKS */
981 int all_files_len = 0;
982 int all_files_size = 0;
984 int file_descriptor_compare(void* a, void* b) {
985 #ifdef NETDATA_INTERNAL_CHECKS
986 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
987 error("Corrupted index data detected. Please report this.");
988 #endif /* NETDATA_INTERNAL_CHECKS */
990 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
993 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
997 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
1000 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
1002 avl_tree all_files_index = {
1004 file_descriptor_compare
1007 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
1008 struct file_descriptor *result = NULL, tmp;
1009 tmp.hash = (hash)?hash:simple_hash(name);
1013 #ifdef NETDATA_INTERNAL_CHECKS
1014 tmp.magic = 0x0BADCAFE;
1015 #endif /* NETDATA_INTERNAL_CHECKS */
1017 avl_search(&all_files_index, (avl *) &tmp, file_descriptor_iterator, (avl **) &result);
1021 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1022 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1024 #define FILETYPE_OTHER 0
1025 #define FILETYPE_FILE 1
1026 #define FILETYPE_PIPE 2
1027 #define FILETYPE_SOCKET 3
1028 #define FILETYPE_INOTIFY 4
1029 #define FILETYPE_EVENTFD 5
1030 #define FILETYPE_EVENTPOLL 6
1031 #define FILETYPE_TIMERFD 7
1032 #define FILETYPE_SIGNALFD 8
1034 void file_descriptor_not_used(int id)
1036 if(id > 0 && id < all_files_size) {
1038 #ifdef NETDATA_INTERNAL_CHECKS
1039 if(all_files[id].magic != 0x0BADCAFE) {
1040 error("Ignoring request to remove empty file id %d.", id);
1043 #endif /* NETDATA_INTERNAL_CHECKS */
1045 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1047 if(all_files[id].count > 0) {
1048 all_files[id].count--;
1050 if(!all_files[id].count) {
1051 if(debug) fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1052 file_descriptor_remove(&all_files[id]);
1053 #ifdef NETDATA_INTERNAL_CHECKS
1054 all_files[id].magic = 0x00000000;
1055 #endif /* NETDATA_INTERNAL_CHECKS */
1060 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1062 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1065 int file_descriptor_find_or_add(const char *name)
1067 static int last_pos = 0;
1068 uint32_t hash = simple_hash(name);
1070 if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1072 struct file_descriptor *fd = file_descriptor_find(name, hash);
1075 if(debug) fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1081 // check we have enough memory to add it
1082 if(!all_files || all_files_len == all_files_size) {
1083 void *old = all_files;
1086 // there is no empty slot
1087 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1088 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1090 // if the address changed, we have to rebuild the index
1091 // since all pointers are now invalid
1092 if(old && old != (void *)all_files) {
1093 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1094 all_files_index.root = NULL;
1095 for(i = 0; i < all_files_size; i++) {
1096 if(!all_files[i].count) continue;
1097 file_descriptor_add(&all_files[i]);
1099 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1102 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1103 all_files[i].count = 0;
1104 all_files[i].name = NULL;
1105 #ifdef NETDATA_INTERNAL_CHECKS
1106 all_files[i].magic = 0x00000000;
1107 #endif /* NETDATA_INTERNAL_CHECKS */
1108 all_files[i].pos = i;
1111 if(!all_files_size) all_files_len = 1;
1112 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1115 if(debug) fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1117 // search for an empty slot
1119 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1120 if(c >= all_files_size) c = 0;
1121 if(c == 0) continue;
1123 if(!all_files[c].count) {
1124 if(debug) fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1126 #ifdef NETDATA_INTERNAL_CHECKS
1127 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1128 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1129 #endif /* NETDATA_INTERNAL_CHECKS */
1131 if(debug) 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);
1132 if(all_files[c].name) free((void *)all_files[c].name);
1133 all_files[c].name = NULL;
1138 if(i == all_files_size) {
1139 fatal("We should find an empty slot, but there isn't any");
1142 if(debug) fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1146 // else we have an empty slot in 'c'
1149 if(name[0] == '/') type = FILETYPE_FILE;
1150 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1151 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1152 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1153 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1154 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1155 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1156 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1157 else if(strncmp(name, "anon_inode:", 11) == 0) {
1158 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1159 type = FILETYPE_OTHER;
1162 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1163 type = FILETYPE_OTHER;
1166 all_files[c].name = strdup(name);
1167 all_files[c].hash = hash;
1168 all_files[c].type = type;
1169 all_files[c].pos = c;
1170 all_files[c].count = 1;
1171 #ifdef NETDATA_INTERNAL_CHECKS
1172 all_files[c].magic = 0x0BADCAFE;
1173 #endif /* NETDATA_INTERNAL_CHECKS */
1174 file_descriptor_add(&all_files[c]);
1176 if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1181 int read_pid_file_descriptors(struct pid_stat *p) {
1182 char dirname[FILENAME_MAX+1];
1184 snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
1185 DIR *fds = opendir(dirname);
1189 char fdname[FILENAME_MAX + 1];
1190 char linkname[FILENAME_MAX + 1];
1192 // make the array negative
1193 for(c = 0 ; c < p->fds_size ; c++)
1194 p->fds[c] = -p->fds[c];
1196 while((de = readdir(fds))) {
1197 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1200 // check if the fds array is small
1201 int fdid = atoi(de->d_name);
1202 if(fdid < 0) continue;
1203 if(fdid >= p->fds_size) {
1204 // it is small, extend it
1205 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1206 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1208 fatal("Cannot re-allocate fds for %s", p->comm);
1212 // and initialize it
1213 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1214 p->fds_size = fdid + 100;
1217 if(p->fds[fdid] == 0) {
1218 // we don't know this fd, get it
1220 sprintf(fdname, "%s/proc/%d/fd/%s", host_prefix, p->pid, de->d_name);
1221 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1223 if(debug || (p->target && p->target->debug)) {
1224 if(debug || (p->target && p->target->debug))
1225 error("Cannot read link %s", fdname);
1232 // if another process already has this, we will get
1234 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1237 // else make it positive again, we need it
1238 // of course, the actual file may have changed, but we don't care so much
1239 // FIXME: we could compare the inode as returned by readdir direct structure
1240 else p->fds[fdid] = -p->fds[fdid];
1244 // remove all the negative file descriptors
1245 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1246 file_descriptor_not_used(-p->fds[c]);
1255 // ----------------------------------------------------------------------------
1257 // 1. read all files in /proc
1258 // 2. for each numeric directory:
1259 // i. read /proc/pid/stat
1260 // ii. read /proc/pid/statm
1261 // iii. read /proc/pid/io (requires root access)
1262 // iii. read the entries in directory /proc/pid/fd (requires root access)
1264 // a. find or create a struct file_descriptor
1265 // b. cleanup any old/unused file_descriptors
1267 // after all these, some pids may be linked to targets, while others may not
1269 // in case of errors, only 1 every 1000 errors is printed
1270 // to avoid filling up all disk space
1271 // if debug is enabled, all errors are printed
1273 int collect_data_for_all_processes_from_proc(void)
1275 char dirname[FILENAME_MAX + 1];
1277 snprintfz(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1278 DIR *dir = opendir(dirname);
1281 struct dirent *file = NULL;
1282 struct pid_stat *p = NULL;
1284 // mark them all as un-updated
1286 for(p = root_of_pids; p ; p = p->next) {
1290 p->children_count = 0;
1294 p->last_minflt = p->minflt;
1295 p->last_cminflt = p->cminflt;
1296 p->last_majflt = p->majflt;
1297 p->last_cmajflt = p->cmajflt;
1298 p->last_utime = p->utime;
1299 p->last_stime = p->stime;
1300 p->last_cutime = p->cutime;
1301 p->last_cstime = p->cstime;
1303 p->last_io_logical_bytes_read = p->io_logical_bytes_read;
1304 p->last_io_logical_bytes_written = p->io_logical_bytes_written;
1305 p->last_io_read_calls = p->io_read_calls;
1306 p->last_io_write_calls = p->io_write_calls;
1307 p->last_io_storage_bytes_read = p->io_storage_bytes_read;
1308 p->last_io_storage_bytes_written = p->io_storage_bytes_written;
1309 p->last_io_cancelled_write_bytes = p->io_cancelled_write_bytes;
1312 while((file = readdir(dir))) {
1313 char *endptr = file->d_name;
1314 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1316 // make sure we read a valid number
1317 if(unlikely(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0'))
1320 p = get_pid_entry(pid);
1321 if(unlikely(!p)) continue;
1324 // --------------------------------------------------------------------
1327 if(unlikely(read_proc_pid_stat(p))) {
1328 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1330 // there is no reason to proceed if we cannot get its status
1334 // check its parent pid
1335 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1336 error("Pid %d states invalid parent pid %d. Using 0.", pid, p->ppid);
1341 // --------------------------------------------------------------------
1342 // /proc/<pid>/cmdline
1344 if(proc_pid_cmdline_is_needed) {
1345 if(unlikely(read_proc_pid_cmdline(p))) {
1346 error("Cannot process %s/proc/%d/cmdline", host_prefix, pid);
1350 // --------------------------------------------------------------------
1351 // /proc/<pid>/statm
1353 if(unlikely(read_proc_pid_statm(p))) {
1354 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1356 // there is no reason to proceed if we cannot get its memory status
1361 // --------------------------------------------------------------------
1364 if(unlikely(read_proc_pid_io(p))) {
1365 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1367 // on systems without /proc/X/io
1368 // allow proceeding without I/O information
1372 // --------------------------------------------------------------------
1375 if(unlikely(read_proc_pid_ownership(p))) {
1376 error("Cannot stat %s/proc/%d", host_prefix, pid);
1379 // --------------------------------------------------------------------
1382 // check if it is target
1383 // we do this only once, the first time this pid is loaded
1384 if(unlikely(p->new_entry)) {
1385 if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
1386 uint32_t hash = simple_hash(p->comm);
1387 size_t pclen = strlen(p->comm);
1390 for(w = apps_groups_root_target; w ; w = w->next) {
1391 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1393 // find it - 4 cases:
1394 // 1. the target is not a pattern
1395 // 2. the target has the prefix
1396 // 3. the target has the suffix
1397 // 4. the target is something inside cmdline
1398 if( (!w->starts_with && !w->ends_with && w->comparehash == hash && !strcmp(w->compare, p->comm))
1399 || (w->starts_with && !w->ends_with && !strncmp(w->compare, p->comm, w->comparelen))
1400 || (!w->starts_with && w->ends_with && pclen >= w->comparelen && !strcmp(w->compare, &p->comm[pclen - w->comparelen]))
1401 || (proc_pid_cmdline_is_needed && w->starts_with && w->ends_with && strstr(p->cmdline, w->compare))
1403 if(w->target) p->target = w->target;
1406 if(debug || (p->target && p->target->debug))
1407 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1412 // --------------------------------------------------------------------
1415 if(unlikely(read_pid_file_descriptors(p))) {
1416 error("Cannot process entries in %s/proc/%d/fd", host_prefix, pid);
1419 // --------------------------------------------------------------------
1422 // mark it as updated
1432 // ----------------------------------------------------------------------------
1434 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1435 // print a tree view of all processes
1436 int debug_childrens_aggregations(pid_t pid, int level) {
1437 struct pid_stat *p = NULL;
1441 for(i = 0; i < level; i++) b[i] = '\t';
1446 for(p = root_of_pids; p ; p = p->next) {
1447 if(p->ppid == pid) {
1448 ret += debug_childrens_aggregations(p->pid, level+1);
1454 if(!p->updated) ret += 1;
1455 if(ret) fprintf(stderr, "%s %s %d [%s, %s] c=%d u=%llu+%llu, s=%llu+%llu, cu=%llu+%llu, cs=%llu+%llu, n=%llu+%llu, j=%llu+%llu, cn=%llu+%llu, cj=%llu+%llu\n"
1456 , b, p->comm, p->pid, p->updated?"OK":"KILLED", p->target->name, p->children_count
1457 , p->utime, p->utime - p->old_utime
1458 , p->stime, p->stime - p->old_stime
1459 , p->cutime, p->cutime - p->old_cutime
1460 , p->cstime, p->cstime - p->old_cstime
1461 , p->minflt, p->minflt - p->old_minflt
1462 , p->majflt, p->majflt - p->old_majflt
1463 , p->cminflt, p->cminflt - p->old_cminflt
1464 , p->cmajflt, p->cmajflt - p->old_cmajflt
1470 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1474 // ----------------------------------------------------------------------------
1475 // update statistics on the targets
1477 // 1. link all childs to their parents
1478 // 2. go from bottom to top, marking as merged all childs to their parents
1479 // this step links all parents without a target to the child target, if any
1480 // 3. link all top level processes (the ones not merged) to the default target
1481 // 4. go from top to bottom, linking all childs without a target, to their parent target
1482 // after this step, all processes have a target
1483 // [5. for each killed pid (updated = 0), remove its usage from its target]
1484 // 6. zero all apps_groups_targets
1485 // 7. concentrate all values on the apps_groups_targets
1486 // 8. remove all killed processes
1487 // 9. find the unique file count for each target
1488 // check: update_apps_groups_statistics()
1490 void link_all_processes_to_their_parents(void) {
1491 struct pid_stat *p = NULL;
1493 // link all children to their parents
1494 // and update children count on parents
1495 for(p = root_of_pids; p ; p = p->next) {
1496 // for each process found running
1499 && p->ppid <= pid_max
1500 && all_pids[p->ppid]
1502 // for valid processes
1504 if(debug || (p->target && p->target->debug))
1505 fprintf(stderr, "apps.plugin: \tparent of %d (%s) is %d (%s)\n", p->pid, p->comm, p->ppid, all_pids[p->ppid]->comm);
1507 p->parent = all_pids[p->ppid];
1508 p->parent->children_count++;
1510 else if(p->ppid != 0)
1511 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1515 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1516 void aggregate_children_to_parents(void) {
1517 struct pid_stat *p = NULL;
1519 // for each killed process, remove its values from the parents
1520 // sums (we had already added them in a previous loop)
1521 for(p = root_of_pids; p ; p = p->next) {
1522 if(p->updated) continue;
1524 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1526 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1527 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1528 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1529 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1531 struct pid_stat *t = p;
1532 while((t = t->parent)) {
1533 if(!t->updated) continue;
1535 unsigned long long x;
1536 if(diff_utime && t->diff_cutime) {
1537 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1539 t->diff_cutime -= x;
1541 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1543 if(diff_stime && t->diff_cstime) {
1544 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1546 t->diff_cstime -= x;
1548 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1550 if(diff_minflt && t->diff_cminflt) {
1551 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1553 t->diff_cminflt -= x;
1554 t->fix_cminflt += x;
1555 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1557 if(diff_majflt && t->diff_cmajflt) {
1558 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1560 t->diff_cmajflt -= x;
1561 t->fix_cmajflt += x;
1562 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1566 if(diff_utime) error("Cannot fix up utime %llu", diff_utime);
1567 if(diff_stime) error("Cannot fix up stime %llu", diff_stime);
1568 if(diff_minflt) error("Cannot fix up minflt %llu", diff_minflt);
1569 if(diff_majflt) error("Cannot fix up majflt %llu", diff_majflt);
1572 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1574 void cleanup_non_existing_pids(void) {
1576 struct pid_stat *p = NULL;
1578 for(p = root_of_pids; p ;) {
1580 // 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);
1582 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1583 file_descriptor_not_used(p->fds[c]);
1595 void apply_apps_groups_targets_inheritance(void) {
1596 struct pid_stat *p = NULL;
1598 // children that do not have a target
1599 // inherit their target from their parent
1603 for(p = root_of_pids; p ; p = p->next) {
1604 // if this process does not have a target
1605 // and it has a parent
1606 // and its parent has a target
1607 // then, set the parent's target to this process
1608 if(unlikely(!p->target && p->parent && p->parent->target)) {
1609 p->target = p->parent->target;
1612 if(debug || (p->target && p->target->debug))
1613 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);
1619 // find all the procs with 0 childs and merge them to their parents
1620 // repeat, until nothing more can be done.
1624 for(p = root_of_pids; p ; p = p->next) {
1625 // if this process does not have any children
1626 // and is not already merged
1628 // and its parent has children
1629 // and the target of this process and its parent is the same, or the parent does not have a target
1630 // and its parent is not init
1631 // then, mark them as merged.
1636 && p->parent->children_count
1637 && (p->target == p->parent->target || !p->parent->target)
1640 p->parent->children_count--;
1643 // the parent inherits the child's target, if it does not have a target itself
1644 if(unlikely(p->target && !p->parent->target)) {
1645 p->parent->target = p->target;
1647 if(debug || (p->target && p->target->debug))
1648 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);
1656 fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1659 // init goes always to default target
1661 all_pids[1]->target = apps_groups_default_target;
1663 // give a default target on all top level processes
1664 for(p = root_of_pids; p ; p = p->next) {
1665 // if the process is not merged itself
1666 // then is is a top level process
1667 if(!p->merged && !p->target)
1668 p->target = apps_groups_default_target;
1670 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1671 // by the way, update the diffs
1672 // will be used later for subtracting killed process times
1673 p->diff_cutime = p->utime - p->cutime;
1674 p->diff_cstime = p->stime - p->cstime;
1675 p->diff_cminflt = p->minflt - p->cminflt;
1676 p->diff_cmajflt = p->majflt - p->cmajflt;
1677 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1680 // give a target to all merged child processes
1684 for(p = root_of_pids; p ; p = p->next) {
1685 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1686 p->target = p->parent->target;
1689 if(debug || (p->target && p->target->debug))
1690 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);
1696 long zero_all_targets(struct target *root) {
1700 for (w = root; w ; w = w->next) {
1703 if(w->fds) free(w->fds);
1719 w->statm_resident = 0;
1726 w->io_logical_bytes_read = 0;
1727 w->io_logical_bytes_written = 0;
1728 w->io_read_calls = 0;
1729 w->io_write_calls = 0;
1730 w->io_storage_bytes_read = 0;
1731 w->io_storage_bytes_written = 0;
1732 w->io_cancelled_write_bytes = 0;
1738 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1739 if(unlikely(!w->fds)) {
1740 w->fds = calloc(sizeof(int), (size_t) all_files_size);
1741 if(unlikely(!w->fds))
1742 error("Cannot allocate memory for fds in %s", w->name);
1745 if(likely(p->updated)) {
1746 w->cutime += p->cutime; // - p->fix_cutime;
1747 w->cstime += p->cstime; // - p->fix_cstime;
1748 w->cminflt += p->cminflt; // - p->fix_cminflt;
1749 w->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1751 w->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1752 w->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1753 w->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1754 w->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1756 //if(p->num_threads < 0)
1757 // error("Negative threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1759 //if(p->num_threads > 10000)
1760 // error("Excessive threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1762 w->num_threads += p->num_threads;
1765 w->statm_size += p->statm_size;
1766 w->statm_resident += p->statm_resident;
1767 w->statm_share += p->statm_share;
1768 w->statm_text += p->statm_text;
1769 w->statm_lib += p->statm_lib;
1770 w->statm_data += p->statm_data;
1771 w->statm_dirty += p->statm_dirty;
1773 w->io_logical_bytes_read += p->io_logical_bytes_read;
1774 w->io_logical_bytes_written += p->io_logical_bytes_written;
1775 w->io_read_calls += p->io_read_calls;
1776 w->io_write_calls += p->io_write_calls;
1777 w->io_storage_bytes_read += p->io_storage_bytes_read;
1778 w->io_storage_bytes_written += p->io_storage_bytes_written;
1779 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1783 if(likely(w->fds)) {
1785 for(c = 0; c < p->fds_size ;c++) {
1786 if(p->fds[c] == 0) continue;
1788 if(likely(p->fds[c] < all_files_size)) {
1789 if(w->fds) w->fds[p->fds[c]]++;
1792 error("Invalid fd number %d", p->fds[c]);
1796 if(unlikely(debug || w->debug))
1797 fprintf(stderr, "apps.plugin: \tAgregating %s pid %d on %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);
1799 /* if(p->utime - p->old_utime > 100) fprintf(stderr, "BIG CHANGE: %d %s utime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->utime - p->old_utime, p->old_utime, p->utime);
1800 if(p->cutime - p->old_cutime > 100) fprintf(stderr, "BIG CHANGE: %d %s cutime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cutime - p->old_cutime, p->old_cutime, p->cutime);
1801 if(p->stime - p->old_stime > 100) fprintf(stderr, "BIG CHANGE: %d %s stime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->stime - p->old_stime, p->old_stime, p->stime);
1802 if(p->cstime - p->old_cstime > 100) fprintf(stderr, "BIG CHANGE: %d %s cstime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cstime - p->old_cstime, p->old_cstime, p->cstime);
1803 if(p->minflt - p->old_minflt > 5000) fprintf(stderr, "BIG CHANGE: %d %s minflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->minflt - p->old_minflt, p->old_minflt, p->minflt);
1804 if(p->majflt - p->old_majflt > 5000) fprintf(stderr, "BIG CHANGE: %d %s majflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->majflt - p->old_majflt, p->old_majflt, p->majflt);
1805 if(p->cminflt - p->old_cminflt > 15000) fprintf(stderr, "BIG CHANGE: %d %s cminflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cminflt - p->old_cminflt, p->old_cminflt, p->cminflt);
1806 if(p->cmajflt - p->old_cmajflt > 15000) fprintf(stderr, "BIG CHANGE: %d %s cmajflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cmajflt - p->old_cmajflt, p->old_cmajflt, p->cmajflt);
1808 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1809 p->old_utime = p->utime;
1810 p->old_cutime = p->cutime;
1811 p->old_stime = p->stime;
1812 p->old_cstime = p->cstime;
1813 p->old_minflt = p->minflt;
1814 p->old_majflt = p->majflt;
1815 p->old_cminflt = p->cminflt;
1816 p->old_cmajflt = p->cmajflt;
1817 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1820 // since the process switched target
1821 // for all incremental values
1822 // we have to subtract its OLD values from the new target
1823 // and add its OLD values to the old target
1826 // We add/subtract the last/OLD values we added to the target
1828 w->fix_cutime -= p->last_cutime;
1829 w->fix_cstime -= p->last_cstime;
1830 w->fix_cminflt -= p->last_cminflt;
1831 w->fix_cmajflt -= p->last_cmajflt;
1833 w->fix_utime -= p->last_utime;
1834 w->fix_stime -= p->last_stime;
1835 w->fix_minflt -= p->last_minflt;
1836 w->fix_majflt -= p->last_majflt;
1839 w->fix_io_logical_bytes_read -= p->last_io_logical_bytes_read;
1840 w->fix_io_logical_bytes_written -= p->last_io_logical_bytes_written;
1841 w->fix_io_read_calls -= p->last_io_read_calls;
1842 w->fix_io_write_calls -= p->last_io_write_calls;
1843 w->fix_io_storage_bytes_read -= p->last_io_storage_bytes_read;
1844 w->fix_io_storage_bytes_written -= p->last_io_storage_bytes_written;
1845 w->fix_io_cancelled_write_bytes -= p->last_io_cancelled_write_bytes;
1849 o->fix_cutime += p->last_cutime;
1850 o->fix_cstime += p->last_cstime;
1851 o->fix_cminflt += p->last_cminflt;
1852 o->fix_cmajflt += p->last_cmajflt;
1854 o->fix_utime += p->last_utime;
1855 o->fix_stime += p->last_stime;
1856 o->fix_minflt += p->last_minflt;
1857 o->fix_majflt += p->last_majflt;
1859 o->fix_io_logical_bytes_read += p->last_io_logical_bytes_read;
1860 o->fix_io_logical_bytes_written += p->last_io_logical_bytes_written;
1861 o->fix_io_read_calls += p->last_io_read_calls;
1862 o->fix_io_write_calls += p->last_io_write_calls;
1863 o->fix_io_storage_bytes_read += p->last_io_storage_bytes_read;
1864 o->fix_io_storage_bytes_written += p->last_io_storage_bytes_written;
1865 o->fix_io_cancelled_write_bytes += p->last_io_cancelled_write_bytes;
1869 // if(o) fprintf(stderr, "apps.plugin: \t\tpid %d (%s) is not updated by OLD target %s (%s) is present.\n", p->pid, p->comm, o->id, o->name);
1871 // since the process has exited, the user
1872 // will see a drop in our charts, because the incremental
1873 // values of this process will not be there
1875 // add them to the fix_* values and they will be added to
1876 // the reported values, so that the report goes steady
1877 w->fix_minflt += p->minflt;
1878 w->fix_majflt += p->majflt;
1879 w->fix_utime += p->utime;
1880 w->fix_stime += p->stime;
1881 w->fix_cminflt += p->cminflt;
1882 w->fix_cmajflt += p->cmajflt;
1883 w->fix_cutime += p->cutime;
1884 w->fix_cstime += p->cstime;
1886 w->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1887 w->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1888 w->fix_io_read_calls += p->io_read_calls;
1889 w->fix_io_write_calls += p->io_write_calls;
1890 w->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1891 w->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1892 w->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1897 void count_targets_fds(struct target *root) {
1901 for (w = root; w ; w = w->next) {
1902 if(!w->fds) continue;
1907 w->openinotifies = 0;
1908 w->openeventfds = 0;
1909 w->opentimerfds = 0;
1910 w->opensignalfds = 0;
1911 w->openeventpolls = 0;
1914 for(c = 1; c < all_files_size ;c++) {
1916 switch(all_files[c].type) {
1925 case FILETYPE_SOCKET:
1929 case FILETYPE_INOTIFY:
1933 case FILETYPE_EVENTFD:
1937 case FILETYPE_TIMERFD:
1941 case FILETYPE_SIGNALFD:
1945 case FILETYPE_EVENTPOLL:
1946 w->openeventpolls++;
1959 void calculate_netdata_statistics(void)
1961 link_all_processes_to_their_parents();
1962 apply_apps_groups_targets_inheritance();
1964 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1965 aggregate_children_to_parents();
1966 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1968 zero_all_targets(users_root_target);
1969 zero_all_targets(groups_root_target);
1970 apps_groups_targets = zero_all_targets(apps_groups_root_target);
1972 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1974 debug_childrens_aggregations(0, 1);
1975 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1977 // this has to be done, before the cleanup
1978 struct pid_stat *p = NULL;
1979 struct target *w = NULL, *o = NULL;
1981 // concentrate everything on the apps_groups_targets
1982 for(p = root_of_pids; p ; p = p->next) {
1984 // --------------------------------------------------------------------
1985 // apps_groups targets
1986 if(likely(p->target))
1987 aggregate_pid_on_target(p->target, p, NULL);
1989 error("pid %d %s was left without a target!", p->pid, p->comm);
1992 // --------------------------------------------------------------------
1995 if(likely(p->user_target && p->user_target->uid == p->uid))
1998 if(unlikely(debug && p->user_target))
1999 fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched user from %d (%s) to %d.\n", p->pid, p->comm, p->user_target->uid, p->user_target->name, p->uid);
2001 w = p->user_target = get_users_target(p->uid);
2005 aggregate_pid_on_target(w, p, o);
2007 error("pid %d %s was left without a user target!", p->pid, p->comm);
2010 // --------------------------------------------------------------------
2012 o = p->group_target;
2013 if(likely(p->group_target && p->group_target->gid == p->gid))
2014 w = p->group_target;
2016 if(unlikely(debug && p->group_target))
2017 fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched group from %d (%s) to %d.\n", p->pid, p->comm, p->group_target->gid, p->group_target->name, p->gid);
2019 w = p->group_target = get_groups_target(p->gid);
2023 aggregate_pid_on_target(w, p, o);
2025 error("pid %d %s was left without a group target!", p->pid, p->comm);
2029 count_targets_fds(apps_groups_root_target);
2030 count_targets_fds(users_root_target);
2031 count_targets_fds(groups_root_target);
2033 cleanup_non_existing_pids();
2036 // ----------------------------------------------------------------------------
2037 // update chart dimensions
2039 unsigned long long send_resource_usage_to_netdata() {
2040 static struct timeval last = { 0, 0 };
2041 static struct rusage me_last;
2046 unsigned long long usec;
2047 unsigned long long cpuuser;
2048 unsigned long long cpusyst;
2051 gettimeofday(&last, NULL);
2052 getrusage(RUSAGE_SELF, &me_last);
2054 // the first time, give a zero to allow
2055 // netdata calibrate to the current time
2056 // usec = update_every * 1000000ULL;
2062 gettimeofday(&now, NULL);
2063 getrusage(RUSAGE_SELF, &me);
2065 usec = usecdiff(&now, &last);
2066 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2067 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2069 bcopy(&now, &last, sizeof(struct timeval));
2070 bcopy(&me, &me_last, sizeof(struct rusage));
2073 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
2074 fprintf(stdout, "SET user = %llu\n", cpuuser);
2075 fprintf(stdout, "SET system = %llu\n", cpusyst);
2076 fprintf(stdout, "END\n");
2078 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
2079 fprintf(stdout, "SET files = %llu\n", file_counter);
2080 fprintf(stdout, "SET pids = %ld\n", all_pids_count);
2081 fprintf(stdout, "SET fds = %d\n", all_files_len);
2082 fprintf(stdout, "SET targets = %ld\n", apps_groups_targets);
2083 fprintf(stdout, "END\n");
2088 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec)
2092 fprintf(stdout, "BEGIN %s.cpu %llu\n", type, usec);
2093 for (w = root; w ; w = w->next) {
2094 if(w->target || (!w->processes && !w->exposed)) continue;
2096 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
2098 fprintf(stdout, "END\n");
2100 fprintf(stdout, "BEGIN %s.cpu_user %llu\n", type, usec);
2101 for (w = root; w ; w = w->next) {
2102 if(w->target || (!w->processes && !w->exposed)) continue;
2104 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
2106 fprintf(stdout, "END\n");
2108 fprintf(stdout, "BEGIN %s.cpu_system %llu\n", type, usec);
2109 for (w = root; w ; w = w->next) {
2110 if(w->target || (!w->processes && !w->exposed)) continue;
2112 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
2114 fprintf(stdout, "END\n");
2116 fprintf(stdout, "BEGIN %s.threads %llu\n", type, usec);
2117 for (w = root; w ; w = w->next) {
2118 if(w->target || (!w->processes && !w->exposed)) continue;
2120 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
2122 fprintf(stdout, "END\n");
2124 fprintf(stdout, "BEGIN %s.processes %llu\n", type, usec);
2125 for (w = root; w ; w = w->next) {
2126 if(w->target || (!w->processes && !w->exposed)) continue;
2128 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
2130 fprintf(stdout, "END\n");
2132 fprintf(stdout, "BEGIN %s.mem %llu\n", type, usec);
2133 for (w = root; w ; w = w->next) {
2134 if(w->target || (!w->processes && !w->exposed)) continue;
2136 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
2138 fprintf(stdout, "END\n");
2140 fprintf(stdout, "BEGIN %s.minor_faults %llu\n", type, usec);
2141 for (w = root; w ; w = w->next) {
2142 if(w->target || (!w->processes && !w->exposed)) continue;
2144 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt);
2146 fprintf(stdout, "END\n");
2148 fprintf(stdout, "BEGIN %s.major_faults %llu\n", type, usec);
2149 for (w = root; w ; w = w->next) {
2150 if(w->target || (!w->processes && !w->exposed)) continue;
2152 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt);
2154 fprintf(stdout, "END\n");
2156 fprintf(stdout, "BEGIN %s.lreads %llu\n", type, usec);
2157 for (w = root; w ; w = w->next) {
2158 if(w->target || (!w->processes && !w->exposed)) continue;
2160 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read + w->fix_io_logical_bytes_read);
2162 fprintf(stdout, "END\n");
2164 fprintf(stdout, "BEGIN %s.lwrites %llu\n", type, usec);
2165 for (w = root; w ; w = w->next) {
2166 if(w->target || (!w->processes && !w->exposed)) continue;
2168 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written + w->fix_io_logical_bytes_written);
2170 fprintf(stdout, "END\n");
2172 fprintf(stdout, "BEGIN %s.preads %llu\n", type, usec);
2173 for (w = root; w ; w = w->next) {
2174 if(w->target || (!w->processes && !w->exposed)) continue;
2176 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read + w->fix_io_storage_bytes_read);
2178 fprintf(stdout, "END\n");
2180 fprintf(stdout, "BEGIN %s.pwrites %llu\n", type, usec);
2181 for (w = root; w ; w = w->next) {
2182 if(w->target || (!w->processes && !w->exposed)) continue;
2184 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written + w->fix_io_storage_bytes_written);
2186 fprintf(stdout, "END\n");
2188 fprintf(stdout, "BEGIN %s.files %llu\n", type, usec);
2189 for (w = root; w ; w = w->next) {
2190 if(w->target || (!w->processes && !w->exposed)) continue;
2192 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
2194 fprintf(stdout, "END\n");
2196 fprintf(stdout, "BEGIN %s.sockets %llu\n", type, usec);
2197 for (w = root; w ; w = w->next) {
2198 if(w->target || (!w->processes && !w->exposed)) continue;
2200 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
2202 fprintf(stdout, "END\n");
2204 fprintf(stdout, "BEGIN %s.pipes %llu\n", type, usec);
2205 for (w = root; w ; w = w->next) {
2206 if(w->target || (!w->processes && !w->exposed)) continue;
2208 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
2210 fprintf(stdout, "END\n");
2216 // ----------------------------------------------------------------------------
2217 // generate the charts
2219 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2222 int newly_added = 0;
2224 for(w = root ; w ; w = w->next)
2225 if(!w->exposed && w->processes) {
2228 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2231 // nothing more to show
2232 if(!newly_added) return;
2234 // we have something new to show
2235 // update the charts
2236 fprintf(stdout, "CHART %s.cpu '' '%s CPU Time (%ld%% = %ld core%s)' 'cpu time %%' cpu %s.cpu stacked 20001 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2237 for (w = root; w ; w = w->next) {
2238 if(w->target || (!w->processes && !w->exposed)) continue;
2240 fprintf(stdout, "DIMENSION %s '' incremental 100 %u %s\n", w->name, hz, w->hidden ? "hidden,noreset" : "noreset");
2243 fprintf(stdout, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2244 for (w = root; w ; w = w->next) {
2245 if(w->target || (!w->processes && !w->exposed)) continue;
2247 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld noreset\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2250 fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2251 for (w = root; w ; w = w->next) {
2252 if(w->target || (!w->processes && !w->exposed)) continue;
2254 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2257 fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2258 for (w = root; w ; w = w->next) {
2259 if(w->target || (!w->processes && !w->exposed)) continue;
2261 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2264 fprintf(stdout, "CHART %s.cpu_user '' '%s CPU User Time (%ld%% = %ld core%s)' 'cpu time %%' cpu %s.cpu_user stacked 20020 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2265 for (w = root; w ; w = w->next) {
2266 if(w->target || (!w->processes && !w->exposed)) continue;
2268 fprintf(stdout, "DIMENSION %s '' incremental 100 %ld noreset\n", w->name, hz * processors);
2271 fprintf(stdout, "CHART %s.cpu_system '' '%s CPU System Time (%ld%% = %ld core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20021 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2272 for (w = root; w ; w = w->next) {
2273 if(w->target || (!w->processes && !w->exposed)) continue;
2275 fprintf(stdout, "DIMENSION %s '' incremental 100 %ld noreset\n", w->name, hz * processors);
2278 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);
2279 for (w = root; w ; w = w->next) {
2280 if(w->target || (!w->processes && !w->exposed)) continue;
2282 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
2285 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);
2286 for (w = root; w ; w = w->next) {
2287 if(w->target || (!w->processes && !w->exposed)) continue;
2289 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
2292 fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2293 for (w = root; w ; w = w->next) {
2294 if(w->target || (!w->processes && !w->exposed)) continue;
2296 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2299 fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2300 for (w = root; w ; w = w->next) {
2301 if(w->target || (!w->processes && !w->exposed)) continue;
2303 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2306 fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2307 for (w = root; w ; w = w->next) {
2308 if(w->target || (!w->processes && !w->exposed)) continue;
2310 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2313 fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2314 for (w = root; w ; w = w->next) {
2315 if(w->target || (!w->processes && !w->exposed)) continue;
2317 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2320 fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2321 for (w = root; w ; w = w->next) {
2322 if(w->target || (!w->processes && !w->exposed)) continue;
2324 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2327 fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2328 for (w = root; w ; w = w->next) {
2329 if(w->target || (!w->processes && !w->exposed)) continue;
2331 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2334 fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2335 for (w = root; w ; w = w->next) {
2336 if(w->target || (!w->processes && !w->exposed)) continue;
2338 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2343 // ----------------------------------------------------------------------------
2344 // parse command line arguments
2346 void parse_args(int argc, char **argv)
2351 for(i = 1; i < argc; i++) {
2353 int n = atoi(argv[i]);
2360 if(strcmp("debug", argv[i]) == 0) {
2362 debug_flags = 0xffffffff;
2371 error("Cannot understand option %s", argv[i]);
2375 if(freq > 0) update_every = freq;
2376 if(!name) name = "groups";
2378 if(read_apps_groups_conf(name)) {
2379 error("Cannot read process groups %s", name);
2384 int main(int argc, char **argv)
2386 // debug_flags = D_PROCFILE;
2388 // set the name for logging
2389 program_name = "apps.plugin";
2391 // disable syslog for apps.plugin
2392 error_log_syslog = 0;
2394 // set errors flood protection to 100 logs per hour
2395 error_log_errors_per_period = 100;
2396 error_log_throttle_period = 3600;
2398 host_prefix = getenv("NETDATA_HOST_PREFIX");
2399 if(host_prefix == NULL) {
2400 info("NETDATA_HOST_PREFIX is not passed from netdata");
2403 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2405 config_dir = getenv("NETDATA_CONFIG_DIR");
2406 if(config_dir == NULL) {
2407 info("NETDATA_CONFIG_DIR is not passed from netdata");
2408 config_dir = CONFIG_DIR;
2410 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2412 #ifdef NETDATA_INTERNAL_CHECKS
2413 if(debug_flags != 0) {
2414 struct rlimit rl = { RLIM_INFINITY, RLIM_INFINITY };
2415 if(setrlimit(RLIMIT_CORE, &rl) != 0)
2416 info("Cannot request unlimited core dumps for debugging... Proceeding anyway...");
2417 prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
2419 #endif /* NETDATA_INTERNAL_CHECKS */
2421 info("starting...");
2423 procfile_adaptive_initial_allocation = 1;
2425 time_t started_t = time(NULL);
2428 pid_max = get_system_pid_max();
2429 processors = get_system_cpus();
2431 parse_args(argc, argv);
2433 all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2435 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2436 printf("DISABLE\n");
2440 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
2441 "DIMENSION user '' incremental 1 1000\n"
2442 "DIMENSION system '' incremental 1 1000\n"
2443 "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %1$d\n"
2444 "DIMENSION files '' incremental 1 1\n"
2445 "DIMENSION pids '' absolute 1 1\n"
2446 "DIMENSION fds '' absolute 1 1\n"
2447 "DIMENSION targets '' absolute 1 1\n", update_every);
2449 #ifndef PROFILING_MODE
2450 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2451 unsigned long long sunow;
2452 #endif /* PROFILING_MODE */
2454 unsigned long long counter = 1;
2455 for(;1; counter++) {
2456 #ifndef PROFILING_MODE
2457 // delay until it is our time to run
2458 while((sunow = timems()) < sunext)
2459 usleep((useconds_t)(sunext - sunow));
2461 // find the next time we need to run
2462 while(timems() > sunext)
2463 sunext += update_every * 1000000ULL;
2464 #endif /* PROFILING_MODE */
2466 if(!collect_data_for_all_processes_from_proc()) {
2467 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2468 printf("DISABLE\n");
2472 calculate_netdata_statistics();
2474 unsigned long long dt = send_resource_usage_to_netdata();
2476 // this is smart enough to show only newly added apps, when needed
2477 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2478 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2479 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2481 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2482 send_collected_data_to_netdata(users_root_target, "users", dt);
2483 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2485 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
2487 current_t = time(NULL);
2489 #ifndef PROFILING_MODE
2490 // restart check (14400 seconds)
2491 if(current_t - started_t > 14400) exit(0);
2493 if(current_t - started_t > 10) exit(0);
2494 #endif /* PROFILING_MODE */