Skip to main content
summaryrefslogtreecommitdiffstats
blob: 295ce64e0aabb44fc8d30a53ca76933170f0219b (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
/*******************************************************************************
 * Copyright (c) 2009 STMicroelectronics.
 * 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:
 *   Xavier Raynaud <xavier.raynaud@st.com> - initial API and implementation
 *******************************************************************************/
package org.eclipse.linuxtools.gprof.view;

import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.action.IToolBarManager;
import org.eclipse.jface.action.Separator;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.linuxtools.binutils.utils.STSymbolManager;
import org.eclipse.linuxtools.dataviewers.abstractview.AbstractSTDataView;
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTViewer;
import org.eclipse.linuxtools.dataviewers.actions.STExportToCSVAction;
import org.eclipse.linuxtools.dataviewers.charts.actions.ChartAction;
import org.eclipse.linuxtools.gprof.Activator;
import org.eclipse.linuxtools.gprof.action.SwitchContentProviderAction;
import org.eclipse.linuxtools.gprof.action.SwitchSampleTimeAction;
import org.eclipse.linuxtools.gprof.parser.GmonDecoder;
import org.eclipse.linuxtools.gprof.parser.HistogramDecoder;
import org.eclipse.linuxtools.gprof.view.fields.SampleProfField;
import org.eclipse.linuxtools.gprof.view.histogram.CGArc;
import org.eclipse.linuxtools.gprof.view.histogram.CGCategory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;


/**
 * The view where gmon file is displayed
 *
 * @author Xavier Raynaud <xavier.raynaud@st.com>
 */
public class GmonView extends AbstractSTDataView {

	/** WHITE color */
	public static final Color WHITE = PlatformUI.getWorkbench().getDisplay().getSystemColor(SWT.COLOR_WHITE);
	/** GREEN1 color : for children category */
	public static final Color GREEN1 = new Color(PlatformUI.getWorkbench().getDisplay(), 207,255,207);
	/** GREEN2 color : for children */
	public static final Color GREEN2 = new Color(PlatformUI.getWorkbench().getDisplay(), 175,255,175);
	/** BLUE1 color : for parent category */
	public static final Color BLUE1 = new Color(PlatformUI.getWorkbench().getDisplay(), 207,207,255);
	/** BLUE2 color : for parents */
	public static final Color BLUE2 = new Color(PlatformUI.getWorkbench().getDisplay(), 175,175,255);

	public static final int CALL_GRAPH_MODE = 0;
	public static final int SAMPLE_MODE = 1;

	private Label label;
	private Action action1;
	private Action action2;
	private Action action3;
	private Action action4;
	private Action switchSampleTime;


	protected void createTitle(Composite parent) {
		label = new Label(parent, SWT.WRAP);
		GridData data = new GridData(SWT.FILL, SWT.BEGINNING, true, false, 1, 1);
		label.setLayoutData(data);
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractview.AbstractSTDataView#contributeToToolbar(org.eclipse.jface.action.IToolBarManager)
	 */
	@Override
	protected void contributeToToolbar(IToolBarManager manager) {
		super.contributeToToolbar(manager);
		manager.add(new Separator());		
		manager.add(action2);
		action2.setChecked(true);
		manager.add(action3);
		manager.add(action4);
		manager.add(action1);
		manager.add(new Separator());
		manager.add(switchSampleTime); 
		manager.add(new Separator());
		manager.add(new ChartAction(getViewSite().getShell(), getSTViewer()));
	}


	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractview.AbstractSTDataView#createActions()
	 */
	@Override
	protected void createActions() {
		super.createActions();
		action1 = new SwitchContentProviderAction("Display function call graph", "icons/ch_callees.png", getSTViewer().getViewer(), CallGraphContentProvider.sharedInstance);
		action2 = new SwitchContentProviderAction("Sort samples per file", "icons/c_file_obj.gif", getSTViewer().getViewer(), FileHistogramContentProvider.sharedInstance);
		action3 = new SwitchContentProviderAction("Sort samples per function", "icons/function_obj.gif", getSTViewer().getViewer(), FunctionHistogramContentProvider.sharedInstance);
		action4 = new SwitchContentProviderAction("Sort samples per line", "icons/line_obj.gif", getSTViewer().getViewer(), FlatHistogramContentProvider.sharedInstance);
		switchSampleTime = new SwitchSampleTimeAction(this);
	}



	/*
	 * I do not know where to put this static method.
	 * It is used by all ProfFields
	 */
	public static Color getBackground(Object element) {
		if (element instanceof CGCategory) {
			CGCategory cat = (CGCategory) element;
			if (CGCategory.CHILDREN.equals(cat.category)) {
				return GmonView.BLUE1;
			} else {
				return GmonView.GREEN1;
			}
		} else if (element instanceof CGArc) {
			CGArc arc = (CGArc) element;
			CGCategory cat = (CGCategory) arc.getParent();
			if (CGCategory.CHILDREN.equals(cat.category)) {
				return GmonView.BLUE2;
			} else {
				return GmonView.GREEN2;
			}
		}
		return GmonView.WHITE;
	}


	public Label getLabel() {
		return label;
	}

	@Override
	protected AbstractSTViewer createAbstractSTViewer(Composite parent) {
		return new GmonViewer(parent);
	}
	/**
	 * set the gprof view title
	 * @param decoder the gmon decoder
	 * @param titleLabel the title label
	 */
	public static void setHistTitle(GmonDecoder decoder, Label titleLabel) {
		String title = " gmon file: "
			+ decoder.getGmonFile()
			+ "\n program file: "
			+ decoder.getProgram().getPath();
		HistogramDecoder histo = decoder.getHistogramDecoder();
		if (histo.hasValues()) {
			double prof_rate = histo.getProf_rate();
			String period = "";
			if (prof_rate != 0){
				char tUnit = histo.getTimeDimension();
				switch (tUnit) {
				case 's': prof_rate /= 1000000000; break;
				case 'm': prof_rate /= 1000000; break;
				case 'u': prof_rate /= 1000; break;
				}
				period = ", each sample counts as " + SampleProfField.getValue(1, prof_rate);
			}
			title += "\n " +histo.getBucketSize()
			+ " bytes per bucket" + period;
		}
		titleLabel.setText(title);
		titleLabel.getParent().layout(true);
	}

	/**
	 * Display gmon results in the GProf View.
	 * NOTE: this method has to be called from within the UI thread.
	 * @param binaryPath
	 * @param gmonPath
	 * @param instanceName
	 */
	public static GmonView displayGprofView(String binaryPath, String gmonPath, String instanceName){
		IBinaryObject binary = STSymbolManager.sharedInstance.getBinaryObject(new Path(binaryPath));
		if (binary == null) {
			MessageDialog.openError(
					PlatformUI.getWorkbench().getDisplay().getActiveShell(),
					"Invalid binary file",
					binaryPath + " is not a valid binary file.");
			return null;
		}

		GmonDecoder decoder = new GmonDecoder(binary);
		try {
			decoder.read(gmonPath);
		} catch(Exception e) {
			Status status = new Status(
					Status.ERROR,
					Activator.PLUGIN_ID,
					IStatus.ERROR,
					e.getMessage(),
					e
			);
			Activator.getDefault().getLog().log(status);
		}
		return displayGprofView(decoder, gmonPath, instanceName);
	}

	/**
	 * Display gmon results in the GProf View.
	 * NOTE: this method has to be called from within the UI thread.
	 * @param decoder
	 * @param gmonPath
	 * @param instanceName
	 */
	public static GmonView displayGprofView(GmonDecoder decoder, String gmonPath, String instanceName){
		GmonView gmonview = null;
		try{
			IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
			IWorkbenchPage page = window.getActivePage();
			if (instanceName != null)
				gmonview = (GmonView) page.showView("org.eclipse.linuxtools.gprof.view",instanceName, IWorkbenchPage.VIEW_ACTIVATE);
			else gmonview = (GmonView) page.showView("org.eclipse.linuxtools.gprof.view");
			if (decoder.getHistogramDecoder().getProf_rate() == 0){
				gmonview.switchSampleTime.setToolTipText("Unable to display time, because profiling rate is null");
				gmonview.switchSampleTime.setEnabled(false);
			}
			gmonview.setInput(decoder);
			GmonView.setHistTitle(decoder, gmonview.label);
			if (!decoder.getHistogramDecoder().hasValues()) {
				gmonview.action1.setChecked(true);
				gmonview.action2.setChecked(false);
				gmonview.action1.run();
			}
			if (decoder.isDCache() || decoder.isICache()) {
				TreeViewer tv = (TreeViewer) gmonview.getSTViewer().getViewer();
				TreeColumn tc = tv.getTree().getColumn(1);
				SampleProfField spf = (SampleProfField) tc.getData();
				tc.setText(spf.getColumnHeaderText());
				tc.setToolTipText(spf.getColumnHeaderTooltip());
				tv.refresh();
			}
		} catch(Exception e) {
			Status status = new Status(
					Status.ERROR,
					Activator.PLUGIN_ID,
					IStatus.ERROR,
					e.getMessage(),
					e
			);
			Activator.getDefault().getLog().log(status);
		}
		return gmonview;
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractview.AbstractSTDataView#createExportToCSVAction()
	 */
	@Override
	protected IAction createExportToCSVAction() {
		IAction action = new STExportToCSVAction(this.getSTViewer()) {
			public void run() {
				Object o = getSTViewer().getInput();
				if (o instanceof GmonDecoder) {
					GmonDecoder gd = (GmonDecoder) o;
					getExporter().setFilePath(gd.getGmonFile() + ".csv");
				}
				super.run();
			}

		};
		return action;
	}

}

Back to the top