From 802494f65f321ba78e49685760850da41701b89a Mon Sep 17 00:00:00 2001 From: aestriplex Date: Sun, 23 Feb 2025 20:03:50 +0100 Subject: [PATCH 1/3] darwin: process' running time should not depend on mach ticks Fixes: #1619 --- configure.ac | 2 -- darwin/DarwinProcess.c | 5 ++--- darwin/Platform.c | 9 --------- darwin/Platform.h | 4 ---- darwin/PlatformHelpers.c | 34 ---------------------------------- darwin/PlatformHelpers.h | 4 ---- 6 files changed, 2 insertions(+), 56 deletions(-) diff --git a/configure.ac b/configure.ac index bada27130..c22c371e3 100644 --- a/configure.ac +++ b/configure.ac @@ -375,8 +375,6 @@ AC_CHECK_FUNCS([ \ ]) if test "$my_htop_platform" = darwin; then - AC_CHECK_FUNCS([mach_timebase_info]) - AC_CHECK_FUNCS([host_statistics64], [ AC_CHECK_TYPES([struct vm_statistics64], [], [], [[#include ]]) ], []) diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 991563bce..7d60debc4 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -378,9 +378,8 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessTable uint64_t total_existing_time_ns = proc->stime + proc->utime; - uint64_t user_time_ns = Platform_machTicksToNanoseconds(pti.pti_total_user); - uint64_t system_time_ns = Platform_machTicksToNanoseconds(pti.pti_total_system); - + uint64_t user_time_ns = pti.pti_total_user; + uint64_t system_time_ns = pti.pti_total_system; uint64_t total_current_time_ns = user_time_ns + system_time_ns; if (total_existing_time_ns && 1E-6 < timeIntervalNS) { diff --git a/darwin/Platform.c b/darwin/Platform.c index 78caedf30..84ebc3951 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -148,14 +148,11 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -static double Platform_nanosecondsPerMachTick = 1.0; - static double Platform_nanosecondsPerSchedulerTick = -1; static mach_port_t iokit_port; // the mach port used to initiate communication with IOKit bool Platform_init(void) { - Platform_nanosecondsPerMachTick = Platform_calculateNanosecondsPerMachTick(); // Determine the number of scheduler clock ticks per second errno = 0; @@ -171,12 +168,6 @@ bool Platform_init(void) { return true; } -// 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_nanosecondsPerSchedulerTick` constant. double Platform_schedulerTicksToNanoseconds(const double scheduler_ticks) { diff --git a/darwin/Platform.h b/darwin/Platform.h index f67db8ff4..5e92483a7 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -38,10 +38,6 @@ extern const MeterClass* const Platform_meterTypes[]; bool 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_nanosecondsPerSchedulerTick` constant. double Platform_schedulerTicksToNanoseconds(const double scheduler_ticks); diff --git a/darwin/PlatformHelpers.c b/darwin/PlatformHelpers.c index 03b9bffe1..3e539a2aa 100644 --- a/darwin/PlatformHelpers.c +++ b/darwin/PlatformHelpers.c @@ -86,37 +86,3 @@ bool Platform_isRunningTranslated(void) { } return ret; } - -double Platform_calculateNanosecondsPerMachTick(void) { - // Check if we can determine the timebase used on this system. - // If the API is unavailable assume we get our timebase in nanoseconds. -#ifndef HAVE_MACH_TIMEBASE_INFO - return 1.0; -#else - mach_timebase_info_data_t info; - - /* WORKAROUND for `mach_timebase_info` giving incorrect values on M1 under Rosetta 2. - * rdar://FB9546856 https://openradar.appspot.com/radar?id=5055988478509056 - * - * We don't know exactly what feature/attribute of the M1 chip causes this mistake under Rosetta 2. - * Until we have more Apple ARM chips to compare against, the best we can do is special-case - * the "Apple M1" chip specifically when running under Rosetta 2. - */ - - bool isRunningUnderRosetta2 = Platform_isRunningTranslated(); - - // Kernel version 20.0.0 is macOS 11.0 (Big Sur) - bool isBuggedVersion = Platform_KernelVersionIsBetween((KernelVersion) {20, 0, 0}, (KernelVersion) {999, 999, 999}); - - if (isRunningUnderRosetta2 && isBuggedVersion) { - // In this case `mach_timebase_info` provides the wrong value, so we hard-code the correct factor, - // as determined from `mach_timebase_info` when the process running natively. - info = (mach_timebase_info_data_t) { .numer = 125, .denom = 3 }; - } else { - // No workarounds needed, use the OS-provided value. - mach_timebase_info(&info); - } - - return (double)info.numer / (double)info.denom; -#endif -} diff --git a/darwin/PlatformHelpers.h b/darwin/PlatformHelpers.h index 45aea1a74..7331b4da2 100644 --- a/darwin/PlatformHelpers.h +++ b/darwin/PlatformHelpers.h @@ -29,12 +29,8 @@ int Platform_CompareKernelVersion(KernelVersion v); // lowerBound <= currentVersion < upperBound bool Platform_KernelVersionIsBetween(KernelVersion lowerBound, KernelVersion upperBound); -double Platform_calculateNanosecondsPerMachTick(void); - void Platform_getCPUBrandString(char* cpuBrandString, size_t cpuBrandStringSize); bool Platform_isRunningTranslated(void); -double Platform_calculateNanosecondsPerMachTick(void); - #endif From 9944d5507f9d57f8c03b0e183523fb28eee79005 Mon Sep 17 00:00:00 2001 From: aestriplex Date: Tue, 25 Feb 2025 11:31:40 +0100 Subject: [PATCH 2/3] darwin: fix conversion from nano-seconds to centi-seconds Fixes: #1619 --- darwin/DarwinProcess.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 7d60debc4..0b4df3f6f 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -275,13 +275,6 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro Process_updateCmdline(proc, k->kp_proc.p_comm, 0, strlen(k->kp_proc.p_comm)); } -// Converts nanoseconds to hundredths 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; - const uint64_t nanoseconds_per_second = 1e9; - return nanoseconds / nanoseconds_per_second * centiseconds_per_second; -} - static char* DarwinProcess_getDevname(dev_t dev) { if (dev == NODEV) { return NULL; @@ -391,7 +384,7 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessTable Process_updateCPUFieldWidths(proc->super.percent_cpu); proc->super.state = pti.pti_numrunning > 0 ? RUNNING : SLEEPING; - proc->super.time = nanosecondsToCentiseconds(total_current_time_ns); + proc->super.time = total_current_time_ns / 1e7; proc->super.nlwp = pti.pti_threadnum; proc->super.m_virt = pti.pti_virtual_size / ONE_K; proc->super.m_resident = pti.pti_resident_size / ONE_K; From 814feeba8236f82282485e4e84d66beb16c2fedf Mon Sep 17 00:00:00 2001 From: aestriplex Date: Sun, 23 Feb 2025 21:04:51 +0100 Subject: [PATCH 3/3] darwin: process' percent_cpu should be updated only if the process' running time has changed Fixes: #1619 --- darwin/DarwinProcess.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 0b4df3f6f..9bdd3f64c 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -370,13 +370,12 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessTable const DarwinMachine* dhost = (const DarwinMachine*) proc->super.super.host; uint64_t total_existing_time_ns = proc->stime + proc->utime; - uint64_t user_time_ns = pti.pti_total_user; uint64_t system_time_ns = pti.pti_total_system; uint64_t total_current_time_ns = user_time_ns + system_time_ns; - if (total_existing_time_ns && 1E-6 < timeIntervalNS) { - uint64_t total_time_diff_ns = total_current_time_ns - total_existing_time_ns; + if (total_existing_time_ns < total_current_time_ns) { + const uint64_t total_time_diff_ns = total_current_time_ns - total_existing_time_ns; proc->super.percent_cpu = ((double)total_time_diff_ns / timeIntervalNS) * 100.0; } else { proc->super.percent_cpu = 0.0;