javamonitoringdiskoshi

Is there a way to get the disk activity time using oshi?


So I'm supposed to get the disk activity time using the oshi library, but everywhere I look I can only find how to get the disk usage, not the activity time. The IBM documentatio for disk utilization says this:

If you know the access time for a given disk, you can use the number of transfers per second that the operating system reports to calculate utilization for the disk. To do so, multiply the average number of transfers per second by the access time for the disk as listed by the disk manufacturer.

Is there a way to get the average number of transfers per second and the acess time in Java?

I have already looked into the oshi docs but I couldn't find something to help, and don't know if I missed something


Solution

  • OSHI's HWDiskStore class has a few methods that may be helpful:

    Based on the docs you quoted:

    you can use the number of transfers per second that the operating system reports to calculate utilization for the disk

    it seems you could sum getReads() and getWrites() and divide by getTransferTime() to convert to "transfers per second" -- at least while the disk was active. Overall active time could just be the transfer time over elapsed time.

    Note that all these stats are snapshot measures that are (or should be) monotonically increasing, so they'll give you a cumulative total. To calculate usage over a time interval you'd need to capture the data twice and then use the deltas to calculate your metric.

    Here's some sample OSHI code to demonstrate the calculation:

    public class Test {
    
        public static void main(String[] args) {
            HWDiskStore disk = new SystemInfo().getHardware().getDiskStores().get(0);
            System.out.format("Reads: %d, Writes: %d, XferTime: %d, Timestamp: %d%n", disk.getReads(), disk.getWrites(),
                    disk.getTransferTime(), disk.getTimeStamp());
            Util.sleep(10000);
            disk.updateAttributes();
            System.out.format("Reads: %d, Writes: %d, XferTime: %d, Timestamp: %d%n", disk.getReads(), disk.getWrites(),
                    disk.getTransferTime(), disk.getTimeStamp());
        }
    }
    

    Here's the output:

    Reads: 70472443, Writes: 62744300, XferTime: 30886365, Timestamp: 1667953835809
    Reads: 70476812, Writes: 62744787, XferTime: 30887206, Timestamp: 1667953845847
    

    So we slept for 10 seconds (actually, 10038 milliseconds). During that time:

    From this we can conclude: