| 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.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; |
| 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 TmfAbstractAnalysisRequirement> REQUIREMENTS = Collections.EMPTY_SET; |
| 53 | |
| 54 | @Override |
| 55 | public @NonNull Iterable<@NonNull TmfAbstractAnalysisRequirement> 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() : DefaultEventLayout.getInstance(); |
| 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.optQuarkAbsolute(Integer.toString(cpu)); |
| 85 | if (cpuQuark == ITmfStateSystem.INVALID_ATTRIBUTE) { |
| 86 | return null; |
| 87 | } |
| 88 | ITmfStateValue value = stateSystem.querySingleState(time, cpuQuark).getStateValue(); |
| 89 | if (value.getType().equals(Type.INTEGER)) { |
| 90 | tid = value.unboxInt(); |
| 91 | } |
| 92 | } catch (StateSystemDisposedException e) { |
| 93 | Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e); |
| 94 | } |
| 95 | return tid; |
| 96 | } |
| 97 | |
| 98 | /** |
| 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 |
| 101 | * |
| 102 | * @param tid |
| 103 | * the tid |
| 104 | * @param time |
| 105 | * the time in nanoseconds |
| 106 | * @return the current CPU at the time for a TID or {@code null} if not |
| 107 | * available |
| 108 | */ |
| 109 | public @Nullable Integer getCpuForTidAtTime(int tid, long time) { |
| 110 | ITmfStateSystem stateSystem = getStateSystem(); |
| 111 | if (stateSystem == null) { |
| 112 | return null; |
| 113 | } |
| 114 | |
| 115 | try { |
| 116 | for (ITmfStateInterval interval : stateSystem.queryFullState(time)) { |
| 117 | if (tid == interval.getStateValue().unboxInt()) { |
| 118 | return Integer.parseInt(stateSystem.getAttributeName(interval.getAttribute())); |
| 119 | } |
| 120 | } |
| 121 | } catch (StateSystemDisposedException e) { |
| 122 | Activator.getDefault().logError(NonNullUtils.nullToEmptyString(e.getMessage()), e); |
| 123 | } |
| 124 | return null; |
| 125 | } |
| 126 | } |