Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 88485af66ac72479b64f44228f83835a34dc1e0c (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
/*******************************************************************************
 * 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.fields;

import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField;
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTTreeViewer;
import org.eclipse.linuxtools.dataviewers.charts.provider.IChartField;
import org.eclipse.linuxtools.gprof.parser.GmonDecoder;
import org.eclipse.linuxtools.gprof.parser.HistogramDecoder;
import org.eclipse.linuxtools.gprof.view.GmonView;
import org.eclipse.linuxtools.gprof.view.histogram.HistFile;
import org.eclipse.linuxtools.gprof.view.histogram.HistFunction;
import org.eclipse.linuxtools.gprof.view.histogram.HistLine;
import org.eclipse.linuxtools.gprof.view.histogram.HistRoot;
import org.eclipse.linuxtools.gprof.view.histogram.TreeElement;
import org.eclipse.swt.graphics.Color;


/**
 * Column "Samples" of displayed elements
 *
 * @author Xavier Raynaud <xavier.raynaud@st.com>
 */
public class SampleProfField extends AbstractSTDataViewersField implements IChartField{

	private boolean samples = true;
	protected final AbstractSTTreeViewer viewer;
	protected final static double UNINITIALIZED = 0;
	
	/**
	 * Constructor
	 * @param viewer the gmon viewer
	 */
	public SampleProfField(AbstractSTTreeViewer viewer) {
		this.viewer = viewer;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractviewers.ISTDataViewersField#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object obj1, Object obj2) {
		TreeElement e1 = (TreeElement) obj1;
		TreeElement e2 = (TreeElement) obj2;
		int s1 = e1.getSamples();
		int s2 = e2.getSamples();
		return s1 - s2;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractviewers.ISTDataViewersField#getColumnHeaderText()
	 */
	public String getColumnHeaderText() {
		String prefix = "";
		Object o = viewer.getInput();
		if (o instanceof GmonDecoder) {
			GmonDecoder decoder = (GmonDecoder) o;
			if (decoder.isICache()) {
				prefix = "ICACHE ";
			} else if (decoder.isDCache()) {
				prefix = "DCACHE ";
			}
		}
		if (samples) return prefix + "Samples";
		return prefix + "Time";
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField#getColumnHeaderTooltip()
	 */
	public String getColumnHeaderTooltip() {
		Object o = viewer.getInput();
		if (o instanceof GmonDecoder) {
			GmonDecoder decoder = (GmonDecoder) o;
			if (decoder.isICache()) {
				return "Time spent by function accessing instruction cache";
			} else if (decoder.isDCache()) {
				return "Time spent by function accessing data cache";
			}
		}
		return null;
	}
	
	

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractviewers.ISTDataViewersField#getValue(java.lang.Object)
	 */
	public String getValue(Object obj) {
		TreeElement e = (TreeElement) obj;
		int i = e.getSamples();
		if (i == -1) return "";
		if (samples) {
			return String.valueOf(i);
		} else {
			double prof_rate = getProfRate();
			if (prof_rate == UNINITIALIZED) return "?";
			return getValue(i, prof_rate);
		}
	}
	
	/**
	 * Get the time value with the best unit display
	 * @param i nbr of samples
	 * @param prof_rate profiling frequency
	 * @return time value with the best adapted time unit
	 */
	public static String getValue(double i, double prof_rate)
	{
		long timeInNs = (long) (i/prof_rate);
		long ns = timeInNs%1000;
		
		long timeInUs = timeInNs/1000;
		if (timeInUs == 0) return ns + "ns";
		long us = timeInUs%1000;
		
		long timeInMs = timeInUs/1000;
		if (timeInMs == 0) {
			String ns_s = "" + ns;
			while (ns_s.length() < 3) ns_s = "0" + ns_s;
			return us + "." + ns_s + "us";
		}
		long ms = timeInMs%1000;
		
		long timeInS = timeInMs/1000;
		if (timeInS == 0) {
			String us_s = "" + us;
			while (us_s.length() < 3) us_s = "0" + us_s;
			return ms + "." + us_s + "ms";
		}
		long s = timeInS%60;
		
		long timeInMin = timeInS/60;
		if (timeInMin == 0) {
			String ms_s = "" + ms;
			while (ms_s.length() < 3) ms_s = "0" + ms_s;
			return s + "." + ms_s + "s";
		}
		long min = timeInMin%60;
		
		long timeInHour = timeInMin/60;
		if (timeInHour == 0) return min + "min " + s + "s";
		
		return timeInHour + "h " + min + "min";
	}
	

	protected double getProfRate() {
		double prof_rate = UNINITIALIZED;
		Object o = viewer.getViewer().getInput();
		if (o instanceof GmonDecoder) {
			GmonDecoder decoder = (GmonDecoder)  o;
			HistogramDecoder histo = decoder.getHistogramDecoder();
			prof_rate = histo.getProf_rate();
			char tUnit = histo.getTimeDimension();
			switch (tUnit) {
			case 's': prof_rate /= 1000000000; break;
			case 'm': prof_rate /= 1000000; break;
			case 'u': prof_rate /= 1000; break;
			}
		}
		return prof_rate;
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField#getBackground(java.lang.Object)
	 */
	public Color getBackground(Object element) {
		return GmonView.getBackground(element);
	}

	/* 
	 * (non-Javadoc)
	 * @see org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField#getToolTipText(java.lang.Object)
	 */
	@Override
	public String getToolTipText(Object element) {
		String suffix = "";
		Object o = viewer.getInput();
		if (o instanceof GmonDecoder) {
			GmonDecoder decoder = (GmonDecoder) o;
			if (decoder.isICache()) {
				suffix = " in instruction cache";
			} else if (decoder.isDCache()) {
				suffix = " in data cache";
			}
		}
		
		if (element instanceof HistRoot) {
			return "total time spent in the program" + suffix;
		} else if (element instanceof HistFunction) {
			return "time spent in this function" + suffix;
		} else if (element instanceof HistFile) {
			return "time spent in this file" + suffix;
		} else if (element instanceof HistLine) {
			return "time spent at this location" + suffix;
		}
		return null;
	}

	/**
	 * Switch from samples to time ans vice-versa
	 */
	public void toggle() {
		this.samples = !this.samples;
	}

	public Number getNumber(Object obj) {
		TreeElement e = (TreeElement) obj;
		int i = e.getSamples();
		if (i == -1) return 0L;
		return i;
	}

	
	
}

Back to the top