~ubuntu-branches/ubuntu/trusty/eclipse-linuxtools/trusty

« back to all changes in this revision

Viewing changes to gprof/org.eclipse.linuxtools.gprof/src/org/eclipse/linuxtools/internal/gprof/view/fields/SampleProfField.java

  • Committer: Package Import Robot
  • Author(s): Jakub Adam
  • Date: 2012-06-29 12:07:30 UTC
  • Revision ID: package-import@ubuntu.com-20120629120730-bfri1xys1i71dpn6
Tags: upstream-1.0.0
ImportĀ upstreamĀ versionĀ 1.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*******************************************************************************
 
2
 * Copyright (c) 2009 STMicroelectronics.
 
3
 * All rights reserved. This program and the accompanying materials
 
4
 * are made available under the terms of the Eclipse Public License v1.0
 
5
 * which accompanies this distribution, and is available at
 
6
 * http://www.eclipse.org/legal/epl-v10.html
 
7
 *
 
8
 * Contributors:
 
9
 *   Xavier Raynaud <xavier.raynaud@st.com> - initial API and implementation
 
10
 *******************************************************************************/
 
11
package org.eclipse.linuxtools.internal.gprof.view.fields;
 
12
 
 
13
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField;
 
14
import org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTTreeViewer;
 
15
import org.eclipse.linuxtools.dataviewers.charts.provider.IChartField;
 
16
import org.eclipse.linuxtools.internal.gprof.parser.GmonDecoder;
 
17
import org.eclipse.linuxtools.internal.gprof.parser.HistogramDecoder;
 
18
import org.eclipse.linuxtools.internal.gprof.view.GmonView;
 
19
import org.eclipse.linuxtools.internal.gprof.view.histogram.HistFile;
 
20
import org.eclipse.linuxtools.internal.gprof.view.histogram.HistFunction;
 
21
import org.eclipse.linuxtools.internal.gprof.view.histogram.HistLine;
 
22
import org.eclipse.linuxtools.internal.gprof.view.histogram.HistRoot;
 
23
import org.eclipse.linuxtools.internal.gprof.view.histogram.TreeElement;
 
24
import org.eclipse.swt.graphics.Color;
 
25
 
 
26
 
 
27
/**
 
28
 * Column "Samples" of displayed elements
 
29
 *
 
30
 * @author Xavier Raynaud <xavier.raynaud@st.com>
 
31
 */
 
