@cryptotaxi247 / netdata-1 / commits / 70abd4f82

apps.plugin now allows to be compiled for 32bit kernels

Costa Tsaousis (ktsaou) committed Jan 25, 2017 at 02:32 UTC 70abd4f82bb0c2bc51ff8c7ab6611921060a4757
3 files changed +577 -436
src/apps_plugin.c
+535 -422
@@ -1,50 +1,114 @@
1 +
2 +/*
3 + * netdata apps.plugin
4 + * (C) Copyright 2016-2017 Costa Tsaousis <costa@tsaousis.gr>
5 + * Released under GPL v3+
6 + */
7 +
8 #include "common.h"
9
10 +
11 +// ----------------------------------------------------------------------------
12 +// string lengths
13 +
14 #define MAX_COMPARE_NAME 100
15 #define MAX_NAME 100
16 #define MAX_CMDLINE 1024
17
7 -// the rates we are going to send to netdata
8 -// will have this detail
9 -// a value of:
10 -// 1 will send just integer parts to netdata
11 -// 100 will send 2 decimal points
12 -// 1000 will send 3 decimal points
18 +
19 +// ----------------------------------------------------------------------------
20 +// the rates we are going to send to netdata will have this detail a value of:
21 +// - 1 will send just integer parts to netdata
22 +// - 100 will send 2 decimal points
23 +// - 1000 will send 3 decimal points
24 // etc.
25 #define RATES_DETAIL 10000ULL
26
27 +
28 +// ----------------------------------------------------------------------------
29 +// to avoid reallocating too frequently, we can increase the number of spare
30 +// file descriptors used by processes.
31 +// IMPORTANT:
32 +// having a lot of spares, increases the CPU utilization of the plugin.
33 #define MAX_SPARE_FDS 1
34
18 -int debug = 0;
35
20 -int update_every = 1;
21 -unsigned long long global_iterations_counter = 1;
22 -unsigned long long file_counter = 0;
23 -int proc_pid_cmdline_is_needed = 0;
24 -int include_exited_childs = 1;
25 -char *config_dir = CONFIG_DIR;
36 +// ----------------------------------------------------------------------------
37 +// command line options
38
27 -pid_t *all_pids_sortlist = NULL;
39 +static int
40 + debug = 0,
41 + update_every = 1,
42 + enable_guest_charts = 0,
43 + enable_file_charts = 1,
44 + enable_users_charts = 1,
45 + enable_groups_charts = 1,
46 + include_exited_childs = 1;
47
29 -// will be automatically set to 1, if guest values are collected
30 -int show_guest_time = 0;
31 -int show_guest_time_old = 0;
48
33 -int enable_guest_charts = 0;
34 -int enable_file_charts = 1;
35 -int enable_users_charts = 1;
36 -int enable_groups_charts = 1;
49 +// will be changed to getenv(NETDATA_CONFIG_DIR) if it exists
50 +static char *config_dir = CONFIG_DIR;
51
52 // ----------------------------------------------------------------------------
53 +// internal flags
54 +// handled in code (automatically set)
55
40 -void netdata_cleanup_and_exit(int ret) {
41 - exit(ret);
42 -}
56 +static int
57 + show_guest_time = 0, // 1 when guest values are collected
58 + show_guest_time_old = 0,
59 + proc_pid_cmdline_is_needed = 0; // 1 when we need to read /proc/cmdline
60 +
61 +
62 +// ----------------------------------------------------------------------------
63 +// internal counters
64 +
65 +static size_t
66 + global_iterations_counter = 1,
67 + file_counter = 0;
68 +
69 +
70 +// ----------------------------------------------------------------------------
71 +// Normalization
72 +//
73 +// With normalization we lower the collected metrics by a factor to make them
74 +// match the total utilization of the system.
75 +// The discrepancy exists because apps.plugin needs some time to collect all
76 +// the metrics. This results in utilization that exceeds the total utilization
77 +// of the system.
78 +//
79 +// With normalization we align the per-process utilization, to the total of
80 +// the system. We first consume the exited children utilization and it the
81 +// collected values is above the total, we proportionally scale each reported
82 +// metric.
83 +
84 +// the total system time, as reported by /proc/stat
85 +static kernel_uint_t
86 + global_utime = 0,
87 + global_stime = 0,
88 + global_gtime = 0;
89 +
90 +
91 +// the normalization ratios, as calculated by normalize_utilization()
92 +double utime_fix_ratio = 1.0,
93 + stime_fix_ratio = 1.0,
94 + gtime_fix_ratio = 1.0,
95 + minflt_fix_ratio = 1.0,
96 + majflt_fix_ratio = 1.0,
97 + cutime_fix_ratio = 1.0,
98 + cstime_fix_ratio = 1.0,
99 + cgtime_fix_ratio = 1.0,
100 + cminflt_fix_ratio = 1.0,
101 + cmajflt_fix_ratio = 1.0;
102
103
104 // ----------------------------------------------------------------------------
105 // target
47 -// target is the structure that process data are aggregated
106 +//
107 +// target is the structure that processes are aggregated to be reported
108 +// to netdata.
109 +//
110 +// - Each entry in /etc/apps_groups.conf creates a target.
111 +// - Each user and group used by a process in the system, creates a target.
112
113 struct target {
114 char compare[MAX_COMPARE_NAME + 1];
@@ -59,74 +123,280 @@ struct target {
123 uid_t uid;
124 gid_t gid;
125
62 - unsigned long long minflt;
63 - unsigned long long cminflt;
64 - unsigned long long majflt;
65 - unsigned long long cmajflt;
66 - unsigned long long utime;
67 - unsigned long long stime;
68 - unsigned long long gtime;
69 - unsigned long long cutime;
70 - unsigned long long cstime;
71 - unsigned long long cgtime;
72 - unsigned long long num_threads;
73 - // unsigned long long rss;
74 -
75 - unsigned long long statm_size;
76 - unsigned long long statm_resident;
77 - unsigned long long statm_share;
78 - // unsigned long long statm_text;
79 - // unsigned long long statm_lib;
80 - // unsigned long long statm_data;
81 - // unsigned long long statm_dirty;
82 -
83 - unsigned long long io_logical_bytes_read;
84 - unsigned long long io_logical_bytes_written;
85 - // unsigned long long io_read_calls;
86 - // unsigned long long io_write_calls;
87 - unsigned long long io_storage_bytes_read;
88 - unsigned long long io_storage_bytes_written;
89 - // unsigned long long io_cancelled_write_bytes;
126 + kernel_uint_t minflt;
127 + kernel_uint_t cminflt;
128 + kernel_uint_t majflt;
129 + kernel_uint_t cmajflt;
130 + kernel_uint_t utime;
131 + kernel_uint_t stime;
132 + kernel_uint_t gtime;
133 + kernel_uint_t cutime;
134 + kernel_uint_t cstime;
135 + kernel_uint_t cgtime;
136 + kernel_uint_t num_threads;
137 + // kernel_uint_t rss;
138 +
139 + kernel_uint_t statm_size;
140 + kernel_uint_t statm_resident;
141 + kernel_uint_t statm_share;
142 + // kernel_uint_t statm_text;
143 + // kernel_uint_t statm_lib;
144 + // kernel_uint_t statm_data;
145 + // kernel_uint_t statm_dirty;
146 +
147 + kernel_uint_t io_logical_bytes_read;
148 + kernel_uint_t io_logical_bytes_written;
149 + // kernel_uint_t io_read_calls;
150 + // kernel_uint_t io_write_calls;
151 + kernel_uint_t io_storage_bytes_read;
152 + kernel_uint_t io_storage_bytes_written;
153 + // kernel_uint_t io_cancelled_write_bytes;
154
155 int *target_fds;
156 int target_fds_size;
157
94 - unsigned long long openfiles;
95 - unsigned long long openpipes;
96 - unsigned long long opensockets;
97 - unsigned long long openinotifies;
98 - unsigned long long openeventfds;
99 - unsigned long long opentimerfds;
100 - unsigned long long opensignalfds;
101 - unsigned long long openeventpolls;
102 - unsigned long long openother;
103 -
104 - unsigned long processes; // how many processes have been merged to this
105 - int exposed; // if set, we have sent this to netdata
106 - int hidden; // if set, we set the hidden flag on the dimension
158 + kernel_uint_t openfiles;
159 + kernel_uint_t openpipes;
160 + kernel_uint_t opensockets;
161 + kernel_uint_t openinotifies;
162 + kernel_uint_t openeventfds;
163 + kernel_uint_t opentimerfds;
164 + kernel_uint_t opensignalfds;
165 + kernel_uint_t openeventpolls;
166 + kernel_uint_t openother;
167 +
168 + unsigned int processes; // how many processes have been merged to this
169 + int exposed; // if set, we have sent this to netdata
170 + int hidden; // if set, we set the hidden flag on the dimension
171 int debug;
172 int ends_with;
109 - int starts_with; // if set, the compare string matches only the
110 - // beginning of the command
173 + int starts_with; // if set, the compare string matches only the
174 + // beginning of the command
175
112 - struct target *target; // the one that will be reported to netdata
176 + struct target *target; // the one that will be reported to netdata
177 struct target *next;
178 };
179
180 +struct target
181 + *apps_groups_default_target = NULL, // the default target
182 + *apps_groups_root_target = NULL, // apps_groups.conf defined
183 + *users_root_target = NULL, // users
184 + *groups_root_target = NULL; // user groups
185 +
186 +size_t
187 + apps_groups_targets_count = 0; // # of apps_groups.conf targets
188 +
189
190 // ----------------------------------------------------------------------------
118 -// apps_groups.conf
119 -// aggregate all processes in groups, to have a limited number of dimensions
191 +// pid_stat
192 +//
193 +// structure to store data for each process running
194 +// see: man proc for the description of the fields
195
121 -struct target *apps_groups_root_target = NULL;
122 -struct target *apps_groups_default_target = NULL;
123 -long apps_groups_targets = 0;
196 +struct pid_stat {
197 + int32_t pid;
198 + char comm[MAX_COMPARE_NAME + 1];
199 + char cmdline[MAX_CMDLINE + 1];
200
125 -struct target *users_root_target = NULL;
126 -struct target *groups_root_target = NULL;
201 + uint32_t log_thrown;
202
128 -static struct target *get_users_target(uid_t uid)
129 -{
203 + // char state;
204 + int32_t ppid;
205 + // int32_t pgrp;
206 + // int32_t session;
207 + // int32_t tty_nr;
208 + // int32_t tpgid;
209 + // uint64_t flags;
210 +
211 + // these are raw values collected
212 + kernel_uint_t minflt_raw;
213 + kernel_uint_t cminflt_raw;
214 + kernel_uint_t majflt_raw;
215 + kernel_uint_t cmajflt_raw;
216 + kernel_uint_t utime_raw;
217 + kernel_uint_t stime_raw;
218 + kernel_uint_t gtime_raw; // guest_time
219 + kernel_uint_t cutime_raw;
220 + kernel_uint_t cstime_raw;
221 + kernel_uint_t cgtime_raw; // cguest_time
222 +
223 + // these are rates
224 + kernel_uint_t minflt;
225 + kernel_uint_t cminflt;
226 + kernel_uint_t majflt;
227 + kernel_uint_t cmajflt;
228 + kernel_uint_t utime;
229 + kernel_uint_t stime;
230 + kernel_uint_t gtime;
231 + kernel_uint_t cutime;
232 + kernel_uint_t cstime;
233 + kernel_uint_t cgtime;
234 +
235 + // int64_t priority;
236 + // int64_t nice;
237 + int32_t num_threads;
238 + // int64_t itrealvalue;
239 + // kernel_uint_t starttime;
240 + // kernel_uint_t vsize;
241 + // kernel_uint_t rss;
242 + // kernel_uint_t rsslim;
243 + // kernel_uint_t starcode;
244 + // kernel_uint_t endcode;
245 + // kernel_uint_t startstack;
246 + // kernel_uint_t kstkesp;
247 + // kernel_uint_t kstkeip;
248 + // uint64_t signal;
249 + // uint64_t blocked;
250 + // uint64_t sigignore;
251 + // uint64_t sigcatch;
252 + // uint64_t wchan;
253 + // uint64_t nswap;
254 + // uint64_t cnswap;
255 + // int32_t exit_signal;
256 + // int32_t processor;
257 + // uint32_t rt_priority;
258 + // uint32_t policy;
259 + // kernel_uint_t delayacct_blkio_ticks;
260 +
261 + uid_t uid;
262 + gid_t gid;
263 +
264 + kernel_uint_t statm_size;
265 + kernel_uint_t statm_resident;
266 + kernel_uint_t statm_share;
267 + // kernel_uint_t statm_text;
268 + // kernel_uint_t statm_lib;
269 + // kernel_uint_t statm_data;
270 + // kernel_uint_t statm_dirty;
271 +
272 + kernel_uint_t io_logical_bytes_read_raw;
273 + kernel_uint_t io_logical_bytes_written_raw;
274 + // kernel_uint_t io_read_calls_raw;
275 + // kernel_uint_t io_write_calls_raw;
276 + kernel_uint_t io_storage_bytes_read_raw;
277 + kernel_uint_t io_storage_bytes_written_raw;
278 + // kernel_uint_t io_cancelled_write_bytes_raw;
279 +
280 + kernel_uint_t io_logical_bytes_read;
281 + kernel_uint_t io_logical_bytes_written;
282 + // kernel_uint_t io_read_calls;
283 + // kernel_uint_t io_write_calls;
284 + kernel_uint_t io_storage_bytes_read;
285 + kernel_uint_t io_storage_bytes_written;
286 + // kernel_uint_t io_cancelled_write_bytes;
287 +
288 + int *fds; // array of fds it uses
289 + int fds_size; // the size of the fds array
290 +
291 + int children_count; // number of processes directly referencing this
292 + char keep:1; // 1 when we need to keep this process in memory even after it exited
293 + int keeploops; // increases by 1 every time keep is 1 and updated 0
294 + char updated:1; // 1 when the process is currently running
295 + char merged:1; // 1 when it has been merged to its parent
296 + char new_entry:1; // 1 when this is a new process, just saw for the first time
297 + char read:1; // 1 when we have already read this process for this iteration
298 +
299 + int sortlist; // higher numbers = top on the process tree
300 + // each process gets a unique number
301 +
302 + struct target *target; // app_groups.conf targets
303 + struct target *user_target; // uid based targets
304 + struct target *group_target; // gid based targets
305 +
306 + usec_t stat_collected_usec;
307 + usec_t last_stat_collected_usec;
308 +
309 + usec_t io_collected_usec;
310 + usec_t last_io_collected_usec;
311 +
312 + char *fds_dirname; // the full directory name in /proc/PID/fd
313 +
314 + char *stat_filename;
315 + char *statm_filename;
316 + char *io_filename;
317 + char *cmdline_filename;
318 +
319 + struct pid_stat *parent;
320 + struct pid_stat *prev;
321 + struct pid_stat *next;
322 +};
323 +
324 +// log each problem once per process
325 +// log flood protection flags (log_thrown)
326 +#define PID_LOG_IO 0x00000001
327 +#define PID_LOG_STATM 0x00000002
328 +#define PID_LOG_CMDLINE 0x00000004
329 +#define PID_LOG_FDS 0x00000008
330 +#define PID_LOG_STAT 0x00000010
331 +
332 +static struct pid_stat
333 + *root_of_pids = NULL, // global list of all processes running
334 + **all_pids = NULL; // to avoid allocations, we pre-allocate the
335 + // the entire pid space.
336 +
337 +static size_t
338 + all_pids_count = 0; // the number of processes running
339 +
340 +// Another pre-allocated list of all possible pids.
341 +// We need it to pids and assign them a unique sortlist id, so that we
342 +// read parents before children. This is needed to prevent a situation where
343 +// a child is found running, but until we read its parent, it has exited and
344 +// its parent has accumulated its resources.
345 +static pid_t
346 + *all_pids_sortlist = NULL;
347 +
348 +
349 +// ----------------------------------------------------------------------------
350 +// file descriptor
351 +//
352 +// this is used to keep a global list of all open files of the system.
353 +// it is needed in order to calculate the unique files processes have open.
354 +
355 +#define FILE_DESCRIPTORS_INCREASE_STEP 100
356 +
357 +struct file_descriptor {
358 + avl avl;
359 +
360 +#ifdef NETDATA_INTERNAL_CHECKS
361 + uint32_t magic;
362 +#endif /* NETDATA_INTERNAL_CHECKS */
363 +
364 + const char *name;
365 + uint32_t hash;
366 +
367 + char type;
368 + int count;
369 + int pos;
370 +} *all_files = NULL;
371 +
372 +static int
373 + all_files_len = 0,
374 + all_files_size = 0;
375 +
376 +// types for struct file_descriptor->type
377 +#define FILETYPE_OTHER 0
378 +#define FILETYPE_FILE 1
379 +#define FILETYPE_PIPE 2
380 +#define FILETYPE_SOCKET 3
381 +#define FILETYPE_INOTIFY 4
382 +#define FILETYPE_EVENTFD 5
383 +#define FILETYPE_EVENTPOLL 6
384 +#define FILETYPE_TIMERFD 7
385 +#define FILETYPE_SIGNALFD 8
386 +
387 +
388 +// ----------------------------------------------------------------------------
389 +// callback required by fatal()
390 +
391 +void netdata_cleanup_and_exit(int ret) {
392 + exit(ret);
393 +}
394 +
395 +// ----------------------------------------------------------------------------
396 +// apps_groups.conf
397 +// aggregate all processes in groups, to have a limited number of dimensions
398 +
399 +static struct target *get_users_target(uid_t uid) {
400 struct target *w;
401 for(w = users_root_target ; w ; w = w->next)
402 if(w->uid == uid) return w;
@@ -221,10 +491,12 @@ static struct target *get_apps_groups_target(const char *id, struct target *targ
491 if(*name == '-') thidden = 1;
492 name++;
493 }
224 - for(target = apps_groups_root_target ; target ; target = target->next) {
494 +
495 + for(target = apps_groups_root_target ; target != NULL ; target = target->next) {
496 if(!target->target && strcmp(name, target->name) == 0)
497 break;
498 }
499 +
500 if(unlikely(debug)) {
501 if(unlikely(target))
502 fprintf(stderr, "apps.plugin: REUSING TARGET NAME '%s' on ID '%s'\n", target->name, target->id);
@@ -302,10 +574,10 @@ static int read_apps_groups_conf(const char *file)
574 if(!ff)
575 return 1;
576
305 - unsigned long line, lines = procfile_lines(ff);
577 + size_t line, lines = procfile_lines(ff);
578
579 for(line = 0; line < lines ;line++) {
308 - unsigned long word, words = procfile_linewords(ff, line);
580 + size_t word, words = procfile_linewords(ff, line);
581 if(!words) continue;
582
583 char *name = procfile_lineword(ff, line, 0);
@@ -351,146 +623,10 @@ static int read_apps_groups_conf(const char *file)
623
624
625 // ----------------------------------------------------------------------------
354 -// data to store for each pid
355 -// see: man proc
356 -
357 -#define PID_LOG_IO 0x00000001
358 -#define PID_LOG_STATM 0x00000002
359 -#define PID_LOG_CMDLINE 0x00000004
360 -#define PID_LOG_FDS 0x00000008
361 -#define PID_LOG_STAT 0x00000010
362 -
363 -struct pid_stat {
364 - int32_t pid;
365 - char comm[MAX_COMPARE_NAME + 1];
366 - char cmdline[MAX_CMDLINE + 1];
367 -
368 - uint32_t log_thrown;
369 -
370 - // char state;
371 - int32_t ppid;
372 - // int32_t pgrp;
373 - // int32_t session;
374 - // int32_t tty_nr;
375 - // int32_t tpgid;
376 - // uint64_t flags;
377 -
378 - // these are raw values collected
379 - unsigned long long minflt_raw;
380 - unsigned long long cminflt_raw;
381 - unsigned long long majflt_raw;
382 - unsigned long long cmajflt_raw;
383 - unsigned long long utime_raw;
384 - unsigned long long stime_raw;
385 - unsigned long long gtime_raw; // guest_time
386 - unsigned long long cutime_raw;
387 - unsigned long long cstime_raw;
388 - unsigned long long cgtime_raw; // cguest_time
389 -
390 - // these are rates
391 - unsigned long long minflt;
392 - unsigned long long cminflt;
393 - unsigned long long majflt;
394 - unsigned long long cmajflt;
395 - unsigned long long utime;
396 - unsigned long long stime;
397 - unsigned long long gtime;
398 - unsigned long long cutime;
399 - unsigned long long cstime;
400 - unsigned long long cgtime;
401 -
402 - // int64_t priority;
403 - // int64_t nice;
404 - int32_t num_threads;
405 - // int64_t itrealvalue;
406 - // unsigned long long starttime;
407 - // unsigned long long vsize;
408 - // unsigned long long rss;
409 - // unsigned long long rsslim;
410 - // unsigned long long starcode;
411 - // unsigned long long endcode;
412 - // unsigned long long startstack;
413 - // unsigned long long kstkesp;
414 - // unsigned long long kstkeip;
415 - // uint64_t signal;
416 - // uint64_t blocked;
417 - // uint64_t sigignore;
418 - // uint64_t sigcatch;
419 - // uint64_t wchan;
420 - // uint64_t nswap;
421 - // uint64_t cnswap;
422 - // int32_t exit_signal;
423 - // int32_t processor;
424 - // uint32_t rt_priority;
425 - // uint32_t policy;
426 - // unsigned long long delayacct_blkio_ticks;
427 -
428 - uid_t uid;
429 - gid_t gid;
430 -
431 - unsigned long long statm_size;
432 - unsigned long long statm_resident;
433 - unsigned long long statm_share;
434 - // unsigned long long statm_text;
435 - // unsigned long long statm_lib;
436 - // unsigned long long statm_data;
437 - // unsigned long long statm_dirty;
438 -
439 - unsigned long long io_logical_bytes_read_raw;
440 - unsigned long long io_logical_bytes_written_raw;
441 - // unsigned long long io_read_calls_raw;
442 - // unsigned long long io_write_calls_raw;
443 - unsigned long long io_storage_bytes_read_raw;
444 - unsigned long long io_storage_bytes_written_raw;
445 - // unsigned long long io_cancelled_write_bytes_raw;
446 -
447 - unsigned long long io_logical_bytes_read;
448 - unsigned long long io_logical_bytes_written;
449 - // unsigned long long io_read_calls;
450 - // unsigned long long io_write_calls;
451 - unsigned long long io_storage_bytes_read;
452 - unsigned long long io_storage_bytes_written;
453 - // unsigned long long io_cancelled_write_bytes;
454 -
455 - int *fds; // array of fds it uses
456 - int fds_size; // the size of the fds array
457 -
458 - int children_count; // number of processes directly referencing this
459 - int keep; // 1 when we need to keep this process in memory even after it exited
460 - int keeploops; // increases by 1 every time keep is 1 and updated 0
461 - int updated; // 1 when the process is currently running
462 - int merged; // 1 when it has been merged to its parent
463 - int new_entry; // 1 when this is a new process, just saw for the first time
464 - int read; // 1 when we have already read this process for this iteration
465 - int sortlist; // higher numbers = top on the process tree
466 - // each process gets a unique number
467 -
468 - struct target *target; // app_groups.conf targets
469 - struct target *user_target; // uid based targets
470 - struct target *group_target; // gid based targets
471 -
472 - unsigned long long stat_collected_usec;
473 - unsigned long long last_stat_collected_usec;
474 -
475 - unsigned long long io_collected_usec;
476 - unsigned long long last_io_collected_usec;
477 -
478 - char *fds_dirname; // the full directory name in /proc/PID/fd
479 -
480 - char *stat_filename;
481 - char *statm_filename;
482 - char *io_filename;
483 - char *cmdline_filename;
484 -
485 - struct pid_stat *parent;
486 - struct pid_stat *prev;
487 - struct pid_stat *next;
488 -} *root_of_pids = NULL, **all_pids;
489 -
490 -long all_pids_count = 0;
626 +// struct pid_stat management
627
628 static inline struct pid_stat *get_pid_entry(pid_t pid) {
493 - if(all_pids[pid]) {
629 + if(unlikely(all_pids[pid])) {
630 all_pids[pid]->new_entry = 0;
631 return all_pids[pid];
632 }
@@ -499,7 +635,9 @@ static inline struct pid_stat *get_pid_entry(pid_t pid) {
635 all_pids[pid]->fds = callocz(sizeof(int), MAX_SPARE_FDS);
636 all_pids[pid]->fds_size = MAX_SPARE_FDS;
637
502 - if(root_of_pids) root_of_pids->prev = all_pids[pid];
638 + if(likely(root_of_pids))
639 + root_of_pids->prev = all_pids[pid];
640 +
641 all_pids[pid]->next = root_of_pids;
642 root_of_pids = all_pids[pid];
643
@@ -512,7 +650,7 @@ static inline struct pid_stat *get_pid_entry(pid_t pid) {
650 }
651
652 static inline void del_pid_entry(pid_t pid) {
515 - if(!all_pids[pid]) {
653 + if(unlikely(!all_pids[pid])) {
654 error("attempted to free pid %d that is not allocated.", pid);
655 return;
656 }
@@ -520,7 +658,9 @@ static inline void del_pid_entry(pid_t pid) {
658 if(unlikely(debug))
659 fprintf(stderr, "apps.plugin: process %d %s exited, deleting it.\n", pid, all_pids[pid]->comm);
660
523 - if(root_of_pids == all_pids[pid]) root_of_pids = all_pids[pid]->next;
661 + if(root_of_pids == all_pids[pid])
662 + root_of_pids = all_pids[pid]->next;
663 +
664 if(all_pids[pid]->next) all_pids[pid]->next->prev = all_pids[pid]->prev;
665 if(all_pids[pid]->prev) all_pids[pid]->prev->next = all_pids[pid]->next;
666
@@ -617,86 +757,86 @@ static inline int read_proc_pid_stat(struct pid_stat *p) {
757 p->stat_collected_usec = now_realtime_usec();
758 file_counter++;
759
620 - // p->pid = str2ul(procfile_lineword(ff, 0, 0+i));
760 + // p->pid = str2pid_t(procfile_lineword(ff, 0, 0+i));
761
762 if(unlikely(!p->comm[0]))
763 strncpyz(p->comm, procfile_lineword(ff, 0, 1), MAX_COMPARE_NAME);
764
765 // p->state = *(procfile_lineword(ff, 0, 2));
626 - p->ppid = (int32_t)str2ul(procfile_lineword(ff, 0, 3));
766 + p->ppid = (int32_t)str2pid_t(procfile_lineword(ff, 0, 3));
767 // p->pgrp = str2ul(procfile_lineword(ff, 0, 4));
768 // p->session = str2ul(procfile_lineword(ff, 0, 5));
769 // p->tty_nr = str2ul(procfile_lineword(ff, 0, 6));
770 // p->tpgid = str2ul(procfile_lineword(ff, 0, 7));
771 // p->flags = str2ull(procfile_lineword(ff, 0, 8));
772
633 - unsigned long long last;
773 + kernel_uint_t last;
774
775 last = p->minflt_raw;
636 - p->minflt_raw = str2ull(procfile_lineword(ff, 0, 9));
776 + p->minflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 9));
777 p->minflt = (p->minflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
778
779 last = p->cminflt_raw;
640 - p->cminflt_raw = str2ull(procfile_lineword(ff, 0, 10));
780 + p->cminflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 10));
781 p->cminflt = (p->cminflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
782
783 last = p->majflt_raw;
644 - p->majflt_raw = str2ull(procfile_lineword(ff, 0, 11));
784 + p->majflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 11));
785 p->majflt = (p->majflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
786
787 last = p->cmajflt_raw;
648 - p->cmajflt_raw = str2ull(procfile_lineword(ff, 0, 12));
788 + p->cmajflt_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 12));
789 p->cmajflt = (p->cmajflt_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
790
791 last = p->utime_raw;
652 - p->utime_raw = str2ull(procfile_lineword(ff, 0, 13));
792 + p->utime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 13));
793 p->utime = (p->utime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
794
795 last = p->stime_raw;
656 - p->stime_raw = str2ull(procfile_lineword(ff, 0, 14));
796 + p->stime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 14));
797 p->stime = (p->stime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
798
799 last = p->cutime_raw;
660 - p->cutime_raw = str2ull(procfile_lineword(ff, 0, 15));
800 + p->cutime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 15));
801 p->cutime = (p->cutime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
802
803 last = p->cstime_raw;
664 - p->cstime_raw = str2ull(procfile_lineword(ff, 0, 16));
804 + p->cstime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 16));
805 p->cstime = (p->cstime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
806
667 - // p->priority = str2ull(procfile_lineword(ff, 0, 17));
668 - // p->nice = str2ull(procfile_lineword(ff, 0, 18));
669 - p->num_threads = (int32_t)str2ul(procfile_lineword(ff, 0, 19));
670 - // p->itrealvalue = str2ull(procfile_lineword(ff, 0, 20));
671 - // p->starttime = str2ull(procfile_lineword(ff, 0, 21));
672 - // p->vsize = str2ull(procfile_lineword(ff, 0, 22));
673 - // p->rss = str2ull(procfile_lineword(ff, 0, 23));
674 - // p->rsslim = str2ull(procfile_lineword(ff, 0, 24));
675 - // p->starcode = str2ull(procfile_lineword(ff, 0, 25));
676 - // p->endcode = str2ull(procfile_lineword(ff, 0, 26));
677 - // p->startstack = str2ull(procfile_lineword(ff, 0, 27));
678 - // p->kstkesp = str2ull(procfile_lineword(ff, 0, 28));
679 - // p->kstkeip = str2ull(procfile_lineword(ff, 0, 29));
680 - // p->signal = str2ull(procfile_lineword(ff, 0, 30));
681 - // p->blocked = str2ull(procfile_lineword(ff, 0, 31));
682 - // p->sigignore = str2ull(procfile_lineword(ff, 0, 32));
683 - // p->sigcatch = str2ull(procfile_lineword(ff, 0, 33));
684 - // p->wchan = str2ull(procfile_lineword(ff, 0, 34));
685 - // p->nswap = str2ull(procfile_lineword(ff, 0, 35));
686 - // p->cnswap = str2ull(procfile_lineword(ff, 0, 36));
687 - // p->exit_signal = str2ul(procfile_lineword(ff, 0, 37));
688 - // p->processor = str2ul(procfile_lineword(ff, 0, 38));
689 - // p->rt_priority = str2ul(procfile_lineword(ff, 0, 39));
690 - // p->policy = str2ul(procfile_lineword(ff, 0, 40));
691 - // p->delayacct_blkio_ticks = str2ull(procfile_lineword(ff, 0, 41));
807 + // p->priority = str2kernel_unit_t(procfile_lineword(ff, 0, 17));
808 + // p->nice = str2kernel_unit_t(procfile_lineword(ff, 0, 18));
809 + p->num_threads = (int32_t)str2uint32_t(procfile_lineword(ff, 0, 19));
810 + // p->itrealvalue = str2kernel_unit_t(procfile_lineword(ff, 0, 20));
811 + // p->starttime = str2kernel_unit_t(procfile_lineword(ff, 0, 21));
812 + // p->vsize = str2kernel_unit_t(procfile_lineword(ff, 0, 22));
813 + // p->rss = str2kernel_unit_t(procfile_lineword(ff, 0, 23));
814 + // p->rsslim = str2kernel_unit_t(procfile_lineword(ff, 0, 24));
815 + // p->starcode = str2kernel_unit_t(procfile_lineword(ff, 0, 25));
816 + // p->endcode = str2kernel_unit_t(procfile_lineword(ff, 0, 26));
817 + // p->startstack = str2kernel_unit_t(procfile_lineword(ff, 0, 27));
818 + // p->kstkesp = str2kernel_unit_t(procfile_lineword(ff, 0, 28));
819 + // p->kstkeip = str2kernel_unit_t(procfile_lineword(ff, 0, 29));
820 + // p->signal = str2kernel_unit_t(procfile_lineword(ff, 0, 30));
821 + // p->blocked = str2kernel_unit_t(procfile_lineword(ff, 0, 31));
822 + // p->sigignore = str2kernel_unit_t(procfile_lineword(ff, 0, 32));
823 + // p->sigcatch = str2kernel_unit_t(procfile_lineword(ff, 0, 33));
824 + // p->wchan = str2kernel_unit_t(procfile_lineword(ff, 0, 34));
825 + // p->nswap = str2kernel_unit_t(procfile_lineword(ff, 0, 35));
826 + // p->cnswap = str2kernel_unit_t(procfile_lineword(ff, 0, 36));
827 + // p->exit_signal = str2kernel_unit_t(procfile_lineword(ff, 0, 37));
828 + // p->processor = str2kernel_unit_t(procfile_lineword(ff, 0, 38));
829 + // p->rt_priority = str2kernel_unit_t(procfile_lineword(ff, 0, 39));
830 + // p->policy = str2kernel_unit_t(procfile_lineword(ff, 0, 40));
831 + // p->delayacct_blkio_ticks = str2kernel_unit_t(procfile_lineword(ff, 0, 41));
832
833 if(enable_guest_charts) {
834 last = p->gtime_raw;
695 - p->gtime_raw = str2ull(procfile_lineword(ff, 0, 42));
835 + p->gtime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 42));
836 p->gtime = (p->gtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
837
838 last = p->cgtime_raw;
699 - p->cgtime_raw = str2ull(procfile_lineword(ff, 0, 43));
839 + p->cgtime_raw = str2kernel_unit_t(procfile_lineword(ff, 0, 43));
840 p->cgtime = (p->cgtime_raw - last) * (USEC_PER_SEC * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
841
842 if (show_guest_time || p->gtime || p->cgtime) {
@@ -707,7 +847,7 @@ static inline int read_proc_pid_stat(struct pid_stat *p) {
847 }
848
849 if(unlikely(debug || (p->target && p->target->debug)))
710 - fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=%llu, stime=%llu, cutime=%llu, cstime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu, threads=%d\n", global_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
850 + fprintf(stderr, "apps.plugin: READ PROC/PID/STAT: %s/proc/%d/stat, process: '%s' on target '%s' (dt=%llu) VALUES: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT ", threads=%d\n", global_host_prefix, p->pid, p->comm, (p->target)?p->target->name:"UNSET", p->stat_collected_usec - p->last_stat_collected_usec, p->utime, p->stime, p->cutime, p->cstime, p->minflt, p->majflt, p->cminflt, p->cmajflt, p->num_threads);
851
852 if(unlikely(global_iterations_counter == 1)) {
853 p->minflt = 0;
@@ -799,7 +939,7 @@ static inline int read_proc_pid_io(struct pid_stat *p) {
939 p->last_io_collected_usec = p->io_collected_usec;
940 p->io_collected_usec = now_realtime_usec();
941
802 - unsigned long long last;
942 + kernel_uint_t last;
943
944 last = p->io_logical_bytes_read_raw;
945 p->io_logical_bytes_read_raw = str2ull(procfile_lineword(ff, 0, 1));
@@ -852,14 +992,10 @@ cleanup:
992 return 0;
993 }
994
855 -unsigned long long global_utime = 0;
856 -unsigned long long global_stime = 0;
857 -unsigned long long global_gtime = 0;
858 -
995 static inline int read_proc_stat() {
996 static char filename[FILENAME_MAX + 1] = "";
997 static procfile *ff = NULL;
862 - static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
998 + static kernel_uint_t utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
999 static usec_t collected_usec = 0, last_collected_usec = 0;
1000
1001 if(unlikely(!ff)) {
@@ -876,7 +1012,7 @@ static inline int read_proc_stat() {
1012
1013 file_counter++;
1014
879 - unsigned long long last;
1015 + kernel_uint_t last;
1016
1017 last = utime_raw;
1018 utime_raw = str2ull(procfile_lineword(ff, 0, 1));
@@ -922,26 +1058,6 @@ cleanup:
1058
1059
1060 // ----------------------------------------------------------------------------
925 -// file descriptor
926 -// this is used to keep a global list of all open files of the system
927 -// it is needed in order to calculate the unique files processes have open
928 -
929 -#define FILE_DESCRIPTORS_INCREASE_STEP 100
930 -
931 -struct file_descriptor {
932 - avl avl;
933 -#ifdef NETDATA_INTERNAL_CHECKS
934 - uint32_t magic;
935 -#endif /* NETDATA_INTERNAL_CHECKS */
936 - uint32_t hash;
937 - const char *name;
938 - int type;
939 - int count;
940 - int pos;
941 -} *all_files = NULL;
942 -
943 -int all_files_len = 0;
944 -int all_files_size = 0;
1061
1062 int file_descriptor_compare(void* a, void* b) {
1063 #ifdef NETDATA_INTERNAL_CHECKS
@@ -982,15 +1098,7 @@ static struct file_descriptor *file_descriptor_find(const char *name, uint32_t h
1098 #define file_descriptor_add(fd) avl_insert(&all_files_index, (avl *)(fd))
1099 #define file_descriptor_remove(fd) avl_remove(&all_files_index, (avl *)(fd))
1100
985 -#define FILETYPE_OTHER 0
986 -#define FILETYPE_FILE 1
987 -#define FILETYPE_PIPE 2
988 -#define FILETYPE_SOCKET 3
989 -#define FILETYPE_INOTIFY 4
990 -#define FILETYPE_EVENTFD 5
991 -#define FILETYPE_EVENTPOLL 6
992 -#define FILETYPE_TIMERFD 7
993 -#define FILETYPE_SIGNALFD 8
1101 +// ----------------------------------------------------------------------------
1102
1103 static inline void file_descriptor_not_used(int id)
1104 {
@@ -1283,7 +1391,7 @@ static inline int read_pid_file_descriptors(struct pid_stat *p) {
1391
1392 // ----------------------------------------------------------------------------
1393
1286 -static inline int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1394 +static inline int print_process_and_parents(struct pid_stat *p, usec_t time) {
1395 char *prefix = "\\_ ";
1396 int indent = 0;
1397
@@ -1298,25 +1406,25 @@ static inline int print_process_and_parents(struct pid_stat *p, unsigned long lo
1406 for(i = 0; i < indent ;i++) buffer[i] = ' ';
1407 buffer[i] = '\0';
1408
1301 - fprintf(stderr, " %s %s%s (%d %s %lld"
1409 + fprintf(stderr, " %s %s%s (%d %s %llu"
1410 , buffer
1411 , prefix
1412 , p->comm
1413 , p->pid
1414 , p->updated?"running":"exited"
1307 - , (long long)p->stat_collected_usec - (long long)time
1415 + , p->stat_collected_usec - time
1416 );
1417
1310 - if(p->utime) fprintf(stderr, " utime=%llu", p->utime);
1311 - if(p->stime) fprintf(stderr, " stime=%llu", p->stime);
1312 - if(p->gtime) fprintf(stderr, " gtime=%llu", p->gtime);
1313 - if(p->cutime) fprintf(stderr, " cutime=%llu", p->cutime);
1314 - if(p->cstime) fprintf(stderr, " cstime=%llu", p->cstime);
1315 - if(p->cgtime) fprintf(stderr, " cgtime=%llu", p->cgtime);
1316 - if(p->minflt) fprintf(stderr, " minflt=%llu", p->minflt);
1317 - if(p->cminflt) fprintf(stderr, " cminflt=%llu", p->cminflt);
1318 - if(p->majflt) fprintf(stderr, " majflt=%llu", p->majflt);
1319 - if(p->cmajflt) fprintf(stderr, " cmajflt=%llu", p->cmajflt);
1418 + if(p->utime) fprintf(stderr, " utime=" KERNEL_UINT_FORMAT, p->utime);
1419 + if(p->stime) fprintf(stderr, " stime=" KERNEL_UINT_FORMAT, p->stime);
1420 + if(p->gtime) fprintf(stderr, " gtime=" KERNEL_UINT_FORMAT, p->gtime);
1421 + if(p->cutime) fprintf(stderr, " cutime=" KERNEL_UINT_FORMAT, p->cutime);
1422 + if(p->cstime) fprintf(stderr, " cstime=" KERNEL_UINT_FORMAT, p->cstime);
1423 + if(p->cgtime) fprintf(stderr, " cgtime=" KERNEL_UINT_FORMAT, p->cgtime);
1424 + if(p->minflt) fprintf(stderr, " minflt=" KERNEL_UINT_FORMAT, p->minflt);
1425 + if(p->cminflt) fprintf(stderr, " cminflt=" KERNEL_UINT_FORMAT, p->cminflt);
1426 + if(p->majflt) fprintf(stderr, " majflt=" KERNEL_UINT_FORMAT, p->majflt);
1427 + if(p->cmajflt) fprintf(stderr, " cmajflt=" KERNEL_UINT_FORMAT, p->cmajflt);
1428 fprintf(stderr, ")\n");
1429
1430 return indent + 1;
@@ -1328,7 +1436,7 @@ static inline void print_process_tree(struct pid_stat *p, char *msg) {
1436 print_process_and_parents(p, p->stat_collected_usec);
1437 }
1438
1331 -static inline void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1439 +static inline void find_lost_child_debug(struct pid_stat *pe, kernel_uint_t lost, int type) {
1440 int found = 0;
1441 struct pid_stat *p = NULL;
1442
@@ -1338,35 +1446,35 @@ static inline void find_lost_child_debug(struct pid_stat *pe, unsigned long long
1446 switch(type) {
1447 case 1:
1448 if(p->cminflt > lost) {
1341 - fprintf(stderr, " > process %d (%s) could use the lost exited child minflt %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1449 + fprintf(stderr, " > process %d (%s) could use the lost exited child minflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1450 found++;
1451 }
1452 break;
1453
1454 case 2:
1455 if(p->cmajflt > lost) {
1348 - fprintf(stderr, " > process %d (%s) could use the lost exited child majflt %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1456 + fprintf(stderr, " > process %d (%s) could use the lost exited child majflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1457 found++;
1458 }
1459 break;
1460
1461 case 3:
1462 if(p->cutime > lost) {
1355 - fprintf(stderr, " > process %d (%s) could use the lost exited child utime %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1463 + fprintf(stderr, " > process %d (%s) could use the lost exited child utime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1464 found++;
1465 }
1466 break;
1467
1468 case 4:
1469 if(p->cstime > lost) {
1362 - fprintf(stderr, " > process %d (%s) could use the lost exited child stime %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1470 + fprintf(stderr, " > process %d (%s) could use the lost exited child stime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1471 found++;
1472 }
1473 break;
1474
1475 case 5:
1476 if(p->cgtime > lost) {
1369 - fprintf(stderr, " > process %d (%s) could use the lost exited child gtime %llu of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1477 + fprintf(stderr, " > process %d (%s) could use the lost exited child gtime " KERNEL_UINT_FORMAT " of process %d (%s)\n", p->pid, p->comm, lost, pe->pid, pe->comm);
1478 found++;
1479 }
1480 break;
@@ -1376,30 +1484,30 @@ static inline void find_lost_child_debug(struct pid_stat *pe, unsigned long long
1484 if(!found) {
1485 switch(type) {
1486 case 1:
1379 - fprintf(stderr, " > cannot find any process to use the lost exited child minflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1487 + fprintf(stderr, " > cannot find any process to use the lost exited child minflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1488 break;
1489
1490 case 2:
1383 - fprintf(stderr, " > cannot find any process to use the lost exited child majflt %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1491 + fprintf(stderr, " > cannot find any process to use the lost exited child majflt " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1492 break;
1493
1494 case 3:
1387 - fprintf(stderr, " > cannot find any process to use the lost exited child utime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1495 + fprintf(stderr, " > cannot find any process to use the lost exited child utime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1496 break;
1497
1498 case 4:
1391 - fprintf(stderr, " > cannot find any process to use the lost exited child stime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1499 + fprintf(stderr, " > cannot find any process to use the lost exited child stime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1500 break;
1501
1502 case 5:
1395 - fprintf(stderr, " > cannot find any process to use the lost exited child gtime %llu of process %d (%s)\n", lost, pe->pid, pe->comm);
1503 + fprintf(stderr, " > cannot find any process to use the lost exited child gtime " KERNEL_UINT_FORMAT " of process %d (%s)\n", lost, pe->pid, pe->comm);
1504 break;
1505 }
1506 }
1507 }
1508
1401 -static inline unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1402 - unsigned long long absorbed = 0;
1509 +static inline kernel_uint_t remove_exited_child_from_parent(kernel_uint_t *field, kernel_uint_t *pfield) {
1510 + kernel_uint_t absorbed = 0;
1511
1512 if(*field > *pfield) {
1513 absorbed += *pfield;
@@ -1422,20 +1530,18 @@ static inline void process_exited_processes() {
1530 if(p->updated || !p->stat_collected_usec)
1531 continue;
1532
1425 - struct pid_stat *pp = p->parent;
1426 -
1427 - unsigned long long utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1428 - unsigned long long stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1429 - unsigned long long gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1430 - unsigned long long minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1431 - unsigned long long majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1533 + kernel_uint_t utime = (p->utime_raw + p->cutime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1534 + kernel_uint_t stime = (p->stime_raw + p->cstime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1535 + kernel_uint_t gtime = (p->gtime_raw + p->cgtime_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1536 + kernel_uint_t minflt = (p->minflt_raw + p->cminflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1537 + kernel_uint_t majflt = (p->majflt_raw + p->cmajflt_raw) * (1000000ULL * RATES_DETAIL) / (p->stat_collected_usec - p->last_stat_collected_usec);
1538
1539 if(utime + stime + gtime + minflt + majflt == 0)
1540 continue;
1541
1542 if(unlikely(debug)) {
1543 log_date(stderr);
1438 - fprintf(stderr, "Absorb %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1544 + fprintf(stderr, "Absorb %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")\n"
1545 , p->comm
1546 , p->pid
1547 , p->updated?"running":"exited"
@@ -1448,29 +1554,30 @@ static inline void process_exited_processes() {
1554 print_process_tree(p, "Searching parents");
1555 }
1556
1557 + struct pid_stat *pp;
1558 for(pp = p->parent; pp ; pp = pp->parent) {
1559 if(!pp->updated) continue;
1560
1454 - unsigned long long absorbed;
1561 + kernel_uint_t absorbed;
1562 absorbed = remove_exited_child_from_parent(&utime, &pp->cutime);
1563 if(unlikely(debug && absorbed))
1457 - fprintf(stderr, " > process %s (%d %s) absorbed %llu utime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1564 + fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " utime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, utime);
1565
1566 absorbed = remove_exited_child_from_parent(&stime, &pp->cstime);
1567 if(unlikely(debug && absorbed))
1461 - fprintf(stderr, " > process %s (%d %s) absorbed %llu stime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1568 + fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " stime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, stime);
1569
1570 absorbed = remove_exited_child_from_parent(&gtime, &pp->cgtime);
1571 if(unlikely(debug && absorbed))
1465 - fprintf(stderr, " > process %s (%d %s) absorbed %llu gtime (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1572 + fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " gtime (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, gtime);
1573
1574 absorbed = remove_exited_child_from_parent(&minflt, &pp->cminflt);
1575 if(unlikely(debug && absorbed))
1469 - fprintf(stderr, " > process %s (%d %s) absorbed %llu minflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1576 + fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " minflt (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, minflt);
1577
1578 absorbed = remove_exited_child_from_parent(&majflt, &pp->cmajflt);
1579 if(unlikely(debug && absorbed))
1473 - fprintf(stderr, " > process %s (%d %s) absorbed %llu majflt (remaining: %llu)\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1580 + fprintf(stderr, " > process %s (%d %s) absorbed " KERNEL_UINT_FORMAT " majflt (remaining: " KERNEL_UINT_FORMAT ")\n", pp->comm, pp->pid, pp->updated?"running":"exited", absorbed, majflt);
1581 }
1582
1583 if(unlikely(utime + stime + gtime + minflt + majflt > 0)) {
@@ -1485,7 +1592,7 @@ static inline void process_exited_processes() {
1592 p->keep = 1;
1593
1594 if(unlikely(debug))
1488 - fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=%llu stime=%llu gtime=%llu minflt=%llu majflt=%llu)\n"
1595 + fprintf(stderr, " > remaining resources - KEEP - for another loop: %s (%d %s total resources: utime=" KERNEL_UINT_FORMAT " stime=" KERNEL_UINT_FORMAT " gtime=" KERNEL_UINT_FORMAT " minflt=" KERNEL_UINT_FORMAT " majflt=" KERNEL_UINT_FORMAT ")\n"
1596 , p->comm
1597 , p->pid
1598 , p->updated?"running":"exited"
@@ -1550,7 +1657,7 @@ static inline void link_all_processes_to_their_parents(void) {
1657 pp->children_count++;
1658
1659 if(unlikely(debug || (p->target && p->target->debug)))
1553 - fprintf(stderr, "apps.plugin: \tchild %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=%llu, stime=%llu, gtime=%llu, minflt=%llu, majflt=%llu, cutime=%llu, cstime=%llu, cgtime=%llu, cminflt=%llu, cmajflt=%llu\n", p->pid, p->comm, p->updated?"running":"exited", (p->target)?p->target->name:"UNSET", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->gtime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cgtime, pp->cminflt, pp->cmajflt);
1660 + fprintf(stderr, "apps.plugin: \tchild %d (%s, %s) on target '%s' has parent %d (%s, %s). Parent: utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "\n", p->pid, p->comm, p->updated?"running":"exited", (p->target)?p->target->name:"UNSET", pp->pid, pp->comm, pp->updated?"running":"exited", pp->utime, pp->stime, pp->gtime, pp->minflt, pp->majflt, pp->cutime, pp->cstime, pp->cgtime, pp->cminflt, pp->cmajflt);
1661 }
1662 else {
1663 p->parent = NULL;
@@ -1737,19 +1844,13 @@ static inline int collect_data_for_pid(pid_t pid) {
1844 return 1;
1845 }
1846
1740 -static int collect_data_for_all_processes_from_proc(void) {
1847 +static int collect_data_for_all_processes(void) {
1848 struct pid_stat *p = NULL;
1849
1850 if(all_pids_count) {
1744 - // read parents before childs
1745 - // this is needed to prevent a situation where
1746 - // a child is found running, but until we read
1747 - // its parent, it has exited and its parent
1748 - // has accumulated its resources
1749 -
1750 - long slc = 0;
1851 + size_t slc = 0;
1852 for(p = root_of_pids; p ; p = p->next) {
1752 - p->read = 0;
1853 + p->read = 0; // mark it as not read, so that collect_data_for_pid() will read it
1854 p->updated = 0;
1855 p->new_entry = 0;
1856 p->merged = 0;
@@ -1760,12 +1861,22 @@ static int collect_data_for_all_processes_from_proc(void) {
1861 }
1862
1863 if(unlikely(slc != all_pids_count)) {
1763 - error("Internal error: I was thinking I had %ld processes in my arrays, but it seems there are more.", all_pids_count);
1864 + error("Internal error: I was thinking I had %zu processes in my arrays, but it seems there are more.", all_pids_count);
1865 all_pids_count = slc;
1866 }
1867
1868 if(include_exited_childs) {
1869 + // Read parents before childs
1870 + // This is needed to prevent a situation where
1871 + // a child is found running, but until we read
1872 + // its parent, it has exited and its parent
1873 + // has accumulated its resources.
1874 +
1875 qsort((void *)all_pids_sortlist, (size_t)all_pids_count, sizeof(pid_t), compar_pid);
1876 +
1877 + // we forward read all running processes
1878 + // collect_data_for_pid() is smart enough,
1879 + // not to read the same pid twice per iterations
1880 for(slc = 0; slc < all_pids_count; slc++)
1881 collect_data_for_pid(all_pids_sortlist[slc]);
1882 }
@@ -1777,14 +1888,18 @@ static int collect_data_for_all_processes_from_proc(void) {
1888 DIR *dir = opendir(dirname);
1889 if(!dir) return 0;
1890
1780 - struct dirent *file = NULL;
1891 + struct dirent *de = NULL;
1892 +
1893 + while((de = readdir(dir))) {
1894 + char *endptr = de->d_name;
1895
1782 - while((file = readdir(dir))) {
1783 - char *endptr = file->d_name;
1784 - pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1896 + if(unlikely(de->d_type != DT_DIR || de->d_name[0] < '0' || de->d_name[0] > '9'))
1897 + continue;
1898 +
1899 + pid_t pid = (pid_t) strtoul(de->d_name, &endptr, 10);
1900
1901 // make sure we read a valid number
1787 - if(unlikely(endptr == file->d_name || *endptr != '\0'))
1902 + if(unlikely(endptr == de->d_name || *endptr != '\0'))
1903 continue;
1904
1905 collect_data_for_pid(pid);
@@ -1794,14 +1909,17 @@ static int collect_data_for_all_processes_from_proc(void) {
1909 if(!all_pids_count)
1910 return 0;
1911
1912 + // we need /proc/stat to normalize the cpu consumption of the exited childs
1913 + read_proc_stat();
1914 +
1915 + // build the process tree
1916 + link_all_processes_to_their_parents();
1917 +
1918 // normally this is done
1919 // however we may have processes exited while we collected values
1920 // so let's find the exited ones
1921 // we do this by collecting the ownership of process
1922 // if we manage to get the ownership, the process still runs
1802 -
1803 - read_proc_stat();
1804 - link_all_processes_to_their_parents();
1923 process_exited_processes();
1924
1925 return 1;
@@ -1829,8 +1947,6 @@ static void cleanup_exited_pids(void) {
1947
1948 for(p = root_of_pids; p ;) {
1949 if(!p->updated && (!p->keep || p->keeploops > 0)) {
1832 -// fprintf(stderr, "\tEXITED %d %s [parent %d %s, target %s] utime=%llu, stime=%llu, gtime=%llu, cutime=%llu, cstime=%llu, cgtime=%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->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
1833 -
1950 if(unlikely(debug && (p->keep || p->keeploops)))
1951 fprintf(stderr, " > CLEANUP cannot keep exited process %d (%s) anymore - removing it.\n", p->pid, p->comm);
1952
@@ -1961,9 +2077,9 @@ static void apply_apps_groups_targets_inheritance(void) {
2077 fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
2078 }
2079
1964 -static long zero_all_targets(struct target *root) {
2080 +static size_t zero_all_targets(struct target *root) {
2081 struct target *w;
1966 - long count = 0;
2082 + size_t count = 0;
2083
2084 for (w = root; w ; w = w->next) {
2085 count++;
@@ -2154,7 +2270,7 @@ static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p,
2270 w->num_threads += p->num_threads;
2271
2272 if(unlikely(debug || w->debug))
2157 - fprintf(stderr, "apps.plugin: \taggregating '%s' pid %d on target '%s' utime=%llu, stime=%llu, gtime=%llu, cutime=%llu, cstime=%llu, cgtime=%llu, minflt=%llu, majflt=%llu, cminflt=%llu, cmajflt=%llu\n", p->comm, p->pid, w->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
2273 + fprintf(stderr, "apps.plugin: \taggregating '%s' pid %d on target '%s' utime=" KERNEL_UINT_FORMAT ", stime=" KERNEL_UINT_FORMAT ", gtime=" KERNEL_UINT_FORMAT ", cutime=" KERNEL_UINT_FORMAT ", cstime=" KERNEL_UINT_FORMAT ", cgtime=" KERNEL_UINT_FORMAT ", minflt=" KERNEL_UINT_FORMAT ", majflt=" KERNEL_UINT_FORMAT ", cminflt=" KERNEL_UINT_FORMAT ", cmajflt=" KERNEL_UINT_FORMAT "\n", p->comm, p->pid, w->name, p->utime, p->stime, p->gtime, p->cutime, p->cstime, p->cgtime, p->minflt, p->majflt, p->cminflt, p->cmajflt);
2274 }
2275
2276 static void calculate_netdata_statistics(void) {
@@ -2163,7 +2279,7 @@ static void calculate_netdata_statistics(void) {
2279
2280 zero_all_targets(users_root_target);
2281 zero_all_targets(groups_root_target);
2166 - apps_groups_targets = zero_all_targets(apps_groups_root_target);
2282 + apps_groups_targets_count = zero_all_targets(apps_groups_root_target);
2283
2284 // this has to be done, before the cleanup
2285 struct pid_stat *p = NULL;
@@ -2225,21 +2341,18 @@ static void calculate_netdata_statistics(void) {
2341
2342 int print_calculated_number(char *str, calculated_number value) { (void)str; (void)value; return 0; }
2343
2228 -static inline void send_BEGIN(const char *type, const char *id, unsigned long long usec) {
2344 +static inline void send_BEGIN(const char *type, const char *id, usec_t usec) {
2345 fprintf(stdout, "BEGIN %s.%s %llu\n", type, id, usec);
2346 }
2347
2232 -static inline void send_SET(const char *name, unsigned long long value) {
2233 - fprintf(stdout, "SET %s = %llu\n", name, value);
2348 +static inline void send_SET(const char *name, kernel_uint_t value) {
2349 + fprintf(stdout, "SET %s = " KERNEL_UINT_FORMAT "\n", name, value);
2350 }
2351
2352 static inline void send_END(void) {
2353 fprintf(stdout, "END\n");
2354 }
2355
2240 -double utime_fix_ratio = 1.0, stime_fix_ratio = 1.0, gtime_fix_ratio = 1.0, cutime_fix_ratio = 1.0, cstime_fix_ratio = 1.0, cgtime_fix_ratio = 1.0;
2241 -double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2242 -
2356 static usec_t send_resource_usage_to_netdata() {
2357 static struct timeval last = { 0, 0 };
2358 static struct rusage me_last;
@@ -2280,17 +2393,17 @@ static usec_t send_resource_usage_to_netdata() {
2393 "SET system = %llu\n"
2394 "END\n"
2395 "BEGIN netdata.apps_files %llu\n"
2283 - "SET files = %llu\n"
2284 - "SET pids = %ld\n"
2396 + "SET files = %zu\n"
2397 + "SET pids = %zu\n"
2398 "SET fds = %d\n"
2286 - "SET targets = %ld\n"
2399 + "SET targets = %zu\n"
2400 "END\n"
2401 "BEGIN netdata.apps_fix %llu\n"
2289 - "SET utime = %llu\n"
2290 - "SET stime = %llu\n"
2291 - "SET gtime = %llu\n"
2292 - "SET minflt = %llu\n"
2293 - "SET majflt = %llu\n"
2402 + "SET utime = %u\n"
2403 + "SET stime = %u\n"
2404 + "SET gtime = %u\n"
2405 + "SET minflt = %u\n"
2406 + "SET majflt = %u\n"
2407 "END\n"
2408 , usec
2409 , cpuuser
@@ -2299,48 +2412,48 @@ static usec_t send_resource_usage_to_netdata() {
2412 , file_counter
2413 , all_pids_count
2414 , all_files_len
2302 - , apps_groups_targets
2415 + , apps_groups_targets_count
2416 , usec
2304 - , (unsigned long long)(utime_fix_ratio * 100 * RATES_DETAIL)
2305 - , (unsigned long long)(stime_fix_ratio * 100 * RATES_DETAIL)
2306 - , (unsigned long long)(gtime_fix_ratio * 100 * RATES_DETAIL)
2307 - , (unsigned long long)(minflt_fix_ratio * 100 * RATES_DETAIL)
2308 - , (unsigned long long)(majflt_fix_ratio * 100 * RATES_DETAIL)
2417 + , (unsigned int)(utime_fix_ratio * 100 * RATES_DETAIL)
2418 + , (unsigned int)(stime_fix_ratio * 100 * RATES_DETAIL)
2419 + , (unsigned int)(gtime_fix_ratio * 100 * RATES_DETAIL)
2420 + , (unsigned int)(minflt_fix_ratio * 100 * RATES_DETAIL)
2421 + , (unsigned int)(majflt_fix_ratio * 100 * RATES_DETAIL)
2422 );
2423
2424 if(include_exited_childs)
2425 fprintf(stdout,
2426 "BEGIN netdata.apps_children_fix %llu\n"
2314 - "SET cutime = %llu\n"
2315 - "SET cstime = %llu\n"
2316 - "SET cgtime = %llu\n"
2317 - "SET cminflt = %llu\n"
2318 - "SET cmajflt = %llu\n"
2427 + "SET cutime = %u\n"
2428 + "SET cstime = %u\n"
2429 + "SET cgtime = %u\n"
2430 + "SET cminflt = %u\n"
2431 + "SET cmajflt = %u\n"
2432 "END\n"
2433 , usec
2321 - , (unsigned long long)(cutime_fix_ratio * 100 * RATES_DETAIL)
2322 - , (unsigned long long)(cstime_fix_ratio * 100 * RATES_DETAIL)
2323 - , (unsigned long long)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2324 - , (unsigned long long)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2325 - , (unsigned long long)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2434 + , (unsigned int)(cutime_fix_ratio * 100 * RATES_DETAIL)
2435 + , (unsigned int)(cstime_fix_ratio * 100 * RATES_DETAIL)
2436 + , (unsigned int)(cgtime_fix_ratio * 100 * RATES_DETAIL)
2437 + , (unsigned int)(cminflt_fix_ratio * 100 * RATES_DETAIL)
2438 + , (unsigned int)(cmajflt_fix_ratio * 100 * RATES_DETAIL)
2439 );
2440
2441 return usec;
2442 }
2443
2331 -static void normalize_data(struct target *root) {
2444 +static void normalize_utilization(struct target *root) {
2445 struct target *w;
2446
2447 // childs processing introduces spikes
2448 // here we try to eliminate them by disabling childs processing either for specific dimensions
2449 // or entirely. Of course, either way, we disable it just a single iteration.
2450
2338 - unsigned long long max = processors * hz * RATES_DETAIL;
2339 - unsigned long long utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2451 + kernel_uint_t max_time = processors * hz * RATES_DETAIL;
2452 + kernel_uint_t utime = 0, cutime = 0, stime = 0, cstime = 0, gtime = 0, cgtime = 0, minflt = 0, cminflt = 0, majflt = 0, cmajflt = 0;
2453
2341 - if(global_utime > max) global_utime = max;
2342 - if(global_stime > max) global_stime = max;
2343 - if(global_gtime > max) global_gtime = max;
2454 + if(global_utime > max_time) global_utime = max_time;
2455 + if(global_stime > max_time) global_stime = max_time;
2456 + if(global_gtime > max_time) global_gtime = max_time;
2457
2458 for(w = root; w ; w = w->next) {
2459 if(w->target || (!w->processes && !w->exposed)) continue;
@@ -2440,11 +2553,11 @@ static void normalize_data(struct target *root) {
2553
2554 if(unlikely(debug)) {
2555 fprintf(stderr,
2443 - "SYSTEM: u=%llu s=%llu g=%llu "
2444 - "COLLECTED: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2445 - "DELTA: u=%lld s=%lld g=%lld "
2556 + "SYSTEM: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
2557 + "COLLECTED: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
2558 + "DELTA: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " "
2559 "FIX: u=%0.2f s=%0.2f g=%0.2f cu=%0.2f cs=%0.2f cg=%0.2f "
2447 - "FINALLY: u=%llu s=%llu g=%llu cu=%llu cs=%llu cg=%llu "
2560 + "FINALLY: u=" KERNEL_UINT_FORMAT " s=" KERNEL_UINT_FORMAT " g=" KERNEL_UINT_FORMAT " cu=" KERNEL_UINT_FORMAT " cs=" KERNEL_UINT_FORMAT " cg=" KERNEL_UINT_FORMAT " "
2561 "\n"
2562 , global_utime
2563 , global_stime
@@ -2455,21 +2568,21 @@ static void normalize_data(struct target *root) {
2568 , cutime
2569 , cstime
2570 , cgtime
2458 - , (long long)utime + (long long)cutime - (long long)global_utime
2459 - , (long long)stime + (long long)cstime - (long long)global_stime
2460 - , (long long)gtime + (long long)cgtime - (long long)global_gtime
2571 + , utime + cutime - global_utime
2572 + , stime + cstime - global_stime
2573 + , gtime + cgtime - global_gtime
2574 , utime_fix_ratio
2575 , stime_fix_ratio
2576 , gtime_fix_ratio
2577 , cutime_fix_ratio
2578 , cstime_fix_ratio
2579 , cgtime_fix_ratio
2467 - , (unsigned long long)(utime * utime_fix_ratio)
2468 - , (unsigned long long)(stime * stime_fix_ratio)
2469 - , (unsigned long long)(gtime * gtime_fix_ratio)
2470 - , (unsigned long long)(cutime * cutime_fix_ratio)
2471 - , (unsigned long long)(cstime * cstime_fix_ratio)
2472 - , (unsigned long long)(cgtime * cgtime_fix_ratio)
2580 + , (kernel_uint_t)(utime * utime_fix_ratio)
2581 + , (kernel_uint_t)(stime * stime_fix_ratio)
2582 + , (kernel_uint_t)(gtime * gtime_fix_ratio)
2583 + , (kernel_uint_t)(cutime * cutime_fix_ratio)
2584 + , (kernel_uint_t)(cstime * cstime_fix_ratio)
2585 + , (kernel_uint_t)(cgtime * cgtime_fix_ratio)
2586 );
2587 }
2588 }
@@ -2480,21 +2593,21 @@ static void send_collected_data_to_netdata(struct target *root, const char *type
2593 send_BEGIN(type, "cpu", usec);
2594 for (w = root; w ; w = w->next) {
2595 if(unlikely(w->exposed))
2483 - send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (unsigned long long)(w->stime * stime_fix_ratio) + (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio) + (unsigned long long)(w->cstime * cstime_fix_ratio) + (unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2596 + send_SET(w->name, (kernel_uint_t)(w->utime * utime_fix_ratio) + (kernel_uint_t)(w->stime * stime_fix_ratio) + (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cutime * cutime_fix_ratio) + (kernel_uint_t)(w->cstime * cstime_fix_ratio) + (kernel_uint_t)(w->cgtime * cgtime_fix_ratio)):0ULL));
2597 }
2598 send_END();
2599
2600 send_BEGIN(type, "cpu_user", usec);
2601 for (w = root; w ; w = w->next) {
2602 if(unlikely(w->exposed))
2490 - send_SET(w->name, (unsigned long long)(w->utime * utime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cutime * cutime_fix_ratio)):0ULL));
2603 + send_SET(w->name, (kernel_uint_t)(w->utime * utime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cutime * cutime_fix_ratio)):0ULL));
2604 }
2605 send_END();
2606
2607 send_BEGIN(type, "cpu_system", usec);
2608 for (w = root; w ; w = w->next) {
2609 if(unlikely(w->exposed))
2497 - send_SET(w->name, (unsigned long long)(w->stime * stime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cstime * cstime_fix_ratio)):0ULL));
2610 + send_SET(w->name, (kernel_uint_t)(w->stime * stime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cstime * cstime_fix_ratio)):0ULL));
2611 }
2612 send_END();
2613
@@ -2502,7 +2615,7 @@ static void send_collected_data_to_netdata(struct target *root, const char *type
2615 send_BEGIN(type, "cpu_guest", usec);
2616 for (w = root; w ; w = w->next) {
2617 if(unlikely(w->exposed))
2505 - send_SET(w->name, (unsigned long long)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cgtime * cgtime_fix_ratio)):0ULL));
2618 + send_SET(w->name, (kernel_uint_t)(w->gtime * gtime_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cgtime * cgtime_fix_ratio)):0ULL));
2619 }
2620 send_END();
2621 }
@@ -2538,14 +2651,14 @@ static void send_collected_data_to_netdata(struct target *root, const char *type
2651 send_BEGIN(type, "minor_faults", usec);
2652 for (w = root; w ; w = w->next) {
2653 if(unlikely(w->exposed))
2541 - send_SET(w->name, (unsigned long long)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cminflt * cminflt_fix_ratio)):0ULL));
2654 + send_SET(w->name, (kernel_uint_t)(w->minflt * minflt_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cminflt * cminflt_fix_ratio)):0ULL));
2655 }
2656 send_END();
2657
2658 send_BEGIN(type, "major_faults", usec);
2659 for (w = root; w ; w = w->next) {
2660 if(unlikely(w->exposed))
2548 - send_SET(w->name, (unsigned long long)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((unsigned long long)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2661 + send_SET(w->name, (kernel_uint_t)(w->majflt * majflt_fix_ratio) + (include_exited_childs?((kernel_uint_t)(w->cmajflt * cmajflt_fix_ratio)):0ULL));
2662 }
2663 send_END();
2664
@@ -2911,7 +3024,7 @@ int main(int argc, char **argv)
3024 parse_args(argc, argv);
3025
3026 all_pids_sortlist = callocz(sizeof(pid_t), (size_t)pid_max);
2914 - all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
3027 + all_pids = callocz(sizeof(struct pid_stat *), (size_t) pid_max);
3028
3029 fprintf(stdout,
3030 "CHART netdata.apps_cpu '' 'Apps Plugin CPU' 'milliseconds/s' apps.plugin netdata.apps_cpu stacked 140000 %1$d\n"
@@ -2962,14 +3075,14 @@ int main(int argc, char **argv)
3075 }
3076 #endif
3077
2965 - if(!collect_data_for_all_processes_from_proc()) {
3078 + if(!collect_data_for_all_processes()) {
3079 error("Cannot collect /proc data for running processes. Disabling apps.plugin...");
3080 printf("DISABLE\n");
3081 exit(1);
3082 }
3083
3084 calculate_netdata_statistics();
2972 - normalize_data(apps_groups_root_target);
3085 + normalize_utilization(apps_groups_root_target);
3086
3087 usec_t dt = send_resource_usage_to_netdata();
3088
@@ -2995,7 +3108,7 @@ int main(int argc, char **argv)
3108 show_guest_time_old = show_guest_time;
3109
3110 if(unlikely(debug))
2998 - fprintf(stderr, "apps.plugin: done Loop No %llu\n", global_iterations_counter);
3111 + fprintf(stderr, "apps.plugin: done Loop No %zu\n", global_iterations_counter);
3112
3113 time_t current_t = now_realtime_sec();
3114
src/common.c
+9 -14
@@ -1155,30 +1155,25 @@ pid_t get_system_pid_max(void) {
1155 return pid_max;
1156 #else
1157
1158 + static char read = 0;
1159 + if(unlikely(read)) return pid_max;
1160 + read = 1;
1161 +
1162 char filename[FILENAME_MAX + 1];
1163 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", global_host_prefix);
1160 - procfile *ff = procfile_open(filename, NULL, PROCFILE_FLAG_DEFAULT);
1161 - if(!ff) {
1162 - error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1163 - return pid_max;
1164 - }
1164
1166 - ff = procfile_readall(ff);
1167 - if(!ff) {
1168 - error("Cannot read file '%s'. Assuming system supports %d pids.", filename, pid_max);
1165 + unsigned long long max = 0;
1166 + if(read_single_number_file(filename, &max) != 0) {
1167 + error("Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
1168 return pid_max;
1169 }
1170
1172 - pid_max = (pid_t)str2i(procfile_lineword(ff, 0, 0));
1173 - if(!pid_max) {
1174 - procfile_close(ff);
1175 - pid_max = 32768;
1171 + if(!max) {
1172 error("Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
1173 return pid_max;
1174 }
1175
1180 - procfile_close(ff);
1181 - debug(D_SYSTEM, "System supports %d pids.", pid_max);
1176 + pid_max = (pid_t) max;
1177 return pid_max;
1178
1179 #endif /* __APPLE__ */
src/inlined.h
+33
@@ -3,6 +3,19 @@
3
4 #include "common.h"
5
6 +#ifdef KERNEL_32BIT
7 +typedef uint32_t kernel_uint_t;
8 +#define str2kernel_unit_t(string) str2uint32_t(string)
9 +#define KERNEL_UINT_FORMAT "%u"
10 +#else
11 +typedef uint64_t kernel_uint_t;
12 +#define str2kernel_unit_t(string) str2uint64_t(string)
13 +#define KERNEL_UINT_FORMAT "%lu"
14 +#endif
15 +
16 +#define str2pid_t(string) str2uint32_t(string)
17 +
18 +
19 // for faster execution, allow the compiler to inline
20 // these functions that are called thousands of times per second
21
@@ -70,6 +83,26 @@ static inline long str2l(const char *s) {
83 return n;
84 }
85
86 +static inline uint32_t str2uint32_t(const char *s) {
87 + uint32_t n = 0;
88 + char c;
89 + for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
90 + n *= 10;
91 + n += c - '0';
92 + }
93 + return n;
94 +}
95 +
96 +static inline uint64_t str2uint64_t(const char *s) {
97 + uint64_t n = 0;
98 + char c;
99 + for(c = *s; c >= '0' && c <= '9' ; c = *(++s)) {
100 + n *= 10;
101 + n += c - '0';
102 + }
103 + return n;
104 +}
105 +
106 static inline unsigned long str2ul(const char *s) {
107 unsigned long n = 0;
108 char c;