如何检索在引导驱动器上读取和写入的总字节数

时间:2012-09-03 23:15:29

标签: objective-c c macos unix system

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上使用目标C并为OSX构建应用程序。

0 个答案:

没有答案