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
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
26 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysisModule
;
27 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelAnalysisEventLayout
;
28 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelTrace
;
29 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.Activator
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
43 * This analysis module computes the CPU usage of a system from a kernel trace.
44 * It requires the LTTng Kernel analysis module to have accurate CPU usage data.
46 * @author Geneviève Bastien
48 public class KernelCpuUsageAnalysis
extends TmfStateSystemAnalysisModule
{
50 /** The ID of this analysis */
51 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.cpuusage"; //$NON-NLS-1$
53 /** Text used to identify 'total' entries in the returned maps */
54 public static final String TOTAL
= "total"; //$NON-NLS-1$
55 /** String used to separate elements in the returned maps */
56 public static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
57 /** Idle process thread ID */
58 public static final String TID_ZERO
= "0"; //$NON-NLS-1$
61 protected ITmfStateProvider
createStateProvider() {
62 ITmfTrace trace
= checkNotNull(getTrace());
63 IKernelAnalysisEventLayout layout
;
65 if (trace
instanceof IKernelTrace
) {
66 layout
= ((IKernelTrace
) trace
).getKernelEventLayout();
68 /* Fall-back to the base LttngEventLayout */
69 layout
= IKernelAnalysisEventLayout
.DEFAULT_LAYOUT
;
72 return new KernelCpuUsageStateProvider(trace
, layout
);
76 protected StateSystemBackendType
getBackendType() {
77 return StateSystemBackendType
.FULL
;
81 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
82 Set
<IAnalysisModule
> modules
= new HashSet
<>();
84 ITmfTrace trace
= getTrace();
86 throw new IllegalStateException();
89 * This analysis depends on the LTTng kernel analysis, so it's added to
92 Iterable
<KernelAnalysisModule
> kernelModules
= TmfTraceUtils
.getAnalysisModulesOfClass(trace
, KernelAnalysisModule
.class);
93 for (KernelAnalysisModule kernelModule
: kernelModules
) {
94 /* Only add the first one we find, if there is one */
95 modules
.add(kernelModule
);
102 * Gets the maximum number of cores detected
104 * @return the number of cores
107 public int getNumberOfCores() {
109 ITmfStateSystem cpuSs
= getStateSystem();
112 int cpusNode
= cpuSs
.getQuarkAbsolute(Attributes
.CPUS
);
113 final @NonNull List
<@NonNull Integer
> subAttributes
= cpuSs
.getSubAttributes(cpusNode
, false);
114 int cpus
= Integer
.MIN_VALUE
;
115 for (Integer quark
: subAttributes
) {
116 cpus
= Math
.max(Integer
.parseInt(cpuSs
.getAttributeName(quark
)), cpus
);
118 return Math
.max(subAttributes
.size(), cpus
);
119 } catch (AttributeNotFoundException e
) {
120 Activator
.getDefault().logError(e
.getMessage(), e
);
128 * Get a map of time spent on CPU by various threads during a time range.
131 * A set of the desired CPUs to get. An empty set gets all the
134 * Start time of requested range
136 * End time of requested range
137 * @return A map of TID -> time spent on CPU in the [start, end] interval
140 public Map
<String
, Long
> getCpuUsageInRange(Set
<@NonNull Integer
> cpus
, long start
, long end
) {
141 Map
<String
, Long
> map
= new HashMap
<>();
142 Map
<String
, Long
> totalMap
= new HashMap
<>();
144 ITmfTrace trace
= getTrace();
145 ITmfStateSystem cpuSs
= getStateSystem();
146 if (trace
== null || cpuSs
== null) {
149 ITmfStateSystem kernelSs
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysisModule
.ID
);
150 if (kernelSs
== null) {
155 * Make sure the start/end times are within the state history, so we
156 * don't get TimeRange exceptions.
158 long startTime
= Math
.max(start
, cpuSs
.getStartTime());
159 startTime
= Math
.max(startTime
, kernelSs
.getStartTime());
160 long endTime
= Math
.min(end
, cpuSs
.getCurrentEndTime());
161 endTime
= Math
.min(endTime
, kernelSs
.getCurrentEndTime());
163 if (endTime
< startTime
) {
168 /* Get the list of quarks for each CPU and CPU's TIDs */
169 int cpusNode
= cpuSs
.getQuarkAbsolute(Attributes
.CPUS
);
170 Map
<Integer
, List
<Integer
>> tidsPerCpu
= new HashMap
<>();
171 for (int cpuNode
: cpuSs
.getSubAttributes(cpusNode
, false)) {
172 final @NonNull List
<@NonNull Integer
> cpuSubAttributes
= cpuSs
.getSubAttributes(cpuNode
, false);
173 if (cpus
.isEmpty() || cpus
.contains(Integer
.parseInt(cpuSs
.getAttributeName(cpuNode
)))) {
174 tidsPerCpu
.put(cpuNode
, cpuSubAttributes
);
178 /* Query full states at start and end times */
179 List
<ITmfStateInterval
> kernelEndState
= kernelSs
.queryFullState(endTime
);
180 List
<ITmfStateInterval
> endState
= cpuSs
.queryFullState(endTime
);
181 List
<ITmfStateInterval
> kernelStartState
= kernelSs
.queryFullState(startTime
);
182 List
<ITmfStateInterval
> startState
= cpuSs
.queryFullState(startTime
);
184 long countAtStart
, countAtEnd
;
186 for (Entry
<Integer
, List
<Integer
>> entry
: tidsPerCpu
.entrySet()) {
187 int cpuNode
= entry
.getKey();
188 List
<Integer
> tidNodes
= entry
.getValue();
190 String curCpuName
= cpuSs
.getAttributeName(cpuNode
);
193 /* Get the quark of the thread running on this CPU */
194 int currentThreadQuark
= kernelSs
.getQuarkAbsolute(Attributes
.CPUS
, curCpuName
, Attributes
.CURRENT_THREAD
);
195 /* Get the currently running thread on this CPU */
196 int startThread
= kernelStartState
.get(currentThreadQuark
).getStateValue().unboxInt();
197 int endThread
= kernelEndState
.get(currentThreadQuark
).getStateValue().unboxInt();
199 for (int tidNode
: tidNodes
) {
200 String curTidName
= cpuSs
.getAttributeName(tidNode
);
201 int tid
= Integer
.parseInt(curTidName
);
203 countAtEnd
= endState
.get(tidNode
).getStateValue().unboxLong();
204 countAtStart
= startState
.get(tidNode
).getStateValue().unboxLong();
205 if (countAtStart
== -1) {
208 if (countAtEnd
== -1) {
213 * Interpolate start and end time of threads running at
216 if (tid
== startThread
|| startThread
== -1) {
217 long runningTime
= kernelStartState
.get(currentThreadQuark
).getEndTime() - kernelStartState
.get(currentThreadQuark
).getStartTime();
218 long runningEnd
= kernelStartState
.get(currentThreadQuark
).getEndTime();
220 countAtStart
= interpolateCount(countAtStart
, startTime
, runningEnd
, runningTime
);
222 if (tid
== endThread
) {
223 long runningTime
= kernelEndState
.get(currentThreadQuark
).getEndTime() - kernelEndState
.get(currentThreadQuark
).getStartTime();
224 long runningEnd
= kernelEndState
.get(currentThreadQuark
).getEndTime();
226 countAtEnd
= interpolateCount(countAtEnd
, endTime
, runningEnd
, runningTime
);
229 * If startThread is -1, we made the hypothesis that the
230 * process running at start was the current one. If the
231 * count is negative, we were wrong in this hypothesis. Also
232 * if the time at end is 0, it either means the process
233 * hasn't been on the CPU or that we still don't know who is
234 * running. In both cases, that invalidates the hypothesis.
236 if ((startThread
== -1) && ((countAtEnd
- countAtStart
< 0) || (countAtEnd
== 0))) {
240 long currentCount
= countAtEnd
- countAtStart
;
241 if (currentCount
< 0) {
242 Activator
.getDefault().logWarning(String
.format("Negative count: start %d, end %d", countAtStart
, countAtEnd
)); //$NON-NLS-1$
244 } else if (currentCount
> endTime
- startTime
) {
245 Activator
.getDefault().logWarning(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$
248 cpuTotal
+= currentCount
;
249 map
.put(curCpuName
+ SPLIT_STRING
+ curTidName
, currentCount
);
250 addToMap(totalMap
, curTidName
, currentCount
);
251 totalTime
+= (currentCount
);
253 map
.put(curCpuName
, cpuTotal
);
256 /* Add the totals to the map */
257 for (Entry
<String
, Long
> entry
: totalMap
.entrySet()) {
258 map
.put(TOTAL
+ SPLIT_STRING
+ entry
.getKey(), entry
.getValue());
260 map
.put(TOTAL
, totalTime
);
262 } catch (TimeRangeException
| AttributeNotFoundException e
) {
264 * Assume there is no events or the attribute does not exist yet,
265 * nothing will be put in the map.
267 } catch (StateValueTypeException
| StateSystemDisposedException e
) {
269 * These other exception types would show a logic problem, so they
272 Activator
.getDefault().logError("Error getting CPU usage in a time range", e
); //$NON-NLS-1$
278 private static long interpolateCount(long count
, long ts
, long runningEnd
, long runningTime
) {
279 long newCount
= count
;
282 if (runningTime
> 0) {
284 long runningStart
= runningEnd
- runningTime
;
286 if (ts
< runningStart
) {
288 * This interval was not started, this can happen if the current
289 * running thread is unknown and we execute this method. It just
290 * means that this process was not the one running
294 newCount
+= (ts
- runningStart
);
300 * Add the value to the previous value in the map. If the key was not set,
303 private static void addToMap(Map
<String
, Long
> map
, String key
, Long value
) {
304 Long addTo
= map
.get(key
);
308 map
.put(key
, addTo
+ value
);