linux: make TID analysis handle no TID more gracefully - Bug 491275
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / analysis / os / linux / core / tid / ActiveTidStateProvider.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9
10 package org.eclipse.tracecompass.analysis.os.linux.core.tid;
11
12 import java.util.Map;
13 import java.util.TreeMap;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
17 import org.eclipse.tracecompass.common.core.NonNullUtils;
18 import org.eclipse.tracecompass.internal.analysis.os.linux.core.Activator;
19 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
20 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
21 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
22 import org.eclipse.tracecompass.statesystem.core.statevalue.TmfStateValue;
23 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
24 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
25 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
26 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
27 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
28 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
29
30 /**
31 * Active TID state provider, this only does one thing: figure out the active
32 * TID on any given CPU. This state provider is designed to do one thing and do
33 * it fast.
34 *
35 * Note: this state provider exists as a way to accelerate the TID aspect, but
36 * also to start splitting up the kernel analysis into smaller sections.
37 *
38 * Note 2: this is deliberately only package visible.
39 *
40 * @author Matthew Khouzam
41 */
42 class ActiveTidStateProvider extends AbstractTmfStateProvider {
43
44 private static final @NonNull String PROVIDER_ID = "activeTidAnalysis.provider"; //$NON-NLS-1$
45 private static final int VERSION = 0;
46
47 private final Map<Integer, Integer> fCpuNumToQuark = new TreeMap<>();
48 private final @NonNull String fSchedSwitch;
49 private final @NonNull String fNextTid;
50 private final @NonNull IKernelAnalysisEventLayout fLayout;
51
52 public ActiveTidStateProvider(@NonNull ITmfTrace trace, @NonNull IKernelAnalysisEventLayout layout) {
53 super(trace, PROVIDER_ID);
54 fSchedSwitch = layout.eventSchedSwitch();
55 fNextTid = layout.fieldNextTid();
56 fLayout = layout;
57 }
58
59 @Override
60 public int getVersion() {
61 return VERSION;
62 }
63
64 @Override
65 public @NonNull ITmfStateProvider getNewInstance() {
66 return new ActiveTidStateProvider(getTrace(), fLayout);
67 }
68
69 @Override
70 protected void eventHandle(@NonNull ITmfEvent event) {
71 Integer cpu = TmfTraceUtils.resolveIntEventAspectOfClassForEvent(event.getTrace(), TmfCpuAspect.class, event);
72 if (cpu == null) {
73 return;
74 }
75 ITmfStateSystemBuilder ssb = getStateSystemBuilder();
76 if (ssb == null) {
77 return;
78 }
79 Integer cpuQuark = fCpuNumToQuark.get(cpu);
80 if (cpuQuark == null) {
81 // this will only happen once
82 String cpuAttributeName = NonNullUtils.nullToEmptyString(cpu);
83 cpuQuark = ssb.getQuarkAbsoluteAndAdd(cpuAttributeName);
84 fCpuNumToQuark.put(cpu, cpuQuark);
85 }
86 if (!event.getName().equals(fSchedSwitch)) {
87 return;
88 }
89 try {
90 int nextTid = ((Long) event.getContent().getField(fNextTid).getValue()).intValue();
91 final TmfStateValue value = TmfStateValue.newValueInt(nextTid);
92 ssb.modifyAttribute(event.getTimestamp().toNanos(), value, cpuQuark);
93 } catch (StateValueTypeException | AttributeNotFoundException e) {
94 Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e);
95 }
96 }
97 }
This page took 0.034152 seconds and 5 git commands to generate.