Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 93ec681d0afeb075a1c3f245ba9da353e0ea884f (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
/*******************************************************************************
 * 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.util.Arrays;

import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ISourceRange;
import org.eclipse.cdt.core.model.ISourceReference;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IMenuListener;
import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.action.MenuManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.ITreeSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.linuxtools.profiling.ui.ProfileUIUtils;
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.cachegrind.model.ICachegrindElement;
import org.eclipse.linuxtools.valgrind.ui.CollapseAction;
import org.eclipse.linuxtools.valgrind.ui.ExpandAction;
import org.eclipse.linuxtools.valgrind.ui.IValgrindToolView;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class CachegrindViewPart extends ViewPart implements IValgrindToolView {

	protected CachegrindOutput[] outputs;
	protected TreeViewer viewer;

	protected static final int COLUMN_SIZE = 75;
	protected CachegrindLabelProvider labelProvider;
	protected CachegrindTreeContentProvider contentProvider;
	protected IDoubleClickListener doubleClickListener;
	protected ExpandAction expandAction;
	protected CollapseAction collapseAction;
	
	// Events - Cache
	protected static final String IR = "Ir"; //$NON-NLS-1$
	protected static final String I1MR = "I1mr"; //$NON-NLS-1$
	protected static final String I2MR = "I2mr"; //$NON-NLS-1$
	protected static final String DR = "Dr"; //$NON-NLS-1$
	protected static final String D1MR = "D1mr"; //$NON-NLS-1$
	protected static final String D2MR = "D2mr"; //$NON-NLS-1$
	protected static final String DW = "Dw"; //$NON-NLS-1$
	protected static final String D1MW = "D1mw"; //$NON-NLS-1$
	protected static final String D2MW = "D2mw"; //$NON-NLS-1$
	
	// Events - Branch
	protected static final String BC = "Bc"; //$NON-NLS-1$
	protected static final String BCM = "Bcm"; //$NON-NLS-1$
	protected static final String BI = "Bi"; //$NON-NLS-1$
	protected static final String BIM = "Bim"; //$NON-NLS-1$
	
	@Override
	public void createPartControl(Composite parent) {
		Composite top = new Composite(parent, SWT.NONE);
		GridLayout topLayout = new GridLayout();
		topLayout.marginHeight = topLayout.marginWidth = 0;
		top.setLayout(topLayout);
		top.setLayoutData(new GridData(GridData.FILL_BOTH));

		viewer = new TreeViewer(top, SWT.SINGLE | SWT.H_SCROLL | SWT.V_SCROLL
				| SWT.FULL_SELECTION);

		labelProvider = new CachegrindLabelProvider();

		Tree tree = viewer.getTree();
		tree.setHeaderVisible(true);
		tree.setLinesVisible(true);
		tree.setLayoutData(new GridData(GridData.FILL_BOTH));

		TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
		column.getColumn().setText(Messages.getString("CachegrindViewPart.Location")); //$NON-NLS-1$
		column.getColumn().setWidth(COLUMN_SIZE * 4);
		column.getColumn().setResizable(true);
		column.getColumn().addSelectionListener(getHeaderListener());
		column.setLabelProvider(labelProvider);

		contentProvider = new CachegrindTreeContentProvider();
		viewer.setContentProvider(contentProvider);
		viewer.setLabelProvider(labelProvider);
		viewer.setAutoExpandLevel(2);
		doubleClickListener = new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {
				Object selection = ((StructuredSelection) event.getSelection()).getFirstElement();
				String path = null;
				int line = 0;
				if (selection instanceof CachegrindFile) {
					path = ((CachegrindFile) selection).getPath();
				}
				else if (selection instanceof CachegrindLine) {
					CachegrindLine element = (CachegrindLine) selection;
					CachegrindFile file = (CachegrindFile) element.getParent().getParent();
					path = file.getPath();
					line = element.getLine();
				}
				else if (selection instanceof CachegrindFunction) {
					CachegrindFunction function = (CachegrindFunction) selection;
					path = ((CachegrindFile) function.getParent()).getPath();
					if (function.getModel() instanceof ISourceReference) {
						ISourceReference model = (ISourceReference) function.getModel();
						try {
							ISourceRange sr = model.getSourceRange();
							if (sr != null) {
								line = sr.getStartLine();
							}
						} catch (CModelException e) {
							e.printStackTrace();
						}						
					}
				}
				if (path != null) {
					try {
						ProfileUIUtils.openEditorAndSelect(path, line);
					} catch (PartInitException e) {
						e.printStackTrace();
					} catch (BadLocationException e) {
						e.printStackTrace();
					}
				}
			}			
		};
		viewer.addDoubleClickListener(doubleClickListener);
		
		expandAction = new ExpandAction(viewer);
		collapseAction = new CollapseAction(viewer);
		
		MenuManager manager = new MenuManager();
		manager.addMenuListener(new IMenuListener() {
			public void menuAboutToShow(IMenuManager manager) {
				ITreeSelection selection = (ITreeSelection) viewer.getSelection();
				ICachegrindElement element = (ICachegrindElement) selection.getFirstElement();
				if (contentProvider.hasChildren(element)) {
					manager.add(expandAction);
					manager.add(collapseAction);
				}
			}			
		});
		
		manager.setRemoveAllWhenShown(true);	
		Menu contextMenu = manager.createContextMenu(viewer.getTree());
		viewer.getControl().setMenu(contextMenu);
	}

	@Override
	public void setFocus() {
		viewer.getTree().setFocus();
	}

	public IAction[] getToolbarActions() {
		return null;
	}

	public void refreshView() {
		if (outputs != null && outputs.length > 0) {
			String[] events = outputs[0].getEvents();
			for (int i = 0; i < events.length; i++) {
				TreeViewerColumn column = new TreeViewerColumn(viewer, SWT.NONE);
				column.getColumn().setText(events[i]);
				column.getColumn().setWidth(COLUMN_SIZE);
				column.getColumn().setToolTipText(getFullEventName(events[i]));
				column.getColumn().setResizable(true);
				column.getColumn().addSelectionListener(getHeaderListener());
				column.setLabelProvider(labelProvider);
			}
			viewer.setInput(outputs);
			viewer.getTree().layout(true);
		}
	}

	public void setOutputs(CachegrindOutput[] outputs) {
		this.outputs = outputs;
	}

	public CachegrindOutput[] getOutputs() {
		return outputs;
	}
	
	public TreeViewer getViewer() {
		return viewer;
	}
	
	public IDoubleClickListener getDoubleClickListener() {
		return doubleClickListener;
	}

	private SelectionListener getHeaderListener() {
		return new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TreeColumn column = (TreeColumn) e.widget;
				Tree tree = viewer.getTree();
				if (column.equals(tree.getSortColumn())) {
					int direction = tree.getSortDirection() == SWT.UP ? SWT.DOWN
							: SWT.UP;
					tree.setSortDirection(direction);
				} else {
					tree.setSortDirection(SWT.UP);
				}
				tree.setSortColumn(column);
				viewer.setComparator(new ViewerComparator() {
					@Override
					public int compare(Viewer viewer, Object e1, Object e2) {
						Tree tree = ((TreeViewer) viewer).getTree();
						int direction = tree.getSortDirection();
						ICachegrindElement o1 = (ICachegrindElement) e1;
						ICachegrindElement o2 = (ICachegrindElement) e2;
						long result = 0;
						
						int sortIndex = Arrays.asList(tree.getColumns()).indexOf(tree.getSortColumn());
						if (sortIndex == 0) { // use compareTo
							result = o1.compareTo(o2);
						}
						else {
							long[] v1 = null;
							long[] v2 = null;
							if (o1 instanceof CachegrindFunction && o2 instanceof CachegrindFunction) {
								v1 = ((CachegrindFunction) o1).getTotals();
								v2 = ((CachegrindFunction) o2).getTotals();
							}
							else if (o1 instanceof CachegrindLine && o2 instanceof CachegrindLine) {
								v1 = ((CachegrindLine) o1).getValues();
								v2 = ((CachegrindLine) o2).getValues();
							}
							else if (o1 instanceof CachegrindOutput && o2 instanceof CachegrindOutput) {
								v1 = ((CachegrindOutput) o1).getSummary();
								v2 = ((CachegrindOutput) o2).getSummary(); 
							}
							
							if (v1 != null && v2 != null) {
								result = v1[sortIndex - 1] - v2[sortIndex - 1];
							}
						}
						
						// ascending or descending
						result = direction == SWT.UP ? result : -result;
						
						// overflow check
						if (result > Integer.MAX_VALUE) {
							result = Integer.MAX_VALUE;
						} else if (result < Integer.MIN_VALUE) {
							result = Integer.MIN_VALUE;
						}
						
						return (int) result;
					}
				});
			}
		};
	}

	private String getFullEventName(String event) {
		String result = event;
		if (event.equals(IR)) {
			result = Messages.getString("CachegrindViewPart.Ir_long"); //$NON-NLS-1$
		}
		else if (event.equals(I1MR)) {
			result = Messages.getString("CachegrindViewPart.I1mr_long"); //$NON-NLS-1$
		}
		else if (event.equals(I2MR)) {
			result = Messages.getString("CachegrindViewPart.I2mr_long"); //$NON-NLS-1$
		}
		else if (event.equals(DR)) {
			result = Messages.getString("CachegrindViewPart.Dr_long"); //$NON-NLS-1$
		}
		else if (event.equals(D1MR)) {
			result = Messages.getString("CachegrindViewPart.D1mr_long"); //$NON-NLS-1$
		}
		else if (event.equals(D2MR)) {
			result = Messages.getString("CachegrindViewPart.D2mr_long"); //$NON-NLS-1$
		}
		else if (event.equals(DW)) {
			result = Messages.getString("CachegrindViewPart.Dw_long"); //$NON-NLS-1$
		}
		else if (event.equals(D1MW)) {
			result = Messages.getString("CachegrindViewPart.D1mw_long"); //$NON-NLS-1$
		}
		else if (event.equals(D2MW)) {
			result = Messages.getString("CachegrindViewPart.D2mw_long"); //$NON-NLS-1$
		}
		else if (event.equals(BC)) {
			result = Messages.getString("CachegrindViewPart.Bc_long"); //$NON-NLS-1$
		}
		else if (event.equals(BCM)) {
			result = Messages.getString("CachegrindViewPart.Bcm_long"); //$NON-NLS-1$
		}
		else if (event.equals(BI)) {
			result = Messages.getString("CachegrindViewPart.Bi_long"); //$NON-NLS-1$
		}
		else if (event.equals(BIM)) {
			result = Messages.getString("CachegrindViewPart.Bim_long"); //$NON-NLS-1$
		}
		return result;
	}

	protected class CachegrindTreeContentProvider implements ITreeContentProvider {

		public Object[] getChildren(Object parentElement) {
			Object[] result = null;
			if (parentElement instanceof CachegrindOutput[]) {
				result = (CachegrindOutput[]) parentElement;
			}
			else if (parentElement instanceof ICachegrindElement) {
				result = ((ICachegrindElement) parentElement).getChildren();
			}
			return result;
		}

		public Object getParent(Object element) {
			return ((ICachegrindElement) element).getParent();
		}

		public boolean hasChildren(Object element) {
			ICachegrindElement[] children = (ICachegrindElement[]) getChildren(element);
			return children != null && children.length > 0;
		}

		public Object[] getElements(Object inputElement) {
			return getChildren(inputElement);
		}

		public void dispose() {
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}

	}

	

}

Back to the top