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