0
io_iterator_t drives = IO_OBJECT_NULL;
mach_port_t masterPort = IO_OBJECT_NULL;

IOMasterPort(bootstrap_port, &masterPort);
IOServiceGetMatchingServices(masterPort, IOServiceMatching(kIOBlockStorageDriverClass), &drives);

io_registry_entry_t drive = 0;
uint64_t totalBytesRead = 0;
uint64_t totalBytesWritten = 0;

while ((drive = IOIteratorNext(drives)))
{
    io_name_t drivePath;
    IORegistryEntryGetPath(drive, kIOServicePlane, drivePath);
    NSLog(@"Path:%s", drivePath);

    NSNumber* statVal = 0;
    NSDictionary* statistics;

    statistics = (NSDictionary*)IORegistryEntryCreateCFProperty(drive, CFSTR(kIOBlockStorageDriverStatisticsKey), kCFAllocatorDefault, kNilOptions);

    if (statistics)
    {
        statVal = (NSNumber*)[statistics objectForKey:(NSString*)CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey)];
        totalBytesRead += [statVal intValue];

        statVal = (NSNumber*)[statistics objectForKey:(NSString*)CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey)];
        totalBytesWritten += [statVal intValue];
    }

    [statistics release];
    IOObjectRelease(drive);
}
IOIteratorReset(drives);

readRate = (double)(totalBytesRead - prevReadVal)/1024;
writeRate = (double)(totalBytesWritten - prevWriteVal)/1024;

prevReadVal = totalBytesRead;
prevWriteVal = totalBytesWritten;

我正在尝试检索自启动以来在引导驱动器上读取和写入的总字节数。但是,上面的代码似乎不起作用。我做了一些调查,发现这条线:statistics = (NSDictionary*)IORegistryEntryCreateCFProperty(drive, CFSTR(kIOBlockStorageDriverStatisticsKey), kCFAllocatorDefault, kNilOptions);行不通。该函数似乎返回 null,而不是返回有效对象。有谁知道为什么?全面的谷歌搜索没有产生任何结果。

注意我在 Mountain Lion OS X 上使用 Objective C 并为 OSX 构建应用程序。

4

0 回答 0