Git Product home page Git Product logo

spork.rs's Introduction

Spork

CPU and memory usage for threads and processes.

Build Status Coverage Status MIT licensed

Documentation

Install

With Cargo Edit:

cargo add spork

Or add to your Cargo.toml

spork = "0.1"

Status

Currently POSIX compliant platforms are developed and tested, but Windows support remains a WIP.

Usage

extern crate spork;

use spork::{
  Spork,
  Error,
  ErrorKind,
  Platform,
  StatType,
  Stats
};

let spork = match Spork::new() {
  Ok(s) => s,
  Err(e) => panic!("Error creating spork client! {:?}", e)
};

println!("Using platform {:?}", spork.platform());
println!("CPU cores: {:?}x @ {:?} Hz", spork.num_cores(), spork.clock_speed());

// get process stats 
let p_stats = match spork.stats(StatType::Process) {
  Ok(s) => s,
  Err(e) => panic!("Error polling process stats! {:?}", e)
};
println!("Process stats: {:?}", p_stats);

// get thread stats 
let t_stats = match spork.stats(StatType::Thread) {
  Ok(s) => s,
  Err(e) => panic!("Error polling thread stats! {:?}", e)
};

println!("Thread CPU: {}%, Memory: {} bytes, Cores: {}, Type: {}, Polled at: {}", 
  t_stats.cpu, t_stats.memory, t_stats.cores, t_stats.kind, t_stats.polled);

// get process stats across all CPU cores
let p_stats = spork.stats_with_cpus(StatType::Process, None).unwrap();

// get process stats across only 2 cores
let p_stats = spork.stats_with_cpus(StatType::Process, Some(2)).unwrap();

// get stats for child threads of the calling thread across all CPU cores
let c_stats = spork.stats_with_cpus(StatType::Children, None).unwrap();

Unsupported Platforms

This module supports POSIX compliant platforms (Linux, OS X, etc) and Windows (soon). If you'd like to use this on an unsupported platform, or one on which you might expect compatibility issues, there are two options available for testing and usage. If you'd prefer to catch any compatibility issues at compile-time just download this library and try to build it. If it builds it shouldTM work, but it's still a good idea to run the test suite before trying it in production.

If you'd prefer to handle compatibility errors at runtime add the compile_unimplemented feature to your Cargo.toml for Spork. Instead of introducing compiler errors this will compile mock functions which always return Unimplemented errors in place of any missing platform-specific ones.

Tests

cargo test

spork.rs's People

Contributors

aembke avatar colingabr avatar ronnychan-okta avatar samuelxing-okta avatar stegmanh avatar virginiachiu-okta avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

spork.rs's Issues

Does not compile on Windows

Below is the error I get, using rust distribution nightly-x86_64-pc-windows-msvc (also tried gnu, same error)

error[E0425]: cannot find function `sysconf` in crate `libc`
   --> C:\Users\Ruben Anders\.cargo\registry\src\github.com-1ecc6299db9ec823\sys-info-0.5.10\lib.rs:396:34
    |
396 |             let buf_size = libc::sysconf(libc::_SC_HOST_NAME_MAX) as usize;
    |                                  ^^^^^^^ not found in `libc`

error[E0425]: cannot find value `_SC_HOST_NAME_MAX` in crate `libc`
   --> C:\Users\Ruben Anders\.cargo\registry\src\github.com-1ecc6299db9ec823\sys-info-0.5.10\lib.rs:396:48
    |
396 |             let buf_size = libc::sysconf(libc::_SC_HOST_NAME_MAX) as usize;
    |                                                ^^^^^^^^^^^^^^^^^ not found in `libc`

error[E0425]: cannot find function `gethostname` in crate `libc`
   --> C:\Users\Ruben Anders\.cargo\registry\src\github.com-1ecc6299db9ec823\sys-info-0.5.10\lib.rs:398:22
    |
398 |               if libc::gethostname(buf.as_mut_ptr() as *mut libc::c_char, buf_size) < 0 {
    |                        ^^^^^^^^^^^ help: a function with a similar name exists: `getsockname`
    | 
   ::: C:\Users\Ruben Anders\.cargo\registry\src\github.com-1ecc6299db9ec823\libc-0.2.72\src\windows\mod.rs:556:5
    |
556 | /     pub fn getsockname(
557 | |         s: SOCKET,
558 | |         name: *mut ::sockaddr,
559 | |         nameln: *mut ::c_int,
560 | |     ) -> ::c_int;
    | |_________________- similarly named function `getsockname` defined here

Extend OS X/Darwin System Calls

It looks like the mach crate currently does not implement the task_info function required to read per-thread CPU and memory information. Until that's updated this module will not be able to handle StatType::Thread on OS X/Darwin. For processes and children POSIX calls seem to work, but for threads Darwin requires using the mach kernel interface.

More info:

TODO:

  • Deal with sizing macros for TASK_BASIC_INFO_COUNT, TASK_EVENTS_INFO_COUNT, and TASK_THREAD_TIMES_INFO_COUNT.
  • Implement the new task structs: task_basic_info, task_basic_info_t, task_events_info, task_events_info_t, task_thread_times_info, task_thread_times_info_t, time_value, time_value_t.
  • Implement task_info: pub fn task_info(target_task: task_t, flavor: c_int, task_info: *mut task_info_t, task_info_count: *mut mach_msg_type_number_t) -> kern_return_t;

Way to specify a process id

I wonder if this library could provide a way to specify a custom process id when querying stats (cpu, mem) ? (I want to use it to collect stats from sub-processes executed from rust. These sub-processes potentially can be running in parallel where I would like to know the resource information per process basis individually).

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.