1 #define __STDC_FORMAT_MACROS
13 #include <sys/resource.h>
25 #include <arpa/inet.h>
27 #include "../common.h"
30 #include "../procfile.h"
32 #define MAX_COMPARE_NAME 15
35 unsigned long long Hertz = 1;
42 unsigned long long file_counter = 0;
44 #define PROC_BUFFER 4096
46 // ----------------------------------------------------------------------------
53 } allocations = { 0, 0, 0 };
55 #define MALLOC_MARK (uint32_t)(0x0BADCAFE)
56 #define MALLOC_PREFIX (sizeof(uint32_t) * 2)
57 #define MALLOC_SUFFIX (sizeof(uint32_t))
58 #define MALLOC_OVERHEAD (MALLOC_PREFIX + MALLOC_SUFFIX)
60 void *mark_allocation(void *allocated_ptr, size_t size_without_overheads) {
61 uint32_t *real_ptr = (uint32_t *)allocated_ptr;
62 real_ptr[0] = MALLOC_MARK;
63 real_ptr[1] = size_without_overheads;
65 uint32_t *end_ptr = (uint32_t *)(allocated_ptr + MALLOC_PREFIX + size_without_overheads);
66 end_ptr[0] = MALLOC_MARK;
68 // fprintf(stderr, "MEMORY_POINTER: Allocated at %p, returning %p.\n", allocated_ptr, (void *)(allocated_ptr + MALLOC_PREFIX));
70 return (void *)(allocated_ptr + MALLOC_PREFIX);
73 void *check_allocation(const char *file, int line, const char *function, void *marked_ptr, size_t *size_without_overheads_ptr) {
74 uint32_t *real_ptr = (uint32_t *)(marked_ptr - MALLOC_PREFIX);
76 // 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);
78 if(real_ptr[0] != MALLOC_MARK) fatal("MEMORY: prefix MARK is not valid for %s/%u@%s.", function, line, file);
80 size_t size = real_ptr[1];
82 uint32_t *end_ptr = (uint32_t *)(marked_ptr + size);
83 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);
85 if(size_without_overheads_ptr) *size_without_overheads_ptr = size;
90 void *malloc_debug(const char *file, int line, const char *function, size_t size) {
91 void *ptr = malloc(size + MALLOC_OVERHEAD);
92 if(!ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
94 allocations.allocated += size;
95 allocations.allocations++;
97 debug(D_MEMORY, "MEMORY: Allocated %zu bytes for %s/%u@%s."
98 " Status: allocated %zu in %zu allocs."
100 , function, line, file
101 , allocations.allocated
102 , allocations.allocations
105 if(allocations.allocated > allocations.allocated_max) {
106 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
107 allocations.allocated_max = allocations.allocated;
111 check_allocation(file, line, function, mark_allocation(ptr, size), &csize);
113 fatal("Invalid size.");
116 return mark_allocation(ptr, size);
119 void *calloc_debug(const char *file, int line, const char *function, size_t nmemb, size_t size) {
120 void *ptr = malloc_debug(file, line, function, (nmemb * size));
121 bzero(ptr, nmemb * size);
125 void free_debug(const char *file, int line, const char *function, void *ptr) {
127 void *real_ptr = check_allocation(file, line, function, ptr, &size);
129 bzero(real_ptr, size + MALLOC_OVERHEAD);
132 allocations.allocated -= size;
133 allocations.allocations--;
135 debug(D_MEMORY, "MEMORY: freed %zu bytes for %s/%u@%s."
136 " Status: allocated %zu in %zu allocs."
138 , function, line, file
139 , allocations.allocated
140 , allocations.allocations
144 void *realloc_debug(const char *file, int line, const char *function, void *ptr, size_t size) {
145 if(!ptr) return malloc_debug(file, line, function, size);
146 if(!size) { free_debug(file, line, function, ptr); return NULL; }
149 void *real_ptr = check_allocation(file, line, function, ptr, &old_size);
151 void *new_ptr = realloc(real_ptr, size + MALLOC_OVERHEAD);
152 if(!new_ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
154 allocations.allocated += size;
155 allocations.allocated -= old_size;
157 debug(D_MEMORY, "MEMORY: Re-allocated from %zu to %zu bytes for %s/%u@%s."
158 " Status: allocated %z in %zu allocs."
160 , function, line, file
161 , allocations.allocated
162 , allocations.allocations
165 if(allocations.allocated > allocations.allocated_max) {
166 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
167 allocations.allocated_max = allocations.allocated;
170 return mark_allocation(new_ptr, size);
173 char *strdup_debug(const char *file, int line, const char *function, const char *ptr) {
180 char *p = malloc_debug(file, line, function, size);
181 if(!p) fatal("Cannot allocate %zu bytes.", size);
183 memcpy(p, ptr, size);
187 #define malloc(size) malloc_debug(__FILE__, __LINE__, __FUNCTION__, (size))
188 #define calloc(nmemb, size) calloc_debug(__FILE__, __LINE__, __FUNCTION__, (nmemb), (size))
189 #define realloc(ptr, size) realloc_debug(__FILE__, __LINE__, __FUNCTION__, (ptr), (size))
190 #define free(ptr) free_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
195 #define strdup(ptr) strdup_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
197 // ----------------------------------------------------------------------------
200 long get_processors(void) {
203 procfile *ff = procfile_open("/proc/stat", "");
206 ff = procfile_readall(ff);
213 for(i = 0; i < procfile_lines(ff); i++) {
214 if(!procfile_linewords(ff, i)) continue;
216 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
219 if(processors < 1) processors = 1;
225 long get_pid_max(void) {
228 procfile *ff = procfile_open("/proc/sys/kernel/pid_max", "");
231 ff = procfile_readall(ff);
237 mpid = atol(procfile_lineword(ff, 0, 0));
238 if(mpid) mpid = 32768;
244 unsigned long long get_hertz(void)
246 unsigned long long hz = 1;
249 if((hz = sysconf(_SC_CLK_TCK)) > 0) {
255 hz = (unsigned long long)HZ; /* <asm/param.h> */
257 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
258 hz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
261 error("apps.plugin: ERROR: unknown HZ value. Assuming %llu.", hz);
266 // ----------------------------------------------------------------------------
268 // target is the point to aggregate a process tree values
271 char compare[MAX_COMPARE_NAME + 1];
272 char id[MAX_NAME + 1];
273 char name[MAX_NAME + 1];
275 unsigned long long minflt;
276 unsigned long long cminflt;
277 unsigned long long majflt;
278 unsigned long long cmajflt;
279 unsigned long long utime;
280 unsigned long long stime;
281 unsigned long long cutime;
282 unsigned long long cstime;
283 unsigned long long num_threads;
284 unsigned long long rss;
286 unsigned long long fix_minflt;
287 unsigned long long fix_cminflt;
288 unsigned long long fix_majflt;
289 unsigned long long fix_cmajflt;
290 unsigned long long fix_utime;
291 unsigned long long fix_stime;
292 unsigned long long fix_cutime;
293 unsigned long long fix_cstime;
295 unsigned long long statm_size;
296 unsigned long long statm_resident;
297 unsigned long long statm_share;
298 unsigned long long statm_text;
299 unsigned long long statm_lib;
300 unsigned long long statm_data;
301 unsigned long long statm_dirty;
303 unsigned long long io_logical_bytes_read;
304 unsigned long long io_logical_bytes_written;
305 unsigned long long io_read_calls;
306 unsigned long long io_write_calls;
307 unsigned long long io_storage_bytes_read;
308 unsigned long long io_storage_bytes_written;
309 unsigned long long io_cancelled_write_bytes;
311 unsigned long long fix_io_logical_bytes_read;
312 unsigned long long fix_io_logical_bytes_written;
313 unsigned long long fix_io_read_calls;
314 unsigned long long fix_io_write_calls;
315 unsigned long long fix_io_storage_bytes_read;
316 unsigned long long fix_io_storage_bytes_written;
317 unsigned long long fix_io_cancelled_write_bytes;
320 unsigned long long openfiles;
321 unsigned long long openpipes;
322 unsigned long long opensockets;
323 unsigned long long openinotifies;
324 unsigned long long openeventfds;
325 unsigned long long opentimerfds;
326 unsigned long long opensignalfds;
327 unsigned long long openeventpolls;
328 unsigned long long openother;
330 unsigned long processes; // how many processes have been merged to this
331 int exposed; // if set, we have sent this to netdata
332 int hidden; // if set, we set the hidden flag on the dimension
335 struct target *target; // the one that will be reported to netdata
337 } *target_root = NULL, *default_target = NULL;
341 // find or create a target
342 // there are targets that are just agregated to other target (the second argument)
343 struct target *get_target(const char *id, struct target *target)
345 const char *nid = id;
346 if(nid[0] == '-') nid++;
349 for(w = target_root ; w ; w = w->next)
350 if(strncmp(nid, w->id, MAX_NAME) == 0) return w;
352 w = calloc(sizeof(struct target), 1);
354 error("apps.plugin: cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
358 strncpy(w->id, nid, MAX_NAME);
359 strncpy(w->name, nid, MAX_NAME);
360 strncpy(w->compare, nid, MAX_COMPARE_NAME);
361 if(id[0] == '-') w->hidden = 1;
365 w->next = target_root;
368 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:"");
373 // read the process groups file
374 int read_process_groups(const char *name)
377 char filename[FILENAME_MAX + 1];
379 snprintf(filename, FILENAME_MAX, "%s/apps_%s.conf", CONFIG_DIR, name);
381 if(debug) fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
382 FILE *fp = fopen(filename, "r");
384 error("apps.plugin: ERROR: cannot open file '%s' (%s)", filename, strerror(errno));
389 while(fgets(buffer, 4096, fp) != NULL) {
390 int whidden = 0, wdebug = 0;
393 // if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", buffer);
395 char *s = buffer, *t, *p;
397 if(!s || !*s || *s == '#') continue;
399 if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", s);
404 if(!t || !*t) continue;
426 if(debug) fprintf(stderr, "apps.plugin: \t\ttarget %s\n", t);
428 struct target *w = NULL;
432 while((p = strsep(&s, " "))) {
434 if(!p || !*p) continue;
436 struct target *n = get_target(p, w);
444 if(w) strncpy(w->name, t, MAX_NAME);
445 if(!count) error("apps.plugin: ERROR: the line %ld on file '%s', for group '%s' does not state any process names.", line, filename, t);
449 default_target = get_target("+p!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
450 strncpy(default_target->name, "other", MAX_NAME);
456 // ----------------------------------------------------------------------------
457 // data to store for each pid
462 char comm[MAX_COMPARE_NAME + 1];
470 unsigned long long minflt;
471 unsigned long long cminflt;
472 unsigned long long majflt;
473 unsigned long long cmajflt;
474 unsigned long long utime;
475 unsigned long long stime;
476 unsigned long long cutime;
477 unsigned long long cstime;
481 // int64_t itrealvalue;
482 // unsigned long long starttime;
483 // unsigned long long vsize;
484 unsigned long long rss;
485 // unsigned long long rsslim;
486 // unsigned long long starcode;
487 // unsigned long long endcode;
488 // unsigned long long startstack;
489 // unsigned long long kstkesp;
490 // unsigned long long kstkeip;
493 // uint64_t sigignore;
494 // uint64_t sigcatch;
498 // int32_t exit_signal;
499 // int32_t processor;
500 // uint32_t rt_priority;
502 // unsigned long long delayacct_blkio_ticks;
503 // uint64_t guest_time;
504 // int64_t cguest_time;
506 unsigned long long statm_size;
507 unsigned long long statm_resident;
508 unsigned long long statm_share;
509 unsigned long long statm_text;
510 unsigned long long statm_lib;
511 unsigned long long statm_data;
512 unsigned long long statm_dirty;
514 unsigned long long io_logical_bytes_read;
515 unsigned long long io_logical_bytes_written;
516 unsigned long long io_read_calls;
517 unsigned long long io_write_calls;
518 unsigned long long io_storage_bytes_read;
519 unsigned long long io_storage_bytes_written;
520 unsigned long long io_cancelled_write_bytes;
522 #ifdef INCLUDE_CHILDS
523 unsigned long long old_utime;
524 unsigned long long old_stime;
525 unsigned long long old_minflt;
526 unsigned long long old_majflt;
528 unsigned long long old_cutime;
529 unsigned long long old_cstime;
530 unsigned long long old_cminflt;
531 unsigned long long old_cmajflt;
533 unsigned long long fix_cutime;
534 unsigned long long fix_cstime;
535 unsigned long long fix_cminflt;
536 unsigned long long fix_cmajflt;
538 unsigned long long diff_cutime;
539 unsigned long long diff_cstime;
540 unsigned long long diff_cminflt;
541 unsigned long long diff_cmajflt;
544 int *fds; // array of fds it uses
545 int fds_size; // the size of the fds array
547 int childs; // number of processes directly referencing this
548 int updated; // 1 when update
549 int merged; // 1 when it has been merged to its parent
551 struct target *target;
552 struct pid_stat *parent;
553 struct pid_stat *prev;
554 struct pid_stat *next;
555 } *root_of_pids = NULL, **all_pids;
557 long all_pids_count = 0;
559 struct pid_stat *get_pid_entry(pid_t pid)
562 all_pids[pid]->new_entry = 0;
563 return all_pids[pid];
566 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
568 error("apps.plugin: ERROR: Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct pid_stat));
572 all_pids[pid]->fds = calloc(sizeof(int), 100);
573 if(!all_pids[pid]->fds)
574 error("apps.plugin: ERROR: Cannot allocate %ld bytes of memory", (unsigned long)(sizeof(int) * 100));
575 else all_pids[pid]->fds_size = 100;
577 if(root_of_pids) root_of_pids->prev = all_pids[pid];
578 all_pids[pid]->next = root_of_pids;
579 root_of_pids = all_pids[pid];
581 all_pids[pid]->pid = pid;
582 all_pids[pid]->new_entry = 1;
584 return all_pids[pid];
587 void del_pid_entry(pid_t pid)
589 if(!all_pids[pid]) return;
591 if(debug) fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
593 if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
594 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
595 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
597 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
599 all_pids[pid] = NULL;
603 // ----------------------------------------------------------------------------
604 // update pids from proc
606 int read_proc_pid_stat(struct pid_stat *p) {
607 char filename[FILENAME_MAX + 1];
609 snprintf(filename, FILENAME_MAX, "/proc/%d/stat", p->pid);
611 procfile *ff = procfile_open(filename, "");
614 ff = procfile_readall(ff);
622 int len = strlen(procfile_lineword(ff, 0, 1));
624 error("apps.plugin: File %s states too small command name '%s'. Ignoring file.", filename, procfile_lineword(ff, 0, 1));
629 len -= 2; // remove ()
630 if(len > MAX_COMPARE_NAME) len = MAX_COMPARE_NAME;
631 strncpy(p->comm, &(procfile_lineword(ff, 0, 1))[1], len);
633 // p->pid = atol(procfile_lineword(ff, 0, 0));
635 // p->state = *(procfile_lineword(ff, 0, 2));
636 p->ppid = atol(procfile_lineword(ff, 0, 3));
637 // p->pgrp = atol(procfile_lineword(ff, 0, 4));
638 // p->session = atol(procfile_lineword(ff, 0, 5));
639 // p->tty_nr = atol(procfile_lineword(ff, 0, 6));
640 // p->tpgid = atol(procfile_lineword(ff, 0, 7));
641 // p->flags = strtoull(procfile_lineword(ff, 0, 8), NULL, 10);
642 p->minflt = strtoull(procfile_lineword(ff, 0, 9), NULL, 10);
643 p->cminflt = strtoull(procfile_lineword(ff, 0, 10), NULL, 10);
644 p->majflt = strtoull(procfile_lineword(ff, 0, 11), NULL, 10);
645 p->cmajflt = strtoull(procfile_lineword(ff, 0, 12), NULL, 10);
646 p->utime = strtoull(procfile_lineword(ff, 0, 13), NULL, 10);
647 p->stime = strtoull(procfile_lineword(ff, 0, 14), NULL, 10);
648 p->cutime = strtoull(procfile_lineword(ff, 0, 15), NULL, 10);
649 p->cstime = strtoull(procfile_lineword(ff, 0, 16), NULL, 10);
650 // p->priority = strtoull(procfile_lineword(ff, 0, 17), NULL, 10);
651 // p->nice = strtoull(procfile_lineword(ff, 0, 18), NULL, 10);
652 p->num_threads = atol(procfile_lineword(ff, 0, 19));
653 // p->itrealvalue = strtoull(procfile_lineword(ff, 0, 20), NULL, 10);
654 // p->starttime = strtoull(procfile_lineword(ff, 0, 21), NULL, 10);
655 // p->vsize = strtoull(procfile_lineword(ff, 0, 22), NULL, 10);
656 p->rss = strtoull(procfile_lineword(ff, 0, 23), NULL, 10);
657 // p->rsslim = strtoull(procfile_lineword(ff, 0, 24), NULL, 10);
658 // p->starcode = strtoull(procfile_lineword(ff, 0, 25), NULL, 10);
659 // p->endcode = strtoull(procfile_lineword(ff, 0, 26), NULL, 10);
660 // p->startstack = strtoull(procfile_lineword(ff, 0, 27), NULL, 10);
661 // p->kstkesp = strtoull(procfile_lineword(ff, 0, 28), NULL, 10);
662 // p->kstkeip = strtoull(procfile_lineword(ff, 0, 29), NULL, 10);
663 // p->signal = strtoull(procfile_lineword(ff, 0, 30), NULL, 10);
664 // p->blocked = strtoull(procfile_lineword(ff, 0, 31), NULL, 10);
665 // p->sigignore = strtoull(procfile_lineword(ff, 0, 32), NULL, 10);
666 // p->sigcatch = strtoull(procfile_lineword(ff, 0, 33), NULL, 10);
667 // p->wchan = strtoull(procfile_lineword(ff, 0, 34), NULL, 10);
668 // p->nswap = strtoull(procfile_lineword(ff, 0, 35), NULL, 10);
669 // p->cnswap = strtoull(procfile_lineword(ff, 0, 36), NULL, 10);
670 // p->exit_signal = atol(procfile_lineword(ff, 0, 37));
671 // p->processor = atol(procfile_lineword(ff, 0, 38));
672 // p->rt_priority = strtoul(procfile_lineword(ff, 0, 39), NULL, 10);
673 // p->policy = strtoul(procfile_lineword(ff, 0, 40), NULL, 10);
674 // p->delayacct_blkio_ticks = strtoull(procfile_lineword(ff, 0, 41), NULL, 10);
675 // p->guest_time = strtoull(procfile_lineword(ff, 0, 42), NULL, 10);
676 // p->cguest_time = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
678 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);
684 int read_proc_pid_statm(struct pid_stat *p) {
685 char filename[FILENAME_MAX + 1];
687 snprintf(filename, FILENAME_MAX, "/proc/%d/statm", p->pid);
689 procfile *ff = procfile_open(filename, "");
692 ff = procfile_readall(ff);
700 p->statm_size = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
701 p->statm_resident = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
702 p->statm_share = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
703 p->statm_text = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
704 p->statm_lib = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
705 p->statm_data = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
706 p->statm_dirty = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
712 int read_proc_pid_io(struct pid_stat *p) {
713 char filename[FILENAME_MAX + 1];
715 snprintf(filename, FILENAME_MAX, "/proc/%d/io", p->pid);
717 procfile *ff = procfile_open(filename, ":");
720 ff = procfile_readall(ff);
728 p->io_logical_bytes_read = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
729 p->io_logical_bytes_written = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
730 p->io_read_calls = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
731 p->io_write_calls = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
732 p->io_storage_bytes_read = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
733 p->io_storage_bytes_written = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
734 p->io_cancelled_write_bytes = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
741 // ----------------------------------------------------------------------------
743 #ifdef INCLUDE_CHILDS
744 // print a tree view of all processes
745 int walk_down(pid_t pid, int level) {
746 struct pid_stat *p = NULL;
750 for(i = 0; i < level; i++) b[i] = '\t';
755 for(p = root_of_pids; p ; p = p->next) {
757 ret += walk_down(p->pid, level+1);
763 if(!p->updated) ret += 1;
764 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"
765 , b, p->comm, p->pid, p->updated?"OK":"KILLED", p->target->name, p->childs
766 , p->utime, p->utime - p->old_utime
767 , p->stime, p->stime - p->old_stime
768 , p->cutime, p->cutime - p->old_cutime
769 , p->cstime, p->cstime - p->old_cstime
770 , p->minflt, p->minflt - p->old_minflt
771 , p->majflt, p->majflt - p->old_majflt
772 , p->cminflt, p->cminflt - p->old_cminflt
773 , p->cmajflt, p->cmajflt - p->old_cmajflt
782 // ----------------------------------------------------------------------------
784 // this is used to keep a global list of all open files of the system
785 // it is needed in order to figure out the unique files a process tree has open
787 #define FILE_DESCRIPTORS_INCREASE_STEP 100
789 struct file_descriptor {
799 int all_files_len = 0;
800 int all_files_size = 0;
802 int file_descriptor_compare(void* a, void* b) {
803 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
804 error("Corrupted index data detected. Please report this.");
806 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
808 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
810 else return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
812 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
814 avl_tree all_files_index = {
816 file_descriptor_compare
819 static struct file_descriptor *file_descriptor_find(const char *name, unsigned long hash) {
820 struct file_descriptor *result = NULL, tmp;
821 tmp.hash = (hash)?hash:simple_hash(name);
825 tmp.magic = 0x0BADCAFE;
827 avl_search(&all_files_index, (avl *)&tmp, file_descriptor_iterator, (avl **)&result);
831 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
832 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
834 #define FILETYPE_OTHER 0
835 #define FILETYPE_FILE 1
836 #define FILETYPE_PIPE 2
837 #define FILETYPE_SOCKET 3
838 #define FILETYPE_INOTIFY 4
839 #define FILETYPE_EVENTFD 5
840 #define FILETYPE_EVENTPOLL 6
841 #define FILETYPE_TIMERFD 7
842 #define FILETYPE_SIGNALFD 8
844 void file_descriptor_not_used(int id)
846 if(id > 0 && id < all_files_size) {
847 if(all_files[id].magic != 0x0BADCAFE) {
848 error("Ignoring request to remove empty file id %d.", id);
852 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %ld).\n", id, all_files[id].count);
854 if(all_files[id].count > 0) {
855 all_files[id].count--;
857 if(!all_files[id].count) {
858 if(debug) fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
859 file_descriptor_remove(&all_files[id]);
860 all_files[id].magic = 0x00000000;
865 error("apps.plugin: ERROR: request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
867 else error("apps.plugin: ERROR: request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
870 unsigned long file_descriptor_find_or_add(const char *name)
872 static int last_pos = 0;
873 unsigned long hash = simple_hash(name);
875 if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %lu\n", name, hash);
877 struct file_descriptor *fd = file_descriptor_find(name, hash);
880 if(debug) fprintf(stderr, "apps.plugin: >> found on slot %ld\n", fd->pos);
886 // check we have enough memory to add it
887 if(!all_files || all_files_len == all_files_size) {
888 void *old = all_files;
891 // there is no empty slot
892 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
893 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
895 // if the address changed, we have to rebuild the index
896 // since all pointers are now invalid
897 if(old && old != (void *)all_files) {
898 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing.\n");
899 all_files_index.root = NULL;
900 for(i = 0; i < all_files_size; i++) {
901 if(!all_files[i].count) continue;
902 file_descriptor_add(&all_files[i]);
904 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
907 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
908 all_files[i].count = 0;
909 all_files[i].name = NULL;
910 all_files[i].magic = 0x00000000;
911 all_files[i].pos = i;
914 if(!all_files_size) all_files_len = 1;
915 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
918 if(debug) fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
920 // search for an empty slot
922 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
923 if(c >= all_files_size) c = 0;
926 if(!all_files[c].count) {
927 if(debug) fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
929 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
930 error("apps.plugin: fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
932 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);
933 if(all_files[c].name) free((void *)all_files[c].name);
934 all_files[c].name = NULL;
939 if(i == all_files_size) {
940 fatal("We should find an empty slot, but there isn't any");
943 if(debug) fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
947 // else we have an empty slot in 'c'
949 int type = FILETYPE_OTHER;
950 if(name[0] == '/') type = FILETYPE_FILE;
951 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
952 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
953 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
954 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
955 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
956 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
957 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
958 else if(strncmp(name, "anon_inode:", 11) == 0) {
959 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
960 type = FILETYPE_OTHER;
963 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
964 type = FILETYPE_OTHER;
967 all_files[c].name = strdup(name);
968 all_files[c].hash = hash;
969 all_files[c].type = type;
970 all_files[c].pos = c;
971 all_files[c].count = 1;
972 all_files[c].magic = 0x0BADCAFE;
974 file_descriptor_add(&all_files[c]);
976 if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
982 // 1. read all files in /proc
983 // 2. for each numeric directory:
984 // i. read /proc/pid/stat
985 // ii. read /proc/pid/statm
986 // iii. read /proc/pid/io (requires root access)
987 // iii. read the entries in directory /proc/pid/fd (requires root access)
989 // a. find or create a struct file_descriptor
990 // b. cleanup any old/unused file_descriptors
992 // after all these, some pids may be linked to targets, while others may not
994 // in case of errors, only 1 every 1000 errors is printed
995 // to avoid filling up all disk space
996 // if debug is enabled, all errors are printed
998 int update_from_proc(void)
1000 static long count_errors = 0;
1002 char filename[FILENAME_MAX+1];
1003 DIR *dir = opendir("/proc");
1006 struct dirent *file = NULL;
1007 struct pid_stat *p = NULL;
1009 // mark them all as un-updated
1011 for(p = root_of_pids; p ; p = p->next) {
1020 while((file = readdir(dir))) {
1021 char *endptr = file->d_name;
1022 pid_t pid = strtoul(file->d_name, &endptr, 10);
1023 if(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0') continue;
1025 p = get_pid_entry(pid);
1028 // --------------------------------------------------------------------
1031 if(read_proc_pid_stat(p)) {
1032 error("Cannot process /proc/%d/stat", pid);
1035 if(p->ppid < 0 || p->ppid > pid_max) p->ppid = 0;
1038 // --------------------------------------------------------------------
1039 // /proc/<pid>/statm
1041 if(read_proc_pid_statm(p)) {
1042 error("Cannot process /proc/%d/statm", pid);
1047 // --------------------------------------------------------------------
1050 if(read_proc_pid_io(p)) {
1051 error("Cannot process /proc/%d/io", pid);
1055 // --------------------------------------------------------------------
1058 // check if it is target
1059 // we do this only once, the first time this pid is loaded
1061 if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
1064 for(w = target_root; w ; w = w->next) {
1065 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1067 if(strcmp(w->compare, p->comm) == 0) {
1068 if(w->target) p->target = w->target;
1071 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1076 // --------------------------------------------------------------------
1079 snprintf(filename, FILENAME_MAX, "/proc/%s/fd", file->d_name);
1080 DIR *fds = opendir(filename);
1084 char fdname[FILENAME_MAX + 1];
1085 char linkname[FILENAME_MAX + 1];
1087 // make the array negative
1088 for(c = 0 ; c < p->fds_size ; c++) p->fds[c] = -p->fds[c];
1090 while((de = readdir(fds))) {
1091 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
1093 // check if the fds array is small
1094 int fdid = atol(de->d_name);
1095 if(fdid < 0) continue;
1096 if(fdid >= p->fds_size) {
1097 // it is small, extend it
1098 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1099 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1101 error("apps.plugin: ERROR: cannot re-allocate fds for %s", p->comm);
1105 // and initialize it
1106 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1107 p->fds_size = fdid + 100;
1110 if(p->fds[fdid] == 0) {
1111 // we don't know this fd, get it
1113 sprintf(fdname, "/proc/%s/fd/%s", file->d_name, de->d_name);
1114 int l = readlink(fdname, linkname, FILENAME_MAX);
1116 if(debug || (p->target && p->target->debug)) {
1117 if(!count_errors++ || debug || (p->target && p->target->debug))
1118 error("apps.plugin: ERROR: cannot read link %s", fdname);
1125 // if another process already has this, we will get
1127 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1130 // else make it positive again, we need it
1131 // of course, the actual file may have changed, but we don't care so much
1132 // FIXME: we could compare the inode as returned by readdir direct structure
1133 else p->fds[fdid] = -p->fds[fdid];
1137 // remove all the negative file descriptors
1138 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1139 file_descriptor_not_used(-p->fds[c]);
1144 // --------------------------------------------------------------------
1147 // mark it as updated
1150 if(count_errors > 1000) {
1151 error("apps.plugin: ERROR: %ld more errors encountered\n", count_errors - 1);
1161 // ----------------------------------------------------------------------------
1162 // update statistics on the targets
1164 // 1. link all childs to their parents
1165 // 2. go from bottom to top, marking as merged all childs to their parents
1166 // this step links all parents without a target to the child target, if any
1167 // 3. link all top level processes (the ones not merged) to the default target
1168 // 4. go from top to bottom, linking all childs without a target, to their parent target
1169 // after this step, all processes have a target
1170 // [5. for each killed pid (updated = 0), remove its usage from its target]
1171 // 6. zero all targets
1172 // 7. concentrate all values on the targets
1173 // 8. remove all killed processes
1174 // 9. find the unique file count for each target
1176 void update_statistics(void)
1179 struct pid_stat *p = NULL;
1181 // link all parents and update childs count
1182 for(p = root_of_pids; p ; p = p->next) {
1183 if(p->ppid > 0 && p->ppid <= pid_max && all_pids[p->ppid]) {
1184 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \tparent of %d %s is %d %s\n", p->pid, p->comm, p->ppid, all_pids[p->ppid]->comm);
1186 p->parent = all_pids[p->ppid];
1187 p->parent->childs++;
1189 else if(p->ppid != 0) error("apps.plugin: \t\tWRONG! pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1192 // find all the procs with 0 childs and merge them to their parents
1193 // repeat, until nothing more can be done.
1197 for(p = root_of_pids; p ; p = p->next) {
1198 // if this process does not have any childs, and
1199 // is not already merged, and
1200 // its parent has childs waiting to be merged, and
1201 // the target of this process and its parent is the same, or the parent does not have a target, or this process does not have a parent
1202 // and its parent is not init
1203 // then... merge them!
1204 if(!p->childs && !p->merged && p->parent && p->parent->childs && (p->target == p->parent->target || !p->parent->target || !p->target) && p->ppid != 1) {
1205 p->parent->childs--;
1208 // the parent inherits the child's target, if it does not have a target itself
1209 if(p->target && !p->parent->target) {
1210 p->parent->target = p->target;
1211 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\ttarget %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);
1217 if(debug) fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1220 // give a default target on all top level processes
1221 // init goes always to default target
1222 if(all_pids[1]) all_pids[1]->target = default_target;
1224 for(p = root_of_pids; p ; p = p->next) {
1225 // if the process is not merged itself
1226 // then is is a top level process
1227 if(!p->merged && !p->target) p->target = default_target;
1229 #ifdef INCLUDE_CHILDS
1230 // by the way, update the diffs
1231 // will be used later for substracting killed process times
1232 p->diff_cutime = p->utime - p->cutime;
1233 p->diff_cstime = p->stime - p->cstime;
1234 p->diff_cminflt = p->minflt - p->cminflt;
1235 p->diff_cmajflt = p->majflt - p->cmajflt;
1239 // give a target to all merged child processes
1243 for(p = root_of_pids; p ; p = p->next) {
1244 if(!p->target && p->merged && p->parent && p->parent->target) {
1245 p->target = p->parent->target;
1251 #ifdef INCLUDE_CHILDS
1252 // for each killed process, remove its values from the parents
1253 // sums (we had already added them in a previous loop)
1254 for(p = root_of_pids; p ; p = p->next) {
1255 if(p->updated) continue;
1257 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1259 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1260 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1261 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1262 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1264 struct pid_stat *t = p;
1265 while((t = t->parent)) {
1266 if(!t->updated) continue;
1268 unsigned long long x;
1269 if(diff_utime && t->diff_cutime) {
1270 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1272 t->diff_cutime -= x;
1274 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1276 if(diff_stime && t->diff_cstime) {
1277 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1279 t->diff_cstime -= x;
1281 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1283 if(diff_minflt && t->diff_cminflt) {
1284 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1286 t->diff_cminflt -= x;
1287 t->fix_cminflt += x;
1288 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1290 if(diff_majflt && t->diff_cmajflt) {
1291 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1293 t->diff_cmajflt -= x;
1294 t->fix_cmajflt += x;
1295 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1299 if(diff_utime) error("apps.plugin: \t cannot fix up utime %llu", diff_utime);
1300 if(diff_stime) error("apps.plugin: \t cannot fix up stime %llu", diff_stime);
1301 if(diff_minflt) error("apps.plugin: \t cannot fix up minflt %llu", diff_minflt);
1302 if(diff_majflt) error("apps.plugin: \t cannot fix up majflt %llu", diff_majflt);
1306 // zero all the targets
1309 for (w = target_root; w ; w = w->next) {
1312 w->fds = calloc(sizeof(int), all_files_size);
1314 error("apps.plugin: ERROR: cannot allocate memory for fds in %s", w->name);
1329 w->statm_resident = 0;
1336 w->io_logical_bytes_read = 0;
1337 w->io_logical_bytes_written = 0;
1338 w->io_read_calls = 0;
1339 w->io_write_calls = 0;
1340 w->io_storage_bytes_read = 0;
1341 w->io_storage_bytes_written = 0;
1342 w->io_cancelled_write_bytes = 0;
1345 #ifdef INCLUDE_CHILDS
1346 if(debug) walk_down(0, 1);
1349 // concentrate everything on the targets
1350 for(p = root_of_pids; p ; p = p->next) {
1352 error("apps.plugin: ERROR: pid %d %s was left without a target!", p->pid, p->comm);
1357 p->target->cutime += p->cutime; // - p->fix_cutime;
1358 p->target->cstime += p->cstime; // - p->fix_cstime;
1359 p->target->cminflt += p->cminflt; // - p->fix_cminflt;
1360 p->target->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1362 p->target->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1363 p->target->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1364 p->target->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1365 p->target->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1367 p->target->num_threads += p->num_threads;
1368 p->target->rss += p->rss;
1370 p->target->statm_size += p->statm_size;
1371 p->target->statm_resident += p->statm_resident;
1372 p->target->statm_share += p->statm_share;
1373 p->target->statm_text += p->statm_text;
1374 p->target->statm_lib += p->statm_lib;
1375 p->target->statm_data += p->statm_data;
1376 p->target->statm_dirty += p->statm_dirty;
1378 p->target->io_logical_bytes_read += p->io_logical_bytes_read;
1379 p->target->io_logical_bytes_written += p->io_logical_bytes_written;
1380 p->target->io_read_calls += p->io_read_calls;
1381 p->target->io_write_calls += p->io_write_calls;
1382 p->target->io_storage_bytes_read += p->io_storage_bytes_read;
1383 p->target->io_storage_bytes_written += p->io_storage_bytes_written;
1384 p->target->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1386 p->target->processes++;
1388 for(c = 0; c < p->fds_size ;c++) {
1389 if(p->fds[c] == 0) continue;
1390 if(p->fds[c] < all_files_size) {
1391 if(p->target->fds) p->target->fds[p->fds[c]]++;
1394 error("apps.plugin: ERROR: invalid fd number %d", p->fds[c]);
1397 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);
1399 /* 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);
1400 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);
1401 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);
1402 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);
1403 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);
1404 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);
1405 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);
1406 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);
1408 #ifdef INCLUDE_CHILDS
1409 p->old_utime = p->utime;
1410 p->old_cutime = p->cutime;
1411 p->old_stime = p->stime;
1412 p->old_cstime = p->cstime;
1413 p->old_minflt = p->minflt;
1414 p->old_majflt = p->majflt;
1415 p->old_cminflt = p->cminflt;
1416 p->old_cmajflt = p->cmajflt;
1420 // since the process has exited, the user
1421 // will see a drop in our charts, because the incremental
1422 // values of this process will not be there
1424 // add them to the fix_* values and they will be added to
1425 // the reported values, so that the report goes steady
1426 p->target->fix_minflt += p->minflt;
1427 p->target->fix_majflt += p->majflt;
1428 p->target->fix_utime += p->utime;
1429 p->target->fix_stime += p->stime;
1430 p->target->fix_cminflt += p->cminflt;
1431 p->target->fix_cmajflt += p->cmajflt;
1432 p->target->fix_cutime += p->cutime;
1433 p->target->fix_cstime += p->cstime;
1435 p->target->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1436 p->target->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1437 p->target->fix_io_read_calls += p->io_read_calls;
1438 p->target->fix_io_write_calls += p->io_write_calls;
1439 p->target->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1440 p->target->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1441 p->target->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1445 // fprintf(stderr, "\n");
1446 // cleanup all un-updated processed (exited, killed, etc)
1447 for(p = root_of_pids; p ;) {
1449 // 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);
1451 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1452 file_descriptor_not_used(p->fds[c]);
1463 for (w = target_root; w ; w = w->next) {
1467 w->openinotifies = 0;
1468 w->openeventfds = 0;
1469 w->opentimerfds = 0;
1470 w->opensignalfds = 0;
1471 w->openeventpolls = 0;
1474 for(c = 1; c < all_files_size ;c++) {
1475 if(w->fds && w->fds[c] > 0) switch(all_files[c].type) {
1484 case FILETYPE_SOCKET:
1488 case FILETYPE_INOTIFY:
1492 case FILETYPE_EVENTFD:
1496 case FILETYPE_TIMERFD:
1500 case FILETYPE_SIGNALFD:
1504 case FILETYPE_EVENTPOLL:
1505 w->openeventpolls++;
1518 // ----------------------------------------------------------------------------
1519 // update chart dimensions
1521 void show_dimensions(void)
1523 static struct timeval last = { 0, 0 };
1524 static struct rusage me_last;
1530 unsigned long long usec;
1531 unsigned long long cpuuser;
1532 unsigned long long cpusyst;
1535 gettimeofday(&last, NULL);
1536 getrusage(RUSAGE_SELF, &me_last);
1538 // the first time, give a zero to allow
1539 // netdata calibrate to the current time
1540 // usec = update_every * 1000000ULL;
1546 gettimeofday(&now, NULL);
1547 getrusage(RUSAGE_SELF, &me);
1549 usec = usecdiff(&now, &last);
1550 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
1551 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
1553 bcopy(&now, &last, sizeof(struct timeval));
1554 bcopy(&me, &me_last, sizeof(struct rusage));
1557 fprintf(stdout, "BEGIN apps.cpu %llu\n", usec);
1558 for (w = target_root; w ; w = w->next) {
1559 if(w->target || (!w->processes && !w->exposed)) continue;
1561 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
1563 fprintf(stdout, "END\n");
1565 fprintf(stdout, "BEGIN apps.cpu_user %llu\n", usec);
1566 for (w = target_root; w ; w = w->next) {
1567 if(w->target || (!w->processes && !w->exposed)) continue;
1569 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
1571 fprintf(stdout, "END\n");
1573 fprintf(stdout, "BEGIN apps.cpu_system %llu\n", usec);
1574 for (w = target_root; w ; w = w->next) {
1575 if(w->target || (!w->processes && !w->exposed)) continue;
1577 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
1579 fprintf(stdout, "END\n");
1581 fprintf(stdout, "BEGIN apps.threads %llu\n", usec);
1582 for (w = target_root; w ; w = w->next) {
1583 if(w->target || (!w->processes && !w->exposed)) continue;
1585 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
1587 fprintf(stdout, "END\n");
1589 fprintf(stdout, "BEGIN apps.processes %llu\n", usec);
1590 for (w = target_root; w ; w = w->next) {
1591 if(w->target || (!w->processes && !w->exposed)) continue;
1593 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
1595 fprintf(stdout, "END\n");
1597 fprintf(stdout, "BEGIN apps.mem %llu\n", usec);
1598 for (w = target_root; w ; w = w->next) {
1599 if(w->target || (!w->processes && !w->exposed)) continue;
1601 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
1603 fprintf(stdout, "END\n");
1605 fprintf(stdout, "BEGIN apps.minor_faults %llu\n", usec);
1606 for (w = target_root; w ; w = w->next) {
1607 if(w->target || (!w->processes && !w->exposed)) continue;
1609 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt);
1611 fprintf(stdout, "END\n");
1613 fprintf(stdout, "BEGIN apps.major_faults %llu\n", usec);
1614 for (w = target_root; w ; w = w->next) {
1615 if(w->target || (!w->processes && !w->exposed)) continue;
1617 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt);
1619 fprintf(stdout, "END\n");
1621 fprintf(stdout, "BEGIN apps.lreads %llu\n", usec);
1622 for (w = target_root; w ; w = w->next) {
1623 if(w->target || (!w->processes && !w->exposed)) continue;
1625 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read);
1627 fprintf(stdout, "END\n");
1629 fprintf(stdout, "BEGIN apps.lwrites %llu\n", usec);
1630 for (w = target_root; w ; w = w->next) {
1631 if(w->target || (!w->processes && !w->exposed)) continue;
1633 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written);
1635 fprintf(stdout, "END\n");
1637 fprintf(stdout, "BEGIN apps.preads %llu\n", usec);
1638 for (w = target_root; w ; w = w->next) {
1639 if(w->target || (!w->processes && !w->exposed)) continue;
1641 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read);
1643 fprintf(stdout, "END\n");
1645 fprintf(stdout, "BEGIN apps.pwrites %llu\n", usec);
1646 for (w = target_root; w ; w = w->next) {
1647 if(w->target || (!w->processes && !w->exposed)) continue;
1649 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written);
1651 fprintf(stdout, "END\n");
1653 fprintf(stdout, "BEGIN apps.files %llu\n", usec);
1654 for (w = target_root; w ; w = w->next) {
1655 if(w->target || (!w->processes && !w->exposed)) continue;
1657 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
1659 fprintf(stdout, "END\n");
1661 fprintf(stdout, "BEGIN apps.sockets %llu\n", usec);
1662 for (w = target_root; w ; w = w->next) {
1663 if(w->target || (!w->processes && !w->exposed)) continue;
1665 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
1667 fprintf(stdout, "END\n");
1669 fprintf(stdout, "BEGIN apps.pipes %llu\n", usec);
1670 for (w = target_root; w ; w = w->next) {
1671 if(w->target || (!w->processes && !w->exposed)) continue;
1673 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
1675 fprintf(stdout, "END\n");
1677 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
1678 fprintf(stdout, "SET user = %llu\n", cpuuser);
1679 fprintf(stdout, "SET system = %llu\n", cpusyst);
1680 fprintf(stdout, "END\n");
1682 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
1683 fprintf(stdout, "SET files = %llu\n", file_counter);
1684 fprintf(stdout, "SET pids = %ld\n", all_pids_count);
1685 fprintf(stdout, "SET fds = %d\n", all_files_len);
1686 fprintf(stdout, "SET targets = %ld\n", targets);
1687 fprintf(stdout, "END\n");
1693 // ----------------------------------------------------------------------------
1694 // generate the charts
1696 void show_charts(void)
1699 int newly_added = 0;
1701 for(w = target_root ; w ; w = w->next)
1702 if(!w->exposed && w->processes) {
1705 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
1708 // nothing more to show
1709 if(!newly_added) return;
1711 // we have something new to show
1712 // update the charts
1713 fprintf(stdout, "CHART apps.cpu '' 'Apps CPU Time (%ld%% = %ld core%s)' 'cpu time %%' apps apps stacked 20001 %d\n", (processors * 100), processors, (processors>1)?"s":"", update_every);
1714 for (w = target_root; w ; w = w->next) {
1715 if(w->target || (!w->processes && !w->exposed)) continue;
1717 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu %s\n", w->name, (unsigned long long)(Hertz * update_every), w->hidden?"hidden":"");
1720 fprintf(stdout, "CHART apps.mem '' 'Apps Dedicated Memory (w/o shared)' 'MB' apps apps stacked 20003 %d\n", update_every);
1721 for (w = target_root; w ; w = w->next) {
1722 if(w->target || (!w->processes && !w->exposed)) continue;
1724 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
1727 fprintf(stdout, "CHART apps.threads '' 'Apps Threads' 'threads' apps apps stacked 20005 %d\n", update_every);
1728 for (w = target_root; w ; w = w->next) {
1729 if(w->target || (!w->processes && !w->exposed)) continue;
1731 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1734 fprintf(stdout, "CHART apps.processes '' 'Apps Processes' 'processes' apps apps stacked 20004 %d\n", update_every);
1735 for (w = target_root; w ; w = w->next) {
1736 if(w->target || (!w->processes && !w->exposed)) continue;
1738 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1741 fprintf(stdout, "CHART apps.cpu_user '' 'Apps CPU User Time (%ld%% = %ld core%s)' 'cpu time %%' apps none stacked 20020 %d\n", (processors * 100), processors, (processors>1)?"s":"", update_every);
1742 for (w = target_root; w ; w = w->next) {
1743 if(w->target || (!w->processes && !w->exposed)) continue;
1745 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors * update_every);
1748 fprintf(stdout, "CHART apps.cpu_system '' 'Apps CPU System Time (%ld%% = %ld core%s)' 'cpu time %%' apps none stacked 20021 %d\n", (processors * 100), processors, (processors>1)?"s":"", update_every);
1749 for (w = target_root; w ; w = w->next) {
1750 if(w->target || (!w->processes && !w->exposed)) continue;
1752 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors * update_every);
1755 fprintf(stdout, "CHART apps.major_faults '' 'Apps Major Page Faults (swaps in)' 'page faults/s' apps apps stacked 20010 %d\n", update_every);
1756 for (w = target_root; w ; w = w->next) {
1757 if(w->target || (!w->processes && !w->exposed)) continue;
1759 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, update_every);
1762 fprintf(stdout, "CHART apps.minor_faults '' 'Apps Minor Page Faults' 'page faults/s' apps none stacked 20011 %d\n", update_every);
1763 for (w = target_root; w ; w = w->next) {
1764 if(w->target || (!w->processes && !w->exposed)) continue;
1766 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, update_every);
1769 fprintf(stdout, "CHART apps.lreads '' 'Apps Disk Logical Reads' 'kilobytes/s' apps none stacked 20042 %d\n", update_every);
1770 for (w = target_root; w ; w = w->next) {
1771 if(w->target || (!w->processes && !w->exposed)) continue;
1773 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1776 fprintf(stdout, "CHART apps.lwrites '' 'Apps I/O Logical Writes' 'kilobytes/s' apps none stacked 20042 %d\n", update_every);
1777 for (w = target_root; w ; w = w->next) {
1778 if(w->target || (!w->processes && !w->exposed)) continue;
1780 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1783 fprintf(stdout, "CHART apps.preads '' 'Apps Disk Reads' 'kilobytes/s' apps apps stacked 20002 %d\n", update_every);
1784 for (w = target_root; w ; w = w->next) {
1785 if(w->target || (!w->processes && !w->exposed)) continue;
1787 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1790 fprintf(stdout, "CHART apps.pwrites '' 'Apps Disk Writes' 'kilobytes/s' apps apps stacked 20002 %d\n", update_every);
1791 for (w = target_root; w ; w = w->next) {
1792 if(w->target || (!w->processes && !w->exposed)) continue;
1794 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1797 fprintf(stdout, "CHART apps.files '' 'Apps Open Files' 'open files' apps apps stacked 20050 %d\n", update_every);
1798 for (w = target_root; w ; w = w->next) {
1799 if(w->target || (!w->processes && !w->exposed)) continue;
1801 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1804 fprintf(stdout, "CHART apps.sockets '' 'Apps Open Sockets' 'open sockets' apps apps stacked 20051 %d\n", update_every);
1805 for (w = target_root; w ; w = w->next) {
1806 if(w->target || (!w->processes && !w->exposed)) continue;
1808 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1811 fprintf(stdout, "CHART apps.pipes '' 'Apps Pipes' 'open pipes' apps none stacked 20053 %d\n", update_every);
1812 for (w = target_root; w ; w = w->next) {
1813 if(w->target || (!w->processes && !w->exposed)) continue;
1815 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1818 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' netdata netdata stacked 10000 %d\n", update_every);
1819 fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000 * update_every);
1820 fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000 * update_every);
1822 fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' netdata netdata line 10001 %d\n", update_every);
1823 fprintf(stdout, "DIMENSION files '' incremental 1 %d\n", update_every);
1824 fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
1825 fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
1826 fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
1832 // ----------------------------------------------------------------------------
1833 // parse command line arguments
1835 void parse_args(int argc, char **argv)
1840 for(i = 1; i < argc; i++) {
1842 int n = atoi(argv[i]);
1849 if(strcmp("debug", argv[i]) == 0) {
1851 debug_flags = 0xffffffff;
1860 error("apps.plugin: ERROR: cannot understand option %s", argv[i]);
1864 if(freq > 0) update_every = freq;
1865 if(!name) name = "groups";
1867 if(read_process_groups(name)) {
1868 error("apps.plugin: ERROR: cannot read process groups %s", name);
1873 int main(int argc, char **argv)
1875 // debug_flags = D_PROCFILE;
1877 info("apps.plugin: starting...");
1879 unsigned long started_t = time(NULL), current_t;
1880 Hertz = get_hertz();
1881 pid_max = get_pid_max();
1882 processors = get_processors();
1884 parse_args(argc, argv);
1886 all_pids = calloc(sizeof(struct pid_stat *), pid_max);
1888 error("apps.plugin: ERROR: cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
1889 printf("DISABLE\n");
1893 unsigned long long counter = 1;
1894 unsigned long long usec = 0, susec = 0;
1895 struct timeval last, now;
1896 gettimeofday(&last, NULL);
1898 for(;1; counter++) {
1899 if(!update_from_proc()) {
1900 error("apps.plugin: ERROR: cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
1901 printf("DISABLE\n");
1905 update_statistics();
1906 show_charts(); // this is smart enough to show only newly added apps, when needed
1909 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
1912 gettimeofday(&now, NULL);
1913 usec = usecdiff(&now, &last) - susec;
1914 if(debug) fprintf(stderr, "apps.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).\n", usec + susec, usec, susec);
1916 // if the last loop took less than half the time
1917 // wait the rest of the time
1918 if(usec < (update_every * 1000000ULL / 2)) susec = (update_every * 1000000ULL) - usec;
1919 else susec = update_every * 1000000ULL / 2;
1922 bcopy(&now, &last, sizeof(struct timeval));
1924 current_t = time(NULL);
1925 if(current_t - started_t > 3600) exit(0);