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
.analysis
.os
.linux
.ui
.views
.cpuusage
;
15 import java
.util
.Arrays
;
16 import java
.util
.HashMap
;
17 import java
.util
.LinkedHashMap
;
19 import java
.util
.Map
.Entry
;
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
;
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.
35 * @author Geneviève Bastien
37 public class CpuUsageXYViewer
extends TmfCommonXLineChartViewer
{
39 private KernelCpuUsageAnalysis fModule
= null;
41 /* Maps a thread ID to a list of y values */
42 private final Map
<String
, double[]> fYValues
= new LinkedHashMap
<>();
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.
47 private static final double RESOLUTION
= 0.4;
49 // Timeout between updates in the updateData thread
50 private static final long BUILD_UPDATE_TIMEOUT
= 500;
52 private long fSelectedThread
= -1;
60 public CpuUsageXYViewer(Composite parent
) {
61 super(parent
, Messages
.CpuUsageXYViewer_Title
, Messages
.CpuUsageXYViewer_TimeXAxis
, Messages
.CpuUsageXYViewer_CpuYAxis
);
62 setResolution(RESOLUTION
);
66 protected void initializeDataSource() {
67 ITmfTrace trace
= getTrace();
69 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, KernelCpuUsageAnalysis
.class, KernelCpuUsageAnalysis
.ID
);
70 if (fModule
== null) {
77 private static double[] zeroFill(int nb
) {
78 double[] arr
= new double[nb
];
79 Arrays
.fill(arr
, 0.0);
84 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
86 if (getTrace() == null || fModule
== null) {
89 fModule
.waitForInitialization();
90 ITmfStateSystem ss
= fModule
.getStateSystem();
94 double[] xvalues
= getXAxis(start
, end
, nb
);
95 if (xvalues
.length
== 0) {
100 boolean complete
= false;
101 long currentEnd
= start
;
103 while (!complete
&& currentEnd
< end
) {
105 if (monitor
.isCanceled()) {
109 long traceStart
= getStartTime();
110 long traceEnd
= getEndTime();
111 long offset
= getTimeOffset();
112 long selectedThread
= fSelectedThread
;
114 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
115 currentEnd
= ss
.getCurrentEndTime();
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
<>();
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
));
127 for (Entry
<String
, Long
> entry
: cpuUsageMap
.entrySet()) {
129 * Process only entries representing the total of all CPUs
130 * and that have time on CPU
132 if (entry
.getValue() == 0) {
135 if (!entry
.getKey().startsWith(KernelCpuUsageAnalysis
.TOTAL
)) {
138 String
[] strings
= entry
.getKey().split(KernelCpuUsageAnalysis
.SPLIT_STRING
, 2);
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());
146 double prevX
= xvalues
[0];
147 long prevTime
= (long) prevX
+ offset
;
149 * make sure that time is in the trace range after double to
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
= 1; i
< xvalues
.length
; i
++) {
156 if (monitor
.isCanceled()) {
160 double x
= xvalues
[i
];
161 long time
= (long) x
+ offset
;
162 time
= Math
.max(traceStart
, time
);
163 time
= Math
.min(traceEnd
, time
);
165 cpuUsageMap
= fModule
.getCpuUsageInRange(prevTime
, time
);
168 * Calculate the sum of all total entries, and add a data
169 * point to the selected one
171 for (Entry
<String
, String
> entry
: totalEntries
.entrySet()) {
172 Long cpuEntry
= cpuUsageMap
.get(entry
.getValue());
173 cpuEntry
= cpuEntry
!= null ? cpuEntry
: 0L;
175 totalCpu
+= cpuEntry
;
177 if (entry
.getKey().equals(stringSelectedThread
)) {
178 /* This is the total cpu usage for a thread */
179 fYValues
.get(entry
.getKey())[i
] = (double) cpuEntry
/ (double) (time
- prevTime
) * 100;
183 fYValues
.get(Messages
.CpuUsageXYViewer_Total
)[i
] = (double) totalCpu
/ (double) (time
- prevTime
) * 100;
186 for (Entry
<String
, double[]> entry
: fYValues
.entrySet()) {
187 setSeries(entry
.getKey(), entry
.getValue());
189 if (monitor
.isCanceled()) {
194 } catch (StateValueTypeException e
) {
195 Activator
.getDefault().logError("Error updating the data of the CPU usage view", e
); //$NON-NLS-1$
201 * Set the selected thread ID, which will be graphed in this viewer
204 * The selected thread ID
206 public void setSelectedThread(long tid
) {
208 deleteSeries(Long
.toString(fSelectedThread
));
209 fSelectedThread
= tid
;