0

I want to write a Python code to read disk IO and network IO as percentage, like we see in the Windows Task Manager. Currently I am using psutil.disk_io_counters() and psutil.net_io_counters(). Through this I am getting byte read & byte write for disk IO and byte received & byte sent for network IO. But I do not know how to convert them into percentage. It is also observed that disk_io_counters() and net_io_counters() does not give the instance value. I have tried the suggestion provided in this link. But I did not get the wanted value. My OS is Windows, but I want the script to be used in a platform independent way. So, without installing any tools like iotop or iostat, is it possible to get the values I require? I have tried the following code:

import psutil, os
print('Disk: ',psutil.disk_io_counters())
print('Network: ',psutil.net_io_counters())

I also tried the following code to check that use of psutil.io_counters() gives the instant disk IO or not. The code is:

import psutil
import time
for x in range(10):
    for proc in psutil.process_iter():
        io_counters = proc.io_counters() 
        disk_usage_process = io_counters[2] + io_counters[3] # read_bytes + write_bytes
        print("PID: ", proc.pid, "Disk", disk_usage_process)
    print('************************************************************')
    time.sleep(1)

But I have observed that the values are not of that moment. For prove I have collected disk IO of two system processes using io.counters() and seen that after times it is changing in increasing order. It means it is adding the disk IO from beginning of the processes. Following is the snapshot:

PID:  10068 Disk 1597555    PID:  8608 Disk 99729700
PID:  10068 Disk 1597555    PID:  8608 Disk 99729828    
PID:  10068 Disk 1597555    PID:  8608 Disk 99729956
PID:  10068 Disk 1597555    PID:  8608 Disk 99730212
PID:  10068 Disk 1598271    PID:  8608 Disk 99730340
PID:  10068 Disk 1598271    PID:  8608 Disk 99730596
PID:  10068 Disk 1598271    PID:  8608 Disk 99730724
PID:  10068 Disk 1598271    PID:  8608 Disk 99730852
PID:  10068 Disk 1598271    PID:  8608 Disk 99731108
PID:  10068 Disk 1598271    PID:  8608 Disk 99731236
Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
M. P
  • 35
  • 2
  • 9
  • would dividing the disk_io count by the disk size and the network bytes by your network capacity be a resonable measure? You can compare the values you get with the ones shown in the task manager to be sure – S Raghav Mar 19 '18 at 07:45
  • Your question seems to be a duplicate of https://stackoverflow.com/questions/31860163/how-to-get-disk-io-and-network-usage-as-percent-by-psutil. Is this what you are looking for? – S Raghav Mar 19 '18 at 07:46
  • @raghav710 yes the question is already asked before and I have also mentioned it in my question by providing the link. But the answer which has been provided in that question is not sufficient. Because in that case those values of disk io and network io is not of that instance. I mean the value of the moment. There is no answers for that. – M. P Mar 19 '18 at 08:10
  • @raghav710 Can you please tell me how to get disk size. and my network capacity... – M. P Mar 19 '18 at 08:19
  • @raghav710 sorry I misunderstood the question. So you want your disk_io as a percentage of the disk_io at the moment? – S Raghav Mar 19 '18 at 08:52

1 Answers1

3

Question:

Get disk usage and network usage of a process as a percentage of the total disk and network usage at the moment

Solution

From what I checked manually (on Windows, using the steps given here), the read_bytes and write_bytes values of yourProcess.io_counters() should give you the disk I/O for the process. Combining with the total I/O using psutil.disk_io_counters() should give you the percentage

So your code to get disk usage percentage can look as follows

 p = psutil.Process()
 io_counters = p.io_counters() 
 disk_usage_process = io_counters[2] + io_counters[3] # read_bytes + write_bytes
 disk_io_counter = psutil.disk_io_counters()
 disk_total = disk_io_counter[2] + disk_io_counter[3] # read_bytes + write_bytes
 print("Disk", disk_usage_process/disk_total * 100)

But for network usage, the task doesnt seem that easy. As mentioned here

AFAIK most (all?) operating systems do not expose those metrics so no, unfortunately this (a process version of psutil.net_io_counters) is not possible

UPDATE: Trying running this in a loop

>>> p = psutil.Process()
>>> for i in range(10):
...   p.io_counters()
...
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
pio(read_count=141L, write_count=0L, read_bytes=651238L, write_bytes=0L, other_c
ount=3060L, other_bytes=37992L)
S Raghav
  • 1,386
  • 1
  • 16
  • 26
  • thank you for your answer. But I have tried your code and this code provides the disk io of the current process only. Not the overall system disk io. and it is also not the instance value. if you use your code inside a for loop, you can see that the disk io is always increasing. – M. P Mar 20 '18 at 05:54
  • Can you clarify what is *instance value* so I can improve my answer? I understood instance value to mean that for the current process and hence the answer. – S Raghav Mar 20 '18 at 05:58
  • The term instance mean is to measure the disk io of process P at time T. If i am measuring disk io for any process at a time t, it will give me the read and writes which exactly occurred at time t only, not from all the read and write occurred from time 0 to t. If we print the disk io inside loop it is increasing, i.e. it is taking sum of all disk io of that process from time 0 (when the process start) to till t. But somehow your answer gave me a hint to calculate the system's disk io by considering all running processes and making a subtraction of disk io of time t-1 to t. Thank you.. – M. P Mar 20 '18 at 06:14
  • @M.P I am interested in understanding the change in values while looping. I have updated my answer with the values printed in a loop and I see that the value remains the same. Can you help me understand? – S Raghav Mar 20 '18 at 06:56
  • sorry i did not mentioned abt time delay inside loop. you have to put delay inside loop after every print. Otherwise it will print the diskio of time t in 10 times. so by putting a delay of 1s ensures you are reading the disk io of that process after every 1 sec. Again the command task will produce a constant disk io, cause it will do same amount of read/write every time. So to check this properly you should print the disk io of all running process inside a loop by putting a time delay. There you can see that disk io of process is increasing coz it adding all disk io from begining. – M. P Mar 20 '18 at 07:57
  • I am updating the script through which you can see. – M. P Mar 20 '18 at 07:57