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
.linuxtools
.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
.linuxtools
.internal
.lttng2
.kernel
.ui
.Activator
;
23 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.cpuusage
.LttngKernelCpuUsageAnalysis
;
24 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
25 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
26 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.xycharts
.linecharts
.TmfCommonXLineChartViewer
;
27 import org
.eclipse
.swt
.widgets
.Composite
;
30 * CPU usage viewer with XY line chart. It displays the total CPU usage and that
31 * of the threads selected in the CPU usage tree viewer.
33 * @author Geneviève Bastien
35 public class CpuUsageXYViewer
extends TmfCommonXLineChartViewer
{
37 private LttngKernelCpuUsageAnalysis fModule
= null;
39 /* Maps a thread ID to a list of y values */
40 private final Map
<String
, double[]> fYValues
= new LinkedHashMap
<>();
42 * To avoid up and downs CPU usage when process is in and out of CPU
43 * frequently, use a smaller resolution to get better averages.
45 private static final double RESOLUTION
= 0.4;
47 // Timeout between updates in the updateData thread
48 private static final long BUILD_UPDATE_TIMEOUT
= 500;
50 private long fSelectedThread
= -1;
58 public CpuUsageXYViewer(Composite parent
) {
59 super(parent
, Messages
.CpuUsageXYViewer_Title
, Messages
.CpuUsageXYViewer_TimeXAxis
, Messages
.CpuUsageXYViewer_CpuYAxis
);
60 setResolution(RESOLUTION
);
64 protected void initializeDataSource() {
65 if (getTrace() != null) {
66 fModule
= getTrace().getAnalysisModuleOfClass(LttngKernelCpuUsageAnalysis
.class, LttngKernelCpuUsageAnalysis
.ID
);
67 if (fModule
== null) {
74 private static double[] zeroFill(int nb
) {
75 double[] arr
= new double[nb
];
76 Arrays
.fill(arr
, 0.0);
81 protected void updateData(long start
, long end
, int nb
, IProgressMonitor monitor
) {
83 if (getTrace() == null || fModule
== null) {
86 fModule
.waitForInitialization();
87 ITmfStateSystem ss
= fModule
.getStateSystem();
91 double[] xvalues
= getXAxis(start
, end
, nb
);
92 if (xvalues
.length
== 0) {
97 boolean complete
= false;
98 long currentEnd
= start
;
100 while (!complete
&& currentEnd
< end
) {
102 if (monitor
.isCanceled()) {
106 long traceStart
= getStartTime();
107 long traceEnd
= getEndTime();
108 long offset
= getTimeOffset();
109 long selectedThread
= fSelectedThread
;
111 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
112 currentEnd
= ss
.getCurrentEndTime();
114 /* Initialize the data */
115 Map
<String
, Long
> cpuUsageMap
= fModule
.getCpuUsageInRange(Math
.max(start
, traceStart
), Math
.min(end
, traceEnd
));
116 Map
<String
, String
> totalEntries
= new HashMap
<>();
118 fYValues
.put(Messages
.CpuUsageXYViewer_Total
, zeroFill(xvalues
.length
));
119 String stringSelectedThread
= Long
.toString(selectedThread
);
120 if (selectedThread
!= -1) {
121 fYValues
.put(stringSelectedThread
, zeroFill(xvalues
.length
));
124 for (Entry
<String
, Long
> entry
: cpuUsageMap
.entrySet()) {
126 * Process only entries representing the total of all CPUs
127 * and that have time on CPU
129 if (entry
.getValue() == 0) {
132 if (!entry
.getKey().startsWith(LttngKernelCpuUsageAnalysis
.TOTAL
)) {
135 String
[] strings
= entry
.getKey().split(LttngKernelCpuUsageAnalysis
.SPLIT_STRING
, 2);
137 if ((strings
.length
> 1) && !(strings
[1].equals(LttngKernelCpuUsageAnalysis
.TID_ZERO
))) {
138 /* This is the total cpu usage for a thread */
139 totalEntries
.put(strings
[1], entry
.getKey());
143 double prevX
= xvalues
[0];
144 long prevTime
= (long) prevX
+ offset
;
146 * make sure that time is in the trace range after double to
149 prevTime
= Math
.max(traceStart
, prevTime
);
150 prevTime
= Math
.min(traceEnd
, prevTime
);
151 /* Get CPU usage statistics for each x value */
152 for (int i
= 1; i
< xvalues
.length
; i
++) {
153 if (monitor
.isCanceled()) {
157 double x
= xvalues
[i
];
158 long time
= (long) x
+ offset
;
159 time
= Math
.max(traceStart
, time
);
160 time
= Math
.min(traceEnd
, time
);
162 cpuUsageMap
= fModule
.getCpuUsageInRange(prevTime
, time
);
165 * Calculate the sum of all total entries, and add a data
166 * point to the selected one
168 for (Entry
<String
, String
> entry
: totalEntries
.entrySet()) {
169 Long cpuEntry
= cpuUsageMap
.get(entry
.getValue());
170 cpuEntry
= cpuEntry
!= null ? cpuEntry
: 0L;
172 totalCpu
+= cpuEntry
;
174 if (entry
.getKey().equals(stringSelectedThread
)) {
175 /* This is the total cpu usage for a thread */
176 fYValues
.get(entry
.getKey())[i
] = (double) cpuEntry
/ (double) (time
- prevTime
) * 100;
180 fYValues
.get(Messages
.CpuUsageXYViewer_Total
)[i
] = (double) totalCpu
/ (double) (time
- prevTime
) * 100;
183 for (Entry
<String
, double[]> entry
: fYValues
.entrySet()) {
184 setSeries(entry
.getKey(), entry
.getValue());
186 if (monitor
.isCanceled()) {
191 } catch (StateValueTypeException e
) {
192 Activator
.getDefault().logError("Error updating the data of the CPU usage view", e
); //$NON-NLS-1$
198 * Set the selected thread ID, which will be graphed in this viewer
201 * The selected thread ID
203 public void setSelectedThread(long tid
) {
205 deleteSeries(Long
.toString(fSelectedThread
));
206 fSelectedThread
= tid
;