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
47 // ----------------------------------------------------------------------------
50 long get_processors(void) {
53 procfile *ff = procfile_open("/proc/stat", O_RDONLY);
56 ff = procfile_readall(ff);
63 for(i = 0; i < procfile_lines(ff); i++) {
64 if(!procfile_linewords(ff, i)) continue;
66 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
69 if(processors < 1) processors = 1;
75 long get_pid_max(void) {
78 procfile *ff = procfile_open("/proc/sys/kernel/pid_max", O_RDONLY);
81 ff = procfile_readall(ff);
87 mpid = atol(procfile_lineword(ff, 0, 0));
88 if(mpid) mpid = 32768;
94 unsigned long long get_hertz(void)
96 unsigned long long hz = 1;
99 if((hz = sysconf(_SC_CLK_TCK)) > 0) {
105 hz = (unsigned long long)HZ; /* <asm/param.h> */
107 /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
108 hz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
111 error("apps.plugin: ERROR: unknown HZ value. Assuming %llu.", hz);
116 // ----------------------------------------------------------------------------
118 // target is the point to aggregate a process tree values
121 char compare[MAX_COMPARE_NAME + 1];
122 char id[MAX_NAME + 1];
123 char name[MAX_NAME + 1];
125 unsigned long long minflt;
126 unsigned long long cminflt;
127 unsigned long long majflt;
128 unsigned long long cmajflt;
129 unsigned long long utime;
130 unsigned long long stime;
131 unsigned long long cutime;
132 unsigned long long cstime;
133 unsigned long long num_threads;
134 unsigned long long rss;
136 unsigned long long fix_minflt;
137 unsigned long long fix_cminflt;
138 unsigned long long fix_majflt;
139 unsigned long long fix_cmajflt;
140 unsigned long long fix_utime;
141 unsigned long long fix_stime;
142 unsigned long long fix_cutime;
143 unsigned long long fix_cstime;
145 unsigned long long statm_size;
146 unsigned long long statm_resident;
147 unsigned long long statm_share;
148 unsigned long long statm_text;
149 unsigned long long statm_lib;
150 unsigned long long statm_data;
151 unsigned long long statm_dirty;
153 unsigned long long io_logical_bytes_read;
154 unsigned long long io_logical_bytes_written;
155 unsigned long long io_read_calls;
156 unsigned long long io_write_calls;
157 unsigned long long io_storage_bytes_read;
158 unsigned long long io_storage_bytes_written;
159 unsigned long long io_cancelled_write_bytes;
161 unsigned long long fix_io_logical_bytes_read;
162 unsigned long long fix_io_logical_bytes_written;
163 unsigned long long fix_io_read_calls;
164 unsigned long long fix_io_write_calls;
165 unsigned long long fix_io_storage_bytes_read;
166 unsigned long long fix_io_storage_bytes_written;
167 unsigned long long fix_io_cancelled_write_bytes;
170 unsigned long long openfiles;
171 unsigned long long openpipes;
172 unsigned long long opensockets;
173 unsigned long long openinotifies;
174 unsigned long long openeventfds;
175 unsigned long long opentimerfds;
176 unsigned long long opensignalfds;
177 unsigned long long openeventpolls;
178 unsigned long long openother;
180 unsigned long processes; // how many processes have been merged to this
181 int exposed; // if set, we have sent this to netdata
182 int hidden; // if set, we set the hidden flag on the dimension
185 struct target *target; // the one that will be reported to netdata
187 } *target_root = NULL, *default_target = NULL;
191 // find or create a target
192 // there are targets that are just agregated to other target (the second argument)
193 struct target *get_target(const char *id, struct target *target)
195 const char *nid = id;
196 if(nid[0] == '-') nid++;
199 for(w = target_root ; w ; w = w->next)
200 if(strncmp(nid, w->id, MAX_NAME) == 0) return w;
202 w = calloc(sizeof(struct target), 1);
204 error("apps.plugin: cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
208 strncpy(w->id, nid, MAX_NAME);
209 strncpy(w->name, nid, MAX_NAME);
210 strncpy(w->compare, nid, MAX_COMPARE_NAME);
211 if(id[0] == '-') w->hidden = 1;
215 w->next = target_root;
218 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:"");
223 // read the process groups file
224 int read_process_groups(const char *name)
227 char filename[FILENAME_MAX + 1];
229 snprintf(filename, FILENAME_MAX, "%s/apps_%s.conf", CONFIG_DIR, name);
231 if(debug) fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
232 FILE *fp = fopen(filename, "r");
234 error("apps.plugin: ERROR: cannot open file '%s' (%s)", filename, strerror(errno));
239 while(fgets(buffer, 4096, fp) != NULL) {
240 int whidden = 0, wdebug = 0;
243 // if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", buffer);
245 char *s = buffer, *t, *p;
247 if(!s || !*s || *s == '#') continue;
249 if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", s);
254 if(!t || !*t) continue;
276 if(debug) fprintf(stderr, "apps.plugin: \t\ttarget %s\n", t);
278 struct target *w = NULL;
282 while((p = strsep(&s, " "))) {
284 if(!p || !*p) continue;
286 struct target *n = get_target(p, w);
294 if(w) strncpy(w->name, t, MAX_NAME);
295 if(!count) error("apps.plugin: ERROR: the line %ld on file '%s', for group '%s' does not state any process names.", line, filename, t);
299 default_target = get_target("+p!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
300 strncpy(default_target->name, "other", MAX_NAME);
306 // ----------------------------------------------------------------------------
307 // data to store for each pid
312 char comm[MAX_COMPARE_NAME + 1];
320 unsigned long long minflt;
321 unsigned long long cminflt;
322 unsigned long long majflt;
323 unsigned long long cmajflt;
324 unsigned long long utime;
325 unsigned long long stime;
326 unsigned long long cutime;
327 unsigned long long cstime;
332 unsigned long long starttime;
333 unsigned long long vsize;
334 unsigned long long rss;
335 unsigned long long rsslim;
336 unsigned long long starcode;
337 unsigned long long endcode;
338 unsigned long long startstack;
339 unsigned long long kstkesp;
340 unsigned long long kstkeip;
350 uint32_t rt_priority;
352 unsigned long long delayacct_blkio_ticks;
356 unsigned long long statm_size;
357 unsigned long long statm_resident;
358 unsigned long long statm_share;
359 unsigned long long statm_text;
360 unsigned long long statm_lib;
361 unsigned long long statm_data;
362 unsigned long long statm_dirty;
364 unsigned long long io_logical_bytes_read;
365 unsigned long long io_logical_bytes_written;
366 unsigned long long io_read_calls;
367 unsigned long long io_write_calls;
368 unsigned long long io_storage_bytes_read;
369 unsigned long long io_storage_bytes_written;
370 unsigned long long io_cancelled_write_bytes;
372 #ifdef INCLUDE_CHILDS
373 unsigned long long old_utime;
374 unsigned long long old_stime;
375 unsigned long long old_minflt;
376 unsigned long long old_majflt;
378 unsigned long long old_cutime;
379 unsigned long long old_cstime;
380 unsigned long long old_cminflt;
381 unsigned long long old_cmajflt;
383 unsigned long long fix_cutime;
384 unsigned long long fix_cstime;
385 unsigned long long fix_cminflt;
386 unsigned long long fix_cmajflt;
388 unsigned long long diff_cutime;
389 unsigned long long diff_cstime;
390 unsigned long long diff_cminflt;
391 unsigned long long diff_cmajflt;
394 int *fds; // array of fds it uses
395 int fds_size; // the size of the fds array
397 int childs; // number of processes directly referencing this
398 int updated; // 1 when update
399 int merged; // 1 when it has been merged to its parent
401 struct target *target;
402 struct pid_stat *parent;
403 struct pid_stat *prev;
404 struct pid_stat *next;
405 } *root = NULL, **all_pids;
409 struct pid_stat *get_entry(pid_t pid)
412 all_pids[pid]->new_entry = 0;
413 return all_pids[pid];
416 all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
418 error("apps.plugin: ERROR: Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct pid_stat));
422 all_pids[pid]->fds = calloc(sizeof(int), 100);
423 if(!all_pids[pid]->fds)
424 error("apps.plugin: ERROR: Cannot allocate %ld bytes of memory", (unsigned long)(sizeof(int) * 100));
425 else all_pids[pid]->fds_size = 100;
427 if(root) root->prev = all_pids[pid];
428 all_pids[pid]->next = root;
429 root = all_pids[pid];
431 all_pids[pid]->new_entry = 1;
433 return all_pids[pid];
436 void del_entry(pid_t pid)
438 if(!all_pids[pid]) return;
440 if(debug) fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
442 if(root == all_pids[pid]) root = all_pids[pid]->next;
443 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
444 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
446 if(all_pids[pid]->fds) free(all_pids[pid]->fds);
448 all_pids[pid] = NULL;
451 #ifdef INCLUDE_CHILDS
452 // print a tree view of all processes
453 int walk_down(pid_t pid, int level) {
454 struct pid_stat *p = NULL;
458 for(i = 0; i < level; i++) b[i] = '\t';
463 for(p = root; p ; p = p->next) {
465 ret += walk_down(p->pid, level+1);
471 if(!p->updated) ret += 1;
472 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"
473 , b, p->comm, p->pid, p->updated?"OK":"KILLED", p->target->name, p->childs
474 , p->utime, p->utime - p->old_utime
475 , p->stime, p->stime - p->old_stime
476 , p->cutime, p->cutime - p->old_cutime
477 , p->cstime, p->cstime - p->old_cstime
478 , p->minflt, p->minflt - p->old_minflt
479 , p->majflt, p->majflt - p->old_majflt
480 , p->cminflt, p->cminflt - p->old_cminflt
481 , p->cmajflt, p->cmajflt - p->old_cmajflt
490 // ----------------------------------------------------------------------------
492 // this is used to keep a global list of all open files of the system
493 // it is needed in order to figure out the unique files a process tree has open
495 #define FILE_DESCRIPTORS_INCREASE_STEP 100
497 struct file_descriptor {
507 int all_files_len = 0;
508 int all_files_size = 0;
510 int file_descriptor_compare(void* a, void* b) {
511 if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
512 error("Corrupted index data detected. Please report this.");
514 if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
516 else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
518 else return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
520 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
522 avl_tree all_files_index = {
524 file_descriptor_compare
527 static struct file_descriptor *file_descriptor_find(const char *name, unsigned long hash) {
528 struct file_descriptor *result = NULL, tmp;
529 tmp.hash = (hash)?hash:simple_hash(name);
533 tmp.magic = 0x0BADCAFE;
535 avl_search(&all_files_index, (avl *)&tmp, file_descriptor_iterator, (avl **)&result);
539 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
540 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
542 #define FILETYPE_OTHER 0
543 #define FILETYPE_FILE 1
544 #define FILETYPE_PIPE 2
545 #define FILETYPE_SOCKET 3
546 #define FILETYPE_INOTIFY 4
547 #define FILETYPE_EVENTFD 5
548 #define FILETYPE_EVENTPOLL 6
549 #define FILETYPE_TIMERFD 7
550 #define FILETYPE_SIGNALFD 8
552 void file_descriptor_not_used(int id)
554 if(id > 0 && id < all_files_size) {
555 if(all_files[id].magic != 0x0BADCAFE) {
556 error("Ignoring request to remove empty file id %d.", id);
560 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %ld).\n", id, all_files[id].count);
562 if(all_files[id].count > 0) {
563 all_files[id].count--;
565 if(!all_files[id].count) {
566 if(debug) fprintf(stderr, "apps.plugin: >> slot %d is empty.\n", id);
567 file_descriptor_remove(&all_files[id]);
568 all_files[id].magic = 0x00000000;
573 error("apps.plugin: ERROR: request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
575 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);
578 unsigned long file_descriptor_find_or_add(const char *name)
580 static int last_pos = 0;
581 unsigned long hash = simple_hash(name);
583 if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %lu\n", name, hash);
585 struct file_descriptor *fd = file_descriptor_find(name, hash);
588 if(debug) fprintf(stderr, "apps.plugin: >> found on slot %ld\n", fd->pos);
594 // check we have enough memory to add it
595 if(!all_files || all_files_len == all_files_size) {
596 void *old = all_files;
598 // there is no empty slot
599 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
600 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
602 // if the address changed, we have to rebuild the index
603 // since all pointers are now invalid
604 if(old && old != (void *)all_files) {
605 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing.\n");
606 all_files_index.root = NULL;
608 for(i = 0; i < all_files_size; i++) {
609 if(!all_files[i].count) continue;
610 file_descriptor_add(&all_files[i]);
612 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
613 all_files[i].count = 0;
614 all_files[i].name = NULL;
615 all_files[i].magic = 0x00000000;
616 all_files[i].pos = i;
618 if(debug) fprintf(stderr, "apps.plugin: >> re-indexing done.\n");
621 if(!all_files_size) all_files_len = 1;
622 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
625 if(debug) fprintf(stderr, "apps.plugin: >> searching for empty slot.\n");
627 // search for an empty slot
629 for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
630 if(c >= all_files_size) c = 0;
633 if(!all_files[c].count) {
634 if(debug) fprintf(stderr, "apps.plugin: >> Examining slot %d.\n", c);
636 if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
637 error("apps.plugin: fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
639 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);
640 if(all_files[c].name) free((void *)all_files[c].name);
641 all_files[c].name = NULL;
646 if(i == all_files_size) {
647 fatal("We should find an empty slot, but there isn't any");
650 if(debug) fprintf(stderr, "apps.plugin: >> updating slot %d.\n", c);
654 // else we have an empty slot in 'c'
656 int type = FILETYPE_OTHER;
657 if(name[0] == '/') type = FILETYPE_FILE;
658 else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
659 else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
660 else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
661 else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
662 else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
663 else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
664 else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
665 else if(strncmp(name, "anon_inode:", 11) == 0) {
666 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
667 type = FILETYPE_OTHER;
670 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
671 type = FILETYPE_OTHER;
674 all_files[c].name = strdup(name);
675 all_files[c].hash = hash;
676 all_files[c].type = type;
677 all_files[c].pos = c;
678 all_files[c].count = 1;
679 all_files[c].magic = 0x0BADCAFE;
681 file_descriptor_add(&all_files[c]);
683 if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
689 // ----------------------------------------------------------------------------
690 // update pids from proc
692 // 1. read all files in /proc
693 // 2. for each numeric directory:
694 // i. read /proc/pid/stat
695 // ii. read /proc/pid/statm
696 // iii. read /proc/pid/io (requires root access)
697 // iii. read the entries in directory /proc/pid/fd (requires root access)
699 // a. find or create a struct file_descriptor
700 // b. cleanup any old/unused file_descriptors
702 // after all these, some pids may be linked to targets, while others may not
704 // in case of errors, only 1 every 1000 errors is printed
705 // to avoid filling up all disk space
706 // if debug is enabled, all errors are printed
708 int update_from_proc(void)
710 static long count_errors = 0;
712 char buffer[PROC_BUFFER + 1];
713 char name[PROC_BUFFER + 1];
714 char filename[FILENAME_MAX+1];
715 DIR *dir = opendir("/proc");
718 struct dirent *file = NULL;
719 struct pid_stat *p = NULL;
721 // mark them all as un-updated
723 for(p = root; p ; p = p->next) {
732 while((file = readdir(dir))) {
733 char *endptr = file->d_name;
734 pid_t pid = strtoul(file->d_name, &endptr, 10);
735 if(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0') continue;
738 // --------------------------------------------------------------------
741 snprintf(filename, FILENAME_MAX, "/proc/%s/stat", file->d_name);
742 int fd = open(filename, O_RDONLY);
744 if(errno != ENOENT && errno != ESRCH) {
745 if(!count_errors++ || debug)
746 error("apps.plugin: ERROR: cannot open file '%s' for reading (%d, %s).", filename, errno, strerror(errno));
752 int bytes = read(fd, buffer, PROC_BUFFER);
756 if(!count_errors++ || debug)
757 error("apps.plugin: ERROR: cannot read from file '%s' (%s).", filename, strerror(errno));
761 if(bytes < 10) continue;
762 buffer[bytes] = '\0';
763 if(debug) fprintf(stderr, "apps.plugin: READ stat: %s", buffer);
768 int parsed = sscanf(buffer,
769 "%d (%[^)]) %c" // pid, comm, state
770 " %d %d %d %d %d" // ppid, pgrp, session, tty_nr, tpgid
771 " %" PRIu64 " %llu %llu %llu %llu" // flags, minflt, cminflt, majflt, cmajflt
772 " %llu %llu %llu %llu" // utime, stime, cutime, cstime
773 " %" PRId64 " %" PRId64 // priority, nice
775 " %" PRId64 // itrealvalue
779 " %llu %llu %llu %llu %llu %llu" // rsslim, starcode, endcode, startstack, kstkesp, kstkeip
780 " %" PRIu64 " %" PRIu64 " %" PRIu64 " %" PRIu64 // signal, blocked, sigignore, sigcatch
781 " %" PRIu64 " %" PRIu64 " %" PRIu64 // wchan, nswap, cnswap
782 " %d %d" // exit_signal, processor
783 " %u %u" // rt_priority, policy
784 " %llu %" PRIu64 " %" PRId64 // delayacct_blkio_ticks, guest_time, cguest_time
785 , &p->pid, name, &p->state
786 , &p->ppid, &p->pgrp, &p->session, &p->tty_nr, &p->tpgid
787 , &p->flags, &p->minflt, &p->cminflt, &p->majflt, &p->cmajflt
788 , &p->utime, &p->stime, &p->cutime, &p->cstime
789 , &p->priority, &p->nice
795 , &p->rsslim, &p->starcode, &p->endcode, &p->startstack, &p->kstkesp, &p->kstkeip
796 , &p->signal, &p->blocked, &p->sigignore, &p->sigcatch
797 , &p->wchan, &p->nswap, &p->cnswap
798 , &p->exit_signal, &p->processor
799 , &p->rt_priority, &p->policy
800 , &p->delayacct_blkio_ticks, &p->guest_time, &p->cguest_time
802 strncpy(p->comm, name, MAX_COMPARE_NAME);
803 p->comm[MAX_COMPARE_NAME] = '\0';
805 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\n", p->comm, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
808 if(!count_errors++ || debug || (p->target && p->target->debug))
809 error("apps.plugin: ERROR: file %s gave %d results (expected 44)\n", filename, parsed);
812 // check if it is target
813 // we do this only once, the first time this pid is loaded
815 if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
818 for(w = target_root; w ; w = w->next) {
819 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
821 if(strcmp(w->compare, p->comm) == 0) {
822 if(w->target) p->target = w->target;
825 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
831 if(p->ppid < 0 || p->ppid > pid_max) p->ppid = 0;
834 // --------------------------------------------------------------------
837 snprintf(filename, FILENAME_MAX, "/proc/%s/statm", file->d_name);
838 fd = open(filename, O_RDONLY);
840 if(errno != ENOENT && errno != ESRCH) {
841 if(!count_errors++ || debug || (p->target && p->target->debug))
842 error("apps.plugin: ERROR: cannot open file '%s' for reading (%d, %s).", filename, errno, strerror(errno));
847 bytes = read(fd, buffer, PROC_BUFFER);
851 if(!count_errors++ || debug || (p->target && p->target->debug))
852 error("apps.plugin: ERROR: cannot read from file '%s' (%s).", filename, strerror(errno));
854 else if(bytes > 10) {
855 buffer[bytes] = '\0';
856 if(debug || (p->target && p->target->debug))
857 fprintf(stderr, "apps.plugin: READ statm: %s", buffer);
859 parsed = sscanf(buffer,
860 "%llu %llu %llu %llu %llu %llu %llu"
871 if(!count_errors++ || debug || (p->target && p->target->debug))
872 error("apps.plugin: ERROR: file %s gave %d results (expected 7)", filename, parsed);
877 // --------------------------------------------------------------------
880 snprintf(filename, FILENAME_MAX, "/proc/%s/io", file->d_name);
881 fd = open(filename, O_RDONLY);
883 if(errno != ENOENT && errno != ESRCH) {
884 if(!count_errors++ || debug || (p->target && p->target->debug))
885 error("apps.plugin: ERROR: cannot open file '%s' for reading (%d, %s).", filename, errno, strerror(errno));
890 bytes = read(fd, buffer, PROC_BUFFER);
894 if(!count_errors++ || debug || (p->target && p->target->debug))
895 error("apps.plugin: ERROR: cannot read from file '%s' (%s).", filename, strerror(errno));
897 else if(bytes > 10) {
898 buffer[bytes] = '\0';
899 if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: READ io: %s", buffer);
901 parsed = sscanf(buffer,
902 "rchar: %llu\nwchar: %llu\nsyscr: %llu\nsyscw: %llu\nread_bytes: %llu\nwrite_bytes: %llu\ncancelled_write_bytes: %llu"
903 , &p->io_logical_bytes_read
904 , &p->io_logical_bytes_written
907 , &p->io_storage_bytes_read
908 , &p->io_storage_bytes_written
909 , &p->io_cancelled_write_bytes
913 if(!count_errors++ || debug || (p->target && p->target->debug))
914 error("apps.plugin: ERROR: file %s gave %d results (expected 7)", filename, parsed);
919 // --------------------------------------------------------------------
922 snprintf(filename, FILENAME_MAX, "/proc/%s/fd", file->d_name);
923 DIR *fds = opendir(filename);
927 char fdname[FILENAME_MAX + 1];
928 char linkname[FILENAME_MAX + 1];
930 // make the array negative
931 for(c = 0 ; c < p->fds_size ; c++) p->fds[c] = -p->fds[c];
933 while((de = readdir(fds))) {
934 if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue;
936 // check if the fds array is small
937 int fdid = atol(de->d_name);
938 if(fdid < 0) continue;
939 if(fdid >= p->fds_size) {
940 // it is small, extend it
941 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
942 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
944 error("apps.plugin: ERROR: cannot re-allocate fds for %s", p->comm);
949 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
950 p->fds_size = fdid + 100;
953 if(p->fds[fdid] == 0) {
954 // we don't know this fd, get it
956 sprintf(fdname, "/proc/%s/fd/%s", file->d_name, de->d_name);
957 int l = readlink(fdname, linkname, FILENAME_MAX);
959 if(debug || (p->target && p->target->debug)) {
960 if(!count_errors++ || debug || (p->target && p->target->debug))
961 error("apps.plugin: ERROR: cannot read link %s", fdname);
968 // if another process already has this, we will get
970 p->fds[fdid] = file_descriptor_find_or_add(linkname);
973 // else make it positive again, we need it
974 // of course, the actual file may have changed, but we don't care so much
975 // FIXME: we could compare the inode as returned by readdir direct structure
976 else p->fds[fdid] = -p->fds[fdid];
980 // remove all the negative file descriptors
981 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
982 file_descriptor_not_used(-p->fds[c]);
987 // --------------------------------------------------------------------
990 // mark it as updated
993 if(count_errors > 1000) {
994 error("apps.plugin: ERROR: %ld more errors encountered\n", count_errors - 1);
1004 // ----------------------------------------------------------------------------
1005 // update statistics on the targets
1007 // 1. link all childs to their parents
1008 // 2. go from bottom to top, marking as merged all childs to their parents
1009 // this step links all parents without a target to the child target, if any
1010 // 3. link all top level processes (the ones not merged) to the default target
1011 // 4. go from top to bottom, linking all childs without a target, to their parent target
1012 // after this step, all processes have a target
1013 // [5. for each killed pid (updated = 0), remove its usage from its target]
1014 // 6. zero all targets
1015 // 7. concentrate all values on the targets
1016 // 8. remove all killed processes
1017 // 9. find the unique file count for each target
1019 void update_statistics(void)
1022 struct pid_stat *p = NULL;
1024 // link all parents and update childs count
1025 for(p = root; p ; p = p->next) {
1026 if(p->ppid > 0 && p->ppid <= pid_max && all_pids[p->ppid]) {
1027 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);
1029 p->parent = all_pids[p->ppid];
1030 p->parent->childs++;
1032 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);
1035 // find all the procs with 0 childs and merge them to their parents
1036 // repeat, until nothing more can be done.
1040 for(p = root; p ; p = p->next) {
1041 // if this process does not have any childs, and
1042 // is not already merged, and
1043 // its parent has childs waiting to be merged, and
1044 // 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
1045 // and its parent is not init
1046 // then... merge them!
1047 if(!p->childs && !p->merged && p->parent && p->parent->childs && (p->target == p->parent->target || !p->parent->target || !p->target) && p->ppid != 1) {
1048 p->parent->childs--;
1051 // the parent inherits the child's target, if it does not have a target itself
1052 if(p->target && !p->parent->target) {
1053 p->parent->target = p->target;
1054 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);
1060 if(debug) fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1063 // give a default target on all top level processes
1064 // init goes always to default target
1065 if(all_pids[1]) all_pids[1]->target = default_target;
1067 for(p = root; p ; p = p->next) {
1068 // if the process is not merged itself
1069 // then is is a top level process
1070 if(!p->merged && !p->target) p->target = default_target;
1072 #ifdef INCLUDE_CHILDS
1073 // by the way, update the diffs
1074 // will be used later for substracting killed process times
1075 p->diff_cutime = p->utime - p->cutime;
1076 p->diff_cstime = p->stime - p->cstime;
1077 p->diff_cminflt = p->minflt - p->cminflt;
1078 p->diff_cmajflt = p->majflt - p->cmajflt;
1082 // give a target to all merged child processes
1086 for(p = root; p ; p = p->next) {
1087 if(!p->target && p->merged && p->parent && p->parent->target) {
1088 p->target = p->parent->target;
1094 #ifdef INCLUDE_CHILDS
1095 // for each killed process, remove its values from the parents
1096 // sums (we had already added them in a previous loop)
1097 for(p = root; p ; p = p->next) {
1098 if(p->updated) continue;
1100 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1102 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1103 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1104 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1105 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1107 struct pid_stat *t = p;
1108 while((t = t->parent)) {
1109 if(!t->updated) continue;
1111 unsigned long long x;
1112 if(diff_utime && t->diff_cutime) {
1113 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1115 t->diff_cutime -= x;
1117 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1119 if(diff_stime && t->diff_cstime) {
1120 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1122 t->diff_cstime -= x;
1124 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1126 if(diff_minflt && t->diff_cminflt) {
1127 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1129 t->diff_cminflt -= x;
1130 t->fix_cminflt += x;
1131 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1133 if(diff_majflt && t->diff_cmajflt) {
1134 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1136 t->diff_cmajflt -= x;
1137 t->fix_cmajflt += x;
1138 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1142 if(diff_utime) error("apps.plugin: \t cannot fix up utime %llu", diff_utime);
1143 if(diff_stime) error("apps.plugin: \t cannot fix up stime %llu", diff_stime);
1144 if(diff_minflt) error("apps.plugin: \t cannot fix up minflt %llu", diff_minflt);
1145 if(diff_majflt) error("apps.plugin: \t cannot fix up majflt %llu", diff_majflt);
1149 // zero all the targets
1152 for (w = target_root; w ; w = w->next) {
1155 w->fds = calloc(sizeof(int), all_files_size);
1157 error("apps.plugin: ERROR: cannot allocate memory for fds in %s", w->name);
1172 w->statm_resident = 0;
1179 w->io_logical_bytes_read = 0;
1180 w->io_logical_bytes_written = 0;
1181 w->io_read_calls = 0;
1182 w->io_write_calls = 0;
1183 w->io_storage_bytes_read = 0;
1184 w->io_storage_bytes_written = 0;
1185 w->io_cancelled_write_bytes = 0;
1188 #ifdef INCLUDE_CHILDS
1189 if(debug) walk_down(0, 1);
1192 // concentrate everything on the targets
1193 for(p = root; p ; p = p->next) {
1195 error("apps.plugin: ERROR: pid %d %s was left without a target!", p->pid, p->comm);
1200 p->target->cutime += p->cutime; // - p->fix_cutime;
1201 p->target->cstime += p->cstime; // - p->fix_cstime;
1202 p->target->cminflt += p->cminflt; // - p->fix_cminflt;
1203 p->target->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1205 p->target->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1206 p->target->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1207 p->target->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1208 p->target->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1210 p->target->num_threads += p->num_threads;
1211 p->target->rss += p->rss;
1213 p->target->statm_size += p->statm_size;
1214 p->target->statm_resident += p->statm_resident;
1215 p->target->statm_share += p->statm_share;
1216 p->target->statm_text += p->statm_text;
1217 p->target->statm_lib += p->statm_lib;
1218 p->target->statm_data += p->statm_data;
1219 p->target->statm_dirty += p->statm_dirty;
1221 p->target->io_logical_bytes_read += p->io_logical_bytes_read;
1222 p->target->io_logical_bytes_written += p->io_logical_bytes_written;
1223 p->target->io_read_calls += p->io_read_calls;
1224 p->target->io_write_calls += p->io_write_calls;
1225 p->target->io_storage_bytes_read += p->io_storage_bytes_read;
1226 p->target->io_storage_bytes_written += p->io_storage_bytes_written;
1227 p->target->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1229 p->target->processes++;
1231 for(c = 0; c < p->fds_size ;c++) {
1232 if(p->fds[c] == 0) continue;
1233 if(p->fds[c] < all_files_size) {
1234 if(p->target->fds) p->target->fds[p->fds[c]]++;
1237 error("apps.plugin: ERROR: invalid fd number %d", p->fds[c]);
1240 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);
1242 /* 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);
1243 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);
1244 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);
1245 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);
1246 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);
1247 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);
1248 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);
1249 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);
1251 #ifdef INCLUDE_CHILDS
1252 p->old_utime = p->utime;
1253 p->old_cutime = p->cutime;
1254 p->old_stime = p->stime;
1255 p->old_cstime = p->cstime;
1256 p->old_minflt = p->minflt;
1257 p->old_majflt = p->majflt;
1258 p->old_cminflt = p->cminflt;
1259 p->old_cmajflt = p->cmajflt;
1263 // since the process has exited, the user
1264 // will see a drop in our charts, because the incremental
1265 // values of this process will not be there
1267 // add them to the fix_* values and they will be added to
1268 // the reported values, so that the report goes steady
1269 p->target->fix_minflt += p->minflt;
1270 p->target->fix_majflt += p->majflt;
1271 p->target->fix_utime += p->utime;
1272 p->target->fix_stime += p->stime;
1273 p->target->fix_cminflt += p->cminflt;
1274 p->target->fix_cmajflt += p->cmajflt;
1275 p->target->fix_cutime += p->cutime;
1276 p->target->fix_cstime += p->cstime;
1278 p->target->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1279 p->target->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1280 p->target->fix_io_read_calls += p->io_read_calls;
1281 p->target->fix_io_write_calls += p->io_write_calls;
1282 p->target->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1283 p->target->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1284 p->target->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1288 // fprintf(stderr, "\n");
1289 // cleanup all un-updated processed (exited, killed, etc)
1290 for(p = root; p ;) {
1292 // 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);
1294 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1295 file_descriptor_not_used(p->fds[c]);
1306 for (w = target_root; w ; w = w->next) {
1310 w->openinotifies = 0;
1311 w->openeventfds = 0;
1312 w->opentimerfds = 0;
1313 w->opensignalfds = 0;
1314 w->openeventpolls = 0;
1317 for(c = 1; c < all_files_size ;c++) {
1318 if(w->fds && w->fds[c] > 0) switch(all_files[c].type) {
1327 case FILETYPE_SOCKET:
1331 case FILETYPE_INOTIFY:
1335 case FILETYPE_EVENTFD:
1339 case FILETYPE_TIMERFD:
1343 case FILETYPE_SIGNALFD:
1347 case FILETYPE_EVENTPOLL:
1348 w->openeventpolls++;
1361 // ----------------------------------------------------------------------------
1362 // update chart dimensions
1364 void show_dimensions(void)
1366 static struct timeval last = { 0, 0 };
1367 static struct rusage me_last;
1373 unsigned long long usec;
1374 unsigned long long cpuuser;
1375 unsigned long long cpusyst;
1378 gettimeofday(&last, NULL);
1379 getrusage(RUSAGE_SELF, &me_last);
1381 // the first time, give a zero to allow
1382 // netdata calibrate to the current time
1383 // usec = update_every * 1000000ULL;
1389 gettimeofday(&now, NULL);
1390 getrusage(RUSAGE_SELF, &me);
1392 usec = usecdiff(&now, &last);
1393 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
1394 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
1396 bcopy(&now, &last, sizeof(struct timeval));
1397 bcopy(&me, &me_last, sizeof(struct rusage));
1400 fprintf(stdout, "BEGIN apps.cpu %llu\n", usec);
1401 for (w = target_root; w ; w = w->next) {
1402 if(w->target || (!w->processes && !w->exposed)) continue;
1404 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
1406 fprintf(stdout, "END\n");
1408 fprintf(stdout, "BEGIN apps.cpu_user %llu\n", usec);
1409 for (w = target_root; w ; w = w->next) {
1410 if(w->target || (!w->processes && !w->exposed)) continue;
1412 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
1414 fprintf(stdout, "END\n");
1416 fprintf(stdout, "BEGIN apps.cpu_system %llu\n", usec);
1417 for (w = target_root; w ; w = w->next) {
1418 if(w->target || (!w->processes && !w->exposed)) continue;
1420 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
1422 fprintf(stdout, "END\n");
1424 fprintf(stdout, "BEGIN apps.threads %llu\n", usec);
1425 for (w = target_root; w ; w = w->next) {
1426 if(w->target || (!w->processes && !w->exposed)) continue;
1428 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
1430 fprintf(stdout, "END\n");
1432 fprintf(stdout, "BEGIN apps.processes %llu\n", usec);
1433 for (w = target_root; w ; w = w->next) {
1434 if(w->target || (!w->processes && !w->exposed)) continue;
1436 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
1438 fprintf(stdout, "END\n");
1440 fprintf(stdout, "BEGIN apps.mem %llu\n", usec);
1441 for (w = target_root; w ; w = w->next) {
1442 if(w->target || (!w->processes && !w->exposed)) continue;
1444 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
1446 fprintf(stdout, "END\n");
1448 fprintf(stdout, "BEGIN apps.minor_faults %llu\n", usec);
1449 for (w = target_root; w ; w = w->next) {
1450 if(w->target || (!w->processes && !w->exposed)) continue;
1452 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt);
1454 fprintf(stdout, "END\n");
1456 fprintf(stdout, "BEGIN apps.major_faults %llu\n", usec);
1457 for (w = target_root; w ; w = w->next) {
1458 if(w->target || (!w->processes && !w->exposed)) continue;
1460 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt);
1462 fprintf(stdout, "END\n");
1464 fprintf(stdout, "BEGIN apps.lreads %llu\n", usec);
1465 for (w = target_root; w ; w = w->next) {
1466 if(w->target || (!w->processes && !w->exposed)) continue;
1468 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read);
1470 fprintf(stdout, "END\n");
1472 fprintf(stdout, "BEGIN apps.lwrites %llu\n", usec);
1473 for (w = target_root; w ; w = w->next) {
1474 if(w->target || (!w->processes && !w->exposed)) continue;
1476 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written);
1478 fprintf(stdout, "END\n");
1480 fprintf(stdout, "BEGIN apps.preads %llu\n", usec);
1481 for (w = target_root; w ; w = w->next) {
1482 if(w->target || (!w->processes && !w->exposed)) continue;
1484 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read);
1486 fprintf(stdout, "END\n");
1488 fprintf(stdout, "BEGIN apps.pwrites %llu\n", usec);
1489 for (w = target_root; w ; w = w->next) {
1490 if(w->target || (!w->processes && !w->exposed)) continue;
1492 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written);
1494 fprintf(stdout, "END\n");
1496 fprintf(stdout, "BEGIN apps.files %llu\n", usec);
1497 for (w = target_root; w ; w = w->next) {
1498 if(w->target || (!w->processes && !w->exposed)) continue;
1500 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
1502 fprintf(stdout, "END\n");
1504 fprintf(stdout, "BEGIN apps.sockets %llu\n", usec);
1505 for (w = target_root; w ; w = w->next) {
1506 if(w->target || (!w->processes && !w->exposed)) continue;
1508 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
1510 fprintf(stdout, "END\n");
1512 fprintf(stdout, "BEGIN apps.pipes %llu\n", usec);
1513 for (w = target_root; w ; w = w->next) {
1514 if(w->target || (!w->processes && !w->exposed)) continue;
1516 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
1518 fprintf(stdout, "END\n");
1520 fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
1521 fprintf(stdout, "SET user = %llu\n", cpuuser);
1522 fprintf(stdout, "SET system = %llu\n", cpusyst);
1523 fprintf(stdout, "END\n");
1525 fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
1526 fprintf(stdout, "SET files = %llu\n", file_counter);
1527 fprintf(stdout, "SET pids = %ld\n", pids);
1528 fprintf(stdout, "SET fds = %d\n", all_files_len);
1529 fprintf(stdout, "SET targets = %ld\n", targets);
1530 fprintf(stdout, "END\n");
1536 // ----------------------------------------------------------------------------
1537 // generate the charts
1539 void show_charts(void)
1542 int newly_added = 0;
1544 for(w = target_root ; w ; w = w->next)
1545 if(!w->exposed && w->processes) {
1548 if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
1551 // nothing more to show
1552 if(!newly_added) return;
1554 // we have something new to show
1555 // update the charts
1556 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);
1557 for (w = target_root; w ; w = w->next) {
1558 if(w->target || (!w->processes && !w->exposed)) continue;
1560 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu %s\n", w->name, (unsigned long long)(Hertz * update_every), w->hidden?"hidden":"");
1563 fprintf(stdout, "CHART apps.mem '' 'Apps Dedicated Memory (w/o shared)' 'MB' apps apps stacked 20003 %d\n", update_every);
1564 for (w = target_root; w ; w = w->next) {
1565 if(w->target || (!w->processes && !w->exposed)) continue;
1567 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
1570 fprintf(stdout, "CHART apps.threads '' 'Apps Threads' 'threads' apps apps stacked 20005 %d\n", update_every);
1571 for (w = target_root; w ; w = w->next) {
1572 if(w->target || (!w->processes && !w->exposed)) continue;
1574 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1577 fprintf(stdout, "CHART apps.processes '' 'Apps Processes' 'processes' apps apps stacked 20004 %d\n", update_every);
1578 for (w = target_root; w ; w = w->next) {
1579 if(w->target || (!w->processes && !w->exposed)) continue;
1581 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1584 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);
1585 for (w = target_root; w ; w = w->next) {
1586 if(w->target || (!w->processes && !w->exposed)) continue;
1588 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors * update_every);
1591 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);
1592 for (w = target_root; w ; w = w->next) {
1593 if(w->target || (!w->processes && !w->exposed)) continue;
1595 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu\n", w->name, Hertz * processors * update_every);
1598 fprintf(stdout, "CHART apps.major_faults '' 'Apps Major Page Faults (swaps in)' 'page faults/s' apps apps stacked 20010 %d\n", update_every);
1599 for (w = target_root; w ; w = w->next) {
1600 if(w->target || (!w->processes && !w->exposed)) continue;
1602 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, update_every);
1605 fprintf(stdout, "CHART apps.minor_faults '' 'Apps Minor Page Faults' 'page faults/s' apps none stacked 20011 %d\n", update_every);
1606 for (w = target_root; w ; w = w->next) {
1607 if(w->target || (!w->processes && !w->exposed)) continue;
1609 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, update_every);
1612 fprintf(stdout, "CHART apps.lreads '' 'Apps Disk Logical Reads' 'kilobytes/s' apps none stacked 20042 %d\n", update_every);
1613 for (w = target_root; w ; w = w->next) {
1614 if(w->target || (!w->processes && !w->exposed)) continue;
1616 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1619 fprintf(stdout, "CHART apps.lwrites '' 'Apps I/O Logical Writes' 'kilobytes/s' apps none stacked 20042 %d\n", update_every);
1620 for (w = target_root; w ; w = w->next) {
1621 if(w->target || (!w->processes && !w->exposed)) continue;
1623 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1626 fprintf(stdout, "CHART apps.preads '' 'Apps Disk Reads' 'kilobytes/s' apps apps stacked 20002 %d\n", update_every);
1627 for (w = target_root; w ; w = w->next) {
1628 if(w->target || (!w->processes && !w->exposed)) continue;
1630 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1633 fprintf(stdout, "CHART apps.pwrites '' 'Apps Disk Writes' 'kilobytes/s' apps apps stacked 20002 %d\n", update_every);
1634 for (w = target_root; w ; w = w->next) {
1635 if(w->target || (!w->processes && !w->exposed)) continue;
1637 fprintf(stdout, "DIMENSION %s '' incremental 1 %d\n", w->name, 1024 * update_every);
1640 fprintf(stdout, "CHART apps.files '' 'Apps Open Files' 'open files' apps apps stacked 20050 %d\n", update_every);
1641 for (w = target_root; w ; w = w->next) {
1642 if(w->target || (!w->processes && !w->exposed)) continue;
1644 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1647 fprintf(stdout, "CHART apps.sockets '' 'Apps Open Sockets' 'open sockets' apps apps stacked 20051 %d\n", update_every);
1648 for (w = target_root; w ; w = w->next) {
1649 if(w->target || (!w->processes && !w->exposed)) continue;
1651 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1654 fprintf(stdout, "CHART apps.pipes '' 'Apps Pipes' 'open pipes' apps none stacked 20053 %d\n", update_every);
1655 for (w = target_root; w ; w = w->next) {
1656 if(w->target || (!w->processes && !w->exposed)) continue;
1658 fprintf(stdout, "DIMENSION %s '' absolute 1 1\n", w->name);
1661 fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' netdata netdata stacked 10000 %d\n", update_every);
1662 fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000 * update_every);
1663 fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000 * update_every);
1665 fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' netdata netdata line 10001 %d\n", update_every);
1666 fprintf(stdout, "DIMENSION files '' incremental 1 %d\n", update_every);
1667 fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
1668 fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
1669 fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
1675 // ----------------------------------------------------------------------------
1676 // parse command line arguments
1678 void parse_args(int argc, char **argv)
1683 for(i = 1; i < argc; i++) {
1685 int n = atoi(argv[i]);
1692 if(strcmp("debug", argv[i]) == 0) {
1702 error("apps.plugin: ERROR: cannot understand option %s", argv[i]);
1706 if(freq > 0) update_every = freq;
1707 if(!name) name = "groups";
1709 if(read_process_groups(name)) {
1710 error("apps.plugin: ERROR: cannot read process groups %s", name);
1715 int main(int argc, char **argv)
1717 // debug_flags = D_PROCFILE;
1719 info("apps.plugin: starting...");
1721 unsigned long started_t = time(NULL), current_t;
1722 Hertz = get_hertz();
1723 pid_max = get_pid_max();
1724 processors = get_processors();
1726 parse_args(argc, argv);
1728 all_pids = calloc(sizeof(struct pid_stat *), pid_max);
1730 error("apps.plugin: ERROR: cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
1731 printf("DISABLE\n");
1735 unsigned long long counter = 1;
1736 unsigned long long usec = 0, susec = 0;
1737 struct timeval last, now;
1738 gettimeofday(&last, NULL);
1740 for(;1; counter++) {
1741 if(!update_from_proc()) {
1742 error("apps.plugin: ERROR: cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
1743 printf("DISABLE\n");
1747 update_statistics();
1748 show_charts(); // this is smart enough to show only newly added apps, when needed
1751 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
1754 gettimeofday(&now, NULL);
1755 usec = usecdiff(&now, &last) - susec;
1756 if(debug) fprintf(stderr, "apps.plugin: last loop took %llu usec (worked for %llu, sleeped for %llu).\n", usec + susec, usec, susec);
1758 // if the last loop took less than half the time
1759 // wait the rest of the time
1760 if(usec < (update_every * 1000000ULL / 2)) susec = (update_every * 1000000ULL) - usec;
1761 else susec = update_every * 1000000ULL / 2;
1764 bcopy(&now, &last, sizeof(struct timeval));
1766 current_t = time(NULL);
1767 if(current_t - started_t > 3600) exit(0);