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
.lttng2
.kernel
.core
.analysis
.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
.internal
.lttng2
.kernel
.core
.Activator
;
25 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.Attributes
;
26 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.trace
.layout
.IKernelAnalysisEventLayout
;
27 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.trace
.layout
.LttngEventLayout
;
28 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.analysis
.kernel
.LttngKernelAnalysis
;
29 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.trace
.LttngKernelTrace
;
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
49 public class LttngKernelCpuUsageAnalysis
extends TmfStateSystemAnalysisModule
{
51 /** The ID of this analysis */
52 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.core.cpuusage"; //$NON-NLS-1$
54 /** Text used to identify 'total' entries in the returned maps */
55 public static final String TOTAL
= "total"; //$NON-NLS-1$
56 /** String used to separate elements in the returned maps */
57 public static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
58 /** Idle process thread ID */
59 public static final String TID_ZERO
= "0"; //$NON-NLS-1$
62 protected ITmfStateProvider
createStateProvider() {
63 ITmfTrace trace
= checkNotNull(getTrace());
64 IKernelAnalysisEventLayout layout
;
66 if (trace
instanceof LttngKernelTrace
) {
67 layout
= ((LttngKernelTrace
) trace
).getEventLayout();
69 /* Fall-back to the base LttngEventLayout */
70 layout
= LttngEventLayout
.getInstance();
73 return new LttngKernelCpuUsageStateProvider(trace
, layout
);
77 protected StateSystemBackendType
getBackendType() {
78 return StateSystemBackendType
.FULL
;
82 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
83 Set
<IAnalysisModule
> modules
= new HashSet
<>();
85 ITmfTrace trace
= getTrace();
87 throw new IllegalStateException();
90 * This analysis depends on the LTTng kernel analysis, so it's added to
93 Iterable
<LttngKernelAnalysis
> kernelModules
= TmfTraceUtils
.getAnalysisModulesOfClass(trace
, LttngKernelAnalysis
.class);
94 for (LttngKernelAnalysis kernelModule
: kernelModules
) {
95 /* Only add the first one we find, if there is one */
96 modules
.add(kernelModule
);
103 * Get a map of time spent on CPU by various threads during a time range.
106 * Start time of requested range
108 * End time of requested range
109 * @return A map of TID -> time spent on CPU in the [start, end] interval
111 public Map
<String
, Long
> getCpuUsageInRange(long start
, long end
) {
112 Map
<String
, Long
> map
= new HashMap
<>();
113 Map
<String
, Long
> totalMap
= new HashMap
<>();
115 ITmfTrace trace
= getTrace();
116 ITmfStateSystem cpuSs
= getStateSystem();
117 if (trace
== null || cpuSs
== null) {
120 ITmfStateSystem kernelSs
= TmfStateSystemAnalysisModule
.getStateSystemByModuleClass(trace
, LttngKernelAnalysis
.class);
121 if (kernelSs
== null) {
126 * Make sure the start/end times are within the state history, so we
127 * don't get TimeRange exceptions.
129 long startTime
= Math
.max(start
, cpuSs
.getStartTime());
130 startTime
= Math
.max(startTime
, kernelSs
.getStartTime());
131 long endTime
= Math
.min(end
, cpuSs
.getCurrentEndTime());
132 endTime
= Math
.min(endTime
, kernelSs
.getCurrentEndTime());
134 if (endTime
< startTime
) {
139 /* Get the list of quarks for each CPU and CPU's TIDs */
140 int cpusNode
= cpuSs
.getQuarkAbsolute(Attributes
.CPUS
);
141 Map
<Integer
, List
<Integer
>> tidsPerCpu
= new HashMap
<>();
142 for (int cpuNode
: cpuSs
.getSubAttributes(cpusNode
, false)) {
143 tidsPerCpu
.put(cpuNode
, cpuSs
.getSubAttributes(cpuNode
, false));
146 /* Query full states at start and end times */
147 List
<ITmfStateInterval
> kernelEndState
= kernelSs
.queryFullState(endTime
);
148 List
<ITmfStateInterval
> endState
= cpuSs
.queryFullState(endTime
);
149 List
<ITmfStateInterval
> kernelStartState
= kernelSs
.queryFullState(startTime
);
150 List
<ITmfStateInterval
> startState
= cpuSs
.queryFullState(startTime
);
152 long countAtStart
, countAtEnd
;
154 for (Entry
<Integer
, List
<Integer
>> entry
: tidsPerCpu
.entrySet()) {
155 int cpuNode
= entry
.getKey();
156 List
<Integer
> tidNodes
= entry
.getValue();
158 String curCpuName
= cpuSs
.getAttributeName(cpuNode
);
161 /* Get the quark of the thread running on this CPU */
162 int currentThreadQuark
= kernelSs
.getQuarkAbsolute(Attributes
.CPUS
, curCpuName
, Attributes
.CURRENT_THREAD
);
163 /* Get the currently running thread on this CPU */
164 int startThread
= kernelStartState
.get(currentThreadQuark
).getStateValue().unboxInt();
165 int endThread
= kernelEndState
.get(currentThreadQuark
).getStateValue().unboxInt();
167 for (int tidNode
: tidNodes
) {
168 String curTidName
= cpuSs
.getAttributeName(tidNode
);
169 int tid
= Integer
.parseInt(curTidName
);
171 countAtEnd
= endState
.get(tidNode
).getStateValue().unboxLong();
172 countAtStart
= startState
.get(tidNode
).getStateValue().unboxLong();
173 if (countAtStart
== -1) {
176 if (countAtEnd
== -1) {
181 * Interpolate start and end time of threads running at
184 if (tid
== startThread
|| startThread
== -1) {
185 long runningTime
= kernelStartState
.get(currentThreadQuark
).getEndTime() - kernelStartState
.get(currentThreadQuark
).getStartTime();
186 long runningEnd
= kernelStartState
.get(currentThreadQuark
).getEndTime();
188 countAtStart
= interpolateCount(countAtStart
, startTime
, runningEnd
, runningTime
);
190 if (tid
== endThread
) {
191 long runningTime
= kernelEndState
.get(currentThreadQuark
).getEndTime() - kernelEndState
.get(currentThreadQuark
).getStartTime();
192 long runningEnd
= kernelEndState
.get(currentThreadQuark
).getEndTime();
194 countAtEnd
= interpolateCount(countAtEnd
, endTime
, runningEnd
, runningTime
);
197 * If startThread is -1, we made the hypothesis that the
198 * process running at start was the current one. If the
199 * count is negative, we were wrong in this hypothesis. Also
200 * if the time at end is 0, it either means the process
201 * hasn't been on the CPU or that we still don't know who is
202 * running. In both cases, that invalidates the hypothesis.
204 if ((startThread
== -1) && ((countAtEnd
- countAtStart
< 0) || (countAtEnd
== 0))) {
208 long currentCount
= countAtEnd
- countAtStart
;
209 if (currentCount
< 0) {
210 Activator
.getDefault().logWarning(String
.format("Negative count: start %d, end %d", countAtStart
, countAtEnd
)); //$NON-NLS-1$
212 } else if (currentCount
> endTime
- startTime
) {
213 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$
216 cpuTotal
+= currentCount
;
217 map
.put(curCpuName
+ SPLIT_STRING
+ curTidName
, currentCount
);
218 addToMap(totalMap
, curTidName
, currentCount
);
219 totalTime
+= (currentCount
);
221 map
.put(curCpuName
, cpuTotal
);
224 /* Add the totals to the map */
225 for (Entry
<String
, Long
> entry
: totalMap
.entrySet()) {
226 map
.put(TOTAL
+ SPLIT_STRING
+ entry
.getKey(), entry
.getValue());
228 map
.put(TOTAL
, totalTime
);
230 } catch (TimeRangeException
| AttributeNotFoundException e
) {
232 * Assume there is no events or the attribute does not exist yet,
233 * nothing will be put in the map.
235 } catch (StateValueTypeException
| StateSystemDisposedException e
) {
237 * These other exception types would show a logic problem, so they
240 Activator
.getDefault().logError("Error getting CPU usage in a time range", e
); //$NON-NLS-1$
246 private static long interpolateCount(long count
, long ts
, long runningEnd
, long runningTime
) {
247 long newCount
= count
;
250 if (runningTime
> 0) {
252 long runningStart
= runningEnd
- runningTime
;
254 if (ts
< runningStart
) {
256 * This interval was not started, this can happen if the current
257 * running thread is unknown and we execute this method. It just
258 * means that this process was not the one running
262 newCount
+= (ts
- runningStart
);
268 * Add the value to the previous value in the map. If the key was not set,
271 private static void addToMap(Map
<String
, Long
> map
, String key
, Long value
) {
272 Long addTo
= map
.get(key
);
276 map
.put(key
, addTo
+ value
);