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 boottime and proc_total issues on windows #48

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: Test

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
linux:
name: Linux ubuntu-latest
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Build
run: cargo build --verbose
- name: Run test
run: cargo test --verbose

windows:
name: Windows ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [windows-10-LTSC-amd64-17763, windows-latest]
steps:
- uses: actions/checkout@v1
- name: Build
run: cargo build --verbose
- name: Run test
run: cargo test --verbose

macos:
name: macOS-latest
runs-on: macOS-latest
steps:
- uses: actions/checkout@v1
- name: Get Rust
run: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs > rustup && sh ./rustup -y
- name: Build
run: source ~/.cargo/env; cargo build --verbose
- name: Run test
run: source ~/.cargo/env; cargo test --verbose
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ os:
- osx
language: rust
rust:
- stable
- beta
- nightly
-stable
-beta
-nightly
cache: rust
script:
- cargo test --all --verbose
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ cc = "1"

[dependencies]
libc = "0.2.29"

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3"
features = ["sysinfoapi"]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ and add this to crate root:
extern crate sys_info;
```

for more usage example, see the source file in test folder.
38 changes: 31 additions & 7 deletions lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
//! And now it can get information of kernel/cpu/memory/disk/load/hostname and so on.
//!

#[cfg(windows)]
extern crate winapi;
extern crate libc;

use std::ffi;
use std::fmt;
use std::io::{self, Read};
use std::fs::File;
use std::os::raw::c_char;
#[cfg(target_os = "windows")]
use winapi::um::sysinfoapi;

#[cfg(target_os = "macos")]
use libc::sysctl;
Expand All @@ -22,6 +25,7 @@ use std::ptr::null_mut;
use libc::timeval;

use std::collections::HashMap;
use std::convert::TryInto;

#[cfg(target_os = "macos")]
static MAC_CTL_KERN: libc::c_int = 1;
Expand Down Expand Up @@ -148,7 +152,7 @@ extern "C" {
fn get_cpu_speed() -> u64;

fn get_loadavg() -> LoadAvg;
fn get_proc_total() -> u64;
fn get_proc_total() -> usize;

fn get_mem_info() -> MemInfo;
fn get_disk_info() -> DiskInfo;
Expand Down Expand Up @@ -314,16 +318,14 @@ pub fn loadavg() -> Result<LoadAvg, Error> {
}

/// Get current processes quantity.
///
/// Notice, it temporarily does not support Windows.
pub fn proc_total() -> Result<u64, Error> {
pub fn proc_total() -> Result<usize, Error> {
if cfg!(target_os = "linux") {
let mut s = String::new();
File::open("/proc/loadavg")?.read_to_string(&mut s)?;
s.split(' ')
.nth(3)
.and_then(|val| val.split('/').last())
.and_then(|val| val.parse::<u64>().ok())
.and_then(|val| val.parse::<usize>().ok())
.ok_or(Error::Unknown)
} else if cfg!(target_os = "macos") || cfg!(target_os = "windows") {
Ok(unsafe { get_proc_total() })
Expand Down Expand Up @@ -413,7 +415,8 @@ pub fn hostname() -> Result<String, Error> {
.map(|output| String::from_utf8(output.stdout).unwrap().trim().to_string())
}

/// Get system boottime

/// Get system boottime from unix like system
#[cfg(not(windows))]
pub fn boottime() -> Result<timeval, Error> {
let mut bt = timeval {
Expand Down Expand Up @@ -446,6 +449,20 @@ pub fn boottime() -> Result<timeval, Error> {
Ok(bt)
}

// get boottime from windows, the tv_usec is in microsecond = 1000 * millisecond, i just simple set it to 0
// if you need this value, you can just simply multiply since_boot by 1000...
#[cfg(windows)]
pub fn boottime() -> Result<timeval, Error> {
let mut bt = timeval {
tv_sec: 0,
tv_usec: 0
};

let since_boot: u64 = unsafe { sysinfoapi::GetTickCount64() };
bt.tv_sec = (since_boot / 1000).try_into().unwrap();
Ok(bt)
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -518,6 +535,13 @@ mod test {
println!("boottime(): {} {}", bt.tv_sec, bt.tv_usec);
assert!(bt.tv_sec > 0 || bt.tv_usec > 0);
}

#[test]
#[cfg(windows)]
pub fn test_boottime(){
let bt = boottime().unwrap();
assert!(bt.tv_sec > 0);
}

#[test]
#[cfg(linux)]
Expand Down
2 changes: 2 additions & 0 deletions test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn main() {

println!("os: {} {}", os_type().unwrap(), os_release().unwrap());
println!("cpu: {} cores, {} MHz", cpu_num().unwrap(), cpu_speed().unwrap());
//the proc total will work correctly on windows-amd64
println!("proc total: {}", proc_total().unwrap());
let load = loadavg().unwrap();
println!("load: {} {} {}", load.one, load.five, load.fifteen);
Expand All @@ -17,6 +18,7 @@ fn main() {
let disk = disk_info().unwrap();
println!("disk: total {} KB, free {} KB", disk.total, disk.free);
println!("hostname: {}", hostname().unwrap());
//now boottime works perfectly on windows-amd64 and linux-amd64
let t = boottime().unwrap();
println!("boottime {} sec, {} usec", t.tv_sec, t.tv_usec);

Expand Down