在Python程序中,我需要获得主机上所有磁盘的累计读/写速度.我使用subprocess.check_output()来调用以下Linux shell命令:
$sudo hdparm -t /dev/sda
这给出了结果:
/dev/sda:
Timing buffered disk reads: 1488 MB in 3.00 seconds = 495.55 MB/sec
然后我可以解析495.55.好的,到目前为止一切顺利.
但是在hdparm的手册页上我找到了-t标志的这个解释,基本上说在执行测量时没有其他进程应该同时读/写磁盘:
Perform timings of device reads for benchmark and comparison purposes. For meaningful results,this operation should be repeated 2-3 times on an otherwise inactive system (no other active processes) with at least a couple of megabytes of free memory. This displays the speed of reading through the buffer cache to the disk without any prior caching of data. This measurement is an indication of how fast the drive can sustain sequential data reads under Linux,without any filesystem overhead. To ensure accurate measurements,the buffer cache is flushed during the processing of -t using the BLKFLSBUF ioctl.
问题是:
如何在执行测量时确保没有其他进程同时访问磁盘?