-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.rs
More file actions
515 lines (462 loc) · 15.2 KB
/
Copy pathdata.rs
File metadata and controls
515 lines (462 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
pub mod aperf_runlog;
pub mod aperf_stats;
pub mod constants;
pub mod cpu_utilization;
pub mod diskstats;
pub mod flamegraphs;
pub mod interrupts;
pub mod java_profile;
pub mod kernel_config;
pub mod meminfodata;
pub mod netstat;
pub mod perf_profile;
pub mod perf_stat;
pub mod processes;
pub mod sysctldata;
pub mod systeminfo;
pub mod utils;
pub mod vmstat;
use crate::utils::DataMetrics;
use crate::visualizer::{GetData, ReportParams};
use crate::{noop, InitParams, APERF_FILE_FORMAT};
use anyhow::Result;
use aperf_runlog::AperfRunlog;
use aperf_stats::AperfStat;
use chrono::prelude::*;
use cpu_utilization::{CpuUtilization, CpuUtilizationRaw};
use diskstats::{Diskstats, DiskstatsRaw};
use flamegraphs::{Flamegraph, FlamegraphRaw};
use interrupts::{InterruptData, InterruptDataRaw};
use java_profile::{JavaProfile, JavaProfileRaw};
use kernel_config::KernelConfig;
use log::trace;
use meminfodata::{MeminfoData, MeminfoDataRaw};
use netstat::{Netstat, NetstatRaw};
use nix::sys::{signal, signal::Signal};
use perf_profile::{PerfProfile, PerfProfileRaw};
use perf_stat::{PerfStat, PerfStatRaw};
use processes::{Processes, ProcessesRaw};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::fs::{File, OpenOptions};
use std::ops::Sub;
use std::path::PathBuf;
use sysctldata::SysctlData;
use systeminfo::SystemInfo;
use vmstat::{Vmstat, VmstatRaw};
#[derive(Clone, Debug)]
pub struct CollectorParams {
pub collection_time: u64,
pub elapsed_time: u64,
pub data_file_path: PathBuf,
pub data_dir: PathBuf,
pub run_name: String,
pub profile: HashMap<String, String>,
pub tmp_dir: PathBuf,
pub signal: Signal,
pub runlog: PathBuf,
pub pmu_config: Option<PathBuf>,
pub perf_frequency: u32,
}
impl CollectorParams {
fn new() -> Self {
CollectorParams {
collection_time: 0,
elapsed_time: 0,
data_file_path: PathBuf::new(),
data_dir: PathBuf::new(),
run_name: String::new(),
profile: HashMap::new(),
tmp_dir: PathBuf::new(),
signal: signal::SIGTERM,
runlog: PathBuf::new(),
pmu_config: Option::None,
perf_frequency: 99,
}
}
}
pub struct DataType {
pub data: Data,
pub file_handle: Option<File>,
pub file_name: String,
pub full_path: String,
pub dir_name: String,
pub is_static: bool,
pub is_profile_option: bool,
pub collector_params: CollectorParams,
}
impl DataType {
pub fn new(data: Data, file_name: String, is_static: bool) -> Self {
DataType {
data,
file_handle: None,
file_name,
full_path: String::new(),
dir_name: String::new(),
is_static,
is_profile_option: false,
collector_params: CollectorParams::new(),
}
}
pub fn set_file_handle(&mut self, handle: Option<File>) {
self.file_handle = handle;
}
pub fn is_profile_option(&mut self) {
self.is_profile_option = true;
}
pub fn set_signal(&mut self, signal: Signal) {
self.collector_params.signal = signal;
}
pub fn init_data_type(&mut self, param: &InitParams) -> Result<()> {
trace!("Initializing data type...");
let name = format!(
"{}_{}.{}",
self.file_name, param.time_str, APERF_FILE_FORMAT
);
self.file_name = name.clone();
self.full_path = format!("{}/{}", param.dir_name, name);
self.dir_name = param.dir_name.clone();
self.collector_params.run_name = param.dir_name.clone();
self.collector_params.collection_time = param.period;
self.collector_params.elapsed_time = 0;
self.collector_params.data_file_path = PathBuf::from(&self.full_path);
self.collector_params.data_dir = PathBuf::from(param.dir_name.clone());
self.collector_params.profile = param.profile.clone();
self.collector_params.tmp_dir = param.tmp_dir.clone();
self.collector_params.runlog = param.runlog.clone();
self.collector_params.pmu_config = param.pmu_config.clone();
self.file_handle = Some(
OpenOptions::new()
.read(true)
.create(true)
.append(true)
.open(&self.full_path)
.expect("Could not create file for data"),
);
Ok(())
}
pub fn prepare_data_collector(&mut self) -> Result<()> {
trace!("Preparing data collector...");
self.data.prepare_data_collector(&self.collector_params)?;
Ok(())
}
pub fn collect_data(&mut self) -> Result<()> {
trace!("Collecting Data...");
self.data.collect_data(&self.collector_params)?;
Ok(())
}
pub fn write_to_file(&mut self) -> Result<()> {
trace!("Writing to file...");
let file_handle = self.file_handle.as_ref().unwrap();
bincode::serialize_into(file_handle.try_clone()?, &self.data)?;
Ok(())
}
pub fn finish_data_collection(&mut self) -> Result<()> {
trace!("Finish data collection...");
self.data.finish_data_collection(&self.collector_params)?;
Ok(())
}
pub fn after_data_collection(&mut self) -> Result<()> {
trace!("Running post collection actions...");
self.data.after_data_collection(&self.collector_params)?;
Ok(())
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
pub enum TimeEnum {
DateTime(DateTime<Utc>),
TimeDiff(u64),
}
impl Sub for TimeEnum {
type Output = TimeEnum;
fn sub(self, rhs: TimeEnum) -> TimeEnum {
let self_time = match self {
TimeEnum::DateTime(value) => value,
_ => panic!("Cannot perform subtract op on TimeEnum::TimeDiff"),
};
let other_time = match rhs {
TimeEnum::DateTime(value) => value,
_ => panic!("Cannot perform subtract op on TimeEnum::TimeDiff"),
};
let time_diff = (self_time - other_time).num_milliseconds() as u64;
// Round up to the nearest second
TimeEnum::TimeDiff((time_diff + 500) / 1000)
}
}
/// Create a Data Enum
///
/// Each enum type will have a collect_data implemented for it.
macro_rules! data {
( $( $x:ident ),* ) => {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum Data {
$(
$x($x),
)*
}
impl Data {
fn collect_data(&mut self, params: &CollectorParams) -> Result<()> {
match self {
$(
Data::$x(ref mut value) => value.collect_data(¶ms)?,
)*
}
Ok(())
}
fn prepare_data_collector(&mut self, params: &CollectorParams) -> Result<()> {
match self {
$(
Data::$x(ref mut value) => value.prepare_data_collector(params)?,
)*
}
Ok(())
}
fn finish_data_collection(&mut self, params: &CollectorParams) -> Result<()> {
match self {
$(
Data::$x(ref mut value) => value.finish_data_collection(params)?,
)*
}
Ok(())
}
fn after_data_collection(&mut self, params: &CollectorParams) -> Result<()> {
match self {
$(
Data::$x(ref mut value) => value.after_data_collection(params)?,
)*
}
Ok(())
}
}
};
}
macro_rules! processed_data {
( $( $x:ident ),* ) => {
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum ProcessedData {
$(
$x($x),
)*
}
impl ProcessedData {
pub fn process_raw_data(&mut self, buffer: Data) -> Result<ProcessedData> {
match self {
$(
ProcessedData::$x(ref mut value) => Ok(value.process_raw_data(buffer)?),
)*
}
}
pub fn custom_raw_data_parser(&mut self, parser_params: ReportParams) -> Result<Vec<ProcessedData>> {
match self {
$(
ProcessedData::$x(ref mut value) => Ok(value.custom_raw_data_parser(parser_params)?),
)*
}
}
pub fn get_data(&mut self, values: Vec<ProcessedData>, query: String, metrics: &mut DataMetrics) -> Result<String> {
match self {
$(
ProcessedData::$x(ref mut value) => Ok(value.get_data(values, query, metrics)?),
)*
}
}
pub fn get_calls(&mut self) -> Result<Vec<String>> {
match self {
$(
ProcessedData::$x(ref mut value) => Ok(value.get_calls()?),
)*
}
}
}
};
}
data!(
CpuUtilizationRaw,
VmstatRaw,
DiskstatsRaw,
SystemInfo,
KernelConfig,
InterruptDataRaw,
SysctlData,
PerfStatRaw,
ProcessesRaw,
MeminfoDataRaw,
NetstatRaw,
PerfProfileRaw,
FlamegraphRaw,
JavaProfileRaw
);
processed_data!(
CpuUtilization,
Vmstat,
Diskstats,
SystemInfo,
KernelConfig,
InterruptData,
SysctlData,
PerfStat,
Processes,
MeminfoData,
Netstat,
PerfProfile,
Flamegraph,
AperfStat,
AperfRunlog,
JavaProfile
);
pub trait CollectData {
fn prepare_data_collector(&mut self, _params: &CollectorParams) -> Result<()> {
noop!();
Ok(())
}
fn collect_data(&mut self, _params: &CollectorParams) -> Result<()> {
noop!();
Ok(())
}
fn finish_data_collection(&mut self, _params: &CollectorParams) -> Result<()> {
noop!();
Ok(())
}
fn after_data_collection(&mut self, _params: &CollectorParams) -> Result<()> {
noop!();
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::cpu_utilization::CpuUtilizationRaw;
use super::{CollectorParams, Data, DataType, TimeEnum};
use crate::InitParams;
use chrono::prelude::*;
use std::fs;
use std::path::Path;
#[test]
fn test_data_type_init() {
let mut param = InitParams::new("".to_string());
let data = CpuUtilizationRaw::new();
let mut dt = DataType {
data: Data::CpuUtilizationRaw(data),
file_handle: None,
file_name: "cpu_utilization".to_string(),
full_path: String::new(),
dir_name: String::new(),
is_static: false,
is_profile_option: false,
collector_params: CollectorParams::new(),
};
param.dir_name = format!("./performance_data_init_test_{}", param.time_str);
fs::DirBuilder::new()
.recursive(true)
.create(param.dir_name.clone())
.unwrap();
dt.init_data_type(¶m).unwrap();
assert!(dt.file_handle.is_some());
fs::remove_file(dt.full_path).unwrap();
fs::remove_dir_all(dt.dir_name).unwrap();
}
#[test]
fn test_print() {
let mut param = InitParams::new("".to_string());
let data = CpuUtilizationRaw::new();
let mut dt = DataType {
data: Data::CpuUtilizationRaw(data),
file_handle: None,
file_name: "cpu_utilization".to_string(),
full_path: String::new(),
dir_name: String::new(),
is_static: false,
is_profile_option: false,
collector_params: CollectorParams::new(),
};
param.dir_name = format!("./performance_data_print_test_{}", param.time_str);
fs::DirBuilder::new()
.recursive(true)
.create(param.dir_name.clone())
.unwrap();
dt.init_data_type(¶m).unwrap();
assert!(Path::new(&dt.full_path).exists());
dt.write_to_file().unwrap();
loop {
match bincode::deserialize_from::<_, Data>(dt.file_handle.as_ref().unwrap()) {
Ok(v) => match v {
Data::CpuUtilizationRaw(ref value) => assert!(value.data.is_empty()),
_ => unreachable!(),
},
Err(e) => match *e {
bincode::ErrorKind::Io(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => {
break
}
_ => unreachable!(),
},
};
}
fs::remove_file(dt.full_path).unwrap();
fs::remove_dir_all(dt.dir_name).unwrap();
}
#[test]
fn test_time_diff_second() {
let time_zero = Utc::now();
let one_second = chrono::Duration::seconds(1);
let time_one = time_zero + one_second;
let time_t0 = TimeEnum::DateTime(time_zero);
let time_t1 = TimeEnum::DateTime(time_one);
let time_diff = time_t1 - time_t0;
match time_diff {
TimeEnum::TimeDiff(value) => assert!(value == 1, "Time diff was expected to be 1"),
_ => unreachable!(),
}
}
#[test]
fn test_time_diff_one_milli_second() {
let time_zero = Utc::now();
let one_millisecond = chrono::Duration::milliseconds(1);
let time_one = time_zero + one_millisecond;
let time_t0 = TimeEnum::DateTime(time_zero);
let time_t1 = TimeEnum::DateTime(time_one);
let time_diff = time_t1 - time_t0;
match time_diff {
TimeEnum::TimeDiff(value) => assert!(value == 0, "Time diff was expected to be 0"),
_ => unreachable!(),
}
}
#[test]
fn test_time_diff_just_less_than_one_second() {
let time_zero = Utc::now();
let just_less_than_one_second = chrono::Duration::milliseconds(992);
let time_one = time_zero + just_less_than_one_second;
let time_t0 = TimeEnum::DateTime(time_zero);
let time_t1 = TimeEnum::DateTime(time_one);
let time_diff = time_t1 - time_t0;
match time_diff {
TimeEnum::TimeDiff(value) => assert!(value == 1, "Time diff was expected to be 1"),
_ => unreachable!(),
}
}
#[test]
fn test_time_diff_half_second() {
let time_zero = Utc::now();
let half_second = chrono::Duration::milliseconds(500);
let time_one = time_zero + half_second;
let time_t0 = TimeEnum::DateTime(time_zero);
let time_t1 = TimeEnum::DateTime(time_one);
let time_diff = time_t1 - time_t0;
match time_diff {
TimeEnum::TimeDiff(value) => assert!(value == 1, "Time diff was expected to be 1"),
_ => unreachable!(),
}
}
#[test]
#[should_panic]
fn test_time_diff_unsupported_sub_op() {
let time_t0 = TimeEnum::TimeDiff(0);
let time_t1 = TimeEnum::TimeDiff(1);
let _diff = time_t1 - time_t0;
}
#[test]
#[should_panic]
fn test_time_diff_mixed_type_sub_op() {
let time_t0 = TimeEnum::TimeDiff(0);
let time_t1 = TimeEnum::DateTime(Utc::now());
let _diff = time_t1 - time_t0;
}
}