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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#[macro_use]
extern crate log;
extern crate serde;
#[macro_use]
extern crate serde_derive;

use std::fs;
use std::io;
use std::io::{Error, ErrorKind, Write};
use std::path::PathBuf;

macro_rules! parse_num {
    ($g:ident, $op:expr) => {{
        let mut chars = fs::read_to_string($g.join($op))?;
        chars.retain(|c| c.is_digit(10));

        match chars.parse() {
            Ok(x) => x,
            Err(e) => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("Cause{}", e),
                ));
            }
        }
    }};
}

/// find relevant sysfs folders in /sys/devices/system/cpu/cpu<x>
pub fn discover_core_settings() -> io::Result<Vec<Core>> {
    let cpu_root = fs::read_dir("/sys/devices/system/cpu/")?;
    debug!("Content of /sys/devices/system/cpu/  {:#?}", cpu_root);

    let is_core = |p: &fs::DirEntry| {
        let f = p
            .file_name()
            .into_string()
            .expect("Encountered invalid path while discovering core directories");
        f.contains("cpu") && !(f.contains("cpuidle") || f.contains("cpufreq"))
    };

    cpu_root
        // WARN: error cases described by read dir seem unrealistic at first so we're gonna ignore them
        .filter_map(|e| e.ok())
        .filter(|p| is_core(p))
        .map(|p| p.path())
        .inspect(|c| debug!("Found core: {:?}", c))
        .try_fold(Vec::new(), |mut cores, c| {
            let c = Core::discover(c)?;
            cores.push(c);
            Ok(cores)
        })
}

/// Representation of the current cpufrequency serttings of a single core
/// Can be obtained by running [discover_core_settings]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Core {
    /// Path to the core directory
    core: PathBuf,
    /// Number of the core
    num: u32,
    /// CPU Minimum Frequency
    cpuinfo_max_freq: u32,
    /// CPU Maximum Frequency
    cpuinfo_min_freq: u32,
    /// List of possible values for the governor
    scaling_available_governors: Vec<String>,
    /// Current upper frequency limit - the governor may increase frequency up to this value
    scaling_max_freq: u32,
    /// Current lower frequency limit - the governor may reduce the frequency down to this value
    scaling_min_freq: u32,
    /// Currently set scaling governor
    scaling_governor: String,
}

impl Core {
    /// discover settings for the core specified by its path
    pub fn discover(core: PathBuf) -> io::Result<Core> {
        let g = core.join("cpufreq");

        let cpuinfo_min_freq: u32 = parse_num!(g, "cpuinfo_min_freq");
        let cpuinfo_max_freq: u32 = parse_num!(g, "cpuinfo_max_freq");
        let scaling_min_freq: u32 = parse_num!(g, "scaling_min_freq");
        let scaling_max_freq: u32 = parse_num!(g, "scaling_max_freq");
        let scaling_governor = {
            let mut chars = fs::read_to_string(g.join("scaling_governor"))?;
            chars.retain(|c| !c.is_control());
            chars
        };

        let scaling_available_governors = {
            let mut chars = fs::read_to_string(g.join("scaling_available_governors"))?;
            chars.retain(|c| !c.is_control());
            chars.split(&" ").map(|s| s.into()).collect()
        };

        // parse the number
        let num = core
            .to_str()
            .expect("Failed to convert PathBuf to String")
            .rsplit("cpu")
            .next()
            .expect("Core name did not contain 'cpu'")
            .parse()
            .expect("Failed to parse the u32 core number");

        let c = Core {
            core,
            num,
            cpuinfo_max_freq,
            cpuinfo_min_freq,
            scaling_available_governors,
            scaling_governor,
            scaling_min_freq,
            scaling_max_freq,
        };
        debug!("Read settings : {:#?}", c);

        Ok(c)
    }

    /// returns the number of the core
    pub fn num(&self) -> u32 {
        self.num
    }

    /// returns cpu minimum frequency in kHz
    pub fn cpu_min(&self) -> u32 {
        self.cpuinfo_min_freq
    }

    /// returns cpu maximum frequency in kHz
    pub fn cpu_max(&self) -> u32 {
        self.cpuinfo_max_freq
    }

    /// returns current min scaling frequency in kHz
    pub fn curr_min(&self) -> u32 {
        self.scaling_min_freq
    }

    /// returns current max scaling frequency in kHz
    pub fn curr_max(&self) -> u32 {
        self.scaling_max_freq
    }

    /// returns the current governor
    pub fn curr_gov(&self) -> &str {
        self.scaling_governor.as_ref()
    }

    /// returns available governors
    pub fn available_govs(&self) -> &[String] {
        self.scaling_available_governors.as_ref()
    }

