1 /*******************************************************************************
2 * Copyright (c) 2014 É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
.lttng2
.kernel
.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
.internal
.lttng2
.kernel
.ui
.Activator
;
24 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.analysis
.cpuusage
.LttngKernelCpuUsageAnalysis
;
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
.TmfTraceUtils
;
28 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
31 * CPU usage viewer with XY line chart. It displays the total CPU usage and that
32 * of the threads selected in the CPU usage tree viewer.
34 * @author Geneviève Bastien
36 public class CpuUsageXYViewer
extends TmfCommonXLineChartViewer
{
38 private LttngKernelCpuUsageAnalysis fModule
= null;
40 /* Maps a thread ID to a list of y values */
41 private final Map
<String
, double[]> fYValues
= new LinkedHashMap
<>();
43 * To avoid up and downs CPU usage when process is in and out of CPU
44 * frequently, use a smaller resolution to get better averages.
46 private static final double RESOLUTION
= 0.4;
48 // Timeout between updates in the updateData thread
49 private static final long BUILD_UPDATE_TIMEOUT
= 500;
51 private long fSelectedThread
= -1;
59 public CpuUsageXYViewer(Composite parent
) {
60 super(parent
, Messages
.CpuUsageXYViewer_Title
, Messages
.CpuUsageXYViewer_TimeXAxis
, Messages
.CpuUsageXYViewer_CpuYAxis
);
61 setResolution(RESOLUTION
);
65 protected void initializeDataSource() {
66 if (getTrace() != null) {
67 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(getTrace(), LttngKernelCpuUsageAnalysis
.class, LttngKernelCpuUsageAnalysis
.ID
);
68 if (fModule
== null) {
75 private static double[] zeroFill(int nb
) {
76 double[] arr
= new double[nb
];
77 Arrays
.fill(arr
, 0.0);
82 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
84 if (getTrace() == null || fModule
== null) {
87 fModule
.waitForInitialization();
88 ITmfStateSystem ss
= fModule
.getStateSystem();
92 double[] xvalues
= getXAxis(start
, end
, nb
);
93 if (xvalues
.length
== 0) {
98 boolean complete
= false;
99 long currentEnd
= start
;
101 while (!complete
&& currentEnd
< end
) {
103 if (monitor
.isCanceled()) {
107 long traceStart
= getStartTime();
108 long traceEnd
= getEndTime();
109 long offset
= getTimeOffset();
110 long selectedThread
= fSelectedThread
;
112 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
113 currentEnd
= ss
.getCurrentEndTime();
115 /* Initialize the data */
116 Map
<String
, Long
> cpuUsageMap
= fModule
.getCpuUsageInRange(Math
.max(start
, traceStart
), Math
.min(end
, traceEnd
));
117 Map
<String
, String
> totalEntries
= new HashMap
<>();
119 fYValues
.put(Messages
.CpuUsageXYViewer_Total
, zeroFill(xvalues
.length
));
120 String stringSelectedThread
= Long
.toString(selectedThread
);
121 if (selectedThread
!= -1) {
122 fYValues
.put(stringSelectedThread
, zeroFill(xvalues
.length
));
125 for (Entry
<String
, Long
> entry
: cpuUsageMap
.entrySet()) {
127 * Process only entries representing the total of all CPUs
128 * and that have time on CPU
130 if (entry
.getValue() == 0) {
133 if (!entry
.getKey().startsWith(LttngKernelCpuUsageAnalysis
.TOTAL
)) {
136 String
[] strings
= entry
.getKey().split(LttngKernelCpuUsageAnalysis
.SPLIT_STRING
, 2);
138 if ((strings
.length
> 1) && !(strings
[1].equals(LttngKernelCpuUsageAnalysis
.TID_ZERO
))) {
139 /* This is the total cpu usage for a thread */
140 totalEntries
.put(strings
[1], entry
.getKey());
144 double prevX
= xvalues
[0];
145 long prevTime
= (long) prevX
+ offset
;
147 * make sure that time is in the trace range after double to
150 prevTime
= Math
.max(traceStart
, prevTime
);
151 prevTime
= Math
.min(traceEnd
, prevTime
);
152 /* Get CPU usage statistics for each x value */
153 for (int i
= 1; i
< xvalues
.length
; i
++) {
154 if (monitor
.isCanceled()) {
158 double x
= xvalues
[i
];
159 long time
= (long) x
+ offset
;
160 time
= Math
.max(traceStart
, time
);
161 time
= Math
.min(traceEnd
, time
);
163 cpuUsageMap
= fModule
.getCpuUsageInRange(prevTime
, time
);
166 * Calculate the sum of all total entries, and add a data
167 * point to the selected one
169 for (Entry
<String
, String
> entry
: totalEntries
.entrySet()) {
170 Long cpuEntry
= cpuUsageMap
.get(entry
.getValue());
171 cpuEntry
= cpuEntry
!= null ? cpuEntry
: 0L;
173 totalCpu
+= cpuEntry
;
175 if (entry
.getKey().equals(stringSelectedThread
)) {
176 /* This is the total cpu usage for a thread */
177 fYValues
.get(entry
.getKey())[i
] = (double) cpuEntry
/ (double) (time
- prevTime
) * 100;
181 fYValues
.get(Messages
.CpuUsageXYViewer_Total
)[i
] = (double) totalCpu
/ (double) (time
- prevTime
) * 100;
184 for (Entry
<String
, double[]> entry
: fYValues
.entrySet()) {
185 setSeries(entry
.getKey(), entry
.getValue());
187 if (monitor
.isCanceled()) {
192 } catch (StateValueTypeException e
) {
193 Activator
.getDefault().logError("Error updating the data of the CPU usage view", e
); //$NON-NLS-1$
199 * Set the selected thread ID, which will be graphed in this viewer
202 * The selected thread ID
204 public void setSelectedThread(long tid
) {
206 deleteSeries(Long
.toString(fSelectedThread
));
207 fSelectedThread
= tid
;