1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.

use std::collections::HashMap;
use std::fs::File;
use std::io::Read;

/// IoLoad represents current system block devices IO statistics
#[derive(Debug)]
pub struct IoLoad {
    /// number of read I/Os processed
    /// units: requests
    pub read_io: f64,
    /// number of read I/Os merged with in-queue I/O
    /// units: requests
    pub read_merges: f64,
    /// number of sectors read
    /// units: sectors
    pub read_sectors: f64,
    /// total wait time for read requests
    /// units: milliseconds
    pub read_ticks: f64,
    /// number of write I/Os processed
    /// units: requests
    pub write_io: f64,
    /// number of write I/Os merged with in-queue I/O
    /// units: requests
    pub write_merges: f64,
    /// number of sectors written
    /// units: sectors
    pub write_sectors: f64,
    /// total wait time for write requests
    /// units: milliseconds
    pub write_ticks: f64,
    /// number of I/Os currently in flight
    /// units: requests
    pub in_flight: f64,
    /// total time this block device has been active
    /// units: milliseconds
    pub io_ticks: f64,
    /// total wait time for all requests
    /// units: milliseconds
    pub time_in_queue: f64,
    /// number of discard I/Os processed
    /// units: requests
    pub discard_io: Option<f64>,
    /// number of discard I/Os merged with in-queue I/O
    /// units: requests
    pub discard_merged: Option<f64>,
    /// number of sectors discarded
    /// units: sectors
    pub discard_sectors: Option<f64>,
    /// total wait time for discard requests
    /// units: milliseconds
    pub discard_ticks: Option<f64>,
}

impl IoLoad {
    /// Returns the current IO statistics
    ///
    /// # Notes
    ///
    /// Current don't support non-unix operating system
    #[cfg(not(unix))]
    pub fn snapshot() -> HashMap<String, NICLoad> {
        HashMap::new()
    }

    /// Returns the current IO statistics
    #[cfg(unix)]
    pub fn snapshot() -> HashMap<String, IoLoad> {
        let mut result = HashMap::new();
        // https://www.kernel.org/doc/Documentation/block/stat.txt
        if let Ok(dir) = std::fs::read_dir("/sys/block/") {
            for entry in dir.flatten() {
                let stat = entry.path().join("stat");
                let mut s = String::new();
                if File::open(stat)
                    .and_then(|mut f| f.read_to_string(&mut s))
                    .is_err()
                {
                    continue;
                }
                let parts = s
                    .split_whitespace()
                    .map(|w| w.parse().unwrap_or_default())
                    .collect::<Vec<f64>>();
                // A not too old Linux kernel supports the first 11 block statistics.
                // Others stats are supported by Linux 4.19+, we consider them as optional ones.
                if parts.len() < 11 {
                    continue;
                }
                let load = IoLoad {
                    read_io: parts[0],
                    read_merges: parts[1],
                    read_sectors: parts[2],
                    read_ticks: parts[3],
                    write_io: parts[4],
                    write_merges: parts[5],
                    write_sectors: parts[6],
                    write_ticks: parts[7],
                    in_flight: parts[8],
                    io_ticks: parts[9],
                    time_in_queue: parts[10],
                    discard_io: parts.get(11).cloned(),
                    discard_merged: parts.get(12).cloned(),
                    discard_sectors: parts.get(13).cloned(),
                    discard_ticks: parts.get(14).cloned(),
                };
                result.insert(format!("{:?}", entry.file_name()), load);
            }
        }
        result
    }
}