32
public class SampleProfField extends AbstractSTDataViewersField implements IChartField{
 
33
 
 
34
        private boolean samples = true;
 
35
        protected final AbstractSTTreeViewer viewer;
 
36
        protected final static double UNINITIALIZED = 0;
 
37
        
 
38
        /**
 
39
         * Constructor
 
40
         * @param viewer the gmon viewer
 
41
         */
 
42
        public SampleProfField(AbstractSTTreeViewer viewer) {
 
43
                this.viewer = viewer;
 
44
        }
 
45
        
 
46
        /*
 
47
         * (non-Javadoc)
 
48
         * @see org.eclipse.linuxtools.dataviewers.abstractviewers.ISTDataViewersField#compare(java.lang.Object, java.lang.Object)
 
49
         */
 
50
        public int compare(Object obj1, Object obj2) {
 
51
                TreeElement e1 = (TreeElement) obj1;
 
52
                TreeElement e2 = (TreeElement) obj2;
 
53
                int s1 = e1.getSamples();
 
54
                int s2 = e2.getSamples();
 
55
                return s1 - s2;
 
56
        }
 
57
 
 
58
        /*
 
59
         * (non-Javadoc)
 
60
         * @see org.eclipse.linuxtools.dataviewers.abstractviewers.ISTDataViewersField#getColumnHeaderText()
 
61
         */
 
62
        public String getColumnHeaderText() {
 
63
                String prefix = "";
 
64
                Object o = viewer.getInput();
 
65
                if (o instanceof GmonDecoder) {
 
66
                        GmonDecoder decoder = (GmonDecoder) o;
 
67
                        if (decoder.isICache()) {
 
68
                                prefix = "ICACHE ";
 
69
                        } else if (decoder.isDCache()) {
 
70
                                prefix = "DCACHE ";
 
71
                        }
 
72
                }
 
73
                if (samples) return prefix + "Samples";
 
74
                return prefix + "Time";
 
75
        }
 
76
        
 
77
        /*
 
78
         * (non-Javadoc)
 
79
         * @see org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField#getColumnHeaderTooltip()
 
80
         */
 
81
        public String getColumnHeaderTooltip() {
 
82
                Object o = viewer.getInput();
 
83
                if (o instanceof GmonDecoder) {
 
84
                        GmonDecoder decoder = (GmonDecoder) o;
 
85
                        if (decoder.isICache()) {
 
86
                                return "Time spent by function accessing instruction cache";
 
87
                        } else if (decoder.isDCache()) {
 
88
                                return "Time spent by function accessing data cache";
 
89
                        }
 
90
                }
 
91
                return null;
 
92
        }
 
93
        
 
94
        
 
95
 
 
96
        /*
 
97
         * (non-Javadoc)
 
98
         * @see org.eclipse.linuxtools.dataviewers.abstractviewers.ISTDataViewersField#getValue(java.lang.Object)
 
99
         */
 
100
        public String getValue(Object obj) {
 
101
                TreeElement e = (TreeElement) obj;
 
102
                int i = e.getSamples();
 
103
                if (i == -1) return "";
 
104
                if (samples) {
 
105
                        return String.valueOf(i);
 
106
                } else {
 
107
                        double prof_rate = getProfRate();
 
108
                        if (prof_rate == UNINITIALIZED) return "?";
 
109
                        return getValue(i, prof_rate);
 
110
                }
 
111
        }
 
112
        
 
113
        /**
 
114
         * Get the time value with the best unit display
 
115
         * @param i nbr of samples
 
116
         * @param prof_rate profiling frequency
 
117
         * @return time value with the best adapted time unit
 
118
         */
 
119
        public static String getValue(double i, double prof_rate)
 
120
        {
 
121
                long timeInNs = (long) (i/prof_rate);
 
122
                long ns = timeInNs%1000;
 
123
                
 
124
                long timeInUs = timeInNs/1000;
 
125
                if (timeInUs == 0) return ns + "ns";
 
126
                long us = timeInUs%1000;
 
127
                
 
128
                long timeInMs = timeInUs/1000;
 
129
                if (timeInMs == 0) {
 
130
                        String ns_s = "" + ns;
 
131
                        while (ns_s.length() < 3) ns_s = "0" + ns_s;
 
132
                        return us + "." + ns_s + "us";
 
133
                }
 
134
                long ms = timeInMs%1000;
 
135
                
 
136
                long timeInS = timeInMs/1000;
 
137
                if (timeInS == 0) {
 
138
                        String us_s = "" + us;
 
139
                        while (us_s.length() < 3) us_s = "0" + us_s;
 
140
                        return ms + "." + us_s + "ms";
 
141
                }
 
142
                long s = timeInS%60;
 
143
                
 
144
                long timeInMin = timeInS/60;
 
145
                if (timeInMin == 0) {
 
146
                        String ms_s = "" + ms;
 
147
                        while (ms_s.length() < 3) ms_s = "0" + ms_s;
 
148
                        return s + "." + ms_s + "s";
 
149
                }
 
150
                long min = timeInMin%60;
 
151
                
 
152
                long timeInHour = timeInMin/60;
 
153
                if (timeInHour == 0) return min + "min " + s + "s";
 
154
                
 
155
                return timeInHour + "h " + min + "min";
 
156
        }
 
157
        
 
158
 
 
159
        protected double getProfRate() {
 
160
                double prof_rate = UNINITIALIZED;
 
161
                Object o = viewer.getViewer().getInput();
 
162
                if (o instanceof GmonDecoder) {
 
163
                        GmonDecoder decoder = (GmonDecoder)  o;
 
164
                        HistogramDecoder histo = decoder.getHistogramDecoder();
 
165
                        prof_rate = histo.getProf_rate();
 
166
                        char tUnit = histo.getTimeDimension();
 
167
                        switch (tUnit) {
 
168
                        case 's': prof_rate /= 1000000000; break;
 
169
                        case 'm': prof_rate /= 1000000; break;
 
170
                        case 'u': prof_rate /= 1000; break;
 
171
                        }
 
172
                }
 
173
                return prof_rate;
 
174
        }
 
175
 
 
176
        /*
 
177
         * (non-Javadoc)
 
178
         * @see org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField#getBackground(java.lang.Object)
 
179
         */
 
180
        public Color getBackground(Object element) {
 
181
                return GmonView.getBackground(element);
 
182
        }
 
183
 
 
184
        /* 
 
185
         * (non-Javadoc)
 
186
         * @see org.eclipse.linuxtools.dataviewers.abstractviewers.AbstractSTDataViewersField#getToolTipText(java.lang.Object)
 
187
         */
 
188
        @Override
 
189
        public String getToolTipText(Object element) {
 
190
                String suffix = "";
 
191
                Object o = viewer.getInput();
 
192
                if (o instanceof GmonDecoder) {
 
193
                        GmonDecoder decoder = (GmonDecoder) o;
 
194
                        if (decoder.isICache()) {
 
195
                                suffix = " in instruction cache";
 
196
                        } else if (decoder.isDCache()) {
 
197
                                suffix = " in data cache";
 
198
                        }
 
199
                }
 
200
                
 
201
                if (element instanceof HistRoot) {
 
202
                        return "total time spent in the program" + suffix;
 
203
                } else if (element instanceof HistFunction) {
 
204
                        return "time spent in this function" + suffix;
 
205
                } else if (element instanceof HistFile) {
 
206
                        return "time spent in this file" + suffix;
 
207
                } else if (element instanceof HistLine) {
 
208
                        return "time spent at this location" + suffix;
 
209
                }
 
210
                return null;
 
211
        }
 
212
 
 
213
        /**
 
214
         * Switch from samples to time ans vice-versa
 
215
         */
 
216
        public void toggle() {
 
217
                this.samples = !this.samples;
 
218
        }
 
219
 
 
220
        public Number getNumber(Object obj) {
 
221
                TreeElement e = (TreeElement) obj;
 
222
                int i = e.getSamples();
 
223
                if (i == -1) return 0L;
 
224
                return i;
 
225
        }
 
226
 
 
227
        
 
228
        
 
229
}