Extract Darwin "PlatformHelpers"

This commit is contained in:
Alexander Momchilov 2021-08-22 12:30:08 -04:00
parent 754c0d6bb9
commit dadcb87ad0
6 changed files with 107 additions and 51 deletions

View File

@ -307,6 +307,7 @@ darwin_platform_headers = \
darwin/DarwinProcess.h \ darwin/DarwinProcess.h \
darwin/DarwinProcessList.h \ darwin/DarwinProcessList.h \
darwin/Platform.h \ darwin/Platform.h \
darwin/PlatformHelpers.h \
darwin/ProcessField.h \ darwin/ProcessField.h \
generic/gettime.h \ generic/gettime.h \
generic/hostname.h \ generic/hostname.h \
@ -318,6 +319,7 @@ darwin_platform_headers = \
darwin_platform_sources = \ darwin_platform_sources = \
darwin/Platform.c \ darwin/Platform.c \
darwin/PlatformHelpers.c \
darwin/DarwinProcess.c \ darwin/DarwinProcess.c \
darwin/DarwinProcessList.c \ darwin/DarwinProcessList.c \
generic/gettime.c \ generic/gettime.c \

View File

@ -12,6 +12,7 @@ in the source distribution for its full text.
#include "Settings.h" #include "Settings.h"
#include "darwin/DarwinProcessList.h" #include "darwin/DarwinProcessList.h"
typedef struct DarwinProcess_ { typedef struct DarwinProcess_ {
Process super; Process super;

View File

@ -22,51 +22,11 @@ in the source distribution for its full text.
#include "ProcessList.h" #include "ProcessList.h"
#include "darwin/DarwinProcess.h" #include "darwin/DarwinProcess.h"
#include "darwin/Platform.h" #include "darwin/Platform.h"
#include "darwin/PlatformHelpers.h"
#include "generic/openzfs_sysctl.h" #include "generic/openzfs_sysctl.h"
#include "zfs/ZfsArcStats.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) { static void ProcessList_getHostInfo(host_basic_info_data_t* p) {
mach_msg_type_number_t info_size = HOST_BASIC_INFO_COUNT; 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 // 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_CompareKernelVersion(17, 0, 0) >= 0 && Platform_CompareKernelVersion(17, 5, 0) < 0);
if (isScanThreadSupported) { if (isScanThreadSupported) {
DarwinProcess_scanThreads(proc); DarwinProcess_scanThreads(proc);

View File

@ -35,6 +35,7 @@ in the source distribution for its full text.
#include "TasksMeter.h" #include "TasksMeter.h"
#include "UptimeMeter.h" #include "UptimeMeter.h"
#include "darwin/DarwinProcessList.h" #include "darwin/DarwinProcessList.h"
#include "darwin/PlatformHelpers.h"
#include "zfs/ZfsArcMeter.h" #include "zfs/ZfsArcMeter.h"
#include "zfs/ZfsCompressedArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h"
@ -42,6 +43,7 @@ in the source distribution for its full text.
#include <mach/clock.h> #include <mach/clock.h>
#include <mach/mach.h> #include <mach/mach.h>
#endif #endif
#ifdef HAVE_MACH_MACH_TIME_H #ifdef HAVE_MACH_MACH_TIME_H
#include <mach/mach_time.h> #include <mach/mach_time.h>
#endif #endif
@ -125,15 +127,7 @@ static double Platform_nanosecondsPerMachTick = 1.0;
static double Platform_nanosecondsPerSchedulerTick = -1; static double Platform_nanosecondsPerSchedulerTick = -1;
void Platform_init(void) { void Platform_init(void) {
// Check if we can determine the timebase used on this system. Platform_nanosecondsPerMachTick = Platform_calculateNanosecondsPerMachTick();
// 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
// Determine the number of scheduler clock ticks per second // Determine the number of scheduler clock ticks per second
errno = 0; errno = 0;

70
darwin/PlatformHelpers.c Normal file
View File

@ -0,0 +1,70 @@
/*
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 <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <sys/sysctl.h>
#include "CRT.h"
#ifdef HAVE_MACH_MACH_TIME_H
#include <mach/mach_time.h>
#endif
void Platform_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
*/
int Platform_CompareKernelVersion(short int major, short int minor, short int component) {
struct kern k;
Platform_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;
}
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.
#ifdef HAVE_MACH_TIMEBASE_INFO
mach_timebase_info_data_t info;
mach_timebase_info(&info);
return (double)info.numer / (double)info.denom;
#else
return 1.0;
#endif
}

29
darwin/PlatformHelpers.h Normal file
View File

@ -0,0 +1,29 @@
#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 <stdbool.h>
#include <sys/types.h>
struct kern {
short int version[3];
};
void Platform_GetKernelVersion(struct kern* 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(short int major, short int minor, short int component);
double Platform_calculateNanosecondsPerMachTick(void);
#endif