tmf: Fix XY viewer first pixel value and NaN when pixel range is 0
[deliverable/tracecompass.git] / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / 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.analysis.os.linux.ui.views.cpuusage;
14
15 import java.util.Arrays;
16 import java.util.HashMap;
17 import java.util.LinkedHashMap;
18 import java.util.Map;
19 import java.util.Map.Entry;
20
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.swt.widgets.Composite;
23 import org.eclipse.tracecompass.analysis.os.linux.core.cpuusage.KernelCpuUsageAnalysis;
24 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
25 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
27 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
28 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
29 import org.eclipse.tracecompass.tmf.ui.viewers.xycharts.linecharts.TmfCommonXLineChartViewer;
30
31 /**
32 * CPU usage viewer with XY line chart. It displays the total CPU usage and that
33 * of the threads selected in the CPU usage tree viewer.
34 *
35 * @author Geneviève Bastien
36 */
37 public class CpuUsageXYViewer extends TmfCommonXLineChartViewer {
38
39 private KernelCpuUsageAnalysis fModule = null;
40
41 /* Maps a thread ID to a list of y values */
42 private final Map<String, double[]> fYValues = new LinkedHashMap<>();
43 /*
44 * To avoid up and downs CPU usage when process is in and out of CPU
45 * frequently, use a smaller resolution to get better averages.
46 */
47 private static final double RESOLUTION = 0.4;
48
49 // Timeout between updates in the updateData thread
50 private static final long BUILD_UPDATE_TIMEOUT = 500;
51
52 private long fSelectedThread = -1;
53
54 /**
55 * Constructor
56 *
57 * @param parent
58 * parent composite
59 */
60 public CpuUsageXYViewer(Composite parent) {
61 super(parent, Messages.CpuUsageXYViewer_Title, Messages.CpuUsageXYViewer_TimeXAxis, Messages.CpuUsageXYViewer_CpuYAxis);
62 setResolution(RESOLUTION);
63 }
64
65 @Override
66 protected void initializeDataSource() {
67 ITmfTrace trace = getTrace();
68 if (trace != null) {
69 fModule = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelCpuUsageAnalysis.class, KernelCpuUsageAnalysis.ID);
70 if (fModule == null) {
71 return;
72 }
73 fModule.schedule();
74 }
75 }
76
77 private static double[] zeroFill(int nb) {
78 double[] arr = new double[nb];
79 Arrays.fill(arr, 0.0);
80 return arr;
81 }
82
83 @Override
84 protected void updateData(long start, long end, int nb, IProgressMonitor monitor) {
85 try {
86 if (getTrace() == null || fModule == null) {
87 return;
88 }
89 fModule.waitForInitialization();
90 ITmfStateSystem ss = fModule.getStateSystem();
91 if (ss == null) {
92 return;
93 }
94 double[] xvalues = getXAxis(start, end, nb);
95 if (xvalues.length == 0) {
96 return;
97 }
98 setXAxis(xvalues);
99
100 boolean complete = false;
101 long currentEnd = start;
102
103 while (!complete && currentEnd < end) {
104
105 if (monitor.isCanceled()) {
106 return;
107 }
108
109 long traceStart = getStartTime();
110 long traceEnd = getEndTime();
111 long offset = getTimeOffset();
112 long selectedThread = fSelectedThread;
113
114 complete = ss.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
115 currentEnd = ss.getCurrentEndTime();
116
117 /* Initialize the data */
118 Map<String, Long> cpuUsageMap = fModule.getCpuUsageInRange(Math.max(start, traceStart), Math.min(end, traceEnd));
119 Map<String, String> totalEntries = new HashMap<>();
120 fYValues.clear();
121 fYValues.put(Messages.CpuUsageXYViewer_Total, zeroFill(xvalues.length));
122 String stringSelectedThread = Long.toString(selectedThread);
123 if (selectedThread != -1) {
124 fYValues.put(stringSelectedThread, zeroFill(xvalues.length));
125 }
126
127 for (Entry<String, Long> entry : cpuUsageMap.entrySet()) {
128 /*
129 * Process only entries representing the total of all CPUs
130 * and that have time on CPU
131 */
132 if (entry.getValue() == 0) {
133 continue;
134 }
135 if (!entry.getKey().startsWith(KernelCpuUsageAnalysis.TOTAL)) {
136 continue;
137 }
138 String[] strings = entry.getKey().split(KernelCpuUsageAnalysis.SPLIT_STRING, 2);
139
140 if ((strings.length > 1) && !(strings[1].equals(KernelCpuUsageAnalysis.TID_ZERO))) {
141 /* This is the total cpu usage for a thread */
142 totalEntries.put(strings[1], entry.getKey());
143 }
144 }
145
146 double prevX = xvalues[0] - 1;
147 long prevTime = (long) prevX + offset;
148 /*
149 * make sure that time is in the trace range after double to
150 * long conversion
151 */
152 prevTime = Math.max(traceStart, prevTime);
153 prevTime = Math.min(traceEnd, prevTime);
154 /* Get CPU usage statistics for each x value */
155 for (int i = 0; i < xvalues.length; i++) {
156 if (monitor.isCanceled()) {
157 return;
158 }
159 long totalCpu = 0;
160 double x = xvalues[i];
161 long time = (long) x + offset;
162 time = Math.max(traceStart, time);
163 time = Math.min(traceEnd, time);
164 if (time == prevTime) {
165 /*
166 * we need at least 1 time unit to be able to get cpu
167 * usage when zoomed in
168 */
169 prevTime = time - 1;
170 }
171
172 cpuUsageMap = fModule.getCpuUsageInRange(prevTime, time);
173
174 /*
175 * Calculate the sum of all total entries, and add a data
176 * point to the selected one
177 */
178 for (Entry<String, String> entry : totalEntries.entrySet()) {
179 Long cpuEntry = cpuUsageMap.get(entry.getValue());
180 cpuEntry = cpuEntry != null ? cpuEntry : 0L;
181
182 totalCpu += cpuEntry;
183
184 if (entry.getKey().equals(stringSelectedThread)) {
185 /* This is the total cpu usage for a thread */
186 fYValues.get(entry.getKey())[i] = (double) cpuEntry / (double) (time - prevTime) * 100;
187 }
188
189 }
190 fYValues.get(Messages.CpuUsageXYViewer_Total)[i] = (double) totalCpu / (double) (time - prevTime) * 100;
191 prevTime = time;
192 }
193 for (Entry<String, double[]> entry : fYValues.entrySet()) {
194 setSeries(entry.getKey(), entry.getValue());
195 }
196 if (monitor.isCanceled()) {
197 return;
198 }
199 updateDisplay();
200 }
201 } catch (StateValueTypeException e) {
202 Activator.getDefault().logError("Error updating the data of the CPU usage view", e); //$NON-NLS-1$
203 }
204
205 }
206
207 /**
208 * Set the selected thread ID, which will be graphed in this viewer
209 *
210 * @param tid
211 * The selected thread ID
212 */
213 public void setSelectedThread(long tid) {
214 cancelUpdate();
215 deleteSeries(Long.toString(fSelectedThread));
216 fSelectedThread = tid;
217 updateContent();
218 }
219
220 }
This page took 0.037367 seconds and 6 git commands to generate.