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