4.2. Disk
The following sections showcase scripts that monitor disk and I/O activity.
4.2.1. Summarizing Disk Read/Write Traffic 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
This section describes how to identify which processes are performing the heaviest disk reads/writes to the system.
例 4.9. disktop.stp
例 4.9 “disktop.stp” outputs the top ten processes responsible for the heaviest reads or writes to a disk. 例 4.10 “例 4.9 “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, andR
refers to read.BYTES
— the amount of data read to or written from disk.
The time and date in the output of 例 4.9 “disktop.stp” is returned by the functions
ctime()
and gettimeofday_s()
. ctime()
derives calendar time in terms of seconds passed since the start of the Unix time (January 1, 1970). gettimeofday_s()
counts the actual number of seconds since the start of the Unix time, 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 (for example, vfs.read.return
and vfs.read.return
).
例 4.10. 例 4.9 “disktop.stp” Sample Output
4.2.2. Tracking I/O Time For Each File Read or Write 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
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.
例 4.11. iotime.stp
例 4.11 “iotime.stp” tracks each time a system call opens, closes, reads from, and writes to a file. For each file any system call accesses, 例 4.11 “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.
例 4.11 “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 例 4.9 “disktop.stp” from 第 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).
例 4.12. 例 4.11 “iotime.stp” Sample Output
例 4.12 “例 4.11 “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 refers 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 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
This section describes how to track the cumulative amount of I/O to the system.
例 4.13. traceio.stp
例 4.13 “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 例 4.13 “traceio.stp” also uses the local variable
$return
, which is also used by 例 4.9 “disktop.stp” from 第 4.2.1 节 “Summarizing Disk Read/Write Traffic”.
例 4.14. 例 4.13 “traceio.stp” Sample Output
4.2.4. I/O Monitoring (By Device) 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
This section describes how to monitor I/O activity on a specific device.
例 4.15. traceio2.stp
例 4.15 “traceio2.stp” takes 1 argument: the whole device number. To get this number, use stat -c "0x%D" directory, where directory is located on 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 例 4.15 “traceio2.stp” includes the name and ID of any process performing a read/write, the function it is performing (
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.
例 4.16. 例 4.15 “traceio2.stp” Sample Output
4.2.5. Monitoring Reads and Writes to a File 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
This section describes how to monitor reads from and writes to a file in real time.
例 4.17. inodewatch.stp
例 4.17 “inodewatch.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 the
/etc/crontab
file, run stat -c '%D %i' /etc/crontab first. This outputs 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 (
vfs_read
or vfs_write
), the device number (in hex format), and the inode
number. 例 4.18 “例 4.17 “inodewatch.stp” Sample Output” contains the output of stap inodewatch.stp 0x8 0x05 1078319 (when cat /etc/crontab is executed while the script is running):
例 4.18. 例 4.17 “inodewatch.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 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
This section describes how to monitor if any processes are changing the attributes of a targeted file, in real time.
例 4.19. inodewatch2-simple.stp
Like 例 4.17 “inodewatch.stp” from 第 4.2.5 节 “Monitoring Reads and Writes to a File”, 例 4.19 “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, see 第 4.2.5 节 “Monitoring Reads and Writes to a File”.
The output for 例 4.19 “inodewatch2-simple.stp” is similar to that of 例 4.17 “inodewatch.stp”, except that 例 4.19 “inodewatch2-simple.stp” also contains the attribute changes to the monitored file, as well as the ID of the user responsible (
uid()
). 例 4.20 “例 4.19 “inodewatch2-simple.stp” Sample Output” shows the output of 例 4.19 “inodewatch2-simple.stp” while monitoring /home/joe/bigfile
when user joe
executes chmod 777 /home/joe/bigfile and chmod 666 /home/joe/bigfile.
例 4.20. 例 4.19 “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