Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix build failure on Windows #2

Merged
merged 2 commits into from
Feb 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions src/io.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;

/// IOLoad represents current system block devices IO statistics
#[derive(Debug)]
Expand Down Expand Up @@ -45,24 +43,30 @@ impl IOLoad {
///
/// # Notes
///
/// Current don't support non-unix operating system
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(not(unix))]
pub fn snapshot() -> HashMap<String, NICLoad> {
pub fn snapshot() -> HashMap<String, Self> {
HashMap::new()
}

/// Returns the current IO statistics
///
/// # Notes
///
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(unix)]
pub fn snapshot() -> HashMap<String, IOLoad> {
pub fn snapshot() -> HashMap<String, Self> {
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 {
if let Ok(entry) = entry {
let stat = entry.path().join("stat");
let mut s = String::new();
if let Err(_) = File::open(stat).and_then(|mut f| f.read_to_string(&mut s)) {
continue;
let s = match std::fs::read_to_string(stat) {
Ok(s) => s,
Err(_) => continue,
};
let parts = s
.split_whitespace()
Expand All @@ -71,7 +75,7 @@ impl IOLoad {
if parts.len() != 11 {
continue;
}
let load = IOLoad {
let load = Self {
read_io: parts[0],
read_merges: parts[1],
read_sectors: parts[2],
Expand Down
14 changes: 10 additions & 4 deletions src/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ impl NICLoad {
///
/// # Notes
///
/// Current don't support non-unix operating system
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(not(unix))]
pub fn snapshot() -> HashMap<String, NICLoad> {
pub fn snapshot() -> HashMap<String, Self> {
HashMap::new()
}

/// Returns the current network interfaces card statistics
///
/// # Notes
///
/// Currently not supported outside Unix. On those operating systems, this
/// method always returns an empty map.
#[cfg(unix)]
pub fn snapshot() -> HashMap<String, NICLoad> {
pub fn snapshot() -> HashMap<String, Self> {
let mut result = HashMap::new();
if let Ok(dir) = std::fs::read_dir("/sys/class/net/") {
for entry in dir {
Expand All @@ -53,7 +59,7 @@ impl NICLoad {
.parse()
.unwrap_or_default()
};
let load = NICLoad {
let load = Self {
rx_bytes: read("rx_bytes"),
tx_bytes: read("tx_bytes"),
rx_packets: read("rx_packets"),
Expand Down
1 change: 1 addition & 0 deletions src/windows/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ pub fn get_key_used(p: &mut Processor) -> &mut Option<KeyHandler> {
&mut p.key_used
}

/// get_cpu_frequency returns the CPU frequency in MHz
pub fn get_cpu_frequency() -> u64 {
// TODO: support windows
0
Expand Down