1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
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
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.views
.cpuusage
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.Arrays
;
18 import java
.util
.HashMap
;
19 import java
.util
.LinkedHashMap
;
21 import java
.util
.Map
.Entry
;
23 import java
.util
.TreeSet
;
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
.trace
.ITmfTrace
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
34 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
36 import com
.google
.common
.base
.Joiner
;
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.
42 * @author Geneviève Bastien
44 public class CpuUsageXYViewer
extends TmfCommonXLineChartViewer
{
46 private KernelCpuUsageAnalysis fModule
= null;
48 /* Maps a thread ID to a list of y values */
49 private final Map
<String
, double[]> fYValues
= new LinkedHashMap
<>();
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.
54 private static final double RESOLUTION
= 0.4;
56 // Timeout between updates in the updateData thread
57 private static final long BUILD_UPDATE_TIMEOUT
= 500;
59 private long fSelectedThread
= -1;
61 private final @NonNull Set
<@NonNull Integer
> fCpus
= new TreeSet
<>();
69 public CpuUsageXYViewer(Composite parent
) {
70 super(parent
, Messages
.CpuUsageXYViewer_Title
, Messages
.CpuUsageXYViewer_TimeXAxis
, Messages
.CpuUsageXYViewer_CpuYAxis
);
71 setResolution(RESOLUTION
);
75 protected void initializeDataSource() {
76 ITmfTrace trace
= getTrace();
78 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, KernelCpuUsageAnalysis
.class, KernelCpuUsageAnalysis
.ID
);
79 if (fModule
== null) {
86 private static double[] zeroFill(int nb
) {
87 double[] arr
= new double[nb
];
88 Arrays
.fill(arr
, 0.0);
93 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
95 if (getTrace() == null || fModule
== null) {
98 fModule
.waitForInitialization();
99 ITmfStateSystem ss
= fModule
.getStateSystem();
103 double[] xvalues
= getXAxis(start
, end
, nb
);
104 if (xvalues
.length
== 0) {
109 boolean complete
= false;
110 long currentEnd
= start
;
112 while (!complete
&& currentEnd
< end
) {
114 if (monitor
.isCanceled()) {
118 long traceStart
= getStartTime();
119 long traceEnd
= getEndTime();
120 long offset
= getTimeOffset();
121 long selectedThread
= fSelectedThread
;
123 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
124 currentEnd
= ss
.getCurrentEndTime();
126 /* Initialize the data */
127 Map
<String
, Long
> cpuUsageMap
= fModule
.getCpuUsageInRange(fCpus
, Math
.max(start
, traceStart
), Math
.min(end
, traceEnd
));
128 Map
<String
, String
> totalEntries
= new HashMap
<>();
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
));
136 for (Entry
<String
, Long
> entry
: cpuUsageMap
.entrySet()) {
138 * Process only entries representing the total of all CPUs
139 * and that have time on CPU
141 if (entry
.getValue() == 0) {
144 if (!entry
.getKey().startsWith(KernelCpuUsageAnalysis
.TOTAL
)) {
147 String
[] strings
= entry
.getKey().split(KernelCpuUsageAnalysis
.SPLIT_STRING
, 2);
149 if ((strings
.length
> 1) && !(strings
[1].equals(KernelCpuUsageAnalysis
.TID_ZERO
))) {
150 /* This is the total cpu usage for a thread */
151 totalEntries
.put(strings
[1], entry
.getKey());
155 double prevX
= xvalues
[0] - 1;
156 long prevTime
= (long) prevX
+ offset
;
158 * make sure that time is in the trace range after double to
161 prevTime
= Math
.max(traceStart
, prevTime
);
162 prevTime
= Math
.min(traceEnd
, prevTime
);
163 /* Get CPU usage statistics for each x value */
164 for (int i
= 0; i
< xvalues
.length
; i
++) {
165 if (monitor
.isCanceled()) {
169 double x
= xvalues
[i
];
170 long time
= (long) x
+ offset
;
171 time
= Math
.max(traceStart
, time
);
172 time
= Math
.min(traceEnd
, time
);
173 if (time
== prevTime
) {
175 * we need at least 1 time unit to be able to get cpu
176 * usage when zoomed in
181 cpuUsageMap
= fModule
.getCpuUsageInRange(fCpus
, prevTime
, time
);
184 * Calculate the sum of all total entries, and add a data
185 * point to the selected one
187 for (Entry
<String
, String
> entry
: totalEntries
.entrySet()) {
188 Long cpuEntry
= cpuUsageMap
.get(entry
.getValue());
189 cpuEntry
= cpuEntry
!= null ? cpuEntry
: 0L;
191 totalCpu
+= cpuEntry
;
193 if (entry
.getKey().equals(stringSelectedThread
)) {
194 /* This is the total cpu usage for a thread */
195 double[] key
= checkNotNull(fYValues
.get(entry
.getKey()));
196 key
[i
] = (double) cpuEntry
/ (double) (time
- prevTime
) * 100;
200 double[] key
= checkNotNull(fYValues
.get(Messages
.CpuUsageXYViewer_Total
));
201 key
[i
] = (double) totalCpu
/ (double) (time
- prevTime
) * 100;
204 for (Entry
<String
, double[]> entry
: fYValues
.entrySet()) {
205 setSeries(entry
.getKey(), entry
.getValue());
207 if (monitor
.isCanceled()) {
212 } catch (StateValueTypeException e
) {
213 Activator
.getDefault().logError("Error updating the data of the CPU usage view", e
); //$NON-NLS-1$
219 * Set the selected thread ID, which will be graphed in this viewer
222 * The selected thread ID
224 public void setSelectedThread(long tid
) {
226 deleteSeries(Long
.toString(fSelectedThread
));
227 fSelectedThread
= tid
;
232 * Gets the analysis module
234 * @return the {@link KernelCpuUsageAnalysis}
238 public KernelCpuUsageAnalysis
getModule() {
249 public void addCpu(int core
) {
253 getSwtChart().getTitle().setText(Messages
.CpuUsageView_Title
+ ' ' + getCpuList());
263 public void removeCpu(int core
) {
267 getSwtChart().getTitle().setText(Messages
.CpuUsageView_Title
+ ' ' + getCpuList());
270 private String
getCpuList() {
271 return Joiner
.on(", ").join(fCpus
); //$NON-NLS-1$
279 public void clearCpu() {
283 getSwtChart().getTitle().setText(Messages
.CpuUsageView_Title
);