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 /* ifdef 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;
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
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 {
845 int all_files_len = 0;
846 int all_files_size = 0;
848 int file_descriptor_compare(void* a, void* b) {
849 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
850 error("Corrupted index data detected. Please report this.");
852 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
854 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
856 else return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
858 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
860 avl_tree all_files_index = {
862 file_descriptor_compare,
863 #ifdef AVL_LOCK_WITH_MUTEX
864 PTHREAD_MUTEX_INITIALIZER
866 PTHREAD_RWLOCK_INITIALIZER
870 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
871 struct file_descriptor *result = NULL, tmp;
872 tmp.hash = (hash)?hash:simple_hash(name);
876 tmp.magic = 0x0BADCAFE;
878 avl_search(&all_files_index, (avl *)&tmp, file_descriptor_iterator, (avl **)&result);
882 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
883 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
885 #define FILETYPE_OTHER 0
886 #define FILETYPE_FILE 1
887 #define FILETYPE_PIPE 2
888 #define FILETYPE_SOCKET 3
889 #define FILETYPE_INOTIFY 4
890 #define FILETYPE_EVENTFD 5
891 #define FILETYPE_EVENTPOLL 6
892 #define FILETYPE_TIMERFD 7
893 #define FILETYPE_SIGNALFD 8
895 void file_descriptor_not_used(int id)
897 if(id > 0 && id < all_files_size) {
898 if(all_files[id].magic != 0x0BADCAFE) {
899 error("Ignoring request to remove empty file id %d.", id);
903 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
905 if(all_files[id].count > 0) {
906 all_files[id].count--;
908 if(!all_files[id].count) {
909 if(debug) fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
910 file_descriptor_remove(&all_files[id]);
911 all_files[id].magic = 0x00000000;
916 error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
918 else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
921 int file_descriptor_find_or_add(const char *name)
923 static int last_pos = 0;
924 uint32_t hash = simple_hash(name);
926 if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
928 struct file_descriptor *fd = file_descriptor_find(name, hash);
931 if(debug) fprintf(stderr, "apps.plugin: >> found on slot %d\n", fd->pos);
937 // check we have enough memory to add it
938 if(!all_files || all_files_len == all_files_size) {
939 void *old = all_files;
942 // there is no empty slot
943 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
944 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
946 // if the address changed, we have to rebuild the index
947 // since all pointers are now invalid
948 if(old && old != (void *)all_files) {
949 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing.\n");
950 all_files_index.root = NULL;
951 for(i = 0; i < all_files_size; i++) {
952 if(!all_files[i].count) continue;
953 file_descriptor_add(&all_files[i]);
955 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
958 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
959 all_files[i].count = 0;
960 all_files[i].name = NULL;
961 all_files[i].magic = 0x00000000;
962 all_files[i].pos = i;
965 if(!all_files_size) all_files_len = 1;
966 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
969 if(debug) fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
971 // search for an empty slot
973 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
974 if(c >= all_files_size) c = 0;
977 if(!all_files[c].count) {
978 if(debug) fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
980 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
981 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
983 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);
984 if(all_files[c].name) free((void *)all_files[c].name);
985 all_files[c].name = NULL;
990 if(i == all_files_size) {
991 fatal("We should find an empty slot, but there isn't any");
994 if(debug) fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
998 // else we have an empty slot in 'c'
1001 if(name[0] == '/') type = FILETYPE_FILE;
1002 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1003 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1004 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1005 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1006 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1007 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1008 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1009 else if(strncmp(name, "anon_inode:", 11) == 0) {
1010 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1011 type = FILETYPE_OTHER;
1014 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1015 type = FILETYPE_OTHER;
1018 all_files[c].name = strdup(name);
1019 all_files[c].hash = hash;
1020 all_files[c].type = type;
1021 all_files[c].pos = c;
1022 all_files[c].count = 1;
1023 all_files[c].magic = 0x0BADCAFE;
1025 file_descriptor_add(&all_files[c]);
1027 if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1033 // 1. read all files in /proc
1034 // 2. for each numeric directory:
1035 // i. read /proc/pid/stat
1036 // ii. read /proc/pid/statm
1037 // iii. read /proc/pid/io (requires root access)
1038 // iii. read the entries in directory /proc/pid/fd (requires root access)
1040 // a. find or create a struct file_descriptor
1041 // b. cleanup any old/unused file_descriptors
1043 // after all these, some pids may be linked to targets, while others may not
1045 // in case of errors, only 1 every 1000 errors is printed
1046 // to avoid filling up all disk space
1047 // if debug is enabled, all errors are printed
1049 int update_from_proc(void)
1051 static long count_errors = 0;
1053 char filename[FILENAME_MAX+1];
1054 char dirname[FILENAME_MAX + 1];
1056 snprintf(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1057 DIR *dir = opendir(dirname);
1060 struct dirent *file = NULL;
1061 struct pid_stat *p = NULL;
1063 // mark them all as un-updated
1065 for(p = root_of_pids; p ; p = p->next) {
1069 p->children_count = 0;
1074 while((file = readdir(dir))) {
1075 char *endptr = file->d_name;
1076 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1077 if(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0') continue;
1079 p = get_pid_entry(pid);
1082 // --------------------------------------------------------------------
1085 if(read_proc_pid_stat(p)) {
1086 if(!count_errors++ || debug || (p->target && p->target->debug))
1087 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1091 if(p->ppid < 0 || p->ppid > pid_max) p->ppid = 0;
1094 // --------------------------------------------------------------------
1095 // /proc/<pid>/statm
1097 if(read_proc_pid_statm(p)) {
1098 if(!count_errors++ || debug || (p->target && p->target->debug))
1099 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1105 // --------------------------------------------------------------------
1108 if(read_proc_pid_io(p)) {
1109 if(!count_errors++ || debug || (p->target && p->target->debug))
1110 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1112 // on systems without /proc/X/io
1113 // allow proceeding without I/O information
1117 // --------------------------------------------------------------------
1120 // check if it is target
1121 // we do this only once, the first time this pid is loaded
1123 if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
1126 for(w = target_root; w ; w = w->next) {
1127 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1129 if(strcmp(w->compare, p->comm) == 0) {
1130 if(w->target) p->target = w->target;
1133 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1138 // --------------------------------------------------------------------
1141 snprintf(filename, FILENAME_MAX, "%s/proc/%s/fd", host_prefix, file->d_name);
1142 DIR *fds = opendir(filename);
1146 char fdname[FILENAME_MAX + 1];
1147 char linkname[FILENAME_MAX + 1];
1149 // make the array negative
1150 for(c = 0 ; c < p->fds_size ; c++) p->fds[c] = -p->fds[c];
1152 while((de = readdir(fds))) {
1153 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1155 // check if the fds array is small
1156 int fdid = atoi(de->d_name);
1157 if(fdid < 0) continue;
1158 if(fdid >= p->fds_size) {
1159 // it is small, extend it
1160 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1161 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1163 error("Cannot re-allocate fds for %s", p->comm);
1167 // and initialize it
1168 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1169 p->fds_size = fdid + 100;
1172 if(p->fds[fdid] == 0) {
1173 // we don't know this fd, get it
1175 sprintf(fdname, "%s/proc/%s/fd/%s", host_prefix, file->d_name, de->d_name);
1176 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1178 if(debug || (p->target && p->target->debug)) {
1179 if(!count_errors++ || debug || (p->target && p->target->debug))
1180 error("Cannot read link %s", fdname);
1187 // if another process already has this, we will get
1189 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1192 // else make it positive again, we need it
1193 // of course, the actual file may have changed, but we don't care so much
1194 // FIXME: we could compare the inode as returned by readdir direct structure
1195 else p->fds[fdid] = -p->fds[fdid];
1199 // remove all the negative file descriptors
1200 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1201 file_descriptor_not_used(-p->fds[c]);
1206 // --------------------------------------------------------------------
1209 // mark it as updated
1212 if(count_errors > 1000) {
1213 error("%ld more errors encountered\n", count_errors - 1);
1223 // ----------------------------------------------------------------------------
1224 // update statistics on the targets
1226 // 1. link all childs to their parents
1227 // 2. go from bottom to top, marking as merged all childs to their parents
1228 // this step links all parents without a target to the child target, if any
1229 // 3. link all top level processes (the ones not merged) to the default target
1230 // 4. go from top to bottom, linking all childs without a target, to their parent target
1231 // after this step, all processes have a target
1232 // [5. for each killed pid (updated = 0), remove its usage from its target]
1233 // 6. zero all targets
1234 // 7. concentrate all values on the targets
1235 // 8. remove all killed processes
1236 // 9. find the unique file count for each target
1238 void update_statistics(void)
1241 struct pid_stat *p = NULL;
1244 // link all children to their parents
1245 // and update children count on parents
1246 for(p = root_of_pids; p ; p = p->next) {
1247 // for each process found running
1250 && p->ppid <= pid_max
1251 && all_pids[p->ppid]
1253 // for valid processes
1255 if(debug || (p->target && p->target->debug))
1256 fprintf(stderr, "apps.plugin: \tparent of %d (%s) is %d (%s)\n", p->pid, p->comm, p->ppid, all_pids[p->ppid]->comm);
1258 p->parent = all_pids[p->ppid];
1259 p->parent->children_count++;
1261 else if(p->ppid != 0)
1262 error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1266 // children that do not have a target
1267 // inherit their target from their parent
1271 for(p = root_of_pids; p ; p = p->next) {
1272 // if this process does not have a target
1273 // and it has a parent
1274 // and its parent has a target
1275 // then, set the parent's target to this process
1276 if(unlikely(!p->target && p->parent && p->parent->target)) {
1277 p->target = p->parent->target;
1280 if(debug || (p->target && p->target->debug))
1281 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);
1287 // find all the procs with 0 childs and merge them to their parents
1288 // repeat, until nothing more can be done.
1292 for(p = root_of_pids; p ; p = p->next) {
1293 // if this process does not have any children
1294 // and is not already merged
1296 // and its parent has children
1297 // and the target of this process and its parent is the same, or the parent does not have a target
1298 // and its parent is not init
1299 // then, mark them as merged.
1304 && p->parent->children_count
1305 && (p->target == p->parent->target || !p->parent->target)
1308 p->parent->children_count--;
1311 // the parent inherits the child's target, if it does not have a target itself
1312 if(unlikely(p->target && !p->parent->target)) {
1313 p->parent->target = p->target;
1315 if(debug || (p->target && p->target->debug))
1316 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);
1324 fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1327 // init goes always to default target
1329 all_pids[1]->target = default_target;
1331 // give a default target on all top level processes
1332 for(p = root_of_pids; p ; p = p->next) {
1333 // if the process is not merged itself
1334 // then is is a top level process
1335 if(!p->merged && !p->target)
1336 p->target = default_target;
1338 #ifdef INCLUDE_CHILDS
1339 // by the way, update the diffs
1340 // will be used later for subtracting killed process times
1341 p->diff_cutime = p->utime - p->cutime;
1342 p->diff_cstime = p->stime - p->cstime;
1343 p->diff_cminflt = p->minflt - p->cminflt;
1344 p->diff_cmajflt = p->majflt - p->cmajflt;
1348 // give a target to all merged child processes
1352 for(p = root_of_pids; p ; p = p->next) {
1353 if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1354 p->target = p->parent->target;
1357 if(debug || (p->target && p->target->debug))
1358 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);
1363 #ifdef INCLUDE_CHILDS
1364 // for each killed process, remove its values from the parents
1365 // sums (we had already added them in a previous loop)
1366 for(p = root_of_pids; p ; p = p->next) {
1367 if(p->updated) continue;
1369 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1371 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1372 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1373 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1374 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1376 struct pid_stat *t = p;
1377 while((t = t->parent)) {
1378 if(!t->updated) continue;
1380 unsigned long long x;
1381 if(diff_utime && t->diff_cutime) {
1382 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1384 t->diff_cutime -= x;
1386 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1388 if(diff_stime && t->diff_cstime) {
1389 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1391 t->diff_cstime -= x;
1393 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1395 if(diff_minflt && t->diff_cminflt) {
1396 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1398 t->diff_cminflt -= x;
1399 t->fix_cminflt += x;
1400 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1402 if(diff_majflt && t->diff_cmajflt) {
1403 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1405 t->diff_cmajflt -= x;
1406 t->fix_cmajflt += x;
1407 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1411 if(diff_utime) error("Cannot fix up utime %llu", diff_utime);
1412 if(diff_stime) error("Cannot fix up stime %llu", diff_stime);
1413 if(diff_minflt) error("Cannot fix up minflt %llu", diff_minflt);
1414 if(diff_majflt) error("Cannot fix up majflt %llu", diff_majflt);
1418 // zero all the targets
1421 for (w = target_root; w ; w = w->next) {
1424 w->fds = calloc(sizeof(int), (size_t) all_files_size);
1426 error("Cannot allocate memory for fds in %s", w->name);
1441 w->statm_resident = 0;
1448 w->io_logical_bytes_read = 0;
1449 w->io_logical_bytes_written = 0;
1450 w->io_read_calls = 0;
1451 w->io_write_calls = 0;
1452 w->io_storage_bytes_read = 0;
1453 w->io_storage_bytes_written = 0;
1454 w->io_cancelled_write_bytes = 0;
1457 #ifdef INCLUDE_CHILDS
1458 if(debug) walk_down(0, 1);
1461 // concentrate everything on the targets
1462 for(p = root_of_pids; p ; p = p->next) {
1464 error("pid %d %s was left without a target!", p->pid, p->comm);
1469 p->target->cutime += p->cutime; // - p->fix_cutime;
1470 p->target->cstime += p->cstime; // - p->fix_cstime;
1471 p->target->cminflt += p->cminflt; // - p->fix_cminflt;
1472 p->target->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1474 p->target->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1475 p->target->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1476 p->target->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1477 p->target->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1479 //if(p->num_threads < 0)
1480 // error("Negative threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1482 //if(p->num_threads > 10000)
1483 // error("Excessive threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1485 p->target->num_threads += p->num_threads;
1486 p->target->rss += p->rss;
1488 p->target->statm_size += p->statm_size;
1489 p->target->statm_resident += p->statm_resident;
1490 p->target->statm_share += p->statm_share;
1491 p->target->statm_text += p->statm_text;
1492 p->target->statm_lib += p->statm_lib;
1493 p->target->statm_data += p->statm_data;
1494 p->target->statm_dirty += p->statm_dirty;
1496 p->target->io_logical_bytes_read += p->io_logical_bytes_read;
1497 p->target->io_logical_bytes_written += p->io_logical_bytes_written;
1498 p->target->io_read_calls += p->io_read_calls;
1499 p->target->io_write_calls += p->io_write_calls;
1500 p->target->io_storage_bytes_read += p->io_storage_bytes_read;
1501 p->target->io_storage_bytes_written += p->io_storage_bytes_written;
1502 p->target->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1504 p->target->processes++;
1506 for(c = 0; c < p->fds_size ;c++) {
1507 if(p->fds[c] == 0) continue;
1508 if(p->fds[c] < all_files_size) {
1509 if(p->target->fds) p->target->fds[p->fds[c]]++;
1512 error("Invalid fd number %d", p->fds[c]);
1515 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);
1517 /* 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);
1518 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);
1519 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);
1520 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);
1521 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);
1522 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);
1523 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);
1524 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);
1526 #ifdef INCLUDE_CHILDS
1527 p->old_utime = p->utime;
1528 p->old_cutime = p->cutime;
1529 p->old_stime = p->stime;
1530 p->old_cstime = p->cstime;
1531 p->old_minflt = p->minflt;
1532 p->old_majflt = p->majflt;
1533 p->old_cminflt = p->cminflt;
1534 p->old_cmajflt = p->cmajflt;
1538 // since the process has exited, the user
1539 // will see a drop in our charts, because the incremental
1540 // values of this process will not be there
1542 // add them to the fix_* values and they will be added to
1543 // the reported values, so that the report goes steady
1544 p->target->fix_minflt += p->minflt;
1545 p->target->fix_majflt += p->majflt;
1546 p->target->fix_utime += p->utime;
1547 p->target->fix_stime += p->stime;
1548 p->target->fix_cminflt += p->cminflt;
1549 p->target->fix_cmajflt += p->cmajflt;
1550 p->target->fix_cutime += p->cutime;
1551 p->target->fix_cstime += p->cstime;
1553 p->target->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1554 p->target->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1555 p->target->fix_io_read_calls += p->io_read_calls;
1556 p->target->fix_io_write_calls += p->io_write_calls;
1557 p->target->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1558 p->target->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1559 p->target->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1563 // fprintf(stderr, "\n");
1564 // cleanup all un-updated processed (exited, killed, etc)
1565 for(p = root_of_pids; p ;) {
1567 // 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);
1569 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1570 file_descriptor_not_used(p->fds[c]);
1581 for (w = target_root; w ; w = w->next) {
1585 w->openinotifies = 0;
1586 w->openeventfds = 0;
1587 w->opentimerfds = 0;
1588 w->opensignalfds = 0;
1589 w->openeventpolls = 0;
1592 for(c = 1; c < all_files_size ;c++) {
1593 if(w->fds && w->fds[c] > 0) switch(all_files[c].type) {
1602 case FILETYPE_SOCKET:
1606 case FILETYPE_INOTIFY:
1610 case FILETYPE_EVENTFD:
1614 case FILETYPE_TIMERFD:
1618 case FILETYPE_SIGNALFD:
1622 case FILETYPE_EVENTPOLL:
1623 w->openeventpolls++;
1636 // ----------------------------------------------------------------------------
1637 // update chart dimensions
1639 void show_dimensions(void)
1641 static struct timeval last = { 0, 0 };
1642 static struct rusage me_last;
1648 unsigned long long usec;
1649 unsigned long long cpuuser;
1650 unsigned long long cpusyst;
1653 gettimeofday(&last, NULL);
1654 getrusage(RUSAGE_SELF, &me_last);
1656 // the first time, give a zero to allow
1657 // netdata calibrate to the current time
1658 // usec = update_every * 1000000ULL;
1664 gettimeofday(&now, NULL);
1665 getrusage(RUSAGE_SELF, &me);
1667 usec = usecdiff(&now, &last);
1668 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
1669 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
1671 bcopy(&now, &last, sizeof(struct timeval));
1672 bcopy(&me, &me_last, sizeof(struct rusage));
1675 fprintf(stdout, "BEGIN apps.cpu %llu\n", usec);
1676 for (w = target_root; w ; w = w->next) {
1677 if(w->target || (!w->processes && !w->exposed)) continue;
1679 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
1681 fprintf(stdout, "END\n");
1683 fprintf(stdout, "BEGIN apps.cpu_user %llu\n", usec);
1684 for (w = target_root; w ; w = w->next) {
1685 if(w->target || (!w->processes && !w->exposed)) continue;
1687 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
1689 fprintf(stdout, "END\n");
1691 fprintf(stdout, "BEGIN apps.cpu_system %llu\n", usec);
1692 for (w = target_root; w ; w = w->next) {
1693 if(w->target || (!w->processes && !w->exposed)) continue;
1695 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
1697 fprintf(stdout, "END\n");
1699 fprintf(stdout, "BEGIN apps.threads %llu\n", usec);
1700 for (w = target_root; w ; w = w->next) {
1701 if(w->target || (!w->processes && !w->exposed)) continue;
1703 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
1705 fprintf(stdout, "END\n");
1707 fprintf(stdout, "BEGIN apps.processes %llu\n", usec);
1708 for (w = target_root; w ; w = w->next) {
1709 if(w->target || (!w->processes && !w->exposed)) continue;
1711 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
1713 fprintf(stdout, "END\n");
1715 fprintf(stdout, "BEGIN apps.mem %llu\n", usec);
1716 for (w = target_root; w ; w = w->next) {
1717 if(w->target || (!w->processes && !w->exposed)) continue;
1719 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
1721 fprintf(stdout, "END\n");
1723 fprintf(stdout, "BEGIN apps.minor_faults %llu\n", usec);
1724 for (w = target_root; w ; w = w->next) {
1725 if(w->target || (!w->processes && !w->exposed)) continue;
1727 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt);
1729 fprintf(stdout, "END\n");
1731 fprintf(stdout, "BEGIN apps.major_faults %llu\n", usec);
1732 for (w = target_root; w ; w = w->next) {
1733 if(w->target || (!w->processes && !w->exposed)) continue;
1735 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt);
1737 fprintf(stdout, "END\n");
1739 fprintf(stdout, "BEGIN apps.lreads %llu\n", usec);
1740 for (w = target_root; w ; w = w->next) {
1741 if(w->target || (!w->processes && !w->exposed)) continue;
1743 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read);
1745 fprintf(stdout, "END\n");
1747 fprintf(stdout, "BEGIN apps.lwrites %llu\n", usec);
1748 for (w = target_root; w ; w = w->next) {
1749 if(w->target || (!w->processes && !w->exposed)) continue;
1751 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written);
1753 fprintf(stdout, "END\n");
1755 fprintf(stdout, "BEGIN apps.preads %llu\n", usec);
1756 for (w = target_root; w ; w = w->next) {
1757 if(w->target || (!w->processes && !w->exposed)) continue;
1759 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read);
1761 fprintf(stdout, "END\n");
1763 fprintf(stdout, "BEGIN apps.pwrites %llu\n", usec);
1764 for (w = target_root; w ; w = w->next) {
1765 if(w->target || (!w->processes && !w->exposed)) continue;
1767 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written);
1769 fprintf(stdout, "END\n");
1771 fprintf(stdout, "BEGIN apps.files %llu\n", usec);
1772 for (w = target_root; w ; w = w->next) {
1773 if(w->target || (!w->processes && !w->exposed)) continue;
1775 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
1777 fprintf(stdout, "END\n");
1779 fprintf(stdout, "BEGIN apps.sockets %llu\n", usec);
1780 for (w = target_root; w ; w = w->next) {
1781 if(w->target || (!w->processes && !w->exposed)) continue;
1783 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
1785 fprintf(stdout, "END\n");
1787 fprintf(stdout, "BEGIN apps.pipes %llu\n", usec);
1788 for (w = target_root; w ; w = w->next) {
1789 if(w->target || (!w->processes && !w->exposed)) continue;
1791 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
1793 fprintf(stdout, "END\n");
1795 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
1796 fprintf(stdout, "SET user = %llu\n", cpuuser);
1797 fprintf(stdout, "SET system = %llu\n", cpusyst);
1798 fprintf(stdout, "END\n");
1800 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
1801 fprintf(stdout, "SET files = %llu\n", file_counter);
1802 fprintf(stdout, "SET pids = %ld\n", all_pids_count);
1803 fprintf(stdout, "SET fds = %d\n", all_files_len);
1804 fprintf(stdout, "SET targets = %ld\n", targets);
1805 fprintf(stdout, "END\n");
1811 // ----------------------------------------------------------------------------
1812 // generate the charts
1814 void show_charts(void)
1817 int newly_added = 0;
1819 for(w = target_root ; w ; w = w->next)
1820 if(!w->exposed && w->processes) {
1823 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
1826 // nothing more to show
1827 if(!newly_added) return;
1829 // we have something new to show
1830 // update the charts
1831 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);
1832 for (w = target_root; w ; w = w->next) {
1833 if(w->target || (!w->processes && !w->exposed)) continue;
1835 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu %s\n", w->name, Hertz, w->hidden ? "hidden,noreset" : "noreset");
1838 fprintf(stdout, "CHART apps.mem '' 'Apps Dedicated Memory (w/o shared)' 'MB' mem apps.mem stacked 20003 %d\n", update_every);
1839 for (w = target_root; w ; w = w->next) {
1840 if(w->target || (!w->processes && !w->exposed)) continue;
1842 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld noreset\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
1845 fprintf(stdout, "CHART apps.threads '' 'Apps Threads' 'threads' processes apps.threads stacked 20005 %d\n", update_every);
1846 for (w = target_root; w ; w = w->next) {
1847 if(w->target || (!w->processes && !w->exposed)) continue;
1849 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1852 fprintf(stdout, "CHART apps.processes '' 'Apps Processes' 'processes' processes apps.processes stacked 20004 %d\n", update_every);
1853 for (w = target_root; w ; w = w->next) {
1854 if(w->target || (!w->processes && !w->exposed)) continue;
1856 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1859 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);
1860 for (w = target_root; w ; w = w->next) {
1861 if(w->target || (!w->processes && !w->exposed)) continue;
1863 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
1866 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);
1867 for (w = target_root; w ; w = w->next) {
1868 if(w->target || (!w->processes && !w->exposed)) continue;
1870 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
1873 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);
1874 for (w = target_root; w ; w = w->next) {
1875 if(w->target || (!w->processes && !w->exposed)) continue;
1877 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
1880 fprintf(stdout, "CHART apps.minor_faults '' 'Apps Minor Page Faults' 'page faults/s' mem apps.minor_faults stacked 20011 %d\n", update_every);
1881 for (w = target_root; w ; w = w->next) {
1882 if(w->target || (!w->processes && !w->exposed)) continue;
1884 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
1887 fprintf(stdout, "CHART apps.lreads '' 'Apps Disk Logical Reads' 'kilobytes/s' disk apps.lreads stacked 20042 %d\n", update_every);
1888 for (w = target_root; w ; w = w->next) {
1889 if(w->target || (!w->processes && !w->exposed)) continue;
1891 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1894 fprintf(stdout, "CHART apps.lwrites '' 'Apps I/O Logical Writes' 'kilobytes/s' disk apps.lwrites stacked 20042 %d\n", update_every);
1895 for (w = target_root; w ; w = w->next) {
1896 if(w->target || (!w->processes && !w->exposed)) continue;
1898 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1901 fprintf(stdout, "CHART apps.preads '' 'Apps Disk Reads' 'kilobytes/s' disk apps.preads stacked 20002 %d\n", update_every);
1902 for (w = target_root; w ; w = w->next) {
1903 if(w->target || (!w->processes && !w->exposed)) continue;
1905 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1908 fprintf(stdout, "CHART apps.pwrites '' 'Apps Disk Writes' 'kilobytes/s' disk apps.pwrites stacked 20002 %d\n", update_every);
1909 for (w = target_root; w ; w = w->next) {
1910 if(w->target || (!w->processes && !w->exposed)) continue;
1912 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
1915 fprintf(stdout, "CHART apps.files '' 'Apps Open Files' 'open files' disk apps.files stacked 20050 %d\n", update_every);
1916 for (w = target_root; w ; w = w->next) {
1917 if(w->target || (!w->processes && !w->exposed)) continue;
1919 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1922 fprintf(stdout, "CHART apps.sockets '' 'Apps Open Sockets' 'open sockets' net apps.sockets stacked 20051 %d\n", update_every);
1923 for (w = target_root; w ; w = w->next) {
1924 if(w->target || (!w->processes && !w->exposed)) continue;
1926 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1929 fprintf(stdout, "CHART apps.pipes '' 'Apps Pipes' 'open pipes' processes apps.pipes stacked 20053 %d\n", update_every);
1930 for (w = target_root; w ; w = w->next) {
1931 if(w->target || (!w->processes && !w->exposed)) continue;
1933 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
1936 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %d\n", update_every);
1937 fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000);
1938 fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000);
1940 fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %d\n", update_every);
1941 fprintf(stdout, "DIMENSION files '' incremental 1 1\n");
1942 fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
1943 fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
1944 fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
1950 // ----------------------------------------------------------------------------
1951 // parse command line arguments
1953 void parse_args(int argc, char **argv)
1958 for(i = 1; i < argc; i++) {
1960 int n = atoi(argv[i]);
1967 if(strcmp("debug", argv[i]) == 0) {
1969 debug_flags = 0xffffffff;
1978 error("Cannot understand option %s", argv[i]);
1982 if(freq > 0) update_every = freq;
1983 if(!name) name = "groups";
1985 if(read_process_groups(name)) {
1986 error("Cannot read process groups %s", name);
1991 int main(int argc, char **argv)
1993 // debug_flags = D_PROCFILE;
1995 // set the name for logging
1996 program_name = "apps.plugin";
1998 host_prefix = getenv("NETDATA_HOST_PREFIX");
1999 if(host_prefix == NULL) {
2000 info("NETDATA_HOST_PREFIX is not passed from netdata");
2003 else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2005 config_dir = getenv("NETDATA_CONFIG_DIR");
2006 if(config_dir == NULL) {
2007 info("NETDATA_CONFIG_DIR is not passed from netdata");
2008 config_dir = CONFIG_DIR;
2010 else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2012 info("starting...");
2014 procfile_adaptive_initial_allocation = 1;
2016 time_t started_t = time(NULL);
2018 Hertz = get_hertz();
2019 pid_max = get_pid_max();
2020 processors = get_processors();
2022 parse_args(argc, argv);
2024 all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2026 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2027 printf("DISABLE\n");
2031 unsigned long long counter = 1;
2032 unsigned long long usec = 0, susec = 0;
2033 struct timeval last, now;
2034 gettimeofday(&last, NULL);
2036 for(;1; counter++) {
2037 if(!update_from_proc()) {
2038 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2039 printf("DISABLE\n");
2043 update_statistics();
2044 show_charts(); // this is smart enough to show only newly added apps, when needed
2047 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
2050 gettimeofday(&now, NULL);
2051 usec = usecdiff(&now, &last) - susec;
2052 if(debug) fprintf(stderr, "apps.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).\n", usec + susec, usec, susec);
2054 // if the last loop took less than half the time
2055 // wait the rest of the time
2056 if(usec < (update_every * 1000000ULL / 2)) susec = (update_every * 1000000ULL) - usec;
2057 else susec = update_every * 1000000ULL / 2;
2059 usleep((useconds_t) susec);
2060 bcopy(&now, &last, sizeof(struct timeval));
2062 // restart once per day (14400 seconds)
2063 current_t = time(NULL);
2064 if(current_t - started_t > 14400) exit(0);