Skip to content

Commit

Permalink
Add system cargo feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Jun 23, 2024
1 parent 8a34c88 commit bfd10db
Show file tree
Hide file tree
Showing 38 changed files with 792 additions and 659 deletions.
43 changes: 24 additions & 19 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,27 @@ edition = "2021"
name = "sysinfo"

[features]
default = ["multithread"]
default = ["system", "multithread"]
system = [
"windows/Wdk_System_SystemInformation",
"windows/Wdk_System_SystemServices",
"windows/Wdk_System_Threading",
"windows/Win32_System_Diagnostics_Debug",
"windows/Win32_System_Kernel",
"windows/Win32_System_Memory",
"windows/Win32_System_Performance",
"windows/Win32_System_Power",
"windows/Win32_System_ProcessStatus",
"windows/Win32_System_Registry",
"windows/Win32_System_RemoteDesktop",
"windows/Win32_System_SystemInformation",
"windows/Win32_System_SystemServices",
"windows/Win32_System_Threading",
"windows/Win32_UI_Shell",
]
apple-sandbox = []
apple-app-store = ["apple-sandbox"]
c-interface = []
c-interface = ["default"]
multithread = ["dep:rayon"]
linux-netdevs = []
linux-tmpfs = []
Expand Down Expand Up @@ -50,9 +67,6 @@ serde = { version = "^1.0.190", optional = true }
[target.'cfg(windows)'.dependencies]
ntapi = "0.4"
windows = { version = "0.56", features = [
"Wdk_System_SystemInformation",
"Wdk_System_SystemServices",
"Wdk_System_Threading",
"Win32_Foundation",
"Win32_NetworkManagement_IpHelper",
"Win32_NetworkManagement_NetManagement",
Expand All @@ -63,26 +77,12 @@ windows = { version = "0.56", features = [
"Win32_Security_Authorization",
"Win32_Storage_FileSystem",
"Win32_System_Com",
"Win32_System_Diagnostics_Debug",
"Win32_System_IO",
"Win32_System_Ioctl",
"Win32_System_LibraryLoader",
"Win32_System_Kernel",
"Win32_System_Memory",
"Win32_System_Ole",
"Win32_System_Performance",
"Win32_System_Power",
"Win32_System_ProcessStatus",
"Win32_System_Registry",
"Win32_System_RemoteDesktop",
"Win32_System_Rpc",
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Variant",
"Win32_System_WindowsProgramming",
"Win32_System_Wmi",
"Win32_UI_Shell",
]}

[target.'cfg(not(any(target_os = "unknown", target_arch = "wasm32")))'.dependencies]
Expand All @@ -96,3 +96,8 @@ tempfile = "3.9"

[dev-dependencies]
serde_json = "1.0" # Used in documentation tests.

[[example]]
name = "simple"
path = "examples/simple.rs"
required-features = ["default"]
12 changes: 9 additions & 3 deletions benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@

extern crate test;

use sysinfo::get_current_pid;

#[cfg(feature = "system")]
#[bench]
fn bench_new(b: &mut test::Bencher) {
b.iter(|| {
sysinfo::System::new();
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_new_all(b: &mut test::Bencher) {
b.iter(|| {
sysinfo::System::new_all();
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_refresh_all(b: &mut test::Bencher) {
let mut s = sysinfo::System::new_all();
Expand All @@ -27,6 +28,7 @@ fn bench_refresh_all(b: &mut test::Bencher) {
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_refresh_processes(b: &mut test::Bencher) {
let mut s = sysinfo::System::new();
Expand All @@ -37,6 +39,7 @@ fn bench_refresh_processes(b: &mut test::Bencher) {
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_first_refresh_processes(b: &mut test::Bencher) {
b.iter(move || {
Expand All @@ -45,13 +48,14 @@ fn bench_first_refresh_processes(b: &mut test::Bencher) {
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_refresh_process(b: &mut test::Bencher) {
let mut s = sysinfo::System::new();

s.refresh_all();
// to be sure it'll exist for at least as long as we run
let pid = get_current_pid().expect("failed to get current pid");
let pid = sysinfo::get_current_pid().expect("failed to get current pid");
b.iter(move || {
s.refresh_process(pid);
});
Expand Down Expand Up @@ -104,6 +108,7 @@ fn bench_refresh_networks_list(b: &mut test::Bencher) {
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_refresh_memory(b: &mut test::Bencher) {
let mut s = sysinfo::System::new();
Expand All @@ -113,6 +118,7 @@ fn bench_refresh_memory(b: &mut test::Bencher) {
});
}

#[cfg(feature = "system")]
#[bench]
fn bench_refresh_cpu_usage(b: &mut test::Bencher) {
let mut s = sysinfo::System::new();
Expand Down
1 change: 1 addition & 0 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub(crate) mod component;
pub(crate) mod disk;
pub(crate) mod network;
#[cfg(feature = "system")]
pub(crate) mod system;
pub(crate) mod user;

Expand Down
73 changes: 73 additions & 0 deletions src/common/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,7 @@ impl Cpu {
#[cfg(test)]
mod test {
use crate::*;
use std::str::FromStr;

// In case `Process::updated` is misused, `System::refresh_processes` might remove them
// so this test ensures that it doesn't happen.
Expand Down Expand Up @@ -2680,4 +2681,76 @@ mod test {
fn check_display_impl_process_status() {
println!("{} {:?}", ProcessStatus::Parked, ProcessStatus::Idle);
}

#[test]
#[allow(clippy::unnecessary_fallible_conversions)]
fn check_pid_from_impls() {
assert!(crate::Pid::try_from(0usize).is_ok());
// If it doesn't panic, it's fine.
let _ = crate::Pid::from(0);
assert!(crate::Pid::from_str("0").is_ok());
}

#[test]
#[allow(clippy::const_is_empty)]
fn check_nb_supported_signals() {
if IS_SUPPORTED_SYSTEM {
assert!(
!SUPPORTED_SIGNALS.is_empty(),
"SUPPORTED_SIGNALS shouldn't be empty on supported systems!"
);
} else {
assert!(
SUPPORTED_SIGNALS.is_empty(),
"SUPPORTED_SIGNALS should be empty on not support systems!"
);
}
}
}

#[cfg(doctest)]
mod doctest {
// FIXME: Can be removed once negative trait bounds are supported.
/// Check that `Process` doesn't implement `Clone`.
///
/// First we check that the "basic" code works:
///
/// ```no_run
/// use sysinfo::{Process, System};
///
/// let mut s = System::new_all();
/// let p: &Process = s.processes().values().next().unwrap();
/// ```
///
/// And now we check if it fails when we try to clone it:
///
/// ```compile_fail
/// use sysinfo::{Process, System};
///
/// let mut s = System::new_all();
/// let p: &Process = s.processes().values().next().unwrap();
/// let p = (*p).clone();
/// ```
mod process_clone {}

// FIXME: Can be removed once negative trait bounds are supported.
/// Check that `System` doesn't implement `Clone`.
///
/// First we check that the "basic" code works:
///
/// ```no_run
/// use sysinfo::{Process, System};
///
/// let s = System::new();
/// ```
///
/// And now we check if it fails when we try to clone it:
///
/// ```compile_fail
/// use sysinfo::{Process, System};
///
/// let s = System::new();
/// let s = s.clone();
/// ```
mod system_clone {}
}
8 changes: 2 additions & 6 deletions src/common/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,8 @@ impl Users {
///
/// Full example:
///
/// ```no_run
#[cfg_attr(feature = "system", doc = "```no_run")]
#[cfg_attr(not(feature = "system"), doc = "```ignore")]
/// use sysinfo::{Pid, System, Users};
///
/// let mut s = System::new_all();
Expand Down Expand Up @@ -523,11 +524,6 @@ mod tests {

assert!(crate::Gid::try_from(0usize).is_ok());
assert!(crate::Gid::from_str("0").is_ok());

assert!(crate::Pid::try_from(0usize).is_ok());
// If it doesn't panic, it's fine.
let _ = crate::Pid::from(0);
assert!(crate::Pid::from_str("0").is_ok());
}

#[test]
Expand Down
13 changes: 7 additions & 6 deletions src/debug.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{
Component, Components, Cpu, Disk, Disks, NetworkData, Networks, Process, System, User, Users,
};
use crate::{Component, Components, Disk, Disks, NetworkData, Networks, User, Users};

use std::fmt;

impl fmt::Debug for Cpu {
#[cfg(feature = "system")]
impl fmt::Debug for crate::Cpu {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Cpu")
.field("name", &self.name())
Expand All @@ -18,7 +17,8 @@ impl fmt::Debug for Cpu {
}
}

impl fmt::Debug for System {
#[cfg(feature = "system")]
impl fmt::Debug for crate::System {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("System")
.field("global CPU usage", &self.global_cpu_usage())
Expand Down Expand Up @@ -49,7 +49,8 @@ impl fmt::Debug for Disk {
}
}

impl fmt::Debug for Process {
#[cfg(feature = "system")]
impl fmt::Debug for crate::Process {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Process")
.field("pid", &self.pid())
Expand Down
Loading

0 comments on commit bfd10db

Please sign in to comment.