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
use std::io::{self, BufRead};

use log::warn;

use crate::collapse::common::Occurrences;
use crate::collapse::Collapse;

// The call graph begins after this line.
static HEADER: &str = "Function Stack,CPU Time:Self,Module";

/// `vtune` folder configuration options.
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct Options {
    /// Don't include modules with function names.
    ///
    /// Default is `false`.
    pub no_modules: bool,
}

/// A stack collapser for CSV call graphs created with the VTune `amplxe-cl` tool.
///
/// To construct one, either use `vtune::Folder::default()` or create an [`Options`] and use
/// `vtune::Folder::from(options)`.
#[derive(Clone, Default)]
pub struct Folder {
    /// Function on the stack in this entry thus far.
    stack: Vec<String>,

    opt: Options,
}

impl Collapse for Folder {
    fn collapse<R, W>(&mut self, mut reader: R, writer: W) -> io::Result<()>
    where
        R: io::BufRead,
        W: io::Write,
    {
        // Consume the header...
        let mut line = Vec::new();
        loop {
            line.clear();
            if reader.read_until(0x0A, &mut line)? == 0 {
                warn!("File ended before header");
                return Ok(());
            };
            let l = String::from_utf8_lossy(&line);
            if l.starts_with(HEADER) {
                break;
            }
        }

        // Process the data...
        let mut occurrences = Occurrences::new(1);
        loop {
            line.clear();
            if reader.read_until(0x0A, &mut line)? == 0 {
                break;
            }
            let l = String::from_utf8_lossy(&line);
            let line = l.trim_end();
            if line.is_empty() {
                continue;
            } else {
                self.on_line(line, &mut occurrences)?;
            }
        }

        // Write the results...
        occurrences.write_and_clear(writer)?;

        // Reset the state...
        self.stack.clear();
        Ok(())
    }

    /// Check for header
    fn is_applicable(&mut self, input: &str) -> Option<bool> {
        let mut input = input.as_bytes();
        let mut line = String::new();
        loop {
            line.clear();
            if let Ok(n) = input.read_line(&mut line) {
                if n == 0 {
                    break;
                }
            } else {
                return Some(false);
            }

            if line.starts_with(HEADER) {
                return Some(true);
            }
        }
        None
    }
}

impl From<Options> for Folder {
    fn from(opt: Options) -> Self {
        Folder {
            opt,
            ..Default::default()
        }
    }
}

impl Folder {
    fn line_parts<'a>(&self, line: &'a str) -> Option<(&'a str, &'a str, &'a str)> {
        let mut line = if let Some(line) = line.strip_prefix('"') {
            // The function name will be in quotes if it contains spaces.
            line.splitn(2, "\",")
        } else {
            // We split on a string because we need to match the type of the other if branch.
            #[allow(clippy::single_char_pattern)]
            line.splitn(2, ",")
        };

        let func = line.next()?;
        let mut line = line.next()?.splitn(2, ',');
        let time = line.next()?;
        let module = if self.opt.no_modules {
            ""
        } else {
            line.next()?
        };

        Some((func, time, module))
    }

    fn on_line(&mut self, line: &str, occurrences: &mut Occurrences) -> io::Result<()> {
        if let Some(spaces) = line.find(|c| c != ' ') {
            let prev_depth = self.stack.len();
            let depth = spaces + 1;

            if depth <= prev_depth {
                // If the depth of this line is less than the previous one,
                // it means the previous line was a leaf node and we should
                // pop the stack back to one before the current depth.
                for _ in 0..=prev_depth - depth {
                    self.stack.pop();
                }
            } else if depth > prev_depth + 1 {
                return invalid_data_error!("Skipped indentation level at line:\n{}", line);
            }

            if let Some((func, time, module)) = self.line_parts(&line[spaces..]) {
                if let Ok(time) = time.parse::<f64>() {
                    let time_ms = (time * 1000.0).round() as usize;
                    if module.is_empty() {
                        self.stack.push(func.to_string());
                    } else {
                        self.stack.push(format!("{}`{}", module, func));
                    }
                    if time_ms > 0 {
                        self.write_stack(occurrences, time_ms);
                    }
                } else {
                    return invalid_data_error!("Invalid `CPU Time:Self` field: {}", time);
                }
            } else {
                return invalid_data_error!("Unable to parse stack line:\n{}", line);
            }
        }

        Ok(())
    }

    fn write_stack(&self, occurrences: &mut Occurrences, time: usize) {
        occurrences.insert(self.stack.join(";"), time);
    }
}