@cryptotaxi247 / netdata-1 / commits / 88037bf63

FreeBSD has CLOCK_UPTIME feature for clock_gettime.

https://www.freebsd.org/cgi/man.cgi?query=clock_gettime&apropos=0&sektion=0&manpath=FreeBSD+10.0-RELEASE&arch=default&format=html the functions below make the same job, but clock version is short, fast i think. //------8<----------------------------------------------------------- int64_t uptime_via_clock() { struct timespec ts; clock_gettime(CLOCK_UPTIME , &ts); return ts.tv_sec; } int64_t uptime_via_sysctl() { struct timespec cur_time , boot_time; size_t len = sizeof(struct timespec); sysctlbyname("kern.boottime" , &boot_time , &len , NULL , 0); clock_gettime(CLOCK_REALTIME , &cur_time); return (cur_time.tv_sec - boot_time.tv_sec); } int main() { printf("Uptime Via Clock: %ld\n",uptime_via_clock()); printf("Uptime Via Sysctl: %ld\n",uptime_via_sysctl()); return 0; } //------8<-----------------------------------------------------------

User Sternix committed Mar 2, 2017 at 21:31 UTC 88037bf63132dd5d5b15b80647d65a75fcb82055
1 file changed +3 -8
src/freebsd_sysctl.c
+3 -8
@@ -274,7 +274,7 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
274 char mntonname[MNAMELEN + 1];
275
276 // NEEDED BY: do_uptime
277 - struct timespec boot_time, cur_time;
277 + struct timespec up_time;
278
279 // --------------------------------------------------------------------
280
@@ -2181,11 +2181,7 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
2181 // --------------------------------------------------------------------
2182
2183 if (likely(do_uptime)) {
2184 - if (unlikely(GETSYSCTL("kern.boottime", boot_time))) {
2185 - do_uptime = 0;
2186 - error("DISABLED: system.uptime");
2187 - } else {
2188 - clock_gettime(CLOCK_REALTIME, &cur_time);
2184 + clock_gettime(CLOCK_UPTIME, &up_time);
2185 st = rrdset_find_localhost("system.uptime");
2186
2187 if(unlikely(!st)) {
@@ -2194,9 +2190,8 @@ int do_freebsd_sysctl(int update_every, usec_t dt) {
2190 }
2191 else rrdset_next(st);
2192
2197 - rrddim_set(st, "uptime", cur_time.tv_sec - boot_time.tv_sec);
2193 + rrddim_set(st, "uptime", up_time.tv_sec);
2194 rrdset_done(st);
2199 - }
2195 }
2196
2197 return 0;