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