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
extern crate ndarray;
extern crate regex;
use std::process::Command;
use self::ndarray::{Axis, stack, OwnedArray, ArrayView, Ix};
use profiler::Profiler;
use std::cmp::Ordering::Less;
use err::ProfError;
use regex::Regex;
pub type Mat<A> = OwnedArray<A, (Ix, Ix)>;
pub enum Metric {
Ir,
I1mr,
ILmr,
Dr,
D1mr,
DLmr,
Dw,
D1mw,
DLmw,
NAN,
}
pub fn sort_matrix(mat: &Mat<f64>, sort_col: ArrayView<f64, Ix>) -> (Mat<f64>, Vec<usize>) {
let mut enum_col = sort_col.iter().enumerate().collect::<Vec<(usize, &f64)>>();
enum_col.sort_by(|a, &b| a.1.partial_cmp(b.1).unwrap_or(Less).reverse());
let indices = enum_col.iter().map(|x| x.0).collect::<Vec<usize>>();
(mat.select(Axis(0), indices.as_slice()), indices)
}
pub trait CacheGrindParser {
fn cachegrind_cli(&self, binary: &str) -> Result<String, ProfError>;
fn cachegrind_parse<'b>(&'b self,
output: &'b str,
num: usize,
sort_metric: Metric)
-> Result<Profiler, ProfError>;
}
impl CacheGrindParser for Profiler {
fn cachegrind_cli(&self, binary: &str) -> Result<String, ProfError> {
let _ = Command::new("valgrind")
.arg("--tool=cachegrind")
.arg("--cachegrind-out-file=cachegrind.out")
.arg(binary)
.output()
.or(Err(ProfError::CliError));
let cachegrind_output = Command::new("cg_annotate")
.arg("cachegrind.out")
.arg(binary)
.output()
.or(Err(ProfError::CliError));
cachegrind_output.and_then(|x| String::from_utf8(x.stdout).or(Err(ProfError::UTF8Error)))
.or(Err(ProfError::CliError))
}
fn cachegrind_parse<'b>(&'b self,
output: &'b str,
num: usize,
sort_metric: Metric)
-> Result<Profiler, ProfError> {
let mut out_split: Vec<&'b str> = output.split("\n").collect();
lazy_static! {
static ref CACHEGRIND_REGEX : Regex = Regex::new(r"\d+\s*[a-zA-Z]*$*_*:*/+\.*@*-*|\d+\s*[a-zA-Z]*$*_*\?+:*/*\.*-*@*-*").unwrap();
static ref COMPILER_TRASH: Regex = Regex::new(r"\$\w{2}\$|\$\w{3}\$").unwrap();
static ref ERROR_REGEX : Regex = Regex::new(r"Valgrind's memory management: out of memory").unwrap();
}
let errs = out_split.to_owned()
.into_iter()
.filter(|x| ERROR_REGEX.is_match(x))
.collect::<Vec<_>>();
if errs.len() > 0 {
return Err(ProfError::OutOfMemoryError);
}
out_split.retain(|x| CACHEGRIND_REGEX.is_match(x));
let mut funcs: Vec<String> = Vec::new();
let mut data_vec: Vec<Mat<f64>> = Vec::new();
for sample in out_split.iter() {
let mut elems = sample.trim()
.split(" ")
.collect::<Vec<&'b str>>();
elems.retain(|x| x.to_string() != "");
let mut numbers = Vec::new();
for elem in elems[0..elems.len() - 1].iter() {
let number = match elem.trim().replace(",", "").parse::<f64>() {
Ok(n) => n,
Err(_) => return Err(ProfError::RegexError),
};
numbers.push(number);
}
if let Ok(data_col) = OwnedArray::from_shape_vec((numbers.len(), 1), numbers) {
data_vec.push(data_col);
}
let path = elems[elems.len() - 1].split("/").collect::<Vec<&'b str>>();
let func = path[path.len() - 1];
let mut func = COMPILER_TRASH.replace_all(func, "");
let idx = func.rfind("::").unwrap_or(func.len());
func.drain(idx..).collect::<String>();
funcs.push(func);
}
let data_matrix = match stack(Axis(1),
&data_vec.iter()
.map(|x| x.view())
.collect::<Vec<_>>()
.as_slice()) {
Ok(m) => m.t().to_owned(),
Err(_) => return Err(ProfError::MisalignedData),
};
let sort_col = match sort_metric {
Metric::Ir => data_matrix.column(0),
Metric::I1mr => data_matrix.column(1),
Metric::ILmr => data_matrix.column(2),
Metric::Dr => data_matrix.column(3),
Metric::D1mr => data_matrix.column(4),
Metric::DLmr => data_matrix.column(5),
Metric::Dw => data_matrix.column(6),
Metric::D1mw => data_matrix.column(7),
Metric::DLmw => data_matrix.column(8),
Metric::NAN => data_matrix.column(0),
};
let (mut sorted_data_matrix, indices) = sort_matrix(&data_matrix, sort_col);
let mut sorted_funcs: Vec<String> = indices.iter()
.map(|&x| (&funcs[x]).to_owned())
.collect::<Vec<String>>();
let ir = sorted_data_matrix.column(0).scalar_sum();
let i1mr = sorted_data_matrix.column(1).scalar_sum();
let ilmr = sorted_data_matrix.column(2).scalar_sum();
let dr = sorted_data_matrix.column(3).scalar_sum();
let d1mr = sorted_data_matrix.column(4).scalar_sum();
let dlmr = sorted_data_matrix.column(5).scalar_sum();
let dw = sorted_data_matrix.column(6).scalar_sum();
let d1mw = sorted_data_matrix.column(7).scalar_sum();
let dlmw = sorted_data_matrix.column(8).scalar_sum();
if num < sorted_data_matrix.rows() {
let ls = (0..num).collect::<Vec<_>>();
sorted_data_matrix = sorted_data_matrix.select(Axis(0), ls.as_slice());
sorted_funcs = sorted_funcs.iter()
.take(num)
.cloned()
.collect();
}
Ok(Profiler::CacheGrind {
ir: ir,
i1mr: i1mr,
ilmr: ilmr,
dr: dr,
d1mr: d1mr,
dlmr: dlmr,
dw: dw,
d1mw: d1mw,
dlmw: dlmw,
data: sorted_data_matrix,
functs: sorted_funcs,
})
}
}
#[cfg(test)]
mod test {
#[test]
fn test_cachegrind_parse_1() {
assert_eq!(1, 1);
}
#[test]
fn test_cachegrind_parse_2() {
assert_eq!(1, 1);
assert_eq!(1, 1);
}
#[test]
fn test_cachegrind_parse_3() {
assert_eq!(1, 1);
}
}