Skip to content

Commit

Permalink
Add GPUMeter code for Darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
aestriplex committed Feb 17, 2025
1 parent 9fcbecd commit 09378df
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 36 deletions.
15 changes: 7 additions & 8 deletions GPUMeter.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ in the source distribution for its full text.


static size_t activeMeters;
const int GPUMeter_attributes[] = {
static const int GPUMeter_attributes[] = {
GPU_ENGINE_1,
GPU_ENGINE_2,
GPU_ENGINE_3,
Expand Down Expand Up @@ -73,21 +73,20 @@ static int humanTimeUnit(char* buffer, size_t size, unsigned long long int value
}

static void GPUMeter_updateValues(Meter* this) {
const Machine* host = this->host;
char* buffer = this->txtBuffer;
size_t size = sizeof(this->txtBuffer);

double totalUsage = Machine_updateGpuUsage(this->host);

if (!isNonnegative(totalUsage)) {
if (!isNonnegative(host->totalGPUUsage)) {
this->values[0] = 0;
int written = xSnprintf(buffer, size, "N/A");
METER_BUFFER_CHECK(buffer, size, written);
return;
return;
}

int written = xSnprintf(buffer, size, "%.1f", totalUsage);
METER_BUFFER_CHECK(buffer, size, written);
METER_BUFFER_APPEND_CHR(buffer, size, '%');
int written = xSnprintf(buffer, size, "%.1f", host->totalGPUUsage);
METER_BUFFER_CHECK(buffer, size, written);
METER_BUFFER_APPEND_CHR(buffer, size, '%');
}

static void GPUMeter_display(const Object* cast, RichString* out) {
Expand Down
2 changes: 1 addition & 1 deletion Machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,6 @@ void Machine_scan(Machine* this);

void Machine_scanTables(Machine* this);

double Machine_updateGpuUsage(Machine* this);
void Machine_scanGPUUsage(Machine* super);

#endif
50 changes: 46 additions & 4 deletions darwin/DarwinMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ in the source distribution for its full text.
#include <unistd.h>
#include <sys/mman.h>
#include <sys/sysctl.h>
#include <CoreFoundation/CoreFoundation.h>
#include <IOKit/IOKitLib.h>

#include "CRT.h"
#include "Machine.h"
Expand Down Expand Up @@ -84,6 +86,7 @@ void Machine_scan(Machine* super) {
DarwinMachine_allocateCPULoadInfo(&host->curr_load);
DarwinMachine_getVMStats(host);
openzfs_sysctl_updateArcStats(&host->zfs);
Machine_scanGPUUsage(super);
}

Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
Expand All @@ -105,12 +108,23 @@ Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
openzfs_sysctl_init(&this->zfs);
openzfs_sysctl_updateArcStats(&this->zfs);

this->GPUService = IOServiceGetMatchingService(kIOMainPortDefault, IOServiceMatching("IOGPU"));
if(!this->GPUService) {
CRT_debug("Cannot create macOS GPUService");
}

/* Initialize GPU metric read-out */
super->totalGPUUsage = NAN;
super->totalGPUTimeDiff = (unsigned long long)-1;

return super;
}

void Machine_delete(Machine* super) {
DarwinMachine* this = (DarwinMachine*) super;

IOObjectRelease(this->GPUService);

DarwinMachine_freeCPULoadInfo(&this->prev_load);

Machine_done(super);
Expand All @@ -126,8 +140,36 @@ bool Machine_isCPUonline(const Machine* host, unsigned int id) {
return true;
}

double Machine_updateGpuUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
void Machine_scanGPUUsage(Machine* super) {
DarwinMachine* dhost = (DarwinMachine *)super;

if (!dhost->GPUService) {
return;
}

CFMutableDictionaryRef properties = NULL;
kern_return_t ret = IORegistryEntryCreateCFProperties(dhost->GPUService, &properties, kCFAllocatorDefault, kNilOptions);
if (ret != KERN_SUCCESS) {
return;
}

CFDictionaryRef perfStats = CFDictionaryGetValue(properties, CFSTR("PerformanceStatistics"));

if (!perfStats) {
goto cleanup;
}

assert(CFGetTypeID(perfStats) == CFDictionaryGetTypeID());

CFNumberRef deviceUtil = CFDictionaryGetValue(perfStats, CFSTR("Device Utilization %"));
if (!deviceUtil) {
goto cleanup;
}

int device = NAN;
CFNumberGetValue(deviceUtil, kCFNumberIntType, &device);
super->totalGPUUsage = (double)device;

cleanup:
CFRelease(properties);
}
3 changes: 3 additions & 0 deletions darwin/DarwinMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Released under the GNU GPLv2+, see the COPYING file
in the source distribution for its full text.
*/

#include <IOKit/IOKitLib.h>
#include <mach/mach_host.h>
#include <sys/sysctl.h>

Expand All @@ -26,6 +27,8 @@ typedef struct DarwinMachine_ {
processor_cpu_load_info_t prev_load;
processor_cpu_load_info_t curr_load;

io_service_t GPUService;

ZfsArcStats zfs;
} DarwinMachine;

Expand Down
2 changes: 2 additions & 0 deletions darwin/Platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ in the source distribution for its full text.
#include "DateMeter.h"
#include "DateTimeMeter.h"
#include "FileDescriptorMeter.h"
#include "GPUMeter.h"
#include "HostnameMeter.h"
#include "LoadAverageMeter.h"
#include "Macros.h"
Expand Down Expand Up @@ -144,6 +145,7 @@ const MeterClass* const Platform_meterTypes[] = {
&DiskIOMeter_class,
&NetworkIOMeter_class,
&FileDescriptorMeter_class,
&GPUMeter_class,
&BlankMeter_class,
NULL
};
Expand Down
6 changes: 3 additions & 3 deletions dragonflybsd/DragonFlyBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ void Machine_scan(Machine* super) {
DragonFlyBSDMachine_scanMemoryInfo(super);
DragonFlyBSDMachine_scanCPUTime(super);
DragonFlyBSDMachine_scanJails(this);
Machine_scanGPUUsage(super);
}

bool Machine_isCPUonline(const Machine* host, unsigned int id) {
Expand All @@ -350,8 +351,7 @@ bool Machine_isCPUonline(const Machine* host, unsigned int id) {
return true;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
super->totalGPUUsage = -1.0;
}
6 changes: 3 additions & 3 deletions freebsd/FreeBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ void Machine_scan(Machine* super) {
openzfs_sysctl_updateArcStats(&this->zfs);
FreeBSDMachine_scanMemoryInfo(super);
FreeBSDMachine_scanCPU(super);
Machine_scanGPUUsage(super);
}

bool Machine_isCPUonline(const Machine* host, unsigned int id) {
Expand All @@ -398,8 +399,7 @@ bool Machine_isCPUonline(const Machine* host, unsigned int id) {
return true;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
super->totalGPUUsage = -1.0;
}
6 changes: 2 additions & 4 deletions linux/LinuxMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,7 @@ void Machine_scan(Machine* super) {
LinuxMachine_scanZfsArcstats(this);
LinuxMachine_scanZramInfo(this);
LinuxMachine_scanCPUTime(this);
Machine_scanGPUUsage(this);

const Settings* settings = super->settings;
if (settings->showCPUFrequency
Expand Down Expand Up @@ -822,7 +823,7 @@ bool Machine_isCPUonline(const Machine* super, unsigned int id) {
return this->cpuData[id + 1].online;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
LinuxMachine* this = (LinuxMachine*) super;

const uint64_t monotonictimeDelta = super->monotonicMs - super->prevMonotonicMs;
Expand All @@ -836,11 +837,8 @@ double Machine_updateGpuUsage(Machine* super) {
this->curResidueTime = this->curGpuTime;

for (gpuEngineData = this->gpuEngineData, i = 0; gpuEngineData; gpuEngineData = gpuEngineData->next, i++) {
// unsigned long long int timeDiff = saturatingSub(gpuEngineData->curTime, gpuEngineData->prevTime);

this->curResidueTime = saturatingSub(this->curResidueTime, gpuEngineData->curTime);
}

super->totalGPUUsage = 100.0 * super->totalGPUTimeDiff / (1000 * 1000) / monotonictimeDelta;
return super->totalGPUUsage;
}
6 changes: 3 additions & 3 deletions netbsd/NetBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ void Machine_scan(Machine* super) {

NetBSDMachine_scanMemoryInfo(this);
NetBSDMachine_scanCPUTime(this);
Machine_scanGPUUsage(super);

if (super->settings->showCPUFrequency) {
NetBSDMachine_scanCPUFrequency(this);
Expand All @@ -284,8 +285,7 @@ bool Machine_isCPUonline(const Machine* host, unsigned int id) {
return true;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
super->totalGPUUsage = -1.0;
}
6 changes: 3 additions & 3 deletions openbsd/OpenBSDMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ void Machine_scan(Machine* super) {
OpenBSDMachine_updateCPUcount(this);
OpenBSDMachine_scanMemoryInfo(this);
OpenBSDMachine_scanCPUTime(this);
Machine_scanGPUUsage(super);
}

bool Machine_isCPUonline(const Machine* super, unsigned int id) {
Expand All @@ -289,8 +290,7 @@ bool Machine_isCPUonline(const Machine* super, unsigned int id) {
return this->cpuData[id + 1].online;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
super->totalGPUUsage = -1.0;
}
6 changes: 3 additions & 3 deletions pcp/PCPMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ void Machine_scan(Machine* super) {
host->period = (host->timestamp - sample) * 100;

PCPMachine_scan(host);
Machine_scanGPUUsage(super);
}

Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
Expand Down Expand Up @@ -344,8 +345,7 @@ bool Machine_isCPUonline(const Machine* host, unsigned int id) {
return false;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
super->totalGPUUsage = -1.0;
}
6 changes: 3 additions & 3 deletions solaris/SolarisMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ void Machine_scan(Machine* super) {
SolarisMachine_scanCPUTime(this);
SolarisMachine_scanMemoryInfo(this);
SolarisMachine_scanZfsArcstats(this);
Machine_scanGPUUsage(super);
}

Machine* Machine_new(UsersTable* usersTable, uid_t userId) {
Expand Down Expand Up @@ -339,8 +340,7 @@ bool Machine_isCPUonline(const Machine* super, unsigned int id) {
return (super->existingCPUs == 1) ? true : this->cpus[id + 1].online;
}

double Machine_updateGpuUsage(Machine* super) {
void Machine_scanGPUUsage(Machine* super) {
/* Not supported yet */
(void)super;
return -1;
super->totalGPUUsage = -1.0;
}
2 changes: 1 addition & 1 deletion unsupported/UnsupportedMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ void Machine_scan(Machine* super) {

double Machine_updateGpuUsage(Machine* super ATTR_UNUSED) {
return -1;
}
}

0 comments on commit 09378df

Please sign in to comment.