os.linux: Make the priority of the KernelState available as an Aspect
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.kernel.core / src / org / eclipse / tracecompass / lttng2 / kernel / core / trace / LttngKernelTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson
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 * Alexandre Montplaisir - Initial API and implementation
11 * Matthew Khouzam - Improved validation
12 ******************************************************************************/
13
14 package org.eclipse.tracecompass.lttng2.kernel.core.trace;
15
16 import java.nio.BufferOverflowException;
17 import java.util.Collection;
18 import java.util.Map;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.jdt.annotation.NonNull;
25 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.ThreadPriorityAspect;
26 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.KernelTidAspect;
27 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelAnalysisEventLayout;
28 import org.eclipse.tracecompass.analysis.os.linux.core.trace.IKernelTrace;
29 import org.eclipse.tracecompass.common.core.NonNullUtils;
30 import org.eclipse.tracecompass.internal.lttng2.kernel.core.Activator;
31 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.Lttng26EventLayout;
32 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.LttngEventLayout;
33 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.PerfEventLayout;
34 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
35 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
36 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
37 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
38 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
39 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
40
41 import com.google.common.collect.ImmutableSet;
42
43 /**
44 * This is the specification of CtfTmfTrace for use with LTTng 2.x kernel
45 * traces.
46 *
47 * @author Alexandre Montplaisir
48 */
49 public class LttngKernelTrace extends CtfTmfTrace implements IKernelTrace {
50
51 /**
52 * Supported Linux kernel tracers
53 */
54 private enum OriginTracer {
55 LTTNG(LttngEventLayout.getInstance()),
56 LTTNG26(Lttng26EventLayout.getInstance()),
57 PERF(PerfEventLayout.getInstance());
58
59 private final @NonNull IKernelAnalysisEventLayout fLayout;
60
61 private OriginTracer(@NonNull IKernelAnalysisEventLayout layout) {
62 fLayout = layout;
63 }
64 }
65
66 /**
67 * Event aspects available for all Lttng Kernel traces
68 */
69 private static final @NonNull Collection<ITmfEventAspect> LTTNG_KERNEL_ASPECTS;
70
71 static {
72 ImmutableSet.Builder<ITmfEventAspect> builder = ImmutableSet.builder();
73 builder.addAll(CtfTmfTrace.CTF_ASPECTS);
74 builder.add(KernelTidAspect.INSTANCE);
75 builder.add(ThreadPriorityAspect.INSTANCE);
76 LTTNG_KERNEL_ASPECTS = NonNullUtils.checkNotNull(builder.build());
77 }
78
79 /**
80 * CTF metadata identifies trace type and tracer version pretty well, we are
81 * quite confident in the inferred trace type.
82 */
83 private static final int CONFIDENCE = 100;
84
85 /** The tracer which originated this trace */
86 private OriginTracer fOriginTracer = null;
87
88 /**
89 * Default constructor
90 */
91 public LttngKernelTrace() {
92 super();
93 }
94
95 @Override
96 public @NonNull IKernelAnalysisEventLayout getKernelEventLayout() {
97 OriginTracer tracer = fOriginTracer;
98 if (tracer == null) {
99 throw new IllegalStateException("Cannot get the layout of a non-initialized trace!"); //$NON-NLS-1$
100 }
101 return tracer.fLayout;
102 }
103
104 @Override
105 public void initTrace(IResource resource, String path,
106 Class<? extends ITmfEvent> eventType) throws TmfTraceException {
107 super.initTrace(resource, path, eventType);
108
109 /*
110 * Set the 'fOriginTracer' in accordance to what is found in the
111 * metadata
112 */
113 Map<String, String> traceEnv = this.getEnvironment();
114 String tracerName = traceEnv.get("tracer_name"); //$NON-NLS-1$
115 String tracerMajor = traceEnv.get("tracer_major"); //$NON-NLS-1$
116 String tracerMinor = traceEnv.get("tracer_minor"); //$NON-NLS-1$
117
118 if ("\"perf\"".equals(tracerName)) { //$NON-NLS-1$
119 fOriginTracer = OriginTracer.PERF;
120
121 } else if ("\"lttng-modules\"".equals(tracerName) && //$NON-NLS-1$
122 tracerMajor != null && (Integer.valueOf(tracerMajor) >= 2) &&
123 tracerMinor != null && (Integer.valueOf(tracerMinor) >= 6)) {
124 fOriginTracer = OriginTracer.LTTNG26;
125
126 } else {
127 fOriginTracer = OriginTracer.LTTNG;
128 }
129 }
130
131 /**
132 * {@inheritDoc}
133 * <p>
134 * This implementation sets the confidence to 100 if the trace is a valid
135 * CTF trace in the "kernel" domain.
136 */
137 @Override
138 public IStatus validate(final IProject project, final String path) {
139 /*
140 * Make sure the trace is openable as a CTF trace. We do this here
141 * instead of calling super.validate() to keep the reference to "temp".
142 */
143 try (CtfTmfTrace temp = new CtfTmfTrace();) {
144 temp.initTrace((IResource) null, path, CtfTmfEvent.class);
145
146 /* Make sure the domain is "kernel" in the trace's env vars */
147 String dom = temp.getEnvironment().get("domain"); //$NON-NLS-1$
148 if (dom != null && dom.equals("\"kernel\"")) { //$NON-NLS-1$
149 return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
150 }
151 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.LttngKernelTrace_DomainError);
152
153 } catch (TmfTraceException e) {
154 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), e);
155 } catch (NullPointerException e) {
156 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, e.toString(), e);
157 } catch (final BufferOverflowException e) {
158 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.LttngKernelTrace_TraceReadError + ": " + Messages.LttngKernelTrace_MalformedTrace); //$NON-NLS-1$
159 }
160 }
161
162 @Override
163 public Iterable<ITmfEventAspect> getEventAspects() {
164 return LTTNG_KERNEL_ASPECTS;
165 }
166
167 }
This page took 0.034096 seconds and 5 git commands to generate.