lttng: Add utility methods to get CTF tracer versions
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Wed, 8 Jul 2015 20:15:24 +0000 (16:15 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Thu, 1 Oct 2015 17:46:21 +0000 (13:46 -0400)
It's not uncommon to check for tracer names and versions of CTF
traces. We can offer convenience methods to do so, instead of
having to parse the environment metadata every time.

Change-Id: I9c4cbc7e36d24d972889f6c4bbc16d2db852f741
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/51661
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
ctf/org.eclipse.tracecompass.tmf.ctf.core/src/org/eclipse/tracecompass/tmf/ctf/core/trace/CtfUtils.java [new file with mode: 0644]
lttng/org.eclipse.tracecompass.lttng2.kernel.core/src/org/eclipse/tracecompass/lttng2/kernel/core/trace/LttngKernelTrace.java
lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/lttng2/ust/core/trace/LttngUstTrace.java

diff --git a/ctf/org.eclipse.tracecompass.tmf.ctf.core/src/org/eclipse/tracecompass/tmf/ctf/core/trace/CtfUtils.java b/ctf/org.eclipse.tracecompass.tmf.ctf.core/src/org/eclipse/tracecompass/tmf/ctf/core/trace/CtfUtils.java
new file mode 100644 (file)
index 0000000..0730dab
--- /dev/null
@@ -0,0 +1,85 @@
+/*******************************************************************************
+ * Copyright (c) 2015 EfficiOS Inc., Alexandre Montplaisir
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *******************************************************************************/
+
+package org.eclipse.tracecompass.tmf.ctf.core.trace;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.eclipse.jdt.annotation.Nullable;
+
+/**
+ * Utility methods for traces in the CTF format.
+ *
+ * @author Alexandre Montplaisir
+ * @since 2.0
+ */
+@NonNullByDefault
+public final class CtfUtils {
+
+    private CtfUtils() {
+    }
+
+    /**
+     * Convenience method to get the tracer name from the trace's metadata. The
+     * leading and trailing "" will be stripped from the returned string.
+     *
+     * @param trace
+     *            The trace to query
+     * @return The tracer's name, or null if it is not defined in the metadata.
+     */
+    public static @Nullable String getTracerName(CtfTmfTrace trace) {
+        String str = trace.getEnvironment().get("tracer_name"); //$NON-NLS-1$
+        if (str == null) {
+            return null;
+        }
+        /* Remove the "" at the start and end of the string */
+        return str.replaceAll("^\"|\"$", ""); //$NON-NLS-1$ //$NON-NLS-2$
+    }
+
+    /**
+     * Convenience method to get the tracer's major version from the trace
+     * metadata.
+     *
+     * @param trace
+     *            The trace to query
+     * @return The tracer's major version, or -1 if it is not defined.
+     */
+    public static int getTracerMajorVersion(CtfTmfTrace trace) {
+        String str = trace.getEnvironment().get("tracer_major"); //$NON-NLS-1$
+        if (str == null) {
+            return -1;
+        }
+        try {
+            int ret = Integer.parseInt(str);
+            return ret;
+        } catch (NumberFormatException e) {
+            return -1;
+        }
+    }
+
+    /**
+     * Convenience method to get the tracer's minor version from the trace
+     * metadata.
+     *
+     * @param trace
+     *            The trace to query
+     * @return The tracer's minor version, or -1 if it is not defined.
+     */
+    public static int getTracerMinorVersion(CtfTmfTrace trace) {
+        String str = trace.getEnvironment().get("tracer_minor"); //$NON-NLS-1$
+        if (str == null) {
+            return -1;
+        }
+        try {
+            int ret = Integer.parseInt(str);
+            return ret;
+        } catch (NumberFormatException e) {
+            return -1;
+        }
+    }
+}
index b3e3c70433b559518160112b28dc572b12549eb2..26c7f94f08e53a70bb572608a3edaafd95eeb2c3 100644 (file)
@@ -37,6 +37,7 @@ import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTraceValidationStatus;
+import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfUtils;
 
 import com.google.common.collect.ImmutableSet;
 
@@ -106,26 +107,26 @@ public class LttngKernelTrace extends CtfTmfTrace implements IKernelTrace {
     public void initTrace(IResource resource, String path,
             Class<? extends ITmfEvent> eventType) throws TmfTraceException {
         super.initTrace(resource, path, eventType);
-        fOriginTracer = getTracerFromEnv(this.getEnvironment());
+        fOriginTracer = getTracerFromEnv();
     }
 
     /**
      * Identify which tracer generated a trace from its metadata.
      */
-    private static OriginTracer getTracerFromEnv(Map<String, String> traceEnv) {
-        String tracerName = traceEnv.get("tracer_name"); //$NON-NLS-1$
-        String tracerMajor = traceEnv.get("tracer_major"); //$NON-NLS-1$
-        String tracerMinor = traceEnv.get("tracer_minor"); //$NON-NLS-1$
+    private OriginTracer getTracerFromEnv() {
+        String tracerName = CtfUtils.getTracerName(this);
+        int tracerMajor = CtfUtils.getTracerMajorVersion(this);
+        int tracerMinor = CtfUtils.getTracerMinorVersion(this);
 
-        if ("\"perf\"".equals(tracerName)) { //$NON-NLS-1$
+        if ("perf".equals(tracerName)) { //$NON-NLS-1$
             return OriginTracer.PERF;
 
-        } else if ("\"lttng-modules\"".equals(tracerName) && tracerMajor != null && tracerMinor != null) { //$NON-NLS-1$
+        } else if ("lttng-modules".equals(tracerName)) { //$NON-NLS-1$
             /* Look for specific versions of LTTng */
-            if (Integer.valueOf(tracerMajor) >= 2) {
-                if (Integer.valueOf(tracerMinor) >= 7) {
+            if (tracerMajor >= 2) {
+                if (tracerMinor >= 7) {
                     return OriginTracer.LTTNG27;
-                } else if (Integer.valueOf(tracerMinor) >= 6) {
+                } else if (tracerMinor >= 6) {
                     return OriginTracer.LTTNG26;
                 }
             }
index dd437a8eee36a02e617c38eb03056e9c4ac345ca..8c4f9c8904e138c97247e3ec7091d3554cb499ba 100644 (file)
@@ -31,6 +31,7 @@ import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTraceValidationStatus;
+import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfUtils;
 
 /**
  * Class to contain LTTng-UST traces
@@ -71,17 +72,17 @@ public class LttngUstTrace extends CtfTmfTrace {
         super.initTrace(resource, path, eventType);
 
         /* Determine the event layout to use from the tracer's version */
-        fLayout = getLayoutFromEnv(this.getEnvironment());
+        fLayout = getLayoutFromEnv();
     }
 
-    private static @NonNull ILttngUstEventLayout getLayoutFromEnv(Map<String, String> traceEnv) {
-        String tracerName = traceEnv.get("tracer_name"); //$NON-NLS-1$
-        String tracerMajor = traceEnv.get("tracer_major"); //$NON-NLS-1$
-        String tracerMinor = traceEnv.get("tracer_minor"); //$NON-NLS-1$
+    private @NonNull ILttngUstEventLayout getLayoutFromEnv() {
+        String tracerName = CtfUtils.getTracerName(this);
+        int tracerMajor = CtfUtils.getTracerMajorVersion(this);
+        int tracerMinor = CtfUtils.getTracerMinorVersion(this);
 
-        if ("\"lttng-ust\"".equals(tracerName) && tracerMajor != null && tracerMinor != null) { //$NON-NLS-1$
-            if (Integer.valueOf(tracerMajor) >= 2) {
-                if (Integer.valueOf(tracerMinor) >= 7) {
+        if ("lttng-ust".equals(tracerName)) { //$NON-NLS-1$
+            if (tracerMajor >= 2) {
+                if (tracerMinor >= 7) {
                     return LttngUst27EventLayout.getInstance();
                 }
                 return LttngUst20EventLayout.getInstance();
This page took 0.029085 seconds and 5 git commands to generate.