A cross-platform library to retrieve performance statistics data.

Overview

perf-monitor-rs

github minimum rustc 1.31.0 MIT licensed docs.rs crates.io

# Cargo.toml
[dependencies]
perf_monitor = "0.2"

A toolkit designed to be a foundation for applications to monitor their performance. It is:

  • Cross-Platform: perf-monitor supports Windows, macOS, Linux, iOS, and Android.
  • Safe Wrapper: perf-monitor uses many system C interfaces internally but exposes safe wrapper API outside.
  • Effective: perf-monitor is a thin wrapper around underlying APIs, taking care of not introducing unnecessary overhead, choosing the most lightweight method among many homogeneous APIs.

Features

  • CPU
    • Usage of current process
    • Usage of other process (coming soon)
    • Usage of any thread in current process
    • Logic core number
  • Memory
    • A global allocator that tracks rust allocations
    • Process memory info of current process for Windows and MacOS(Linux is conming soon).
  • IO
    • Disk IO
    • Network IO(coming soon)
  • FD
    • FD number

Example

A simple activity monitor:

    use perf_monitor::cpu::{ThreadStat, ProcessStat, processor_numbers};
    use perf_monitor::fd::fd_count_cur;
    use perf_monitor::io::get_process_io_stats;
    use perf_monitor::mem::get_process_memory_info;

    // cpu
    let core_num = processor_numbers().unwrap();
    let mut stat_p = ProcessStat::cur().unwrap();
    let mut stat_t = ThreadStat::cur().unwrap();

    let _ = (0..1_000).into_iter().sum::<i128>();

    let usage_p = stat_p.cpu().unwrap() * 100f64;
    let usage_t = stat_t.cpu().unwrap() * 100f64;

    println!("[CPU] core Number: {}, process usage: {:.2}%, current thread usage: {:.2}%", core_num, usage_p, usage_t);

    // mem
    let mem_info = get_process_memory_info().unwrap();
    println!("[Memory] memory used: {} bytes, virtural memory used: {} bytes ", mem_info.resident_set_size, mem_info.virtual_memory_size);

    // fd
    let fd_num = fd_count_cur().unwrap();
    println!("[FD] fd number: {}", fd_num);

    // io
    let io_stat = get_process_io_stats().unwrap();   
    println!("[IO] io-in: {} bytes, io-out: {} bytes", io_stat.read_bytes, io_stat.write_bytes);

The above code should have the following output:

[CPU] core Number: 12, process usage: 502.16%, current thread usage: 2.91%
[Memory] memory used: 1073152 bytes, virtural memory used: 4405747712 bytes 
[FD] fd number: 7
[IO] io-in: 0 bytes, io-out: 32768 bytes

See examples for details.

Perfomance

We are concerned about the overhead associated with obtaining performance information. We try to use the most efficient methods while ensuring the API usability.

For example, CPU usage and FD number cost on these devices has following result:

  • MacOS: MacBookPro15,1; 6-Core Intel Core i7; 2.6GHz; 16GB
  • Windows: Windows10; Intel Core i3-2310M; 2.10GHz; 64bit; 4GB
  • Andorid: Pixel 2; android 10
profiling Windows MacOS Android
thread CPU usage (ms) 3 0.45 16
FD number (ms) 0.15 0.07 10

Supported Platform

profiling Windows MacOS iOS Android Linux
CPU
Memory
FD count
IO

See documents of each module for usage and more details.

Rust Version

To compile document require the nightly version, others should work both in stable and nightly version.

cargo build

cargo +nightly doc 

cargo +nightly test
cargo test --lib

Contribution

Contributions are welcome!

Open an issue or create a PR to report bugs, add new features or improve documents and tests. If you are a new contributor, see this page for help.

Why perf-monitor-rs?

There are some crates to do similar things, such as spork, procfs, and sysinfo.

Our application needs to monitor itself at runtime to help us find out performance issues. For example, when the CPU usage rises abnormally, we want to figure out which threads cause this.

However, none of the above crates meet our needs.

  • spork can't get other thread information other than the calling thread. Only memory and CPU information can be processed. And it stops updating for years.
  • procfs looks good enough now, but only support the Linux platform. In its early stages, when we developed perf_monitor_rs, there was no way to get thread information.
  • sysinfo support all platform we need, but we think its interface is not elegant, because an explicit refresh is required before each call, otherwise an old value will be retrieved and you are not able to tell from the returning value. More importantly, it lacks some features like fd, CPU usage.

If you are building a cross-platform application and facing the same problem, we hope perf_monitor_rs can be your first choice.

License

perf-monitor is providing under the MIT license. See LICENSE.

You might also like...
A highly efficient daemon for streaming data from Kafka into Delta Lake

A highly efficient daemon for streaming data from Kafka into Delta Lake

TensorBase is a new big data warehousing with modern efforts.
TensorBase is a new big data warehousing with modern efforts.

TensorBase is a new big data warehousing with modern efforts.

Fill Apache Arrow record batches from an ODBC data source in Rust.

