#define DISKSTATS "/proc/diskstats"
By accessing the diskstats virtual file, get the IO read and write data for the current period, and then count
void Read_diskstats_stat (int curr)
{
FILE *FP;
Char line[256], Dev_name[max_name_len];
Char *dm_name;
struct Io_stats Sdev;
int i;
unsigned int ios_pgr, tot_ticks, Rq_ticks, wr_ticks;
unsigned long Rd_ios, rd_merges_or_rd_sec, Rd_ticks_or_wr_sec, Wr_ios;
unsigned long wr_merges, Rd_sec_or_wr_ios, wr_sec;
Char *ioc_dname;
unsigned int major, minor;
/* Every I/O device entry is potentially unregistered */
Set_entries_unregistered (IODEV_NR, St_hdr_iodev);
if (fp = fopen (Diskstats, "R")) = = = NULL)
Return
while (fgets (line, sizeof), fp)! = NULL) {
/* Major Minor name Rio Rmerge rsect Ruse wio wmerge wsect Wuse running use AVEQ */
i = sscanf (line, "%u%u%s%lu%lu%lu%lu%lu%lu%lu%u%u%u",
&major, &minor, Dev_name,
&rd_ios, &rd_merges_or_rd_sec, &rd_sec_or_wr_ios, &rd_ticks_or_wr_sec,
&wr_ios, &wr_merges, &wr_sec, &wr_ticks, &IOS_PGR, &tot_ticks, &rq_ticks);
if (i = = 14) {
/* Device or partition */
if (!dlist_idx &&!) Display_partitions (Flags) &&
!is_device (Dev_name, accept_virtual_devices))
Continue
Sdev.rd_ios = Rd_ios;
Sdev.rd_merges = rd_merges_or_rd_sec;
Sdev.rd_sectors = Rd_sec_or_wr_ios;
Sdev.rd_ticks = (unsigned int) rd_ticks_or_wr_sec;
Sdev.wr_ios = Wr_ios;
Sdev.wr_merges = wr_merges;
Sdev.wr_sectors = wr_sec;
Sdev.wr_ticks = Wr_ticks;
SDEV.IOS_PGR = IOS_PGR;
Sdev.tot_ticks = Tot_ticks;
Sdev.rq_ticks = Rq_ticks;
}
else if (i = = 7) {
/* Partition without extended statistics */
if (display_extended (flags) | |
(!dlist_idx &&!) Display_partitions (flags)))
Continue
Sdev.rd_ios = Rd_ios;
Sdev.rd_sectors = rd_merges_or_rd_sec;
Sdev.wr_ios = Rd_sec_or_wr_ios;
Sdev.wr_sectors = rd_ticks_or_wr_sec;
}
Else
/* Unknown Entry:ignore It */
Continue
if ((Ioc_dname = ioc_name (Major, minor)) != null) {
if (strcmp (dev_name, ioc_dname) && strcmp (ioc_dname, k_ Nodev)) {
/*
* no match: use name generated from sysstat.ioconf data
* (if different from "Nodev") works around known issues
* with emc powerpath.
*/
strncpy (dev_name, ioc_dname, max_ Name_len);
}
}
if (Display_devmap_name (Flags)) && (major = = Dm_major)) {
/*
* If The device is a device mapper device, try-to-get its
* Assigned name of its logical device.
*/
Dm_name = Transform_devmapname (major, minor);
if (dm_name) {
strncpy (Dev_name, Dm_name, Max_name_len);
}
}
Save_stats (Dev_name, Curr, &sdev, IODEV_NR, St_hdr_iodev);
}
Fclose (FP);
/* free structures corresponding to unregistered devices */
Free_unregistered_entries (IODEV_NR, St_hdr_iodev);
}