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>
41 #define MAX_COMPARE_NAME 15
44 unsigned long long Hertz = 1;
51 unsigned long long file_counter = 0;
53 char *host_prefix = "";
54 char *config_dir = CONFIG_DIR;
56 #ifdef NETDATA_INTERNAL_CHECKS
57 // ----------------------------------------------------------------------------
59 // do not use in production systems - it mis-aligns allocated memory
65 } allocations = { 0, 0, 0 };
67 #define MALLOC_MARK (uint32_t)(0x0BADCAFE)
68 #define MALLOC_PREFIX (sizeof(uint32_t) * 2)
69 #define MALLOC_SUFFIX (sizeof(uint32_t))
70 #define MALLOC_OVERHEAD (MALLOC_PREFIX + MALLOC_SUFFIX)
72 void *mark_allocation(void *allocated_ptr, size_t size_without_overheads) {
73 uint32_t *real_ptr = (uint32_t *)allocated_ptr;
74 real_ptr[0] = MALLOC_MARK;
75 real_ptr[1] = (uint32_t) size_without_overheads;
77 uint32_t *end_ptr = (uint32_t *)(allocated_ptr + MALLOC_PREFIX + size_without_overheads);
78 end_ptr[0] = MALLOC_MARK;
80 // fprintf(stderr, "MEMORY_POINTER: Allocated at %p, returning %p.\n", allocated_ptr, (void *)(allocated_ptr + MALLOC_PREFIX));
82 return allocated_ptr + MALLOC_PREFIX;
85 void *check_allocation(const char *file, int line, const char *function, void *marked_ptr, size_t *size_without_overheads_ptr) {
86 uint32_t *real_ptr = (uint32_t *)(marked_ptr - MALLOC_PREFIX);
88 // 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);
90 if(real_ptr[0] != MALLOC_MARK) fatal("MEMORY: prefix MARK is not valid for %s/%u@%s.", function, line, file);
92 size_t size = real_ptr[1];
94 uint32_t *end_ptr = (uint32_t *)(marked_ptr + size);
95 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);
97 if(size_without_overheads_ptr) *size_without_overheads_ptr = size;
102 void *malloc_debug(const char *file, int line, const char *function, size_t size) {
103 void *ptr = malloc(size + MALLOC_OVERHEAD);
104 if(!ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
106 allocations.allocated += size;
107 allocations.allocations++;
109 debug(D_MEMORY, "MEMORY: Allocated %zu bytes for %s/%u@%s."
110 " Status: allocated %zu in %zu allocs."
112 , function, line, file
113 , allocations.allocated
114 , allocations.allocations
117 if(allocations.allocated > allocations.allocated_max) {
118 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
119 allocations.allocated_max = allocations.allocated;
123 check_allocation(file, line, function, mark_allocation(ptr, size), &csize);
125 fatal("Invalid size.");
128 return mark_allocation(ptr, size);
131 void *calloc_debug(const char *file, int line, const char *function, size_t nmemb, size_t size) {
132 void *ptr = malloc_debug(file, line, function, (nmemb * size));
133 bzero(ptr, nmemb * size);
137 void free_debug(const char *file, int line, const char *function, void *ptr) {
139 void *real_ptr = check_allocation(file, line, function, ptr, &size);
141 bzero(real_ptr, size + MALLOC_OVERHEAD);
144 allocations.allocated -= size;
145 allocations.allocations--;
147 debug(D_MEMORY, "MEMORY: freed %zu bytes for %s/%u@%s."
148 " Status: allocated %zu in %zu allocs."
150 , function, line, file
151 , allocations.allocated
152 , allocations.allocations
156 void *realloc_debug(const char *file, int line, const char *function, void *ptr, size_t size) {
157 if(!ptr) return malloc_debug(file, line, function, size);
158 if(!size) { free_debug(file, line, function, ptr); return NULL; }
161 void *real_ptr = check_allocation(file, line, function, ptr, &old_size);
163 void *new_ptr = realloc(real_ptr, size + MALLOC_OVERHEAD);
164 if(!new_ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
166 allocations.allocated += size;
167 allocations.allocated -= old_size;
169 debug(D_MEMORY, "MEMORY: Re-allocated from %zu to %zu bytes for %s/%u@%s."
170 " Status: allocated %z in %zu allocs."
172 , function, line, file
173 , allocations.allocated
174 , allocations.allocations
177 if(allocations.allocated > allocations.allocated_max) {
178 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
179 allocations.allocated_max = allocations.allocated;
182 return mark_allocation(new_ptr, size);
185 char *strdup_debug(const char *file, int line, const char *function, const char *ptr) {
192 char *p = malloc_debug(file, line, function, size);
193 if(!p) fatal("Cannot allocate %zu bytes.", size);
195 memcpy(p, ptr, size);
199 #define malloc(size) malloc_debug(__FILE__, __LINE__, __FUNCTION__, (size))
200 #define calloc(nmemb, size) calloc_debug(__FILE__, __LINE__, __FUNCTION__, (nmemb), (size))
201 #define realloc(ptr, size) realloc_debug(__FILE__, __LINE__, __FUNCTION__, (ptr), (size))
202 #define free(ptr) free_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
207 #define strdup(ptr) strdup_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
209 #endif /* NETDATA_INTERNAL_CHECKS */
212 // ----------------------------------------------------------------------------
214 // to retrieve settings of the system
218 long get_system_cpus(void) {
221 char filename[FILENAME_MAX + 1];
222 snprintf(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
224 ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
227 ff = procfile_readall(ff);
229 // procfile_close(ff);
234 for(i = 0; i < procfile_lines(ff); i++) {
235 if(!procfile_linewords(ff, i)) continue;
237 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
240 if(processors < 1) processors = 1;
242 // procfile_close(ff);
246 long get_system_pid_max(void) {
249 char filename[FILENAME_MAX + 1];
250 snprintf(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
251 ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
254 ff = procfile_readall(ff);
256 // procfile_close(ff);
260 mpid = atol(procfile_lineword(ff, 0, 0));
261 if(!mpid) mpid = 32768;
263 // procfile_close(ff);
267 unsigned long long get_system_hertz(void)
269 unsigned long long myhz = 1;
272 if((myhz = (unsigned long long int) sysconf(_SC_CLK_TCK)) > 0) {
278 myhz = HZ; /* <asm/param.h> */
280 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
281 hz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
284 error("Unknown HZ value. Assuming %llu.", myhz);
289 // ----------------------------------------------------------------------------
291 // target is the structure that process data are aggregated
294 char compare[MAX_COMPARE_NAME + 1];
297 char id[MAX_NAME + 1];
298 char name[MAX_NAME + 1];
303 unsigned long long minflt;
304 unsigned long long cminflt;
305 unsigned long long majflt;
306 unsigned long long cmajflt;
307 unsigned long long utime;
308 unsigned long long stime;
309 unsigned long long cutime;
310 unsigned long long cstime;
311 unsigned long long num_threads;
312 unsigned long long rss;
314 unsigned long long fix_minflt;
315 unsigned long long fix_cminflt;
316 unsigned long long fix_majflt;
317 unsigned long long fix_cmajflt;
318 unsigned long long fix_utime;
319 unsigned long long fix_stime;
320 unsigned long long fix_cutime;
321 unsigned long long fix_cstime;
323 unsigned long long statm_size;
324 unsigned long long statm_resident;
325 unsigned long long statm_share;
326 unsigned long long statm_text;
327 unsigned long long statm_lib;
328 unsigned long long statm_data;
329 unsigned long long statm_dirty;
331 unsigned long long io_logical_bytes_read;
332 unsigned long long io_logical_bytes_written;
333 unsigned long long io_read_calls;
334 unsigned long long io_write_calls;
335 unsigned long long io_storage_bytes_read;
336 unsigned long long io_storage_bytes_written;
337 unsigned long long io_cancelled_write_bytes;
339 unsigned long long fix_io_logical_bytes_read;
340 unsigned long long fix_io_logical_bytes_written;
341 unsigned long long fix_io_read_calls;
342 unsigned long long fix_io_write_calls;
343 unsigned long long fix_io_storage_bytes_read;
344 unsigned long long fix_io_storage_bytes_written;
345 unsigned long long fix_io_cancelled_write_bytes;
348 unsigned long long openfiles;
349 unsigned long long openpipes;
350 unsigned long long opensockets;
351 unsigned long long openinotifies;
352 unsigned long long openeventfds;
353 unsigned long long opentimerfds;
354 unsigned long long opensignalfds;
355 unsigned long long openeventpolls;
356 unsigned long long openother;
358 unsigned long processes; // how many processes have been merged to this
359 int exposed; // if set, we have sent this to netdata
360 int hidden; // if set, we set the hidden flag on the dimension
363 struct target *target; // the one that will be reported to netdata
368 // ----------------------------------------------------------------------------
370 // aggregate all processes in groups, to have a limited number of dimensions
372 struct target *apps_groups_root_target = NULL;
373 struct target *apps_groups_default_target = NULL;
374 long apps_groups_targets = 0;
376 struct target *users_root_target = NULL;
377 struct target *groups_root_target = NULL;
379 struct target *get_users_target(uid_t uid)
382 for(w = users_root_target ; w ; w = w->next)
383 if(w->uid == uid) return w;
385 w = calloc(sizeof(struct target), 1);
387 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
391 snprintf(w->compare, MAX_COMPARE_NAME, "%d", uid);
392 w->hash = simple_hash(w->compare);
394 snprintf(w->id, MAX_NAME, "%d", uid);
396 struct passwd *pw = getpwuid(uid);
398 snprintf(w->name, MAX_NAME, "%d", uid);
400 snprintf(w->name, MAX_NAME, "%s", pw->pw_name);
404 w->next = users_root_target;
405 users_root_target = w;
408 fprintf(stderr, "apps.plugin: added uid %d ('%s') target\n", w->uid, w->name);
413 struct target *get_groups_target(gid_t gid)
416 for(w = groups_root_target ; w ; w = w->next)
417 if(w->gid == gid) return w;
419 w = calloc(sizeof(struct target), 1);
421 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
425 snprintf(w->compare, MAX_COMPARE_NAME, "%d", gid);
426 w->hash = simple_hash(w->compare);
428 snprintf(w->id, MAX_NAME, "%d", gid);
430 struct group *gr = getgrgid(gid);
432 snprintf(w->name, MAX_NAME, "%d", gid);
434 snprintf(w->name, MAX_NAME, "%s", gr->gr_name);
438 w->next = groups_root_target;
439 groups_root_target = w;
442 fprintf(stderr, "apps.plugin: added gid %d ('%s') target\n", w->gid, w->name);
447 // find or create a new target
448 // there are targets that are just aggregated to other target (the second argument)
449 struct target *get_apps_groups_target(const char *id, struct target *target)
451 const char *nid = id;
452 if(nid[0] == '-') nid++;
455 for(w = apps_groups_root_target ; w ; w = w->next)
456 if(strncmp(nid, w->id, MAX_NAME) == 0) return w;
458 w = calloc(sizeof(struct target), 1);
460 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
464 strncpy(w->id, nid, MAX_NAME);
465 strncpy(w->name, nid, MAX_NAME);
466 strncpy(w->compare, nid, MAX_COMPARE_NAME);
467 w->hash = simple_hash(w->compare);
469 if(id[0] == '-') w->hidden = 1;
473 w->next = apps_groups_root_target;
474 apps_groups_root_target = w;
477 fprintf(stderr, "apps.plugin: adding hook for process '%s', compare '%s' on target '%s'\n", w->id, w->compare, w->target?w->target->id:"");
482 // read the apps_groups.conf file
483 int read_apps_groups_conf(const char *name)
486 char filename[FILENAME_MAX + 1];
488 snprintf(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
491 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
493 FILE *fp = fopen(filename, "r");
495 error("Cannot open file '%s'", filename);
500 while(fgets(buffer, 4096, fp) != NULL) {
501 int whidden = 0, wdebug = 0;
504 // if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", buffer);
506 char *s = buffer, *t, *p;
508 if(!s || !*s || *s == '#') continue;
510 if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", s);
515 if(!t || !*t) continue;
537 if(debug) fprintf(stderr, "apps.plugin: \t\ttarget %s\n", t);
539 struct target *w = NULL;
542 char buffer[4097] = "";
546 while((p = strsep(&s, " "))) {
548 if(!p || !*p) continue;
550 strncpy(&buffer[blen], p, 4096 - blen);
551 blen = strlen(buffer);
553 while(buffer[blen - 1] == '\\') {
554 buffer[blen - 1] = ' ';
556 if((p = strsep(&s, " ")))
559 if(!p || !*p) p = " ";
560 strncpy(&buffer[blen], p, 4096 - blen);
561 blen = strlen(buffer);
564 struct target *n = get_apps_groups_target(buffer, w);
575 if(w) strncpy(w->name, t, MAX_NAME);
576 if(!count) error("The line %ld on file '%s', for group '%s' does not state any process names.", line, filename, t);
580 apps_groups_default_target = get_apps_groups_target("+p!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
581 strncpy(apps_groups_default_target->name, "other", MAX_NAME);
587 // ----------------------------------------------------------------------------
588 // data to store for each pid
593 char comm[MAX_COMPARE_NAME + 1];
601 unsigned long long minflt;
602 unsigned long long cminflt;
603 unsigned long long majflt;
604 unsigned long long cmajflt;
605 unsigned long long utime;
606 unsigned long long stime;
607 unsigned long long cutime;
608 unsigned long long cstime;
612 // int64_t itrealvalue;
613 // unsigned long long starttime;
614 // unsigned long long vsize;
615 unsigned long long rss;
616 // unsigned long long rsslim;
617 // unsigned long long starcode;
618 // unsigned long long endcode;
619 // unsigned long long startstack;
620 // unsigned long long kstkesp;
621 // unsigned long long kstkeip;
624 // uint64_t sigignore;
625 // uint64_t sigcatch;
629 // int32_t exit_signal;
630 // int32_t processor;
631 // uint32_t rt_priority;
633 // unsigned long long delayacct_blkio_ticks;
634 // uint64_t guest_time;
635 // int64_t cguest_time;
640 unsigned long long statm_size;
641 unsigned long long statm_resident;
642 unsigned long long statm_share;
643 unsigned long long statm_text;
644 unsigned long long statm_lib;
645 unsigned long long statm_data;
646 unsigned long long statm_dirty;
648 unsigned long long io_logical_bytes_read;
649 unsigned long long io_logical_bytes_written;
650 unsigned long long io_read_calls;
651 unsigned long long io_write_calls;
652 unsigned long long io_storage_bytes_read;
653 unsigned long long io_storage_bytes_written;
654 unsigned long long io_cancelled_write_bytes;
656 // we need the last values
657 // for all incremental counters
658 // so that when a process switches users/groups
659 // we will subtract these values from the old
661 unsigned long long last_minflt;
662 unsigned long long last_cminflt;
663 unsigned long long last_majflt;
664 unsigned long long last_cmajflt;
665 unsigned long long last_utime;
666 unsigned long long last_stime;
667 unsigned long long last_cutime;
668 unsigned long long last_cstime;
670 unsigned long long last_io_logical_bytes_read;
671 unsigned long long last_io_logical_bytes_written;
672 unsigned long long last_io_read_calls;
673 unsigned long long last_io_write_calls;
674 unsigned long long last_io_storage_bytes_read;
675 unsigned long long last_io_storage_bytes_written;
676 unsigned long long last_io_cancelled_write_bytes;
678 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
679 unsigned long long old_utime;
680 unsigned long long old_stime;
681 unsigned long long old_minflt;
682 unsigned long long old_majflt;
684 unsigned long long old_cutime;
685 unsigned long long old_cstime;
686 unsigned long long old_cminflt;
687 unsigned long long old_cmajflt;
689 unsigned long long fix_cutime;
690 unsigned long long fix_cstime;
691 unsigned long long fix_cminflt;
692 unsigned long long fix_cmajflt;
694 unsigned long long diff_cutime;
695 unsigned long long diff_cstime;
696 unsigned long long diff_cminflt;
697 unsigned long long diff_cmajflt;
698 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
700 int *fds; // array of fds it uses
701 int fds_size; // the size of the fds array
703 int children_count; // number of processes directly referencing this
704 int updated; // 1 when update
705 int merged; // 1 when it has been merged to its parent
708 struct target *target; // app_groups.conf targets
709 struct target *user_target; // uid based targets
710 struct target *group_target; // gid based targets
712 struct pid_stat *parent;
713 struct pid_stat *prev;
714 struct pid_stat *next;
716 } *root_of_pids = NULL, **all_pids;
718 long all_pids_count = 0;
720 struct pid_stat *get_pid_entry(pid_t pid)
723 all_pids[pid]->new_entry = 0;
724 return all_pids[pid];
727 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
729 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct pid_stat));
733 all_pids[pid]->fds = calloc(sizeof(int), 100);
734 if(!all_pids[pid]->fds)
735 error("Cannot allocate %ld bytes of memory", (unsigned long)(sizeof(int) * 100));
736 else all_pids[pid]->fds_size = 100;
738 if(root_of_pids) root_of_pids->prev = all_pids[pid];
739 all_pids[pid]->next = root_of_pids;
740 root_of_pids = all_pids[pid];
742 all_pids[pid]->pid = pid;
743 all_pids[pid]->new_entry = 1;
745 return all_pids[pid];
748 void del_pid_entry(pid_t pid)
750 if(!all_pids[pid]) return;
752 if(debug) fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
754 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
755 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
756 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
758 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
760 all_pids[pid] = NULL;
764 // ----------------------------------------------------------------------------
765 // update pids from proc
767 int read_proc_pid_ownership(struct pid_stat *p) {
768 char filename[FILENAME_MAX + 1];
770 snprintf(filename, FILENAME_MAX, "%s/proc/%d", host_prefix, p->pid);
772 // ----------------------------------------
776 if(stat(filename, &st) != 0)
785 int read_proc_pid_stat(struct pid_stat *p) {
786 char filename[FILENAME_MAX + 1];
788 snprintf(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
790 // ----------------------------------------
792 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
795 ff = procfile_readall(ff);
797 // procfile_close(ff);
804 p->comm[MAX_COMPARE_NAME] = '\0';
807 char *s = procfile_lineword(ff, 0, 1);
809 size_t len = strlen(s);
811 while(len && s[len - 1] != ')') {
812 if(blen < MAX_COMPARE_NAME) {
813 strncpy(&p->comm[blen], s, MAX_COMPARE_NAME - blen);
814 blen = strlen(p->comm);
818 s = procfile_lineword(ff, 0, 1+i);
821 if(len && s[len - 1] == ')') s[len - 1] = '\0';
822 if(blen < MAX_COMPARE_NAME)
823 strncpy(&p->comm[blen], s, MAX_COMPARE_NAME - blen);
825 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
827 // p->state = *(procfile_lineword(ff, 0, 2+i));
828 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3 + i));
829 // p->pgrp = atol(procfile_lineword(ff, 0, 4+i));
830 // p->session = atol(procfile_lineword(ff, 0, 5+i));
831 // p->tty_nr = atol(procfile_lineword(ff, 0, 6+i));
832 // p->tpgid = atol(procfile_lineword(ff, 0, 7+i));
833 // p->flags = strtoull(procfile_lineword(ff, 0, 8+i), NULL, 10);
834 p->minflt = strtoull(procfile_lineword(ff, 0, 9+i), NULL, 10);
835 p->cminflt = strtoull(procfile_lineword(ff, 0, 10+i), NULL, 10);
836 p->majflt = strtoull(procfile_lineword(ff, 0, 11+i), NULL, 10);
837 p->cmajflt = strtoull(procfile_lineword(ff, 0, 12+i), NULL, 10);
838 p->utime = strtoull(procfile_lineword(ff, 0, 13+i), NULL, 10);
839 p->stime = strtoull(procfile_lineword(ff, 0, 14+i), NULL, 10);
840 p->cutime = strtoull(procfile_lineword(ff, 0, 15+i), NULL, 10);
841 p->cstime = strtoull(procfile_lineword(ff, 0, 16+i), NULL, 10);
842 // p->priority = strtoull(procfile_lineword(ff, 0, 17+i), NULL, 10);
843 // p->nice = strtoull(procfile_lineword(ff, 0, 18+i), NULL, 10);
844 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19 + i));
845 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20+i), NULL, 10);
846 // p->starttime = strtoull(procfile_lineword(ff, 0, 21+i), NULL, 10);
847 // p->vsize = strtoull(procfile_lineword(ff, 0, 22+i), NULL, 10);
848 p->rss = strtoull(procfile_lineword(ff, 0, 23+i), NULL, 10);
849 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24+i), NULL, 10);
850 // p->starcode = strtoull(procfile_lineword(ff, 0, 25+i), NULL, 10);
851 // p->endcode = strtoull(procfile_lineword(ff, 0, 26+i), NULL, 10);
852 // p->startstack = strtoull(procfile_lineword(ff, 0, 27+i), NULL, 10);
853 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28+i), NULL, 10);
854 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29+i), NULL, 10);
855 // p->signal = strtoull(procfile_lineword(ff, 0, 30+i), NULL, 10);
856 // p->blocked = strtoull(procfile_lineword(ff, 0, 31+i), NULL, 10);
857 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32+i), NULL, 10);
858 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33+i), NULL, 10);
859 // p->wchan = strtoull(procfile_lineword(ff, 0, 34+i), NULL, 10);
860 // p->nswap = strtoull(procfile_lineword(ff, 0, 35+i), NULL, 10);
861 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36+i), NULL, 10);
862 // p->exit_signal = atol(procfile_lineword(ff, 0, 37+i));
863 // p->processor = atol(procfile_lineword(ff, 0, 38+i));
864 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39+i), NULL, 10);
865 // p->policy = strtoul(procfile_lineword(ff, 0, 40+i), NULL, 10);
866 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41+i), NULL, 10);
867 // p->guest_time = strtoull(procfile_lineword(ff, 0, 42+i), NULL, 10);
868 // p->cguest_time = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
870 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: VALUES: %s utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu, threads=%d\n", p->comm, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
872 // procfile_close(ff);
876 int read_proc_pid_statm(struct pid_stat *p) {
877 char filename[FILENAME_MAX + 1];
879 snprintf(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
881 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
884 ff = procfile_readall(ff);
886 // procfile_close(ff);
892 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
893 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
894 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
895 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
896 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
897 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
898 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
900 // procfile_close(ff);
904 int read_proc_pid_io(struct pid_stat *p) {
905 char filename[FILENAME_MAX + 1];
907 snprintf(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
909 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
912 ff = procfile_readall(ff);
914 // procfile_close(ff);
920 p->io_logical_bytes_read = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
921 p->io_logical_bytes_written = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
922 p->io_read_calls = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
923 p->io_write_calls = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
924 p->io_storage_bytes_read = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
925 p->io_storage_bytes_written = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
926 p->io_cancelled_write_bytes = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
928 // procfile_close(ff);
933 // ----------------------------------------------------------------------------
935 // this is used to keep a global list of all open files of the system
936 // it is needed in order to calculate the unique files processes have open
938 #define FILE_DESCRIPTORS_INCREASE_STEP 100
940 struct file_descriptor {
942 #ifdef NETDATA_INTERNAL_CHECKS
944 #endif /* NETDATA_INTERNAL_CHECKS */
952 int all_files_len = 0;
953 int all_files_size = 0;
955 int file_descriptor_compare(void* a, void* b) {
956 #ifdef NETDATA_INTERNAL_CHECKS
957 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
958 error("Corrupted index data detected. Please report this.");
959 #endif /* NETDATA_INTERNAL_CHECKS */
961 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
964 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
968 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
971 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
973 avl_tree all_files_index = {
975 file_descriptor_compare,
976 #ifndef AVL_WITHOUT_PTHREADS
977 #ifdef AVL_LOCK_WITH_MUTEX
978 PTHREAD_MUTEX_INITIALIZER
980 PTHREAD_RWLOCK_INITIALIZER
982 #endif /* AVL_WITHOUT_PTHREADS */
985 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
986 struct file_descriptor *result = NULL, tmp;
987 tmp.hash = (hash)?hash:simple_hash(name);
991 #ifdef NETDATA_INTERNAL_CHECKS
992 tmp.magic = 0x0BADCAFE;
993 #endif /* NETDATA_INTERNAL_CHECKS */
995 avl_search(&all_files_index, (avl *)&tmp, file_descriptor_iterator, (avl **)&result);
999 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1000 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1002 #define FILETYPE_OTHER 0
1003 #define FILETYPE_FILE 1
1004 #define FILETYPE_PIPE 2
1005 #define FILETYPE_SOCKET 3
1006 #define FILETYPE_INOTIFY 4
1007 #define FILETYPE_EVENTFD 5
1008 #define FILETYPE_EVENTPOLL 6
1009 #define FILETYPE_TIMERFD 7
1010 #define FILETYPE_SIGNALFD 8
1012 void file_descriptor_not_used(int id)
1014 if(id > 0 && id < all_files_size) {
1016 #ifdef NETDATA_INTERNAL_CHECKS
1017 if(all_files[id].magic != 0x0BADCAFE) {
1018 error("Ignoring request to remove empty file id %d.", id);
1021 #endif /* NETDATA_INTERNAL_CHECKS */
1023 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1025 if(all_files[id].count > 0) {
1026 all_files[id].count--;
1028 if(!all_files[id].count) {
1029 if(debug) fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
1030 file_descriptor_remove(&all_files[id]);
1031 #ifdef NETDATA_INTERNAL_CHECKS
1032 all_files[id].magic = 0x00000000;
1033 #endif /* NETDATA_INTERNAL_CHECKS */
1038 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1040 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1043 int file_descriptor_find_or_add(const char *name)
1045 static int last_pos = 0;
1046 uint32_t hash = simple_hash(name);
1048 if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1050 struct file_descriptor *fd = file_descriptor_find(name, hash);
1053 if(debug) fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
1059 // check we have enough memory to add it
1060 if(!all_files || all_files_len == all_files_size) {
1061 void *old = all_files;
1064 // there is no empty slot
1065 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1066 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1068 // if the address changed, we have to rebuild the index
1069 // since all pointers are now invalid
1070 if(old && old != (void *)all_files) {
1071 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing.\n");
1072 all_files_index.root = NULL;
1073 for(i = 0; i < all_files_size; i++) {
1074 if(!all_files[i].count) continue;
1075 file_descriptor_add(&all_files[i]);
1077 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
1080 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1081 all_files[i].count = 0;
1082 all_files[i].name = NULL;
1083 #ifdef NETDATA_INTERNAL_CHECKS
1084 all_files[i].magic = 0x00000000;
1085 #endif /* NETDATA_INTERNAL_CHECKS */
1086 all_files[i].pos = i;
1089 if(!all_files_size) all_files_len = 1;
1090 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1093 if(debug) fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
1095 // search for an empty slot
1097 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1098 if(c >= all_files_size) c = 0;
1099 if(c == 0) continue;
1101 if(!all_files[c].count) {
1102 if(debug) fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
1104 #ifdef NETDATA_INTERNAL_CHECKS
1105 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1106 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1107 #endif /* NETDATA_INTERNAL_CHECKS */
1109 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);
1110 if(all_files[c].name) free((void *)all_files[c].name);
1111 all_files[c].name = NULL;
1116 if(i == all_files_size) {
1117 fatal("We should find an empty slot, but there isn't any");
1120 if(debug) fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1124 // else we have an empty slot in 'c'
1127 if(name[0] == '/') type = FILETYPE_FILE;
1128 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1129 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1130 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1131 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1132 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1133 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1134 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1135 else if(strncmp(name, "anon_inode:", 11) == 0) {
1136 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1137 type = FILETYPE_OTHER;
1140 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1141 type = FILETYPE_OTHER;
1144 all_files[c].name = strdup(name);
1145 all_files[c].hash = hash;
1146 all_files[c].type = type;
1147 all_files[c].pos = c;
1148 all_files[c].count = 1;
1149 #ifdef NETDATA_INTERNAL_CHECKS
1150 all_files[c].magic = 0x0BADCAFE;
1151 #endif /* NETDATA_INTERNAL_CHECKS */
1152 file_descriptor_add(&all_files[c]);
1154 if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1159 int read_pid_file_descriptors(struct pid_stat *p) {
1160 char dirname[FILENAME_MAX+1];
1162 snprintf(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
1163 DIR *fds = opendir(dirname);
1167 char fdname[FILENAME_MAX + 1];
1168 char linkname[FILENAME_MAX + 1];
1170 // make the array negative
1171 for(c = 0 ; c < p->fds_size ; c++)
1172 p->fds[c] = -p->fds[c];
1174 while((de = readdir(fds))) {
1175 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1178 // check if the fds array is small
1179 int fdid = atoi(de->d_name);
1180 if(fdid < 0) continue;
1181 if(fdid >= p->fds_size) {
1182 // it is small, extend it
1183 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1184 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1186 error("Cannot re-allocate fds for %s", p->comm);
1190 // and initialize it
1191 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1192 p->fds_size = fdid + 100;
1195 if(p->fds[fdid] == 0) {
1196 // we don't know this fd, get it
1198 sprintf(fdname, "%s/proc/%d/fd/%s", host_prefix, p->pid, de->d_name);
1199 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1201 if(debug || (p->target && p->target->debug)) {
1202 if(debug || (p->target && p->target->debug))
1203 error("Cannot read link %s", fdname);
1210 // if another process already has this, we will get
1212 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1215 // else make it positive again, we need it
1216 // of course, the actual file may have changed, but we don't care so much
1217 // FIXME: we could compare the inode as returned by readdir direct structure
1218 else p->fds[fdid] = -p->fds[fdid];
1222 // remove all the negative file descriptors
1223 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1224 file_descriptor_not_used(-p->fds[c]);
1233 // ----------------------------------------------------------------------------
1235 // 1. read all files in /proc
1236 // 2. for each numeric directory:
1237 // i. read /proc/pid/stat
1238 // ii. read /proc/pid/statm
1239 // iii. read /proc/pid/io (requires root access)
1240 // iii. read the entries in directory /proc/pid/fd (requires root access)
1242 // a. find or create a struct file_descriptor
1243 // b. cleanup any old/unused file_descriptors
1245 // after all these, some pids may be linked to targets, while others may not
1247 // in case of errors, only 1 every 1000 errors is printed
1248 // to avoid filling up all disk space
1249 // if debug is enabled, all errors are printed
1251 int collect_data_for_all_processes_from_proc(void)
1253 static long count_errors = 0;
1255 char dirname[FILENAME_MAX + 1];
1257 snprintf(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1258 DIR *dir = opendir(dirname);
1261 struct dirent *file = NULL;
1262 struct pid_stat *p = NULL;
1264 // mark them all as un-updated
1266 for(p = root_of_pids; p ; p = p->next) {
1270 p->children_count = 0;
1274 p->last_minflt = p->minflt;
1275 p->last_cminflt = p->cminflt;
1276 p->last_majflt = p->majflt;
1277 p->last_cmajflt = p->cmajflt;
1278 p->last_utime = p->utime;
1279 p->last_stime = p->stime;
1280 p->last_cutime = p->cutime;
1281 p->last_cstime = p->cstime;
1283 p->last_io_logical_bytes_read = p->io_logical_bytes_read;
1284 p->last_io_logical_bytes_written = p->io_logical_bytes_written;
1285 p->last_io_read_calls = p->io_read_calls;
1286 p->last_io_write_calls = p->io_write_calls;
1287 p->last_io_storage_bytes_read = p->io_storage_bytes_read;
1288 p->last_io_storage_bytes_written = p->io_storage_bytes_written;
1289 p->last_io_cancelled_write_bytes = p->io_cancelled_write_bytes;
1292 while((file = readdir(dir))) {
1293 char *endptr = file->d_name;
1294 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1296 // make sure we read a valid number
1297 if(unlikely(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0'))
1300 p = get_pid_entry(pid);
1301 if(unlikely(!p)) continue;
1304 // --------------------------------------------------------------------
1307 if(unlikely(read_proc_pid_stat(p))) {
1308 if(!count_errors++ || debug || (p->target && p->target->debug))
1309 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1311 // there is no reason to proceed if we cannot get its status
1315 // check its parent pid
1316 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1317 if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1318 error("Pid %d states invalid parent pid %d. Using 0.", pid, p->ppid);
1324 // --------------------------------------------------------------------
1325 // /proc/<pid>/statm
1327 if(unlikely(read_proc_pid_statm(p))) {
1328 if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1329 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1331 // there is no reason to proceed if we cannot get its memory status
1336 // --------------------------------------------------------------------
1339 if(unlikely(read_proc_pid_io(p))) {
1340 if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1341 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1343 // on systems without /proc/X/io
1344 // allow proceeding without I/O information
1348 // --------------------------------------------------------------------
1351 if(unlikely(read_proc_pid_ownership(p))) {
1352 if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1353 error("Cannot stat %s/proc/%d", host_prefix, pid);
1356 // --------------------------------------------------------------------
1359 // check if it is target
1360 // we do this only once, the first time this pid is loaded
1361 if(unlikely(p->new_entry)) {
1362 if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
1363 uint32_t hash = simple_hash(p->comm);
1366 for(w = apps_groups_root_target; w ; w = w->next) {
1367 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1369 if(w->hash == hash && strcmp(w->compare, p->comm) == 0) {
1370 if(w->target) p->target = w->target;
1373 if(debug || (p->target && p->target->debug))
1374 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1379 // --------------------------------------------------------------------
1382 if(unlikely(read_pid_file_descriptors(p))) {
1383 if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1384 error("Cannot process entries in %s/proc/%d/fd", host_prefix, pid);
1387 // --------------------------------------------------------------------
1390 // mark it as updated
1394 if(unlikely(count_errors > 1000)) {
1395 error("%ld more errors encountered\n", count_errors - 1);
1405 // ----------------------------------------------------------------------------
1407 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1408 // print a tree view of all processes
1409 int debug_childrens_aggregations(pid_t pid, int level) {
1410 struct pid_stat *p = NULL;
1414 for(i = 0; i < level; i++) b[i] = '\t';
1419 for(p = root_of_pids; p ; p = p->next) {
1420 if(p->ppid == pid) {
1421 ret += debug_childrens_aggregations(p->pid, level+1);
1427 if(!p->updated) ret += 1;
1428 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"
1429 , b, p->comm, p->pid, p->updated?"OK":"KILLED", p->target->name, p->children_count
1430 , p->utime, p->utime - p->old_utime
1431 , p->stime, p->stime - p->old_stime
1432 , p->cutime, p->cutime - p->old_cutime
1433 , p->cstime, p->cstime - p->old_cstime
1434 , p->minflt, p->minflt - p->old_minflt
1435 , p->majflt, p->majflt - p->old_majflt
1436 , p->cminflt, p->cminflt - p->old_cminflt
1437 , p->cmajflt, p->cmajflt - p->old_cmajflt
1443 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1447 // ----------------------------------------------------------------------------
1448 // update statistics on the targets
1450 // 1. link all childs to their parents
1451 // 2. go from bottom to top, marking as merged all childs to their parents
1452 // this step links all parents without a target to the child target, if any
1453 // 3. link all top level processes (the ones not merged) to the default target
1454 // 4. go from top to bottom, linking all childs without a target, to their parent target
1455 // after this step, all processes have a target
1456 // [5. for each killed pid (updated = 0), remove its usage from its target]
1457 // 6. zero all apps_groups_targets
1458 // 7. concentrate all values on the apps_groups_targets
1459 // 8. remove all killed processes
1460 // 9. find the unique file count for each target
1461 // check: update_apps_groups_statistics()
1463 void link_all_processes_to_their_parents(void) {
1464 struct pid_stat *p = NULL;
1466 // link all children to their parents
1467 // and update children count on parents
1468 for(p = root_of_pids; p ; p = p->next) {
1469 // for each process found running
1472 && p->ppid <= pid_max
1473 && all_pids[p->ppid]
1475 // for valid processes
1477 if(debug || (p->target && p->target->debug))
1478 fprintf(stderr, "apps.plugin: \tparent of %d (%s) is %d (%s)\n", p->pid, p->comm, p->ppid, all_pids[p->ppid]->comm);
1480 p->parent = all_pids[p->ppid];
1481 p->parent->children_count++;
1483 else if(p->ppid != 0)
1484 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1488 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1489 void aggregate_children_to_parents(void) {
1490 struct pid_stat *p = NULL;
1492 // for each killed process, remove its values from the parents
1493 // sums (we had already added them in a previous loop)
1494 for(p = root_of_pids; p ; p = p->next) {
1495 if(p->updated) continue;
1497 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1499 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1500 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1501 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1502 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1504 struct pid_stat *t = p;
1505 while((t = t->parent)) {
1506 if(!t->updated) continue;
1508 unsigned long long x;
1509 if(diff_utime && t->diff_cutime) {
1510 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1512 t->diff_cutime -= x;
1514 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1516 if(diff_stime && t->diff_cstime) {
1517 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1519 t->diff_cstime -= x;
1521 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1523 if(diff_minflt && t->diff_cminflt) {
1524 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1526 t->diff_cminflt -= x;
1527 t->fix_cminflt += x;
1528 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1530 if(diff_majflt && t->diff_cmajflt) {
1531 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1533 t->diff_cmajflt -= x;
1534 t->fix_cmajflt += x;
1535 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1539 if(diff_utime) error("Cannot fix up utime %llu", diff_utime);
1540 if(diff_stime) error("Cannot fix up stime %llu", diff_stime);
1541 if(diff_minflt) error("Cannot fix up minflt %llu", diff_minflt);
1542 if(diff_majflt) error("Cannot fix up majflt %llu", diff_majflt);
1545 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1547 void cleanup_non_existing_pids(void) {
1549 struct pid_stat *p = NULL;
1551 for(p = root_of_pids; p ;) {
1553 // 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);
1555 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1556 file_descriptor_not_used(p->fds[c]);
1568 void apply_apps_groups_targets_inheritance(void) {
1569 struct pid_stat *p = NULL;
1571 // children that do not have a target
1572 // inherit their target from their parent
1576 for(p = root_of_pids; p ; p = p->next) {
1577 // if this process does not have a target
1578 // and it has a parent
1579 // and its parent has a target
1580 // then, set the parent's target to this process
1581 if(unlikely(!p->target && p->parent && p->parent->target)) {
1582 p->target = p->parent->target;
1585 if(debug || (p->target && p->target->debug))
1586 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);
1592 // find all the procs with 0 childs and merge them to their parents
1593 // repeat, until nothing more can be done.
1597 for(p = root_of_pids; p ; p = p->next) {
1598 // if this process does not have any children
1599 // and is not already merged
1601 // and its parent has children
1602 // and the target of this process and its parent is the same, or the parent does not have a target
1603 // and its parent is not init
1604 // then, mark them as merged.
1609 && p->parent->children_count
1610 && (p->target == p->parent->target || !p->parent->target)
1613 p->parent->children_count--;
1616 // the parent inherits the child's target, if it does not have a target itself
1617 if(unlikely(p->target && !p->parent->target)) {
1618 p->parent->target = p->target;
1620 if(debug || (p->target && p->target->debug))
1621 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);
1629 fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1632 // init goes always to default target
1634 all_pids[1]->target = apps_groups_default_target;
1636 // give a default target on all top level processes
1637 for(p = root_of_pids; p ; p = p->next) {
1638 // if the process is not merged itself
1639 // then is is a top level process
1640 if(!p->merged && !p->target)
1641 p->target = apps_groups_default_target;
1643 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1644 // by the way, update the diffs
1645 // will be used later for subtracting killed process times
1646 p->diff_cutime = p->utime - p->cutime;
1647 p->diff_cstime = p->stime - p->cstime;
1648 p->diff_cminflt = p->minflt - p->cminflt;
1649 p->diff_cmajflt = p->majflt - p->cmajflt;
1650 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1653 // give a target to all merged child processes
1657 for(p = root_of_pids; p ; p = p->next) {
1658 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1659 p->target = p->parent->target;
1662 if(debug || (p->target && p->target->debug))
1663 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);
1669 long zero_all_targets(struct target *root) {
1673 for (w = root; w ; w = w->next) {
1676 if(w->fds) free(w->fds);
1692 w->statm_resident = 0;
1699 w->io_logical_bytes_read = 0;
1700 w->io_logical_bytes_written = 0;
1701 w->io_read_calls = 0;
1702 w->io_write_calls = 0;
1703 w->io_storage_bytes_read = 0;
1704 w->io_storage_bytes_written = 0;
1705 w->io_cancelled_write_bytes = 0;
1711 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1712 if(unlikely(!w->fds)) {
1713 w->fds = calloc(sizeof(int), (size_t) all_files_size);
1714 if(unlikely(!w->fds))
1715 error("Cannot allocate memory for fds in %s", w->name);
1718 if(likely(p->updated)) {
1719 w->cutime += p->cutime; // - p->fix_cutime;
1720 w->cstime += p->cstime; // - p->fix_cstime;
1721 w->cminflt += p->cminflt; // - p->fix_cminflt;
1722 w->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1724 w->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1725 w->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1726 w->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1727 w->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1729 //if(p->num_threads < 0)
1730 // error("Negative threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1732 //if(p->num_threads > 10000)
1733 // error("Excessive threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1735 w->num_threads += p->num_threads;
1738 w->statm_size += p->statm_size;
1739 w->statm_resident += p->statm_resident;
1740 w->statm_share += p->statm_share;
1741 w->statm_text += p->statm_text;
1742 w->statm_lib += p->statm_lib;
1743 w->statm_data += p->statm_data;
1744 w->statm_dirty += p->statm_dirty;
1746 w->io_logical_bytes_read += p->io_logical_bytes_read;
1747 w->io_logical_bytes_written += p->io_logical_bytes_written;
1748 w->io_read_calls += p->io_read_calls;
1749 w->io_write_calls += p->io_write_calls;
1750 w->io_storage_bytes_read += p->io_storage_bytes_read;
1751 w->io_storage_bytes_written += p->io_storage_bytes_written;
1752 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1756 if(likely(w->fds)) {
1758 for(c = 0; c < p->fds_size ;c++) {
1759 if(p->fds[c] == 0) continue;
1761 if(likely(p->fds[c] < all_files_size)) {
1762 if(w->fds) w->fds[p->fds[c]]++;
1765 error("Invalid fd number %d", p->fds[c]);
1769 if(unlikely(debug || w->debug))
1770 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);
1772 /* 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);
1773 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);
1774 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);
1775 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);
1776 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);
1777 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);
1778 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);
1779 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);
1781 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1782 p->old_utime = p->utime;
1783 p->old_cutime = p->cutime;
1784 p->old_stime = p->stime;
1785 p->old_cstime = p->cstime;
1786 p->old_minflt = p->minflt;
1787 p->old_majflt = p->majflt;
1788 p->old_cminflt = p->cminflt;
1789 p->old_cmajflt = p->cmajflt;
1790 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1793 // since the process switched target
1794 // for all incremental values
1795 // we have to subtract its OLD values from the new target
1796 // and add its OLD values to the old target
1799 // We add/subtract the last/OLD values we added to the target
1801 w->fix_cutime -= p->last_cutime;
1802 w->fix_cstime -= p->last_cstime;
1803 w->fix_cminflt -= p->last_cminflt;
1804 w->fix_cmajflt -= p->last_cmajflt;
1806 w->fix_utime -= p->last_utime;
1807 w->fix_stime -= p->last_stime;
1808 w->fix_minflt -= p->last_minflt;
1809 w->fix_majflt -= p->last_majflt;
1812 w->fix_io_logical_bytes_read -= p->last_io_logical_bytes_read;
1813 w->fix_io_logical_bytes_written -= p->last_io_logical_bytes_written;
1814 w->fix_io_read_calls -= p->last_io_read_calls;
1815 w->fix_io_write_calls -= p->last_io_write_calls;
1816 w->fix_io_storage_bytes_read -= p->last_io_storage_bytes_read;
1817 w->fix_io_storage_bytes_written -= p->last_io_storage_bytes_written;
1818 w->fix_io_cancelled_write_bytes -= p->last_io_cancelled_write_bytes;
1822 o->fix_cutime += p->last_cutime;
1823 o->fix_cstime += p->last_cstime;
1824 o->fix_cminflt += p->last_cminflt;
1825 o->fix_cmajflt += p->last_cmajflt;
1827 o->fix_utime += p->last_utime;
1828 o->fix_stime += p->last_stime;
1829 o->fix_minflt += p->last_minflt;
1830 o->fix_majflt += p->last_majflt;
1832 o->fix_io_logical_bytes_read += p->last_io_logical_bytes_read;
1833 o->fix_io_logical_bytes_written += p->last_io_logical_bytes_written;
1834 o->fix_io_read_calls += p->last_io_read_calls;
1835 o->fix_io_write_calls += p->last_io_write_calls;
1836 o->fix_io_storage_bytes_read += p->last_io_storage_bytes_read;
1837 o->fix_io_storage_bytes_written += p->last_io_storage_bytes_written;
1838 o->fix_io_cancelled_write_bytes += p->last_io_cancelled_write_bytes;
1842 // 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);
1844 // since the process has exited, the user
1845 // will see a drop in our charts, because the incremental
1846 // values of this process will not be there
1848 // add them to the fix_* values and they will be added to
1849 // the reported values, so that the report goes steady
1850 w->fix_minflt += p->minflt;
1851 w->fix_majflt += p->majflt;
1852 w->fix_utime += p->utime;
1853 w->fix_stime += p->stime;
1854 w->fix_cminflt += p->cminflt;
1855 w->fix_cmajflt += p->cmajflt;
1856 w->fix_cutime += p->cutime;
1857 w->fix_cstime += p->cstime;
1859 w->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1860 w->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1861 w->fix_io_read_calls += p->io_read_calls;
1862 w->fix_io_write_calls += p->io_write_calls;
1863 w->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1864 w->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1865 w->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1870 void count_targets_fds(struct target *root) {
1874 for (w = root; w ; w = w->next) {
1875 if(!w->fds) continue;
1880 w->openinotifies = 0;
1881 w->openeventfds = 0;
1882 w->opentimerfds = 0;
1883 w->opensignalfds = 0;
1884 w->openeventpolls = 0;
1887 for(c = 1; c < all_files_size ;c++) {
1889 switch(all_files[c].type) {
1898 case FILETYPE_SOCKET:
1902 case FILETYPE_INOTIFY:
1906 case FILETYPE_EVENTFD:
1910 case FILETYPE_TIMERFD:
1914 case FILETYPE_SIGNALFD:
1918 case FILETYPE_EVENTPOLL:
1919 w->openeventpolls++;
1932 void calculate_netdata_statistics(void)
1934 link_all_processes_to_their_parents();
1935 apply_apps_groups_targets_inheritance();
1937 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1938 aggregate_children_to_parents();
1939 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1941 zero_all_targets(users_root_target);
1942 zero_all_targets(groups_root_target);
1943 apps_groups_targets = zero_all_targets(apps_groups_root_target);
1945 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1947 debug_childrens_aggregations(0, 1);
1948 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1950 // this has to be done, before the cleanup
1951 struct pid_stat *p = NULL;
1952 struct target *w = NULL, *o = NULL;
1954 // concentrate everything on the apps_groups_targets
1955 for(p = root_of_pids; p ; p = p->next) {
1957 // --------------------------------------------------------------------
1958 // apps_groups targets
1959 if(likely(p->target))
1960 aggregate_pid_on_target(p->target, p, NULL);
1962 error("pid %d %s was left without a target!", p->pid, p->comm);
1965 // --------------------------------------------------------------------
1968 if(likely(p->user_target && p->user_target->uid == p->uid))
1971 if(unlikely(debug && p->user_target))
1972 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);
1974 w = p->user_target = get_users_target(p->uid);
1978 aggregate_pid_on_target(w, p, o);
1980 error("pid %d %s was left without a user target!", p->pid, p->comm);
1983 // --------------------------------------------------------------------
1985 o = p->group_target;
1986 if(likely(p->group_target && p->group_target->gid == p->gid))
1987 w = p->group_target;
1989 if(unlikely(debug && p->group_target))
1990 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);
1992 w = p->group_target = get_groups_target(p->gid);
1996 aggregate_pid_on_target(w, p, o);
1998 error("pid %d %s was left without a group target!", p->pid, p->comm);
2002 count_targets_fds(apps_groups_root_target);
2003 count_targets_fds(users_root_target);
2004 count_targets_fds(groups_root_target);
2006 cleanup_non_existing_pids();
2009 // ----------------------------------------------------------------------------
2010 // update chart dimensions
2012 unsigned long long send_resource_usage_to_netdata() {
2013 static struct timeval last = { 0, 0 };
2014 static struct rusage me_last;
2019 unsigned long long usec;
2020 unsigned long long cpuuser;
2021 unsigned long long cpusyst;
2024 gettimeofday(&last, NULL);
2025 getrusage(RUSAGE_SELF, &me_last);
2027 // the first time, give a zero to allow
2028 // netdata calibrate to the current time
2029 // usec = update_every * 1000000ULL;
2035 gettimeofday(&now, NULL);
2036 getrusage(RUSAGE_SELF, &me);
2038 usec = usecdiff(&now, &last);
2039 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2040 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2042 bcopy(&now, &last, sizeof(struct timeval));
2043 bcopy(&me, &me_last, sizeof(struct rusage));
2046 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
2047 fprintf(stdout, "SET user = %llu\n", cpuuser);
2048 fprintf(stdout, "SET system = %llu\n", cpusyst);
2049 fprintf(stdout, "END\n");
2051 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
2052 fprintf(stdout, "SET files = %llu\n", file_counter);
2053 fprintf(stdout, "SET pids = %ld\n", all_pids_count);
2054 fprintf(stdout, "SET fds = %d\n", all_files_len);
2055 fprintf(stdout, "SET targets = %ld\n", apps_groups_targets);
2056 fprintf(stdout, "END\n");
2061 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec)
2065 fprintf(stdout, "BEGIN %s.cpu %llu\n", type, usec);
2066 for (w = root; w ; w = w->next) {
2067 if(w->target || (!w->processes && !w->exposed)) continue;
2069 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
2071 fprintf(stdout, "END\n");
2073 fprintf(stdout, "BEGIN %s.cpu_user %llu\n", type, usec);
2074 for (w = root; w ; w = w->next) {
2075 if(w->target || (!w->processes && !w->exposed)) continue;
2077 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
2079 fprintf(stdout, "END\n");
2081 fprintf(stdout, "BEGIN %s.cpu_system %llu\n", type, usec);
2082 for (w = root; w ; w = w->next) {
2083 if(w->target || (!w->processes && !w->exposed)) continue;
2085 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
2087 fprintf(stdout, "END\n");
2089 fprintf(stdout, "BEGIN %s.threads %llu\n", type, usec);
2090 for (w = root; w ; w = w->next) {
2091 if(w->target || (!w->processes && !w->exposed)) continue;
2093 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
2095 fprintf(stdout, "END\n");
2097 fprintf(stdout, "BEGIN %s.processes %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 = %lu\n", w->name, w->processes);
2103 fprintf(stdout, "END\n");
2105 fprintf(stdout, "BEGIN %s.mem %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 = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
2111 fprintf(stdout, "END\n");
2113 fprintf(stdout, "BEGIN %s.minor_faults %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->minflt + w->fix_minflt);
2119 fprintf(stdout, "END\n");
2121 fprintf(stdout, "BEGIN %s.major_faults %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->majflt + w->fix_majflt);
2127 fprintf(stdout, "END\n");
2129 fprintf(stdout, "BEGIN %s.lreads %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 = %llu\n", w->name, w->io_logical_bytes_read + w->fix_io_logical_bytes_read);
2135 fprintf(stdout, "END\n");
2137 fprintf(stdout, "BEGIN %s.lwrites %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 = %llu\n", w->name, w->io_logical_bytes_written + w->fix_io_logical_bytes_written);
2143 fprintf(stdout, "END\n");
2145 fprintf(stdout, "BEGIN %s.preads %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->io_storage_bytes_read + w->fix_io_storage_bytes_read);
2151 fprintf(stdout, "END\n");
2153 fprintf(stdout, "BEGIN %s.pwrites %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->io_storage_bytes_written + w->fix_io_storage_bytes_written);
2159 fprintf(stdout, "END\n");
2161 fprintf(stdout, "BEGIN %s.files %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->openfiles);
2167 fprintf(stdout, "END\n");
2169 fprintf(stdout, "BEGIN %s.sockets %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->opensockets);
2175 fprintf(stdout, "END\n");
2177 fprintf(stdout, "BEGIN %s.pipes %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->openpipes);
2183 fprintf(stdout, "END\n");
2189 // ----------------------------------------------------------------------------
2190 // generate the charts
2192 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2195 int newly_added = 0;
2197 for(w = root ; w ; w = w->next)
2198 if(!w->exposed && w->processes) {
2201 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2204 // nothing more to show
2205 if(!newly_added) return;
2207 // we have something new to show
2208 // update the charts
2209 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);
2210 for (w = root; w ; w = w->next) {
2211 if(w->target || (!w->processes && !w->exposed)) continue;
2213 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu %s\n", w->name, Hertz, w->hidden ? "hidden,noreset" : "noreset");
2216 fprintf(stdout, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2217 for (w = root; w ; w = w->next) {
2218 if(w->target || (!w->processes && !w->exposed)) continue;
2220 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld noreset\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2223 fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2224 for (w = root; w ; w = w->next) {
2225 if(w->target || (!w->processes && !w->exposed)) continue;
2227 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2230 fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2231 for (w = root; w ; w = w->next) {
2232 if(w->target || (!w->processes && !w->exposed)) continue;
2234 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2237 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);
2238 for (w = root; w ; w = w->next) {
2239 if(w->target || (!w->processes && !w->exposed)) continue;
2241 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
2244 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);
2245 for (w = root; w ; w = w->next) {
2246 if(w->target || (!w->processes && !w->exposed)) continue;
2248 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
2251 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);
2252 for (w = root; w ; w = w->next) {
2253 if(w->target || (!w->processes && !w->exposed)) continue;
2255 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
2258 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);
2259 for (w = root; w ; w = w->next) {
2260 if(w->target || (!w->processes && !w->exposed)) continue;
2262 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
2265 fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2266 for (w = root; w ; w = w->next) {
2267 if(w->target || (!w->processes && !w->exposed)) continue;
2269 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2272 fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2273 for (w = root; w ; w = w->next) {
2274 if(w->target || (!w->processes && !w->exposed)) continue;
2276 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2279 fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2280 for (w = root; w ; w = w->next) {
2281 if(w->target || (!w->processes && !w->exposed)) continue;
2283 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2286 fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2287 for (w = root; w ; w = w->next) {
2288 if(w->target || (!w->processes && !w->exposed)) continue;
2290 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2293 fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2294 for (w = root; w ; w = w->next) {
2295 if(w->target || (!w->processes && !w->exposed)) continue;
2297 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2300 fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2301 for (w = root; w ; w = w->next) {
2302 if(w->target || (!w->processes && !w->exposed)) continue;
2304 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2307 fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2308 for (w = root; w ; w = w->next) {
2309 if(w->target || (!w->processes && !w->exposed)) continue;
2311 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2316 // ----------------------------------------------------------------------------
2317 // parse command line arguments
2319 void parse_args(int argc, char **argv)
2324 for(i = 1; i < argc; i++) {
2326 int n = atoi(argv[i]);
2333 if(strcmp("debug", argv[i]) == 0) {
2335 debug_flags = 0xffffffff;
2344 error("Cannot understand option %s", argv[i]);
2348 if(freq > 0) update_every = freq;
2349 if(!name) name = "groups";
2351 if(read_apps_groups_conf(name)) {
2352 error("Cannot read process groups %s", name);
2357 unsigned long long sutime() {
2359 gettimeofday(&now, NULL);
2360 return now.tv_sec * 1000000ULL + now.tv_usec;
2363 int main(int argc, char **argv)
2365 // debug_flags = D_PROCFILE;
2367 // set the name for logging
2368 program_name = "apps.plugin";
2370 host_prefix = getenv("NETDATA_HOST_PREFIX");
2371 if(host_prefix == NULL) {
2372 info("NETDATA_HOST_PREFIX is not passed from netdata");
2375 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2377 config_dir = getenv("NETDATA_CONFIG_DIR");
2378 if(config_dir == NULL) {
2379 info("NETDATA_CONFIG_DIR is not passed from netdata");
2380 config_dir = CONFIG_DIR;
2382 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2384 info("starting...");
2386 procfile_adaptive_initial_allocation = 1;
2388 time_t started_t = time(NULL);
2390 Hertz = get_system_hertz();
2391 pid_max = get_system_pid_max();
2392 processors = get_system_cpus();
2394 parse_args(argc, argv);
2396 all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2398 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2399 printf("DISABLE\n");
2403 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %d\n", update_every);
2404 fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000);
2405 fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000);
2407 fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %d\n", update_every);
2408 fprintf(stdout, "DIMENSION files '' incremental 1 1\n");
2409 fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
2410 fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
2411 fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
2414 #ifndef PROFILING_MODE
2415 unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2416 unsigned long long sunow;
2417 #endif /* PROFILING_MODE */
2419 unsigned long long counter = 1;
2420 for(;1; counter++) {
2421 #ifndef PROFILING_MODE
2422 // delay until it is our time to run
2423 while((sunow = sutime()) < sunext)
2424 usleep((useconds_t)(sunext - sunow));
2426 // find the next time we need to run
2427 while(sutime() > sunext)
2428 sunext += update_every * 1000000ULL;
2429 #endif /* PROFILING_MODE */
2431 if(!collect_data_for_all_processes_from_proc()) {
2432 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2433 printf("DISABLE\n");
2437 calculate_netdata_statistics();
2439 unsigned long long dt = send_resource_usage_to_netdata();
2441 // this is smart enough to show only newly added apps, when needed
2442 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2443 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2444 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2446 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2447 send_collected_data_to_netdata(users_root_target, "users", dt);
2448 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2450 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
2453 current_t = time(NULL);
2455 #ifndef PROFILING_MODE
2456 // restart check (14400 seconds)
2457 if(current_t - started_t > 14400) exit(0);
2459 if(current_t - started_t > 10) exit(0);
2460 #endif /* PROFILING_MODE */