1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal and others.
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
.core
.cpuusage
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.HashMap
;
18 import java
.util
.HashSet
;
19 import java
.util
.List
;
21 import java
.util
.Map
.Entry
;
24 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
25 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysis
;
26 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelAnalysisEventLayout
;
27 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelTrace
;
28 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.Activator
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
42 * This analysis module computes the CPU usage of a system from a kernel trace.
43 * It requires the LTTng Kernel analysis module to have accurate CPU usage data.
45 * @author Geneviève Bastien
47 public class KernelCpuUsageAnalysis
extends TmfStateSystemAnalysisModule
{
49 /** The ID of this analysis */
50 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.cpuusage"; //$NON-NLS-1$
52 /** Text used to identify 'total' entries in the returned maps */
53 public static final String TOTAL
= "total"; //$NON-NLS-1$
54 /** String used to separate elements in the returned maps */
55 public static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
56 /** Idle process thread ID */
57 public static final String TID_ZERO
= "0"; //$NON-NLS-1$
60 protected ITmfStateProvider
createStateProvider() {
61 ITmfTrace trace
= checkNotNull(getTrace());
62 IKernelAnalysisEventLayout layout
;
64 if (trace
instanceof IKernelTrace
) {
65 layout
= ((IKernelTrace
) trace
).getKernelEventLayout();
67 /* Fall-back to the base LttngEventLayout */
68 layout
= IKernelAnalysisEventLayout
.DEFAULT_LAYOUT
;
71 return new KernelCpuUsageStateProvider(trace
, layout
);
75 protected StateSystemBackendType
getBackendType() {
76 return StateSystemBackendType
.FULL
;
80 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
81 Set
<IAnalysisModule
> modules
= new HashSet
<>();
83 ITmfTrace trace
= getTrace();
85 throw new IllegalStateException();
88 * This analysis depends on the LTTng kernel analysis, so it's added to
91 Iterable
<KernelAnalysis
> kernelModules
= TmfTraceUtils
.getAnalysisModulesOfClass(trace
, KernelAnalysis
.class);
92 for (KernelAnalysis kernelModule
: kernelModules
) {
93 /* Only add the first one we find, if there is one */
94 modules
.add(kernelModule
);
101 * Get a map of time spent on CPU by various threads during a time range.
104 * Start time of requested range
106 * End time of requested range
107 * @return A map of TID -> time spent on CPU in the [start, end] interval
109 public Map
<String
, Long
> getCpuUsageInRange(long start
, long end
) {
110 Map
<String
, Long
> map
= new HashMap
<>();
111 Map
<String
, Long
> totalMap
= new HashMap
<>();
113 ITmfTrace trace
= getTrace();
114 ITmfStateSystem cpuSs
= getStateSystem();
115 if (trace
== null || cpuSs
== null) {
118 ITmfStateSystem kernelSs
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysis
.ID
);
119 if (kernelSs
== null) {
124 * Make sure the start/end times are within the state history, so we
125 * don't get TimeRange exceptions.
127 long startTime
= Math
.max(start
, cpuSs
.getStartTime());
128 startTime
= Math
.max(startTime
, kernelSs
.getStartTime());
129 long endTime
= Math
.min(end
, cpuSs
.getCurrentEndTime());
130 endTime
= Math
.min(endTime
, kernelSs
.getCurrentEndTime());
132 if (endTime
< startTime
) {
137 /* Get the list of quarks for each CPU and CPU's TIDs */
138 int cpusNode
= cpuSs
.getQuarkAbsolute(Attributes
.CPUS
);
139 Map
<Integer
, List
<Integer
>> tidsPerCpu
= new HashMap
<>();
140 for (int cpuNode
: cpuSs
.getSubAttributes(cpusNode
, false)) {
141 tidsPerCpu
.put(cpuNode
, cpuSs
.getSubAttributes(cpuNode
, false));
144 /* Query full states at start and end times */
145 List
<ITmfStateInterval
> kernelEndState
= kernelSs
.queryFullState(endTime
);
146 List
<ITmfStateInterval
> endState
= cpuSs
.queryFullState(endTime
);
147 List
<ITmfStateInterval
> kernelStartState
= kernelSs
.queryFullState(startTime
);
148 List
<ITmfStateInterval
> startState
= cpuSs
.queryFullState(startTime
);
150 long countAtStart
, countAtEnd
;
152 for (Entry
<Integer
, List
<Integer
>> entry
: tidsPerCpu
.entrySet()) {
153 int cpuNode
= entry
.getKey();
154 List
<Integer
> tidNodes
= entry
.getValue();
156 String curCpuName
= cpuSs
.getAttributeName(cpuNode
);
159 /* Get the quark of the thread running on this CPU */
160 int currentThreadQuark
= kernelSs
.getQuarkAbsolute(Attributes
.CPUS
, curCpuName
, Attributes
.CURRENT_THREAD
);
161 /* Get the currently running thread on this CPU */
162 int startThread
= kernelStartState
.get(currentThreadQuark
).getStateValue().unboxInt();
163 int endThread
= kernelEndState
.get(currentThreadQuark
).getStateValue().unboxInt();
165 for (int tidNode
: tidNodes
) {
166 String curTidName
= cpuSs
.getAttributeName(tidNode
);
167 int tid
= Integer
.parseInt(curTidName
);
169 countAtEnd
= endState
.get(tidNode
).getStateValue().unboxLong();
170 countAtStart
= startState
.get(tidNode
).getStateValue().unboxLong();
171 if (countAtStart
== -1) {
174 if (countAtEnd
== -1) {
179 * Interpolate start and end time of threads running at
182 if (tid
== startThread
|| startThread
== -1) {
183 long runningTime
= kernelStartState
.get(currentThreadQuark
).getEndTime() - kernelStartState
.get(currentThreadQuark
).getStartTime();
184 long runningEnd
= kernelStartState
.get(currentThreadQuark
).getEndTime();
186 countAtStart
= interpolateCount(countAtStart
, startTime
, runningEnd
, runningTime
);
188 if (tid
== endThread
) {
189 long runningTime
= kernelEndState
.get(currentThreadQuark
).getEndTime() - kernelEndState
.get(currentThreadQuark
).getStartTime();
190 long runningEnd
= kernelEndState
.get(currentThreadQuark
).getEndTime();
192 countAtEnd
= interpolateCount(countAtEnd
, endTime
, runningEnd
, runningTime
);
195 * If startThread is -1, we made the hypothesis that the
196 * process running at start was the current one. If the
197 * count is negative, we were wrong in this hypothesis. Also
198 * if the time at end is 0, it either means the process
199 * hasn't been on the CPU or that we still don't know who is
200 * running. In both cases, that invalidates the hypothesis.
202 if ((startThread
== -1) && ((countAtEnd
- countAtStart
< 0) || (countAtEnd
== 0))) {
206 long currentCount
= countAtEnd
- countAtStart
;
207 if (currentCount
< 0) {
208 Activator
.getDefault().logWarning(checkNotNull(String
.format("Negative count: start %d, end %d", countAtStart
, countAtEnd
))); //$NON-NLS-1$
210 } else if (currentCount
> endTime
- startTime
) {
211 Activator
.getDefault().logWarning(checkNotNull(String
.format("CPU Usage: Spent more time on CPU than allowed: %s spent %d when max should be %d", curTidName
, currentCount
, endTime
- startTime
))); //$NON-NLS-1$
214 cpuTotal
+= currentCount
;
215 map
.put(curCpuName
+ SPLIT_STRING
+ curTidName
, currentCount
);
216 addToMap(totalMap
, curTidName
, currentCount
);
217 totalTime
+= (currentCount
);
219 map
.put(curCpuName
, cpuTotal
);
222 /* Add the totals to the map */
223 for (Entry
<String
, Long
> entry
: totalMap
.entrySet()) {
224 map
.put(TOTAL
+ SPLIT_STRING
+ entry
.getKey(), entry
.getValue());
226 map
.put(TOTAL
, totalTime
);
228 } catch (TimeRangeException
| AttributeNotFoundException e
) {
230 * Assume there is no events or the attribute does not exist yet,
231 * nothing will be put in the map.
233 } catch (StateValueTypeException
| StateSystemDisposedException e
) {
235 * These other exception types would show a logic problem, so they
238 Activator
.getDefault().logError("Error getting CPU usage in a time range", e
); //$NON-NLS-1$
244 private static long interpolateCount(long count
, long ts
, long runningEnd
, long runningTime
) {
245 long newCount
= count
;
248 if (runningTime
> 0) {
250 long runningStart
= runningEnd
- runningTime
;
252 if (ts
< runningStart
) {
254 * This interval was not started, this can happen if the current
255 * running thread is unknown and we execute this method. It just
256 * means that this process was not the one running
260 newCount
+= (ts
- runningStart
);
266 * Add the value to the previous value in the map. If the key was not set,
269 private static void addToMap(Map
<String
, Long
> map
, String key
, Long value
) {
270 Long addTo
= map
.get(key
);
274 map
.put(key
, addTo
+ value
);