Skip to content

Commit

Permalink
Merge pull request #287 from GuillaumeGomez/process_disk_rw
Browse files Browse the repository at this point in the history
Process disk rw
  • Loading branch information
GuillaumeGomez authored Apr 7, 2020
2 parents 7e3e92e + 0f0ed78 commit f57aa89
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 202 deletions.
87 changes: 45 additions & 42 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,21 @@ Here are the current results:
<details>

```text
test bench_new ... bench: 3,741 ns/iter (+/- 252)
test bench_new_all ... bench: 10,491,084 ns/iter (+/- 450,925)
test bench_refresh_all ... bench: 2,787,974 ns/iter (+/- 235,649)
test bench_refresh_components ... bench: 24,270 ns/iter (+/- 1,127)
test bench_refresh_components_list ... bench: 370,693 ns/iter (+/- 51,925)
test bench_refresh_cpu ... bench: 13,367 ns/iter (+/- 1,858)
test bench_refresh_disks ... bench: 2,532 ns/iter (+/- 108)
test bench_refresh_disks_lists ... bench: 50,359 ns/iter (+/- 5,877)
test bench_refresh_memory ... bench: 11,713 ns/iter (+/- 1,006)
test bench_refresh_networks ... bench: 220,246 ns/iter (+/- 24,294)
test bench_refresh_networks_list ... bench: 229,648 ns/iter (+/- 82,050)
test bench_refresh_process ... bench: 77,375 ns/iter (+/- 10,657)
test bench_refresh_processes ... bench: 2,282,106 ns/iter (+/- 154,098)
test bench_refresh_system ... bench: 52,466 ns/iter (+/- 4,710)
test bench_new ... bench: 186,638 ns/iter (+/- 8,603)
test bench_new_all ... bench: 18,604,576 ns/iter (+/- 1,323,790)
test bench_refresh_all ... bench: 5,147,523 ns/iter (+/- 337,096)
test bench_refresh_components ... bench: 25,391 ns/iter (+/- 1,725)
test bench_refresh_components_list ... bench: 381,490 ns/iter (+/- 18,237)
test bench_refresh_cpu ... bench: 13,397 ns/iter (+/- 880)
test bench_refresh_disks ... bench: 2,515 ns/iter (+/- 161)
test bench_refresh_disks_list ... bench: 50,986 ns/iter (+/- 5,301)
test bench_refresh_memory ... bench: 11,843 ns/iter (+/- 779)
test bench_refresh_networks ... bench: 250,242 ns/iter (+/- 68,482)
test bench_refresh_networks_list ... bench: 255,148 ns/iter (+/- 23,677)
test bench_refresh_process ... bench: 128,860 ns/iter (+/- 22,638)
test bench_refresh_processes ... bench: 4,714,965 ns/iter (+/- 309,573)
test bench_refresh_system ... bench: 52,044 ns/iter (+/- 4,510)
test bench_refresh_users_list ... bench: 2,095,043 ns/iter (+/- 648,385)
```
</details>

Expand All @@ -130,20 +131,21 @@ test bench_refresh_system ... bench: 52,466 ns/iter (+/- 4,710)
<details>

