diff options
author | Vladimir Kobal <vlad@prokk.net> | 2019-08-29 20:35:05 +0300 |
---|---|---|
committer | Paul Emm. Katsoulakis <34388743+paulkatsoulakis@users.noreply.github.com> | 2019-08-29 20:35:05 +0300 |
commit | c79112e85317d80f46acb26296e47a7da8d9d8b0 (patch) | |
tree | 7c65b3df8a78e0ee0ac38bea4746fe6aa2bc7dff /libnetdata | |
parent | 1907c1486fba9ecb534058d1d3b7573777b67b82 (diff) |
Display uptime for processes (#6654)
* Get process uptime
* Calculate target uptime
* Update charts
* Show collected data
* Fix chart names
* Update the documentation
* Fix a flag value
* Add an explanation note for the 'carried over uptime' chart
* Move the functions for getting uptime to libnetdata
* Rename the function for geting uptime
* Remove redundant code
* Fix starttime calculation
* More accurate definition for the carried over uptime
* fix group starttime calculation
* Fix typo
Diffstat (limited to 'libnetdata')
-rw-r--r-- | libnetdata/clocks/clocks.c | 65 | ||||
-rw-r--r-- | libnetdata/clocks/clocks.h | 2 |
2 files changed, 67 insertions, 0 deletions
diff --git a/libnetdata/clocks/clocks.c b/libnetdata/clocks/clocks.c index f7d2171780..161225a9b6 100644 --- a/libnetdata/clocks/clocks.c +++ b/libnetdata/clocks/clocks.c @@ -210,3 +210,68 @@ int sleep_usec(usec_t usec) { return ret; #endif } + +static inline collected_number uptime_from_boottime(void) { +#ifdef CLOCK_BOOTTIME_IS_AVAILABLE + return now_boottime_usec() / 1000; +#else + error("uptime cannot be read from CLOCK_BOOTTIME on this system."); + return 0; +#endif +} + +static procfile *read_proc_uptime_ff = NULL; +static inline collected_number read_proc_uptime(char *filename) { + if(unlikely(!read_proc_uptime_ff)) { + read_proc_uptime_ff = procfile_open(filename, " \t", PROCFILE_FLAG_DEFAULT); + if(unlikely(!read_proc_uptime_ff)) return 0; + } + + read_proc_uptime_ff = procfile_readall(read_proc_uptime_ff); + if(unlikely(!read_proc_uptime_ff)) return 0; + + if(unlikely(procfile_lines(read_proc_uptime_ff) < 1)) { + error("/proc/uptime has no lines."); + return 0; + } + if(unlikely(procfile_linewords(read_proc_uptime_ff, 0) < 1)) { + error("/proc/uptime has less than 1 word in it."); + return 0; + } + + return (collected_number)(strtold(procfile_lineword(read_proc_uptime_ff, 0, 0), NULL) * 1000.0); +} + +inline collected_number uptime_msec(char *filename){ + static int use_boottime = -1; + + if(unlikely(use_boottime == -1)) { + collected_number uptime_boottime = uptime_from_boottime(); + collected_number uptime_proc = read_proc_uptime(filename); + + long long delta = (long long)uptime_boottime - (long long)uptime_proc; + if(delta < 0) delta = -delta; + + if(delta <= 1000 && uptime_boottime != 0) { + procfile_close(read_proc_uptime_ff); + info("Using now_boottime_usec() for uptime (dt is %lld ms)", delta); + use_boottime = 1; + } + else if(uptime_proc != 0) { + info("Using /proc/uptime for uptime (dt is %lld ms)", delta); + use_boottime = 0; + } + else { + error("Cannot find any way to read uptime on this system."); + return 1; + } + } + + collected_number uptime; + if(use_boottime) + uptime = uptime_from_boottime(); + else + uptime = read_proc_uptime(filename); + + return uptime; +} diff --git a/libnetdata/clocks/clocks.h b/libnetdata/clocks/clocks.h index 47aa148c12..4af451d60a 100644 --- a/libnetdata/clocks/clocks.h +++ b/libnetdata/clocks/clocks.h @@ -136,4 +136,6 @@ extern int sleep_usec(usec_t usec); */ void test_clock_boottime(void); +extern collected_number uptime_msec(char *filename); + #endif /* NETDATA_CLOCKS_H */ |