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