TMF: allow multiple analysis helpers to have the same ID
[deliverable/tracecompass.git] / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / analysis / os / linux / core / cpuusage / KernelCpuUsageAnalysis.java
1 /*******************************************************************************
2 * Copyright (c) 2014, 2015 École Polytechnique de Montréal
3 *
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
8 *
9 * Contributors:
10 * Geneviève Bastien - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.analysis.os.linux.core.cpuusage;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23
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;
40
41 /**
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.
44 *
45 * @author Geneviève Bastien
46 */
47 public class KernelCpuUsageAnalysis extends TmfStateSystemAnalysisModule {
48
49 /** The ID of this analysis */
50 public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.cpuusage"; //$NON-NLS-1$
51
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$
58
59 @Override
60 protected ITmfStateProvider createStateProvider() {
61 ITmfTrace trace = checkNotNull(getTrace());
62 IKernelAnalysisEventLayout layout;
63
64 if (trace instanceof IKernelTrace) {
65 layout = ((IKernelTrace) trace).getKernelEventLayout();
66 } else {
67 /* Fall-back to the base LttngEventLayout */
68 layout = IKernelAnalysisEventLayout.DEFAULT_LAYOUT;
69 }
70
71 return new KernelCpuUsageStateProvider(trace, layout);
72 }
73
74 @Override
75 protected StateSystemBackendType getBackendType() {
76 return StateSystemBackendType.FULL;
77 }
78
79 @Override
80 protected Iterable<IAnalysisModule> getDependentAnalyses() {
81 Set<IAnalysisModule> modules = new HashSet<>();
82
83 ITmfTrace trace = getTrace();
84 if (trace == null) {
85 throw new IllegalStateException();
86 }
87 /*
88 * This analysis depends on the LTTng kernel analysis, so it's added to
89 * dependent modules.
90 */
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);
95 break;
96 }
97 return modules;
98 }
99
100 /**
101 * Get a map of time spent on CPU by various threads during a time range.
102 *
103 * @param start
104 * Start time of requested range
105 * @param end
106 * End time of requested range
107 * @return A map of TID -> time spent on CPU in the [start, end] interval
108 */
109 public Map<String, Long> getCpuUsageInRange(long start, long end) {
110 Map<String, Long> map = new HashMap<>();
111 Map<String, Long> totalMap = new HashMap<>();
112
113 ITmfTrace trace = getTrace();
114 ITmfStateSystem cpuSs = getStateSystem();
115 if (trace == null || cpuSs == null) {
116 return map;
117 }
118 ITmfStateSystem kernelSs = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysis.ID);
119 if (kernelSs == null) {
120 return map;
121 }
122
123 /*
124 * Make sure the start/end times are within the state history, so we
125 * don't get TimeRange exceptions.
126 */
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());
131 long totalTime = 0;
132 if (endTime < startTime) {
133 return map;
134 }
135
136 try {
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));
142 }
143
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);
149
150 long countAtStart, countAtEnd;
151
152 for (Entry<Integer, List<Integer>> entry : tidsPerCpu.entrySet()) {
153 int cpuNode = entry.getKey();
154 List<Integer> tidNodes = entry.getValue();
155
156 String curCpuName = cpuSs.getAttributeName(cpuNode);
157 long cpuTotal = 0;
158
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();
164
165 for (int tidNode : tidNodes) {
166 String curTidName = cpuSs.getAttributeName(tidNode);
167 if (curTidName == null) {
168 continue;
169 }
170 int tid = Integer.parseInt(curTidName);
171
172 countAtEnd = endState.get(tidNode).getStateValue().unboxLong();
173 countAtStart = startState.get(tidNode).getStateValue().unboxLong();
174 if (countAtStart == -1) {
175 countAtStart = 0;
176 }
177 if (countAtEnd == -1) {
178 countAtEnd = 0;
179 }
180
181 /*
182 * Interpolate start and end time of threads running at
183 * those times
184 */
185 if (tid == startThread || startThread == -1) {
186 long runningTime = kernelStartState.get(currentThreadQuark).getEndTime() - kernelStartState.get(currentThreadQuark).getStartTime();
187 long runningEnd = kernelStartState.get(currentThreadQuark).getEndTime();
188
189 countAtStart = interpolateCount(countAtStart, startTime, runningEnd, runningTime);
190 }
191 if (tid == endThread) {
192 long runningTime = kernelEndState.get(currentThreadQuark).getEndTime() - kernelEndState.get(currentThreadQuark).getStartTime();
193 long runningEnd = kernelEndState.get(currentThreadQuark).getEndTime();
194
195 countAtEnd = interpolateCount(countAtEnd, endTime, runningEnd, runningTime);
196 }
197 /*
198 * If startThread is -1, we made the hypothesis that the
199 * process running at start was the current one. If the
200 * count is negative, we were wrong in this hypothesis. Also
201 * if the time at end is 0, it either means the process
202 * hasn't been on the CPU or that we still don't know who is
203 * running. In both cases, that invalidates the hypothesis.
204 */
205 if ((startThread == -1) && ((countAtEnd - countAtStart < 0) || (countAtEnd == 0))) {
206 countAtStart = 0;
207 }
208
209 long currentCount = countAtEnd - countAtStart;
210 if (currentCount < 0) {
211 Activator.getDefault().logWarning(checkNotNull(String.format("Negative count: start %d, end %d", countAtStart, countAtEnd))); //$NON-NLS-1$
212 currentCount = 0;
213 } else if (currentCount > endTime - startTime) {
214 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$
215 currentCount = 0;
216 }
217 cpuTotal += currentCount;
218 map.put(curCpuName + SPLIT_STRING + curTidName, currentCount);
219 addToMap(totalMap, curTidName, currentCount);
220 totalTime += (currentCount);
221 }
222 map.put(curCpuName, cpuTotal);
223 }
224
225 /* Add the totals to the map */
226 for (Entry<String, Long> entry : totalMap.entrySet()) {
227 map.put(TOTAL + SPLIT_STRING + entry.getKey(), entry.getValue());
228 }
229 map.put(TOTAL, totalTime);
230
231 } catch (TimeRangeException | AttributeNotFoundException e) {
232 /*
233 * Assume there is no events or the attribute does not exist yet,
234 * nothing will be put in the map.
235 */
236 } catch (StateValueTypeException | StateSystemDisposedException e) {
237 /*
238 * These other exception types would show a logic problem, so they
239 * should not happen.
240 */
241 Activator.getDefault().logError("Error getting CPU usage in a time range", e); //$NON-NLS-1$
242 }
243
244 return map;
245 }
246
247 private static long interpolateCount(long count, long ts, long runningEnd, long runningTime) {
248 long newCount = count;
249
250 /* sanity check */
251 if (runningTime > 0) {
252
253 long runningStart = runningEnd - runningTime;
254
255 if (ts < runningStart) {
256 /*
257 * This interval was not started, this can happen if the current
258 * running thread is unknown and we execute this method. It just
259 * means that this process was not the one running
260 */
261 return newCount;
262 }
263 newCount += (ts - runningStart);
264 }
265 return newCount;
266 }
267
268 /*
269 * Add the value to the previous value in the map. If the key was not set,
270 * assume 0
271 */
272 private static void addToMap(Map<String, Long> map, String key, Long value) {
273 Long addTo = map.get(key);
274 if (addTo == null) {
275 map.put(key, value);
276 } else {
277 map.put(key, addTo + value);
278 }
279 }
280
281 }
This page took 0.037654 seconds and 6 git commands to generate.