]> arthur.barton.de Git - netdata.git/blob - src/apps_plugin.c
apps.plugin: major new features and optimizations: now it reports system usage (all...
[netdata.git] / src / apps_plugin.c
1 // TODO
2 //
3 // 1. disable RESET_OR_OVERFLOW check in charts
4
5 #ifdef HAVE_CONFIG_H
6 #include <config.h>
7 #endif
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <time.h>
12 #include <unistd.h>
13 #include <sys/types.h>
14 #include <sys/time.h>
15 #include <sys/wait.h>
16 #include <sys/stat.h>
17
18 #include <sys/resource.h>
19 #include <sys/stat.h>
20
21 #include <errno.h>
22 #include <stdarg.h>
23 #include <locale.h>
24 #include <ctype.h>
25 #include <fcntl.h>
26
27 #include <malloc.h>
28 #include <dirent.h>
29 #include <arpa/inet.h>
30
31 #include <sys/types.h>
32 #include <pwd.h>
33 #include <grp.h>
34
35 #include "avl.h"
36
37 #include "common.h"
38 #include "log.h"
39 #include "procfile.h"
40
41 #define MAX_COMPARE_NAME 15
42 #define MAX_NAME 100
43
44 unsigned long long Hertz = 1;
45
46 long processors = 1;
47 long pid_max = 32768;
48 int debug = 0;
49
50 int update_every = 1;
51 unsigned long long file_counter = 0;
52
53 char *host_prefix = "";
54 char *config_dir = CONFIG_DIR;
55
56 #ifdef NETDATA_INTERNAL_CHECKS
57 // ----------------------------------------------------------------------------
58 // memory debugger
59 // do not use in production systems - it mis-aligns allocated memory
60
61 struct allocations {
62         size_t allocations;
63         size_t allocated;
64         size_t allocated_max;
65 } allocations = { 0, 0, 0 };
66
67 #define MALLOC_MARK (uint32_t)(0x0BADCAFE)
68 #define MALLOC_PREFIX (sizeof(uint32_t) * 2)
69 #define MALLOC_SUFFIX (sizeof(uint32_t))
70 #define MALLOC_OVERHEAD (MALLOC_PREFIX + MALLOC_SUFFIX)
71
72 void *mark_allocation(void *allocated_ptr, size_t size_without_overheads) {
73         uint32_t *real_ptr = (uint32_t *)allocated_ptr;
74         real_ptr[0] = MALLOC_MARK;
75         real_ptr[1] = (uint32_t) size_without_overheads;
76
77         uint32_t *end_ptr = (uint32_t *)(allocated_ptr + MALLOC_PREFIX + size_without_overheads);
78         end_ptr[0] = MALLOC_MARK;
79
80         // fprintf(stderr, "MEMORY_POINTER: Allocated at %p, returning %p.\n", allocated_ptr, (void *)(allocated_ptr + MALLOC_PREFIX));
81
82         return allocated_ptr + MALLOC_PREFIX;
83 }
84
85 void *check_allocation(const char *file, int line, const char *function, void *marked_ptr, size_t *size_without_overheads_ptr) {
86         uint32_t *real_ptr = (uint32_t *)(marked_ptr - MALLOC_PREFIX);
87
88         // fprintf(stderr, "MEMORY_POINTER: Checking pointer at %p, real %p for %s/%u@%s.\n", marked_ptr, (void *)(marked_ptr - MALLOC_PREFIX), function, line, file);
89
90         if(real_ptr[0] != MALLOC_MARK) fatal("MEMORY: prefix MARK is not valid for %s/%u@%s.", function, line, file);
91
92         size_t size = real_ptr[1];
93
94         uint32_t *end_ptr = (uint32_t *)(marked_ptr + size);
95         if(end_ptr[0] != MALLOC_MARK) fatal("MEMORY: suffix MARK of allocation with size %zu is not valid for %s/%u@%s.", size, function, line, file);
96
97         if(size_without_overheads_ptr) *size_without_overheads_ptr = size;
98
99         return real_ptr;
100 }
101
102 void *malloc_debug(const char *file, int line, const char *function, size_t size) {
103         void *ptr = malloc(size + MALLOC_OVERHEAD);
104         if(!ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
105
106         allocations.allocated += size;
107         allocations.allocations++;
108
109         debug(D_MEMORY, "MEMORY: Allocated %zu bytes for %s/%u@%s."
110                 " Status: allocated %zu in %zu allocs."
111                 , size
112                 , function, line, file
113                 , allocations.allocated
114                 , allocations.allocations
115         );
116
117         if(allocations.allocated > allocations.allocated_max) {
118                 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
119                 allocations.allocated_max = allocations.allocated;
120         }
121
122         size_t csize;
123         check_allocation(file, line, function, mark_allocation(ptr, size), &csize);
124         if(size != csize) {
125                 fatal("Invalid size.");
126         }
127
128         return mark_allocation(ptr, size);
129 }
130
131 void *calloc_debug(const char *file, int line, const char *function, size_t nmemb, size_t size) {
132         void *ptr = malloc_debug(file, line, function, (nmemb * size));
133         bzero(ptr, nmemb * size);
134         return ptr;
135 }
136
137 void free_debug(const char *file, int line, const char *function, void *ptr) {
138         size_t size;
139         void *real_ptr = check_allocation(file, line, function, ptr, &size);
140
141         bzero(real_ptr, size + MALLOC_OVERHEAD);
142
143         free(real_ptr);
144         allocations.allocated -= size;
145         allocations.allocations--;
146
147         debug(D_MEMORY, "MEMORY: freed %zu bytes for %s/%u@%s."
148                 " Status: allocated %zu in %zu allocs."
149                 , size
150                 , function, line, file
151                 , allocations.allocated
152                 , allocations.allocations
153         );
154 }
155
156 void *realloc_debug(const char *file, int line, const char *function, void *ptr, size_t size) {
157         if(!ptr) return malloc_debug(file, line, function, size);
158         if(!size) { free_debug(file, line, function, ptr); return NULL; }
159
160         size_t old_size;
161         void *real_ptr = check_allocation(file, line, function, ptr, &old_size);
162
163         void *new_ptr = realloc(real_ptr, size + MALLOC_OVERHEAD);
164         if(!new_ptr) fatal("MEMORY: Cannot allocate %zu bytes for %s/%u@%s.", size, function, line, file);
165
166         allocations.allocated += size;
167         allocations.allocated -= old_size;
168
169         debug(D_MEMORY, "MEMORY: Re-allocated from %zu to %zu bytes for %s/%u@%s."
170                 " Status: allocated %z in %zu allocs."
171                 , old_size, size
172                 , function, line, file
173                 , allocations.allocated
174                 , allocations.allocations
175         );
176
177         if(allocations.allocated > allocations.allocated_max) {
178                 debug(D_MEMORY, "MEMORY: total allocation peak increased from %zu to %zu", allocations.allocated_max, allocations.allocated);
179                 allocations.allocated_max = allocations.allocated;
180         }
181
182         return mark_allocation(new_ptr, size);
183 }
184
185 char *strdup_debug(const char *file, int line, const char *function, const char *ptr) {
186         size_t size = 0;
187         const char *s = ptr;
188
189         while(*s++) size++;
190         size++;
191
192         char *p = malloc_debug(file, line, function, size);
193         if(!p) fatal("Cannot allocate %zu bytes.", size);
194
195         memcpy(p, ptr, size);
196         return p;
197 }
198
199 #define malloc(size) malloc_debug(__FILE__, __LINE__, __FUNCTION__, (size))
200 #define calloc(nmemb, size) calloc_debug(__FILE__, __LINE__, __FUNCTION__, (nmemb), (size))
201 #define realloc(ptr, size) realloc_debug(__FILE__, __LINE__, __FUNCTION__, (ptr), (size))
202 #define free(ptr) free_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
203
204 #ifdef strdup
205 #undef strdup
206 #endif
207 #define strdup(ptr) strdup_debug(__FILE__, __LINE__, __FUNCTION__, (ptr))
208
209 #endif /* NETDATA_INTERNAL_CHECKS */
210
211
212 // ----------------------------------------------------------------------------
213 // system functions
214 // to retrieve settings of the system
215
216 procfile *ff = NULL;
217
218 long get_system_cpus(void) {
219         int processors = 0;
220
221         char filename[FILENAME_MAX + 1];
222         snprintf(filename, FILENAME_MAX, "%s/proc/stat", host_prefix);
223
224         ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
225         if(!ff) return 1;
226
227         ff = procfile_readall(ff);
228         if(!ff) {
229                 // procfile_close(ff);
230                 return 1;
231         }
232
233         unsigned int i;
234         for(i = 0; i < procfile_lines(ff); i++) {
235                 if(!procfile_linewords(ff, i)) continue;
236
237                 if(strncmp(procfile_lineword(ff, i, 0), "cpu", 3) == 0) processors++;
238         }
239         processors--;
240         if(processors < 1) processors = 1;
241
242         // procfile_close(ff);
243         return processors;
244 }
245
246 long get_system_pid_max(void) {
247         long mpid = 32768;
248
249         char filename[FILENAME_MAX + 1];
250         snprintf(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", host_prefix);
251         ff = procfile_reopen(ff, filename, "", PROCFILE_FLAG_DEFAULT);
252         if(!ff) return mpid;
253
254         ff = procfile_readall(ff);
255         if(!ff) {
256                 // procfile_close(ff);
257                 return mpid;
258         }
259
260         mpid = atol(procfile_lineword(ff, 0, 0));
261         if(!mpid) mpid = 32768;
262
263         // procfile_close(ff);
264         return mpid;
265 }
266
267 unsigned long long get_system_hertz(void)
268 {
269         unsigned long long myhz = 1;
270
271 #ifdef _SC_CLK_TCK
272         if((myhz = (unsigned long long int) sysconf(_SC_CLK_TCK)) > 0) {
273                 return myhz;
274         }
275 #endif
276
277 #ifdef HZ
278         myhz = HZ;    /* <asm/param.h> */
279 #else /* HZ */
280         /* If 32-bit or big-endian (not Alpha or ia64), assume HZ is 100. */
281         hz = (sizeof(long)==sizeof(int) || htons(999)==999) ? 100UL : 1024UL;
282 #endif /* HZ */
283
284         error("Unknown HZ value. Assuming %llu.", myhz);
285         return myhz;
286 }
287
288
289 // ----------------------------------------------------------------------------
290 // target
291 // target is the structure that process data are aggregated
292
293 struct target {
294         char compare[MAX_COMPARE_NAME + 1];
295         uint32_t hash;
296
297         char id[MAX_NAME + 1];
298         char name[MAX_NAME + 1];
299
300         uid_t uid;
301         gid_t gid;
302
303         unsigned long long minflt;
304         unsigned long long cminflt;
305         unsigned long long majflt;
306         unsigned long long cmajflt;
307         unsigned long long utime;
308         unsigned long long stime;
309         unsigned long long cutime;
310         unsigned long long cstime;
311         unsigned long long num_threads;
312         unsigned long long rss;
313
314         unsigned long long fix_minflt;
315         unsigned long long fix_cminflt;
316         unsigned long long fix_majflt;
317         unsigned long long fix_cmajflt;
318         unsigned long long fix_utime;
319         unsigned long long fix_stime;
320         unsigned long long fix_cutime;
321         unsigned long long fix_cstime;
322
323         unsigned long long statm_size;
324         unsigned long long statm_resident;
325         unsigned long long statm_share;
326         unsigned long long statm_text;
327         unsigned long long statm_lib;
328         unsigned long long statm_data;
329         unsigned long long statm_dirty;
330
331         unsigned long long io_logical_bytes_read;
332         unsigned long long io_logical_bytes_written;
333         unsigned long long io_read_calls;
334         unsigned long long io_write_calls;
335         unsigned long long io_storage_bytes_read;
336         unsigned long long io_storage_bytes_written;
337         unsigned long long io_cancelled_write_bytes;
338
339         unsigned long long fix_io_logical_bytes_read;
340         unsigned long long fix_io_logical_bytes_written;
341         unsigned long long fix_io_read_calls;
342         unsigned long long fix_io_write_calls;
343         unsigned long long fix_io_storage_bytes_read;
344         unsigned long long fix_io_storage_bytes_written;
345         unsigned long long fix_io_cancelled_write_bytes;
346
347         int *fds;
348         unsigned long long openfiles;
349         unsigned long long openpipes;
350         unsigned long long opensockets;
351         unsigned long long openinotifies;
352         unsigned long long openeventfds;
353         unsigned long long opentimerfds;
354         unsigned long long opensignalfds;
355         unsigned long long openeventpolls;
356         unsigned long long openother;
357
358         unsigned long processes;        // how many processes have been merged to this
359         int exposed;                            // if set, we have sent this to netdata
360         int hidden;                                     // if set, we set the hidden flag on the dimension
361         int debug;
362
363         struct target *target;          // the one that will be reported to netdata
364         struct target *next;
365 };
366
367
368 // ----------------------------------------------------------------------------
369 // apps_groups.conf
370 // aggregate all processes in groups, to have a limited number of dimensions
371
372 struct target *apps_groups_root_target = NULL;
373 struct target *apps_groups_default_target = NULL;
374 long apps_groups_targets = 0;
375
376 struct target *users_root_target = NULL;
377 struct target *groups_root_target = NULL;
378
379 struct target *get_users_target(uid_t uid)
380 {
381         struct target *w;
382         for(w = users_root_target ; w ; w = w->next)
383                 if(w->uid == uid) return w;
384
385         w = calloc(sizeof(struct target), 1);
386         if(unlikely(!w)) {
387                 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
388                 return NULL;
389         }
390
391         snprintf(w->compare, MAX_COMPARE_NAME, "%d", uid);
392         w->hash = simple_hash(w->compare);
393
394         snprintf(w->id, MAX_NAME, "%d", uid);
395
396         struct passwd *pw = getpwuid(uid);
397         if(!pw)
398                 snprintf(w->name, MAX_NAME, "%d", uid);
399         else
400                 snprintf(w->name, MAX_NAME, "%s", pw->pw_name);
401
402         w->uid = uid;
403
404         w->next = users_root_target;
405         users_root_target = w;
406
407         if(unlikely(debug))
408                 fprintf(stderr, "apps.plugin: added uid %d ('%s') target\n", w->uid, w->name);
409
410         return w;
411 }
412
413 struct target *get_groups_target(gid_t gid)
414 {
415         struct target *w;
416         for(w = groups_root_target ; w ; w = w->next)
417                 if(w->gid == gid) return w;
418
419         w = calloc(sizeof(struct target), 1);
420         if(unlikely(!w)) {
421                 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
422                 return NULL;
423         }
424
425         snprintf(w->compare, MAX_COMPARE_NAME, "%d", gid);
426         w->hash = simple_hash(w->compare);
427
428         snprintf(w->id, MAX_NAME, "%d", gid);
429
430         struct group *gr = getgrgid(gid);
431         if(!gr)
432                 snprintf(w->name, MAX_NAME, "%d", gid);
433         else
434                 snprintf(w->name, MAX_NAME, "%s", gr->gr_name);
435
436         w->gid = gid;
437
438         w->next = groups_root_target;
439         groups_root_target = w;
440
441         if(unlikely(debug))
442                 fprintf(stderr, "apps.plugin: added gid %d ('%s') target\n", w->gid, w->name);
443
444         return w;
445 }
446
447 // find or create a new target
448 // there are targets that are just aggregated to other target (the second argument)
449 struct target *get_apps_groups_target(const char *id, struct target *target)
450 {
451         const char *nid = id;
452         if(nid[0] == '-') nid++;
453
454         struct target *w;
455         for(w = apps_groups_root_target ; w ; w = w->next)
456                 if(strncmp(nid, w->id, MAX_NAME) == 0) return w;
457
458         w = calloc(sizeof(struct target), 1);
459         if(unlikely(!w)) {
460                 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct target));
461                 return NULL;
462         }
463
464         strncpy(w->id, nid, MAX_NAME);
465         strncpy(w->name, nid, MAX_NAME);
466         strncpy(w->compare, nid, MAX_COMPARE_NAME);
467         w->hash = simple_hash(w->compare);
468
469         if(id[0] == '-') w->hidden = 1;
470
471         w->target = target;
472
473         w->next = apps_groups_root_target;
474         apps_groups_root_target = w;
475
476         if(unlikely(debug))
477                 fprintf(stderr, "apps.plugin: adding hook for process '%s', compare '%s' on target '%s'\n", w->id, w->compare, w->target?w->target->id:"");
478
479         return w;
480 }
481
482 // read the apps_groups.conf file
483 int read_apps_groups_conf(const char *name)
484 {
485         char buffer[4096+1];
486         char filename[FILENAME_MAX + 1];
487
488         snprintf(filename, FILENAME_MAX, "%s/apps_%s.conf", config_dir, name);
489
490         if(unlikely(debug))
491                 fprintf(stderr, "apps.plugin: process groups file: '%s'\n", filename);
492
493         FILE *fp = fopen(filename, "r");
494         if(unlikely(!fp)) {
495                 error("Cannot open file '%s'", filename);
496                 return 1;
497         }
498
499         long line = 0;
500         while(fgets(buffer, 4096, fp) != NULL) {
501                 int whidden = 0, wdebug = 0;
502                 line++;
503
504                 // if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", buffer);
505
506                 char *s = buffer, *t, *p;
507                 s = trim(s);
508                 if(!s || !*s || *s == '#') continue;
509
510                 if(debug) fprintf(stderr, "apps.plugin: \tread %s\n", s);
511
512                 // the target name
513                 t = strsep(&s, ":");
514                 if(t) t = trim(t);
515                 if(!t || !*t) continue;
516
517                 while(t[0]) {
518                         int stop = 1;
519
520                         switch(t[0]) {
521                                 case '-':
522                                         stop = 0;
523                                         whidden = 1;
524                                         t++;
525                                         break;
526
527                                 case '+':
528                                         stop = 0;
529                                         wdebug = 1;
530                                         t++;
531                                         break;
532                         }
533
534                         if(stop) break;
535                 }
536
537                 if(debug) fprintf(stderr, "apps.plugin: \t\ttarget %s\n", t);
538
539                 struct target *w = NULL;
540                 long count = 0;
541                 int blen = 0;
542                 char buffer[4097] = "";
543                 buffer[4096] = '\0';
544
545                 // the process names
546                 while((p = strsep(&s, " "))) {
547                         p = trim(p);
548                         if(!p || !*p) continue;
549
550                         strncpy(&buffer[blen], p, 4096 - blen);
551                         blen = strlen(buffer);
552
553                         while(buffer[blen - 1] == '\\') {
554                                 buffer[blen - 1] = ' ';
555
556                                 if((p = strsep(&s, " ")))
557                                         p = trim(p);
558
559                                 if(!p || !*p) p = " ";
560                                 strncpy(&buffer[blen], p, 4096 - blen);
561                                 blen = strlen(buffer);
562                         }
563
564                         struct target *n = get_apps_groups_target(buffer, w);
565                         n->hidden = whidden;
566                         n->debug = wdebug;
567                         if(!w) w = n;
568
569                         buffer[0] = '\0';
570                         blen = 0;
571
572                         count++;
573                 }
574
575                 if(w) strncpy(w->name, t, MAX_NAME);
576                 if(!count) error("The line %ld on file '%s', for group '%s' does not state any process names.", line, filename, t);
577         }
578         fclose(fp);
579
580         apps_groups_default_target = get_apps_groups_target("+p!o@w#e$i^r&7*5(-i)l-o_", NULL); // match nothing
581         strncpy(apps_groups_default_target->name, "other", MAX_NAME);
582
583         return 0;
584 }
585
586
587 // ----------------------------------------------------------------------------
588 // data to store for each pid
589 // see: man proc
590
591 struct pid_stat {
592         int32_t pid;
593         char comm[MAX_COMPARE_NAME + 1];
594         // char state;
595         int32_t ppid;
596         // int32_t pgrp;
597         // int32_t session;
598         // int32_t tty_nr;
599         // int32_t tpgid;
600         // uint64_t flags;
601         unsigned long long minflt;
602         unsigned long long cminflt;
603         unsigned long long majflt;
604         unsigned long long cmajflt;
605         unsigned long long utime;
606         unsigned long long stime;
607         unsigned long long cutime;
608         unsigned long long cstime;
609         // int64_t priority;
610         // int64_t nice;
611         int32_t num_threads;
612         // int64_t itrealvalue;
613         // unsigned long long starttime;
614         // unsigned long long vsize;
615         unsigned long long rss;
616         // unsigned long long rsslim;
617         // unsigned long long starcode;
618         // unsigned long long endcode;
619         // unsigned long long startstack;
620         // unsigned long long kstkesp;
621         // unsigned long long kstkeip;
622         // uint64_t signal;
623         // uint64_t blocked;
624         // uint64_t sigignore;
625         // uint64_t sigcatch;
626         // uint64_t wchan;
627         // uint64_t nswap;
628         // uint64_t cnswap;
629         // int32_t exit_signal;
630         // int32_t processor;
631         // uint32_t rt_priority;
632         // uint32_t policy;
633         // unsigned long long delayacct_blkio_ticks;
634         // uint64_t guest_time;
635         // int64_t cguest_time;
636
637         uid_t uid;
638         gid_t gid;
639
640         unsigned long long statm_size;
641         unsigned long long statm_resident;
642         unsigned long long statm_share;
643         unsigned long long statm_text;
644         unsigned long long statm_lib;
645         unsigned long long statm_data;
646         unsigned long long statm_dirty;
647
648         unsigned long long io_logical_bytes_read;
649         unsigned long long io_logical_bytes_written;
650         unsigned long long io_read_calls;
651         unsigned long long io_write_calls;
652         unsigned long long io_storage_bytes_read;
653         unsigned long long io_storage_bytes_written;
654         unsigned long long io_cancelled_write_bytes;
655
656         // we need the last values
657         // for all incremental counters
658         // so that when a process switches users/groups
659         // we will subtract these values from the old
660         // target
661         unsigned long long last_minflt;
662         unsigned long long last_cminflt;
663         unsigned long long last_majflt;
664         unsigned long long last_cmajflt;
665         unsigned long long last_utime;
666         unsigned long long last_stime;
667         unsigned long long last_cutime;
668         unsigned long long last_cstime;
669
670         unsigned long long last_io_logical_bytes_read;
671         unsigned long long last_io_logical_bytes_written;
672         unsigned long long last_io_read_calls;
673         unsigned long long last_io_write_calls;
674         unsigned long long last_io_storage_bytes_read;
675         unsigned long long last_io_storage_bytes_written;
676         unsigned long long last_io_cancelled_write_bytes;
677
678 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
679         unsigned long long old_utime;
680         unsigned long long old_stime;
681         unsigned long long old_minflt;
682         unsigned long long old_majflt;
683
684         unsigned long long old_cutime;
685         unsigned long long old_cstime;
686         unsigned long long old_cminflt;
687         unsigned long long old_cmajflt;
688
689         unsigned long long fix_cutime;
690         unsigned long long fix_cstime;
691         unsigned long long fix_cminflt;
692         unsigned long long fix_cmajflt;
693
694         unsigned long long diff_cutime;
695         unsigned long long diff_cstime;
696         unsigned long long diff_cminflt;
697         unsigned long long diff_cmajflt;
698 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
699
700         int *fds;                                               // array of fds it uses
701         int fds_size;                                   // the size of the fds array
702
703         int children_count;                             // number of processes directly referencing this
704         int updated;                                    // 1 when update
705         int merged;                                             // 1 when it has been merged to its parent
706         int new_entry;
707
708         struct target *target;                  // app_groups.conf targets
709         struct target *user_target;             // uid based targets
710         struct target *group_target;    // gid based targets
711
712         struct pid_stat *parent;
713         struct pid_stat *prev;
714         struct pid_stat *next;
715
716 } *root_of_pids = NULL, **all_pids;
717
718 long all_pids_count = 0;
719
720 struct pid_stat *get_pid_entry(pid_t pid)
721 {
722         if(all_pids[pid]) {
723                 all_pids[pid]->new_entry = 0;
724                 return all_pids[pid];
725         }
726
727         all_pids[pid] = calloc(sizeof(struct pid_stat), 1);
728         if(!all_pids[pid]) {
729                 error("Cannot allocate %lu bytes of memory", (unsigned long)sizeof(struct pid_stat));
730                 return NULL;
731         }
732
733         all_pids[pid]->fds = calloc(sizeof(int), 100);
734         if(!all_pids[pid]->fds)
735                 error("Cannot allocate %ld bytes of memory", (unsigned long)(sizeof(int) * 100));
736         else all_pids[pid]->fds_size = 100;
737
738         if(root_of_pids) root_of_pids->prev = all_pids[pid];
739         all_pids[pid]->next = root_of_pids;
740         root_of_pids = all_pids[pid];
741
742         all_pids[pid]->pid = pid;
743         all_pids[pid]->new_entry = 1;
744
745         return all_pids[pid];
746 }
747
748 void del_pid_entry(pid_t pid)
749 {
750         if(!all_pids[pid]) return;
751
752         if(debug) fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
753
754         if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
755         if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
756         if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
757
758         if(all_pids[pid]->fds) free(all_pids[pid]->fds);
759         free(all_pids[pid]);
760         all_pids[pid] = NULL;
761 }
762
763
764 // ----------------------------------------------------------------------------
765 // update pids from proc
766
767 int read_proc_pid_ownership(struct pid_stat *p) {
768         char filename[FILENAME_MAX + 1];
769
770         snprintf(filename, FILENAME_MAX, "%s/proc/%d", host_prefix, p->pid);
771
772         // ----------------------------------------
773         // read uid and gid
774
775         struct stat st;
776         if(stat(filename, &st) != 0)
777                 return 1;
778
779         p->uid = st.st_uid;
780         p->gid = st.st_gid;
781
782         return 0;
783 }
784
785 int read_proc_pid_stat(struct pid_stat *p) {
786         char filename[FILENAME_MAX + 1];
787
788         snprintf(filename, FILENAME_MAX, "%s/proc/%d/stat", host_prefix, p->pid);
789
790         // ----------------------------------------
791
792         ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
793         if(!ff) return 1;
794
795         ff = procfile_readall(ff);
796         if(!ff) {
797                 // procfile_close(ff);
798                 return 1;
799         }
800
801         file_counter++;
802
803         p->comm[0] = '\0';
804         p->comm[MAX_COMPARE_NAME] = '\0';
805         size_t blen = 0;
806
807         char *s = procfile_lineword(ff, 0, 1);
808         if(*s == '(') s++;
809         size_t len = strlen(s);
810         unsigned int i = 0;
811         while(len && s[len - 1] != ')') {
812                 if(blen < MAX_COMPARE_NAME) {
813                         strncpy(&p->comm[blen], s, MAX_COMPARE_NAME - blen);
814                         blen = strlen(p->comm);
815                 }
816
817                 i++;
818                 s = procfile_lineword(ff, 0, 1+i);
819                 len = strlen(s);
820         }
821         if(len && s[len - 1] == ')') s[len - 1] = '\0';
822         if(blen < MAX_COMPARE_NAME)
823                 strncpy(&p->comm[blen], s, MAX_COMPARE_NAME - blen);
824
825         // p->pid                       = atol(procfile_lineword(ff, 0, 0+i));
826         // comm is at 1
827         // p->state                     = *(procfile_lineword(ff, 0, 2+i));
828         p->ppid                         = (int32_t) atol(procfile_lineword(ff, 0, 3 + i));
829         // p->pgrp                      = atol(procfile_lineword(ff, 0, 4+i));
830         // p->session           = atol(procfile_lineword(ff, 0, 5+i));
831         // p->tty_nr            = atol(procfile_lineword(ff, 0, 6+i));
832         // p->tpgid                     = atol(procfile_lineword(ff, 0, 7+i));
833         // p->flags                     = strtoull(procfile_lineword(ff, 0, 8+i), NULL, 10);
834         p->minflt                       = strtoull(procfile_lineword(ff, 0, 9+i), NULL, 10);
835         p->cminflt                      = strtoull(procfile_lineword(ff, 0, 10+i), NULL, 10);
836         p->majflt                       = strtoull(procfile_lineword(ff, 0, 11+i), NULL, 10);
837         p->cmajflt                      = strtoull(procfile_lineword(ff, 0, 12+i), NULL, 10);
838         p->utime                        = strtoull(procfile_lineword(ff, 0, 13+i), NULL, 10);
839         p->stime                        = strtoull(procfile_lineword(ff, 0, 14+i), NULL, 10);
840         p->cutime                       = strtoull(procfile_lineword(ff, 0, 15+i), NULL, 10);
841         p->cstime                       = strtoull(procfile_lineword(ff, 0, 16+i), NULL, 10);
842         // p->priority          = strtoull(procfile_lineword(ff, 0, 17+i), NULL, 10);
843         // p->nice                      = strtoull(procfile_lineword(ff, 0, 18+i), NULL, 10);
844         p->num_threads          = (int32_t) atol(procfile_lineword(ff, 0, 19 + i));
845         // p->itrealvalue       = strtoull(procfile_lineword(ff, 0, 20+i), NULL, 10);
846         // p->starttime         = strtoull(procfile_lineword(ff, 0, 21+i), NULL, 10);
847         // p->vsize                     = strtoull(procfile_lineword(ff, 0, 22+i), NULL, 10);
848         p->rss                          = strtoull(procfile_lineword(ff, 0, 23+i), NULL, 10);
849         // p->rsslim            = strtoull(procfile_lineword(ff, 0, 24+i), NULL, 10);
850         // p->starcode          = strtoull(procfile_lineword(ff, 0, 25+i), NULL, 10);
851         // p->endcode           = strtoull(procfile_lineword(ff, 0, 26+i), NULL, 10);
852         // p->startstack        = strtoull(procfile_lineword(ff, 0, 27+i), NULL, 10);
853         // p->kstkesp           = strtoull(procfile_lineword(ff, 0, 28+i), NULL, 10);
854         // p->kstkeip           = strtoull(procfile_lineword(ff, 0, 29+i), NULL, 10);
855         // p->signal            = strtoull(procfile_lineword(ff, 0, 30+i), NULL, 10);
856         // p->blocked           = strtoull(procfile_lineword(ff, 0, 31+i), NULL, 10);
857         // p->sigignore         = strtoull(procfile_lineword(ff, 0, 32+i), NULL, 10);
858         // p->sigcatch          = strtoull(procfile_lineword(ff, 0, 33+i), NULL, 10);
859         // p->wchan                     = strtoull(procfile_lineword(ff, 0, 34+i), NULL, 10);
860         // p->nswap                     = strtoull(procfile_lineword(ff, 0, 35+i), NULL, 10);
861         // p->cnswap            = strtoull(procfile_lineword(ff, 0, 36+i), NULL, 10);
862         // p->exit_signal       = atol(procfile_lineword(ff, 0, 37+i));
863         // p->processor         = atol(procfile_lineword(ff, 0, 38+i));
864         // p->rt_priority       = strtoul(procfile_lineword(ff, 0, 39+i), NULL, 10);
865         // p->policy            = strtoul(procfile_lineword(ff, 0, 40+i), NULL, 10);
866         // p->delayacct_blkio_ticks             = strtoull(procfile_lineword(ff, 0, 41+i), NULL, 10);
867         // p->guest_time        = strtoull(procfile_lineword(ff, 0, 42+i), NULL, 10);
868         // p->cguest_time       = strtoull(procfile_lineword(ff, 0, 43), NULL, 10);
869
870         if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: VALUES: %s utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu, threads=%d\n", p->comm, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
871
872         // procfile_close(ff);
873         return 0;
874 }
875
876 int read_proc_pid_statm(struct pid_stat *p) {
877         char filename[FILENAME_MAX + 1];
878
879         snprintf(filename, FILENAME_MAX, "%s/proc/%d/statm", host_prefix, p->pid);
880
881         ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
882         if(!ff) return 1;
883
884         ff = procfile_readall(ff);
885         if(!ff) {
886                 // procfile_close(ff);
887                 return 1;
888         }
889
890         file_counter++;
891
892         p->statm_size                   = strtoull(procfile_lineword(ff, 0, 0), NULL, 10);
893         p->statm_resident               = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
894         p->statm_share                  = strtoull(procfile_lineword(ff, 0, 2), NULL, 10);
895         p->statm_text                   = strtoull(procfile_lineword(ff, 0, 3), NULL, 10);
896         p->statm_lib                    = strtoull(procfile_lineword(ff, 0, 4), NULL, 10);
897         p->statm_data                   = strtoull(procfile_lineword(ff, 0, 5), NULL, 10);
898         p->statm_dirty                  = strtoull(procfile_lineword(ff, 0, 6), NULL, 10);
899
900         // procfile_close(ff);
901         return 0;
902 }
903
904 int read_proc_pid_io(struct pid_stat *p) {
905         char filename[FILENAME_MAX + 1];
906
907         snprintf(filename, FILENAME_MAX, "%s/proc/%d/io", host_prefix, p->pid);
908
909         ff = procfile_reopen(ff, filename, NULL, PROCFILE_FLAG_NO_ERROR_ON_FILE_IO);
910         if(!ff) return 1;
911
912         ff = procfile_readall(ff);
913         if(!ff) {
914                 // procfile_close(ff);
915                 return 1;
916         }
917
918         file_counter++;
919
920         p->io_logical_bytes_read                = strtoull(procfile_lineword(ff, 0, 1), NULL, 10);
921         p->io_logical_bytes_written     = strtoull(procfile_lineword(ff, 1, 1), NULL, 10);
922         p->io_read_calls                                = strtoull(procfile_lineword(ff, 2, 1), NULL, 10);
923         p->io_write_calls                               = strtoull(procfile_lineword(ff, 3, 1), NULL, 10);
924         p->io_storage_bytes_read                = strtoull(procfile_lineword(ff, 4, 1), NULL, 10);
925         p->io_storage_bytes_written     = strtoull(procfile_lineword(ff, 5, 1), NULL, 10);
926         p->io_cancelled_write_bytes             = strtoull(procfile_lineword(ff, 6, 1), NULL, 10);
927
928         // procfile_close(ff);
929         return 0;
930 }
931
932
933 // ----------------------------------------------------------------------------
934 // file descriptor
935 // this is used to keep a global list of all open files of the system
936 // it is needed in order to calculate the unique files processes have open
937
938 #define FILE_DESCRIPTORS_INCREASE_STEP 100
939
940 struct file_descriptor {
941         avl avl;
942 #ifdef NETDATA_INTERNAL_CHECKS
943         uint32_t magic;
944 #endif /* NETDATA_INTERNAL_CHECKS */
945         uint32_t hash;
946         const char *name;
947         int type;
948         int count;
949         int pos;
950 } *all_files = NULL;
951
952 int all_files_len = 0;
953 int all_files_size = 0;
954
955 int file_descriptor_compare(void* a, void* b) {
956 #ifdef NETDATA_INTERNAL_CHECKS
957         if(((struct file_descriptor *)a)->magic != 0x0BADCAFE || ((struct file_descriptor *)b)->magic != 0x0BADCAFE)
958                 error("Corrupted index data detected. Please report this.");
959 #endif /* NETDATA_INTERNAL_CHECKS */
960
961         if(((struct file_descriptor *)a)->hash < ((struct file_descriptor *)b)->hash)
962                 return -1;
963
964         else if(((struct file_descriptor *)a)->hash > ((struct file_descriptor *)b)->hash)
965                 return 1;
966
967         else
968                 return strcmp(((struct file_descriptor *)a)->name, ((struct file_descriptor *)b)->name);
969 }
970
971 int file_descriptor_iterator(avl *a) { if(a) {}; return 0; }
972
973 avl_tree all_files_index = {
974                 NULL,
975                 file_descriptor_compare,
976 #ifndef AVL_WITHOUT_PTHREADS
977 #ifdef AVL_LOCK_WITH_MUTEX
978                 PTHREAD_MUTEX_INITIALIZER
979 #else
980                 PTHREAD_RWLOCK_INITIALIZER
981 #endif
982 #endif /* AVL_WITHOUT_PTHREADS */
983 };
984
985 static struct file_descriptor *file_descriptor_find(const char *name, uint32_t hash) {
986         struct file_descriptor *result = NULL, tmp;
987         tmp.hash = (hash)?hash:simple_hash(name);
988         tmp.name = name;
989         tmp.count = 0;
990         tmp.pos = 0;
991 #ifdef NETDATA_INTERNAL_CHECKS
992         tmp.magic = 0x0BADCAFE;
993 #endif /* NETDATA_INTERNAL_CHECKS */
994
995         avl_search(&all_files_index, (avl *)&tmp, file_descriptor_iterator, (avl **)&result);
996         return result;
997 }
998
999 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1000 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1001
1002 #define FILETYPE_OTHER 0
1003 #define FILETYPE_FILE 1
1004 #define FILETYPE_PIPE 2
1005 #define FILETYPE_SOCKET 3
1006 #define FILETYPE_INOTIFY 4
1007 #define FILETYPE_EVENTFD 5
1008 #define FILETYPE_EVENTPOLL 6
1009 #define FILETYPE_TIMERFD 7
1010 #define FILETYPE_SIGNALFD 8
1011
1012 void file_descriptor_not_used(int id)
1013 {
1014         if(id > 0 && id < all_files_size) {
1015
1016 #ifdef NETDATA_INTERNAL_CHECKS
1017                 if(all_files[id].magic != 0x0BADCAFE) {
1018                         error("Ignoring request to remove empty file id %d.", id);
1019                         return;
1020                 }
1021 #endif /* NETDATA_INTERNAL_CHECKS */
1022
1023                 if(debug) fprintf(stderr, "apps.plugin: decreasing slot %d (count = %d).\n", id, all_files[id].count);
1024
1025                 if(all_files[id].count > 0) {
1026                         all_files[id].count--;
1027
1028                         if(!all_files[id].count) {
1029                                 if(debug) fprintf(stderr, "apps.plugin:   >> slot %d is empty.\n", id);
1030                                 file_descriptor_remove(&all_files[id]);
1031 #ifdef NETDATA_INTERNAL_CHECKS
1032                                 all_files[id].magic = 0x00000000;
1033 #endif /* NETDATA_INTERNAL_CHECKS */
1034                                 all_files_len--;
1035                         }
1036                 }
1037                 else
1038                         error("Request to decrease counter of fd %d (%s), while the use counter is 0", id, all_files[id].name);
1039         }
1040         else    error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1041 }
1042
1043 int file_descriptor_find_or_add(const char *name)
1044 {
1045         static int last_pos = 0;
1046         uint32_t hash = simple_hash(name);
1047
1048         if(debug) fprintf(stderr, "apps.plugin: adding or finding name '%s' with hash %u\n", name, hash);
1049
1050         struct file_descriptor *fd = file_descriptor_find(name, hash);
1051         if(fd) {
1052                 // found
1053                 if(debug) fprintf(stderr, "apps.plugin:   >> found on slot %d\n", fd->pos);
1054                 fd->count++;
1055                 return fd->pos;
1056         }
1057         // not found
1058
1059         // check we have enough memory to add it
1060         if(!all_files || all_files_len == all_files_size) {
1061                 void *old = all_files;
1062                 int i;
1063
1064                 // there is no empty slot
1065                 if(debug) fprintf(stderr, "apps.plugin: extending fd array to %d entries\n", all_files_size + FILE_DESCRIPTORS_INCREASE_STEP);
1066                 all_files = realloc(all_files, (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP) * sizeof(struct file_descriptor));
1067
1068                 // if the address changed, we have to rebuild the index
1069                 // since all pointers are now invalid
1070                 if(old && old != (void *)all_files) {
1071                         if(debug) fprintf(stderr, "apps.plugin:   >> re-indexing.\n");
1072                         all_files_index.root = NULL;
1073                         for(i = 0; i < all_files_size; i++) {
1074                                 if(!all_files[i].count) continue;
1075                                 file_descriptor_add(&all_files[i]);
1076                         }
1077                         if(debug) fprintf(stderr, "apps.plugin:   >> re-indexing done.\n");
1078                 }
1079
1080                 for(i = all_files_size; i < (all_files_size + FILE_DESCRIPTORS_INCREASE_STEP); i++) {
1081                         all_files[i].count = 0;
1082                         all_files[i].name = NULL;
1083 #ifdef NETDATA_INTERNAL_CHECKS
1084                         all_files[i].magic = 0x00000000;
1085 #endif /* NETDATA_INTERNAL_CHECKS */
1086                         all_files[i].pos = i;
1087                 }
1088
1089                 if(!all_files_size) all_files_len = 1;
1090                 all_files_size += FILE_DESCRIPTORS_INCREASE_STEP;
1091         }
1092
1093         if(debug) fprintf(stderr, "apps.plugin:   >> searching for empty slot.\n");
1094
1095         // search for an empty slot
1096         int i, c;
1097         for(i = 0, c = last_pos ; i < all_files_size ; i++, c++) {
1098                 if(c >= all_files_size) c = 0;
1099                 if(c == 0) continue;
1100
1101                 if(!all_files[c].count) {
1102                         if(debug) fprintf(stderr, "apps.plugin:   >> Examining slot %d.\n", c);
1103
1104 #ifdef NETDATA_INTERNAL_CHECKS
1105                         if(all_files[c].magic == 0x0BADCAFE && all_files[c].name && file_descriptor_find(all_files[c].name, all_files[c].hash))
1106                                 error("fd on position %d is not cleared properly. It still has %s in it.\n", c, all_files[c].name);
1107 #endif /* NETDATA_INTERNAL_CHECKS */
1108
1109                         if(debug) fprintf(stderr, "apps.plugin:   >> %s fd position %d for %s (last name: %s)\n", all_files[c].name?"re-using":"using", c, name, all_files[c].name);
1110                         if(all_files[c].name) free((void *)all_files[c].name);
1111                         all_files[c].name = NULL;
1112                         last_pos = c;
1113                         break;
1114                 }
1115         }
1116         if(i == all_files_size) {
1117                 fatal("We should find an empty slot, but there isn't any");
1118                 exit(1);
1119         }
1120         if(debug) fprintf(stderr, "apps.plugin:   >> updating slot %d.\n", c);
1121
1122         all_files_len++;
1123
1124         // else we have an empty slot in 'c'
1125
1126         int type;
1127         if(name[0] == '/') type = FILETYPE_FILE;
1128         else if(strncmp(name, "pipe:", 5) == 0) type = FILETYPE_PIPE;
1129         else if(strncmp(name, "socket:", 7) == 0) type = FILETYPE_SOCKET;
1130         else if(strcmp(name, "anon_inode:inotify") == 0 || strcmp(name, "inotify") == 0) type = FILETYPE_INOTIFY;
1131         else if(strcmp(name, "anon_inode:[eventfd]") == 0) type = FILETYPE_EVENTFD;
1132         else if(strcmp(name, "anon_inode:[eventpoll]") == 0) type = FILETYPE_EVENTPOLL;
1133         else if(strcmp(name, "anon_inode:[timerfd]") == 0) type = FILETYPE_TIMERFD;
1134         else if(strcmp(name, "anon_inode:[signalfd]") == 0) type = FILETYPE_SIGNALFD;
1135         else if(strncmp(name, "anon_inode:", 11) == 0) {
1136                 if(debug) fprintf(stderr, "apps.plugin: FIXME: unknown anonymous inode: %s\n", name);
1137                 type = FILETYPE_OTHER;
1138         }
1139         else {
1140                 if(debug) fprintf(stderr, "apps.plugin: FIXME: cannot understand linkname: %s\n", name);
1141                 type = FILETYPE_OTHER;
1142         }
1143
1144         all_files[c].name = strdup(name);
1145         all_files[c].hash = hash;
1146         all_files[c].type = type;
1147         all_files[c].pos  = c;
1148         all_files[c].count = 1;
1149 #ifdef NETDATA_INTERNAL_CHECKS
1150         all_files[c].magic = 0x0BADCAFE;
1151 #endif /* NETDATA_INTERNAL_CHECKS */
1152         file_descriptor_add(&all_files[c]);
1153
1154         if(debug) fprintf(stderr, "apps.plugin: using fd position %d (name: %s)\n", c, all_files[c].name);
1155
1156         return c;
1157 }
1158
1159 int read_pid_file_descriptors(struct pid_stat *p) {
1160         char dirname[FILENAME_MAX+1];
1161
1162         snprintf(dirname, FILENAME_MAX, "%s/proc/%d/fd", host_prefix, p->pid);
1163         DIR *fds = opendir(dirname);
1164         if(fds) {
1165                 int c;
1166                 struct dirent *de;
1167                 char fdname[FILENAME_MAX + 1];
1168                 char linkname[FILENAME_MAX + 1];
1169
1170                 // make the array negative
1171                 for(c = 0 ; c < p->fds_size ; c++)
1172                         p->fds[c] = -p->fds[c];
1173
1174                 while((de = readdir(fds))) {
1175                         if(strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0)
1176                                 continue;
1177
1178                         // check if the fds array is small
1179                         int fdid = atoi(de->d_name);
1180                         if(fdid < 0) continue;
1181                         if(fdid >= p->fds_size) {
1182                                 // it is small, extend it
1183                                 if(debug) fprintf(stderr, "apps.plugin: extending fd memory slots for %s from %d to %d\n", p->comm, p->fds_size, fdid + 100);
1184                                 p->fds = realloc(p->fds, (fdid + 100) * sizeof(int));
1185                                 if(!p->fds) {
1186                                         error("Cannot re-allocate fds for %s", p->comm);
1187                                         break;
1188                                 }
1189
1190                                 // and initialize it
1191                                 for(c = p->fds_size ; c < (fdid + 100) ; c++) p->fds[c] = 0;
1192                                 p->fds_size = fdid + 100;
1193                         }
1194
1195                         if(p->fds[fdid] == 0) {
1196                                 // we don't know this fd, get it
1197
1198                                 sprintf(fdname, "%s/proc/%d/fd/%s", host_prefix, p->pid, de->d_name);
1199                                 ssize_t l = readlink(fdname, linkname, FILENAME_MAX);
1200                                 if(l == -1) {
1201                                         if(debug || (p->target && p->target->debug)) {
1202                                                 if(debug || (p->target && p->target->debug))
1203                                                         error("Cannot read link %s", fdname);
1204                                         }
1205                                         continue;
1206                                 }
1207                                 linkname[l] = '\0';
1208                                 file_counter++;
1209
1210                                 // if another process already has this, we will get
1211                                 // the same id
1212                                 p->fds[fdid] = file_descriptor_find_or_add(linkname);
1213                         }
1214
1215                         // else make it positive again, we need it
1216                         // of course, the actual file may have changed, but we don't care so much
1217                         // FIXME: we could compare the inode as returned by readdir direct structure
1218                         else p->fds[fdid] = -p->fds[fdid];
1219                 }
1220                 closedir(fds);
1221
1222                 // remove all the negative file descriptors
1223                 for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] < 0) {
1224                         file_descriptor_not_used(-p->fds[c]);
1225                         p->fds[c] = 0;
1226                 }
1227         }
1228         else return 1;
1229
1230         return 0;
1231 }
1232
1233 // ----------------------------------------------------------------------------
1234
1235 // 1. read all files in /proc
1236 // 2. for each numeric directory:
1237 //    i.   read /proc/pid/stat
1238 //    ii.  read /proc/pid/statm
1239 //    iii. read /proc/pid/io (requires root access)
1240 //    iii. read the entries in directory /proc/pid/fd (requires root access)
1241 //         for each entry:
1242 //         a. find or create a struct file_descriptor
1243 //         b. cleanup any old/unused file_descriptors
1244
1245 // after all these, some pids may be linked to targets, while others may not
1246
1247 // in case of errors, only 1 every 1000 errors is printed
1248 // to avoid filling up all disk space
1249 // if debug is enabled, all errors are printed
1250
1251 int collect_data_for_all_processes_from_proc(void)
1252 {
1253         static long count_errors = 0;
1254
1255         char dirname[FILENAME_MAX + 1];
1256
1257         snprintf(dirname, FILENAME_MAX, "%s/proc", host_prefix);
1258         DIR *dir = opendir(dirname);
1259         if(!dir) return 0;
1260
1261         struct dirent *file = NULL;
1262         struct pid_stat *p = NULL;
1263
1264         // mark them all as un-updated
1265         all_pids_count = 0;
1266         for(p = root_of_pids; p ; p = p->next) {
1267                 all_pids_count++;
1268                 p->parent = NULL;
1269                 p->updated = 0;
1270                 p->children_count = 0;
1271                 p->merged = 0;
1272                 p->new_entry = 0;
1273
1274         p->last_minflt  = p->minflt;
1275         p->last_cminflt  = p->cminflt;
1276         p->last_majflt  = p->majflt;
1277         p->last_cmajflt  = p->cmajflt;
1278         p->last_utime  = p->utime;
1279         p->last_stime  = p->stime;
1280         p->last_cutime  = p->cutime;
1281         p->last_cstime  = p->cstime;
1282
1283         p->last_io_logical_bytes_read  = p->io_logical_bytes_read;
1284         p->last_io_logical_bytes_written  = p->io_logical_bytes_written;
1285         p->last_io_read_calls  = p->io_read_calls;
1286         p->last_io_write_calls  = p->io_write_calls;
1287         p->last_io_storage_bytes_read  = p->io_storage_bytes_read;
1288         p->last_io_storage_bytes_written  = p->io_storage_bytes_written;
1289         p->last_io_cancelled_write_bytes  = p->io_cancelled_write_bytes;
1290         }
1291
1292         while((file = readdir(dir))) {
1293                 char *endptr = file->d_name;
1294                 pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1295
1296                 // make sure we read a valid number
1297                 if(unlikely(pid <= 0 || pid > pid_max || endptr == file->d_name || *endptr != '\0'))
1298                         continue;
1299
1300                 p = get_pid_entry(pid);
1301                 if(unlikely(!p)) continue;
1302
1303
1304                 // --------------------------------------------------------------------
1305                 // /proc/<pid>/stat
1306
1307                 if(unlikely(read_proc_pid_stat(p))) {
1308                         if(!count_errors++ || debug || (p->target && p->target->debug))
1309                                 error("Cannot process %s/proc/%d/stat", host_prefix, pid);
1310
1311                         // there is no reason to proceed if we cannot get its status
1312                         continue;
1313                 }
1314
1315                 // check its parent pid
1316                 if(unlikely(p->ppid < 0 || p->ppid > pid_max)) {
1317                         if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1318                                 error("Pid %d states invalid parent pid %d. Using 0.", pid, p->ppid);
1319
1320                         p->ppid = 0;
1321                 }
1322
1323
1324                 // --------------------------------------------------------------------
1325                 // /proc/<pid>/statm
1326
1327                 if(unlikely(read_proc_pid_statm(p))) {
1328                         if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1329                                 error("Cannot process %s/proc/%d/statm", host_prefix, pid);
1330
1331                         // there is no reason to proceed if we cannot get its memory status
1332                         continue;
1333                 }
1334
1335
1336                 // --------------------------------------------------------------------
1337                 // /proc/<pid>/io
1338
1339                 if(unlikely(read_proc_pid_io(p))) {
1340                         if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1341                                 error("Cannot process %s/proc/%d/io", host_prefix, pid);
1342
1343                         // on systems without /proc/X/io
1344                         // allow proceeding without I/O information
1345                         // continue;
1346                 }
1347
1348                 // --------------------------------------------------------------------
1349                 // <pid> ownership
1350
1351                 if(unlikely(read_proc_pid_ownership(p))) {
1352                         if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1353                                 error("Cannot stat %s/proc/%d", host_prefix, pid);
1354                 }
1355
1356                 // --------------------------------------------------------------------
1357                 // link it
1358
1359                 // check if it is target
1360                 // we do this only once, the first time this pid is loaded
1361                 if(unlikely(p->new_entry)) {
1362                         if(debug) fprintf(stderr, "apps.plugin: \tJust added %s\n", p->comm);
1363                         uint32_t hash = simple_hash(p->comm);
1364
1365                         struct target *w;
1366                         for(w = apps_groups_root_target; w ; w = w->next) {
1367                                 // if(debug || (p->target && p->target->debug)) fprintf(stderr, "apps.plugin: \t\tcomparing '%s' with '%s'\n", w->compare, p->comm);
1368
1369                                 if(w->hash == hash && strcmp(w->compare, p->comm) == 0) {
1370                                         if(w->target) p->target = w->target;
1371                                         else p->target = w;
1372
1373                                         if(debug || (p->target && p->target->debug))
1374                                                 fprintf(stderr, "apps.plugin: \t\t%s linked to target %s\n", p->comm, p->target->name);
1375                                 }
1376                         }
1377                 }
1378
1379                 // --------------------------------------------------------------------
1380                 // /proc/<pid>/fd
1381
1382                 if(unlikely(read_pid_file_descriptors(p))) {
1383                         if(unlikely(!count_errors++ || debug || (p->target && p->target->debug)))
1384                                 error("Cannot process entries in %s/proc/%d/fd", host_prefix, pid);
1385                 }
1386
1387                 // --------------------------------------------------------------------
1388                 // done!
1389
1390                 // mark it as updated
1391                 p->updated = 1;
1392         }
1393
1394         if(unlikely(count_errors > 1000)) {
1395                 error("%ld more errors encountered\n", count_errors - 1);
1396                 count_errors = 0;
1397         }
1398
1399         closedir(dir);
1400
1401         return 1;
1402 }
1403
1404
1405 // ----------------------------------------------------------------------------
1406
1407 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1408 // print a tree view of all processes
1409 int debug_childrens_aggregations(pid_t pid, int level) {
1410         struct pid_stat *p = NULL;
1411         char b[level+3];
1412         int i, ret = 0;
1413
1414         for(i = 0; i < level; i++) b[i] = '\t';
1415         b[level] = '|';
1416         b[level+1] = '-';
1417         b[level+2] = '\0';
1418
1419         for(p = root_of_pids; p ; p = p->next) {
1420                 if(p->ppid == pid) {
1421                         ret += debug_childrens_aggregations(p->pid, level+1);
1422                 }
1423         }
1424
1425         p = all_pids[pid];
1426         if(p) {
1427                 if(!p->updated) ret += 1;
1428                 if(ret) fprintf(stderr, "%s %s %d [%s, %s] c=%d u=%llu+%llu, s=%llu+%llu, cu=%llu+%llu, cs=%llu+%llu, n=%llu+%llu, j=%llu+%llu, cn=%llu+%llu, cj=%llu+%llu\n"
1429                         , b, p->comm, p->pid, p->updated?"OK":"KILLED", p->target->name, p->children_count
1430                         , p->utime, p->utime - p->old_utime
1431                         , p->stime, p->stime - p->old_stime
1432                         , p->cutime, p->cutime - p->old_cutime
1433                         , p->cstime, p->cstime - p->old_cstime
1434                         , p->minflt, p->minflt - p->old_minflt
1435                         , p->majflt, p->majflt - p->old_majflt
1436                         , p->cminflt, p->cminflt - p->old_cminflt
1437                         , p->cmajflt, p->cmajflt - p->old_cmajflt
1438                         );
1439         }
1440
1441         return ret;
1442 }
1443 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1444
1445
1446
1447 // ----------------------------------------------------------------------------
1448 // update statistics on the targets
1449
1450 // 1. link all childs to their parents
1451 // 2. go from bottom to top, marking as merged all childs to their parents
1452 //    this step links all parents without a target to the child target, if any
1453 // 3. link all top level processes (the ones not merged) to the default target
1454 // 4. go from top to bottom, linking all childs without a target, to their parent target
1455 //    after this step, all processes have a target
1456 // [5. for each killed pid (updated = 0), remove its usage from its target]
1457 // 6. zero all apps_groups_targets
1458 // 7. concentrate all values on the apps_groups_targets
1459 // 8. remove all killed processes
1460 // 9. find the unique file count for each target
1461 // check: update_apps_groups_statistics()
1462
1463 void link_all_processes_to_their_parents(void) {
1464         struct pid_stat *p = NULL;
1465
1466         // link all children to their parents
1467         // and update children count on parents
1468         for(p = root_of_pids; p ; p = p->next) {
1469                 // for each process found running
1470
1471                 if(p->ppid > 0
1472                                 && p->ppid <= pid_max
1473                                 && all_pids[p->ppid]
1474                         ) {
1475                         // for valid processes
1476
1477                         if(debug || (p->target && p->target->debug))
1478                                 fprintf(stderr, "apps.plugin: \tparent of %d (%s) is %d (%s)\n", p->pid, p->comm, p->ppid, all_pids[p->ppid]->comm);
1479
1480                         p->parent = all_pids[p->ppid];
1481                         p->parent->children_count++;
1482                 }
1483                 else if(p->ppid != 0)
1484                         error("pid %d %s states parent %d, but the later does not exist.", p->pid, p->comm, p->ppid);
1485         }
1486 }
1487
1488 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1489 void aggregate_children_to_parents(void) {
1490         struct pid_stat *p = NULL;
1491
1492         // for each killed process, remove its values from the parents
1493         // sums (we had already added them in a previous loop)
1494         for(p = root_of_pids; p ; p = p->next) {
1495                 if(p->updated) continue;
1496
1497                 if(debug) fprintf(stderr, "apps.plugin: UNMERGING %d %s\n", p->pid, p->comm);
1498
1499                 unsigned long long diff_utime = p->utime + p->cutime + p->fix_cutime;
1500                 unsigned long long diff_stime = p->stime + p->cstime + p->fix_cstime;
1501                 unsigned long long diff_minflt = p->minflt + p->cminflt + p->fix_cminflt;
1502                 unsigned long long diff_majflt = p->majflt + p->cmajflt + p->fix_cmajflt;
1503
1504                 struct pid_stat *t = p;
1505                 while((t = t->parent)) {
1506                         if(!t->updated) continue;
1507
1508                         unsigned long long x;
1509                         if(diff_utime && t->diff_cutime) {
1510                                 x = (t->diff_cutime < diff_utime)?t->diff_cutime:diff_utime;
1511                                 diff_utime -= x;
1512                                 t->diff_cutime -= x;
1513                                 t->fix_cutime += x;
1514                                 if(debug) fprintf(stderr, "apps.plugin: \t cutime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1515                         }
1516                         if(diff_stime && t->diff_cstime) {
1517                                 x = (t->diff_cstime < diff_stime)?t->diff_cstime:diff_stime;
1518                                 diff_stime -= x;
1519                                 t->diff_cstime -= x;
1520                                 t->fix_cstime += x;
1521                                 if(debug) fprintf(stderr, "apps.plugin: \t cstime %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1522                         }
1523                         if(diff_minflt && t->diff_cminflt) {
1524                                 x = (t->diff_cminflt < diff_minflt)?t->diff_cminflt:diff_minflt;
1525                                 diff_minflt -= x;
1526                                 t->diff_cminflt -= x;
1527                                 t->fix_cminflt += x;
1528                                 if(debug) fprintf(stderr, "apps.plugin: \t cminflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1529                         }
1530                         if(diff_majflt && t->diff_cmajflt) {
1531                                 x = (t->diff_cmajflt < diff_majflt)?t->diff_cmajflt:diff_majflt;
1532                                 diff_majflt -= x;
1533                                 t->diff_cmajflt -= x;
1534                                 t->fix_cmajflt += x;
1535                                 if(debug) fprintf(stderr, "apps.plugin: \t cmajflt %llu from %d %s %s\n", x, t->pid, t->comm, t->target->name);
1536                         }
1537                 }
1538
1539                 if(diff_utime) error("Cannot fix up utime %llu", diff_utime);
1540                 if(diff_stime) error("Cannot fix up stime %llu", diff_stime);
1541                 if(diff_minflt) error("Cannot fix up minflt %llu", diff_minflt);
1542                 if(diff_majflt) error("Cannot fix up majflt %llu", diff_majflt);
1543         }
1544 }
1545 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1546
1547 void cleanup_non_existing_pids(void) {
1548         int c;
1549         struct pid_stat *p = NULL;
1550
1551         for(p = root_of_pids; p ;) {
1552                 if(!p->updated) {
1553 //                      fprintf(stderr, "\tEXITED %d %s [parent %d %s, target %s] utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->pid, p->comm, p->parent->pid, p->parent->comm, p->target->name,  p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1554
1555                         for(c = 0 ; c < p->fds_size ; c++) if(p->fds[c] > 0) {
1556                                 file_descriptor_not_used(p->fds[c]);
1557                                 p->fds[c] = 0;
1558                         }
1559
1560                         pid_t r = p->pid;
1561                         p = p->next;
1562                         del_pid_entry(r);
1563                 }
1564                 else p = p->next;
1565         }
1566 }
1567
1568 void apply_apps_groups_targets_inheritance(void) {
1569         struct pid_stat *p = NULL;
1570
1571         // children that do not have a target
1572         // inherit their target from their parent
1573         int found = 1;
1574         while(found) {
1575                 found = 0;
1576                 for(p = root_of_pids; p ; p = p->next) {
1577                         // if this process does not have a target
1578                         // and it has a parent
1579                         // and its parent has a target
1580                         // then, set the parent's target to this process
1581                         if(unlikely(!p->target && p->parent && p->parent->target)) {
1582                                 p->target = p->parent->target;
1583                                 found++;
1584
1585                                 if(debug || (p->target && p->target->debug))
1586                                         fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s).\n", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
1587                         }
1588                 }
1589         }
1590
1591
1592         // find all the procs with 0 childs and merge them to their parents
1593         // repeat, until nothing more can be done.
1594         found = 1;
1595         while(found) {
1596                 found = 0;
1597                 for(p = root_of_pids; p ; p = p->next) {
1598                         // if this process does not have any children
1599                         // and is not already merged
1600                         // and has a parent
1601                         // and its parent has children
1602                         // and the target of this process and its parent is the same, or the parent does not have a target
1603                         // and its parent is not init
1604                         // then, mark them as merged.
1605                         if(unlikely(
1606                                         !p->children_count
1607                                         && !p->merged
1608                                         && p->parent
1609                                         && p->parent->children_count
1610                                         && (p->target == p->parent->target || !p->parent->target)
1611                                         && p->ppid != 1
1612                                 )) {
1613                                 p->parent->children_count--;
1614                                 p->merged = 1;
1615
1616                                 // the parent inherits the child's target, if it does not have a target itself
1617                                 if(unlikely(p->target && !p->parent->target)) {
1618                                         p->parent->target = p->target;
1619
1620                                         if(debug || (p->target && p->target->debug))
1621                                                 fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its child %d (%s).\n", p->target->name, p->parent->pid, p->parent->comm, p->pid, p->comm);
1622                                 }
1623
1624                                 found++;
1625                         }
1626                 }
1627
1628                 if(debug)
1629                         fprintf(stderr, "apps.plugin: merged %d processes\n", found);
1630         }
1631
1632         // init goes always to default target
1633         if(all_pids[1])
1634                 all_pids[1]->target = apps_groups_default_target;
1635
1636         // give a default target on all top level processes
1637         for(p = root_of_pids; p ; p = p->next) {
1638                 // if the process is not merged itself
1639                 // then is is a top level process
1640                 if(!p->merged && !p->target)
1641                         p->target = apps_groups_default_target;
1642
1643 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1644                 // by the way, update the diffs
1645                 // will be used later for subtracting killed process times
1646                 p->diff_cutime = p->utime - p->cutime;
1647                 p->diff_cstime = p->stime - p->cstime;
1648                 p->diff_cminflt = p->minflt - p->cminflt;
1649                 p->diff_cmajflt = p->majflt - p->cmajflt;
1650 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1651         }
1652
1653         // give a target to all merged child processes
1654         found = 1;
1655         while(found) {
1656                 found = 0;
1657                 for(p = root_of_pids; p ; p = p->next) {
1658                         if(unlikely(!p->target && p->merged && p->parent && p->parent->target)) {
1659                                 p->target = p->parent->target;
1660                                 found++;
1661
1662                                 if(debug || (p->target && p->target->debug))
1663                                         fprintf(stderr, "apps.plugin: \t\tTARGET INHERITANCE: %s is inherited by %d (%s) from its parent %d (%s) at phase 2.\n", p->target->name, p->pid, p->comm, p->parent->pid, p->parent->comm);
1664                         }
1665                 }
1666         }
1667 }
1668
1669 long zero_all_targets(struct target *root) {
1670         struct target *w;
1671         long count = 0;
1672
1673         for (w = root; w ; w = w->next) {
1674                 count++;
1675
1676                 if(w->fds) free(w->fds);
1677                 w->fds = NULL;
1678
1679                 w->minflt = 0;
1680                 w->majflt = 0;
1681                 w->utime = 0;
1682                 w->stime = 0;
1683                 w->cminflt = 0;
1684                 w->cmajflt = 0;
1685                 w->cutime = 0;
1686                 w->cstime = 0;
1687                 w->num_threads = 0;
1688                 w->rss = 0;
1689                 w->processes = 0;
1690
1691                 w->statm_size = 0;
1692                 w->statm_resident = 0;
1693                 w->statm_share = 0;
1694                 w->statm_text = 0;
1695                 w->statm_lib = 0;
1696                 w->statm_data = 0;
1697                 w->statm_dirty = 0;
1698
1699                 w->io_logical_bytes_read = 0;
1700                 w->io_logical_bytes_written = 0;
1701                 w->io_read_calls = 0;
1702                 w->io_write_calls = 0;
1703                 w->io_storage_bytes_read = 0;
1704                 w->io_storage_bytes_written = 0;
1705                 w->io_cancelled_write_bytes = 0;
1706         }
1707
1708         return count;
1709 }
1710
1711 void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1712         if(unlikely(!w->fds)) {
1713                 w->fds = calloc(sizeof(int), (size_t) all_files_size);
1714                 if(unlikely(!w->fds))
1715                         error("Cannot allocate memory for fds in %s", w->name);
1716         }
1717
1718         if(likely(p->updated)) {
1719                 w->cutime += p->cutime; // - p->fix_cutime;
1720                 w->cstime += p->cstime; // - p->fix_cstime;
1721                 w->cminflt += p->cminflt; // - p->fix_cminflt;
1722                 w->cmajflt += p->cmajflt; // - p->fix_cmajflt;
1723
1724                 w->utime += p->utime; //+ (p->pid != 1)?(p->cutime - p->fix_cutime):0;
1725                 w->stime += p->stime; //+ (p->pid != 1)?(p->cstime - p->fix_cstime):0;
1726                 w->minflt += p->minflt; //+ (p->pid != 1)?(p->cminflt - p->fix_cminflt):0;
1727                 w->majflt += p->majflt; //+ (p->pid != 1)?(p->cmajflt - p->fix_cmajflt):0;
1728
1729                 //if(p->num_threads < 0)
1730                 //      error("Negative threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1731
1732                 //if(p->num_threads > 10000)
1733                 //      error("Excessive threads number for pid '%s' (%d): %d", p->comm, p->pid, p->num_threads);
1734
1735                 w->num_threads += p->num_threads;
1736                 w->rss += p->rss;
1737
1738                 w->statm_size += p->statm_size;
1739                 w->statm_resident += p->statm_resident;
1740                 w->statm_share += p->statm_share;
1741                 w->statm_text += p->statm_text;
1742                 w->statm_lib += p->statm_lib;
1743                 w->statm_data += p->statm_data;
1744                 w->statm_dirty += p->statm_dirty;
1745
1746                 w->io_logical_bytes_read += p->io_logical_bytes_read;
1747                 w->io_logical_bytes_written += p->io_logical_bytes_written;
1748                 w->io_read_calls += p->io_read_calls;
1749                 w->io_write_calls += p->io_write_calls;
1750                 w->io_storage_bytes_read += p->io_storage_bytes_read;
1751                 w->io_storage_bytes_written += p->io_storage_bytes_written;
1752                 w->io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1753
1754                 w->processes++;
1755
1756                 if(likely(w->fds)) {
1757                         int c;
1758                         for(c = 0; c < p->fds_size ;c++) {
1759                                 if(p->fds[c] == 0) continue;
1760
1761                                 if(likely(p->fds[c] < all_files_size)) {
1762                                         if(w->fds) w->fds[p->fds[c]]++;
1763                                 }
1764                                 else
1765                                         error("Invalid fd number %d", p->fds[c]);
1766                         }
1767                 }
1768
1769                 if(unlikely(debug || w->debug))
1770                         fprintf(stderr, "apps.plugin: \tAgregating %s pid %d on %s utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->comm, p->pid, w->name, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1771
1772 /*              if(p->utime - p->old_utime > 100) fprintf(stderr, "BIG CHANGE: %d %s utime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->utime - p->old_utime, p->old_utime, p->utime);
1773                 if(p->cutime - p->old_cutime > 100) fprintf(stderr, "BIG CHANGE: %d %s cutime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cutime - p->old_cutime, p->old_cutime, p->cutime);
1774                 if(p->stime - p->old_stime > 100) fprintf(stderr, "BIG CHANGE: %d %s stime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->stime - p->old_stime, p->old_stime, p->stime);
1775                 if(p->cstime - p->old_cstime > 100) fprintf(stderr, "BIG CHANGE: %d %s cstime increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cstime - p->old_cstime, p->old_cstime, p->cstime);
1776                 if(p->minflt - p->old_minflt > 5000) fprintf(stderr, "BIG CHANGE: %d %s minflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->minflt - p->old_minflt, p->old_minflt, p->minflt);
1777                 if(p->majflt - p->old_majflt > 5000) fprintf(stderr, "BIG CHANGE: %d %s majflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->majflt - p->old_majflt, p->old_majflt, p->majflt);
1778                 if(p->cminflt - p->old_cminflt > 15000) fprintf(stderr, "BIG CHANGE: %d %s cminflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cminflt - p->old_cminflt, p->old_cminflt, p->cminflt);
1779                 if(p->cmajflt - p->old_cmajflt > 15000) fprintf(stderr, "BIG CHANGE: %d %s cmajflt increased by %llu from %llu to %llu\n", p->pid, p->comm, p->cmajflt - p->old_cmajflt, p->old_cmajflt, p->cmajflt);
1780 */
1781 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1782                 p->old_utime = p->utime;
1783                 p->old_cutime = p->cutime;
1784                 p->old_stime = p->stime;
1785                 p->old_cstime = p->cstime;
1786                 p->old_minflt = p->minflt;
1787                 p->old_majflt = p->majflt;
1788                 p->old_cminflt = p->cminflt;
1789                 p->old_cmajflt = p->cmajflt;
1790 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1791
1792                 if(o) {
1793                         // since the process switched target
1794                         // for all incremental values
1795                         // we have to subtract its OLD values from the new target
1796                         // and add its OLD values to the old target
1797
1798                         // IMPORTANT
1799                         // We add/subtract the last/OLD values we added to the target
1800
1801                         w->fix_cutime -= p->last_cutime;
1802                         w->fix_cstime -= p->last_cstime;
1803                         w->fix_cminflt -= p->last_cminflt;
1804                         w->fix_cmajflt -= p->last_cmajflt;
1805
1806                         w->fix_utime -= p->last_utime;
1807                         w->fix_stime -= p->last_stime;
1808                         w->fix_minflt -= p->last_minflt;
1809                         w->fix_majflt -= p->last_majflt;
1810
1811
1812                         w->fix_io_logical_bytes_read -= p->last_io_logical_bytes_read;
1813                         w->fix_io_logical_bytes_written -= p->last_io_logical_bytes_written;
1814                         w->fix_io_read_calls -= p->last_io_read_calls;
1815                         w->fix_io_write_calls -= p->last_io_write_calls;
1816                         w->fix_io_storage_bytes_read -= p->last_io_storage_bytes_read;
1817                         w->fix_io_storage_bytes_written -= p->last_io_storage_bytes_written;
1818                         w->fix_io_cancelled_write_bytes -= p->last_io_cancelled_write_bytes;
1819
1820                         // ---
1821
1822                         o->fix_cutime += p->last_cutime;
1823                         o->fix_cstime += p->last_cstime;
1824                         o->fix_cminflt += p->last_cminflt;
1825                         o->fix_cmajflt += p->last_cmajflt;
1826
1827                         o->fix_utime += p->last_utime;
1828                         o->fix_stime += p->last_stime;
1829                         o->fix_minflt += p->last_minflt;
1830                         o->fix_majflt += p->last_majflt;
1831
1832                         o->fix_io_logical_bytes_read += p->last_io_logical_bytes_read;
1833                         o->fix_io_logical_bytes_written += p->last_io_logical_bytes_written;
1834                         o->fix_io_read_calls += p->last_io_read_calls;
1835                         o->fix_io_write_calls += p->last_io_write_calls;
1836                         o->fix_io_storage_bytes_read += p->last_io_storage_bytes_read;
1837                         o->fix_io_storage_bytes_written += p->last_io_storage_bytes_written;
1838                         o->fix_io_cancelled_write_bytes += p->last_io_cancelled_write_bytes;
1839                 }
1840         }
1841         else {
1842                 // if(o) fprintf(stderr, "apps.plugin: \t\tpid %d (%s) is not updated by OLD target %s (%s) is present.\n", p->pid, p->comm, o->id, o->name);
1843
1844                 // since the process has exited, the user
1845                 // will see a drop in our charts, because the incremental
1846                 // values of this process will not be there
1847
1848                 // add them to the fix_* values and they will be added to
1849                 // the reported values, so that the report goes steady
1850                 w->fix_minflt += p->minflt;
1851                 w->fix_majflt += p->majflt;
1852                 w->fix_utime += p->utime;
1853                 w->fix_stime += p->stime;
1854                 w->fix_cminflt += p->cminflt;
1855                 w->fix_cmajflt += p->cmajflt;
1856                 w->fix_cutime += p->cutime;
1857                 w->fix_cstime += p->cstime;
1858
1859                 w->fix_io_logical_bytes_read += p->io_logical_bytes_read;
1860                 w->fix_io_logical_bytes_written += p->io_logical_bytes_written;
1861                 w->fix_io_read_calls += p->io_read_calls;
1862                 w->fix_io_write_calls += p->io_write_calls;
1863                 w->fix_io_storage_bytes_read += p->io_storage_bytes_read;
1864                 w->fix_io_storage_bytes_written += p->io_storage_bytes_written;
1865                 w->fix_io_cancelled_write_bytes += p->io_cancelled_write_bytes;
1866         }
1867
1868 }
1869
1870 void count_targets_fds(struct target *root) {
1871         int c;
1872         struct target *w;
1873
1874         for (w = root; w ; w = w->next) {
1875                 if(!w->fds) continue;
1876
1877                 w->openfiles = 0;
1878                 w->openpipes = 0;
1879                 w->opensockets = 0;
1880                 w->openinotifies = 0;
1881                 w->openeventfds = 0;
1882                 w->opentimerfds = 0;
1883                 w->opensignalfds = 0;
1884                 w->openeventpolls = 0;
1885                 w->openother = 0;
1886
1887                 for(c = 1; c < all_files_size ;c++) {
1888                         if(w->fds[c] > 0)
1889                                 switch(all_files[c].type) {
1890                                 case FILETYPE_FILE:
1891                                         w->openfiles++;
1892                                         break;
1893
1894                                 case FILETYPE_PIPE:
1895                                         w->openpipes++;
1896                                         break;
1897
1898                                 case FILETYPE_SOCKET:
1899                                         w->opensockets++;
1900                                         break;
1901
1902                                 case FILETYPE_INOTIFY:
1903                                         w->openinotifies++;
1904                                         break;
1905
1906                                 case FILETYPE_EVENTFD:
1907                                         w->openeventfds++;
1908                                         break;
1909
1910                                 case FILETYPE_TIMERFD:
1911                                         w->opentimerfds++;
1912                                         break;
1913
1914                                 case FILETYPE_SIGNALFD:
1915                                         w->opensignalfds++;
1916                                         break;
1917
1918                                 case FILETYPE_EVENTPOLL:
1919                                         w->openeventpolls++;
1920                                         break;
1921
1922                                 default:
1923                                         w->openother++;
1924                         }
1925                 }
1926
1927                 free(w->fds);
1928                 w->fds = NULL;
1929         }
1930 }
1931
1932 void calculate_netdata_statistics(void)
1933 {
1934         link_all_processes_to_their_parents();
1935         apply_apps_groups_targets_inheritance();
1936
1937 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1938         aggregate_children_to_parents();
1939 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1940
1941         zero_all_targets(users_root_target);
1942         zero_all_targets(groups_root_target);
1943         apps_groups_targets = zero_all_targets(apps_groups_root_target);
1944
1945 #ifdef AGGREGATE_CHILDREN_TO_PARENTS
1946         if(debug)
1947                 debug_childrens_aggregations(0, 1);
1948 #endif /* AGGREGATE_CHILDREN_TO_PARENTS */
1949
1950         // this has to be done, before the cleanup
1951         struct pid_stat *p = NULL;
1952         struct target *w = NULL, *o = NULL;
1953
1954         // concentrate everything on the apps_groups_targets
1955         for(p = root_of_pids; p ; p = p->next) {
1956
1957                 // --------------------------------------------------------------------
1958                 // apps_groups targets
1959                 if(likely(p->target))
1960                         aggregate_pid_on_target(p->target, p, NULL);
1961                 else
1962                         error("pid %d %s was left without a target!", p->pid, p->comm);
1963
1964
1965                 // --------------------------------------------------------------------
1966                 // user targets
1967                 o = p->user_target;
1968                 if(likely(p->user_target && p->user_target->uid == p->uid))
1969                         w = p->user_target;
1970                 else {
1971                         if(unlikely(debug && p->user_target))
1972                                         fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched user from %d (%s) to %d.\n", p->pid, p->comm, p->user_target->uid, p->user_target->name, p->uid);
1973
1974                         w = p->user_target = get_users_target(p->uid);
1975                 }
1976
1977                 if(likely(w))
1978                         aggregate_pid_on_target(w, p, o);
1979                 else
1980                         error("pid %d %s was left without a user target!", p->pid, p->comm);
1981
1982
1983                 // --------------------------------------------------------------------
1984                 // group targets
1985                 o = p->group_target;
1986                 if(likely(p->group_target && p->group_target->gid == p->gid))
1987                         w = p->group_target;
1988                 else {
1989                         if(unlikely(debug && p->group_target))
1990                                         fprintf(stderr, "apps.plugin: \t\tpid %d (%s) switched group from %d (%s) to %d.\n", p->pid, p->comm, p->group_target->gid, p->group_target->name, p->gid);
1991
1992                         w = p->group_target = get_groups_target(p->gid);
1993                 }
1994
1995                 if(likely(w))
1996                         aggregate_pid_on_target(w, p, o);
1997                 else
1998                         error("pid %d %s was left without a group target!", p->pid, p->comm);
1999
2000         }
2001
2002         count_targets_fds(apps_groups_root_target);
2003         count_targets_fds(users_root_target);
2004         count_targets_fds(groups_root_target);
2005
2006         cleanup_non_existing_pids();
2007 }
2008
2009 // ----------------------------------------------------------------------------
2010 // update chart dimensions
2011
2012 unsigned long long send_resource_usage_to_netdata() {
2013         static struct timeval last = { 0, 0 };
2014         static struct rusage me_last;
2015
2016         struct timeval now;
2017         struct rusage me;
2018
2019         unsigned long long usec;
2020         unsigned long long cpuuser;
2021         unsigned long long cpusyst;
2022
2023         if(!last.tv_sec) {
2024                 gettimeofday(&last, NULL);
2025                 getrusage(RUSAGE_SELF, &me_last);
2026
2027                 // the first time, give a zero to allow
2028                 // netdata calibrate to the current time
2029                 // usec = update_every * 1000000ULL;
2030                 usec = 0ULL;
2031                 cpuuser = 0;
2032                 cpusyst = 0;
2033         }
2034         else {
2035                 gettimeofday(&now, NULL);
2036                 getrusage(RUSAGE_SELF, &me);
2037
2038                 usec = usecdiff(&now, &last);
2039                 cpuuser = me.ru_utime.tv_sec * 1000000ULL + me.ru_utime.tv_usec;
2040                 cpusyst = me.ru_stime.tv_sec * 1000000ULL + me.ru_stime.tv_usec;
2041
2042                 bcopy(&now, &last, sizeof(struct timeval));
2043                 bcopy(&me, &me_last, sizeof(struct rusage));
2044         }
2045
2046         fprintf(stdout, "BEGIN netdata.apps_cpu %llu\n", usec);
2047         fprintf(stdout, "SET user = %llu\n", cpuuser);
2048         fprintf(stdout, "SET system = %llu\n", cpusyst);
2049         fprintf(stdout, "END\n");
2050
2051         fprintf(stdout, "BEGIN netdata.apps_files %llu\n", usec);
2052         fprintf(stdout, "SET files = %llu\n", file_counter);
2053         fprintf(stdout, "SET pids = %ld\n", all_pids_count);
2054         fprintf(stdout, "SET fds = %d\n", all_files_len);
2055         fprintf(stdout, "SET targets = %ld\n", apps_groups_targets);
2056         fprintf(stdout, "END\n");
2057
2058         return usec;
2059 }
2060
2061 void send_collected_data_to_netdata(struct target *root, const char *type, unsigned long long usec)
2062 {
2063         struct target *w;
2064
2065         fprintf(stdout, "BEGIN %s.cpu %llu\n", type, usec);
2066         for (w = root; w ; w = w->next) {
2067                 if(w->target || (!w->processes && !w->exposed)) continue;
2068
2069                 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->stime + w->fix_utime + w->fix_stime);
2070         }
2071         fprintf(stdout, "END\n");
2072
2073         fprintf(stdout, "BEGIN %s.cpu_user %llu\n", type, usec);
2074         for (w = root; w ; w = w->next) {
2075                 if(w->target || (!w->processes && !w->exposed)) continue;
2076
2077                 fprintf(stdout, "SET %s = %llu\n", w->name, w->utime + w->fix_utime);
2078         }
2079         fprintf(stdout, "END\n");
2080
2081         fprintf(stdout, "BEGIN %s.cpu_system %llu\n", type, usec);
2082         for (w = root; w ; w = w->next) {
2083                 if(w->target || (!w->processes && !w->exposed)) continue;
2084
2085                 fprintf(stdout, "SET %s = %llu\n", w->name, w->stime + w->fix_stime);
2086         }
2087         fprintf(stdout, "END\n");
2088
2089         fprintf(stdout, "BEGIN %s.threads %llu\n", type, usec);
2090         for (w = root; w ; w = w->next) {
2091                 if(w->target || (!w->processes && !w->exposed)) continue;
2092
2093                 fprintf(stdout, "SET %s = %llu\n", w->name, w->num_threads);
2094         }
2095         fprintf(stdout, "END\n");
2096
2097         fprintf(stdout, "BEGIN %s.processes %llu\n", type, usec);
2098         for (w = root; w ; w = w->next) {
2099                 if(w->target || (!w->processes && !w->exposed)) continue;
2100
2101                 fprintf(stdout, "SET %s = %lu\n", w->name, w->processes);
2102         }
2103         fprintf(stdout, "END\n");
2104
2105         fprintf(stdout, "BEGIN %s.mem %llu\n", type, usec);
2106         for (w = root; w ; w = w->next) {
2107                 if(w->target || (!w->processes && !w->exposed)) continue;
2108
2109                 fprintf(stdout, "SET %s = %lld\n", w->name, (long long)w->statm_resident - (long long)w->statm_share);
2110         }
2111         fprintf(stdout, "END\n");
2112
2113         fprintf(stdout, "BEGIN %s.minor_faults %llu\n", type, usec);
2114         for (w = root; w ; w = w->next) {
2115                 if(w->target || (!w->processes && !w->exposed)) continue;
2116
2117                 fprintf(stdout, "SET %s = %llu\n", w->name, w->minflt + w->fix_minflt);
2118         }
2119         fprintf(stdout, "END\n");
2120
2121         fprintf(stdout, "BEGIN %s.major_faults %llu\n", type, usec);
2122         for (w = root; w ; w = w->next) {
2123                 if(w->target || (!w->processes && !w->exposed)) continue;
2124
2125                 fprintf(stdout, "SET %s = %llu\n", w->name, w->majflt + w->fix_majflt);
2126         }
2127         fprintf(stdout, "END\n");
2128
2129         fprintf(stdout, "BEGIN %s.lreads %llu\n", type, usec);
2130         for (w = root; w ; w = w->next) {
2131                 if(w->target || (!w->processes && !w->exposed)) continue;
2132
2133                 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_read + w->fix_io_logical_bytes_read);
2134         }
2135         fprintf(stdout, "END\n");
2136
2137         fprintf(stdout, "BEGIN %s.lwrites %llu\n", type, usec);
2138         for (w = root; w ; w = w->next) {
2139                 if(w->target || (!w->processes && !w->exposed)) continue;
2140
2141                 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_logical_bytes_written + w->fix_io_logical_bytes_written);
2142         }
2143         fprintf(stdout, "END\n");
2144
2145         fprintf(stdout, "BEGIN %s.preads %llu\n", type, usec);
2146         for (w = root; w ; w = w->next) {
2147                 if(w->target || (!w->processes && !w->exposed)) continue;
2148
2149                 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_read + w->fix_io_storage_bytes_read);
2150         }
2151         fprintf(stdout, "END\n");
2152
2153         fprintf(stdout, "BEGIN %s.pwrites %llu\n", type, usec);
2154         for (w = root; w ; w = w->next) {
2155                 if(w->target || (!w->processes && !w->exposed)) continue;
2156
2157                 fprintf(stdout, "SET %s = %llu\n", w->name, w->io_storage_bytes_written + w->fix_io_storage_bytes_written);
2158         }
2159         fprintf(stdout, "END\n");
2160
2161         fprintf(stdout, "BEGIN %s.files %llu\n", type, usec);
2162         for (w = root; w ; w = w->next) {
2163                 if(w->target || (!w->processes && !w->exposed)) continue;
2164
2165                 fprintf(stdout, "SET %s = %llu\n", w->name, w->openfiles);
2166         }
2167         fprintf(stdout, "END\n");
2168
2169         fprintf(stdout, "BEGIN %s.sockets %llu\n", type, usec);
2170         for (w = root; w ; w = w->next) {
2171                 if(w->target || (!w->processes && !w->exposed)) continue;
2172
2173                 fprintf(stdout, "SET %s = %llu\n", w->name, w->opensockets);
2174         }
2175         fprintf(stdout, "END\n");
2176
2177         fprintf(stdout, "BEGIN %s.pipes %llu\n", type, usec);
2178         for (w = root; w ; w = w->next) {
2179                 if(w->target || (!w->processes && !w->exposed)) continue;
2180
2181                 fprintf(stdout, "SET %s = %llu\n", w->name, w->openpipes);
2182         }
2183         fprintf(stdout, "END\n");
2184
2185         fflush(stdout);
2186 }
2187
2188
2189 // ----------------------------------------------------------------------------
2190 // generate the charts
2191
2192 void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2193 {
2194         struct target *w;
2195         int newly_added = 0;
2196
2197         for(w = root ; w ; w = w->next)
2198                 if(!w->exposed && w->processes) {
2199                         newly_added++;
2200                         w->exposed = 1;
2201                         if(debug || w->debug) fprintf(stderr, "apps.plugin: %s just added - regenerating charts.\n", w->name);
2202                 }
2203
2204         // nothing more to show
2205         if(!newly_added) return;
2206
2207         // we have something new to show
2208         // update the charts
2209         fprintf(stdout, "CHART %s.cpu '' '%s CPU Time (%ld%% = %ld core%s)' 'cpu time %%' cpu %s.cpu stacked 20001 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2210         for (w = root; w ; w = w->next) {
2211                 if(w->target || (!w->processes && !w->exposed)) continue;
2212
2213                 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu %s\n", w->name, Hertz, w->hidden ? "hidden,noreset" : "noreset");
2214         }
2215
2216         fprintf(stdout, "CHART %s.mem '' '%s Dedicated Memory (w/o shared)' 'MB' mem %s.mem stacked 20003 %d\n", type, title, type, update_every);
2217         for (w = root; w ; w = w->next) {
2218                 if(w->target || (!w->processes && !w->exposed)) continue;
2219
2220                 fprintf(stdout, "DIMENSION %s '' absolute %ld %ld noreset\n", w->name, sysconf(_SC_PAGESIZE), 1024L*1024L);
2221         }
2222
2223         fprintf(stdout, "CHART %s.threads '' '%s Threads' 'threads' processes %s.threads stacked 20005 %d\n", type, title, type, update_every);
2224         for (w = root; w ; w = w->next) {
2225                 if(w->target || (!w->processes && !w->exposed)) continue;
2226
2227                 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2228         }
2229
2230         fprintf(stdout, "CHART %s.processes '' '%s Processes' 'processes' processes %s.processes stacked 20004 %d\n", type, title, type, update_every);
2231         for (w = root; w ; w = w->next) {
2232                 if(w->target || (!w->processes && !w->exposed)) continue;
2233
2234                 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2235         }
2236
2237         fprintf(stdout, "CHART %s.cpu_user '' '%s CPU User Time (%ld%% = %ld core%s)' 'cpu time %%' cpu %s.cpu_user stacked 20020 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2238         for (w = root; w ; w = w->next) {
2239                 if(w->target || (!w->processes && !w->exposed)) continue;
2240
2241                 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
2242         }
2243
2244         fprintf(stdout, "CHART %s.cpu_system '' '%s CPU System Time (%ld%% = %ld core%s)' 'cpu time %%' cpu %s.cpu_system stacked 20021 %d\n", type, title, (processors * 100), processors, (processors>1)?"s":"", type, update_every);
2245         for (w = root; w ; w = w->next) {
2246                 if(w->target || (!w->processes && !w->exposed)) continue;
2247
2248                 fprintf(stdout, "DIMENSION %s '' incremental 100 %llu noreset\n", w->name, Hertz * processors);
2249         }
2250
2251         fprintf(stdout, "CHART %s.major_faults '' '%s Major Page Faults (swap read)' 'page faults/s' swap %s.major_faults stacked 20010 %d\n", type, title, type, update_every);
2252         for (w = root; w ; w = w->next) {
2253                 if(w->target || (!w->processes && !w->exposed)) continue;
2254
2255                 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
2256         }
2257
2258         fprintf(stdout, "CHART %s.minor_faults '' '%s Minor Page Faults' 'page faults/s' mem %s.minor_faults stacked 20011 %d\n", type, title, type, update_every);
2259         for (w = root; w ; w = w->next) {
2260                 if(w->target || (!w->processes && !w->exposed)) continue;
2261
2262                 fprintf(stdout, "DIMENSION %s '' incremental 1 1 noreset\n", w->name);
2263         }
2264
2265         fprintf(stdout, "CHART %s.lreads '' '%s Disk Logical Reads' 'kilobytes/s' disk %s.lreads stacked 20042 %d\n", type, title, type, update_every);
2266         for (w = root; w ; w = w->next) {
2267                 if(w->target || (!w->processes && !w->exposed)) continue;
2268
2269                 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2270         }
2271
2272         fprintf(stdout, "CHART %s.lwrites '' '%s I/O Logical Writes' 'kilobytes/s' disk %s.lwrites stacked 20042 %d\n", type, title, type, update_every);
2273         for (w = root; w ; w = w->next) {
2274                 if(w->target || (!w->processes && !w->exposed)) continue;
2275
2276                 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2277         }
2278
2279         fprintf(stdout, "CHART %s.preads '' '%s Disk Reads' 'kilobytes/s' disk %s.preads stacked 20002 %d\n", type, title, type, update_every);
2280         for (w = root; w ; w = w->next) {
2281                 if(w->target || (!w->processes && !w->exposed)) continue;
2282
2283                 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2284         }
2285
2286         fprintf(stdout, "CHART %s.pwrites '' '%s Disk Writes' 'kilobytes/s' disk %s.pwrites stacked 20002 %d\n", type, title, type, update_every);
2287         for (w = root; w ; w = w->next) {
2288                 if(w->target || (!w->processes && !w->exposed)) continue;
2289
2290                 fprintf(stdout, "DIMENSION %s '' incremental 1 %d noreset\n", w->name, 1024);
2291         }
2292
2293         fprintf(stdout, "CHART %s.files '' '%s Open Files' 'open files' disk %s.files stacked 20050 %d\n", type, title, type, update_every);
2294         for (w = root; w ; w = w->next) {
2295                 if(w->target || (!w->processes && !w->exposed)) continue;
2296
2297                 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2298         }
2299
2300         fprintf(stdout, "CHART %s.sockets '' '%s Open Sockets' 'open sockets' net %s.sockets stacked 20051 %d\n", type, title, type, update_every);
2301         for (w = root; w ; w = w->next) {
2302                 if(w->target || (!w->processes && !w->exposed)) continue;
2303
2304                 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2305         }
2306
2307         fprintf(stdout, "CHART %s.pipes '' '%s Pipes' 'open pipes' processes %s.pipes stacked 20053 %d\n", type, title, type, update_every);
2308         for (w = root; w ; w = w->next) {
2309                 if(w->target || (!w->processes && !w->exposed)) continue;
2310
2311                 fprintf(stdout, "DIMENSION %s '' absolute 1 1 noreset\n", w->name);
2312         }
2313 }
2314
2315
2316 // ----------------------------------------------------------------------------
2317 // parse command line arguments
2318
2319 void parse_args(int argc, char **argv)
2320 {
2321         int i, freq = 0;
2322         char *name = NULL;
2323
2324         for(i = 1; i < argc; i++) {
2325                 if(!freq) {
2326                         int n = atoi(argv[i]);
2327                         if(n > 0) {
2328                                 freq = n;
2329                                 continue;
2330                         }
2331                 }
2332
2333                 if(strcmp("debug", argv[i]) == 0) {
2334                         debug = 1;
2335                         debug_flags = 0xffffffff;
2336                         continue;
2337                 }
2338
2339                 if(!name) {
2340                         name = argv[i];
2341                         continue;
2342                 }
2343
2344                 error("Cannot understand option %s", argv[i]);
2345                 exit(1);
2346         }
2347
2348         if(freq > 0) update_every = freq;
2349         if(!name) name = "groups";
2350
2351         if(read_apps_groups_conf(name)) {
2352                 error("Cannot read process groups %s", name);
2353                 exit(1);
2354         }
2355 }
2356
2357 unsigned long long sutime() {
2358         struct timeval now;
2359         gettimeofday(&now, NULL);
2360         return now.tv_sec * 1000000ULL + now.tv_usec;
2361 }
2362
2363 int main(int argc, char **argv)
2364 {
2365         // debug_flags = D_PROCFILE;
2366
2367         // set the name for logging
2368         program_name = "apps.plugin";
2369
2370         host_prefix = getenv("NETDATA_HOST_PREFIX");
2371         if(host_prefix == NULL) {
2372                 info("NETDATA_HOST_PREFIX is not passed from netdata");
2373                 host_prefix = "";
2374         }
2375         else info("Found NETDATA_HOST_PREFIX='%s'", host_prefix);
2376
2377         config_dir = getenv("NETDATA_CONFIG_DIR");
2378         if(config_dir == NULL) {
2379                 info("NETDATA_CONFIG_DIR is not passed from netdata");
2380                 config_dir = CONFIG_DIR;
2381         }
2382         else info("Found NETDATA_CONFIG_DIR='%s'", config_dir);
2383
2384         info("starting...");
2385
2386         procfile_adaptive_initial_allocation = 1;
2387
2388         time_t started_t = time(NULL);
2389         time_t current_t;
2390         Hertz = get_system_hertz();
2391         pid_max = get_system_pid_max();
2392         processors = get_system_cpus();
2393
2394         parse_args(argc, argv);
2395
2396         all_pids = calloc(sizeof(struct pid_stat *), (size_t) pid_max);
2397         if(!all_pids) {
2398                 error("Cannot allocate %lu bytes of memory.", sizeof(struct pid_stat *) * pid_max);
2399                 printf("DISABLE\n");
2400                 exit(1);
2401         }
2402
2403         fprintf(stdout, "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %d\n", update_every);
2404         fprintf(stdout, "DIMENSION user '' incremental 1 %d\n", 1000);
2405         fprintf(stdout, "DIMENSION system '' incremental 1 %d\n", 1000);
2406
2407         fprintf(stdout, "CHART netdata.apps_files '' 'Apps Plugin Files' 'files/s' apps.plugin netdata.apps_files line 140001 %d\n", update_every);
2408         fprintf(stdout, "DIMENSION files '' incremental 1 1\n");
2409         fprintf(stdout, "DIMENSION pids '' absolute 1 1\n");
2410         fprintf(stdout, "DIMENSION fds '' absolute 1 1\n");
2411         fprintf(stdout, "DIMENSION targets '' absolute 1 1\n");
2412
2413
2414 #ifndef PROFILING_MODE
2415         unsigned long long sunext = (time(NULL) - (time(NULL) % update_every) + update_every) * 1000000ULL;
2416         unsigned long long sunow;
2417 #endif /* PROFILING_MODE */
2418
2419         unsigned long long counter = 1;
2420         for(;1; counter++) {
2421 #ifndef PROFILING_MODE
2422                 // delay until it is our time to run
2423                 while((sunow = sutime()) < sunext)
2424                         usleep((useconds_t)(sunext - sunow));
2425
2426                 // find the next time we need to run
2427                 while(sutime() > sunext)
2428                         sunext += update_every * 1000000ULL;
2429 #endif /* PROFILING_MODE */
2430
2431                 if(!collect_data_for_all_processes_from_proc()) {
2432                         error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
2433                         printf("DISABLE\n");
2434                         exit(1);
2435                 }
2436
2437                 calculate_netdata_statistics();
2438
2439                 unsigned long long dt = send_resource_usage_to_netdata();
2440
2441                 // this is smart enough to show only newly added apps, when needed
2442                 send_charts_updates_to_netdata(apps_groups_root_target, "apps", "Apps");
2443                 send_charts_updates_to_netdata(users_root_target, "users", "Users");
2444                 send_charts_updates_to_netdata(groups_root_target, "groups", "User Groups");
2445
2446                 send_collected_data_to_netdata(apps_groups_root_target, "apps", dt);
2447                 send_collected_data_to_netdata(users_root_target, "users", dt);
2448                 send_collected_data_to_netdata(groups_root_target, "groups", dt);
2449
2450                 if(debug) fprintf(stderr, "apps.plugin: done Loop No %llu\n", counter);
2451                 fflush(NULL);
2452
2453                 current_t = time(NULL);
2454
2455 #ifndef PROFILING_MODE
2456                 // restart check (14400 seconds)
2457                 if(current_t - started_t > 14400) exit(0);
2458 #else
2459                 if(current_t - started_t > 10) exit(0);
2460 #endif /* PROFILING_MODE */
2461         }
2462 }