    /// Validate the given minimum value. Must be >= the discovered CPU frequency minimum
    pub fn validate_min(&self, freq: u32) -> io::Result<u32> {
        if self.cpuinfo_min_freq <= freq && freq <= self.scaling_max_freq {
            Ok(freq)
        } else {
            Err(Error::new(
                ErrorKind::InvalidInput,
                format!(
                    "Min Frequency {} not in ({},{}]",
                    freq, self.cpuinfo_min_freq, self.scaling_max_freq
                ),
            ))
        }
    }

    /// Validate the given maximum value. Must be >= current min and <= CPU frequency maximum
    pub fn validate_max(&self, freq: u32) -> io::Result<u32> {
        if (self.cpuinfo_min_freq <= freq && self.scaling_min_freq <= freq)
            && freq <= self.cpuinfo_max_freq
        {
            Ok(freq)
        } else {
            Err(Error::new(
                ErrorKind::InvalidInput,
                format!(
                    "Max Frequency {} not in min({},{})..={}",
                    freq, self.cpuinfo_min_freq, self.scaling_min_freq, self.cpuinfo_max_freq
                ),
            ))
        }
    }

    /// Validate the governor by checking against the list of available governors
    pub fn validate_governor<'a>(&self, governor: &'a str) -> io::Result<&'a str> {
        if self
            .scaling_available_governors
            .iter()
            .any(|g| g.as_str().eq(governor))
        {
            Ok(governor)
        } else {
            Err(Error::new(
                ErrorKind::InvalidInput,
                format!(
                    "Governor {} not available. Must be one of {:?}",
                    governor, self.scaling_available_governors
                ),
            ))
        }
    }

    /// Set the minimum scaling frequency (lower frequency limit)
    /// This operation is not checked by mediocore, but the kernel may refuse to accept certain inputs.  
    /// Use [Core::validate_min] on the value beforehand.
    pub fn set_min(&mut self, freq: u32) -> io::Result<()> {
        debug!("Setting minimum scaling frequency {} on {}", freq, self.num);
        let mut f = fs::OpenOptions::new()
            .write(true)
            .open(self.core.join("cpufreq/scaling_min_freq"))?;
        f.write_all(format!("{}", freq).as_ref())?;
        Ok(())
    }

    /// Set the maximum scaling frequency (lower frequency limit)  
    /// This operation is not checked by mediocore, but the kernel may refuse to accept certain inputs.  
    /// Use [Core::validate_max] on the value beforehand.
    pub fn set_max(&mut self, freq: u32) -> io::Result<()> {
        debug!("Setting maximum scaling frequency {} on {}", freq, self.num);
        let mut f = fs::OpenOptions::new()
            .write(true)
            .open(self.core.join("cpufreq/scaling_max_freq"))?;
        f.write_all(format!("{}", freq).as_ref())?;
        Ok(())
    }

    /// Apply the given governor
    /// This operation is not checked by mediocore, but the kernel may refuse to accept certain inputs.
    /// Use [Core::validate_governor] on the value beforehand.
    pub fn set_governor(&mut self, guvnor: &str) -> io::Result<()> {
        debug!("Setting governor {} on {}", guvnor, self.num);
        fs::OpenOptions::new()
            .write(true)
            .read(false)
            .open(self.core.join("cpufreq/scaling_governor"))
            .and_then(|mut f| f.write_all(guvnor.as_bytes()))
            .map(|_| ())
    }
}

#[cfg(test)]
mod test {
    use io::{ErrorKind, Result};
    use std::path::PathBuf;
    use Core;

    #[test]
    fn freq_validation() {
        let s = Core {
            core: PathBuf::from("/sys/devices/system/cpu/cpu0"),
            num: 0,
            cpuinfo_min_freq: 800000,
            cpuinfo_max_freq: 2500000,
            scaling_available_governors: vec![],
            scaling_governor: "".into(),
            scaling_min_freq: 850000,
            scaling_max_freq: 900000,
        };

        let check_val = |x, v| match x {
            Ok(u) => v == u,
            Err(_) => false,
        };

        let check_err = |x: Result<u32>| match x {
            Ok(_) => false,
            Err(ref e) if e.kind() == ErrorKind::InvalidInput => true,
            _ => false,
        };

        assert!(check_val(s.validate_min(800000), 800000));
        assert!(check_val(s.validate_min(850000), 850000));
        assert!(check_err(s.validate_min(1000000)));

        assert!(check_val(s.validate_max(1000000), 1000000));
        assert!(check_err(s.validate_max(8000000)));
    }

    #[test]
    fn govnor_validation() {
        let s = Core {
            core: PathBuf::from(&"/sys/devices/system/cpu/cpu0"),
            num: 0,
            cpuinfo_min_freq: 800000,
            cpuinfo_max_freq: 2500000,
            scaling_available_governors: vec!["performance".into(), "powersave".into()],
            scaling_governor: "powersave".into(),
            scaling_min_freq: 850000,
            scaling_max_freq: 900000,
        };

        assert!(s.validate_governor(&"performance").is_ok());
        assert!(s.validate_governor(&"conservative").is_err());
    }

}