Commit | Line | Data |
---|---|---|
dffc234f | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2014, 2015 École Polytechnique de Montréal |
dffc234f GB |
3 | * |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Geneviève Bastien - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
8b131c41 | 13 | package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.cpuusage; |
dffc234f | 14 | |
202956f1 AM |
15 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
16 | ||
dffc234f GB |
17 | import java.util.Arrays; |
18 | import java.util.HashMap; | |
19 | import java.util.LinkedHashMap; | |
20 | import java.util.Map; | |
21 | import java.util.Map.Entry; | |
3fda290f MK |
22 | import java.util.Set; |
23 | import java.util.TreeSet; | |
dffc234f GB |
24 | |
25 | import org.eclipse.core.runtime.IProgressMonitor; | |
3fda290f | 26 | import org.eclipse.jdt.annotation.NonNull; |
dffc234f | 27 | import org.eclipse.swt.widgets.Composite; |
e363eae1 AM |
28 | import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis; |
29 | import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator; | |
e894a508 AM |
30 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
31 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException; | |
629bf3c1 JCK |
32 | import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler; |
33 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceOpenedSignal; | |
34 | import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal; | |
1d83ed07 | 35 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
b8585c7c | 36 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
2bdf0193 | 37 | import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer; |
dffc234f | 38 | |
3fda290f MK |
39 | import com.google.common.base.Joiner; |
40 | ||
dffc234f GB |
41 | /** |
42 | * CPU usage viewer with XY line chart. It displays the total CPU usage and that | |
43 | * of the threads selected in the CPU usage tree viewer. | |
44 | * | |
45 | * @author Geneviève Bastien | |
46 | */ | |
47 | public class CpuUsageXYViewer extends TmfCommonXLineChartViewer { | |
48 | ||
629bf3c1 JCK |
49 | private static final int NOT_SELECTED = -1; |
50 | ||
e363eae1 | 51 | private KernelCpuUsageAnalysis fModule = null; |
dffc234f GB |
52 | |
53 | /* Maps a thread ID to a list of y values */ | |
54 | private final Map<String, double[]> fYValues = new LinkedHashMap<>(); | |
55 | /* | |
56 | * To avoid up and downs CPU usage when process is in and out of CPU | |
57 | * frequently, use a smaller resolution to get better averages. | |
58 | */ | |
59 | private static final double RESOLUTION = 0.4; | |
60 | ||
d48661f2 GB |
61 | // Timeout between updates in the updateData thread |
62 | private static final long BUILD_UPDATE_TIMEOUT = 500; | |
63 | ||
629bf3c1 | 64 | private long fSelectedThread = NOT_SELECTED; |
dffc234f | 65 | |
3fda290f MK |
66 | private final @NonNull Set<@NonNull Integer> fCpus = new TreeSet<>(); |
67 | ||
dffc234f GB |
68 | /** |
69 | * Constructor | |
70 | * | |
71 | * @param parent | |
72 | * parent composite | |
dffc234f | 73 | */ |
e9a0d1cb | 74 | public CpuUsageXYViewer(Composite parent) { |
dffc234f GB |
75 | super(parent, Messages.CpuUsageXYViewer_Title, Messages.CpuUsageXYViewer_TimeXAxis, Messages.CpuUsageXYViewer_CpuYAxis); |
76 | setResolution(RESOLUTION); | |
58bc31eb MK |
77 | getSwtChart().getTitle().setVisible(true); |
78 | getSwtChart().getLegend().setVisible(false); | |
dffc234f GB |
79 | } |
80 | ||
81 | @Override | |
82 | protected void initializeDataSource() { | |
1d83ed07 AM |
83 | ITmfTrace trace = getTrace(); |
84 | if (trace != null) { | |
85 | fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID); | |
dffc234f GB |
86 | if (fModule == null) { |
87 | return; | |
88 | } | |
89 | fModule.schedule(); | |
90 | } | |
91 | } | |
92 | ||
93 | private static double[] zeroFill(int nb) { | |
94 | double[] arr = new double[nb]; | |
95 | Arrays.fill(arr, 0.0); | |
96 | return arr; | |
97 | } | |
98 | ||
99 | @Override | |
100 | protected void updateData(long start, long end, int nb, IProgressMonitor monitor) { | |
101 | try { | |
102 | if (getTrace() == null || fModule == null) { | |
103 | return; | |
104 | } | |
d48661f2 | 105 | fModule.waitForInitialization(); |
dffc234f | 106 | ITmfStateSystem ss = fModule.getStateSystem(); |
dffc234f GB |
107 | if (ss == null) { |
108 | return; | |
109 | } | |
110 | double[] xvalues = getXAxis(start, end, nb); | |
111 | if (xvalues.length == 0) { | |
112 | return; | |
113 | } | |
114 | setXAxis(xvalues); | |
115 | ||
d48661f2 | 116 | boolean complete = false; |
fee88beb | 117 | long currentEnd = Math.max(ss.getStartTime(), start); |
dffc234f | 118 | |
d48661f2 | 119 | while (!complete && currentEnd < end) { |
dffc234f | 120 | |
dffc234f GB |
121 | if (monitor.isCanceled()) { |
122 | return; | |
123 | } | |
dffc234f | 124 | |
fee88beb | 125 | long traceStart = Math.max(getStartTime(), ss.getStartTime()); |
d48661f2 GB |
126 | long traceEnd = getEndTime(); |
127 | long offset = getTimeOffset(); | |
128 | long selectedThread = fSelectedThread; | |
129 | ||
130 | complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT); | |
131 | currentEnd = ss.getCurrentEndTime(); | |
132 | ||
133 | /* Initialize the data */ | |
3fda290f | 134 | Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(fCpus, Math.max(start, traceStart), Math.min(end, traceEnd)); |
d48661f2 GB |
135 | Map<String, String> totalEntries = new HashMap<>(); |
136 | fYValues.clear(); | |
137 | fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length)); | |
138 | String stringSelectedThread = Long.toString(selectedThread); | |
629bf3c1 | 139 | if (selectedThread != NOT_SELECTED) { |
d48661f2 GB |
140 | fYValues.put(stringSelectedThread, zeroFill(xvalues.length)); |
141 | } | |
142 | ||
143 | for (Entry<String, Long> entry : cpuUsageMap.entrySet()) { | |
144 | /* | |
145 | * Process only entries representing the total of all CPUs | |
146 | * and that have time on CPU | |
147 | */ | |
148 | if (entry.getValue() == 0) { | |
149 | continue; | |
150 | } | |
e363eae1 | 151 | if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) { |
d48661f2 GB |
152 | continue; |
153 | } | |
e363eae1 | 154 | String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2); |
d48661f2 | 155 | |
e363eae1 | 156 | if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) { |
d48661f2 GB |
157 | /* This is the total cpu usage for a thread */ |
158 | totalEntries.put(strings[1], entry.getKey()); | |
159 | } | |
160 | } | |
dffc234f | 161 | |
932fc182 | 162 | double prevX = xvalues[0] - 1; |
d48661f2 | 163 | long prevTime = (long) prevX + offset; |
dffc234f | 164 | /* |
d48661f2 GB |
165 | * make sure that time is in the trace range after double to |
166 | * long conversion | |
dffc234f | 167 | */ |
d48661f2 GB |
168 | prevTime = Math.max(traceStart, prevTime); |
169 | prevTime = Math.min(traceEnd, prevTime); | |
170 | /* Get CPU usage statistics for each x value */ | |
932fc182 | 171 | for (int i = 0; i < xvalues.length; i++) { |
d48661f2 GB |
172 | if (monitor.isCanceled()) { |
173 | return; | |
174 | } | |
175 | long totalCpu = 0; | |
176 | double x = xvalues[i]; | |
177 | long time = (long) x + offset; | |
178 | time = Math.max(traceStart, time); | |
179 | time = Math.min(traceEnd, time); | |
932fc182 PT |
180 | if (time == prevTime) { |
181 | /* | |
182 | * we need at least 1 time unit to be able to get cpu | |
183 | * usage when zoomed in | |
184 | */ | |
185 | prevTime = time - 1; | |
186 | } | |
dffc234f | 187 | |
3fda290f | 188 | cpuUsageMap = fModule.getCpuUsageInRange(fCpus, prevTime, time); |
dffc234f | 189 | |
d48661f2 GB |
190 | /* |
191 | * Calculate the sum of all total entries, and add a data | |
192 | * point to the selected one | |
193 | */ | |
194 | for (Entry<String, String> entry : totalEntries.entrySet()) { | |
195 | Long cpuEntry = cpuUsageMap.get(entry.getValue()); | |
196 | cpuEntry = cpuEntry != null ? cpuEntry : 0L; | |
197 | ||
198 | totalCpu += cpuEntry; | |
dffc234f | 199 | |
d48661f2 GB |
200 | if (entry.getKey().equals(stringSelectedThread)) { |
201 | /* This is the total cpu usage for a thread */ | |
202956f1 AM |
202 | double[] key = checkNotNull(fYValues.get(entry.getKey())); |
203 | key[i] = (double) cpuEntry / (double) (time - prevTime) * 100; | |
d48661f2 GB |
204 | } |
205 | ||
206 | } | |
202956f1 AM |
207 | double[] key = checkNotNull(fYValues.get(Messages.CpuUsageXYViewer_Total)); |
208 | key[i] = (double) totalCpu / (double) (time - prevTime) * 100; | |
d48661f2 | 209 | prevTime = time; |
dffc234f | 210 | } |
d48661f2 GB |
211 | for (Entry<String, double[]> entry : fYValues.entrySet()) { |
212 | setSeries(entry.getKey(), entry.getValue()); | |
213 | } | |
214 | if (monitor.isCanceled()) { | |
215 | return; | |
216 | } | |
217 | updateDisplay(); | |
dffc234f | 218 | } |
dffc234f GB |
219 | } catch (StateValueTypeException e) { |
220 | Activator.getDefault().logError("Error updating the data of the CPU usage view", e); //$NON-NLS-1$ | |
221 | } | |
222 | ||
223 | } | |
224 | ||
225 | /** | |
226 | * Set the selected thread ID, which will be graphed in this viewer | |
227 | * | |
228 | * @param tid | |
229 | * The selected thread ID | |
230 | */ | |
231 | public void setSelectedThread(long tid) { | |
232 | cancelUpdate(); | |
233 | deleteSeries(Long.toString(fSelectedThread)); | |
234 | fSelectedThread = tid; | |
235 | updateContent(); | |
236 | } | |
237 | ||
3fda290f MK |
238 | /** |
239 | * Gets the analysis module | |
240 | * | |
241 | * @return the {@link KernelCpuUsageAnalysis} | |
242 | * | |
243 | * @since 2.0 | |
244 | */ | |
245 | public KernelCpuUsageAnalysis getModule() { | |
246 | return fModule; | |
247 | } | |
248 | ||
249 | /** | |
250 | * Add a core | |
251 | * | |
252 | * @param core | |
253 | * the core to add | |
254 | * @since 2.0 | |
255 | */ | |
256 | public void addCpu(int core) { | |
257 | fCpus.add(core); | |
258 | cancelUpdate(); | |
259 | updateContent(); | |
260 | getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList()); | |
261 | } | |
262 | ||
263 | /** | |
264 | * Remove a core | |
265 | * | |
266 | * @param core | |
267 | * the core to remove | |
268 | * @since 2.0 | |
269 | */ | |
270 | public void removeCpu(int core) { | |
271 | fCpus.remove(core); | |
272 | cancelUpdate(); | |
273 | updateContent(); | |
274 | getSwtChart().getTitle().setText(Messages.CpuUsageView_Title + ' ' + getCpuList()); | |
275 | } | |
276 | ||
277 | private String getCpuList() { | |
278 | return Joiner.on(", ").join(fCpus); //$NON-NLS-1$ | |
279 | } | |
280 | ||
281 | /** | |
282 | * Clears the cores | |
283 | * | |
284 | * @since 2.0 | |
285 | */ | |
286 | public void clearCpu() { | |
287 | fCpus.clear(); | |
288 | cancelUpdate(); | |
289 | updateContent(); | |
290 | getSwtChart().getTitle().setText(Messages.CpuUsageView_Title); | |
291 | } | |
292 | ||
629bf3c1 JCK |
293 | @Override |
294 | @TmfSignalHandler | |
295 | public void traceSelected(TmfTraceSelectedSignal signal) { | |
296 | setSelectedThread(NOT_SELECTED); | |
297 | super.traceSelected(signal); | |
298 | } | |
299 | ||
300 | @Override | |
301 | @TmfSignalHandler | |
302 | public void traceOpened(TmfTraceOpenedSignal signal) { | |
303 | setSelectedThread(NOT_SELECTED); | |
304 | super.traceOpened(signal); | |
305 | } | |
306 | ||
dffc234f | 307 | } |