1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
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 *******************************************************************************/
10 package org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.tid
;
12 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
14 import java
.util
.Collections
;
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
.DefaultEventLayout
;
21 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelAnalysisEventLayout
;
22 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.trace
.IKernelTrace
;
23 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
24 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.core
.Activator
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
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
.requirements
.TmfAbstractAnalysisRequirement
;
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
;
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}.
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.
43 * @author Matthew Khouzam
46 public class TidAnalysisModule
extends TmfStateSystemAnalysisModule
{
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$
51 /** The requirements as an immutable set */
52 private static final @NonNull Set
<@NonNull TmfAbstractAnalysisRequirement
> REQUIREMENTS
= Collections
.EMPTY_SET
;
55 public @NonNull Iterable
<@NonNull TmfAbstractAnalysisRequirement
> getAnalysisRequirements() {
60 protected @NonNull ITmfStateProvider
createStateProvider() {
61 ITmfTrace trace
= checkNotNull(getTrace());
62 IKernelAnalysisEventLayout layout
= (trace
instanceof IKernelTrace
) ?
((IKernelTrace
) trace
).getKernelEventLayout() : DefaultEventLayout
.getInstance();
63 return new ActiveTidStateProvider(trace
, layout
);
67 * Gets the current thread ID on a given CPU for a given time
72 * the time in nanoseconds
73 * @return the current TID at the time on the CPU or {@code null} if not
76 public @Nullable Integer
getThreadOnCpuAtTime(int cpu
, long time
) {
77 ITmfStateSystem stateSystem
= getStateSystem();
78 if (stateSystem
== null) {
84 int cpuQuark
= stateSystem
.optQuarkAbsolute(Integer
.toString(cpu
));
85 if (cpuQuark
== ITmfStateSystem
.INVALID_ATTRIBUTE
) {
88 ITmfStateValue value
= stateSystem
.querySingleState(time
, cpuQuark
).getStateValue();
89 if (value
.getType().equals(Type
.INTEGER
)) {
90 tid
= value
.unboxInt();
92 } catch (StateSystemDisposedException e
) {
93 Activator
.getDefault().logError(NonNullUtils
.nullToEmptyString(e
.getMessage()), e
);
99 * Gets the CPU a thread is running on for a given time <br>
100 * Note: this is not designed to be fast, only convenient
105 * the time in nanoseconds
106 * @return the current CPU at the time for a TID or {@code null} if not
109 public @Nullable Integer
getCpuForTidAtTime(int tid
, long time
) {
110 ITmfStateSystem stateSystem
= getStateSystem();
111 if (stateSystem
== null) {
116 for (ITmfStateInterval interval
: stateSystem
.queryFullState(time
)) {
117 if (tid
== interval
.getStateValue().unboxInt()) {
118 return Integer
.parseInt(stateSystem
.getAttributeName(interval
.getAttribute()));
121 } catch (StateSystemDisposedException e
) {
122 Activator
.getDefault().logError(NonNullUtils
.nullToEmptyString(e
.getMessage()), e
);