1

如何检索磁盘的特定读/写速率?(可以在活动监视器中看到每秒写入的数据和每秒读取的数据)

注意我特别要求在 OS X 上使用 Objective-C/C 解决方案。

4

1 回答 1

4

您可以查看XRG,它是 OSX 的活动监视器。整个磁盘统计信息采集器在XRGDiskView.m,getDISKcounters函数中。

总而言之,您可以使用以下代码获得硬盘列表:

#import <IOKit/IOKitLib.h>
#import <IOKit/storage/IOBlockStorageDriver.h>

io_iterator_t drivelist  = IO_OBJECT_NULL;
mach_port_t masterPort = IO_OBJECT_NULL;

/* get ports and services for drive stats */
/* Obtain the I/O Kit communication handle */
IOMasterPort(bootstrap_port, &masterPort);

/* Obtain the list of all drive objects */
IOServiceGetMatchingServices(masterPort, 
                             IOServiceMatching("IOBlockStorageDriver"), 
                             &drivelist);

然后,您调用以下函数以从系统启动获取磁盘上的总读/写字节数:

void getDISKcounters(io_iterator_t drivelist, io_stats *i_dsk, io_stats *o_dsk)
{
    io_registry_entry_t drive       = 0;  /* needs release */
    UInt64          totalReadBytes  = 0;
    UInt64          totalWriteBytes = 0;

    while ((drive = IOIteratorNext(drivelist))) {
        CFNumberRef     number      = 0;  /* don't release */
        CFDictionaryRef properties  = 0;  /* needs release */
        CFDictionaryRef statistics  = 0;  /* don't release */
        UInt64      value       = 0;

        /* Obtain the properties for this drive object */

        IORegistryEntryCreateCFProperties(drive, (CFMutableDictionaryRef *) &properties, kCFAllocatorDefault, kNilOptions);

        /* Obtain the statistics from the drive properties */
        statistics = (CFDictionaryRef) CFDictionaryGetValue(properties, CFSTR(kIOBlockStorageDriverStatisticsKey));

        if (statistics) {
            /* Obtain the number of bytes read from the drive statistics */
            number = (CFNumberRef) CFDictionaryGetValue(statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesReadKey));
            if (number) {
                CFNumberGetValue(number, kCFNumberSInt64Type, &value);
                totalReadBytes += value;
            }

            /* Obtain the number of bytes written from the drive statistics */
            number = (CFNumberRef) CFDictionaryGetValue (statistics, CFSTR(kIOBlockStorageDriverStatisticsBytesWrittenKey));
            if (number) {
                CFNumberGetValue(number, kCFNumberSInt64Type, &value);
                totalWriteBytes += value;
            }
        }
        /* Release resources */

        CFRelease(properties); properties = 0;
        IOObjectRelease(drive); drive = 0;

    }
    IOIteratorReset(drivelist);

    i_dsk->bytes = totalReadBytes;
    o_dsk->bytes = totalWriteBytes;
}

然后,定期调用它:

io_stats i_dsk_t0;
io_stats o_dsk_t0;
getDISKcounters(drivelist, &i_dsk_t0, &o_dsk_t0);

// Wait 1s
io_stats i_dsk_t1;
io_stats o_dsk_t1;
getDISKcounters(drivelist, &i_dsk_t1, &o_dsk_t1);

一旦你有了这个总读/写字节,你只需要在刷新的数据之间做一些增量。例如:

// t=0s
i_dsk_t0->bytes == 0 bytes

// t=1s
i_dsk_t1->bytes == 1000 bytes

delta = i_dsk_t1->bytes - i_dsk_t0->bytes
rate = delta/duration = delta/1s = 1000bytes/second

这只是一个简短的未经测试的分析,但它应该可以按预期工作。


编辑

更清楚地说,我所说的“delta”只是两个值之间的差异。您需要一个费率,这意味着:

rate = bytes read/written by seconds

       bytes read/written for a period of N seconds
     = --------------------------------------------
                        N seconds                           

现在,只需给您自系统启动以来getDISKcounter读/写字节的总量(或另一个绝对时间间隔,但您真的不在乎)。因此,要在有限的时间内获得读/写字节的数量,您需要计算两个绝对结果之间的差异:

B = total read/written bytes for N seconds = total read/written bytes since the system startup at time (X + N) -  total read/written bytes since the system startup at time X

rate = B / N

那只是简单的数学,这里没有什么复杂的。

于 2012-08-15T00:35:45.377 回答