rust-psutil

repository·main·Indexed 18 days ago

https://github.com/rust-psutil/rust-psutil

A process and system monitoring library for Rust inspired by the Python psutil module. It provides tools for inspecting CPU, memory, disks, network, and process information. The library supports Linux and macOS, with varying levels of functionality across platforms. It utilizes a collector-based design for stateful measurements (such as CPU usage percentages) and provides opt-in subsystems via Cargo feature flags.

Tokens
12.2K
Snippets
29
Records
37
Agent score
62%

What's inside rust-psutil

  1. API stability and versioning

    main
    The rust-psutil API is considered relatively unstable. While the project attempts to follow semantic versioning, major version bumps are used to introduce multiple breaking changes, often occurring when significant new features are added that require adjustments to existing APIs.
  2. Install rust-psutil

    main

    To use rust-psutil in your Rust project, add it to your Cargo.toml dependencies. You can install the full library or opt for a minimal build by disabling default features and selecting specific submodules like cpu or process to reduce compilation time and binary size.

    # Install the full library
    [dependencies]
    psutil = "4.0.0"
    # Install only specific submodules
    [dependencies]
    psutil = { version = "4.0.0", default-features = false, features = ["cpu", "process"] }
  3. Understand the rust-psutil API design and differences from Python psutil

    main

    While rust-psutil is inspired by the Python psutil module, it follows Rust idioms and has several key architectural differences:

    • Namespacing: The crate is namespaced by subsystem. For example, instead of a global function, you use cpu::cpu_percent().
    • Collectors for State: Functions that require persisting data between calls (to calculate deltas) are implemented as methods on 'collector' structs. For example, cpu_percent() is accessed via CpuPercentCollector::cpu_percent().
    • Platform Extensions: Platform-specific functionality is hidden behind traits. To use Linux-specific process features, you must explicitly import the relevant trait, such as cpu::os::linux::ProcessExt.
    • Type Safety:
      • Uses std::time::Duration instead of floats for time-based values.
      • Uses structs instead of named tuples.
      • Uses enums instead of constants.
      • Most struct fields are accessed via getter methods to support platform-based extensions.
    • Feature Flags: Subsystems (cpu, disk, host, memory, network, process, sensors) are opt-in via Cargo feature flags.
  4. Use CpuTimesPercentCollector to get CPU usage percentages

    main

    To calculate CPU usage percentages over a period of time, use the CpuTimesPercentCollector. This collector tracks the delta between consecutive calls to provide non-blocking percentage values.

    1. Initialize the collector using CpuTimesPercentCollector::new().
    2. Call cpu_times_percent() to get the aggregate CPU usage.
    3. Call cpu_times_percent_percpu() to get usage for each individual CPU core.

    Note: The first call to these methods will return the usage since the collector was initialized. Subsequent calls return the usage since the previous call. If the time elapsed between calls is too short, the returned values may be zero.

    let mut cpu_times_percent_collector = psutil::cpu::CpuTimesPercentCollector::new().unwrap();
    
    // Get aggregate CPU usage
    let cpu_times_percent = cpu_times_percent_collector.cpu_times_percent().unwrap();
    
    // Get per-CPU usage
    let cpu_times_percent_percpu = cpu_times_percent_collector.cpu_times_percent_percpu().unwrap();
  5. Configure rust-psutil subsystems via Cargo features

    main

    The library is modular. You can reduce binary size and compilation time by opting into only the subsystems you need using Cargo feature flags. Available subsystems include:

    • cpu
    • disk
    • host
    • memory
    • network
    • process
    • sensors
    • serde (for serialization support)
  6. Check platform support for CPU functions

    main

    The availability of CPU-related functions in rust-psutil varies by operating system.

    • Linux and macOS support: cpu_times, cpu_percent, cpu_times_percent, and cpu_count.
    • Windows support: cpu_count only.
    • FreeBSD support: None of the listed CPU functions are currently supported.
    | Function | Linux | macOS | Windows | FreeBSD |
    |---|---|---|---|---|
    | `cpu_times` | ✅ | ✅ | | |
    | `cpu_percent` | ✅ | ✅ | | |
    | `cpu_times_percent` | ✅ | ✅ | | |
    | `cpu_count` | ✅ | ✅ | ✅ | |
    | `cpu_stats` | | | | |
    | `cpu_freq` | | | | |
  7. Platform support for rust-psutil

    main

    The rust-psutil library currently only supports the following operating systems:

    • Linux
    • macOS

    Other platforms are not supported. For detailed implementation details regarding each platform, refer to the platform-support.md file in the repository.

  8. Sensor API platform support

    main

    Hardware sensor monitoring is currently only supported on Linux.

    • sensors_temperatures: Supported on Linux.
    • sensors_fans: Not supported.
    |                                                                                              | Linux              | macOS | Windows | FreeBSD |
    |----------------------------------------------------------------------------------------------|--------------------|-------|---------|---------|
    | [sensors_temperatures](https://psutil.readthedocs.io/en/latest/#psutil.sensors_temperatures) | :heavy_check_mark: |       |         |         |
    | [sensors_fans](https://psutil.readthedocs.io/en/latest/#psutil.sensors_fans)                 |                    |       |         |         |
  9. Check platform support for Host functions

    main

    Host-related functions have the following platform availability:

    • Linux support: loadavg and boot_time.
    • macOS, Windows, and FreeBSD support: None of the listed Host functions are currently supported.
    | Function | Linux | macOS | Windows | FreeBSD |
    |---|---|---|---|---|
    | `loadavg` | ✅ | | | |
    | `boot_time` | ✅ | | | |
    | `users` | | | | |
  10. Check platform support for Network functions

    main

    Network-related functions have the following platform availability:

    • Linux and macOS support: net_io_counters.
    • Windows and FreeBSD support: None of the listed Network functions are currently supported.
    | Function | Linux | macOS | Windows | FreeBSD |
    |---|---|---|---|---|
    | `net_io_counters` | ✅ | ✅ | | |
    | `net_connections` | | | | |
    | `net_if_addrs` | | | | |
    | `net_if_stats` | | | | |
  11. Per-process API platform support

    main

    The following methods are available on Process objects. Support varies significantly by operating system.

    Supported on Linux and macOS

    • pid: Process ID.
    • name: Process name.
    • create_time: Creation time.
    • cpu_times: CPU times.
    • cpu_percent: CPU utilization percentage.
    • memory_info: Memory information.
    • memory_percent: Memory utilization percentage.
    • is_running: Check if process is running.
    • send_signal: Send signals to the process.
    • suspend: Suspend the process.
    • resume: Resume the process.
    • terminate: Terminate the process.
    • kill: Kill the process.

    Supported on Linux only

    • ppid: Parent process ID.
    • exe: Path to the executable.
    • cmdline: Command line arguments.
    • environ: Environment variables.
    • parent: Parent process object.
    • status: Process status.
    • cwd: Current working directory.
    • uids: User IDs.
    • gids: Group IDs.
    • open_files: List of open files.

    Supported on Linux and macOS (Partial/Specific)

    • pid: Process ID.
    • name: Process name.
    • create_time: Creation time.
    • cpu_times: CPU times.
    • cpu_percent: CPU utilization percentage.
    • memory_info: Memory information.
    • memory_percent: Memory utilization percentage.
    • is_running: Check if process is running.
    • send_signal: Send signals to the process.
    • suspend: Suspend the process.
    • resume: Resume the process.
    • terminate: Terminate the process.
    • kill: Kill the process.
    |                                                                                              | Linux              | macOS              | Windows | FreeBSD |
    |----------------------------------------------------------------------------------------------|--------------------|--------------------|---------|---------|
    | [pid](https://psutil.readthedocs.io/en/latest/#psutil.Process.pid)                           | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [ppid](https://psutil.readthedocs.io/en/latest/#psutil.Process.ppid)                         | :heavy_check_mark: |                    |         |         |
    | [name](https://psutil.readthedocs.io/en/latest/#psutil.Process.name)                         | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [exe](https://psutil.readthedocs.io/en/latest/#psutil.Process.exe)                           | :heavy_check_mark: |                    |         |         |
    | [cmdline](https://psutil.readthedocs.io/en/latest/#psutil.Process.cmdline)                   | :heavy_check_mark: |                    |         |         |
    | [environ](https://psutil.readthedocs.io/en/latest/#psutil.Process.environ)                   | :heavy_check_mark: |                    |         |         |
    | [create_time](https://psutil.readthedocs.io/en/latest/#psutil.Process.create_time)           | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [as_dict](https://psutil.readthedocs.io/en/latest/#psutil.Process.as_dict)                   |                    |                    |         |         |
    | [parent](https://psutil.readthedocs.io/en/latest/#psutil.Process.parent)                     | :heavy_check_mark: |                    |         |         |
    | [parents](https://psutil.readthedocs.io/en/latest/#psutil.Process.parents)                 |                    |                    |         |         |
    | [status](https://psutil.readthedocs.io/en/latest/#psutil.Process.status)                       | :heavy_check_mark: |                    |         |         |
    | [cwd](https://psutil.readthedocs.io/en/latest/#psutil.Process.cwd)                             | :heavy_check_mark: |                    |         |         |
    | [username](https://psutil.readthedocs.io/en/latest/#psutil.Process.username)                 |                    |                    |         |         |
    | [uids](https://psutil.readthedocs.io/en/latest/#psutil.Process.uids)                           | :heavy_check_mark: |                    |         |         |
    | [gids](https://psutil.readthedocs.io/en/latest/#psutil.Process.gids)                           | :heavy_check_mark: |                    |         |         |
    | [terminal](https://psutil.readthedocs.io/en/latest/#psutil.Process.terminal)                 |                    |                    |         |         |
    | [nice](https://psutil.readthedocs.io/en/latest/#psutil.Process.nice)                           |                    |                    |         |         |
    | [ionice](https://psutil.readthedocs.io/en/latest/#psutil.Process.ionice)                       |                    |                    |         |         |
    | [rlimit](https://psutil.readthedocs.io/en/latest/#psutil.Process.rlimit)                       |                    |                    |         |         |
    | [io_counters](https://psutil.readthedocs.io/en/latest/#psutil.Process.io_counters)           |                    |                    |         |         |
    | [num_ctx_switches](https://psutil.readthedocs.io/en/latest/#psutil.Process.num_ctx_switches) |                    |                    |         |         |
    | [num_fds](https://psutil.readthedocs.io/en/latest/#psutil.Process.num_fds)                     |                    |                    |         |         |
    | [num_threads](https://psutil.readthedocs.io/en/latest/#psutil.Process.num_threads)             |                    |                    |         |         |
    | [threads](https://psutil.readthedocs.io/en/latest/#psutil.Process.threads)                     |                    |                    |         |         |
    | [cpu_times](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_times)                 | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [cpu_percent](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent)             | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [cpu_affinity](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_affinity)         |                    |                    |         |         |
    | [cpu_num](https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_num)                     |                    |                    |         |         |
    | [memory_info](https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_info)           | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [memory_info_full](https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_info_full) |                    |                    |         |         |
    | [memory_percent](https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_percent)     | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [memory_maps](https://psutil.readthedocs.io/en/latest/#psutil.Process.memory_maps)           |                    |                    |         |         |
    | [children](https://psutil.readthedocs.io/en/latest/#psutil.Process.children)                     |                    |                    |         |         |
    | [open_files](https://psutil.readthedocs.io/en/latest/#psutil.Process.open_files)             | :heavy_check_mark: |                    |         |         |
    | [connections](https://psutil.readthedocs.io/en/latest/#psutil.Process.connections)     |                    |                    |         |         |
    | [is_running](https://psutil.readthedocs.io/en/latest/#psutil.Process.is_running)     | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [send_signal](https://psutil.readthedocs.io/en/latest/#psutil.Process.send_signal)     | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [suspend](https://psutil.readthedocs.io/en/latest/#psutil.Process.suspend)           | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [resume](https://psutil.readthedocs.io/en/latest/#psutil.Process.resume)             | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [terminate](https://psutil.readthedocs.io/en/latest/#psutil.Process.terminate)               | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [kill](https://psutil.readthedocs.io/en/latest/#psutil.Process.kill)                         | :heavy_check_mark: | :heavy_check_mark: |         |         |
    | [wait](https://psutil.readthedocs.io/en/latest/#psutil.Process.wait)                         |                    |                    |         |         |
  12. Identify filesystem types with the `FileSystem` enum

    main

    The FileSystem enum is used to categorize the type of filesystem being queried. It distinguishes between known physical filesystems (like Ext4, Ntfs, Zfs, Apfs) and virtual filesystems.

    • Physical vs Virtual: Use is_physical() to check if the filesystem is a known physical device. Use is_virtual() to identify virtual filesystems (such as tmpfs or smb mounts). Any filesystem categorized as Other(String) is considered virtual.
    • String Representation: Use as_str() to get a lowercase string identifier for the filesystem (e.g., FileSystem::Ext4 returns `