| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 Keba AG |
| 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 | * Christian Mansky - Initial implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.tracecompass.analysis.os.linux.core.event.aspect; |
| 14 | |
| 15 | import org.eclipse.jdt.annotation.NonNull; |
| 16 | import org.eclipse.jdt.annotation.Nullable; |
| 17 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule; |
| 18 | import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelTidAspect; |
| 19 | import org.eclipse.tracecompass.common.core.NonNullUtils; |
| 20 | import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes; |
| 21 | import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem; |
| 22 | import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException; |
| 23 | import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException; |
| 24 | import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException; |
| 25 | import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval; |
| 26 | import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue; |
| 27 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 28 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
| 29 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; |
| 30 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace; |
| 31 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
| 32 | |
| 33 | /** |
| 34 | * This aspect finds the priority of the thread running from this event using |
| 35 | * the {@link KernelAnalysisModule}. |
| 36 | * |
| 37 | * @author Christian Mansky |
| 38 | * @since 2.0 |
| 39 | */ |
| 40 | public final class ThreadPriorityAspect implements ITmfEventAspect { |
| 41 | |
| 42 | /** The singleton instance */ |
| 43 | public static final ThreadPriorityAspect INSTANCE = new ThreadPriorityAspect(); |
| 44 | |
| 45 | private ThreadPriorityAspect() { |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public final String getName() { |
| 50 | return NonNullUtils.nullToEmptyString(Messages.AspectName_Prio); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public final String getHelpText() { |
| 55 | return NonNullUtils.nullToEmptyString(Messages.AspectHelpText_Prio); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public @Nullable Integer resolve(ITmfEvent event) { |
| 60 | final @NonNull ITmfTrace trace = event.getTrace(); |
| 61 | KernelAnalysisModule kernelAnalysis = TmfTraceUtils.getAnalysisModuleOfClass(trace, KernelAnalysisModule.class, KernelAnalysisModule.ID); |
| 62 | if (kernelAnalysis == null) { |
| 63 | return null; |
| 64 | } |
| 65 | |
| 66 | ITmfStateSystem ss = kernelAnalysis.getStateSystem(); |
| 67 | if (ss == null) { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | Integer tid = KernelTidAspect.INSTANCE.resolve(event); |
| 72 | if (tid == null) { |
| 73 | return null; |
| 74 | } |
| 75 | |
| 76 | final long ts = event.getTimestamp().getValue(); |
| 77 | Integer execPrio = null; |
| 78 | try { |
| 79 | Integer cpu = 0; |
| 80 | if (tid == 0) { |
| 81 | /* Find the CPU this event is run on */ |
| 82 | cpu = TmfTraceUtils.resolveIntEventAspectOfClassForEvent(trace, TmfCpuAspect.class, event); |
| 83 | } |
| 84 | int execPrioQuark = ss.getQuarkAbsolute(Attributes.THREADS, Attributes.buildThreadAttributeName(tid, cpu), Attributes.PRIO); |
| 85 | ITmfStateInterval interval = ss.querySingleState(ts, execPrioQuark); |
| 86 | ITmfStateValue prioValue = interval.getStateValue(); |
| 87 | /* We know the prio must be an Integer */ |
| 88 | execPrio = prioValue.unboxInt(); |
| 89 | } catch (AttributeNotFoundException | StateSystemDisposedException | TimeRangeException e) { |
| 90 | } |
| 91 | return execPrio; |
| 92 | } |
| 93 | } |