From 30ce3b4c264c51c98f280e88e23792ff7deb2317 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Fri, 15 Oct 2021 09:03:04 +0200 Subject: New upstream version 3.1.1 --- darwin/DarwinProcess.c | 4 +- darwin/DarwinProcess.h | 3 +- darwin/DarwinProcessList.c | 46 ++--------------- darwin/DarwinProcessList.h | 2 +- darwin/Platform.c | 14 ++--- darwin/Platform.h | 2 +- darwin/PlatformHelpers.c | 125 +++++++++++++++++++++++++++++++++++++++++++++ darwin/PlatformHelpers.h | 40 +++++++++++++++ darwin/ProcessField.h | 2 +- 9 files changed, 179 insertions(+), 59 deletions(-) create mode 100644 darwin/PlatformHelpers.c create mode 100644 darwin/PlatformHelpers.h (limited to 'darwin') diff --git a/darwin/DarwinProcess.c b/darwin/DarwinProcess.c index 20e91f7..62b6651 100644 --- a/darwin/DarwinProcess.c +++ b/darwin/DarwinProcess.c @@ -1,7 +1,7 @@ /* htop - DarwinProcess.c (C) 2015 Hisham H. Muhammad -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ @@ -145,7 +145,7 @@ static void DarwinProcess_updateCmdLine(const struct kinfo_proc* k, Process* pro } /* Allocate space for the arguments. */ - procargs = (char*)xMalloc(argmax); + procargs = (char*)malloc(argmax); if ( procargs == NULL ) { goto ERROR_A; } diff --git a/darwin/DarwinProcess.h b/darwin/DarwinProcess.h index 0a49eea..b4b86fd 100644 --- a/darwin/DarwinProcess.h +++ b/darwin/DarwinProcess.h @@ -3,7 +3,7 @@ /* htop - DarwinProcess.h (C) 2015 Hisham H. Muhammad -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ @@ -12,6 +12,7 @@ in the source distribution for its full text. #include "Settings.h" #include "darwin/DarwinProcessList.h" + typedef struct DarwinProcess_ { Process super; diff --git a/darwin/DarwinProcessList.c b/darwin/DarwinProcessList.c index 7dd86ff..bd7821b 100644 --- a/darwin/DarwinProcessList.c +++ b/darwin/DarwinProcessList.c @@ -1,7 +1,7 @@ /* htop - DarwinProcessList.c (C) 2014 Hisham H. Muhammad -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ @@ -22,51 +22,11 @@ in the source distribution for its full text. #include "ProcessList.h" #include "darwin/DarwinProcess.h" #include "darwin/Platform.h" +#include "darwin/PlatformHelpers.h" #include "generic/openzfs_sysctl.h" #include "zfs/ZfsArcStats.h" -struct kern { - short int version[3]; -}; - -static void GetKernelVersion(struct kern* k) { - static short int version_[3] = {0}; - if (!version_[0]) { - // just in case it fails someday - version_[0] = version_[1] = version_[2] = -1; - char str[256] = {0}; - size_t size = sizeof(str); - int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0); - if (ret == 0) { - sscanf(str, "%hd.%hd.%hd", &version_[0], &version_[1], &version_[2]); - } - } - memcpy(k->version, version_, sizeof(version_)); -} - -/* compare the given os version with the one installed returns: -0 if equals the installed version -positive value if less than the installed version -negative value if more than the installed version -*/ -static int CompareKernelVersion(short int major, short int minor, short int component) { - struct kern k; - GetKernelVersion(&k); - - if (k.version[0] != major) { - return k.version[0] - major; - } - if (k.version[1] != minor) { - return k.version[1] - minor; - } - if (k.version[2] != component) { - return k.version[2] - component; - } - - return 0; -} - static void ProcessList_getHostInfo(host_basic_info_data_t* p) { mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT; @@ -216,7 +176,7 @@ void ProcessList_goThroughEntries(ProcessList* super, bool pauseProcessUpdate) { } // Disabled for High Sierra due to bug in macOS High Sierra - bool isScanThreadSupported = ! ( CompareKernelVersion(17, 0, 0) >= 0 && CompareKernelVersion(17, 5, 0) < 0); + bool isScanThreadSupported = !Platform_KernelVersionIsBetween((KernelVersion) {17, 0, 0}, (KernelVersion) {17, 5, 0}); if (isScanThreadSupported) { DarwinProcess_scanThreads(proc); diff --git a/darwin/DarwinProcessList.h b/darwin/DarwinProcessList.h index af1140b..393e656 100644 --- a/darwin/DarwinProcessList.h +++ b/darwin/DarwinProcessList.h @@ -3,7 +3,7 @@ /* htop - DarwinProcessList.h (C) 2014 Hisham H. Muhammad -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ diff --git a/darwin/Platform.c b/darwin/Platform.c index 93262bb..3f596a3 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -2,7 +2,7 @@ htop - darwin/Platform.c (C) 2014 Hisham H. Muhammad (C) 2015 David C. Hunt -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ @@ -35,6 +35,7 @@ in the source distribution for its full text. #include "TasksMeter.h" #include "UptimeMeter.h" #include "darwin/DarwinProcessList.h" +#include "darwin/PlatformHelpers.h" #include "zfs/ZfsArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h" @@ -42,6 +43,7 @@ in the source distribution for its full text. #include #include #endif + #ifdef HAVE_MACH_MACH_TIME_H #include #endif @@ -125,15 +127,7 @@ static double Platform_nanosecondsPerMachTick = 1.0; static double Platform_nanosecondsPerSchedulerTick = -1; void Platform_init(void) { - // 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_nanosecondsPerMachTick = (double)info.numer / (double)info.denom; -#else - Platform_nanosecondsPerMachTick = 1.0; -#endif + Platform_nanosecondsPerMachTick = Platform_calculateNanosecondsPerMachTick(); // Determine the number of scheduler clock ticks per second errno = 0; diff --git a/darwin/Platform.h b/darwin/Platform.h index e7647db..c03a9b4 100644 --- a/darwin/Platform.h +++ b/darwin/Platform.h @@ -4,7 +4,7 @@ htop - darwin/Platform.h (C) 2014 Hisham H. Muhammad (C) 2015 David C. Hunt -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ diff --git a/darwin/PlatformHelpers.c b/darwin/PlatformHelpers.c new file mode 100644 index 0000000..bde9068 --- /dev/null +++ b/darwin/PlatformHelpers.c @@ -0,0 +1,125 @@ +/* +htop - darwin/PlatformHelpers.c +(C) 2018 Pierre Malhaire, 2020-2021 htop dev team, 2021 Alexander Momchilov +Released under the GNU GPLv2+, see the COPYING file +in the source distribution for its full text. +*/ + +#include "darwin/PlatformHelpers.h" + +#include +#include +#include +#include +#include + +#include "CRT.h" + +#ifdef HAVE_MACH_MACH_TIME_H +#include +#endif + + +void Platform_GetKernelVersion(KernelVersion* k) { + static KernelVersion cachedKernelVersion; + + if (!cachedKernelVersion.major) { + // just in case it fails someday + cachedKernelVersion = (KernelVersion) { -1, -1, -1 }; + char str[256] = {0}; + size_t size = sizeof(str); + int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0); + if (ret == 0) { + sscanf(str, "%hd.%hd.%hd", &cachedKernelVersion.major, &cachedKernelVersion.minor, &cachedKernelVersion.patch); + } + } + memcpy(k, &cachedKernelVersion, sizeof(cachedKernelVersion)); +} + +int Platform_CompareKernelVersion(KernelVersion v) { + struct KernelVersion actualVersion; + Platform_GetKernelVersion(&actualVersion); + + if (actualVersion.major != v.major) { + return actualVersion.major - v.major; + } + if (actualVersion.minor != v.minor) { + return actualVersion.minor - v.minor; + } + if (actualVersion.patch != v.patch) { + return actualVersion.patch - v.patch; + } + + return 0; +} + +bool Platform_KernelVersionIsBetween(KernelVersion lowerBound, KernelVersion upperBound) { + return 0 <= Platform_CompareKernelVersion(lowerBound) + && Platform_CompareKernelVersion(upperBound) < 0; +} + +void Platform_getCPUBrandString(char *cpuBrandString, size_t cpuBrandStringSize) { + if (sysctlbyname("machdep.cpu.brand_string", cpuBrandString, &cpuBrandStringSize, NULL, 0) == -1) { + fprintf(stderr, + "WARN: Unable to determine the CPU brand string.\n" + "errno: %i, %s\n", errno, strerror(errno)); + + String_safeStrncpy(cpuBrandString, "UNKNOWN!", cpuBrandStringSize); + } +} + +// Adapted from https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment +bool Platform_isRunningTranslated() { + int ret = 0; + size_t size = sizeof(ret); + errno = 0; + if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) { + if (errno == ENOENT) return false; + + fprintf(stderr, + "WARN: Could not determine if this process was running in a translation environment like Rosetta 2.\n" + "Assuming that we're not.\n" + "errno: %i, %s\n", errno, strerror(errno)); + + return false; + } + return ret; +} + +double Platform_calculateNanosecondsPerMachTick() { + // 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. + */ + + size_t cpuBrandStringSize = 1024; + char cpuBrandString[cpuBrandStringSize]; + Platform_getCPUBrandString(cpuBrandString, cpuBrandStringSize); + + 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 && String_eq(cpuBrandString, "Apple M1") && 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 new file mode 100644 index 0000000..f1af1c0 --- /dev/null +++ b/darwin/PlatformHelpers.h @@ -0,0 +1,40 @@ +#ifndef HEADER_PlatformHelpers +#define HEADER_PlatformHelpers +/* +htop - darwin/PlatformHelpers.h +(C) 2018 Pierre Malhaire, 2020-2021 htop dev team, 2021 Alexander Momchilov +Released under the GNU GPLv2, see the COPYING file +in the source distribution for its full text. +*/ + +#include +#include + + +typedef struct KernelVersion { + short int major; + short int minor; + short int patch; +} KernelVersion; + +void Platform_GetKernelVersion(KernelVersion* k); + +/* compare the given os version with the one installed returns: +0 if equals the installed version +positive value if less than the installed version +negative value if more than the installed version +*/ +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 diff --git a/darwin/ProcessField.h b/darwin/ProcessField.h index 25dbb45..05fbc3b 100644 --- a/darwin/ProcessField.h +++ b/darwin/ProcessField.h @@ -3,7 +3,7 @@ /* htop - darwin/ProcessField.h (C) 2020 htop dev team -Released under the GNU GPLv2, see the COPYING file +Released under the GNU GPLv2+, see the COPYING file in the source distribution for its full text. */ -- cgit v1.2.3