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
.ArrayList
;
18 import java
.util
.Collections
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
22 import java
.util
.Map
.Entry
;
24 import java
.util
.TreeSet
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.jface
.viewers
.Viewer
;
28 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
29 import org
.eclipse
.osgi
.util
.NLS
;
30 import org
.eclipse
.swt
.widgets
.Composite
;
31 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.cpuusage
.KernelCpuUsageAnalysis
;
32 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernel
.KernelAnalysisModule
;
33 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.kernel
.Attributes
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.AbstractTmfTreeViewer
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.ITmfTreeColumnDataProvider
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.ITmfTreeViewerEntry
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeColumnData
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeColumnData
.ITmfColumnPercentageProvider
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.tree
.TmfTreeViewerEntry
;
55 * Tree viewer to display CPU usage information in a specified time range. It
56 * shows the process's TID, its name, the time spent on the CPU during that
57 * range, in % and absolute value.
59 * @author Geneviève Bastien
61 public class CpuUsageComposite
extends AbstractTmfTreeViewer
{
63 // Timeout between to wait for in the updateElements method
64 private static final long BUILD_UPDATE_TIMEOUT
= 500;
66 private KernelCpuUsageAnalysis fModule
= null;
67 private String fSelectedThread
= null;
69 private static final String
[] COLUMN_NAMES
= new String
[] {
70 Messages
.CpuUsageComposite_ColumnTID
,
71 Messages
.CpuUsageComposite_ColumnProcess
,
72 Messages
.CpuUsageComposite_ColumnPercent
,
73 Messages
.CpuUsageComposite_ColumnTime
76 /* A map that saves the mapping of a thread ID to its executable name */
77 private final Map
<String
, String
> fProcessNameMap
= new HashMap
<>();
79 private final @NonNull Set
<@NonNull Integer
> fCpus
= new TreeSet
<>();
81 /** Provides label for the CPU usage tree viewer cells */
82 protected static class CpuLabelProvider
extends TreeLabelProvider
{
85 public String
getColumnText(Object element
, int columnIndex
) {
86 CpuUsageEntry obj
= (CpuUsageEntry
) element
;
87 if (columnIndex
== 0) {
89 } else if (columnIndex
== 1) {
90 return obj
.getProcessName();
91 } else if (columnIndex
== 2) {
92 return String
.format(Messages
.CpuUsageComposite_TextPercent
, obj
.getPercent());
93 } else if (columnIndex
== 3) {
94 return NLS
.bind(Messages
.CpuUsageComposite_TextTime
, obj
.getTime());
97 return element
.toString();
106 * The parent composite that holds this viewer
108 public CpuUsageComposite(Composite parent
) {
109 super(parent
, false);
110 setLabelProvider(new CpuLabelProvider());
114 protected ITmfTreeColumnDataProvider
getColumnDataProvider() {
115 return new ITmfTreeColumnDataProvider() {
118 public List
<TmfTreeColumnData
> getColumnData() {
119 /* All columns are sortable */
120 List
<TmfTreeColumnData
> columns
= new ArrayList
<>();
121 TmfTreeColumnData column
= new TmfTreeColumnData(COLUMN_NAMES
[0]);
122 column
.setComparator(new ViewerComparator() {
124 public int compare(Viewer viewer
, Object e1
, Object e2
) {
125 CpuUsageEntry n1
= (CpuUsageEntry
) e1
;
126 CpuUsageEntry n2
= (CpuUsageEntry
) e2
;
128 return n1
.getTid().compareTo(n2
.getTid());
133 column
= new TmfTreeColumnData(COLUMN_NAMES
[1]);
134 column
.setComparator(new ViewerComparator() {
136 public int compare(Viewer viewer
, Object e1
, Object e2
) {
137 CpuUsageEntry n1
= (CpuUsageEntry
) e1
;
138 CpuUsageEntry n2
= (CpuUsageEntry
) e2
;
140 return n1
.getProcessName().compareTo(n2
.getProcessName());
145 column
= new TmfTreeColumnData(COLUMN_NAMES
[2]);
146 column
.setComparator(new ViewerComparator() {
148 public int compare(Viewer viewer
, Object e1
, Object e2
) {
149 CpuUsageEntry n1
= (CpuUsageEntry
) e1
;
150 CpuUsageEntry n2
= (CpuUsageEntry
) e2
;
152 return n1
.getPercent().compareTo(n2
.getPercent());
156 column
.setPercentageProvider(new ITmfColumnPercentageProvider() {
159 public double getPercentage(Object data
) {
160 CpuUsageEntry parent
= (CpuUsageEntry
) data
;
161 return parent
.getPercent() / 100;
165 column
= new TmfTreeColumnData(COLUMN_NAMES
[3]);
166 column
.setComparator(new ViewerComparator() {
168 public int compare(Viewer viewer
, Object e1
, Object e2
) {
169 CpuUsageEntry n1
= (CpuUsageEntry
) e1
;
170 CpuUsageEntry n2
= (CpuUsageEntry
) e2
;
172 return n1
.getTime().compareTo(n2
.getTime());
184 // ------------------------------------------------------------------------
186 // ------------------------------------------------------------------------
189 protected void contentChanged(ITmfTreeViewerEntry rootEntry
) {
190 String selectedThread
= fSelectedThread
;
191 if (selectedThread
!= null) {
192 /* Find the selected thread among the inputs */
193 for (ITmfTreeViewerEntry entry
: rootEntry
.getChildren()) {
194 if (entry
instanceof CpuUsageEntry
) {
195 if (selectedThread
.equals(((CpuUsageEntry
) entry
).getTid())) {
196 List
<ITmfTreeViewerEntry
> list
= Collections
.singletonList(entry
);
197 super.setSelection(list
);
206 public void initializeDataSource() {
207 /* Should not be called while trace is still null */
208 ITmfTrace trace
= checkNotNull(getTrace());
210 fModule
= TmfTraceUtils
.getAnalysisModuleOfClass(trace
, KernelCpuUsageAnalysis
.class, KernelCpuUsageAnalysis
.ID
);
211 if (fModule
== null) {
215 fModule
.waitForInitialization();
216 fProcessNameMap
.clear();
220 protected ITmfTreeViewerEntry
updateElements(long start
, long end
, boolean isSelection
) {
221 if (isSelection
|| (start
== end
)) {
224 if (getTrace() == null || fModule
== null) {
227 fModule
.waitForInitialization();
228 ITmfStateSystem ss
= fModule
.getStateSystem();
233 boolean complete
= false;
234 long currentEnd
= Math
.max(start
, ss
.getStartTime());
236 while (!complete
&& currentEnd
< end
) {
237 complete
= ss
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
238 currentEnd
= ss
.getCurrentEndTime();
241 /* Initialize the data */
242 Map
<String
, Long
> cpuUsageMap
= fModule
.getCpuUsageInRange(fCpus
, Math
.max(start
, getStartTime()), Math
.min(end
, getEndTime()));
244 TmfTreeViewerEntry root
= new TmfTreeViewerEntry(""); //$NON-NLS-1$
245 List
<ITmfTreeViewerEntry
> entryList
= root
.getChildren();
247 for (Entry
<String
, Long
> entry
: cpuUsageMap
.entrySet()) {
249 * Process only entries representing the total of all CPUs and that
252 if (entry
.getValue() == 0) {
255 if (!entry
.getKey().startsWith(KernelCpuUsageAnalysis
.TOTAL
)) {
258 String
[] strings
= entry
.getKey().split(KernelCpuUsageAnalysis
.SPLIT_STRING
, 2);
260 if ((strings
.length
> 1) && !(strings
[1].equals(KernelCpuUsageAnalysis
.TID_ZERO
))) {
261 CpuUsageEntry obj
= new CpuUsageEntry(strings
[1], getProcessName(strings
[1]), (double) entry
.getValue() / (double) (end
- start
) * 100, entry
.getValue());
270 * Get the process name from its TID by using the LTTng kernel analysis
273 private String
getProcessName(String tid
) {
274 String execName
= fProcessNameMap
.get(tid
);
275 if (execName
!= null) {
278 ITmfTrace trace
= getTrace();
282 ITmfStateSystem kernelSs
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
283 if (kernelSs
== null) {
288 int cpusNode
= kernelSs
.getQuarkAbsolute(Attributes
.THREADS
);
290 /* Get the quarks for each cpu */
291 List
<Integer
> cpuNodes
= kernelSs
.getSubAttributes(cpusNode
, false);
293 for (Integer tidQuark
: cpuNodes
) {
294 if (kernelSs
.getAttributeName(tidQuark
).equals(tid
)) {
296 List
<ITmfStateInterval
> execNameIntervals
;
298 execNameQuark
= kernelSs
.getQuarkRelative(tidQuark
, Attributes
.EXEC_NAME
);
299 execNameIntervals
= StateSystemUtils
.queryHistoryRange(kernelSs
, execNameQuark
, getStartTime(), getEndTime());
300 } catch (TimeRangeException
| AttributeNotFoundException e
) {
302 * No information on this thread (yet?), skip it for now
305 } catch (StateSystemDisposedException e
) {
306 /* State system is closing down, no point continuing */
310 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
311 if (!execNameInterval
.getStateValue().isNull() &&
312 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
313 execName
= execNameInterval
.getStateValue().unboxStr();
314 fProcessNameMap
.put(tid
, execName
);
321 } catch (AttributeNotFoundException e
) {
322 /* can't find the process name, just return the tid instead */
328 * Set the currently selected thread ID
331 * The selected thread ID
333 public void setSelectedThread(String tid
) {
334 fSelectedThread
= tid
;
344 public void addCpu(int core
) {
346 updateContent(getWindowStartTime(), getWindowEndTime(), false);
356 public void removeCpu(int core
) {
358 updateContent(getWindowStartTime(), getWindowEndTime(), false);
367 public void clearCpu() {
369 updateContent(getWindowStartTime(), getWindowEndTime(), false);
374 public void traceSelected(TmfTraceSelectedSignal signal
) {
375 setSelectedThread(null);
376 super.traceSelected(signal
);
381 public void traceOpened(TmfTraceOpenedSignal signal
) {
382 setSelectedThread(null);
383 super.traceOpened(signal
);