From 69f439eff387a6ecb52734e400b297a3c85f2285 Mon Sep 17 00:00:00 2001 From: Daniel Lange Date: Tue, 21 Sep 2021 08:35:19 +0200 Subject: New upstream version 3.1.0 --- darwin/Platform.c | 178 +++++++++++++++++++++++++++++++----------------------- 1 file changed, 104 insertions(+), 74 deletions(-) (limited to 'darwin/Platform.c') diff --git a/darwin/Platform.c b/darwin/Platform.c index a4ed464..93262bb 100644 --- a/darwin/Platform.c +++ b/darwin/Platform.c @@ -8,7 +8,7 @@ in the source distribution for its full text. #include "config.h" // IWYU pragma: keep -#include "Platform.h" +#include "darwin/Platform.h" #include #include @@ -22,20 +22,26 @@ in the source distribution for its full text. #include "ClockMeter.h" #include "CPUMeter.h" #include "CRT.h" -#include "DarwinProcessList.h" #include "DateMeter.h" #include "DateTimeMeter.h" #include "HostnameMeter.h" #include "LoadAverageMeter.h" #include "Macros.h" #include "MemoryMeter.h" +#include "MemorySwapMeter.h" #include "ProcessLocksScreen.h" #include "SwapMeter.h" +#include "SysArchMeter.h" #include "TasksMeter.h" #include "UptimeMeter.h" +#include "darwin/DarwinProcessList.h" #include "zfs/ZfsArcMeter.h" #include "zfs/ZfsCompressedArcMeter.h" +#ifdef HAVE_HOST_GET_CLOCK_SERVICE +#include +#include +#endif #ifdef HAVE_MACH_MACH_TIME_H #include #endif @@ -90,9 +96,11 @@ const MeterClass* const Platform_meterTypes[] = { &LoadMeter_class, &MemoryMeter_class, &SwapMeter_class, + &MemorySwapMeter_class, &TasksMeter_class, &BatteryMeter_class, &HostnameMeter_class, + &SysArchMeter_class, &UptimeMeter_class, &AllCPUsMeter_class, &AllCPUs2Meter_class, @@ -112,9 +120,9 @@ const MeterClass* const Platform_meterTypes[] = { NULL }; -double Platform_timebaseToNS = 1.0; +static double Platform_nanosecondsPerMachTick = 1.0; -long Platform_clockTicksPerSec = -1; +static double Platform_nanosecondsPerSchedulerTick = -1; void Platform_init(void) { // Check if we can determine the timebase used on this system. @@ -122,18 +130,33 @@ void Platform_init(void) { #ifdef HAVE_MACH_TIMEBASE_INFO mach_timebase_info_data_t info; mach_timebase_info(&info); - Platform_timebaseToNS = (double)info.numer / (double)info.denom; + Platform_nanosecondsPerMachTick = (double)info.numer / (double)info.denom; #else - Platform_timebaseToNS = 1.0; + Platform_nanosecondsPerMachTick = 1.0; #endif - // Determine the number of clock ticks per second + // Determine the number of scheduler clock ticks per second errno = 0; - Platform_clockTicksPerSec = sysconf(_SC_CLK_TCK); + long scheduler_ticks_per_sec = sysconf(_SC_CLK_TCK); - if (errno || Platform_clockTicksPerSec < 1) { + if (errno || scheduler_ticks_per_sec < 1) { CRT_fatalError("Unable to retrieve clock tick rate"); } + + const double nanos_per_sec = 1e9; + Platform_nanosecondsPerSchedulerTick = nanos_per_sec / scheduler_ticks_per_sec; +} + +// 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) { + return scheduler_ticks * Platform_nanosecondsPerSchedulerTick; } void Platform_done(void) { @@ -180,24 +203,24 @@ int Platform_getMaxPid() { static double Platform_setCPUAverageValues(Meter* mtr) { const ProcessList* dpl = mtr->pl; - int cpus = dpl->cpuCount; + unsigned int activeCPUs = dpl->activeCPUs; double sumNice = 0.0; double sumNormal = 0.0; double sumKernel = 0.0; double sumPercent = 0.0; - for (int i = 1; i <= cpus; i++) { + for (unsigned int i = 1; i <= dpl->existingCPUs; i++) { sumPercent += Platform_setCPUValues(mtr, i); sumNice += mtr->values[CPU_METER_NICE]; sumNormal += mtr->values[CPU_METER_NORMAL]; sumKernel += mtr->values[CPU_METER_KERNEL]; } - mtr->values[CPU_METER_NICE] = sumNice / cpus; - mtr->values[CPU_METER_NORMAL] = sumNormal / cpus; - mtr->values[CPU_METER_KERNEL] = sumKernel / cpus; - return sumPercent / cpus; + mtr->values[CPU_METER_NICE] = sumNice / activeCPUs; + mtr->values[CPU_METER_NORMAL] = sumNormal / activeCPUs; + mtr->values[CPU_METER_KERNEL] = sumKernel / activeCPUs; + return sumPercent / activeCPUs; } -double Platform_setCPUValues(Meter* mtr, int cpu) { +double Platform_setCPUValues(Meter* mtr, unsigned int cpu) { if (cpu == 0) { return Platform_setCPUAverageValues(mtr); @@ -239,7 +262,9 @@ void Platform_setMemoryValues(Meter* mtr) { mtr->total = dpl->host_info.max_mem / 1024; mtr->values[0] = (double)(vm->active_count + vm->wire_count) * page_K; mtr->values[1] = (double)vm->purgeable_count * page_K; - mtr->values[2] = (double)vm->inactive_count * page_K; + // mtr->values[2] = "shared memory, like tmpfs and shm" + mtr->values[3] = (double)vm->inactive_count * page_K; + // mtr->values[4] = "available memory" } void Platform_setSwapValues(Meter* mtr) { @@ -316,14 +341,14 @@ char* Platform_getProcessEnv(pid_t pid) { } char* Platform_getInodeFilename(pid_t pid, ino_t inode) { - (void)pid; - (void)inode; - return NULL; + (void)pid; + (void)inode; + return NULL; } FileLocks_ProcessData* Platform_getProcessLocks(pid_t pid) { - (void)pid; - return NULL; + (void)pid; + return NULL; } bool Platform_getDiskIO(DiskIOData* data) { @@ -332,78 +357,83 @@ bool Platform_getDiskIO(DiskIOData* data) { return false; } -bool Platform_getNetworkIO(unsigned long int* bytesReceived, - unsigned long int* packetsReceived, - unsigned long int* bytesTransmitted, - unsigned long int* packetsTransmitted) { +bool Platform_getNetworkIO(NetworkIOData* data) { // TODO - *bytesReceived = 0; - *packetsReceived = 0; - *bytesTransmitted = 0; - *packetsTransmitted = 0; + (void)data; return false; } void Platform_getBattery(double* percent, ACPresence* isOnAC) { - CFTypeRef power_sources = IOPSCopyPowerSourcesInfo(); - *percent = NAN; *isOnAC = AC_ERROR; - if (NULL == power_sources) - return; + CFArrayRef list = NULL; - CFArrayRef list = IOPSCopyPowerSourcesList(power_sources); - CFDictionaryRef battery = NULL; - int len; - - if (NULL == list) { - CFRelease(power_sources); + CFTypeRef power_sources = IOPSCopyPowerSourcesInfo(); + if (!power_sources) + goto cleanup; - return; - } + list = IOPSCopyPowerSourcesList(power_sources); + if (!list) + goto cleanup; - len = CFArrayGetCount(list); + double cap_current = 0.0; + double cap_max = 0.0; /* Get the battery */ - for (int i = 0; i < len && battery == NULL; ++i) { - CFDictionaryRef candidate = IOPSGetPowerSourceDescription(power_sources, - CFArrayGetValueAtIndex(list, i)); /* GET rule */ - CFStringRef type; - - if (NULL != candidate) { - type = (CFStringRef) CFDictionaryGetValue(candidate, - CFSTR(kIOPSTransportTypeKey)); /* GET rule */ - - if (kCFCompareEqualTo == CFStringCompare(type, CFSTR(kIOPSInternalType), 0)) { - CFRetain(candidate); - battery = candidate; - } - } - } + for (int i = 0, len = CFArrayGetCount(list); i < len; ++i) { + CFDictionaryRef power_source = IOPSGetPowerSourceDescription(power_sources, CFArrayGetValueAtIndex(list, i)); /* GET rule */ + + if (!power_source) + continue; + + CFStringRef power_type = CFDictionaryGetValue(power_source, CFSTR(kIOPSTransportTypeKey)); /* GET rule */ + + if (kCFCompareEqualTo != CFStringCompare(power_type, CFSTR(kIOPSInternalType), 0)) + continue; - if (NULL != battery) { /* Determine the AC state */ - CFStringRef power_state = CFDictionaryGetValue(battery, CFSTR(kIOPSPowerSourceStateKey)); + CFStringRef power_state = CFDictionaryGetValue(power_source, CFSTR(kIOPSPowerSourceStateKey)); - *isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0)) - ? AC_PRESENT - : AC_ABSENT; + if (*isOnAC != AC_PRESENT) + *isOnAC = (kCFCompareEqualTo == CFStringCompare(power_state, CFSTR(kIOPSACPowerValue), 0)) ? AC_PRESENT : AC_ABSENT; /* Get the percentage remaining */ - double current; - double max; + double tmp; + CFNumberGetValue(CFDictionaryGetValue(power_source, CFSTR(kIOPSCurrentCapacityKey)), kCFNumberDoubleType, &tmp); + cap_current += tmp; + CFNumberGetValue(CFDictionaryGetValue(power_source, CFSTR(kIOPSMaxCapacityKey)), kCFNumberDoubleType, &tmp); + cap_max += tmp; + } - CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSCurrentCapacityKey)), - kCFNumberDoubleType, ¤t); - CFNumberGetValue(CFDictionaryGetValue(battery, CFSTR(kIOPSMaxCapacityKey)), - kCFNumberDoubleType, &max); + if (cap_max > 0.0) + *percent = 100.0 * cap_current / cap_max; - *percent = (current * 100.0) / max; +cleanup: + if (list) + CFRelease(list); - CFRelease(battery); - } + if (power_sources) + CFRelease(power_sources); +} + +void Platform_gettime_monotonic(uint64_t* msec) { + +#ifdef HAVE_HOST_GET_CLOCK_SERVICE + + clock_serv_t cclock; + mach_timespec_t mts; + + host_get_clock_service(mach_host_self(), SYSTEM_CLOCK, &cclock); + clock_get_time(cclock, &mts); + mach_port_deallocate(mach_task_self(), cclock); + + *msec = ((uint64_t)mts.tv_sec * 1000) + ((uint64_t)mts.tv_nsec / 1000000); + +#else + + Generic_gettime_monotonic(msec); + +#endif - CFRelease(list); - CFRelease(power_sources); } -- cgit v1.2.3