From 59d0c5b26a55a68be059f2ac32a7c083b2ff01ee Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Sun, 22 Aug 2021 10:47:11 -0400 Subject: Refactor Darwin platform unit conversion helpers --- darwin/DarwinProcess.c | 11 ++--------- darwin/DarwinProcessList.c | 9 +-------- darwin/Platform.c | 29 ++++++++++++++++++++++------- darwin/Platform.h | 12 ++++++++---- 4 files changed, 33 insertions(+), 28 deletions(-) diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index d9834af3..70cce725 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -269,13 +269,6 @@ ERROR_A: Process_updateCmdline(proc, k->kp_proc.p_comm, 0, strlen(k->kp_proc.p_comm)); } -// Converts ticks in the Mach "timebase" to nanoseconds. -// See `mach_timebase_info`, as used to define the `Platform_timebaseToNS` constant. -static uint64_t machTicksToNanoseconds(uint64_t schedulerTicks) { - double nanoseconds_per_mach_tick = Platform_timebaseToNS; - return (uint64_t) (nanoseconds_per_mach_tick * (double) schedulerTicks); -} - // Converts nanoseconds to hundreths of a second (centiseconds) as needed by the "time" field of the Process struct. static long long int nanosecondsToCentiseconds(uint64_t nanoseconds) { const uint64_t centiseconds_per_second = 100; @@ -350,8 +343,8 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessList* if (sizeof(pti) == proc_pidinfo(proc->super.pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti))) { uint64_t total_existing_time_ns = proc->stime + proc->utime; - uint64_t user_time_ns = machTicksToNanoseconds(pti.pti_total_user); - uint64_t system_time_ns = machTicksToNanoseconds(pti.pti_total_system); + uint64_t user_time_ns = Platform_machTicksToNanoseconds(pti.pti_total_user); + uint64_t system_time_ns = Platform_machTicksToNanoseconds(pti.pti_total_system); uint64_t total_current_time_ns = user_time_ns + system_time_ns; diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 3cfdca6f..7dd86ff5 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -160,13 +160,6 @@ void ProcessList_delete(ProcessList* this) { free(this); } -// Converts "scheduler ticks" to nanoseconds. -// See `sysconf(_SC_CLK_TCK)`, as used to define the `Platform_clockTicksPerSec` constant. -static double schedulerTicksToNanoseconds(const double ticks) { - const double nanos_per_sec = 1e9; - return ticks * (nanos_per_sec / (double) Platform_clockTicksPerSec); -} - void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { DarwinProcessList* dpl = (DarwinProcessList*)super; bool preExisting = true; @@ -194,7 +187,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { } } - const double time_interval_ns = schedulerTicksToNanoseconds(dpl->global_diff) / (double) dpl->super.activeCPUs; + const double time_interval_ns = Platform_schedulerTicksToNanoseconds(dpl->global_diff) / (double) dpl->super.activeCPUs; /* Clear the thread counts */ super->kernelThreads = 0; diff --git a/darwin/Platform.c b/darwin/Platform.c index 2630e2d4..c5a11df2 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -120,9 +120,9 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -double Platform_timebaseToNS = 1.0; +static double Platform_nanosecondsPerMachTick = 1.0; -long Platform_clockTicksPerSec = -1; +static double Platform_schedulerTicksPerNS = -1; void Platform_init(void) { // Check if we can determine the timebase used on this system. @@ -130,18 +130,33 @@ void Platform_init(void) { #ifdef HAVE_MACH_TIMEBASE_INFO mach_timebase_info_data_t info; mach_timebase_info(&info); - Platform_timebaseToNS = (double)info.numer / (double)info.denom; + Platform_nanosecondsPerMachTick = (double)info.numer / (double)info.denom; #else - Platform_timebaseToNS = 1.0; + Platform_nanosecondsPerMachTick = 1.0; #endif - // Determine the number of clock ticks per second + // Determine the number of scheduler clock ticks per second errno = 0; - Platform_clockTicksPerSec = sysconf(_SC_CLK_TCK); + long scheduler_ticks_per_sec = sysconf(_SC_CLK_TCK); - if (errno || Platform_clockTicksPerSec < 1) { + if (errno || scheduler_ticks_per_sec < 1) { CRT_fatalError("Unable to retrieve clock tick rate"); } + + const double nanos_per_sec = 1e9; + Platform_schedulerTicksPerNS = nanos_per_sec / scheduler_ticks_per_sec; +} + +// Converts ticks in the Mach "timebase" to nanoseconds. +// See `mach_timebase_info`, as used to define the `Platform_nanosecondsPerMachTick` constant. +uint64_t Platform_machTicksToNanoseconds(uint64_t mach_ticks) { + return (uint64_t) ((double) mach_ticks * Platform_nanosecondsPerMachTick); +} + +// Converts "scheduler ticks" to nanoseconds. +// See `sysconf(_SC_CLK_TCK)`, as used to define the `Platform_schedulerTicksPerNS` constant. +double Platform_schedulerTicksToNanoseconds(const double scheduler_ticks) { + return scheduler_ticks * Platform_schedulerTicksPerNS; } void Platform_done(void) { diff --git a/darwin/Platform.h b/darwin/Platform.h index b1733a56..b907a8d5 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -27,10 +27,6 @@ in the source distribution for its full text. extern const ProcessField Platform_defaultFields[]; -extern double Platform_timebaseToNS; - -extern long Platform_clockTicksPerSec; - extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals; @@ -39,6 +35,14 @@ extern const MeterClass* const Platform_meterTypes[]; void Platform_init(void); +// Converts ticks in the Mach "timebase" to nanoseconds. +// See `mach_timebase_info`, as used to define the `Platform_nanosecondsPerMachTick` constant. +uint64_t Platform_machTicksToNanoseconds(uint64_t mach_ticks); + +// Converts "scheduler ticks" to nanoseconds. +// See `sysconf(_SC_CLK_TCK)`, as used to define the `Platform_schedulerTicksPerNS` constant. +double Platform_schedulerTicksToNanoseconds(const double scheduler_ticks); + void Platform_done(void); void Platform_setBindings(Htop_Action* keys); -- cgit v1.2.3