Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6449b2b5f5503a2030efe24080d6d4c4707f5122 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2018 Red Hat, Inc.
 * 
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.internal.valgrind.massif;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot.SnapshotType;
import org.eclipse.linuxtools.internal.valgrind.massif.MassifSnapshot.TimeUnit;
import org.eclipse.linuxtools.valgrind.core.ValgrindParserUtils;
import org.eclipse.osgi.util.NLS;

public class MassifParser {
    private static final String COLON = ":"; //$NON-NLS-1$
    private static final String SPACE = " "; //$NON-NLS-1$
    private static final String EQUALS = "="; //$NON-NLS-1$

    private static final String CMD = "cmd"; //$NON-NLS-1$
    private static final String TIME_UNIT = "time_unit"; //$NON-NLS-1$
    private static final String SNAPSHOT = "snapshot"; //$NON-NLS-1$
    private static final String TIME = "time"; //$NON-NLS-1$
    private static final String MEM_HEAP_B = "mem_heap_B"; //$NON-NLS-1$
    private static final String MEM_HEAP_EXTRA_B = "mem_heap_extra_B"; //$NON-NLS-1$
    private static final String MEM_STACKS_B = "mem_stacks_B"; //$NON-NLS-1$
    private static final String HEAP_TREE = "heap_tree"; //$NON-NLS-1$

    private static final String INSTRUCTIONS = "i"; //$NON-NLS-1$
    private static final String MILLISECONDS = "ms"; //$NON-NLS-1$
    private static final String BYTES = "B"; //$NON-NLS-1$
    private static final String PEAK = "peak"; //$NON-NLS-1$
    private static final String DETAILED = "detailed"; //$NON-NLS-1$
    private static final String EMPTY = "empty"; //$NON-NLS-1$

    private Integer pid;
    private MassifSnapshot[] snapshots;

