58c1fdcc8ae100a06a71cfe41b0da8cbc91173b7
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.core / src / org / eclipse / tracecompass / analysis / os / linux / core / tid / TidAnalysisModule.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 static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
13
14 import java.util.Collections;
15 import java.util.Set;
16
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.eclipse.jdt.annotation.Nullable;
19 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
20 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
21 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelTrace;
22 import org.eclipse.tracecompass.common.core.NonNullUtils;
23 import org.eclipse.tracecompass.internal.analysis.os.linux.core.Activator;
24 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
25 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
26 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
27 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
28 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
29 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue.Type;
30 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement;
31 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
32 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
33 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
34
35 /**
36 * Active tid analysis module, this only does one thing: figure out the active
37 * tid on any given cpu. This analysis should be approx 10x faster than the full
38 * {@link KernelAnalysisModule}.
39 *
40 * Note: this module exists as a way to accelerate the TID aspect, but also to
41 * start splitting up the kernel analysis into smaller sections.
42 *
43 * @author Matthew Khouzam
44 * @since 2.0
45 */
46 public class TidAnalysisModule extends TmfStateSystemAnalysisModule {
47
48 /** The ID of this analysis module */
49 public static final @NonNull String ID = "org.eclipse.tracecompass.analysis.os.linux.kernel.tid"; //$NON-NLS-1$
50
51 /** The requirements as an immutable set */
52 private static final @NonNull Set<@NonNull TmfAnalysisRequirement> REQUIREMENTS = Collections.EMPTY_SET;
53
54 @Override
55 public @NonNull Iterable<@NonNull TmfAnalysisRequirement> getAnalysisRequirements() {
56 return REQUIREMENTS;
57 }
58
59 @Override
60 protected @NonNull ITmfStateProvider createStateProvider() {
61 ITmfTrace trace = checkNotNull(getTrace());
62 IKernelAnalysisEventLayout layout = (trace instanceof IKernelTrace) ? ((IKernelTrace) trace).getKernelEventLayout() : IKernelAnalysisEventLayout.DEFAULT_LAYOUT;
63 return new ActiveTidStateProvider(trace, layout);
64 }
65
66 /**
67 * Gets the current thread ID on a given CPU for a given time
68 *
69 * @param cpu
70 * the CPU
71 * @param time
72 * the time in nanoseconds
73 * @return the current TID at the time on the CPU or {@code null} if not
74 * known
75 */
76 public @Nullable Integer getThreadOnCpuAtTime(int cpu, long time) {
77 ITmfStateSystem stateSystem = getStateSystem();
78 if (stateSystem == null) {
79 return null;
80 }
81
82 Integer tid = null;
83 try {
84 int cpuQuark = stateSystem.getQuarkAbsolute(Integer.toString(cpu));
85 ITmfStateValue value = stateSystem.querySingleState(time, cpuQuark).getStateValue();
86 if (value.getType().equals(Type.INTEGER)) {
87 tid = value.unboxInt();
88 }
89 } catch (AttributeNotFoundException | StateSystemDisposedException e) {
90 Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e);
91 }
92 return tid;
93 }
94
95 /**
96 * Gets the CPU a thread is running on for a given time <br>
97 * Note: this is not designed to be fast, only convenient
98 *
99 * @param tid
100 * the tid
101 * @param time
102 * the time in nanoseconds
103 * @return the current CPU at the time for a TID or {@code null} if not
104 * available
105 */
106 public @Nullable Integer getCpuForTidAtTime(int tid, long time) {
107 ITmfStateSystem stateSystem = getStateSystem();
108 if (stateSystem == null) {
109 return null;
110 }
111
112 try {
113 for (ITmfStateInterval interval : stateSystem.queryFullState(time)) {
114 if (tid == interval.getStateValue().unboxInt()) {
115 return Integer.parseInt(stateSystem.getAttributeName(interval.getAttribute()));
116 }
117 }
118 } catch (StateSystemDisposedException e) {
119 Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e);
120 }
121 return null;
122 }
123 }
This page took 0.035133 seconds and 4 git commands to generate.