fdh
fdh

Reputation: 5344

How can I retrieve read/write disk info?

How can I retrieve the specific read/write rates of a disk? (Data written per second and data read per second as can be seen in activity monitor)

Note I am specifically asking for an Objective-C/C solution on OS X.

Upvotes: 1

Views: 1149

Answers (1)

ldiqual
ldiqual

Reputation: 15365

You can take a look at XRG, which is an activity monitor for OSX. The whole disk statistics grabber is in XRGDiskView.m, in the getDISKcounters function.

To sum it up, you get a list of hardrives using the following code:

#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);

Then, you call the following function to get total read/written bytes on disks from, I assume, the system startup:

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;
}

Then, call it at regular intervals:

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);

As soon as you have this total read/written bytes, you just have to make some deltas between your refreshed data. For instance:

// 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

That's just a short untested analysis, but it should work as expected.


Edit

To be more clear, what I call "delta" is just a difference between two values. You need a rate, that means:

rate = bytes read/written by seconds

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

Now, getDISKcounter just give you the total amount of read/written bytes since the system startup (or another absolute time interval, but you really don't care). So to get the amount of read/written bytes during a limited period, you need to compute the difference between two absolute results:

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

That is just simple mathematics, nothing complex here.

Upvotes: 4

Related Questions