sysinfo Rust Library

repository·main·Indexed 25 days ago

https://github.com/guillaumegomez/sysinfo

A cross-platform Rust crate for retrieving comprehensive system information, including CPU usage, memory, disks, networks, processes, and hardware components. It features delta-based updates for high performance, a C interface, and specialized support for macOS/iOS sandboxing and Raspberry Pi cross-compilation. Version 0.39.6.

Tokens
11.7K
Snippets
37
Records
67
Agent score
83%

What's inside sysinfo

  1. Migrate refresh list methods (v0.32 to v0.33)

    main

    In v0.33, the following methods were renamed to refresh:

    • Users::refresh_list
    • Groups::refresh_list
    • Components::refresh_list
    • Networks::refresh_list
    • Disks::refresh_list

    Note that all of these (except Users::refresh and Groups::refresh) now require a boolean argument to specify whether sysinfo should keep removed items.

  2. Configure Cargo features for smaller builds (v0.30 to v0.31)

    main

    Starting in v0.31, you can reduce build size and time by disabling default features and enabling only what you need. For example, to only include network information:

    sysinfo = { version = "0.31", default-features = false, features = ["network"] }
    sysinfo = { version = "0.31", default-features = false, features = ["network"] }
  3. Migrate from Traits to direct methods (v0.29 to v0.30)

    main

    In v0.30, all traits were removed. You no longer need to import extension traits like SystemExt to use methods on System.

    Before v0.30:

    use sysinfo::{System, SystemExt};
    
    // `SystemExt` is needed for both `new` and `refresh_processes`.
    let mut s = System::new();
    s.refresh_processes();

    In v0.30+:

    use sysinfo::System;
    
    // No need for `SystemExt` anymore!
    let s = System::new();
    s.refresh_processes();
    use sysinfo::System;
    
    // No need for `SystemExt` anymore!
    let s = System::new();
    s.refresh_processes();
  4. Migrate process refresh logic (v0.30 to v0.31)

    main

    In v0.31, System::refresh_process, System::refresh_process_specifics, and System::refresh_pids were removed. They are replaced by System::refresh_processes and System::refresh_processes_specifics, which take a ProcessesToUpdate argument.

    To refresh specific PIDs:

    use sysinfo::{ProcessesToUpdate, System};
    
    let pid = 1337;
    let mut s = System::new();
    s.refresh_processes(ProcessesToUpdate::Some(&[pid.into()]));

    To refresh all processes:

    use sysinfo::{ProcessesToUpdate, System};
    
    let mut s = System::new();
    s.refresh_processes(ProcessesToUpdate::All);
    use sysinfo::{ProcessesToUpdate, System};
    
    let pid = 1337;
    let mut s = System::new();
    s.refresh_processes(ProcessesToUpdate::Some(&[pid.into()]));
  5. Migrate System and Process refresh methods (v0.33 to v0.34)

    main

    In v0.34, the multithread feature is disabled by default to avoid issues with the rayon crate.

    System::physical_core_count is now an associated function and no longer requires a System instance.

    System::refresh_all and System::refresh_specifics now automatically remove dead processes. To prevent this behavior, use System::refresh_processes_specifics directly.

  6. Cross-compile sysinfo for Raspberry Pi

    main

    Building directly on Raspberry Pi can be difficult. It is recommended to cross-compile on a host machine (e.g., Ubuntu) using the armv7-unknown-linux-gnueabihf target.

    1. Install the toolchain:
      sudo apt install gcc-multilib-arm-linux-gnueabihf
    2. Configure Cargo to use the linker:
      cat << EOF > ~/.cargo/config
      [target.armv7-unknown-linux-gnueabihf]
      linker = "arm-linux-gnueabihf-gcc"
      EOF
    3. Build the target:
      rustup target add armv7-unknown-linux-gnueabihf
      cargo build --target=armv7-unknown-linux-gnueabihf
    sudo apt install gcc-multilib-arm-linux-gnueabihf
    
    cat << EOF > ~/.cargo/config
    [target.armv7-unknown-linux-gnueabihf]
    linker = "arm-linux-gnueabihf-gcc"
    EOF
    
    rustup target add armv7-unknown-linux-gnueabihf
    cargo build --target=armv7-unknown-linux-gnueabihf
  7. Get started with sysinfo in Rust

    main

    To use sysinfo to retrieve system information, you must first instantiate a System struct and call a refresh method. Most information (like CPU usage or memory) is calculated based on the difference between the current and previous values, so you should keep a single instance of System alive throughout your program rather than recreating it.

    To ensure all lists (like CPUs and processes) are populated on the first run, use System::new_all().

  8. Optimize sysinfo performance

    main

    Follow these best practices to improve performance and resource usage:

    1. Use specific refreshes: Instead of refresh_all(), use refresh_specifics(...) methods to update only the subset of information your application requires.
    2. Reuse the System instance: Avoid recreating the System struct. Reusing the same instance allows the library to compute deltas (like CPU usage) and avoids repeated memory allocations for lists like processes.
    3. Manage file descriptors: If your application requires a high number of file descriptors, call sysinfo::set_open_files_limit(0); to prevent sysinfo from consuming them for performance optimization.
    4. Feature Flags: The multithread feature enables multi-threaded data collection but may increase memory usage on certain platforms like macOS.
  9. Configure sysinfo for macOS/iOS App Store or Sandboxing

    main

    By default, sysinfo may use APIs prohibited by Apple's App Store policies. To ensure compatibility:

    • Use the apple-app-store feature flag to disable prohibited features. This automatically enables the apple-sandbox feature.
    • If you are running in a sandbox outside of the App Store, you can enable the apple-sandbox feature alone.