arrow-odbc Fill Apache Arrow arrays from ODBC data sources. This crate is build on top of the arrow and odbc-api crate and enables you to read the dat

Analysis of Canadian Federal Elections Data

Canadian Federal Elections election is a small Rust program for processing vote data from Canadian Federal Elections. After building, see election --h

📊  Cube.js — Open-Source Analytics API for Building Data Apps
📊 Cube.js — Open-Source Analytics API for Building Data Apps

📊 Cube.js — Open-Source Analytics API for Building Data Apps

Provides a way to use enums to describe and execute ordered data pipelines. 🦀🐾

enum_pipline Provides a way to use enums to describe and execute ordered data pipelines. 🦀 🐾 I needed a succinct way to describe 2d pixel map operat

Perhaps the fastest and most memory efficient way to pull data from PostgreSQL into pandas and numpy. 🚀

flaco Perhaps the fastest and most memory efficient way to pull data from PostgreSQL into pandas and numpy. 🚀 Have a gander at the initial benchmarks

AppFlowy is an open-source alternative to Notion. You are in charge of your data and customizations
AppFlowy is an open-source alternative to Notion. You are in charge of your data and customizations

AppFlowy is an open-source alternative to Notion. You are in charge of your data and customizations. Built with Flutter and Rust.

An example repository on how to start building graph applications on streaming data. Just clone and start building 💻 💪
An example repository on how to start building graph applications on streaming data. Just clone and start building 💻 💪

An example repository on how to start building graph applications on streaming data. Just clone and start building 💻 💪

Comments
  • chore: CI support

    chore: CI support

    I have added some CI checks for a better development experience.

    The changes must be formatted correctly before merging. It will be built on all supported platforms and tested on Linux.

    The Clippy check is temporarily disabled for some naming problems.

    opened by heymind 0
  • Network IO(coming soon)

    Network IO(coming soon)

    Any ETA on Network I/O being inspectable?

    Or, alternatively, you have any guidance on how I can implement this myself, and I might be able to send a PR?

    opened by CapableWeb 0
  • question about thread build

    question about thread build

    very nice lib to start off, its the thing was looking for, for a long time

    just one little question here

    when i use ThreadStat::build() i don't seem to get the numbers i have try it on linux and macos both numbers remains 0 at all time, but the cur does work perfect

    any help would be awesome

    thanks advance

    Jason

    opened by jason-shen 0
  • crash in Android

    crash in Android " java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "dl_iterate_phdr"

    java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "dl_iterate_phdr"

    fn getCpuUsage() -> f64 {
        let mut stat_p = ProcessStat::cur().unwrap();
        let _ = (0..1_000).into_iter().sum::<i128>();
        return stat_p.cpu().unwrap() * 100f64;
    }
    
    opened by MichaelJokAr 0
Owner
Lark Technologies Pte. Ltd.
Lark Technologies Pte. Ltd.
A high-performance, high-reliability observability data pipeline.

Quickstart • Docs • Guides • Integrations • Chat • Download What is Vector? Vector is a high-performance, end-to-end (agent & aggregator) observabilit

Timber 11.9k Dec 1, 2022
This library provides a data view for reading and writing data in a byte array.

Docs This library provides a data view for reading and writing data in a byte array. This library requires feature(generic_const_exprs) to be enabled.

null 2 Nov 2, 2022
A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, built to make the Data Cloud easy

Datafuse Labs 4.8k Nov 26, 2022
New generation decentralized data warehouse and streaming data pipeline

World's first decentralized real-time data warehouse, on your laptop Docs | Demo | Tutorials | Examples | FAQ | Chat Get Started Watch this introducto

kamu 181 Nov 26, 2022
Rayon: A data parallelism library for Rust

Rayon Rayon is a data-parallelism library for Rust. It is extremely lightweight and makes it easy to convert a sequential computation into a parallel

null 7.6k Nov 23, 2022
ConnectorX - Fastest library to load data from DB to DataFrames in Rust and Python

ConnectorX enables you to load data from databases into Python in the fastest and most memory efficient way.

SFU Database Group 888 Nov 25, 2022
Dataflow is a data processing library, primarily for machine learning

Dataflow Dataflow is a data processing library, primarily for machine learning. It provides efficient pipeline primitives to build a directed acyclic

Sidekick AI 8 Aug 27, 2022
Quickwit is a big data search engine.

Quickwit This repository will host Quickwit, the big data search engine developed by Quickwit Inc. We will progressively polish and opensource our cod

Quickwit Inc. 2.7k Nov 23, 2022
DataFrame / Series data processing in Rust

black-jack While PRs are welcome, the approach taken only allows for concrete types (String, f64, i64, ...) I'm not sure this is the way to go. I want

Miles Granger 30 Oct 9, 2022
A Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture, written in Rust

Datafuse Modern Real-Time Data Processing & Analytics DBMS with Cloud-Native Architecture Datafuse is a Real-Time Data Processing & Analytics DBMS wit

Datafuse Labs 4.8k Nov 30, 2022