Skip to content

Commit

Permalink
Merge branch 'darwin-running-time' of aestriplex/htop
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Lange authored and Daniel Lange committed Feb 25, 2025
2 parents b4ab345 + 814feeb commit 77132de
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 67 deletions.
2 changes: 0 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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 <mach/vm_statistics.h>]])
], [])
Expand Down
19 changes: 5 additions & 14 deletions darwin/DarwinProcess.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -377,22 +370,20 @@ 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 = 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) {
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;
}
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;
Expand Down
9 changes: 0 additions & 9 deletions darwin/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 0 additions & 4 deletions darwin/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 0 additions & 34 deletions darwin/PlatformHelpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 0 additions & 4 deletions darwin/PlatformHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 77132de

Please sign in to comment.