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
/*<LICENSE>
    This file is part of Memthol.

    Copyright (C) 2020 OCamlPro.

    Memthol is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Memthol is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Memthol.  If not, see <https://www.gnu.org/licenses/>.
*/

//! Allocation-site-file-based automatic filter generation.
//!
//! Parameterized with an optional `min_count: usize`. This generator generates one filter per
//! allocation-site-file with at least `min_count` allocations in it. Note that the behavior is the
//! same when `min_count` is `0` or when it is `1`.
//!
//! When no `min_count` parameter is present, the current behavior is the same as `min_count == 1`.

prelude! {}

use filter::gen::*;

/// Parameters for the alloc-site generator.
#[derive(Debug, Clone)]
pub struct AllocSiteParams {
    /// Minimum number of allocations needed for a filter to be created for a given file.
    min_count: Option<usize>,
    /// Chart generation.
    chart_gen: bool,
}
impl Default for AllocSiteParams {
    fn default() -> Self {
        Self {
            min_count: None,
            chart_gen: true,
        }
    }
}

impl AllocSiteParams {
    /// Constructor.
    pub fn new() -> Self {
        Self::default()
    }
}

type FileName = String;

/// Actual alloc-site generator worker.
pub struct AllocSiteWork {
    /// Maps file names to the number of allocations in them.
    map: BTMap<FileName, (usize, Option<uid::Filter>)>,
    /// Number of allocations in unknown files.
    unk: usize,
}

impl AllocSiteWork {
    /// Constructor.
    pub fn new() -> Self {
        Self {
            map: BTMap::new(),
            unk: 0,
        }
    }

    /// Increments by one the number of allocations in `file`.
    ///
    /// If `None`, `file` is treated as the unknown file.
    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
        }
    }

    /// Scans the input data to populate the map from file names to allocation count.
    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)))
        }
    }

    /// Generates a subfilter for a specific file name.
    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()
    }

    /// Extracts allocation-site-file filters.
    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 {
            // let avg = self.map.values().fold(0, |acc, cnt| acc + *cnt) / self.map.len();
            // avg / 20
            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();

        // Rev-sorting by number of allocations. Note that the order does not matter as the filter
        // exact-match different allocation-site-files.
        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));
            // rev-sorting
            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",
            ))
        }

        // log::info!("allocation sites:");
        // for (file, (count, uid)) in &self.map {
        //     log::info!("    {:>30}: {}, captured by {:?}", file, count, uid)
        // }

        Ok(res)
    }

    /// Runs chart generation.
    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)
        }
    }
}

/// Unit-struct handling CLAP and creating/running the actual generator.
#[derive(Debug, Clone, Copy)]
pub struct AllocSite;

/// Name of the `min` key.
const MIN_KEY: &str = "min";
/// Name of the `chart_gen` key.
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(&params).map(Filters::new_with)?;
        let charts = work.chart_gen(&params, &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,
        ));
    }
}