# sysinfo-linux Usage Guide
This is a Linux-only fork of the popular `sysinfo` crate, refactored to support only Linux systems.
## Installation
### Method 1: Add to your Cargo.toml from GitHub
Add this to your `Cargo.toml`:
```toml
[dependencies]
sysinfo = { git = "https://github.com/YOUR_USERNAME/sysinfo-linux.git" }
```
### Method 2: Use from local path
If you've cloned the repository locally:
```toml
[dependencies]
sysinfo = { path = "/path/to/sysinfo-linux" }
```
### Method 3: Clone and build
```bash
# Clone the repository
git clone https://github.com/YOUR_USERNAME/sysinfo-linux.git
cd sysinfo-linux
# Build the library
cargo build --release
# Run tests
cargo test
# Build the example
cargo run --example simple
```
## Features
The crate supports the following features (all enabled by default):
- `component` - Temperature sensors and hardware components
- `disk` - Disk information and usage
- `gpu` - GPU information
- `network` - Network interfaces and statistics
- `system` - System, CPU, and process information
- `user` - User and group information
Optional features:
- `c-interface` - C FFI interface
- `multithread` - Parallel processing using rayon
- `linux-netdevs` - Additional network device information
- `linux-tmpfs` - Include tmpfs in disk list
- `debug` - Extra debugging information
- `serde` - Serialization/deserialization support
### Using specific features only
If you only need specific functionality (e.g., only CPU and memory info):
```toml
[dependencies]
sysinfo = { git = "https://github.com/YOUR_USERNAME/sysinfo-linux.git", default-features = false, features = ["system"] }
```
## Quick Start
### Basic Example
```rust
use sysinfo::System;
fn main() {
// Create a new system instance
let mut sys = System::new_all().expect("Failed to create System");
// Display system information
println!("System name: {:?}", System::name());
println!("Total memory: {} MB", sys.total_memory() / 1024 / 1024);
println!("CPUs: {}", sys.cpus().len());
}
```
### Memory Information
```rust
use sysinfo::System;
fn main() {
let sys = System::new_all().expect("Failed to create System");
println!("Total memory: {} MB", sys.total_memory() / 1024 / 1024);
println!("Used memory: {} MB", sys.used_memory() / 1024 / 1024);
println!("Total swap: {} MB", sys.total_swap() / 1024 / 1024);
println!("Used swap: {} MB", sys.used_swap() / 1024 / 1024);
}
```
### CPU Information and Usage
```rust
use sysinfo::{System, RefreshKind, CpuRefreshKind};
use std::thread;
use std::time::Duration;
fn main() {
// Create system with CPU refresh
let mut sys = System::new_with_specifics(
RefreshKind::nothing().with_cpu(CpuRefreshKind::everything())
).expect("Failed to create System");
// Wait a bit for CPU usage to be computed
thread::sleep(Duration::from_millis(500));
sys.refresh_cpu_usage();
println!("Number of CPUs: {}", sys.cpus().len());
for (i, cpu) in sys.cpus().iter().enumerate() {
println!("CPU {}: {}% - {}", i, cpu.usage(), cpu.brand());
}
}
```
### Process Information
```rust
use sysinfo::{System, ProcessesToUpdate, ProcessRefreshKind};
fn main() {
let mut sys = System::new().expect("Failed to create System");
// Refresh process list
sys.refresh_processes(ProcessesToUpdate::All, true);
println!("Total processes: {}", sys.processes().len());
// Find top memory consumers
let mut processes: Vec<_> = sys.processes().values().collect();
processes.sort_by_key(|p| std::cmp::Reverse(p.memory()));
println!("\nTop 5 processes by memory:");
for process in processes.iter().take(5) {
println!(
"[PID {}] {} - {} MB",
process.pid(),
process.name().to_string_lossy(),
process.memory() / 1024 / 1024
);
}
}
```
### Disk Information
```rust
use sysinfo::Disks;
fn main() {
let disks = Disks::new_with_refreshed_list().expect("Failed to get disks");
for disk in disks.list() {
println!("Disk: {:?}", disk.name());
println!(" Mount point: {:?}", disk.mount_point());
println!(" File system: {:?}", disk.file_system());
println!(" Total space: {} GB", disk.total_space() / 1024 / 1024 / 1024);
println!(" Available space: {} GB", disk.available_space() / 1024 / 1024 / 1024);
println!(" Is removable: {}", disk.is_removable());
println!();
}
}
```
### Network Information
```rust
use sysinfo::Networks;
fn main() {
let networks = Networks::new_with_refreshed_list().expect("Failed to get networks");
for (interface_name, data) in networks.iter() {
println!("Interface: {}", interface_name);
println!(" Received: {} MB", data.total_received() / 1024 / 1024);
println!(" Transmitted: {} MB", data.total_transmitted() / 1024 / 1024);
println!(" MAC address: {}", data.mac_address());
println!();
}
}
```
### Temperature Sensors (Components)
```rust
use sysinfo::Components;
fn main() {
let components = Components::new_with_refreshed_list()
.expect("Failed to get components");
for component in components.list() {
if let Some(temp) = component.temperature() {
println!("{}: {:.1}°C", component.label(), temp);
if let Some(max) = component.max() {
println!(" Max recorded: {:.1}°C", max);
}
if let Some(critical) = component.critical() {
println!(" Critical threshold: {:.1}°C", critical);
}
}
}
}
```
### GPU Information
```rust
use sysinfo::Gpus;
fn main() {
let gpus = Gpus::new_with_refreshed_list().expect("Failed to get GPUs");
for gpu in gpus.list() {
println!("GPU: {}", gpu.name());
println!(" Vendor: {}", gpu.vendor());
if let Some(mem) = gpu.memory() {
println!(" Memory: {} MB", mem / 1024 / 1024);
}
}
}
```
### User Information
```rust
use sysinfo::Users;
fn main() {
let users = Users::new_with_refreshed_list().expect("Failed to get users");
for user in users.list() {
println!("User: {}", user.name());
println!(" UID: {}", *user.id());
println!(" GID: {}", *user.group_id());
}
}
```
## Performance Tips
1. **Keep System instance alive**: Instead of creating a new `System` instance every time, keep one instance and refresh it as needed. This is much more efficient.
```rust
// Good - reuse instance
let mut sys = System::new().expect("Failed to create System");
loop {
sys.refresh_all();
// Use sys...
thread::sleep(Duration::from_secs(1));
}
// Bad - create new instance each time
loop {
let sys = System::new_all().expect("Failed to create System");
// Use sys...
thread::sleep(Duration::from_secs(1));
}
```
2. **Refresh only what you need**: Use specific refresh methods instead of `refresh_all()`:
```rust
use sysinfo::{System, RefreshKind, MemoryRefreshKind};
// Only refresh memory information
let mut sys = System::new_with_specifics(
RefreshKind::nothing().with_memory(MemoryRefreshKind::everything())
).expect("Failed to create System");
```
3. **Control file descriptors**: If your application uses many file descriptors, you can limit how many sysinfo keeps open:
```rust
// Limit to 10 open file descriptors
sysinfo::set_open_files_limit(10);
let sys = System::new().expect("Failed to create System");
```
## Differences from Original sysinfo
This is a Linux-only fork with the following changes:
1. **Linux-only**: All Windows, macOS, BSD, iOS, and Android support has been removed
2. **Simplified structure**: Flattened module hierarchy (`src/linux/` instead of `src/unix/linux/`)
3. **No platform conditionals**: Removed all `cfg!` macros for platform selection
4. **Cleaner dependencies**: No Windows or macOS-specific dependencies
5. **Focused CI/CD**: Only tests Linux targets
## System Requirements
- **Operating System**: Linux (kernel 2.6.32 or later)
- **Rust**: 1.95.0 or later
- **Architecture**: x86_64, i686, aarch64, armv7, or armv6
## Troubleshooting
### "Failed to retrieve OS version"
This is normal in some containerized environments (Docker, WSL) where OS information is not fully available.
### No temperature sensors found
Temperature sensors require:
- Proper kernel modules loaded (`lm_sensors` package)
- Access to `/sys/class/hwmon` or `/sys/class/thermal`
- May not work in virtual machines or containers
### Permission denied errors
Some operations require elevated privileges:
- Reading certain process information may require root
- Some hardware information requires access to system files
## Contributing
Since this is a Linux-focused fork, contributions should:
1. Focus on Linux-specific improvements
2. Not reintroduce multi-platform support
3. Maintain compatibility with the original sysinfo API where reasonable
## License
MIT License (same as original sysinfo)
## Credits
This fork is based on the excellent [sysinfo](https://github.com/GuillaumeGomez/sysinfo) crate by Guillaume Gomez.