tmf: Null-annotate state system API classes
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.core / src / org / eclipse / tracecompass / internal / lttng2 / kernel / core / analysis / vm / module / VirtualMachineStateProvider.java
CommitLineData
4a74f111
MG
1/*******************************************************************************
2 * Copyright (c) 2014 É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 * Mohamad Gebai - Initial API and implementation
11 *******************************************************************************/
12
13package org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.module;
14
d0c7e4ba
AM
15import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
4a74f111
MG
17import java.util.HashMap;
18import java.util.Map;
19
20import org.eclipse.jdt.annotation.Nullable;
21import org.eclipse.tracecompass.common.core.NonNullUtils;
22import org.eclipse.tracecompass.internal.lttng2.kernel.core.Activator;
23import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.VcpuStateValues;
24import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.VmAttributes;
25import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.model.HostThread;
26import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.model.IVirtualMachineModel;
27import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.model.VirtualCPU;
28import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.model.VirtualMachine;
29import org.eclipse.tracecompass.internal.lttng2.kernel.core.analysis.vm.model.qemukvm.QemuKvmVmModel;
30import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.IKernelAnalysisEventLayout;
31import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.LttngEventLayout;
32import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelAnalysis;
33import org.eclipse.tracecompass.lttng2.kernel.core.analysis.kernel.LttngKernelThreadInformationProvider;
34import org.eclipse.tracecompass.lttng2.kernel.core.trace.LttngKernelTrace;
d0c7e4ba 35import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
4a74f111
MG
36import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
37import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
38import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
39import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
40import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
41import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
42import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
43import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
44import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
45import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
46import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
47import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
48import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperimentUtils;
49
50import com.google.common.collect.HashBasedTable;
51import com.google.common.collect.Table;
52
53/**
54 * This is the state provider which translates the virtual machine experiment
55 * events to a state system.
56 *
57 * Attribute tree:
58 *
59 * <pre>
60 * |- Virtual Machines
61 * | |- <Guest Host ID> -> Friendly name (trace name)
62 * | | |- <VCPU number>
63 * | | | |- Status -> <Status value>
64 * </pre>
65 *
66 * The status value of the VCPUs are either {@link VcpuStateValues#VCPU_IDLE},
67 * {@link VcpuStateValues#VCPU_UNKNOWN} or {@link VcpuStateValues#VCPU_RUNNING}.
68 * Those three values are ORed with flags {@link VcpuStateValues#VCPU_VMM}
69 * and/or {@link VcpuStateValues#VCPU_PREEMPT} to indicate respectively whether
70 * they are in hypervisor mode or preempted on the host.
71 *
72 * @author Mohamad Gebai
73 */
74public class VirtualMachineStateProvider extends AbstractTmfStateProvider {
75
76 /**
77 * Version number of this state provider. Please bump this if you modify the
78 * contents of the generated state history in some way.
79 */
80 private static final int VERSION = 1;
81
82 private static final int SCHED_SWITCH_INDEX = 0;
83
84 /* TODO: An analysis should support many hypervisor models */
85 private IVirtualMachineModel fModel;
86 private final Table<ITmfTrace, String, Integer> fEventNames;
87 private final Map<ITmfTrace, IKernelAnalysisEventLayout> fLayouts;
88
89 // ------------------------------------------------------------------------
90 // Constructor
91 // ------------------------------------------------------------------------
92
93 /**
94 * Constructor
95 *
96 * @param experiment
97 * The virtual machine experiment
98 */
99 public VirtualMachineStateProvider(TmfExperiment experiment) {
100 super(experiment, ITmfEvent.class, "Virtual Machine State Provider"); //$NON-NLS-1$
101
102 fModel = new QemuKvmVmModel(experiment);
103 Table<ITmfTrace, String, Integer> table = NonNullUtils.checkNotNull(HashBasedTable.<ITmfTrace, String, Integer> create());
104 fEventNames = table;
105 fLayouts = new HashMap<>();
106 }
107
108 // ------------------------------------------------------------------------
109 // Event names management
110 // ------------------------------------------------------------------------
111
112 private void buildEventNames(ITmfTrace trace) {
113 IKernelAnalysisEventLayout layout;
114 if (trace instanceof LttngKernelTrace) {
115 layout = ((LttngKernelTrace) trace).getEventLayout();
116 } else {
117 /* Fall-back to the base LttngEventLayout */
118 layout = LttngEventLayout.getInstance();
119 }
120 fLayouts.put(trace, layout);
121 fEventNames.put(trace, layout.eventSchedSwitch(), SCHED_SWITCH_INDEX);
122 }
123
124 // ------------------------------------------------------------------------
125 // IStateChangeInput
126 // ------------------------------------------------------------------------
127
128 @Override
129 public TmfExperiment getTrace() {
130 ITmfTrace trace = super.getTrace();
131 if (trace instanceof TmfExperiment) {
132 return (TmfExperiment) trace;
133 }
134 throw new IllegalStateException("VirtualMachineStateProvider: The associated trace should be an experiment"); //$NON-NLS-1$
135 }
136
137 @Override
138 public int getVersion() {
139 return VERSION;
140 }
141
142 @Override
143 public VirtualMachineStateProvider getNewInstance() {
144 TmfExperiment trace = getTrace();
145 return new VirtualMachineStateProvider(trace);
146 }
147
148 @Override
149 protected void eventHandle(@Nullable ITmfEvent event) {
150 if (event == null) {
151 return;
152 }
153
154 /* Is the event managed by this analysis */
155 final String eventName = event.getType().getName();
156
157 /* TODO When requirements work again, don't hardcode this */
158 if (!eventName.equals("sched_switch") && //$NON-NLS-1$
159 !fModel.getRequiredEvents().contains(eventName)) {
160 return;
161 }
162
d0c7e4ba 163 ITmfStateSystemBuilder ss = checkNotNull(getStateSystemBuilder());
4a74f111
MG
164 ITmfStateValue value;
165
166 final ITmfEventField content = event.getContent();
167 final long ts = event.getTimestamp().getValue();
168 final String hostId = event.getTrace().getHostId();
169 try {
170 /* Do we know this trace's role yet? */
171 VirtualMachine host = fModel.getCurrentMachine(event);
172 if (host == null) {
173 return;
174 }
175
176 /* Make sure guest traces are added to the state system */
177 if (host.isGuest()) {
178 /*
179 * If event from a guest OS, make sure the guest exists in the
180 * state system
181 */
182 int vmQuark = -1;
183 try {
184 vmQuark = ss.getQuarkRelative(getNodeVirtualMachines(), host.getHostId());
185 } catch (AttributeNotFoundException e) {
186 /*
187 * We should enter this catch only once per machine, so it
188 * is not so costly to do compared with adding the trace's
189 * name for each guest event
190 */
191 vmQuark = ss.getQuarkRelativeAndAdd(getNodeVirtualMachines(), host.getHostId());
192 TmfStateValue machineName = TmfStateValue.newValueString(event.getTrace().getName());
193 ss.modifyAttribute(ts, machineName, vmQuark);
194 }
195 }
196
197 /* Have the hypervisor models handle the event first */
198 fModel.handleEvent(event);
199
200 /* Handle the event here */
201 if (!fEventNames.containsRow(event.getTrace())) {
202 buildEventNames(event.getTrace());
203 }
204 Integer idx = fEventNames.get(event.getTrace(), eventName);
205 int intval = (idx == null ? -1 : idx.intValue());
206 switch (intval) {
207 case SCHED_SWITCH_INDEX: // "sched_switch":
208 /*
209 * Fields: string prev_comm, int32 prev_tid, int32 prev_prio, int64
210 * prev_state, string next_comm, int32 next_tid, int32 next_prio
211 */
212 {
213 Integer prevTid = ((Long) content.getField(fLayouts.get(event.getTrace()).fieldPrevTid()).getValue()).intValue();
214 Integer nextTid = ((Long) content.getField(fLayouts.get(event.getTrace()).fieldNextTid()).getValue()).intValue();
215
216 if (prevTid == null || nextTid == null) {
217 break;
218 }
219
220 if (host.isGuest()) {
221 /* Get the event's CPU */
222 Integer cpu = null;
223 Iterable<TmfCpuAspect> aspects = TmfTraceUtils.getEventAspectsOfClass(event.getTrace(), TmfCpuAspect.class);
224 for (TmfCpuAspect aspect : aspects) {
225 if (!aspect.resolve(event).equals(TmfCpuAspect.CPU_UNAVAILABLE)) {
226 cpu = aspect.resolve(event);
227 break;
228 }
229 }
230 if (cpu == null) {
231 /*
232 * We couldn't find any CPU information, ignore this
233 * event
234 */
235 break;
236 }
237
238 /*
239 * If sched switch is from a guest, just update the status
240 * of the virtual CPU to either idle or running
241 */
242 int curStatusQuark = ss.getQuarkRelativeAndAdd(getNodeVirtualMachines(), host.getHostId(),
243 cpu.toString(), VmAttributes.STATUS);
244 value = TmfStateValue.newValueInt(VcpuStateValues.VCPU_IDLE);
245 if (nextTid > 0) {
246 value = TmfStateValue.newValueInt(VcpuStateValues.VCPU_RUNNING);
247 }
248 ss.modifyAttribute(ts, value, curStatusQuark);
249 break;
250 }
251
252 /* Event is not from a guest */
253 /* Verify if the previous thread corresponds to a virtual CPU */
254 HostThread ht = new HostThread(hostId, prevTid);
255 VirtualCPU vcpu = fModel.getVirtualCpu(ht);
256
257 /*
258 * If previous thread is virtual CPU, update status of the
259 * virtual CPU to preempted
260 */
261 if (vcpu != null) {
262 VirtualMachine vm = vcpu.getVm();
263
264 int curStatusQuark = ss.getQuarkRelativeAndAdd(getNodeVirtualMachines(), vm.getHostId(),
265 vcpu.getCpuId().toString(), VmAttributes.STATUS);
266
267 /* Add the preempted flag to the status */
268 value = ss.queryOngoingState(curStatusQuark);
269 int newVal = Math.max(VcpuStateValues.VCPU_UNKNOWN, value.unboxInt());
270 value = TmfStateValue.newValueInt(newVal | VcpuStateValues.VCPU_PREEMPT);
271 ss.modifyAttribute(ts, value, curStatusQuark);
272 }
273
274 /* Verify if the next thread corresponds to a virtual CPU */
275 ht = new HostThread(hostId, nextTid);
276 vcpu = fModel.getVirtualCpu(ht);
277
278 /*
279 * If next thread is virtual CPU, update status of the virtual
280 * CPU the previous status
281 */
282 if (vcpu != null) {
283 VirtualMachine vm = vcpu.getVm();
284 int curStatusQuark = ss.getQuarkRelativeAndAdd(getNodeVirtualMachines(), vm.getHostId(),
285 vcpu.getCpuId().toString(), VmAttributes.STATUS);
286
287 /* Remove the preempted flag from the status */
288 value = ss.queryOngoingState(curStatusQuark);
289 int newVal = Math.max(VcpuStateValues.VCPU_UNKNOWN, value.unboxInt());
290 value = TmfStateValue.newValueInt(newVal & ~VcpuStateValues.VCPU_PREEMPT);
291 ss.modifyAttribute(ts, value, curStatusQuark);
292
293 }
294
295 }
296 break;
297
298 default:
299 /* Other events not covered by the main switch */
300 {
301 HostThread ht = getCurrentHostThread(event, ts);
302 if (ht == null) {
303 break;
304 }
305
306 /*
307 * Are we entering the hypervisor mode and if so, which virtual
308 * CPU is concerned?
309 */
310 VirtualCPU virtualCpu = fModel.getVCpuEnteringHypervisorMode(event, ht);
311 if (virtualCpu != null) {
312 /* Add the hypervisor flag to the status */
313 VirtualMachine vm = virtualCpu.getVm();
314 int curStatusQuark = ss.getQuarkRelativeAndAdd(getNodeVirtualMachines(), vm.getHostId(),
315 Long.toString(virtualCpu.getCpuId()), VmAttributes.STATUS);
316 value = ss.queryOngoingState(curStatusQuark);
317 int newVal = Math.max(VcpuStateValues.VCPU_UNKNOWN, value.unboxInt());
318 value = TmfStateValue.newValueInt(newVal | VcpuStateValues.VCPU_VMM);
319 ss.modifyAttribute(ts, value, curStatusQuark);
320 }
321
322 /*
323 * Are we exiting the hypervisor mode and if so, which virtual
324 * CPU is concerned?
325 */
326 virtualCpu = fModel.getVCpuExitingHypervisorMode(event, ht);
327 if (virtualCpu != null) {
328 /* Remove the hypervisor flag from the status */
329 VirtualMachine vm = virtualCpu.getVm();
330 int curStatusQuark = ss.getQuarkRelativeAndAdd(getNodeVirtualMachines(), vm.getHostId(),
331 Long.toString(virtualCpu.getCpuId()), VmAttributes.STATUS);
332 value = ss.queryOngoingState(curStatusQuark);
333 int newVal = Math.max(VcpuStateValues.VCPU_UNKNOWN, value.unboxInt());
334 value = TmfStateValue.newValueInt(newVal & ~VcpuStateValues.VCPU_VMM);
335 ss.modifyAttribute(ts, value, curStatusQuark);
336 }
337
338 }
339 break;
340 }
341
342 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
343 Activator.getDefault().logError("Error handling event in VirtualMachineStateProvider", e); //$NON-NLS-1$
344 }
345 }
346
347 // ------------------------------------------------------------------------
348 // Convenience methods for commonly-used attribute tree locations
349 // ------------------------------------------------------------------------
350
351 private int getNodeVirtualMachines() {
d0c7e4ba 352 return checkNotNull(getStateSystemBuilder()).getQuarkAbsoluteAndAdd(VmAttributes.VIRTUAL_MACHINES);
4a74f111
MG
353 }
354
355 private @Nullable HostThread getCurrentHostThread(ITmfEvent event, long ts) {
356 /* Get the LTTng kernel analysis for the host */
357 String hostId = event.getTrace().getHostId();
358 LttngKernelAnalysis module = TmfExperimentUtils.getAnalysisModuleOfClassForHost(getTrace(), hostId, LttngKernelAnalysis.class);
359 if (module == null) {
360 return null;
361 }
362
363 /* Get the CPU the event is running on */
364 Integer cpu = null;
365 Iterable<TmfCpuAspect> aspects = TmfTraceUtils.getEventAspectsOfClass(event.getTrace(), TmfCpuAspect.class);
366 for (TmfCpuAspect aspect : aspects) {
367 if (!aspect.resolve(event).equals(TmfCpuAspect.CPU_UNAVAILABLE)) {
368 cpu = aspect.resolve(event);
369 break;
370 }
371 }
372 if (cpu == null) {
373 /* We couldn't find any CPU information, ignore this event */
374 return null;
375 }
376
377 Integer currentTid = LttngKernelThreadInformationProvider.getThreadOnCpu(module, cpu, ts);
378 if (currentTid == null) {
379 return null;
380 }
381 return new HostThread(hostId, currentTid);
382 }
383
384}
This page took 0.039723 seconds and 5 git commands to generate.