@cryptotaxi247 / netdata-1 / commits / 36d92aa30

handle also the case that neither CLOCK_BOOTTIME nor /proc/uptime are available

Costa Tsaousis (ktsaou) committed Feb 19, 2018 at 01:47 UTC 36d92aa30f5d19b85a52df9f041d96bf003c7956
1 file changed +33 -25
src/proc_uptime.c
+33 -25
@@ -1,64 +1,72 @@
1 #include "common.h"
2
3 -static procfile *ff = NULL;
3 +static inline collected_number uptime_from_boottime(void) {
4 +#ifdef CLOCK_BOOTTIME_IS_AVAILABLE
5 + return now_boottime_usec() / 1000;
6 +#else
7 + error("uptime cannot be read from CLOCK_BOOTTIME on this system.");
8 + return 0;
9 +#endif
10 +}
11
5 -static collected_number read_proc_uptime(void) {
6 - if(unlikely(!ff)) {
12 +static procfile *read_proc_uptime_ff = NULL;
13 +static inline collected_number read_proc_uptime(void) {
14 + if(unlikely(!read_proc_uptime_ff)) {
15 char filename[FILENAME_MAX + 1];
16 snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/uptime");
17
10 - ff = procfile_open(config_get("plugin:proc:/proc/uptime", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
11 - if(unlikely(!ff))
12 - return 1;
18 + read_proc_uptime_ff = procfile_open(config_get("plugin:proc:/proc/uptime", "filename to monitor", filename), " \t", PROCFILE_FLAG_DEFAULT);
19 + if(unlikely(!read_proc_uptime_ff)) return 0;
20 }
21
15 - ff = procfile_readall(ff);
16 - if(unlikely(!ff))
17 - return 0; // we return 0, so that we will retry to open it next time
22 + read_proc_uptime_ff = procfile_readall(read_proc_uptime_ff);
23 + if(unlikely(!read_proc_uptime_ff)) return 0;
24
19 - if(unlikely(procfile_lines(ff) < 1)) {
25 + if(unlikely(procfile_lines(read_proc_uptime_ff) < 1)) {
26 error("/proc/uptime has no lines.");
21 - return 1;
27 + return 0;
28 }
23 - if(unlikely(procfile_linewords(ff, 0) < 1)) {
29 + if(unlikely(procfile_linewords(read_proc_uptime_ff, 0) < 1)) {
30 error("/proc/uptime has less than 1 word in it.");
25 - return 1;
31 + return 0;
32 }
33
28 - return (collected_number)(strtold(procfile_lineword(ff, 0, 0), NULL) * 1000.0);
34 + return (collected_number)(strtold(procfile_lineword(read_proc_uptime_ff, 0, 0), NULL) * 1000.0);
35 }
36
37 int do_proc_uptime(int update_every, usec_t dt) {
38 (void)dt;
39
34 - collected_number uptime;
35 -
36 -#ifdef CLOCK_BOOTTIME_IS_AVAILABLE
40 static int use_boottime = -1;
41
42 if(unlikely(use_boottime == -1)) {
40 - long long delta = (long long)(now_boottime_usec() / 1000) - (long long)read_proc_uptime();
43 + collected_number uptime_boottime = uptime_from_boottime();
44 + collected_number uptime_proc = read_proc_uptime();
45 +
46 + long long delta = (long long)uptime_boottime - (long long)uptime_proc;
47 if(delta < 0) delta = -delta;
48
43 - if(delta <= 1000) {
44 - procfile_close(ff);
49 + if(delta <= 1000 && uptime_boottime != 0) {
50 + procfile_close(read_proc_uptime_ff);
51 info("Using now_boottime_usec() for uptime (dt is %lld ms)", delta);
52 use_boottime = 1;
53 }
48 - else {
54 + else if(uptime_proc != 0) {
55 info("Using /proc/uptime for uptime (dt is %lld ms)", delta);
56 use_boottime = 0;
57 }
58 + else {
59 + error("Cannot find any way to read uptime on this system.");
60 + return 1;
61 + }
62 }
63
64 + collected_number uptime;
65 if(use_boottime)
55 - uptime = now_boottime_usec() / 1000;
66 + uptime = uptime_from_boottime();
67 else
68 uptime = read_proc_uptime();
69
59 -#else
60 - uptime = read_proc_uptime();
61 -#endif
70
71 // --------------------------------------------------------------------
72