|
1 | 1 | #include "diskio.h" |
| 2 | +#include "util/apple/cf_helpers.h" |
2 | 3 |
|
3 | 4 | #include <IOKit/IOKitLib.h> |
| 5 | +#include <IOKit/IOBSD.h> |
4 | 6 | #include <IOKit/storage/IOMedia.h> |
5 | 7 | #include <IOKit/storage/IOBlockStorageDriver.h> |
| 8 | +#include <IOKit/storage/IOStorageProtocolCharacteristics.h> |
| 9 | + |
| 10 | +static inline void wrapIoObjectRelease(io_service_t* service) |
| 11 | +{ |
| 12 | + assert(service); |
| 13 | + if (*service) |
| 14 | + IOObjectRelease(*service); |
| 15 | +} |
| 16 | +#define FF_IOOBJECT_AUTO_RELEASE __attribute__((__cleanup__(wrapIoObjectRelease))) |
6 | 17 |
|
7 | 18 | const char* ffDiskIOGetIoCounters(FFlist* result, FFDiskIOOptions* options) |
8 | 19 | { |
9 | | - io_iterator_t iterator; |
10 | | - if(IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOMediaClass), &iterator) != kIOReturnSuccess) |
| 20 | + FF_IOOBJECT_AUTO_RELEASE io_iterator_t iterator = 0; |
| 21 | + if(IOServiceGetMatchingServices(MACH_PORT_NULL, IOServiceMatching(kIOMediaClass), &iterator) != KERN_SUCCESS) |
11 | 22 | return "IOServiceGetMatchingServices() failed"; |
12 | 23 |
|
13 | 24 | io_registry_entry_t registryEntry; |
14 | 25 | while((registryEntry = IOIteratorNext(iterator)) != 0) |
15 | 26 | { |
16 | | - CFMutableDictionaryRef properties; |
17 | | - if(IORegistryEntryCreateCFProperties(registryEntry, &properties, kCFAllocatorDefault, kNilOptions) != kIOReturnSuccess) |
18 | | - { |
19 | | - IOObjectRelease(registryEntry); |
| 27 | + FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryPartition = registryEntry; |
| 28 | + |
| 29 | + io_name_t deviceName; |
| 30 | + if (IORegistryEntryGetName(registryEntry, deviceName) != KERN_SUCCESS) |
20 | 31 | continue; |
21 | | - } |
22 | 32 |
|
23 | | - io_registry_entry_t parent; |
24 | | - IORegistryEntryGetParentEntry(registryEntry, kIOServicePlane, &parent); |
25 | | - if(IOObjectConformsTo(parent, kIOBlockStorageDriverClass)) continue; |
| 33 | + FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryDriver = 0; |
| 34 | + if (IORegistryEntryGetParentEntry(entryPartition, kIOServicePlane, &entryDriver) != KERN_SUCCESS) |
| 35 | + continue; |
26 | 36 |
|
27 | | - io_name_t name; |
28 | | - if (IORegistryEntryGetName(registryEntry, name) != KERN_SUCCESS) |
| 37 | + if (!IOObjectConformsTo(entryDriver, kIOBlockStorageDriverClass)) // physical disk only |
29 | 38 | continue; |
30 | 39 |
|
31 | | - // NSDictionary* props = (__bridge NSDictionary*) properties; |
| 40 | + FF_CFTYPE_AUTO_RELEASE CFDictionaryRef statistics = IORegistryEntryCreateCFProperty(entryDriver, CFSTR(kIOBlockStorageDriverStatisticsKey), kCFAllocatorDefault, kNilOptions); |
| 41 | + if (!statistics) |
| 42 | + continue; |
32 | 43 |
|
33 | | - // id statistics = [props valueForKey:@(kIOBlockStorageDriverStatisticsKey)]; |
34 | | - // if (!statistics) continue; |
| 44 | + FFDiskIOResult* device = (FFDiskIOResult*) ffListAdd(result); |
| 45 | + ffStrbufInitS(&device->name, deviceName); |
| 46 | + ffStrbufInit(&device->devPath); |
| 47 | + ffStrbufInit(&device->type); |
| 48 | + ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey), (int64_t*) &device->bytesRead); |
| 49 | + ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey), (int64_t*) &device->bytesWritten); |
| 50 | + ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsReadsKey), (int64_t*) &device->readCount); |
| 51 | + ffCfDictGetInt64(statistics, CFSTR(kIOBlockStorageDriverStatisticsWritesKey), (int64_t*) &device->writeCount); |
35 | 52 |
|
36 | | - // id volGroupaMntFromName = [props valueForKey:@"VolGroupMntFromName"]; |
37 | | - // if (!volGroupaMntFromName) continue; |
| 53 | + FF_CFTYPE_AUTO_RELEASE CFStringRef bsdName = IORegistryEntryCreateCFProperty(entryPartition, CFSTR(kIOBSDNameKey), kCFAllocatorDefault, kNilOptions); |
| 54 | + if (bsdName) |
| 55 | + { |
| 56 | + ffCfStrGetString(bsdName, &device->devPath); |
| 57 | + ffStrbufPrependS(&device->devPath, "/dev/"); |
| 58 | + } |
38 | 59 |
|
39 | | - // NSLog(@"%@", props); |
| 60 | + FF_IOOBJECT_AUTO_RELEASE io_registry_entry_t entryPhysical = 0; |
| 61 | + if (IORegistryEntryGetParentEntry(entryDriver, kIOServicePlane, &entryPhysical) == KERN_SUCCESS) |
| 62 | + { |
| 63 | + FF_CFTYPE_AUTO_RELEASE CFDictionaryRef protocolCharacteristics = IORegistryEntryCreateCFProperty(entryPhysical, CFSTR(kIOPropertyProtocolCharacteristicsKey), kCFAllocatorDefault, kNilOptions); |
| 64 | + if (protocolCharacteristics) |
| 65 | + ffCfDictGetString(protocolCharacteristics, CFSTR("Physical Interconnect"), &device->type); |
| 66 | + } |
40 | 67 | } |
41 | 68 |
|
42 | | - IOObjectRelease(iterator); |
43 | | - |
44 | 69 | return NULL; |
45 | 70 | } |
0 commit comments