| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 École Polytechnique de Montréal |
| 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 | * Geneviève Bastien - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.lttng.scope.lttng.kernel.core.event.aspect; |
| 14 | |
| 15 | import static org.lttng.scope.common.core.NonNullUtils.nullToEmptyString; |
| 16 | |
| 17 | import org.eclipse.core.runtime.IProgressMonitor; |
| 18 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 19 | import org.eclipse.jdt.annotation.NonNull; |
| 20 | import org.eclipse.jdt.annotation.Nullable; |
| 21 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
| 22 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
| 23 | import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect; |
| 24 | import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils; |
| 25 | import org.lttng.scope.lttng.kernel.core.analysis.os.KernelAnalysisModule; |
| 26 | import org.lttng.scope.lttng.kernel.core.analysis.os.KernelThreadInformationProvider; |
| 27 | |
| 28 | /** |
| 29 | * This aspect finds the ID of the thread running from this event using the |
| 30 | * {@link KernelAnalysisModule}. |
| 31 | * |
| 32 | * @author Geneviève Bastien |
| 33 | */ |
| 34 | public final class KernelTidAspect implements ITmfEventAspect<Integer> { |
| 35 | |
| 36 | /** The singleton instance */ |
| 37 | public static final KernelTidAspect INSTANCE = new KernelTidAspect(); |
| 38 | |
| 39 | private static final IProgressMonitor NULL_MONITOR = new NullProgressMonitor(); |
| 40 | |
| 41 | private KernelTidAspect() { |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public String getName() { |
| 46 | return nullToEmptyString(Messages.KernelTidAspect_Name); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public String getHelpText() { |
| 51 | return nullToEmptyString(Messages.KernelTidAspect_HelpText); |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public @Nullable Integer resolve(ITmfEvent event) { |
| 56 | return resolve(event, false, NULL_MONITOR); |
| 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public @Nullable Integer resolve(@NonNull ITmfEvent event, boolean block, final IProgressMonitor monitor) { |
| 61 | /* Find the CPU this event is run on */ |
| 62 | Integer cpu = TmfTraceUtils.resolveIntEventAspectOfClassForEvent(event.getTrace(), |
| 63 | TmfCpuAspect.class, event); |
| 64 | if (cpu == null) { |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | /* Find the analysis module for the trace */ |
| 69 | KernelAnalysisModule analysis = TmfTraceUtils.getAnalysisModuleOfClass(event.getTrace(), |
| 70 | KernelAnalysisModule.class, KernelAnalysisModule.ID); |
| 71 | if (analysis == null) { |
| 72 | return null; |
| 73 | } |
| 74 | long ts = event.getTimestamp().toNanos(); |
| 75 | while (block && !analysis.isQueryable(ts) && !monitor.isCanceled()) { |
| 76 | try { |
| 77 | Thread.sleep(100); |
| 78 | } catch (InterruptedException e) { |
| 79 | e.printStackTrace(); |
| 80 | } |
| 81 | } |
| 82 | return KernelThreadInformationProvider.getThreadOnCpu(analysis, cpu, ts); |
| 83 | } |
| 84 | |
| 85 | } |