4.2. Disk
The following sections showcase scripts that monitor disk and I/O activity.
4.2.1. Summarizing Disk Read/Write Traffic Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to identify which processes are performing the heaviest disk reads/writes to the system.
disktop.stp
disktop.stp outputs the top ten processes responsible for the heaviest reads/writes to disk. Example 4.4, “disktop.stp Sample Output” displays a sample output for this script, and includes the following data per listed process:
UID
— user ID. A user ID of0
refers to the root user.PID
— the ID of the listed process.PPID
— the process ID of the listed process's parent process.CMD
— the name of the listed process.DEVICE
— which storage device the listed process is reading from or writing to.T
— the type of action performed by the listed process;W
refers to write, whileR
refers to read.BYTES
— the amount of data read to or written from disk.
The time and date in the output of disktop.stp is returned by the functions
ctime()
and gettimeofday_s()
. ctime()
derives calendar time in terms of seconds passed since the Unix epoch (January 1, 1970). gettimeofday_s()
counts the actual number of seconds since Unix epoch, which gives a fairly accurate human-readable timestamp for the output.
In this script, the
$return
is a local variable that stores the actual number of bytes each process reads or writes from the virtual file system. $return
can only be used in return probes (e.g. vfs.read.return
and vfs.read.return
).
Example 4.4. disktop.stp Sample Output
4.2.2. Tracking I/O Time For Each File Read or Write Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to monitor the amount of time it takes for each process to read from or write to any file. This is useful if you wish to determine what files are slow to load on a given system.
iotime.stp
iotime.stp tracks each time a system call opens, closes, reads from, and writes to a file. For each file any system call accesses, iotime.stp counts the number of microseconds it takes for any reads or writes to finish and tracks the amount of data (in bytes) read from or written to the file.
iotime.stp also uses the local variable
$count
to track the amount of data (in bytes) that any system call attempts to read or write. Note that $return
(as used in disktop.stp from Section 4.2.1, “Summarizing Disk Read/Write Traffic”) stores the actual amount of data read/written. $count
can only be used on probes that track data reads or writes (e.g. syscall.read
and syscall.write
).
Example 4.5. iotime.stp Sample Output
Example 4.5, “iotime.stp Sample Output” prints out the following data:
- A timestamp, in microseconds
- Process ID and process name
- An
access
oriotime
flag - The file accessed
If a process was able to read or write any data, a pair of
access
and iotime
lines should appear together. The access
line's timestamp refer to the time that a given process started accessing a file; at the end of the line, it will show the amount of data read/written (in bytes). The iotime
line will show the amount of time (in microseconds) that the process took in order to perform the read or write.
If an
access
line is not followed by an iotime
line, it simply means that the process did not read or write any data.
4.2.3. Track Cumulative IO Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to track the cumulative amount of I/O to the system.
traceio.stp
traceio.stp prints the top ten executables generating I/O traffic over time. In addition, it also tracks the cumulative amount of I/O reads and writes done by those ten executables. This information is tracked and printed out in 1-second intervals, and in descending order.
Note that traceio.stp also uses the local variable
$return
, which is also used by disktop.stp from Section 4.2.1, “Summarizing Disk Read/Write Traffic”.
Example 4.6. traceio.stp Sample Output
4.2.4. I/O Monitoring (By Device) Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to monitor I/O activity on a specific device.
traceio2.stp
traceio2.stp takes 1 argument: the whole device number. To get this number, use
stat -c "0x%D" directory
, where directory
is located in the device you wish to monitor.
The
usrdev2kerndev()
function converts the whole device number into the format understood by the kernel. The output produced by usrdev2kerndev()
is used in conjunction with the MKDEV()
, MINOR()
, and MAJOR()
functions to determine the major and minor numbers of a specific device.
The output of traceio2.stp includes the name and ID of any process performing a read/write, the function it is performing (i.e.
vfs_read
or vfs_write
), and the kernel device number.
The following example is an excerpt from the full output of
stap traceio2.stp 0x805
, where 0x805
is the whole device number of /home
. /home
resides in /dev/sda5
, which is the device we wish to monitor.
Example 4.7. traceio2.stp Sample Output
4.2.5. Monitoring Reads and Writes to a File Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to monitor reads from and writes to a file in real time.
inodewatch-simple.stp
inodewatch-simple.stp takes the following information about the file as arguments on the command line:
- The file's major device number.
- The file's minor device number.
- The file's
inode
number.
To get this information, use
stat -c '%D %i' filename
, where filename
is an absolute path.
For instance: if you wish to monitor
/etc/crontab
, run stat -c '%D %i' /etc/crontab
first. This gives the following output:
805 1078319
805 1078319
805
is the base-16 (hexadecimal) device number. The lower two digits are the minor device number and the upper digits are the major number. 1078319
is the inode
number. To start monitoring /etc/crontab
, run stap inodewatch.stp 0x8 0x05 1078319
(The 0x
prefixes indicate base-16 values.
The output of this command contains the name and ID of any process performing a read/write, the function it is performing (i.e.
vfs_read
or vfs_write
), the device number (in hex format), and the inode
number. Example 4.8, “inodewatch-simple.stp Sample Output” contains the output of stap inodewatch.stp 0x8 0x05 1078319
(when cat /etc/crontab
is executed while the script is running) :
Example 4.8. inodewatch-simple.stp Sample Output
cat(16437) vfs_read 0x800005/1078319 cat(16437) vfs_read 0x800005/1078319
cat(16437) vfs_read 0x800005/1078319
cat(16437) vfs_read 0x800005/1078319
4.2.6. Monitoring Changes to File Attributes Copy linkLink copied to clipboard!
Copy linkLink copied to clipboard!
This section describes how to monitor if any processes are changing the attributes of a targeted file, in real time.
inodewatch2-simple.stp
Like inodewatch-simple.stp from Section 4.2.5, “Monitoring Reads and Writes to a File”, inodewatch2-simple.stp takes the targeted file's device number (in integer format) and
inode
number as arguments. For more information on how to retrieve this information, refer to Section 4.2.5, “Monitoring Reads and Writes to a File”.
The output for inodewatch2-simple.stp is similar to that of inodewatch-simple.stp, except that inodewatch2-simple.stp also contains the attribute changes to the monitored file, as well as the ID of the user responsible (
uid()
). Example 4.9, “inodewatch2-simple.stp Sample Output” contains shows the output of inodewatch2-simple.stp while monitoring /home/joe/bigfile
when user joe
executes chmod 777 /home/joe/bigfile
and chmod 666 /home/joe/bigfile
.
Example 4.9. inodewatch2-simple.stp Sample Output
chmod(17448) inode_setattr 0x800005/6011835 100777 500 chmod(17449) inode_setattr 0x800005/6011835 100666 500
chmod(17448) inode_setattr 0x800005/6011835 100777 500
chmod(17449) inode_setattr 0x800005/6011835 100666 500