What is the psutil.disk_io_counters() method?

The psutil module

The psutil )Python system and process utilities) is a Python package that retrieves information on ongoing processes and system usage (CPU, memory, storage, network, and sensors). We mainly use it for system monitoring, profiling, restricting process resources, and process management.

The module can be installed via pip as follows:

pip install psutil

The disk_io_counters method

The disk_io_counters method returns the disk input output statistics in the form of counters as a tuple. The different counters returned are as follows:

  • read_count: The number of reads performed.
  • write_count: The number of writes performed.
  • read_bytes: The number of bytes read from disk.
  • write_bytes: The number of bytes written to the disk.

There are certain fields returned that are specific to certain platforms:

Field name Description Platform availability
read_time The time spent reading from a disk in milliseconds. Available on all platforms except NetBSD and OpenBSD.
write_time The time spent writing to a disk in milliseconds. Available on all platforms except NetBSD and OpenBSD.
busy_time The time spent doing actual I/Os operations in milliseconds. Available only on Linux and FreeBSD.
read_merged_count The number of merged reads. Available only on Linux.
write_merged_count The number of merged writes Available only on Linux.

Note: For efficiency, read and write operations that are close together can be combined. As a result, two 4K read operations may become one 8K read operation before being transferred to the disc, resulting in just one I/O being counted (and queued).

Method signature

psutil.disk_io_counters(perdisk=False, nowrap=True)
  • perdisk: If True, the method returns the same information for all different disks on the system.
  • nowrap: On some systems, such as Linux, the numbers reported by the kernel may overflow and wrap on an extremely busy or long-lived system. Setting nowrap to True adjusts the values such that it increases or remains the same but never decreases.

Example

import psutil
print("psutil.disk_io_counters() = ", psutil.disk_io_counters())

Explanation

  • Line 1: We import the psutil module.
  • Line 3: We retrieve the current disk IO statistics and print them to the console.