|
| 1 | +use crate::config::*; |
1 | 2 | use std::cell::RefCell;
|
2 | 3 | use std::fs::File;
|
3 | 4 | use std::io::prelude::*;
|
4 | 5 | use std::io::BufReader;
|
5 |
| -use std::time::{Instant,Duration}; |
6 |
| -use std::thread; |
7 |
| -use std::sync::Arc; |
| 6 | +use std::rc::Rc; |
8 | 7 | use std::sync::atomic::{AtomicBool, Ordering};
|
| 8 | +use std::sync::Arc; |
| 9 | +use std::thread; |
| 10 | +use std::time::{Duration, Instant}; |
| 11 | +use timely::communication::Allocator; |
| 12 | +use timely::dataflow::operators::*; |
| 13 | +use timely::dataflow::ProbeHandle; |
| 14 | +use timely::worker::Worker; |
9 | 15 |
|
10 | 16 | pub struct MonitorThread {
|
11 | 17 | handle: thread::JoinHandle<Vec<(Duration, SystemUsage)>>,
|
@@ -33,30 +39,73 @@ impl MonitorThread {
|
33 | 39 |
|
34 | 40 | samples
|
35 | 41 | });
|
36 |
| - Some(MonitorThread{handle, running}) |
| 42 | + Some(MonitorThread { handle, running }) |
37 | 43 | }
|
38 | 44 |
|
39 | 45 | pub fn join(self) -> Vec<(Duration, SystemUsage)> {
|
40 | 46 | self.running.store(false, Ordering::SeqCst);
|
41 |
| - self.handle.join().expect("failed to join monitoring thread") |
| 47 | + self.handle |
| 48 | + .join() |
| 49 | + .expect("failed to join monitoring thread") |
42 | 50 | }
|
43 | 51 | }
|
44 | 52 |
|
45 |
| - |
46 | 53 | #[derive(Abomonation, Clone, Copy, Debug)]
|
47 | 54 | pub struct SystemUsage {
|
48 | 55 | cpu: CpuUsage,
|
49 | 56 | net: NetworkUsage,
|
50 |
| - mem: MemorySample |
| 57 | + mem: MemorySample, |
51 | 58 | }
|
52 | 59 |
|
53 | 60 | impl SystemUsage {
|
| 61 | + /// sets up a small dataflow to exchange information about the network exchanges |
| 62 | + pub fn collect_from_workers( |
| 63 | + worker: &mut Worker<Allocator>, |
| 64 | + usage: Vec<(Duration, SystemUsage)>, |
| 65 | + ) -> Vec<(Duration, String, SystemUsage)> { |
| 66 | + use timely::dataflow::channels::pact::Pipeline; |
| 67 | + |
| 68 | + let result = Rc::new(RefCell::new(Vec::new())); |
| 69 | + let result_read = Rc::clone(&result); |
| 70 | + |
| 71 | + let (mut input, probe) = worker.dataflow::<u32, _, _>(move |scope| { |
| 72 | + let (input, stream) = scope.new_input::<(Duration, String, SystemUsage)>(); |
| 73 | + let mut probe = ProbeHandle::new(); |
| 74 | + stream |
| 75 | + .exchange(|_| 0) |
| 76 | + .unary(Pipeline, "collector", move |_, _| { |
| 77 | + move |input, output| { |
| 78 | + input.for_each(|t, data| { |
| 79 | + let data = data.replace(Vec::new()); |
| 80 | + result.borrow_mut().extend(data.into_iter()); |
| 81 | + output.session(&t).give(()); |
| 82 | + }); |
| 83 | + } |
| 84 | + }) |
| 85 | + .probe_with(&mut probe); |
| 86 | + |
| 87 | + (input, probe) |
| 88 | + }); |
| 89 | + |
| 90 | + let host = get_hostname(); |
| 91 | + for (d, su) in usage { |
| 92 | + input.send((d, host.clone(), su)); |
| 93 | + } |
| 94 | + |
| 95 | + input.close(); |
| 96 | + worker.step_while(|| !probe.done()); |
| 97 | + |
| 98 | + result_read.replace(Vec::new()) |
| 99 | + } |
| 100 | + |
54 | 101 | pub fn compute(prev: &SystemSample, current: &SystemSample, elapsed: Duration) -> SystemUsage {
|
55 | 102 | let secs = elapsed.as_secs_f64();
|
56 | 103 | SystemUsage {
|
57 | 104 | cpu: CpuUsage {
|
58 |
| - user: (current.cpu.user - prev.cpu.user) as f64 / (current.cpu.total - prev.cpu.total) as f64, |
59 |
| - system: (current.cpu.system - prev.cpu.system) as f64 / (current.cpu.total - prev.cpu.total) as f64, |
| 105 | + user: (current.cpu.user - prev.cpu.user) as f64 |
| 106 | + / (current.cpu.total - prev.cpu.total) as f64, |
| 107 | + system: (current.cpu.system - prev.cpu.system) as f64 |
| 108 | + / (current.cpu.total - prev.cpu.total) as f64, |
60 | 109 | },
|
61 | 110 | net: NetworkUsage {
|
62 | 111 | tx: (current.net.tx - prev.net.tx) as f64 / secs,
|
@@ -122,8 +171,7 @@ impl CpuSample {
|
122 | 171 | .unwrap()
|
123 | 172 | .split_whitespace()
|
124 | 173 | .skip(1)
|
125 |
| - .map(|s| { |
126 |
| - s.parse::<u64>().expect("error parsing CPU stat")}), |
| 174 | + .map(|s| s.parse::<u64>().expect("error parsing CPU stat")), |
127 | 175 | );
|
128 | 176 | let buf = buf.borrow();
|
129 | 177 | let total: u64 = buf.iter().sum();
|
|
0 commit comments