00e813be8c416803fdf98cdede3a70965dac149e
[deliverable/tracecompass.git] / lttng / 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.util.Collection;
17 import java.util.Map;
18 import java.util.Set;
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.KernelTidAspect;
26 import org.eclipse.tracecompass.analysis.os.linux.core.kernelanalysis.ThreadPriorityAspect;
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.Lttng27EventLayout;
33 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.Lttng28EventLayout;
34 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.LttngEventLayout;
35 import org.eclipse.tracecompass.internal.lttng2.kernel.core.trace.layout.PerfEventLayout;
36 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
37 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
38 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
39 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
40 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
41 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
42 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTraceValidationStatus;
43 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfUtils;
44
45 import com.google.common.collect.ImmutableSet;
46
47 /**
48 * This is the specification of CtfTmfTrace for use with LTTng 2.x kernel
49 * traces.
50 *
51 * @author Alexandre Montplaisir
52 */
53 public class LttngKernelTrace extends CtfTmfTrace implements IKernelTrace {
54
55 /**
56 * Supported Linux kernel tracers
57 */
58 private enum OriginTracer {
59 LTTNG(LttngEventLayout.getInstance()),
60 LTTNG26(Lttng26EventLayout.getInstance()),
61 LTTNG27(Lttng27EventLayout.getInstance()),
62 LTTNG28(Lttng28EventLayout.getInstance()),
63 PERF(PerfEventLayout.getInstance());
64
65 private final @NonNull IKernelAnalysisEventLayout fLayout;
66
67 private OriginTracer(@NonNull IKernelAnalysisEventLayout layout) {
68 fLayout = layout;
69 }
70 }
71
72 /**
73 * Event aspects available for all Lttng Kernel traces
74 */
75 private static final @NonNull Collection<ITmfEventAspect> LTTNG_KERNEL_ASPECTS;
76
77 static {
78 ImmutableSet.Builder<ITmfEventAspect> builder = ImmutableSet.builder();
79 builder.addAll(CtfTmfTrace.CTF_ASPECTS);
80 builder.add(KernelTidAspect.INSTANCE);
81 builder.add(ThreadPriorityAspect.INSTANCE);
82 LTTNG_KERNEL_ASPECTS = NonNullUtils.checkNotNull(builder.build());
83 }
84
85 /**
86 * CTF metadata identifies trace type and tracer version pretty well, we are
87 * quite confident in the inferred trace type.
88 */
89 private static final int CONFIDENCE = 100;
90
91 /** The tracer which originated this trace */
92 private OriginTracer fOriginTracer = null;
93
94 /**
95 * Default constructor
96 */
97 public LttngKernelTrace() {
98 super();
99 }
100
101 @Override
102 public @NonNull IKernelAnalysisEventLayout getKernelEventLayout() {
103 OriginTracer tracer = fOriginTracer;
104 if (tracer == null) {
105 throw new IllegalStateException("Cannot get the layout of a non-initialized trace!"); //$NON-NLS-1$
106 }
107 return tracer.fLayout;
108 }
109
110 @Override
111 public void initTrace(IResource resource, String path,
112 Class<? extends ITmfEvent> eventType) throws TmfTraceException {
113 super.initTrace(resource, path, eventType);
114 fOriginTracer = getTracerFromEnv();
115 }
116
117 /**
118 * Identify which tracer generated a trace from its metadata.
119 */
120 private OriginTracer getTracerFromEnv() {
121 String tracerName = CtfUtils.getTracerName(this);
122 int tracerMajor = CtfUtils.getTracerMajorVersion(this);
123 int tracerMinor = CtfUtils.getTracerMinorVersion(this);
124
125 if ("perf".equals(tracerName)) { //$NON-NLS-1$
126 return OriginTracer.PERF;
127
128 } else if ("lttng-modules".equals(tracerName)) { //$NON-NLS-1$
129 /* Look for specific versions of LTTng */
130 if (tracerMajor >= 2) {
131 if (tracerMinor >= 8) {
132 return OriginTracer.LTTNG28;
133 } else if (tracerMinor >= 7) {
134 return OriginTracer.LTTNG27;
135 } else if (tracerMinor >= 6) {
136 return OriginTracer.LTTNG26;
137 }
138 }
139 }
140
141 /* Use base LTTng layout as default */
142 return OriginTracer.LTTNG;
143 }
144
145 /**
146 * {@inheritDoc}
147 * <p>
148 * This implementation sets the confidence to 100 if the trace is a valid
149 * CTF trace in the "kernel" domain.
150 */
151 @Override
152 public IStatus validate(final IProject project, final String path) {
153 IStatus status = super.validate(project, path);
154 if (status instanceof CtfTraceValidationStatus) {
155 Map<String, String> environment = ((CtfTraceValidationStatus) status).getEnvironment();
156 /* Make sure the domain is "kernel" in the trace's env vars */
157 String domain = environment.get("domain"); //$NON-NLS-1$
158 if (domain == null || !domain.equals("\"kernel\"")) { //$NON-NLS-1$
159 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.LttngKernelTrace_DomainError);
160 }
161 return new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
162 }
163 return status;
164 }
165
166 @Override
167 public Iterable<ITmfEventAspect> getEventAspects() {
168 return LTTNG_KERNEL_ASPECTS;
169 }
170
171 /*
172 * Needs explicit @NonNull generic type annotation. Can be removed once this
173 * class becomes @NonNullByDefault.
174 */
175 @Override
176 public @NonNull Set<@NonNull CtfTmfEventType> getContainedEventTypes() {
177 return super.getContainedEventTypes();
178 }
179
180 }
This page took 0.034795 seconds and 5 git commands to generate.