    public MassifParser(File inputFile) throws IOException {
        ArrayList<MassifSnapshot> list = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(inputFile))){
            String line;
            MassifSnapshot snapshot = null;
            String cmd = null;
            TimeUnit unit = null;
            int n = 0;

            // retrive PID from filename
            String filename = inputFile.getName();
            pid = ValgrindParserUtils.parsePID(filename, MassifLaunchDelegate.OUT_PREFIX);

            // parse contents of file
            while ((line = br.readLine()) != null) {
                if (line.startsWith(CMD + COLON)){
                    cmd = ValgrindParserUtils.parseStrValue(line, COLON + SPACE);
                } else if (line.startsWith(TIME_UNIT + COLON)) {
                    unit = parseTimeUnit(line);
                } else if (line.startsWith(SNAPSHOT)) {
                    if (snapshot != null) {
                        // this snapshot finished parsing
                        list.add(snapshot);
                        n++;
                    }
                    snapshot = new MassifSnapshot(n);
                    snapshot.setCmd(cmd);
                    snapshot.setUnit(unit);
                } else if (line.startsWith(TIME + EQUALS)) {
                    snapshot.setTime(ValgrindParserUtils.parseLongValue(line, EQUALS));
                } else if (line.startsWith(MEM_HEAP_B + EQUALS)) {
                    snapshot.setHeapBytes(ValgrindParserUtils.parseLongValue(line, EQUALS));
                } else if (line.startsWith(MEM_HEAP_EXTRA_B + EQUALS)) {
                    snapshot.setHeapExtra(ValgrindParserUtils.parseLongValue(line, EQUALS));
                } else if (line.startsWith(MEM_STACKS_B + EQUALS)) {
                    snapshot.setStacks(ValgrindParserUtils.parseLongValue(line, EQUALS));
                } else if (line.startsWith(HEAP_TREE + EQUALS)) {
                    SnapshotType type = parseSnapshotType(line);
                    snapshot.setType(type);
                    switch (type) {
                    case DETAILED:
                    case PEAK:
                        MassifHeapTreeNode node = parseTree(snapshot, null, br);
                        node.setText(NLS.bind(Messages.getString("MassifParser.Snapshot_n"), n, node.getText())); // prepend snapshot number //$NON-NLS-1$
                        snapshot.setRoot(node);
                    }
                }
            }
            if (snapshot != null) {
                // last snapshot that finished parsing
                list.add(snapshot);
            }
            snapshots = list.toArray(new MassifSnapshot[list.size()]);
        }
    }

    private MassifHeapTreeNode parseTree(MassifSnapshot snapshot, MassifHeapTreeNode parent, BufferedReader br) throws IOException {
        String line = br.readLine();
        if (line == null) {
            throw new IOException(Messages.getString("MassifParser.Unexpected_EOF")); //$NON-NLS-1$
        }
        line = line.trim(); // remove leading whitespace
        String[] parts = line.split(" "); //$NON-NLS-1$
        // bounds checking so we can fail with a more informative error
        if (parts.length < 2) {
            ValgrindParserUtils.fail(line);
        }

        Integer numChildren = parseNumChildren(parts[0]);
        if (numChildren == null) {
            ValgrindParserUtils.fail(line);
        }

        Long numBytes = parseNumBytes(parts[1]);
        if (numBytes == null) {
            ValgrindParserUtils.fail(line);
        }

        double percentage;
        if (numBytes.intValue() == 0) {
            percentage = 0;
        } else {
            percentage = numBytes.doubleValue() / snapshot.getTotal() * 100;
        }

        MassifHeapTreeNode node;
        String address = null;
        String function = null;
        String filename = null;
        int lineNo = 0;
        if (parts[2].startsWith("0x")) { //$NON-NLS-1$
            // we extend the above bounds checking
            if (parts.length < 3) {
                ValgrindParserUtils.fail(line);
            }
            // remove colon from address
            address = parts[2].substring(0, parts[2].length() - 1);

            function = parseFunction(parts[3], line);

            // Parse source file if specified
            Object[] subparts = ValgrindParserUtils.parseFilename(line);
            filename = (String) subparts[0];
            lineNo = (Integer) subparts[1];

            node = new MassifHeapTreeNode(parent, percentage, numBytes, address, function, filename, lineNo);
        } else {
            // concatenate the rest
        	StringBuilder text = new StringBuilder();
            for (int i = 2; i < parts.length; i++) {
                text.append(parts[i]);
                text.append(" "); //$NON-NLS-1$
            }

            node = new MassifHeapTreeNode(parent, percentage, numBytes, text.toString().trim());
        }


        for (int i = 0; i < numChildren.intValue(); i++) {
            node.addChild(parseTree(snapshot, node, br));
        }
        return node;
    }

    private String parseFunction(String start, String line) throws IOException {
        String function = null;
        int ix = line.lastIndexOf('(');
        if (ix >= 0) {
            function = line.substring(line.indexOf(start), ix);
        }
        else {
            function = line.substring(line.indexOf(start));
        }
        if (function != null) {
            function = function.trim();
        }
        else {
            ValgrindParserUtils.fail(line);
        }

        return function;
    }

    private Long parseNumBytes(String string) {
        Long result = null;
        if (ValgrindParserUtils.isNumber(string)) {
            result = Long.parseLong(string);
        }
        return result;
    }

    /*
     * format is "n[0-9]+:"
     */
    private Integer parseNumChildren(String string) {
        Integer result = null;
        if (string.length() >= 3) {
            String number = string.substring(1, string.length() - 1);
            if (ValgrindParserUtils.isNumber(number)) {
                result = Integer.parseInt(number);
            }
        }
        return result;
    }

    public Integer getPid() {
        return pid;
    }

    public MassifSnapshot[] getSnapshots() {
        return snapshots;
    }

    private SnapshotType parseSnapshotType(String line) throws IOException {
        SnapshotType result = null;
        String[] parts = line.split(EQUALS);
        if (parts.length > 1) {
            String type = parts[1];
            if (type.equals(EMPTY)) {
                result = SnapshotType.EMPTY;
            }
            else if (type.equals(DETAILED)) {
                result = SnapshotType.DETAILED;
            }
            else if (type.equals(PEAK)) {
                result = SnapshotType.PEAK;
            }
        }
        if (result == null) {
            ValgrindParserUtils.fail(line);
        }
        return result;
    }

    private TimeUnit parseTimeUnit(String line) throws IOException {
        TimeUnit result = null;
        String[] parts = line.split(COLON + SPACE);
        if (parts.length > 1) {
            String type = parts[1];
            if (type.equals(INSTRUCTIONS)) {
                result = TimeUnit.INSTRUCTIONS;
            }
            else if (type.equals(MILLISECONDS)) {
                result = TimeUnit.MILLISECONDS;
            }
            else if (type.equals(BYTES)) {
                result = TimeUnit.BYTES;
            }
        }
        if (result == null) {
            ValgrindParserUtils.fail(line);
        }
        return result;
    }
}

Back to the top