```text
test bench_new ... bench: 7,688,460 ns/iter (+/- 1,230,010)
test bench_new_all ... bench: 24,098,860 ns/iter (+/- 5,260,950)
test bench_refresh_all ... bench: 3,096,107 ns/iter (+/- 94,257)
test bench_refresh_components ... bench: 1,205,378 ns/iter (+/- 40,071)
test bench_refresh_components_list ... bench: 3,181,602 ns/iter (+/- 102,533)
test bench_refresh_cpu ... bench: 395 ns/iter (+/- 18)
test bench_refresh_disks ... bench: 53,082 ns/iter (+/- 1,834)
test bench_refresh_disks_lists ... bench: 114,080 ns/iter (+/- 1,920)
test bench_refresh_memory ... bench: 596 ns/iter (+/- 48)
test bench_refresh_networks ... bench: 37,549 ns/iter (+/- 1,622)
test bench_refresh_networks_list ... bench: 667,180 ns/iter (+/- 59,859)
test bench_refresh_process ... bench: 755 ns/iter (+/- 47)
test bench_refresh_processes ... bench: 1,217,488 ns/iter (+/- 69,041)
test bench_refresh_system ... bench: 1,214,780 ns/iter (+/- 52,013)
test bench_new ... bench: 7,335,755 ns/iter (+/- 469,000)
test bench_new_all ... bench: 32,233,480 ns/iter (+/- 1,567,239)
test bench_refresh_all ... bench: 1,433,015 ns/iter (+/- 126,322)
test bench_refresh_components ... bench: 1 ns/iter (+/- 0)
test bench_refresh_components_list ... bench: 9,835,060 ns/iter (+/- 407,072)
test bench_refresh_cpu ... bench: 33,873 ns/iter (+/- 2,177)
test bench_refresh_disks ... bench: 58,951 ns/iter (+/- 6,128)
test bench_refresh_disks_list ... bench: 125,199 ns/iter (+/- 2,741)
test bench_refresh_memory ... bench: 1,004 ns/iter (+/- 56)
test bench_refresh_networks ... bench: 39,013 ns/iter (+/- 2,676)
test bench_refresh_networks_list ... bench: 1,341,850 ns/iter (+/- 78,258)
test bench_refresh_process ... bench: 2,116 ns/iter (+/- 58)
test bench_refresh_processes ... bench: 1,032,447 ns/iter (+/- 57,695)
test bench_refresh_system ... bench: 35,374 ns/iter (+/- 3,200)
test bench_refresh_users_list ... bench: 3,321,140 ns/iter (+/- 135,160)
```
</details>

Expand All @@ -152,20 +154,21 @@ test bench_refresh_system ... bench: 1,214,780 ns/iter (+/- 52,013)
<details>

```text
test bench_new ... bench: 56,861 ns/iter (+/- 5,653)
test bench_new_all ... bench: 4,634,509 ns/iter (+/- 1,604,369)
test bench_refresh_all ... bench: 1,962,343 ns/iter (+/- 129,726)
test bench_refresh_components ... bench: 294,752 ns/iter (+/- 45,107)
test bench_refresh_components_list ... bench: 895,672 ns/iter (+/- 112,586)
test bench_refresh_cpu ... bench: 11,187 ns/iter (+/- 2,483)
test bench_refresh_disks ... bench: 975 ns/iter (+/- 50)
test bench_refresh_disks_lists ... bench: 25,955 ns/iter (+/- 3,159)
test bench_refresh_memory ... bench: 3,440 ns/iter (+/- 198)
test bench_refresh_networks ... bench: 211,552 ns/iter (+/- 16,686)
test bench_refresh_networks_list ... bench: 211,138 ns/iter (+/- 22,644)
test bench_refresh_process ... bench: 4,174 ns/iter (+/- 1,249)
test bench_refresh_processes ... bench: 803,559 ns/iter (+/- 42,974)
test bench_refresh_system ... bench: 365,762 ns/iter (+/- 55,893)
test bench_new ... bench: 86,404 ns/iter (+/- 9,402)
test bench_new_all ... bench: 21,123,771 ns/iter (+/- 570,722)
test bench_refresh_all ... bench: 1,757,683 ns/iter (+/- 203,234)
test bench_refresh_components ... bench: 325,560 ns/iter (+/- 41,068)
test bench_refresh_components_list ... bench: 989,827 ns/iter (+/- 221,093)
test bench_refresh_cpu ... bench: 8,535 ns/iter (+/- 487)
test bench_refresh_disks ... bench: 939 ns/iter (+/- 33)
test bench_refresh_disks_lists ... bench: 25,093 ns/iter (+/- 2,080)
test bench_refresh_memory ... bench: 2,174 ns/iter (+/- 55)
test bench_refresh_networks ... bench: 181,558 ns/iter (+/- 7,325)
test bench_refresh_networks_list ... bench: 180,410 ns/iter (+/- 2,414)
test bench_refresh_process ... bench: 5,570 ns/iter (+/- 431)
test bench_refresh_processes ... bench: 683,455 ns/iter (+/- 14,995)
test bench_refresh_system ... bench: 362,875 ns/iter (+/- 172,547)
test bench_refresh_users_list ... bench: 16,783,834 ns/iter (+/- 465,111)
```
</details>

