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 java
.util
.HashMap
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
19 import java
.util
.Map
.Entry
;
22 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.Activator
;
23 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.Attributes
;
24 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.trace
.layout
.IKernelAnalysisEventLayout
;
25 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.trace
.layout
.LttngEventLayout
;
26 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.analysis
.kernel
.LttngKernelAnalysis
;
27 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.trace
.LttngKernelTrace
;
28 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
29 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
30 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
40 * This analysis module computes the CPU usage of a system from a kernel trace.
41 * It requires the LTTng Kernel analysis module to have accurate CPU usage data.
43 * @author Geneviève Bastien
46 public class LttngKernelCpuUsageAnalysis
extends TmfStateSystemAnalysisModule
{
48 /** The ID of this analysis */
49 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.core.cpuusage"; //$NON-NLS-1$
51 /** Text used to identify 'total' entries in the returned maps */
52 public static final String TOTAL
= "total"; //$NON-NLS-1$
53 /** String used to separate elements in the returned maps */
54 public static final String SPLIT_STRING
= "/"; //$NON-NLS-1$
55 /** Idle process thread ID */
56 public static final String TID_ZERO
= "0"; //$NON-NLS-1$
59 protected ITmfStateProvider
createStateProvider() {
60 ITmfTrace trace
= getTrace();
61 IKernelAnalysisEventLayout layout
;
63 if (trace
instanceof LttngKernelTrace
) {
64 layout
= ((LttngKernelTrace
) trace
).getEventLayout();
66 /* Fall-back to the base LttngEventLayout */
67 layout
= LttngEventLayout
.getInstance();
70 return new LttngKernelCpuUsageStateProvider(trace
, layout
);
74 protected StateSystemBackendType
getBackendType() {
75 return StateSystemBackendType
.FULL
;
79 protected Iterable
<IAnalysisModule
> getDependentAnalyses() {
80 Set
<IAnalysisModule
> modules
= new HashSet
<>();
82 ITmfTrace trace
= getTrace();
84 throw new IllegalStateException();
87 * This analysis depends on the LTTng kernel analysis, so it's added to
90 Iterable
<LttngKernelAnalysis
> kernelModules
= trace
.getAnalysisModulesOfClass(LttngKernelAnalysis
.class);
91 if (kernelModules
.iterator().hasNext()) {
92 modules
.add(kernelModules
.iterator().next());
98 * Get a map of time spent on CPU by various threads during a time range.
101 * Start time of requested range
103 * End time of requested range
104 * @return A map of TID -> time spent on CPU in the [start, end] interval
106 public Map
<String
, Long
> getCpuUsageInRange(long start
, long end
) {
107 Map
<String
, Long
> map
= new HashMap
<>();
108 Map
<String
, Long
> totalMap
= new HashMap
<>();
110 ITmfTrace trace
= getTrace();
111 ITmfStateSystem cpuSs
= getStateSystem();
112 if (trace
== null || cpuSs
== null) {
115 ITmfStateSystem kernelSs
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysis
.ID
);
116 if (kernelSs
== null) {
121 * Make sure the start/end times are within the state history, so we
122 * don't get TimeRange exceptions.
124 long startTime
= Math
.max(start
, cpuSs
.getStartTime());
125 startTime
= Math
.max(startTime
, kernelSs
.getStartTime());
126 long endTime
= Math
.min(end
, cpuSs
.getCurrentEndTime());
127 endTime
= Math
.min(endTime
, kernelSs
.getCurrentEndTime());
129 if (endTime
< startTime
) {
134 /* Get the list of quarks for each CPU and CPU's TIDs */
135 int cpusNode
= cpuSs
.getQuarkAbsolute(Attributes
.CPUS
);
136 Map
<Integer
, List
<Integer
>> tidsPerCpu
= new HashMap
<>();
137 for (int cpuNode
: cpuSs
.getSubAttributes(cpusNode
, false)) {
138 tidsPerCpu
.put(cpuNode
, cpuSs
.getSubAttributes(cpuNode
, false));
141 /* Query full states at start and end times */
142 List
<ITmfStateInterval
> kernelEndState
= kernelSs
.queryFullState(endTime
);
143 List
<ITmfStateInterval
> endState
= cpuSs
.queryFullState(endTime
);
144 List
<ITmfStateInterval
> kernelStartState
= kernelSs
.queryFullState(startTime
);
145 List
<ITmfStateInterval
> startState
= cpuSs
.queryFullState(startTime
);
147 long countAtStart
, countAtEnd
;
149 for (Entry
<Integer
, List
<Integer
>> entry
: tidsPerCpu
.entrySet()) {
150 int cpuNode
= entry
.getKey();
151 List
<Integer
> tidNodes
= entry
.getValue();
153 String curCpuName
= cpuSs
.getAttributeName(cpuNode
);
156 /* Get the quark of the thread running on this CPU */
157 int currentThreadQuark
= kernelSs
.getQuarkAbsolute(Attributes
.CPUS
, curCpuName
, Attributes
.CURRENT_THREAD
);
158 /* Get the currently running thread on this CPU */
159 int startThread
= kernelStartState
.get(currentThreadQuark
).getStateValue().unboxInt();
160 int endThread
= kernelEndState
.get(currentThreadQuark
).getStateValue().unboxInt();
162 for (int tidNode
: tidNodes
) {
163 String curTidName
= cpuSs
.getAttributeName(tidNode
);
164 int tid
= Integer
.parseInt(curTidName
);
166 countAtEnd
= endState
.get(tidNode
).getStateValue().unboxLong();
167 countAtStart
= startState
.get(tidNode
).getStateValue().unboxLong();
168 if (countAtStart
== -1) {
171 if (countAtEnd
== -1) {
176 * Interpolate start and end time of threads running at
179 if (tid
== startThread
|| startThread
== -1) {
180 long runningTime
= kernelStartState
.get(currentThreadQuark
).getEndTime() - kernelStartState
.get(currentThreadQuark
).getStartTime();
181 long runningEnd
= kernelStartState
.get(currentThreadQuark
).getEndTime();
183 countAtStart
= interpolateCount(countAtStart
, startTime
, runningEnd
, runningTime
);
185 if (tid
== endThread
) {
186 long runningTime
= kernelEndState
.get(currentThreadQuark
).getEndTime() - kernelEndState
.get(currentThreadQuark
).getStartTime();
187 long runningEnd
= kernelEndState
.get(currentThreadQuark
).getEndTime();
189 countAtEnd
= interpolateCount(countAtEnd
, endTime
, runningEnd
, runningTime
);
192 * If startThread is -1, we made the hypothesis that the
193 * process running at start was the current one. If the
194 * count is negative, we were wrong in this hypothesis. Also
195 * if the time at end is 0, it either means the process
196 * hasn't been on the CPU or that we still don't know who is
197 * running. In both cases, that invalidates the hypothesis.
199 if ((startThread
== -1) && ((countAtEnd
- countAtStart
< 0) || (countAtEnd
== 0))) {
203 long currentCount
= countAtEnd
- countAtStart
;
204 if (currentCount
< 0) {
205 Activator
.getDefault().logWarning(String
.format("Negative count: start %d, end %d", countAtStart
, countAtEnd
)); //$NON-NLS-1$
207 } else if (currentCount
> endTime
- startTime
) {
208 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$
211 cpuTotal
+= currentCount
;
212 map
.put(curCpuName
+ SPLIT_STRING
+ curTidName
, currentCount
);
213 addToMap(totalMap
, curTidName
, currentCount
);
214 totalTime
+= (currentCount
);
216 map
.put(curCpuName
, cpuTotal
);
219 /* Add the totals to the map */
220 for (Entry
<String
, Long
> entry
: totalMap
.entrySet()) {
221 map
.put(TOTAL
+ SPLIT_STRING
+ entry
.getKey(), entry
.getValue());
223 map
.put(TOTAL
, totalTime
);
225 } catch (TimeRangeException
| AttributeNotFoundException e
) {
227 * Assume there is no events or the attribute does not exist yet,
228 * nothing will be put in the map.
230 } catch (StateValueTypeException
| StateSystemDisposedException e
) {
232 * These other exception types would show a logic problem, so they
235 Activator
.getDefault().logError("Error getting CPU usage in a time range", e
); //$NON-NLS-1$
241 private static long interpolateCount(long count
, long ts
, long runningEnd
, long runningTime
) {
242 long newCount
= count
;
245 if (runningTime
> 0) {
247 long runningStart
= runningEnd
- runningTime
;
249 if (ts
< runningStart
) {
251 * This interval was not started, this can happen if the current
252 * running thread is unknown and we execute this method. It just
253 * means that this process was not the one running
257 newCount
+= (ts
- runningStart
);
263 * Add the value to the previous value in the map. If the key was not set,
266 private static void addToMap(Map
<String
, Long
> map
, String key
, Long value
) {
267 Long addTo
= map
.get(key
);
271 map
.put(key
, addTo
+ value
);