Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: d49bbd9247e9591d1ebdcd028095d6db50e6d11d (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
/*******************************************************************************
 * Copyright (c) 2009 Red Hat, Inc.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Elliott Baron <ebaron@redhat.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.valgrind.cachegrind;

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

import org.eclipse.linuxtools.valgrind.cachegrind.model.CachegrindDescription;
import org.eclipse.linuxtools.valgrind.cachegrind.model.CachegrindFile;
import org.eclipse.linuxtools.valgrind.cachegrind.model.CachegrindFunction;
import org.eclipse.linuxtools.valgrind.cachegrind.model.CachegrindLine;
import org.eclipse.linuxtools.valgrind.cachegrind.model.CachegrindOutput;
import org.eclipse.linuxtools.valgrind.core.AbstractValgrindTextParser;

public class CachegrindParser extends AbstractValgrindTextParser {
	private static final String CMD = "cmd"; //$NON-NLS-1$
	private static final String DESC = "desc"; //$NON-NLS-1$
	private static final String FL = "fl"; //$NON-NLS-1$
	private static final String FN = "fn"; //$NON-NLS-1$
	private static final String EVENTS = "events"; //$NON-NLS-1$
	private static final String SUMMARY = "summary"; //$NON-NLS-1$

	private static final String COMMA = ","; //$NON-NLS-1$

	protected static CachegrindParser instance;

	protected CachegrindParser() {
	}

	public static CachegrindParser getParser() {
		if (instance == null) {
			instance = new CachegrindParser();
		}
		return instance;
	}

	public void parse(CachegrindOutput output, File cgOut) throws IOException {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(cgOut));
			output.setPid(parsePID(cgOut.getName(), CachegrindLaunchDelegate.OUT_PREFIX));

			String line;
			CachegrindFile curFl = null;
			CachegrindFunction curFn = null;
			while ((line = br.readLine()) != null) {		
				if (line.startsWith(EVENTS + COLON)) {
					output.setEvents(parseStrValue(line, COLON + SPACE).split(SPACE));
				}
				else if (line.startsWith(CMD + COLON)) {
					output.setCommand(parseStrValue(line, COLON + SPACE));
				}
				else if (line.startsWith(DESC + COLON)) {
					CachegrindDescription description = parseDescription(line);
					output.addDescription(description);
				}
				else if (line.startsWith(FL + EQUALS)) {
					curFl = new CachegrindFile(output, parseStrValue(line, EQUALS));
					output.addFile(curFl);
				}
				else if (line.startsWith(FN + EQUALS)) {				
					if (curFl != null) {
						curFn = new CachegrindFunction(curFl, parseStrValue(line, EQUALS));
						curFl.addFunction(curFn);
					}
					else {
						fail(line);
					}
				}
				else if (line.startsWith(SUMMARY + COLON)) {
					long[] summary = parseData(line, parseStrValue(line, COLON + SPACE).split(SPACE));
					output.setSummary(summary);
				}
				else { // line data
					String[] tokens = line.split(SPACE, 2);
					if (isNumber(tokens[0])) {
						int lineNo = Integer.parseInt(tokens[0]);

						long[] data = parseData(line, tokens[1].split(SPACE));
						if (curFn != null) {
							curFn.addLine(new CachegrindLine(curFn, lineNo, data));
						}
						else {
							fail(line);
						}
					}
					else {
						fail(line);
					}
				}
			}
		} finally {
			if (br != null) {
				br.close();
			}
		}
	}

	private long[] parseData(String line, String[] data) throws IOException {
		long[] result = new long[data.length];
		for (int i = 0; i < data.length; i++) {
			if (!isNumber(data[i])) {
				fail(line);
			}
			result[i] = Long.parseLong(data[i]);
		}
		return result;
	}

	private CachegrindDescription parseDescription(String line) throws IOException {
		CachegrindDescription desc = null;
		String[] tokens = line.split(COLON + "\\s+"); //$NON-NLS-1$
		if (tokens.length == 3) {
			String name = tokens[1];
			tokens = tokens[2].split(COMMA + SPACE);
			if (tokens.length == 3) {
				desc = new CachegrindDescription(name, tokens[0], tokens[1], tokens[2]);
			}
			else {
				fail(line);
			}
		}
		else {
			fail(line);
		}
		return desc;
	}
}

Back to the top