Skip to main content
summaryrefslogtreecommitdiffstats
blob: f233a0a1db77da84ac1f63015d95433a20e1360a (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*******************************************************************************
 * 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:
 *     Red Hat - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.callgraph;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

import org.eclipse.cdt.core.model.CoreModel;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.linuxtools.callgraph.core.Helper;
import org.eclipse.linuxtools.callgraph.core.SystemTapParser;
import org.eclipse.linuxtools.callgraph.core.SystemTapUIErrorMessages;
import org.eclipse.swt.widgets.Shell;


/**
 * This class is used only in the case that we are rendering a graph
 * using GEF (specifically Zest).  
 *
 * After a stap command is sent to be executed, and after data is stored
 * into some temporary file, the data must be parsed to be used. This class
 * handles all of the parsing. All data is stored into Maps and this class
 * also starts the job responsible for taking the parsed data and rendering it.
 */
public class StapGraphParser extends SystemTapParser {
	
	private static final String NEW_LINE = "\n";
	public  HashMap<Integer, Long> timeMap;
	public  TreeMap<Integer, String> serialMap;
	public  HashMap<Integer, ArrayList<Integer>> outNeighbours;
	public  HashMap<String, Long> cumulativeTimeMap;
	public  HashMap<String, Integer> countMap;
	public  ArrayList<Integer> callOrderList;
	public  HashMap<Integer, String> markedMap;
	public String markedNodes;
	public int validator;
	public Long endingTimeInNS;
	public long totalTime;
	public int lastFunctionCalled;
	public ICProject project;

	
	public String text;
	
	@Override
	protected void initialize() {
		//INITIALIZE MAPS
		outNeighbours = new HashMap<Integer, ArrayList<Integer>>();
		timeMap = new HashMap<Integer, Long>();
		serialMap = new TreeMap<Integer, String>();
		cumulativeTimeMap = new HashMap<String, Long>();
		countMap = new HashMap<String, Integer>();
		endingTimeInNS = 0l;
		callOrderList = new ArrayList<Integer>();
		markedMap = new HashMap<Integer, String>();
		lastFunctionCalled = 0;
		project = null;		
	}

	
	public IStatus executeParsing(){
		//Clear maps (in case a previous execution left values hanging)
		outNeighbours.clear();
		timeMap.clear();
		serialMap.clear();
		cumulativeTimeMap.clear();
		countMap.clear();
		text = ""; //$NON-NLS-1$
		callOrderList.clear();
		
		try {
			BufferedReader buff = new BufferedReader(new FileReader(filePath));
			String tmp;
			while ((tmp = buff.readLine()) != null) {
				if (monitor.isCanceled()) {
					return Status.CANCEL_STATUS;
				}
				
				if (tmp.equals("PROBE_BEGIN")){ //$NON-NLS-1$
					tmp = buff.readLine();
					
					if (tmp != null && tmp.length() > 0) {
						project = CoreModel.getDefault().getCModel().getCProject(tmp);
					}
					else {
						launchFileDialogError();
						return Status.CANCEL_STATUS;
					}
					
					text = buff.readLine();
					
					tmp = buff.readLine();
					if (tmp != null && tmp.length() > 0)
						endingTimeInNS = Long.parseLong(tmp);
					else {
						launchFileDialogError();
						return Status.CANCEL_STATUS;
					}
					
					tmp = buff.readLine();
					if (tmp != null && tmp.length() > 0)
						totalTime = Long.parseLong(tmp);
					else {
						launchFileDialogError();
						return Status.CANCEL_STATUS;
					}
				}
			}
			buff.close();
					
		} catch (IOException e) {
			launchFileDialogError();
			return Status.CANCEL_STATUS;
		}
		
		
		if (text.length() > 0) {
			
			boolean encounteredMain = false;
			
			ArrayList<Integer> shouldGetEndingTimeForID = new ArrayList <Integer>();
			String[] callsAndReturns = text.split(";"); //$NON-NLS-1$
			String[] args;
			ArrayList<String> nameList = new ArrayList<String>();
			ArrayList<Integer> idList = new ArrayList<Integer>();
			boolean skippedDirectives = false; 
			
			String name;
			int id;
			long time;
			long cumulativeTime;
			int parentID;
			int firstNode = -1;
			try {
			for (String s : callsAndReturns) {
				switch (s.charAt(0)) {
					case '<' :
						
						args = s.substring(1, s.length()).split(",,"); //$NON-NLS-1$
						// args[0] = name
						// args[1] = id
						// arsg[2] = time of event
						id = Integer.parseInt(args[1]);
						time = Long.parseLong(args[2]);
						name = args[0];
						
						//If we haven't encountered a main function yet and the name isn't clean,
						//and the name contains "__", then this is probably a C directive
						if (!encounteredMain && !isFunctionNameClean(name) && name.contains("__")) { //$NON-NLS-1$
							skippedDirectives = true;
							break;
						}
						name = cleanFunctionName(name);
						if (name.equals("main")) //$NON-NLS-1$
							encounteredMain = true;
						if (firstNode == -1) {
							firstNode = id;
						}
						
						serialMap.put(id, name);
						timeMap.put(id, time);
						
						if (cumulativeTimeMap.get(name) == null){
							cumulativeTimeMap.put(name, (long) 0);
						}

						//IF THERE ARE PREVIOUS FUNCTIONS WITH THE SAME NAME
						//WE ARE IN ONE OF THEM SO DO NOT ADD TO CUMULATIVE TIME
						if (nameList.indexOf(name) == -1) {
							cumulativeTime = cumulativeTimeMap.get(name) - time;
							cumulativeTimeMap.put(name, cumulativeTime);
							shouldGetEndingTimeForID.add(id);
						}
						
						
						if (countMap.get(name) == null){
							countMap.put(name, 0);
						}
						countMap.put(name, countMap.get(name) + 1);
						
						nameList.add(name);
						idList.add(id);
						
						if (outNeighbours.get(id) == null){
							outNeighbours.put(id, new ArrayList<Integer>());
						}
						
						if (idList.size() > 1) {
							parentID = idList.get(idList.size() - 2);
							outNeighbours.get(parentID).add(id);
						}
						
						callOrderList.add(id);
						lastFunctionCalled = id;

						break;
					case '>' :
						args = s.substring(1, s.length()).split(",,"); //$NON-NLS-1$
						//args[0] = name
						//args[1] = time of event
						name = args[0];
						
						
						//If we haven't encountered a main function yet and the name isn't clean,
						//and the name contains "__", then this is probably a C directive
						if (!encounteredMain && !isFunctionNameClean(name) && name.contains("__")) { //$NON-NLS-1$
							skippedDirectives = true;							
							break;
						}
						name = cleanFunctionName(name);
						int lastOccurance = nameList.lastIndexOf(name);
						if (lastOccurance < 0) {
							parsingError(Messages.getString("StapGraphParser.12") + name); //$NON-NLS-1$
							return Status.CANCEL_STATUS;
						}
						
						nameList.remove(lastOccurance);
						id = idList.remove(lastOccurance);
						
						
						if (timeMap.get(id) == null) {
							parsingError(Messages.getString("StapGraphParser.13") + name); //$NON-NLS-1$
							return Status.CANCEL_STATUS;
						}		
						time =  Long.parseLong(args[1]) - timeMap.get(id);
						timeMap.put(id, time);
						
						
						//IF AN ID IS IN THIS ARRAY IT IS BECAUSE WE NEED THE ENDING TIME
						// TO BE ADDED TO THE CUMULATIVE TIME FOR FUNCTIONS OF THIS NAME
						if (shouldGetEndingTimeForID.contains(id)){
							cumulativeTime = cumulativeTimeMap.get(name) + Long.parseLong(args[1]);
							cumulativeTimeMap.put(name, cumulativeTime);
						}
						
						
						//Use + for end times
//						cumulativeTime = cumulativeTimeMap.get(name) + Long.parseLong(args[1]);
//						cumulativeTimeMap.put(name, cumulativeTime);
						
						break;
					default : 
						parsingError(Messages.getString("StapGraphParser.14") + s.charAt(0) + //$NON-NLS-1$
								Messages.getString("StapGraphParser.15") ); //$NON-NLS-1$
						return Status.CANCEL_STATUS;
					
				} 
				
			} 
			
			
			//CHECK FOR EXIT() CALL
			if (idList.size() != 0){
				for (int val : idList){
					name = serialMap.get(val);
					time =  endingTimeInNS - timeMap.get(val);
					timeMap.put(val, time);
					if (shouldGetEndingTimeForID.contains(val)){
						cumulativeTime = cumulativeTimeMap.get(name) + endingTimeInNS;
						cumulativeTimeMap.put(name, cumulativeTime);
					}
					
//					if (name.equals("main")) {
//						totalTime = time;
//					}
					lastFunctionCalled = val;
				}
				markedMap.put(lastFunctionCalled, Messages.getString("StapGraphParser.16")); //$NON-NLS-1$
			}
				
			//timecheck is true if the total execution time is less than 10ms
			//and the first function is more than 1% off from the total time.
			boolean timeCheck = totalTime < 50000000 && 
								(((float)timeMap.get(firstNode)/totalTime) > 1.01 ||
								((float)timeMap.get(firstNode)/totalTime) < 0.99);
			
			
								
			if (skippedDirectives || timeCheck) {
				totalTime = timeMap.get(firstNode);
				String markedMessage = ""; //$NON-NLS-1$
				if (markedMap.containsKey(firstNode)) {
					markedMessage = markedMap.get(firstNode) + "\n"; //$NON-NLS-1$
				}
				if (skippedDirectives)
					markedMessage += Messages.getString("StapGraphParser.19"); //$NON-NLS-1$
				if (timeCheck)
					markedMessage += Messages.getString("StapGraphParser.20"); //$NON-NLS-1$
				
				markedMessage += Messages.getString("StapGraphParser.21"); //$NON-NLS-1$
				
				markedMap.put(firstNode, markedMessage);
			}
			
			
			} catch (NumberFormatException e) {
				SystemTapUIErrorMessages mess = new SystemTapUIErrorMessages(Messages.getString("StapGraphParser.22"),  //$NON-NLS-1$
						Messages.getString("StapGraphParser.23"), Messages.getString("StapGraphParser.24") + //$NON-NLS-1$ //$NON-NLS-2$
						Messages.getString("StapGraphParser.25")); //$NON-NLS-1$
				mess.schedule();
				
				return Status.CANCEL_STATUS;
			}
		} else {
			parsingError(Messages.getString("StapGraphParser.26")); //$NON-NLS-1$
			return Status.CANCEL_STATUS;
		}
		
		//Create a UIJob to handle the rest
		GraphUIJob uijob = new GraphUIJob(Messages.getString("StapGraphParser.5"), this); //$NON-NLS-1$
		uijob.schedule(); 
		return Status.OK_STATUS;
			
	}


	@Override
	public void saveData(String filePath) {
		File file = new File(filePath);
		String content = Messages.getString("CallgraphView.25") //$NON-NLS-1$
		+ project.getElementName()
		+ NEW_LINE
		+ text
		+ NEW_LINE
		+ endingTimeInNS
		+ NEW_LINE
		+ totalTime;
		try {
			// WAS THE FILE CREATED OR DOES IT ALREADY EXIST
			if (file.createNewFile()) {
				Helper.writeToFile(filePath, content);
			} else {
				if (MessageDialog
						.openConfirm(
								new Shell(),
								Messages
										.getString("CallgraphView.FileExistsTitle"), //$NON-NLS-1$
								Messages
										.getString("CallgraphView.FileExistsMessage"))) { //$NON-NLS-1$
					file.delete();
					file.createNewFile();
					Helper.writeToFile(filePath, content);
				}
			}
		} catch (IOException e1) {
			e1.printStackTrace();
		}		
	}



	
}

Back to the top