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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! Standard APIs in Linux

use std::fmt;
use std::ops;
use std::time::Duration;

use libc::{_SC_CLK_TCK, _SC_PAGESIZE};
use libc::sysconf;

lazy_static!(
    pub static ref USER_HZ: u64 = unsafe { sysconf(_SC_CLK_TCK) } as u64;
    pub static ref PAGESIZE: u64 = unsafe { sysconf(_SC_PAGESIZE) } as u64;
);

pub fn pages_to_human_size(pages: u64) -> String {
    let bytes = pages * (*PAGESIZE);
    bytes_to_human_size(bytes)
}

/// Convert a large number to something smaller with a suffix
///
/// ```
/// let human = tabin_plugins::linux::bytes_to_human_size(9999);
/// assert_eq!(human, "9.8K");
/// ```
pub fn bytes_to_human_size(bytes: u64) -> String {
    let mut bytes = bytes as f64;
    let sizes = ["", "K", "M", "G", "T", "P", "E", "Z", "Y"];
    let mut reductions = 0;
    while reductions < sizes.len() - 1 {
        if bytes > 1000.0 {
            bytes = bytes / 1024.0;
            reductions += 1;
        } else {
            break;
        }
    }
    format!("{:.1}{}", bytes, sizes[reductions])
}

#[test]
fn bytes_to_human_size_produces_shortest() {
    let reprs = [
        (999, "999.0"),
        (9_999, "9.8K"),
        (9_999_999, "9.5M"),
        (35_999_999, "34.3M"),
        (9_999_999_999, "9.3G"),
        (9_999_999_999_999, "9.1T"),
        (90_999_999_999_999_999, "80.8P"),
    ];

    reprs
        .iter()
        .map(|&(raw, repr): &(u64, &str)| assert_eq!(bytes_to_human_size(raw), repr))
        .collect::<Vec<_>>();
}

/// A value that is in USER_HZ units
///
/// This generally represents some time period of CPU usage
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct UserHz(u64);

impl UserHz {
    pub fn new(val: u64) -> UserHz {
        UserHz(val)
    }

    pub fn duration(&self) -> Duration {
        Duration::from_millis(self.0 * 1000)
    }
}

impl fmt::Display for UserHz {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "UserHz({})", self.0)
    }
}

impl ops::Add for UserHz {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        UserHz(self.0 + rhs.0)
    }
}

impl ops::Sub for UserHz {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        UserHz(self.0 - rhs.0)
    }
}

/// A count of Jiffies
///
/// This represents some value read from the /proc fs
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Jiffies(u64);

impl Jiffies {
    pub fn new(val: u64) -> Jiffies {
        Jiffies(val)
    }

    pub fn duration(&self) -> Duration {
        Duration::from_millis(self.0 * 1000 / *USER_HZ)
    }
}

impl From<u64> for Jiffies {
    fn from(u: u64) -> Jiffies {
        Jiffies::new(u)
    }
}

impl From<i64> for Jiffies {
    fn from(u: i64) -> Jiffies {
        Jiffies::new(u as u64)
    }
}

impl fmt::Display for Jiffies {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Jiffies({})", self.0)
    }
}

impl ops::Add for Jiffies {
    type Output = Self;
    fn add(self, rhs: Self) -> Self::Output {
        Jiffies(self.0 + rhs.0)
    }
}

impl ops::Sub for Jiffies {
    type Output = Self;
    fn sub(self, rhs: Self) -> Self::Output {
        Jiffies(self.0 - rhs.0)
    }
}

impl ops::Div for Jiffies {
    type Output = f64;
    fn div(self, rhs: Self) -> f64 {
        self.0 as f64 / rhs.0 as f64
    }
}

impl ops::Div<Jiffies> for u64 {
    type Output = f64;
    fn div(self, rhs: Jiffies) -> Self::Output {
        self as f64 / rhs.0 as f64
    }
}

impl ops::Mul<Jiffies> for u64 {
    type Output = Self;
    fn mul(self, rhs: Jiffies) -> Self::Output {
        self * rhs.0
    }
}

pub trait Ratio<Rhs = Self> {
    fn ratio(&self, rhs: &Rhs) -> f64;
}

impl Ratio for Duration {
    fn ratio(&self, rhs: &Self) -> f64 {
        let my_nanos = self.as_secs() * 1_000_000_000 + self.subsec_nanos() as u64;
        let rhs_nanos = rhs.as_secs() * 1_000_000_000 + rhs.subsec_nanos() as u64;
        my_nanos as f64 / rhs_nanos as f64
    }
}