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
prelude! {}
use filter::gen::*;
#[derive(Debug, Clone)]
pub struct AllocSiteParams {
min_count: Option<usize>,
chart_gen: bool,
}
impl Default for AllocSiteParams {
fn default() -> Self {
Self {
min_count: None,
chart_gen: true,
}
}
}
impl AllocSiteParams {
pub fn new() -> Self {
Self::default()
}
}
type FileName = String;
pub struct AllocSiteWork {
map: BTMap<FileName, (usize, Option<uid::Filter>)>,
unk: usize,
}
impl AllocSiteWork {
pub fn new() -> Self {
Self {
map: BTMap::new(),
unk: 0,
}
}
pub fn inc(&mut self, file: Option<alloc::Str>) {
if let Some(file) = file {
file.str_do(|file| {
if let Some((count, _)) = self.map.get_mut(file) {
*count += 1
} else {
let prev = self.map.insert(file.to_string(), (1, None));
debug_assert!(prev.is_none())
}
})
} else {
self.unk += 1
}
}
pub fn scan(&mut self, data: &data::Data) {
for alloc in data.iter_allocs() {
alloc.alloc_site_do(|cloc_opt| self.inc(cloc_opt.map(|cloc| cloc.loc.file)))
}
}
pub fn generate_subfilter(file: &str) -> filter::sub::RawSubFilter {
let pred = filter::string_like::Pred::Contain;
let line = filter::loc::LineSpec::any();
let final_loc_spec = filter::loc::LocSpec::Value {
value: file.into(),
line,
};
let loc_spec = vec![filter::loc::LocSpec::Anything, final_loc_spec];
let filter = filter::loc::LocFilter::new(pred, loc_spec);
filter.into()
}
pub fn extract(&mut self, params: &AllocSiteParams) -> Res<Vec<Filter>> {
let mut res = Vec::with_capacity(self.map.len());
if self.map.is_empty() || (self.map.len() == 1 && self.unk == 0) {
return Ok(res);
}
let min_count = if let Some(min) = params.min_count {
min
} else {
0
};
let validate = |count: usize| min_count <= count;
let filter_count = self.map.iter().fold(
0,
|acc, (_, (count, _))| if validate(*count) { acc + 1 } else { acc },
);
for (file, (count, uid_opt)) in &mut self.map {
if validate(*count) {
let sub_filter = Self::generate_subfilter(&file);
let color = Color::BLACK.clone();
let mut spec = filter::FilterSpec::new(color);
spec.set_name(file.clone());
let mut filter = filter::Filter::new(spec)?;
filter.insert(sub_filter)?;
debug_assert_eq!(*uid_opt, None);
*uid_opt = Some(filter.uid());
res.push(filter)
}
}
res.shrink_to_fit();
res.sort_by(|lft, rgt| {
let lft = self.map.get(lft.name()).cloned().unwrap_or((0, None));
let rgt = self.map.get(rgt.name()).cloned().unwrap_or((0, None));
rgt.cmp(&lft)
});
let mut colors = Color::randoms(filter_count).into_iter();
for filter in &mut res {
filter.spec_mut().set_color(colors.next().expect(
"internal error, `filter_count` is not consistant with the actual filter count",
))
}
Ok(res)
}
pub fn chart_gen(self, params: &AllocSiteParams, filters: &Filters) -> Res<Vec<chart::Chart>> {
if params.chart_gen {
chart_gen::alloc_file_prefix(
filters,
self.map
.iter()
.filter_map(|(file, (_count, uid))| uid.map(|uid| (file, uid))),
)
} else {
chart_gen::default(filters)
}
}
}
#[derive(Debug, Clone, Copy)]
pub struct AllocSite;
const MIN_KEY: &str = "min";
const CHART_GEN_KEY: &str = "chart_gen";
impl FilterGenExt for AllocSite {
type Params = AllocSiteParams;
const KEY: &'static str = "alloc_site";
const FMT: Option<&'static str> = Some("min: <int>, chart_gen: <bool>");
fn work(data: &data::Data, params: Self::Params) -> Res<(Filters, Vec<chart::Chart>)> {
let mut work = AllocSiteWork::new();
work.scan(data);
let filters = work.extract(¶ms).map(Filters::new_with)?;
let charts = work.chart_gen(¶ms, &filters)?;
Ok((filters, charts))
}
fn parse_args(parser: Option<Parser>) -> Option<FilterGen> {
let mut parser = if let Some(parser) = parser {
parser
} else {
return Some(Self::Params::default().into());
};
let mut params = AllocSiteParams::default();
loop {
if parser.id_tag(MIN_KEY) {
parser.ws();
if !parser.char(':') {
return None;
}
parser.ws();
params.min_count = Some(parser.usize()?);
} else if parser.id_tag(CHART_GEN_KEY) {
parser.ws();
if !parser.char(':') {
return None;
}
parser.ws();
params.chart_gen = parser.bool()?;
} else {
return None;
}
parser.ws();
if parser.is_at_eoi() {
break;
} else if parser.char(',') {
parser.ws();
continue;
}
}
if !parser.is_at_eoi() {
return None;
}
Some(params.into())
}
fn add_help(s: &mut String) {
s.push_str(&format!(
"\
- allocation site generator: `{0} {{ {1} }}`
Generates one filter per allocation site, iff it is responsible for at least `{2}` allocations.
Defaults: `{2}: 1`.
\
",
Self::KEY,
Self::FMT.unwrap(),
MIN_KEY,
));
}
}