3 // 1. disable RESET_OR_OVERFLOW check in charts
13 #include <sys/types.h>
17 #include <sys/resource.h>
28 #include <arpa/inet.h>
35 #define MAX_COMPARE_NAME 15
38 unsigned long long Hertz = 1;
45 unsigned long long file_counter = 0;
47 char *host_prefix = "";
48 char *config_dir = CONFIG_DIR;
50 #ifdef NETDATA_INTERNAL_CHECKS
51 // ----------------------------------------------------------------------------
58 } allocations = { 0, 0, 0 };
60 #define MALLOC_MARK (uint32_t)(0x0BADCAFE)
61 #define MALLOC_PREFIX (sizeof(uint32_t) * 2)
62 #define MALLOC_SUFFIX (sizeof(uint32_t))
63 #define MALLOC_OVERHEAD (MALLOC_PREFIX + MALLOC_SUFFIX)
65 void *mark_allocation(void *allocated_ptr, size_t size_without_overheads) {
66 uint32_t *real_ptr = (uint32_t *)allocated_ptr;
67 real_ptr[0] = MALLOC_MARK;
68 real_ptr[1] = (uint32_t) size_without_overheads;
70 uint32_t *end_ptr = (uint32_t *)(allocated_ptr + MALLOC_PREFIX + size_without_overheads);
71 end_ptr[0] = MALLOC_MARK;
73 // fprintf(stderr, "MEMORY_POINTER: Allocated at %p, returning %p.\n", allocated_ptr, (void *)(allocated_ptr + MALLOC_PREFIX));
75 return allocated_ptr + MALLOC_PREFIX;
78 void *check_allocation(const char *file, int line, const char *function, void *marked_ptr, size_t *size_without_overheads_ptr) {
79 uint32_t *real_ptr = (uint32_t *)(marked_ptr - MALLOC_PREFIX);
81 // 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);
83 if(real_ptr[0] != MALLOC_MARK) fatal("MEMORY: prefix MARK is not valid for %s/%u@%s.", function, line, file);
85 size_t size = real_ptr[1];
87 uint32_t *end_ptr = (uint32_t *)(marked_ptr + size);
88 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);
90 if(size_without_overheads_ptr) *size_without_overheads_ptr = size;
95 void *malloc_debug(const char *file, int line, const char *function, size_t size) {
96 void *ptr = malloc(size + MALLOC_OVERHEAD);
97 if(!ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
99 allocations.allocated += size;
100 allocations.allocations++;
102 debug(D_MEMORY, "MEMORY: Allocated %zu bytes for %s/%u@%s."
103 " Status: allocated %zu in %zu allocs."
105 , function, line, file
106 , allocations.allocated
107 , allocations.allocations
110 if(allocations.allocated > allocations.allocated_max) {
111 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
112 allocations.allocated_max = allocations.allocated;
116 check_allocation(file, line, function, mark_allocation(ptr, size), &csize);
118 fatal("Invalid size.");
121 return mark_allocation(ptr, size);
124 void *calloc_debug(const char *file, int line, const char *function, size_t nmemb, size_t size) {
125 void *ptr = malloc_debug(file, line, function, (nmemb * size));
126 bzero(ptr, nmemb * size);
130 void free_debug(const char *file, int line, const char *function, void *ptr) {
132 void *real_ptr = check_allocation(file, line, function, ptr, &size);
134 bzero(real_ptr, size + MALLOC_OVERHEAD);
137 allocations.allocated -= size;
138 allocations.allocations--;
140 debug(D_MEMORY, "MEMORY: freed %zu bytes for %s/%u@%s."
141 " Status: allocated %zu in %zu allocs."
143 , function, line, file
144 , allocations.allocated
145 , allocations.allocations
149 void *realloc_debug(const char *file, int line, const char *function, void *ptr, size_t size) {
150 if(!ptr) return malloc_debug(file, line, function, size);
151 if(!size) { free_debug(file, line, function, ptr); return NULL; }
154 void *real_ptr = check_allocation(file, line, function, ptr, &old_size);
156 void *new_ptr = realloc(real_ptr, size + MALLOC_OVERHEAD);
157 if(!new_ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
159 allocations.allocated += size;
160 allocations.allocated -= old_size;
162 debug(D_MEMORY, "MEMORY: Re-allocated from %zu to %zu bytes for %s/%u@%s."
163 " Status: allocated %z in %zu allocs."
165 , function, line, file
166 , allocations.allocated
167 , allocations.allocations
170 if(allocations.allocated > allocations.allocated_max) {
171 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
172 allocations.allocated_max = allocations.allocated;
175 return mark_allocation(new_ptr, size);
178 char *strdup_debug(const char *file, int line, const char *function, const char *ptr) {
185 char *p = malloc_debug(file, line, function, size);
186 if(!p) fatal("Cannot allocate %zu bytes.", size);
188 memcpy(p, ptr, size);
192 #define malloc(size) malloc_debug(__FILE__, __LINE__, __FUNCTION__, (size))
193 #define calloc(nmemb, size) calloc_debug(__FILE__, __LINE__, __FUNCTION__, (nmemb), (size))
194 #define realloc(ptr, size) realloc_debug(__FILE__, __LINE__, __FUNCTION__, (ptr), (size))
195 #define free(ptr) free_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
200 #define strdup(ptr) strdup_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
202 #endif /* NETDATA_INTERNAL_CHECKS */
205 // ----------------------------------------------------------------------------
210 long get_processors(void) {
213 char filename[FILENAME_MAX + 1];
214 snprintf(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
216 ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
219 ff = procfile_readall(ff);
221 // procfile_close(ff);
226 for(i = 0; i < procfile_lines(ff); i++) {
227 if(!procfile_linewords(ff, i)) continue;
229 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
232 if(processors < 1) processors = 1;
234 // procfile_close(ff);
238 long get_pid_max(void) {
241 char filename[FILENAME_MAX + 1];
242 snprintf(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
243 ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
246 ff = procfile_readall(ff);
248 // procfile_close(ff);
252 mpid = atol(procfile_lineword(ff, 0, 0));
253 if(!mpid) mpid = 32768;
255 // procfile_close(ff);
259 unsigned long long get_hertz(void)
261 unsigned long long myhz = 1;
264 if((myhz = (unsigned long long int) sysconf(_SC_CLK_TCK)) > 0) {
270 myhz = HZ; /* <asm/param.h> */
272 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
273 hz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
276 error("Unknown HZ value. Assuming %llu.", myhz);
281 // ----------------------------------------------------------------------------
283 // target is the point to aggregate a process tree values
286 char compare[MAX_COMPARE_NAME + 1];
287 char id[MAX_NAME + 1];
288 char name[MAX_NAME + 1];
290 unsigned long long minflt;
291 unsigned long long cminflt;
292 unsigned long long majflt;
293 unsigned long long cmajflt;
294 unsigned long long utime;
295 unsigned long long stime;
296 unsigned long long cutime;
297 unsigned long long cstime;
298 unsigned long long num_threads;
299 unsigned long long rss;
301 unsigned long long fix_minflt;
302 unsigned long long fix_cminflt;
303 unsigned long long fix_majflt;
304 unsigned long long fix_cmajflt;
305 unsigned long long fix_utime;
306 unsigned long long fix_stime;
307 unsigned long long fix_cutime;
308 unsigned long long fix_cstime;
310 unsigned long long statm_size;
311 unsigned long long statm_resident;
312 unsigned long long statm_share;
313 unsigned long long statm_text;
314 unsigned long long statm_lib;
315 unsigned long long statm_data;
316 unsigned long long statm_dirty;
318 unsigned long long io_logical_bytes_read;
319 unsigned long long io_logical_bytes_written;
320 unsigned long long io_read_calls;
321 unsigned long long io_write_calls;
322 unsigned long long io_storage_bytes_read;
323 unsigned long long io_storage_bytes_written;
324 unsigned long long io_cancelled_write_bytes;
326 unsigned long long fix_io_logical_bytes_read;
327 unsigned long long fix_io_logical_bytes_written;
328 unsigned long long fix_io_read_calls;
329 unsigned long long fix_io_write_calls;
330 unsigned long long fix_io_storage_bytes_read;
331 unsigned long long fix_io_storage_bytes_written;
332 unsigned long long fix_io_cancelled_write_bytes;
335 unsigned long long openfiles;
336 unsigned long long openpipes;
337 unsigned long long opensockets;
338 unsigned long long openinotifies;
339 unsigned long long openeventfds;
340 unsigned long long opentimerfds;
341 unsigned long long opensignalfds;
342 unsigned long long openeventpolls;
343 unsigned long long openother;
345 unsigned long processes; // how many processes have been merged to this
346 int exposed; // if set, we have sent this to netdata
347 int hidden; // if set, we set the hidden flag on the dimension
350 struct target *target; // the one that will be reported to netdata
352 } *target_root = NULL, *default_target = NULL;
356 // find or create a target
357 // there are targets that are just agregated to other target (the second argument)
358 struct target *get_target(const char *id, struct target *target)
360 const char *nid = id;
361 if(nid[0] == '-') nid++;
364 for(w = target_root ; w ; w = w->next)
365 if(strncmp(nid, w->id, MAX_NAME) == 0) return w;
367 w = calloc(sizeof(struct target), 1);
369 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
373 strncpy(w->id, nid, MAX_NAME);
374 strncpy(w->name, nid, MAX_NAME);
375 strncpy(w->compare, nid, MAX_COMPARE_NAME);
376 if(id[0] == '-') w->hidden = 1;
380 w->next = target_root;
383 if(debug) fprintf(stderr, "apps.plugin: adding hook for process '%s', compare '%s' on target '%s'\n", w->id, w->compare, w->target?w->target->id:"");
388 // read the process groups file
389 int read_process_groups(const char *name)
392 char filename[FILENAME_MAX + 1];
394 snprintf(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
396 if(debug) fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
397 FILE *fp = fopen(filename, "r");
399 error("Cannot open file '%s'", filename);
404 while(fgets(buffer, 4096, fp) != NULL) {
405 int whidden = 0, wdebug = 0;
408 // if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", buffer);
410 char *s = buffer, *t, *p;
412 if(!s || !*s || *s == '#') continue;
414 if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", s);
419 if(!t || !*t) continue;
441 if(debug) fprintf(stderr, "apps.plugin: \t\ttarget %s\n", t);
443 struct target *w = NULL;
446 char buffer[4097] = "";
450 while((p = strsep(&s, " "))) {
452 if(!p || !*p) continue;
454 strncpy(&buffer[blen], p, 4096 - blen);
455 blen = strlen(buffer);
457 while(buffer[blen - 1] == '\\') {
458 buffer[blen - 1] = ' ';
460 if((p = strsep(&s, " ")))
463 if(!p || !*p) p = " ";
464 strncpy(&buffer[blen], p, 4096 - blen);
465 blen = strlen(buffer);
468 struct target *n = get_target(buffer, w);
479 if(w) strncpy(w->name, t, MAX_NAME);
480 if(!count) error("The line %ld on file '%s', for group '%s' does not state any process names.", line, filename, t);
484 default_target = get_target("+p!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
485 strncpy(default_target->name, "other", MAX_NAME);
491 // ----------------------------------------------------------------------------
492 // data to store for each pid
497 char comm[MAX_COMPARE_NAME + 1];
505 unsigned long long minflt;
506 unsigned long long cminflt;
507 unsigned long long majflt;
508 unsigned long long cmajflt;
509 unsigned long long utime;
510 unsigned long long stime;
511 unsigned long long cutime;
512 unsigned long long cstime;
516 // int64_t itrealvalue;
517 // unsigned long long starttime;
518 // unsigned long long vsize;
519 unsigned long long rss;
520 // unsigned long long rsslim;
521 // unsigned long long starcode;
522 // unsigned long long endcode;
523 // unsigned long long startstack;
524 // unsigned long long kstkesp;
525 // unsigned long long kstkeip;
528 // uint64_t sigignore;
529 // uint64_t sigcatch;
533 // int32_t exit_signal;
534 // int32_t processor;
535 // uint32_t rt_priority;
537 // unsigned long long delayacct_blkio_ticks;
538 // uint64_t guest_time;
539 // int64_t cguest_time;
541 unsigned long long statm_size;
542 unsigned long long statm_resident;
543 unsigned long long statm_share;
544 unsigned long long statm_text;
545 unsigned long long statm_lib;
546 unsigned long long statm_data;
547 unsigned long long statm_dirty;
549 unsigned long long io_logical_bytes_read;
550 unsigned long long io_logical_bytes_written;
551 unsigned long long io_read_calls;
552 unsigned long long io_write_calls;
553 unsigned long long io_storage_bytes_read;
554 unsigned long long io_storage_bytes_written;
555 unsigned long long io_cancelled_write_bytes;
557 #ifdef INCLUDE_CHILDS
558 unsigned long long old_utime;
559 unsigned long long old_stime;
560 unsigned long long old_minflt;
561 unsigned long long old_majflt;
563 unsigned long long old_cutime;
564 unsigned long long old_cstime;
565 unsigned long long old_cminflt;
566 unsigned long long old_cmajflt;
568 unsigned long long fix_cutime;
569 unsigned long long fix_cstime;
570 unsigned long long fix_cminflt;
571 unsigned long long fix_cmajflt;
573 unsigned long long diff_cutime;
574 unsigned long long diff_cstime;
575 unsigned long long diff_cminflt;
576 unsigned long long diff_cmajflt;
577 #endif /* INCLUDE_CHILDS */
579 int *fds; // array of fds it uses
580 int fds_size; // the size of the fds array
582 int children_count; // number of processes directly referencing this
583 int updated; // 1 when update
584 int merged; // 1 when it has been merged to its parent
586 struct target *target;
587 struct pid_stat *parent;
588 struct pid_stat *prev;
589 struct pid_stat *next;
590 } *root_of_pids = NULL, **all_pids;
592 long all_pids_count = 0;
594 struct pid_stat *get_pid_entry(pid_t pid)
597 all_pids[pid]->new_entry = 0;
598 return all_pids[pid];
601 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
603 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct pid_stat));
607 all_pids[pid]->fds = calloc(sizeof(int), 100);
608 if(!all_pids[pid]->fds)
609 error("Cannot allocate %ld bytes of memory", (unsigned long)(sizeof(int) * 100));
610 else all_pids[pid]->fds_size = 100;
612 if(root_of_pids) root_of_pids->prev = all_pids[pid];
613 all_pids[pid]->next = root_of_pids;
614 root_of_pids = all_pids[pid];
616 all_pids[pid]->pid = pid;
617 all_pids[pid]->new_entry = 1;
619 return all_pids[pid];
622 void del_pid_entry(pid_t pid)
624 if(!all_pids[pid]) return;
626 if(debug) fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
628 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
629 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
630 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
632 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
634 all_pids[pid] = NULL;
638 // ----------------------------------------------------------------------------
639 // update pids from proc
641 int read_proc_pid_stat(struct pid_stat *p) {
642 char filename[FILENAME_MAX + 1];
644 snprintf(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
646 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
649 ff = procfile_readall(ff);
651 // procfile_close(ff);
658 p->comm[MAX_COMPARE_NAME] = '\0';
661 char *s = procfile_lineword(ff, 0, 1);
663 size_t len = strlen(s);
665 while(len && s[len - 1] != ')') {
666 if(blen < MAX_COMPARE_NAME) {
667 strncpy(&p->comm[blen], s, MAX_COMPARE_NAME - blen);
668 blen = strlen(p->comm);
672 s = procfile_lineword(ff, 0, 1+i);
675 if(len && s[len - 1] == ')') s[len - 1] = '\0';
676 if(blen < MAX_COMPARE_NAME)
677 strncpy(&p->comm[blen], s, MAX_COMPARE_NAME - blen);
679 // p->pid = atol(procfile_lineword(ff, 0, 0+i));
681 // p->state = *(procfile_lineword(ff, 0, 2+i));
682 p->ppid = (int32_t) atol(procfile_lineword(ff, 0, 3 + i));
683 // p->pgrp = atol(procfile_lineword(ff, 0, 4+i));
684 // p->session = atol(procfile_lineword(ff, 0, 5+i));
685 // p->tty_nr = atol(procfile_lineword(ff, 0, 6+i));
686 // p->tpgid = atol(procfile_lineword(ff, 0, 7+i));
687 // p->flags = strtoull(procfile_lineword(ff, 0, 8+i), NULL, 10);
688 p->minflt = strtoull(procfile_lineword(ff, 0, 9+i), NULL, 10);
689 p->cminflt = strtoull(procfile_lineword(ff, 0, 10+i), NULL, 10);
690 p->majflt = strtoull(procfile_lineword(ff, 0, 11+i), NULL, 10);
691 p->cmajflt = strtoull(procfile_lineword(ff, 0, 12+i), NULL, 10);
692 p->utime = strtoull(procfile_lineword(ff, 0, 13+i), NULL, 10);
693 p->stime = strtoull(procfile_lineword(ff, 0, 14+i), NULL, 10);
694 p->cutime = strtoull(procfile_lineword(ff, 0, 15+i), NULL, 10);
695 p->cstime = strtoull(procfile_lineword(ff, 0, 16+i), NULL, 10);
696 // p->priority = strtoull(procfile_lineword(ff, 0, 17+i), NULL, 10);
697 // p->nice = strtoull(procfile_lineword(ff, 0, 18+i), NULL, 10);
698 p->num_threads = (int32_t) atol(procfile_lineword(ff, 0, 19 + i));
699 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20+i), NULL, 10);
700 // p->starttime = strtoull(procfile_lineword(ff, 0, 21+i), NULL, 10);
701 // p->vsize = strtoull(procfile_lineword(ff, 0, 22+i), NULL, 10);
702 p->rss = strtoull(procfile_lineword(ff, 0, 23+i), NULL, 10);
703 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24+i), NULL, 10);
704 // p->starcode = strtoull(procfile_lineword(ff, 0, 25+i), NULL, 10);
705 // p->endcode = strtoull(procfile_lineword(ff, 0, 26+i), NULL, 10);
706 // p->startstack = strtoull(procfile_lineword(ff, 0, 27+i), NULL, 10);
707 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28+i), NULL, 10);
708 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29+i), NULL, 10);
709 // p->signal = strtoull(procfile_lineword(ff, 0, 30+i), NULL, 10);
710 // p->blocked = strtoull(procfile_lineword(ff, 0, 31+i), NULL, 10);
711 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32+i), NULL, 10);
712 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33+i), NULL, 10);
713 // p->wchan = strtoull(procfile_lineword(ff, 0, 34+i), NULL, 10);
714 // p->nswap = strtoull(procfile_lineword(ff, 0, 35+i), NULL, 10);
715 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36+i), NULL, 10);
716 // p->exit_signal = atol(procfile_lineword(ff, 0, 37+i));
717 // p->processor = atol(procfile_lineword(ff, 0, 38+i));
718 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39+i), NULL, 10);
719 // p->policy = strtoul(procfile_lineword(ff, 0, 40+i), NULL, 10);
720 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41+i), NULL, 10);
721 // p->guest_time = strtoull(procfile_lineword(ff, 0, 42+i), NULL, 10);
722 // p->cguest_time = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
724 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);
726 // procfile_close(ff);
730 int read_proc_pid_statm(struct pid_stat *p) {
731 char filename[FILENAME_MAX + 1];
733 snprintf(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
735 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
738 ff = procfile_readall(ff);
740 // procfile_close(ff);
746 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
747 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
748 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
749 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
750 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
751 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
752 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
754 // procfile_close(ff);
758 int read_proc_pid_io(struct pid_stat *p) {
759 char filename[FILENAME_MAX + 1];
761 snprintf(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
763 ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
766 ff = procfile_readall(ff);
768 // procfile_close(ff);
774 p->io_logical_bytes_read = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
775 p->io_logical_bytes_written = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
776 p->io_read_calls = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
777 p->io_write_calls = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
778 p->io_storage_bytes_read = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
779 p->io_storage_bytes_written = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
780 p->io_cancelled_write_bytes = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
782 // procfile_close(ff);
787 // ----------------------------------------------------------------------------
789 #ifdef INCLUDE_CHILDS
790 // print a tree view of all processes
791 int walk_down(pid_t pid, int level) {
792 struct pid_stat *p = NULL;
796 for(i = 0; i < level; i++) b[i] = '\t';
801 for(p = root_of_pids; p ; p = p->next) {
803 ret += walk_down(p->pid, level+1);
809 if(!p->updated) ret += 1;
810 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"
811 , b, p->comm, p->pid, p->updated?"OK":"KILLED", p->target->name, p->children_count
812 , p->utime, p->utime - p->old_utime
813 , p->stime, p->stime - p->old_stime
814 , p->cutime, p->cutime - p->old_cutime
815 , p->cstime, p->cstime - p->old_cstime
816 , p->minflt, p->minflt - p->old_minflt
817 , p->majflt, p->majflt - p->old_majflt
818 , p->cminflt, p->cminflt - p->old_cminflt
819 , p->cmajflt, p->cmajflt - p->old_cmajflt
825 #endif /* INCLUDE_CHILDS */
828 // ----------------------------------------------------------------------------
830 // this is used to keep a global list of all open files of the system
831 // it is needed in order to figure out the unique files a process tree has open
833 #define FILE_DESCRIPTORS_INCREASE_STEP 100
835 struct file_descriptor {
837 #ifdef NETDATA_INTERNAL_CHECKS
839 #endif /* NETDATA_INTERNAL_CHECKS */
847 int all_files_len = 0;
848 int all_files_size = 0;
850 int file_descriptor_compare(void* a, void* b) {
851 #ifdef NETDATA_INTERNAL_CHECKS
852 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
853 error("Corrupted index data detected. Please report this.");
854 #endif /* NETDATA_INTERNAL_CHECKS */
856 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
859 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
863 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
866 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
868 avl_tree all_files_index = {
870 file_descriptor_compare,
871 #ifdef AVL_LOCK_WITH_MUTEX
872 PTHREAD_MUTEX_INITIALIZER
874 PTHREAD_RWLOCK_INITIALIZER
878 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
879 struct file_descriptor *result = NULL, tmp;
880 tmp.hash = (hash)?hash:simple_hash(name);
884 #ifdef NETDATA_INTERNAL_CHECKS
885 tmp.magic = 0x0BADCAFE;
886 #endif /* NETDATA_INTERNAL_CHECKS */
888 avl_search(&all_files_index, (avl *)&tmp, file_descriptor_iterator, (avl **)&result);
892 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
893 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
895 #define FILETYPE_OTHER 0
896 #define FILETYPE_FILE 1
897 #define FILETYPE_PIPE 2
898 #define FILETYPE_SOCKET 3
899 #define FILETYPE_INOTIFY 4
900 #define FILETYPE_EVENTFD 5
901 #define FILETYPE_EVENTPOLL 6
902 #define FILETYPE_TIMERFD 7
903 #define FILETYPE_SIGNALFD 8
905 void file_descriptor_not_used(int id)
907 if(id > 0 && id < all_files_size) {
909 #ifdef NETDATA_INTERNAL_CHECKS
910 if(all_files[id].magic != 0x0BADCAFE) {
911 error("Ignoring request to remove empty file id %d.", id);
914 #endif /* NETDATA_INTERNAL_CHECKS */
916 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
918 if(all_files[id].count > 0) {
919 all_files[id].count--;
921 if(!all_files[id].count) {
922 if(debug) fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
923 file_descriptor_remove(&all_files[id]);
924 #ifdef NETDATA_INTERNAL_CHECKS
925 all_files[id].magic = 0x00000000;
926 #endif /* NETDATA_INTERNAL_CHECKS */
931 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
933 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
936 int file_descriptor_find_or_add(const char *name)
938 static int last_pos = 0;
939 uint32_t hash = simple_hash(name);
941 if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
943 struct file_descriptor *fd = file_descriptor_find(name, hash);
946 if(debug) fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
952 // check we have enough memory to add it
953 if(!all_files || all_files_len == all_files_size) {
954 void *old = all_files;
957 // there is no empty slot
958 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
959 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
961 // if the address changed, we have to rebuild the index
962 // since all pointers are now invalid
963 if(old && old != (void *)all_files) {
964 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing.\n");
965 all_files_index.root = NULL;
966 for(i = 0; i < all_files_size; i++) {
967 if(!all_files[i].count) continue;
968 file_descriptor_add(&all_files[i]);
970 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
973 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
974 all_files[i].count = 0;
975 all_files[i].name = NULL;
976 #ifdef NETDATA_INTERNAL_CHECKS
977 all_files[i].magic = 0x00000000;
978 #endif /* NETDATA_INTERNAL_CHECKS */
979 all_files[i].pos = i;
982 if(!all_files_size) all_files_len = 1;
983 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
986 if(debug) fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
988 // search for an empty slot
990 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
991 if(c >= all_files_size) c = 0;
994 if(!all_files[c].count) {
995 if(debug) fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
997 #ifdef NETDATA_INTERNAL_CHECKS
998 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
999 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1000 #endif /* NETDATA_INTERNAL_CHECKS */
1002 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);
1003 if(all_files[c].name) free((void *)all_files[c].name);
1004 all_files[c].name = NULL;
1009 if(i == all_files_size) {
1010 fatal("We should find an empty slot, but there isn't any");
1013 if(debug) fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
1017 // else we have an empty slot in 'c'
1020 if(name[0] == '/') type = FILETYPE_FILE;
1021 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1022 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1023 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1024 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1025 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1026 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1027 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1028 else if(strncmp(name, "anon_inode:", 11) == 0) {
1029 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1030 type = FILETYPE_OTHER;
1033 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1034 type = FILETYPE_OTHER;
1037 all_files[c].name = strdup(name);
1038 all_files[c].hash = hash;
1039 all_files[c].type = type;
1040 all_files[c].pos = c;
1041 all_files[c].count = 1;
1042 #ifdef NETDATA_INTERNAL_CHECKS
1043 all_files[c].magic = 0x0BADCAFE;
1044 #endif /* NETDATA_INTERNAL_CHECKS */
1045 file_descriptor_add(&all_files[c]);
1047 if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1053 // 1. read all files in /proc
1054 // 2. for each numeric directory:
1055 // i. read /proc/pid/stat
1056 // ii. read /proc/pid/statm
1057 // iii. read /proc/pid/io (requires root access)
1058 // iii. read the entries in directory /proc/pid/fd (requires root access)
1060 // a. find or create a struct file_descriptor
1061 // b. cleanup any old/unused file_descriptors
1063 // after all these, some pids may be linked to targets, while others may not
1065 // in case of errors, only 1 every 1000 errors is printed
1066 // to avoid filling up all disk space
1067 // if debug is enabled, all errors are printed
1069 int update_from_proc(void)
1071 static long count_errors = 0;
1073 char filename[FILENAME_MAX+1];
1074 char dirname[FILENAME_MAX + 1];
1076 snprintf(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1077 DIR *dir = opendir(dirname);
1080 struct dirent *file = NULL;
1081 struct pid_stat *p = NULL;
1083 // mark them all as un-updated
1085 for(p = root_of_pids; p ; p = p->next) {
1089 p->children_count = 0;
1094 while((file = readdir(dir))) {
1095 char *endptr = file->d_name;
1096 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1097 if(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0') continue;
1099 p = get_pid_entry(pid);
1102 // --------------------------------------------------------------------
1105 if(read_proc_pid_stat(p)) {
1106 if(!count_errors++ || debug || (p->target && p->target->debug))
1107 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1111 if(p->ppid < 0 || p->ppid > pid_max) p->ppid = 0;
1114 // --------------------------------------------------------------------
1115 // /proc/<pid>/statm
1117 if(read_proc_pid_statm(p)) {
1118 if(!count_errors++ || debug || (p->target && p->target->debug))
1119 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1125 // --------------------------------------------------------------------
1128 if(read_proc_pid_io(p)) {
1129 if(!count_errors++ || debug || (p->target && p->target->debug))
1130 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1132 // on systems without /proc/X/io
1133 // allow proceeding without I/O information
1137 // --------------------------------------------------------------------
1140 // check if it is target
1141 // we do this only once, the first time this pid is loaded
1143 if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
1146 for(w = target_root; w ; w = w->next) {
1147 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1149 if(strcmp(w->compare, p->comm) == 0) {
1150 if(w->target) p->target = w->target;
1153 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1158 // --------------------------------------------------------------------
1161 snprintf(filename, FILENAME_MAX, "%s/proc/%s/fd", host_prefix, file->d_name);
1162 DIR *fds = opendir(filename);
1166 char fdname[FILENAME_MAX + 1];
1167 char linkname[FILENAME_MAX + 1];
1169 // make the array negative
1170 for(c = 0 ; c < p->fds_size ; c++) p->fds[c] = -p->fds[c];
1172 while((de = readdir(fds))) {
1173 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1175 // check if the fds array is small
1176 int fdid = atoi(de->d_name);
1177 if(fdid < 0) continue;
1178 if(fdid >= p->fds_size) {
1179 // it is small, extend it
1180 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1181 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1183 error("Cannot re-allocate fds for %s", p->comm);
1187 // and initialize it
1188 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1189 p->fds_size = fdid + 100;
1192 if(p->fds[fdid] == 0) {
1193 // we don't know this fd, get it
1195 sprintf(fdname, "%s/proc/%s/fd/%s", host_prefix, file->d_name, de->d_name);
1196 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1198 if(debug || (p->target && p->target->debug)) {
1199 if(!count_errors++ || debug || (p->target && p->target->debug))
1200 error("Cannot read link %s", fdname);
1207 // if another process already has this, we will get
1209 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1212 // else make it positive again, we need it
1213 // of course, the actual file may have changed, but we don't care so much
1214 // FIXME: we could compare the inode as returned by readdir direct structure
1215 else p->fds[fdid] = -p->fds[fdid];
1219 // remove all the negative file descriptors
1220 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1221 file_descriptor_not_used(-p->fds[c]);
1226 // --------------------------------------------------------------------
1229 // mark it as updated
1232 if(count_errors > 1000) {
1233 error("%ld more errors encountered\n", count_errors - 1);
1243 // ----------------------------------------------------------------------------
1244 // update statistics on the targets
1246 // 1. link all childs to their parents
1247 // 2. go from bottom to top, marking as merged all childs to their parents
1248 // this step links all parents without a target to the child target, if any
1249 // 3. link all top level processes (the ones not merged) to the default target
1250 // 4. go from top to bottom, linking all childs without a target, to their parent target
1251 // after this step, all processes have a target
1252 // [5. for each killed pid (updated = 0), remove its usage from its target]
1253 // 6. zero all targets
1254 // 7. concentrate all values on the targets
1255 // 8. remove all killed processes
1256 // 9. find the unique file count for each target
1258 void update_statistics(void)
1261 struct pid_stat *p = NULL;
1264 // link all children to their parents
1265 // and update children count on parents
1266 for(p = root_of_pids; p ; p = p->next) {
1267 // for each process found running
1270 && p->ppid <= pid_max
1271 && all_pids[p->ppid]
1273 // for valid processes
1275 if(debug || (p->target && p->target->debug))
1276 fprintf(stderr, "apps.plugin: \tparent of %d (%s) is %d (%s)\n", p->pid, p->comm, p->ppid, all_pids[p->ppid]->comm);
1278 p->parent = all_pids[p->ppid];
1279 p->parent->children_count++;
1281 else if(p->ppid != 0)
1282 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1286 // children that do not have a target
1287 // inherit their target from their parent
1291 for(p = root_of_pids; p ; p = p->next) {
1292 // if this process does not have a target
1293 // and it has a parent
1294 // and its parent has a target
1295 // then, set the parent's target to this process
1296 if(unlikely(!p->target && p->parent && p->parent->target)) {
1297 p->target = p->parent->target;
1300 if(debug || (p->target && p->target->debug))
1301 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);
1307 // find all the procs with 0 childs and merge them to their parents
1308 // repeat, until nothing more can be done.
1312 for(p = root_of_pids; p ; p = p->next) {
1313 // if this process does not have any children
1314 // and is not already merged
1316 // and its parent has children
1317 // and the target of this process and its parent is the same, or the parent does not have a target
1318 // and its parent is not init
1319 // then, mark them as merged.
1324 && p->parent->children_count
1325 && (p->target == p->parent->target || !p->parent->target)
1328 p->parent->children_count--;
1331 // the parent inherits the child's target, if it does not have a target itself
1332 if(unlikely(p->target && !p->parent->target)) {
1333 p->parent->target = p->target;
1335 if(debug || (p->target && p->target->debug))
1336 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);
1344 fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1347 // init goes always to default target
1349 all_pids[1]->target = default_target;
1351 // give a default target on all top level processes
1352 for(p = root_of_pids; p ; p = p->next) {
1353 // if the process is not merged itself
1354 // then is is a top level process
1355 if(!p->merged && !p->target)
1356 p->target = default_target;
1358 #ifdef INCLUDE_CHILDS
1359 // by the way, update the diffs
1360 // will be used later for subtracting killed process times
1361 p->diff_cutime = p->utime - p->cutime;
1362 p->diff_cstime = p->stime - p->cstime;
1363 p->diff_cminflt = p->minflt - p->cminflt;
1364 p->diff_cmajflt = p->majflt - p->cmajflt;
1365 #endif /* INCLUDE_CHILDS */
1368 // give a target to all merged child processes
1372 for(p = root_of_pids; p ; p = p->next) {
1373 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1374 p->target = p->parent->target;
1377 if(debug || (p->target && p->target->debug))
1378 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);
1383 #ifdef INCLUDE_CHILDS
1384 // for each killed process, remove its values from the parents
1385 // sums (we had already added them in a previous loop)
1386 for(p = root_of_pids; p ; p = p->next) {
1387 if(p->updated) continue;
1389 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1391 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1392 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1393 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1394 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1396 struct pid_stat *t = p;
1397 while((t = t->parent)) {
1398 if(!t->updated) continue;
1400 unsigned long long x;
1401 if(diff_utime && t->diff_cutime) {
1402 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1404 t->diff_cutime -= x;
1406 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1408 if(diff_stime && t->diff_cstime) {
1409 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1411 t->diff_cstime -= x;
1413 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1415 if(diff_minflt && t->diff_cminflt) {
1416 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1418 t->diff_cminflt -= x;
1419 t->fix_cminflt += x;
1420 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1422 if(diff_majflt && t->diff_cmajflt) {
1423 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1425 t->diff_cmajflt -= x;
1426 t->fix_cmajflt += x;
1427 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1431 if(diff_utime) error("Cannot fix up utime %llu", diff_utime);
1432 if(diff_stime) error("Cannot fix up stime %llu", diff_stime);
1433 if(diff_minflt) error("Cannot fix up minflt %llu", diff_minflt);
1434 if(diff_majflt) error("Cannot fix up majflt %llu", diff_majflt);
1436 #endif /* INCLUDE_CHILDS */
1438 // zero all the targets
1441 for (w = target_root; w ; w = w->next) {
1444 w->fds = calloc(sizeof(int), (size_t) all_files_size);
1446 error("Cannot allocate memory for fds in %s", w->name);
1461 w->statm_resident = 0;
1468 w->io_logical_bytes_read = 0;
1469 w->io_logical_bytes_written = 0;
1470 w->io_read_calls = 0;
1471 w->io_write_calls = 0;
1472 w->io_storage_bytes_read = 0;
1473 w->io_storage_bytes_written = 0;
1474 w->io_cancelled_write_bytes = 0;
1477 #ifdef INCLUDE_CHILDS
1478 if(debug) walk_down(0, 1);
1479 #endif /* INCLUDE_CHILDS */
1481 // concentrate everything on the targets
1482 for(p = root_of_pids; p ; p = p->next) {
1484 error("pid %d %s was left without a target!", p->pid, p->comm);
1489 p->target->cutime += p->cutime; // - p->fix_cutime;
1490 p->target->cstime += p->cstime; // - p->fix_cstime;
1491 p->target->cminflt += p->cminflt; // - p->fix_cminflt;
1492 p->target->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1494 p->target->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1495 p->target->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1496 p->target->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1497 p->target->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1499 //if(p->num_threads < 0)
1500 // error("Negative threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1502 //if(p->num_threads > 10000)
1503 // error("Excessive threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1505 p->target->num_threads += p->num_threads;
1506 p->target->rss += p->rss;
1508 p->target->statm_size += p->statm_size;
1509 p->target->statm_resident += p->statm_resident;
1510 p->target->statm_share += p->statm_share;
1511 p->target->statm_text += p->statm_text;
1512 p->target->statm_lib += p->statm_lib;
1513 p->target->statm_data += p->statm_data;
1514 p->target->statm_dirty += p->statm_dirty;
1516 p->target->io_logical_bytes_read += p->io_logical_bytes_read;
1517 p->target->io_logical_bytes_written += p->io_logical_bytes_written;
1518 p->target->io_read_calls += p->io_read_calls;
1519 p->target->io_write_calls += p->io_write_calls;
1520 p->target->io_storage_bytes_read += p->io_storage_bytes_read;
1521 p->target->io_storage_bytes_written += p->io_storage_bytes_written;
1522 p->target->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1524 p->target->processes++;
1526 for(c = 0; c < p->fds_size ;c++) {
1527 if(p->fds[c] == 0) continue;
1528 if(p->fds[c] < all_files_size) {
1529 if(p->target->fds) p->target->fds[p->fds[c]]++;
1532 error("Invalid fd number %d", p->fds[c]);
1535 if(debug || p->target->debug) 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, p->target->name, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1537 /* 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);
1538 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);
1539 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);
1540 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);
1541 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);
1542 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);
1543 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);
1544 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);
1546 #ifdef INCLUDE_CHILDS
1547 p->old_utime = p->utime;
1548 p->old_cutime = p->cutime;
1549 p->old_stime = p->stime;
1550 p->old_cstime = p->cstime;
1551 p->old_minflt = p->minflt;
1552 p->old_majflt = p->majflt;
1553 p->old_cminflt = p->cminflt;
1554 p->old_cmajflt = p->cmajflt;
1555 #endif /* INCLUDE_CHILDS */
1558 // since the process has exited, the user
1559 // will see a drop in our charts, because the incremental
1560 // values of this process will not be there
1562 // add them to the fix_* values and they will be added to
1563 // the reported values, so that the report goes steady
1564 p->target->fix_minflt += p->minflt;
1565 p->target->fix_majflt += p->majflt;
1566 p->target->fix_utime += p->utime;
1567 p->target->fix_stime += p->stime;
1568 p->target->fix_cminflt += p->cminflt;
1569 p->target->fix_cmajflt += p->cmajflt;
1570 p->target->fix_cutime += p->cutime;
1571 p->target->fix_cstime += p->cstime;
1573 p->target->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1574 p->target->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1575 p->target->fix_io_read_calls += p->io_read_calls;
1576 p->target->fix_io_write_calls += p->io_write_calls;
1577 p->target->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1578 p->target->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1579 p->target->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1583 // fprintf(stderr, "\n");
1584 // cleanup all un-updated processed (exited, killed, etc)
1585 for(p = root_of_pids; p ;) {
1587 // 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);
1589 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1590 file_descriptor_not_used(p->fds[c]);
1601 for (w = target_root; w ; w = w->next) {
1605 w->openinotifies = 0;
1606 w->openeventfds = 0;
1607 w->opentimerfds = 0;
1608 w->opensignalfds = 0;
1609 w->openeventpolls = 0;
1612 for(c = 1; c < all_files_size ;c++) {
1613 if(w->fds && w->fds[c] > 0) switch(all_files[c].type) {
1622 case FILETYPE_SOCKET:
1626 case FILETYPE_INOTIFY:
1630 case FILETYPE_EVENTFD:
1634 case FILETYPE_TIMERFD:
1638 case FILETYPE_SIGNALFD:
1642 case FILETYPE_EVENTPOLL:
1643 w->openeventpolls++;
1656 // ----------------------------------------------------------------------------
1657 // update chart dimensions
1659 void show_dimensions(void)
1661 static struct timeval last = { 0, 0 };
1662 static struct rusage me_last;
1668 unsigned long long usec;
1669 unsigned long long cpuuser;
1670 unsigned long long cpusyst;
1673 gettimeofday(&last, NULL);
1674 getrusage(RUSAGE_SELF, &me_last);
1676 // the first time, give a zero to allow
1677 // netdata calibrate to the current time
1678 // usec = update_every * 1000000ULL;
1684 gettimeofday(&now, NULL);
1685 getrusage(RUSAGE_SELF, &me);
1687 usec = usecdiff(&now, &last);
1688 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
1689 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
1691 bcopy(&now, &last, sizeof(struct timeval));
1692 bcopy(&me, &me_last, sizeof(struct rusage));
1695 fprintf(stdout, "BEGIN apps.cpu %llu\n", usec);
1696 for (w = target_root; w ; w = w->next) {
1697 if(w->target || (!w->processes && !w->exposed)) continue;
1699 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
1701 fprintf(stdout, "END\n");
1703 fprintf(stdout, "BEGIN apps.cpu_user %llu\n", usec);
1704 for (w = target_root; w ; w = w->next) {
1705 if(w->target || (!w->processes && !w->exposed)) continue;
1707 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
1709 fprintf(stdout, "END\n");
1711 fprintf(stdout, "BEGIN apps.cpu_system %llu\n", usec);
1712 for (w = target_root; w ; w = w->next) {
1713 if(w->target || (!w->processes && !w->exposed)) continue;
1715 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
1717 fprintf(stdout, "END\n");
1719 fprintf(stdout, "BEGIN apps.threads %llu\n", usec);
1720 for (w = target_root; w ; w = w->next) {
1721 if(w->target || (!w->processes && !w->exposed)) continue;
1723 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
1725 fprintf(stdout, "END\n");
1727 fprintf(stdout, "BEGIN apps.processes %llu\n", usec);
1728 for (w = target_root; w ; w = w->next) {
1729 if(w->target || (!w->processes && !w->exposed)) continue;
1731 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
1733 fprintf(stdout, "END\n");
1735 fprintf(stdout, "BEGIN apps.mem %llu\n", usec);
1736 for (w = target_root; w ; w = w->next) {
1737 if(w->target || (!w->processes && !w->exposed)) continue;
1739 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
1741 fprintf(stdout, "END\n");
1743 fprintf(stdout, "BEGIN apps.minor_faults %llu\n", usec);
1744 for (w = target_root; w ; w = w->next) {
1745 if(w->target || (!w->processes && !w->exposed)) continue;
1747 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt);
1749 fprintf(stdout, "END\n");
1751 fprintf(stdout, "BEGIN apps.major_faults %llu\n", usec);
1752 for (w = target_root; w ; w = w->next) {
1753 if(w->target || (!w->processes && !w->exposed)) continue;
1755 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt);
1757 fprintf(stdout, "END\n");
1759 fprintf(stdout, "BEGIN apps.lreads %llu\n", usec);
1760 for (w = target_root; w ; w = w->next) {
1761 if(w->target || (!w->processes && !w->exposed)) continue;
1763 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read);
1765 fprintf(stdout, "END\n");
1767 fprintf(stdout, "BEGIN apps.lwrites %llu\n", usec);
1768 for (w = target_root; w ; w = w->next) {
1769 if(w->target || (!w->processes && !w->exposed)) continue;
1771 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written);
1773 fprintf(stdout, "END\n");
1775 fprintf(stdout, "BEGIN apps.preads %llu\n", usec);
1776 for (w = target_root; w ; w = w->next) {
1777 if(w->target || (!w->processes && !w->exposed)) continue;
1779 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read);
1781 fprintf(stdout, "END\n");
1783 fprintf(stdout, "BEGIN apps.pwrites %llu\n", usec);
1784 for (w = target_root; w ; w = w->next) {
1785 if(w->target || (!w->processes && !w->exposed)) continue;
1787 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written);
1789 fprintf(stdout, "END\n");
1791 fprintf(stdout, "BEGIN apps.files %llu\n", usec);
1792 for (w = target_root; w ; w = w->next) {
1793 if(w->target || (!w->processes && !w->exposed)) continue;
1795 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
1797 fprintf(stdout, "END\n");
1799 fprintf(stdout, "BEGIN apps.sockets %llu\n", usec);
1800 for (w = target_root; w ; w = w->next) {
1801 if(w->target || (!w->processes && !w->exposed)) continue;
1803 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
1805 fprintf(stdout, "END\n");
1807 fprintf(stdout, "BEGIN apps.pipes %llu\n", usec);
1808 for (w = target_root; w ; w = w->next) {
1809 if(w->target || (!w->processes && !w->exposed)) continue;
1811 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
1813 fprintf(stdout, "END\n");
1815 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
1816 fprintf(stdout, "SET user = %llu\n", cpuuser);
1817 fprintf(stdout, "SET system = %llu\n", cpusyst);
1818 fprintf(stdout, "END\n");
1820 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
1821 fprintf(stdout, "SET files = %llu\n", file_counter);
1822 fprintf(stdout, "SET pids = %ld\n", all_pids_count);
1823 fprintf(stdout, "SET fds = %d\n", all_files_len);
1824 fprintf(stdout, "SET targets = %ld\n", targets);
1825 fprintf(stdout, "END\n");
1831 // ----------------------------------------------------------------------------
1832 // generate the charts
1834 void show_charts(void)
1837 int newly_added = 0;
1839 for(w = target_root ; w ; w = w->next)
1840 if(!w->exposed && w->processes) {
1843 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
1846 // nothing more to show
1847 if(!newly_added) return;
1849 // we have something new to show
1850 // update the charts
1851 fprintf(stdout, "CHART apps.cpu '' 'Apps CPU Time (%ld%% = %ld core%s)' 'cpu time %%' cpu apps.cpu stacked 20001 %d\n", (processors * 100), processors, (processors>1)?"s":"", update_every);
1852 for (w = target_root; w ; w = w->next) {
1853 if(w->target || (!w->processes && !w->exposed)) continue;
1855 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu %s\n", w->name, Hertz, w->hidden ? "hidden,noreset" : "noreset");
1858 fprintf(stdout, "CHART apps.mem '' 'Apps Dedicated Memory (w/o shared)' 'MB' mem apps.mem stacked 20003 %d\n", update_every);
1859 for (w = target_root; w ; w = w->next) {
1860 if(w->target || (!w->processes && !w->exposed)) continue;
1862 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld noreset\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
1865 fprintf(stdout, "CHART apps.threads '' 'Apps Threads' 'threads' processes apps.threads stacked 20005 %d\n", update_every);
1866 for (w = target_root; w ; w = w->next) {
1867 if(w->target || (!w->processes && !w->exposed)) continue;
1869 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1872 fprintf(stdout, "CHART apps.processes '' 'Apps Processes' 'processes' processes apps.processes stacked 20004 %d\n", update_every);
1873 for (w = target_root; w ; w = w->next) {
1874 if(w->target || (!w->processes && !w->exposed)) continue;
1876 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1879 fprintf(stdout, "CHART apps.cpu_user '' 'Apps CPU User Time (%ld%% = %ld core%s)' 'cpu time %%' cpu apps.cpu_user stacked 20020 %d\n", (processors * 100), processors, (processors>1)?"s":"", update_every);
1880 for (w = target_root; w ; w = w->next) {
1881 if(w->target || (!w->processes && !w->exposed)) continue;
1883 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
1886 fprintf(stdout, "CHART apps.cpu_system '' 'Apps CPU System Time (%ld%% = %ld core%s)' 'cpu time %%' cpu apps.cpu_system stacked 20021 %d\n", (processors * 100), processors, (processors>1)?"s":"", update_every);
1887 for (w = target_root; w ; w = w->next) {
1888 if(w->target || (!w->processes && !w->exposed)) continue;
1890 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
1893 fprintf(stdout, "CHART apps.major_faults '' 'Apps Major Page Faults (swap read)' 'page faults/s' swap apps.major_faults stacked 20010 %d\n", update_every);
1894 for (w = target_root; w ; w = w->next) {
1895 if(w->target || (!w->processes && !w->exposed)) continue;
1897 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
1900 fprintf(stdout, "CHART apps.minor_faults '' 'Apps Minor Page Faults' 'page faults/s' mem apps.minor_faults stacked 20011 %d\n", update_every);
1901 for (w = target_root; w ; w = w->next) {
1902 if(w->target || (!w->processes && !w->exposed)) continue;
1904 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
1907 fprintf(stdout, "CHART apps.lreads '' 'Apps Disk Logical Reads' 'kilobytes/s' disk apps.lreads stacked 20042 %d\n", update_every);
1908 for (w = target_root; w ; w = w->next) {
1909 if(w->target || (!w->processes && !w->exposed)) continue;
1911 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1914 fprintf(stdout, "CHART apps.lwrites '' 'Apps I/O Logical Writes' 'kilobytes/s' disk apps.lwrites stacked 20042 %d\n", update_every);
1915 for (w = target_root; w ; w = w->next) {
1916 if(w->target || (!w->processes && !w->exposed)) continue;
1918 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1921 fprintf(stdout, "CHART apps.preads '' 'Apps Disk Reads' 'kilobytes/s' disk apps.preads stacked 20002 %d\n", update_every);
1922 for (w = target_root; w ; w = w->next) {
1923 if(w->target || (!w->processes && !w->exposed)) continue;
1925 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1928 fprintf(stdout, "CHART apps.pwrites '' 'Apps Disk Writes' 'kilobytes/s' disk apps.pwrites stacked 20002 %d\n", update_every);
1929 for (w = target_root; w ; w = w->next) {
1930 if(w->target || (!w->processes && !w->exposed)) continue;
1932 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1935 fprintf(stdout, "CHART apps.files '' 'Apps Open Files' 'open files' disk apps.files stacked 20050 %d\n", update_every);
1936 for (w = target_root; w ; w = w->next) {
1937 if(w->target || (!w->processes && !w->exposed)) continue;
1939 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1942 fprintf(stdout, "CHART apps.sockets '' 'Apps Open Sockets' 'open sockets' net apps.sockets stacked 20051 %d\n", update_every);
1943 for (w = target_root; w ; w = w->next) {
1944 if(w->target || (!w->processes && !w->exposed)) continue;
1946 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1949 fprintf(stdout, "CHART apps.pipes '' 'Apps Pipes' 'open pipes' processes apps.pipes stacked 20053 %d\n", update_every);
1950 for (w = target_root; w ; w = w->next) {
1951 if(w->target || (!w->processes && !w->exposed)) continue;
1953 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1956 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %d\n", update_every);
1957 fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000);
1958 fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000);
1960 fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %d\n", update_every);
1961 fprintf(stdout, "DIMENSION files '' incremental 1 1\n");
1962 fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
1963 fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
1964 fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
1970 // ----------------------------------------------------------------------------
1971 // parse command line arguments
1973 void parse_args(int argc, char **argv)
1978 for(i = 1; i < argc; i++) {
1980 int n = atoi(argv[i]);
1987 if(strcmp("debug", argv[i]) == 0) {
1989 debug_flags = 0xffffffff;
1998 error("Cannot understand option %s", argv[i]);
2002 if(freq > 0) update_every = freq;
2003 if(!name) name = "groups";
2005 if(read_process_groups(name)) {
2006 error("Cannot read process groups %s", name);
2011 int main(int argc, char **argv)
2013 // debug_flags = D_PROCFILE;
2015 // set the name for logging
2016 program_name = "apps.plugin";
2018 host_prefix = getenv("NETDATA_HOST_PREFIX");
2019 if(host_prefix == NULL) {
2020 info("NETDATA_HOST_PREFIX is not passed from netdata");
2023 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2025 config_dir = getenv("NETDATA_CONFIG_DIR");
2026 if(config_dir == NULL) {
2027 info("NETDATA_CONFIG_DIR is not passed from netdata");
2028 config_dir = CONFIG_DIR;
2030 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2032 info("starting...");
2034 procfile_adaptive_initial_allocation = 1;
2036 time_t started_t = time(NULL);
2038 Hertz = get_hertz();
2039 pid_max = get_pid_max();
2040 processors = get_processors();
2042 parse_args(argc, argv);
2044 all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2046 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2047 printf("DISABLE\n");
2051 unsigned long long counter = 1;
2052 unsigned long long usec = 0, susec = 0;
2053 struct timeval last, now;
2054 gettimeofday(&last, NULL);
2056 for(;1; counter++) {
2057 if(!update_from_proc()) {
2058 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2059 printf("DISABLE\n");
2063 update_statistics();
2064 show_charts(); // this is smart enough to show only newly added apps, when needed
2067 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
2070 gettimeofday(&now, NULL);
2071 usec = usecdiff(&now, &last) - susec;
2072 if(debug) fprintf(stderr, "apps.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).\n", usec + susec, usec, susec);
2074 // if the last loop took less than half the time
2075 // wait the rest of the time
2076 if(usec < (update_every * 1000000ULL / 2)) susec = (update_every * 1000000ULL) - usec;
2077 else susec = update_every * 1000000ULL / 2;
2079 usleep((useconds_t) susec);
2080 bcopy(&now, &last, sizeof(struct timeval));
2082 // restart once per day (14400 seconds)
2083 current_t = time(NULL);
2084 if(current_t - started_t > 14400) exit(0);