Expand Down
2 changes: 1 addition & 1 deletion benches/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn bench_refresh_disks(b: &mut test::Bencher) {
}

#[bench]
fn bench_refresh_disks_lists(b: &mut test::Bencher) {
fn bench_refresh_disks_list(b: &mut test::Bencher) {
let mut s = sysinfo::System::new();

b.iter(move || {
Expand Down
160 changes: 160 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use NetworkData;
use Networks;
use NetworksExt;
use UserExt;

/// Trait to have a common fallback for the [`Pid`][crate::Pid] type.
pub trait AsU32 {
Expand Down Expand Up @@ -273,3 +274,162 @@ impl From<isize> for DiskType {
}
}
}

/// An enum representing signal on UNIX-like systems.
#[repr(C)]
#[derive(Clone, PartialEq, PartialOrd, Debug, Copy)]
pub enum Signal {
/// Hangup detected on controlling terminal or death of controlling process.
Hangup = 1,
/// Interrupt from keyboard.
Interrupt = 2,
/// Quit from keyboard.
Quit = 3,
/// Illegal instruction.
Illegal = 4,
/// Trace/breakpoint trap.
Trap = 5,
/// Abort signal from C abort function.
Abort = 6,
// IOT trap. A synonym for SIGABRT.
// IOT = 6,
/// Bus error (bad memory access).
Bus = 7,
/// Floating point exception.
FloatingPointException = 8,
/// Kill signal.
Kill = 9,
/// User-defined signal 1.
User1 = 10,
/// Invalid memory reference.
Segv = 11,
/// User-defined signal 2.
User2 = 12,
/// Broken pipe: write to pipe with no readers.
Pipe = 13,
/// Timer signal from C alarm function.
Alarm = 14,
/// Termination signal.
Term = 15,
/// Stack fault on coprocessor (unused).
Stklft = 16,
/// Child stopped or terminated.
Child = 17,
/// Continue if stopped.
Continue = 18,
/// Stop process.
Stop = 19,
/// Stop typed at terminal.
TSTP = 20,
/// Terminal input for background process.
TTIN = 21,
/// Terminal output for background process.
TTOU = 22,
/// Urgent condition on socket.
Urgent = 23,
/// CPU time limit exceeded.
XCPU = 24,
/// File size limit exceeded.
XFSZ = 25,
/// Virtual alarm clock.
VirtualAlarm = 26,
/// Profiling time expired.
Profiling = 27,
/// Windows resize signal.
Winch = 28,
/// I/O now possible.
IO = 29,
// Pollable event (Sys V). Synonym for IO
//Poll = 29,
/// Power failure (System V).
Power = 30,
/// Bad argument to routine (SVr4).
Sys = 31,
}

/// A struct representing system load average value.
///
/// It is returned by [`SystemExt::get_load_average`][crate::SystemExt::get_load_average].
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
/// let s = System::new_all();
/// let load_avg = s.get_load_average();
/// println!(
/// "one minute: {}%, five minutes: {}%, fifteen minutes: {}%",
/// load_avg.one,
/// load_avg.five,
/// load_avg.fifteen,
/// );
/// ```
#[repr(C)]
#[derive(Default, Debug, Clone)]
pub struct LoadAvg {
/// Average load within one minute.
pub one: f64,
/// Average load within five minutes.
pub five: f64,
/// Average load within fifteen minutes.
pub fifteen: f64,
}

/// Type containing user information.
///
/// It is returned by [`SystemExt::get_users`][crate::SystemExt::get_users].
///
/// ```no_run
/// use sysinfo::{System, SystemExt};
///
/// let s = System::new_all();
/// println!("users: {:?}", s.get_users());
/// ```
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug)]
pub struct User {
pub(crate) name: String,
pub(crate) groups: Vec<String>,
}

impl UserExt for User {
fn get_name(&self) -> &str {
&self.name
}

fn get_groups(&self) -> &[String] {
&self.groups
}
}

/// Type containing read and written bytes.
///
/// It is returned by [`ProcessExt::get_disk_usage`][crate::ProcessExt::get_disk_usage].
///
/// ```no_run
/// use sysinfo::{ProcessExt, System, SystemExt};
///
/// let s = System::new_all();
/// for (pid, process) in s.get_processes() {
/// let disk_usage = process.get_disk_usage();
/// println!("[{}] read bytes : new/total => {}/{}",
/// pid,
/// disk_usage.read_bytes,
/// disk_usage.total_read_bytes,
/// );
/// println!("[{}] written bytes: new/total => {}/{}",
/// pid,
/// disk_usage.written_bytes,
/// disk_usage.total_written_bytes,
/// );
/// }
/// ```
#[derive(Debug, Default, Clone, Copy, PartialEq, PartialOrd)]
pub struct DiskUsage {
/// Total number of written bytes.
pub total_written_bytes: u64,
/// Number of written bytes.
pub written_bytes: u64,
/// Total number of read bytes.
pub total_read_bytes: u64,
/// Number of read bytes.
pub read_bytes: u64,
}
10 changes: 8 additions & 2 deletions src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,17 @@ impl fmt::Debug for NetworkData {
.field("packets income", &self.get_packets_received())
.field("total packets income", &self.get_total_packets_received())
.field("packets outcome", &self.get_packets_transmitted())
.field("total packets outcome", &self.get_total_packets_transmitted())
.field(
"total packets outcome",
&self.get_total_packets_transmitted(),
)
.field("errors income", &self.get_errors_on_received())
.field("total errors income", &self.get_total_errors_on_received())
.field("errors outcome", &self.get_errors_on_transmitted())
.field("total errors outcome", &self.get_total_errors_on_transmitted())
.field(
"total errors outcome",
&self.get_total_errors_on_transmitted(),
)
.finish()
}
}
47 changes: 47 additions & 0 deletions src/linux/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::path::{Path, PathBuf};

