PlatformHelpers.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. htop - darwin/PlatformHelpers.c
  3. (C) 2018 Pierre Malhaire, 2020-2021 htop dev team, 2021 Alexander Momchilov
  4. Released under the GNU GPLv2+, see the COPYING file
  5. in the source distribution for its full text.
  6. */
  7. #include "darwin/PlatformHelpers.h"
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include <sys/sysctl.h>
  13. #include "CRT.h"
  14. #ifdef HAVE_MACH_MACH_TIME_H
  15. #include <mach/mach_time.h>
  16. #endif
  17. void Platform_GetKernelVersion(KernelVersion* k) {
  18. static KernelVersion cachedKernelVersion;
  19. if (!cachedKernelVersion.major) {
  20. // just in case it fails someday
  21. cachedKernelVersion = (KernelVersion) { -1, -1, -1 };
  22. char str[256] = {0};
  23. size_t size = sizeof(str);
  24. int ret = sysctlbyname("kern.osrelease", str, &size, NULL, 0);
  25. if (ret == 0) {
  26. sscanf(str, "%hd.%hd.%hd", &cachedKernelVersion.major, &cachedKernelVersion.minor, &cachedKernelVersion.patch);
  27. }
  28. }
  29. memcpy(k, &cachedKernelVersion, sizeof(cachedKernelVersion));
  30. }
  31. int Platform_CompareKernelVersion(KernelVersion v) {
  32. struct KernelVersion actualVersion;
  33. Platform_GetKernelVersion(&actualVersion);
  34. if (actualVersion.major != v.major) {
  35. return actualVersion.major - v.major;
  36. }
  37. if (actualVersion.minor != v.minor) {
  38. return actualVersion.minor - v.minor;
  39. }
  40. if (actualVersion.patch != v.patch) {
  41. return actualVersion.patch - v.patch;
  42. }
  43. return 0;
  44. }
  45. bool Platform_KernelVersionIsBetween(KernelVersion lowerBound, KernelVersion upperBound) {
  46. return 0 <= Platform_CompareKernelVersion(lowerBound)
  47. && Platform_CompareKernelVersion(upperBound) < 0;
  48. }
  49. void Platform_getCPUBrandString(char* cpuBrandString, size_t cpuBrandStringSize) {
  50. if (sysctlbyname("machdep.cpu.brand_string", cpuBrandString, &cpuBrandStringSize, NULL, 0) == -1) {
  51. fprintf(stderr,
  52. "WARN: Unable to determine the CPU brand string.\n"
  53. "errno: %i, %s\n", errno, strerror(errno));
  54. String_safeStrncpy(cpuBrandString, "UNKNOWN!", cpuBrandStringSize);
  55. }
  56. }
  57. // Adapted from https://developer.apple.com/documentation/apple-silicon/about-the-rosetta-translation-environment
  58. bool Platform_isRunningTranslated(void) {
  59. int ret = 0;
  60. size_t size = sizeof(ret);
  61. errno = 0;
  62. if (sysctlbyname("sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
  63. if (errno == ENOENT)
  64. return false;
  65. fprintf(stderr,
  66. "WARN: Could not determine if this process was running in a translation environment like Rosetta 2.\n"
  67. "Assuming that we're not.\n"
  68. "errno: %i, %s\n", errno, strerror(errno));
  69. return false;
  70. }
  71. return ret;
  72. }
  73. double Platform_calculateNanosecondsPerMachTick(void) {
  74. // Check if we can determine the timebase used on this system.
  75. // If the API is unavailable assume we get our timebase in nanoseconds.
  76. #ifndef HAVE_MACH_TIMEBASE_INFO
  77. return 1.0;
  78. #else
  79. mach_timebase_info_data_t info;
  80. /* WORKAROUND for `mach_timebase_info` giving incorrect values on M1 under Rosetta 2.
  81. * rdar://FB9546856 https://openradar.appspot.com/radar?id=5055988478509056
  82. *
  83. * We don't know exactly what feature/attribute of the M1 chip causes this mistake under Rosetta 2.
  84. * Until we have more Apple ARM chips to compare against, the best we can do is special-case
  85. * the "Apple M1" chip specifically when running under Rosetta 2.
  86. */
  87. char cpuBrandString[1024] = "";
  88. Platform_getCPUBrandString(cpuBrandString, sizeof(cpuBrandString));
  89. bool isRunningUnderRosetta2 = Platform_isRunningTranslated();
  90. // Kernel version 20.0.0 is macOS 11.0 (Big Sur)
  91. bool isBuggedVersion = Platform_KernelVersionIsBetween((KernelVersion) {20, 0, 0}, (KernelVersion) {999, 999, 999});
  92. if (isRunningUnderRosetta2 && String_eq(cpuBrandString, "Apple M1") && isBuggedVersion) {
  93. // In this case `mach_timebase_info` provides the wrong value, so we hard-code the correct factor,
  94. // as determined from `mach_timebase_info` when the process running natively.
  95. info = (mach_timebase_info_data_t) { .numer = 125, .denom = 3 };
  96. } else {
  97. // No workarounds needed, use the OS-provided value.
  98. mach_timebase_info(&info);
  99. }
  100. return (double)info.numer / (double)info.denom;
  101. #endif
  102. }