// Research only: enumerate Apple's private counter descriptors, never start sampling. // clang -fobjc-arc -framework Foundation -framework IOKit -framework Metal tools/gpu_raw_counter_probe.m -o /tmp/kymo-gpu-raw-probe #import #import #import #import #import @interface NSObject (KymoRawCounterDiscovery) - (instancetype)initWithAcceleratorPort:(io_service_t)port; - (NSArray *)sourceList; - (NSArray *)availableCounters; - (NSString *)name; - (io_service_t)acceleratorPort; @end int main(void) { @autoreleasepool { void *bundle = dlopen("/System/Library/Extensions/AGXGPURawCounterBundle.bundle/Contents/MacOS/AGXGPURawCounterBundle", RTLD_LAZY); Class type = NSClassFromString(@"AGXGPURawCounterSourceGroup"); if (!bundle || !type) { fprintf(stderr, "Raw counter plugin unavailable\n"); return 1; } io_iterator_t iterator = 0; if (IOServiceGetMatchingServices(kIOMainPortDefault, IOServiceMatching("IOAccelerator"), &iterator) != KERN_SUCCESS) return 2; NSMutableArray *devices = [NSMutableArray array]; io_service_t service; while ((service = IOIteratorNext(iterator))) { io_connect_t connection = IO_OBJECT_NULL; @try { id group = [[type alloc] initWithAcceleratorPort:service]; // Also test the standard type-0 device connection; do not guess // privileged user-client types or enable performance collection. kern_return_t openStatus = KERN_SUCCESS; BOOL serviceInitialized = group != nil; if (!group) { openStatus = IOServiceOpen(service, mach_task_self(), 0, &connection); if (openStatus == KERN_SUCCESS) group = [[type alloc] initWithAcceleratorPort:connection]; } BOOL connectionInitialized = group != nil; uint64_t registryID = 0; IORegistryEntryGetRegistryEntryID(service, ®istryID); NSArray> *metalDevices = MTLCopyAllDevices(); BOOL metalPortAttempted = NO; for (id device in metalDevices) { if (!group && (metalDevices.count == 1 || [device registryID] == registryID) && [device respondsToSelector:@selector(acceleratorPort)]) { metalPortAttempted = YES; group = [[type alloc] initWithAcceleratorPort:[device acceleratorPort]]; } } NSMutableArray *sources = [NSMutableArray array]; for (id source in [group sourceList]) { NSMutableArray *counters = [NSMutableArray array]; for (id counter in [source availableCounters]) { [counters addObject:@{@"name": [counter name] ?: @"", @"description": [counter description] ?: @""}]; } [sources addObject:@{@"name": [source name] ?: @"", @"counters": counters}]; } [devices addObject:@{@"serviceInitialized": @(serviceInitialized), @"connectionInitialized": @(connectionInitialized), @"metalPortAttempted": @(metalPortAttempted), @"initialized": @(group != nil), @"type0OpenStatus": @(openStatus), @"sources": sources}]; group = nil; } @catch (NSException *exception) { [devices addObject:@{@"error": exception.reason ?: exception.name}]; } @finally { if (connection != IO_OBJECT_NULL) IOServiceClose(connection); IOObjectRelease(service); } } IOObjectRelease(iterator); NSError *error = nil; NSDictionary *report = @{@"uid": @(getuid()), @"euid": @(geteuid()), @"devices": devices}; NSData *data = [NSJSONSerialization dataWithJSONObject:report options:NSJSONWritingPrettyPrinted error:&error]; if (!data) { fprintf(stderr, "%s\n", error.description.UTF8String); return 3; } puts([[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].UTF8String); } return 0; }