disable apps.plugin when there are not any processes to collect data for
Costa Tsaousis (ktsaou) committed
Dec 30, 2016 at 03:01 UTC
fca208abfa7743a6e54dc921d7198be29980b957
1 file changed
+50
-41
src/apps_plugin.c
+50
-41
@@ -121,7 +121,7 @@ long apps_groups_targets = 0;
121
struct target *users_root_target = NULL;
122
struct target *groups_root_target = NULL;
123
124
-struct target *get_users_target(uid_t uid)
124
+static struct target *get_users_target(uid_t uid)
125
{
126
struct target *w;
127
for(w = users_root_target ; w ; w = w->next)
@@ -189,7 +189,7 @@ struct target *get_groups_target(gid_t gid)
189
190
// find or create a new target
191
// there are targets that are just aggregated to other target (the second argument)
192
-struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
192
+static struct target *get_apps_groups_target(const char *id, struct target *target, const char *name) {
193
int tdebug = 0, thidden = target?target->hidden:0, ends_with = 0;
194
const char *nid = id;
195
@@ -278,7 +278,7 @@ struct target *get_apps_groups_target(const char *id, struct target *target, con
278
}
279
280
// read the apps_groups.conf file
281
-int read_apps_groups_conf(const char *file)
281
+static int read_apps_groups_conf(const char *file)
282
{
283
char filename[FILENAME_MAX + 1];
284
@@ -483,7 +483,7 @@ struct pid_stat {
483
484
long all_pids_count = 0;
485
486
-struct pid_stat *get_pid_entry(pid_t pid) {
486
+static inline struct pid_stat *get_pid_entry(pid_t pid) {
487
if(all_pids[pid]) {
488
all_pids[pid]->new_entry = 0;
489
return all_pids[pid];
@@ -505,7 +505,7 @@ struct pid_stat *get_pid_entry(pid_t pid) {
505
return all_pids[pid];
506
}
507
508
-void del_pid_entry(pid_t pid) {
508
+static inline void del_pid_entry(pid_t pid) {
509
if(!all_pids[pid]) {
510
error("attempted to free pid %d that is not allocated.", pid);
511
return;
@@ -533,7 +533,7 @@ void del_pid_entry(pid_t pid) {
533
// ----------------------------------------------------------------------------
534
// update pids from proc
535
536
-int read_proc_pid_cmdline(struct pid_stat *p) {
536
+static inline int read_proc_pid_cmdline(struct pid_stat *p) {
537
538
if(unlikely(!p->cmdline_filename)) {
539
char filename[FILENAME_MAX + 1];
@@ -564,7 +564,7 @@ cleanup:
564
return 0;
565
}
566
567
-int read_proc_pid_ownership(struct pid_stat *p) {
567
+static inline int read_proc_pid_ownership(struct pid_stat *p) {
568
if(unlikely(!p->stat_filename)) {
569
error("pid %d does not have a stat_filename", p->pid);
570
return 0;
@@ -585,7 +585,7 @@ int read_proc_pid_ownership(struct pid_stat *p) {
585
return 1;
586
}
587
588
-int read_proc_pid_stat(struct pid_stat *p) {
588
+static inline int read_proc_pid_stat(struct pid_stat *p) {
589
static procfile *ff = NULL;
590
591
if(unlikely(!p->stat_filename)) {
@@ -731,7 +731,7 @@ cleanup:
731
return 0;
732
}
733
734
-int read_proc_pid_statm(struct pid_stat *p) {
734
+static inline int read_proc_pid_statm(struct pid_stat *p) {
735
static procfile *ff = NULL;
736
737
if(unlikely(!p->statm_filename)) {
@@ -769,7 +769,7 @@ cleanup:
769
return 0;
770
}
771
772
-int read_proc_pid_io(struct pid_stat *p) {
772
+static inline int read_proc_pid_io(struct pid_stat *p) {
773
static procfile *ff = NULL;
774
775
if(unlikely(!p->io_filename)) {
@@ -847,7 +847,7 @@ unsigned long long global_utime = 0;
847
unsigned long long global_stime = 0;
848
unsigned long long global_gtime = 0;
849
850
-int read_proc_stat() {
850
+static inline int read_proc_stat() {
851
static char filename[FILENAME_MAX + 1] = "";
852
static procfile *ff = NULL;
853
static unsigned long long utime_raw = 0, stime_raw = 0, gtime_raw = 0, gntime_raw = 0, ntime_raw = 0;
@@ -983,7 +983,7 @@ static struct file_descriptor *file_descriptor_find(const char *name, uint32_t h
983
#define FILETYPE_TIMERFD 7
984
#define FILETYPE_SIGNALFD 8
985
986
-void file_descriptor_not_used(int id)
986
+static inline void file_descriptor_not_used(int id)
987
{
988
if(id > 0 && id < all_files_size) {
989
@@ -1017,7 +1017,7 @@ void file_descriptor_not_used(int id)
1017
else error("Request to decrease counter of fd %d, which is outside the array size (1 to %d)", id, all_files_size);
1018
}
1019
1020
-int file_descriptor_find_or_add(const char *name)
1020
+static inline int file_descriptor_find_or_add(const char *name)
1021
{
1022
static int last_pos = 0;
1023
uint32_t hash = simple_hash(name);
@@ -1153,7 +1153,7 @@ int file_descriptor_find_or_add(const char *name)
1153
return c;
1154
}
1155
1156
-int read_pid_file_descriptors(struct pid_stat *p) {
1156
+static inline int read_pid_file_descriptors(struct pid_stat *p) {
1157
char dirname[FILENAME_MAX+1];
1158
1159
snprintfz(dirname, FILENAME_MAX, "%s/proc/%d/fd", global_host_prefix, p->pid);
@@ -1231,7 +1231,7 @@ int read_pid_file_descriptors(struct pid_stat *p) {
1231
1232
// ----------------------------------------------------------------------------
1233
1234
-int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1234
+static inline int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1235
char *prefix = "\\_ ";
1236
int indent = 0;
1237
@@ -1270,13 +1270,13 @@ int print_process_and_parents(struct pid_stat *p, unsigned long long time) {
1270
return indent + 1;
1271
}
1272
1273
-void print_process_tree(struct pid_stat *p, char *msg) {
1273
+static inline void print_process_tree(struct pid_stat *p, char *msg) {
1274
log_date(stderr);
1275
fprintf(stderr, "%s: process %s (%d, %s) with parents:\n", msg, p->comm, p->pid, p->updated?"running":"exited");
1276
print_process_and_parents(p, p->stat_collected_usec);
1277
}
1278
1279
-void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1279
+static inline void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int type) {
1280
int found = 0;
1281
struct pid_stat *p = NULL;
1282
@@ -1346,7 +1346,7 @@ void find_lost_child_debug(struct pid_stat *pe, unsigned long long lost, int typ
1346
}
1347
}
1348
1349
-unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1349
+static inline unsigned long long remove_exited_child_from_parent(unsigned long long *field, unsigned long long *pfield) {
1350
unsigned long long absorbed = 0;
1351
1352
if(*field > *pfield) {
@@ -1363,7 +1363,7 @@ unsigned long long remove_exited_child_from_parent(unsigned long long *field, un
1363
return absorbed;
1364
}
1365
1366
-void process_exited_processes() {
1366
+static inline void process_exited_processes() {
1367
struct pid_stat *p;
1368
1369
for(p = root_of_pids; p ; p = p->next) {
@@ -1476,7 +1476,7 @@ void process_exited_processes() {
1476
}
1477
}
1478
1479
-void link_all_processes_to_their_parents(void) {
1479
+static inline void link_all_processes_to_their_parents(void) {
1480
struct pid_stat *p, *pp;
1481
1482
// link all children to their parents
@@ -1579,15 +1579,15 @@ static inline int managed_log(struct pid_stat *p, uint32_t log, int status) {
1579
return status;
1580
}
1581
1582
-void collect_data_for_pid(pid_t pid) {
1582
+static inline int collect_data_for_pid(pid_t pid) {
1583
if(unlikely(pid <= 0 || pid > pid_max)) {
1584
error("Invalid pid %d read (expected 1 to %d). Ignoring process.", pid, pid_max);
1585
- return;
1585
+ return 0;
1586
}
1587
1588
struct pid_stat *p = get_pid_entry(pid);
1589
- if(unlikely(!p || p->read)) return;
1590
- p->read = 1;
1589
+ if(unlikely(!p || p->read)) return 0;
1590
+ p->read = 1;
1591
1592
// fprintf(stderr, "Reading process %d (%s), sortlist %d\n", p->pid, p->comm, p->sortlist);
1593
@@ -1596,7 +1596,7 @@ void collect_data_for_pid(pid_t pid) {
1596
1597
if(unlikely(!managed_log(p, PID_LOG_STAT, read_proc_pid_stat(p))))
1598
// there is no reason to proceed if we cannot get its status
1599
- return;
1599
+ return 0;
1600
1601
read_proc_pid_ownership(p);
1602
@@ -1616,7 +1616,7 @@ void collect_data_for_pid(pid_t pid) {
1616
1617
if(unlikely(!managed_log(p, PID_LOG_STATM, read_proc_pid_statm(p))))
1618
// there is no reason to proceed if we cannot get its memory status
1619
- return;
1619
+ return 0;
1620
1621
// --------------------------------------------------------------------
1622
// link it
@@ -1675,9 +1675,11 @@ void collect_data_for_pid(pid_t pid) {
1675
p->updated = 1;
1676
p->keep = 0;
1677
p->keeploops = 0;
1678
+
1679
+ return 1;
1680
}
1681
1680
-int collect_data_for_all_processes_from_proc(void) {
1682
+static int collect_data_for_all_processes_from_proc(void) {
1683
struct pid_stat *p = NULL;
1684
1685
if(all_pids_count) {
@@ -1719,18 +1721,25 @@ int collect_data_for_all_processes_from_proc(void) {
1721
1722
struct dirent *file = NULL;
1723
1724
+ size_t found = 0;
1725
while((file = readdir(dir))) {
1726
char *endptr = file->d_name;
1727
pid_t pid = (pid_t) strtoul(file->d_name, &endptr, 10);
1728
1729
// make sure we read a valid number
1727
- if(unlikely(endptr == file->d_name || *endptr != '\0'))
1730
+ if(unlikely(endptr == file->d_name || *endptr != '\0')) {
1731
+ found++;
1732
continue;
1733
+ }
1734
1730
- collect_data_for_pid(pid);
1735
+ if(collect_data_for_pid(pid))
1736
+ found++;
1737
}
1738
closedir(dir);
1739
1740
+ if(!found)
1741
+ return 0;
1742
+
1743
// normally this is done
1744
// however we may have processes exited while we collected values
1745
// so let's find the exited ones
@@ -1760,7 +1769,7 @@ int collect_data_for_all_processes_from_proc(void) {
1769
// 9. find the unique file count for each target
1770
// check: update_apps_groups_statistics()
1771
1763
-void cleanup_exited_pids(void) {
1772
+static void cleanup_exited_pids(void) {
1773
int c;
1774
struct pid_stat *p = NULL;
1775
@@ -1788,7 +1797,7 @@ void cleanup_exited_pids(void) {
1797
}
1798
}
1799
1791
-void apply_apps_groups_targets_inheritance(void) {
1800
+static void apply_apps_groups_targets_inheritance(void) {
1801
struct pid_stat *p = NULL;
1802
1803
// children that do not have a target
@@ -1898,7 +1907,7 @@ void apply_apps_groups_targets_inheritance(void) {
1907
fprintf(stderr, "apps.plugin: apply_apps_groups_targets_inheritance() made %d loops on the process tree\n", loops);
1908
}
1909
1901
-long zero_all_targets(struct target *root) {
1910
+static long zero_all_targets(struct target *root) {
1911
struct target *w;
1912
long count = 0;
1913
@@ -1942,7 +1951,7 @@ long zero_all_targets(struct target *root) {
1951
return count;
1952
}
1953
1945
-void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1954
+static inline void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target *o) {
1955
(void)o;
1956
1957
if(unlikely(!w->fds))
@@ -2000,7 +2009,7 @@ void aggregate_pid_on_target(struct target *w, struct pid_stat *p, struct target
2009
}
2010
}
2011
2003
-void count_targets_fds(struct target *root) {
2012
+static inline void count_targets_fds(struct target *root) {
2013
int c;
2014
struct target *w;
2015
@@ -2062,7 +2071,7 @@ void count_targets_fds(struct target *root) {
2071
}
2072
}
2073
2065
-void calculate_netdata_statistics(void) {
2074
+static void calculate_netdata_statistics(void) {
2075
apply_apps_groups_targets_inheritance();
2076
2077
zero_all_targets(users_root_target);
@@ -2162,7 +2171,7 @@ static inline void send_END(void) {
2171
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;
2172
double minflt_fix_ratio = 1.0, majflt_fix_ratio = 1.0, cminflt_fix_ratio = 1.0, cmajflt_fix_ratio = 1.0;
2173
2165
-usec_t send_resource_usage_to_netdata() {
2174
+static usec_t send_resource_usage_to_netdata() {
2175
static struct timeval last = { 0, 0 };
2176
static struct rusage me_last;
2177
@@ -2250,7 +2259,7 @@ usec_t send_resource_usage_to_netdata() {
2259
return usec;
2260
}
2261
2253
-void normalize_data(struct target *root) {
2262
+static void normalize_data(struct target *root) {
2263
struct target *w;
2264
2265
// childs processing introduces spikes
@@ -2396,7 +2405,7 @@ void normalize_data(struct target *root) {
2405
}
2406
}
2407
2399
-void send_collected_data_to_netdata(struct target *root, const char *type, usec_t usec) {
2408
+static void send_collected_data_to_netdata(struct target *root, const char *type, usec_t usec) {
2409
struct target *w;
2410
2411
send_BEGIN(type, "cpu", usec);
@@ -2527,7 +2536,7 @@ void send_collected_data_to_netdata(struct target *root, const char *type, usec_
2536
// ----------------------------------------------------------------------------
2537
// generate the charts
2538
2530
-void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2539
+static void send_charts_updates_to_netdata(struct target *root, const char *type, const char *title)
2540
{
2541
struct target *w;
2542
int newly_added = 0;
@@ -2661,7 +2670,7 @@ void send_charts_updates_to_netdata(struct target *root, const char *type, const
2670
// ----------------------------------------------------------------------------
2671
// parse command line arguments
2672
2664
-void parse_args(int argc, char **argv)
2673
+static void parse_args(int argc, char **argv)
2674
{
2675
int i, freq = 0;
2676
char *name = NULL;
@@ -2767,7 +2776,7 @@ void parse_args(int argc, char **argv)
2776
if(!name) name = "groups";
2777
2778
if(read_apps_groups_conf(name)) {
2770
- error("Cannot read process groups %s", name);
2779
+ error("Cannot read process groups '%s/apps_%s.conf'. There are no internal defaults. Failing.", config_dir, name);
2780
exit(1);
2781
}
2782
}