use libc::{c_int, gid_t, kill, uid_t};

use DiskUsage;
use Pid;
use ProcessExt;

Expand Down Expand Up @@ -124,6 +125,10 @@ pub struct Process {
/// Tasks run by this process.
pub tasks: HashMap<Pid, Process>,
pub(crate) stat_file: Option<File>,
old_read_bytes: u64,
old_written_bytes: u64,
read_bytes: u64,
written_bytes: u64,
}

impl ProcessExt for Process {
Expand Down Expand Up @@ -155,6 +160,10 @@ impl ProcessExt for Process {
HashMap::new()
},
stat_file: None,
old_read_bytes: 0,
old_written_bytes: 0,
read_bytes: 0,
written_bytes: 0,
}
}

Expand Down Expand Up @@ -215,6 +224,15 @@ impl ProcessExt for Process {
fn cpu_usage(&self) -> f32 {
self.cpu_usage
}

fn get_disk_usage(&self) -> DiskUsage {
DiskUsage {
written_bytes: self.written_bytes - self.old_written_bytes,
total_written_bytes: self.written_bytes,
read_bytes: self.read_bytes - self.old_read_bytes,
total_read_bytes: self.read_bytes,
}
}
}

impl Drop for Process {
Expand Down Expand Up @@ -244,3 +262,32 @@ pub fn set_time(p: &mut Process, utime: u64, stime: u64) {
pub fn has_been_updated(p: &Process) -> bool {
p.updated
}

pub(crate) fn update_process_disk_activity(p: &mut Process, path: &Path) {
let mut path = PathBuf::from(path);
path.push("io");
let data = match super::system::get_all_data(&path, 16_384) {
Ok(d) => d,
Err(_) => return,
};
let mut done = 0;
for line in data.split("\n") {
let mut parts = line.split(": ");
match parts.next() {
Some("read_bytes") => {
p.old_read_bytes = p.read_bytes;
p.read_bytes = parts.next().and_then(|x| x.parse::<u64>().ok()).unwrap_or(0);
}
Some("write_bytes") => {
p.old_written_bytes = p.written_bytes;
p.written_bytes = parts.next().and_then(|x| x.parse::<u64>().ok()).unwrap_or(0);
}
_ => continue,
}
done += 1;
if done > 1 {
// No need to continue the reading.
break;
}
}
}
Loading

0 comments on commit f57aa89

Please sign in to comment.