From 356488aa53e8c0bedeb7641685d931c8900098c5 Mon Sep 17 00:00:00 2001 From: Nathan Scott Date: Tue, 30 Mar 2021 15:55:48 +1100 Subject: Request the realtime and monotonic clock times once per sample Refactor the sample time code to make one call to gettimeofday (aka the realtime clock in clock_gettime, when available) and one to the monotonic clock. Stores each in more appropriately named ProcessList fields for ready access when needed. Every platform gets the opportunity to provide their own clock code, and the existing Mac OS X specific code is moved below darwin instead of in Compat. A couple of leftover time(2) calls are converted to use these ProcessList fields as well, instead of yet again sampling the system clock. Related to https://github.com/htop-dev/htop/pull/574 --- generic/gettime.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ generic/gettime.h | 19 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 generic/gettime.c create mode 100644 generic/gettime.h (limited to 'generic') diff --git a/generic/gettime.c b/generic/gettime.c new file mode 100644 index 00000000..ef7d2290 --- /dev/null +++ b/generic/gettime.c @@ -0,0 +1,57 @@ +/* +htop - generic/gettime.c +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ +#include "config.h" // IWYU pragma: keep + +#include +#include + +#include "generic/gettime.h" + + +void Generic_gettime_realtime(struct timeval* tvp, uint64_t* msec) { + +#if defined(HAVE_CLOCK_GETTIME) + + struct timespec ts; + if (clock_gettime(CLOCK_REALTIME, &ts) == 0) { + tvp->tv_sec = ts.tv_sec; + tvp->tv_usec = ts.tv_nsec / 1000; + *msec = ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000); + } else { + memset(tvp, 0, sizeof(struct timeval)); + *msec = 0; + } + +#else /* lower resolution gettimeofday(2) is always available */ + + struct timeval tv; + if (gettimeofday(&tv, NULL) == 0) { + *tsp = tv; /* struct copy */ + *msec = ((uint64_t)tv.tv_sec * 1000) + ((uint64_t)tv.tv_usec / 1000); + } else { + memset(tvp, 0, sizeof(struct timeval)); + *msec = 0; + } + +#endif +} + +void Generic_gettime_monotonic(uint64_t* msec) { +#if defined(HAVE_CLOCK_GETTIME) + + struct timespec ts; + if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) + *msec = ((uint64_t)ts.tv_sec * 1000) + ((uint64_t)ts.tv_nsec / 1000000); + else + *msec = 0; + +#else + +# error "No monotonic clock available" + +#endif +} diff --git a/generic/gettime.h b/generic/gettime.h new file mode 100644 index 00000000..fab33da1 --- /dev/null +++ b/generic/gettime.h @@ -0,0 +1,19 @@ +#ifndef HEADER_gettime +#define HEADER_gettime +/* +htop - generic/gettime.h +(C) 2021 htop dev team +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include +#include +#include + + +void Generic_gettime_realtime(struct timeval* ts, uint64_t* msec); + +void Generic_gettime_monotonic(uint64_t* msec); + +#endif -- cgit v1.2.3