From e65cdf947cba77e143a31981295062f1aef6d494 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sat, 12 Dec 2020 20:41:19 +0100 Subject: [PATCH 1/2] Sort include in Darwin platform headers --- darwin/Platform.c | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/darwin/Platform.c b/darwin/Platform.c index e24b67bc..d22ba579 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -7,21 +7,6 @@ in the source distribution for its full text. */ #include "Platform.h" -#include "Macros.h" -#include "CPUMeter.h" -#include "MemoryMeter.h" -#include "SwapMeter.h" -#include "TasksMeter.h" -#include "LoadAverageMeter.h" -#include "ClockMeter.h" -#include "DateMeter.h" -#include "DateTimeMeter.h" -#include "HostnameMeter.h" -#include "ProcessLocksScreen.h" -#include "UptimeMeter.h" -#include "zfs/ZfsArcMeter.h" -#include "zfs/ZfsCompressedArcMeter.h" -#include "DarwinProcessList.h" #include #include @@ -31,6 +16,23 @@ in the source distribution for its full text. #include #include +#include "ClockMeter.h" +#include "CPUMeter.h" +#include "DarwinProcessList.h" +#include "DateMeter.h" +#include "DateTimeMeter.h" +#include "HostnameMeter.h" +#include "LoadAverageMeter.h" +#include "Macros.h" +#include "MemoryMeter.h" +#include "ProcessLocksScreen.h" +#include "SwapMeter.h" +#include "TasksMeter.h" +#include "UptimeMeter.h" +#include "zfs/ZfsArcMeter.h" +#include "zfs/ZfsCompressedArcMeter.h" + + ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; const SignalItem Platform_signals[] = { From f32f0188cd140f965c5e02f7fb875116522c4b41 Mon Sep 17 00:00:00 2001 From: Benny Baumann Date: Sat, 12 Dec 2020 20:43:08 +0100 Subject: [PATCH 2/2] Correct timebase for non-x86 CPUs on Darwin Fixes: #368 --- configure.ac | 5 +++++ darwin/DarwinProcess.c | 3 ++- darwin/Platform.c | 18 +++++++++++++++++- darwin/Platform.h | 3 +++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 0a1f430e..a5b7093a 100644 --- a/configure.ac +++ b/configure.ac @@ -240,6 +240,11 @@ else AC_SEARCH_LIBS([keypad], [tinfo]) fi +if test "$my_htop_platform" = "darwin"; then + AC_CHECK_HEADERS([mach/mach_time.h]) + AC_CHECK_FUNCS([mach_timebase_info]) +fi + if test "$my_htop_platform" = "freebsd"; then AC_CHECK_LIB([kvm], [kvm_open], [], [missing_libraries="$missing_libraries libkvm"]) fi diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 904df0bd..8a5be389 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -14,6 +14,7 @@ in the source distribution for its full text. #include #include "CRT.h" +#include "Platform.h" #include "Process.h" @@ -248,7 +249,7 @@ void DarwinProcess_setFromLibprocPidinfo(DarwinProcess* proc, DarwinProcessList* uint64_t diff = (pti.pti_total_system - proc->stime) + (pti.pti_total_user - proc->utime); - proc->super.percent_cpu = (double)diff * (double)dpl->super.cpuCount + proc->super.percent_cpu = (double)diff * (double)dpl->super.cpuCount * Platform_timebaseToNS / ((double)dpl->global_diff * 100000.0); // fprintf(stderr, "%f %llu %llu %llu %llu %llu\n", proc->super.percent_cpu, diff --git a/darwin/Platform.c b/darwin/Platform.c index d22ba579..69cd32a4 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -6,6 +6,8 @@ Released under the GNU GPLv2, see the COPYING file in the source distribution for its full text. */ +#include "config.h" // IWYU pragma: keep + #include "Platform.h" #include @@ -32,6 +34,10 @@ in the source distribution for its full text. #include "zfs/ZfsArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h" +#ifdef HAVE_MACH_MACH_TIME_H +#include +#endif + ProcessField Platform_defaultFields[] = { PID, USER, PRIORITY, NICE, M_VIRT, M_RESIDENT, STATE, PERCENT_CPU, PERCENT_MEM, TIME, COMM, 0 }; @@ -135,8 +141,18 @@ const MeterClass* const Platform_meterTypes[] = { int Platform_numberOfFields = 100; +double Platform_timebaseToNS = 1.0; + void Platform_init(void) { - /* no platform-specific setup needed */ + // Check if we can determine the timebase used on this system. + // If the API is unavailable assume we get our timebase in nanoseconds. +#ifdef HAVE_MACH_TIMEBASE_INFO + mach_timebase_info_data_t info; + mach_timebase_info(&info); + Platform_timebaseToNS = (double)info.numer / (double)info.denom; +#else + Platform_timebaseToNS = 1.0; +#endif } void Platform_done(void) { diff --git a/darwin/Platform.h b/darwin/Platform.h index e1f83553..6213164c 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -19,12 +19,15 @@ in the source distribution for its full text. #include "ProcessLocksScreen.h" #include "SignalsPanel.h" + extern ProcessFieldData Process_fields[]; extern ProcessField Platform_defaultFields[]; extern int Platform_numberOfFields; +extern double Platform_timebaseToNS; + extern const SignalItem Platform_signals[]; extern const unsigned int Platform_numberOfSignals;