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 --- darwin/Platform.c | 24 ++++++++++++++++++++++++ darwin/Platform.h | 7 +++++++ 2 files changed, 31 insertions(+) (limited to 'darwin') diff --git a/darwin/Platform.c b/darwin/Platform.c index 3842fddc..ca8df611 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -37,6 +37,10 @@ in the source distribution for its full text. #include "zfs/ZfsArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h" +#ifdef HAVE_HOST_GET_CLOCK_SERVICE +#include +#include +#endif #ifdef HAVE_MACH_MACH_TIME_H #include #endif @@ -405,3 +409,23 @@ void Platform_getBattery(double* percent, ACPresence* isOnAC) { CFRelease(list); CFRelease(power_sources); } + +void Platform_gettime_monotonic(uint64_t* msec) { + +#ifdef HAVE_HOST_GET_CLOCK_SERVICE + + clock_serv_t cclock; + mach_timespec_t mts; + + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + + *msec = ((uint64_t)mts.tv_sec * 1000) + ((uint64_t)mts.tv_nsec / 1000000); + +#else + + Generic_gettime_monotomic(msec); + +#endif +} diff --git a/darwin/Platform.h b/darwin/Platform.h index 7bce9b35..902d27ef 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -19,6 +19,7 @@ in the source distribution for its full text. #include "NetworkIOMeter.h" #include "ProcessLocksScreen.h" #include "SignalsPanel.h" +#include "generic/gettime.h" #include "generic/hostname.h" #include "generic/uname.h" @@ -85,4 +86,10 @@ static inline bool Platform_getLongOption(ATTR_UNUSED int opt, ATTR_UNUSED int a return false; } +static inline void Platform_gettime_realtime(struct timeval* tv, uint64_t* msec) { + Generic_gettime_realtime(tv, msec); +} + +void Platform_gettime_monotonic(uint64_t* msec); + #endif -- cgit v1.2.3