Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I check how busy the HDD is with PHP?

I've noticed that some cloud hosting solutions have really poor Disk IO. This causes a few problems that could be solved by having the script wait until the disk was less busy.

With PHP is it possible to monitor the busy (or not so busy) state of the filesystem without making things worse?

like image 917
Matthew Brown aka Lord Matt Avatar asked Aug 01 '16 16:08

Matthew Brown aka Lord Matt


1 Answers

If this is a Linux system, you can calculate disk usage yourself - the language you choose to implement it in will use the same concepts.

Your kernel is most likely using sysfs which makes a lot of information about your system available at /sys; we can grab information about our desired disks at a regular interval, and calculate usage based on the differences between them.

On my system I will be looking at the disk, sda, yours may differ.

$ cat /sys/class/block/sda/stat
   42632       25  2045318   247192  6956543  7362278 123236256 23878974        0  3703033 24119492

Now if we look at the Kernel documentation for /sys/class/block/<dev>/stat we can see the following descriptions for each column of the output.

Name            units         description
----            -----         -----------
read I/Os       requests      number of read I/Os processed
read merges     requests      number of read I/Os merged with in-queue I/O
read sectors    sectors       number of sectors read
read ticks      milliseconds  total wait time for read requests
write I/Os      requests      number of write I/Os processed
write merges    requests      number of write I/Os merged with in-queue I/O
write sectors   sectors       number of sectors written
write ticks     milliseconds  total wait time for write requests
in_flight       requests      number of I/Os currently in flight
io_ticks        milliseconds  total time this block device has been active
time_in_queue   milliseconds  total wait time for all requests

If we run this on a cron schedule, and diff some of the wait times, we can see just how long we are waiting on each operation. You will also have other stats about total IOPS, and RW bandwidth. The documentation goes more in depth on each field.

Whatever language is chosen, the file descriptor to open to get information about the disk will be

/sys/class/block/<dev>/stat

If we do this on a schedule, we can draw fancy graphs ;)

enter image description here

like image 152
Matt Clark Avatar answered Oct 01 '22 22:10

Matt Clark