1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 * Alexis Cabana-Loriaux - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.contextswitch
;
15 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.HashSet
;
20 import java
.util
.List
;
24 import org
.eclipse
.jdt
.annotation
.NonNull
;
25 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
26 import org
.eclipse
.jdt
.annotation
.Nullable
;
27 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernel
.Attributes
;
28 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernel
.KernelAnalysisModule
;
29 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelAnalysisEventLayout
;
30 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelTrace
;
31 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.Activator
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.analysis
.IAnalysisModule
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.ITmfStateProvider
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
45 * This analysis module computes the number of context switches of a system from
48 * @author Alexis Cabana-Loriaux
52 public class KernelContextSwitchAnalysis
extends TmfStateSystemAnalysisModule
{
54 /** The ID of this analysis */
55 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.contextswitch"; //$NON-NLS-1$
57 /** Integer used to identify 'total' entries in the returned maps */
58 public static final Integer TOTAL
= -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 KernelContextSwitchStateProvider(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 * Get a map of the number of context switch per CPU during a time range.
105 * Start time of requested range
107 * End time of requested range
108 * @return A map of CPU# -> nb of context switch in the [start, end]
109 * interval. CPU# == -1 represents the total number of context
111 * @throws TimeRangeException
112 * if one or more of the parameters is outside the range the
115 public @NonNullByDefault({}) @NonNull Map
<Integer
, Long
> getContextSwitchesRange(final long startParam
, final long endParam
) {
116 final @Nullable ITmfStateSystem stateSystem
= getStateSystem();
117 ITmfTrace trace
= getTrace();
118 if (trace
== null || stateSystem
== null) {
119 return Collections
.<Integer
, Long
> emptyMap();
121 long start
= Math
.max(startParam
, stateSystem
.getStartTime());
122 long end
= Math
.min(endParam
, stateSystem
.getCurrentEndTime());
123 ITmfStateSystem contextSwitchStateSystem
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelContextSwitchAnalysis
.ID
);
124 if (contextSwitchStateSystem
== null) {
125 return Collections
.<Integer
, Long
> emptyMap();
129 * Make sure the start/end times are within the state history, so we
130 * don't get TimeRange exceptions.
132 long startTime
= contextSwitchStateSystem
.getStartTime();
133 long endTime
= contextSwitchStateSystem
.getCurrentEndTime();
134 if (endTime
< startTime
) {
135 return Collections
.<Integer
, Long
> emptyMap();
138 Map
<Integer
, Long
> map
= new HashMap
<>();
140 /* Get the list of quarks for each CPU */
141 int cpusNode
= contextSwitchStateSystem
.getQuarkAbsolute(Attributes
.CPUS
);
142 List
<Integer
> cpuQuarks
= contextSwitchStateSystem
.getSubAttributes(cpusNode
, false);
143 /* Query full states at start and end times */
144 List
<ITmfStateInterval
> kernelEndState
= contextSwitchStateSystem
.queryFullState(end
);
145 List
<ITmfStateInterval
> kernelStartState
= contextSwitchStateSystem
.queryFullState(start
);
146 Long totalNbCxtSwt
= 0l;
147 for (Integer cpuQuark
: cpuQuarks
) {
148 int cpuNb
= Integer
.parseInt(contextSwitchStateSystem
.getAttributeName(cpuQuark
.intValue()));
149 Long nbCxtSwtForCore
= kernelEndState
.get(cpuQuark
).getStateValue().unboxLong() - kernelStartState
.get(cpuQuark
).getStateValue().unboxLong();
150 map
.put(cpuNb
, nbCxtSwtForCore
);
151 totalNbCxtSwt
+= nbCxtSwtForCore
;
154 /* Put the total number of context switches in the interval */
155 map
.put(TOTAL
, totalNbCxtSwt
);
156 } catch (TimeRangeException
| AttributeNotFoundException e
) {
158 * Assume there is no events or the attribute does not exist yet,
159 * nothing will be put in the map.
161 } catch (StateValueTypeException
| StateSystemDisposedException e
) {
163 * These other exception types would show a logic problem, so they
166 Activator
.getDefault().logError("Error getting CPU context switches in a time range", e
); //$NON-NLS-1$