lttng: Move the UST memory analysis to use ILttngUstEventLayout
authorAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Fri, 21 Aug 2015 01:20:11 +0000 (21:20 -0400)
committerAlexandre Montplaisir <alexmonthy@voxpopuli.im>
Mon, 28 Sep 2015 17:02:28 +0000 (13:02 -0400)
This will allow the analysis to work with different versions
of the UST.

Removed the getRequirements() test, because testing (what used
to be) statically-defined values is not particularly useful.

Fixes Bug: 470186.

Change-Id: Id3c80467a55c9c3d47c8073d49e8f4ea3ead4f5b
Signed-off-by: Alexandre Montplaisir <alexmonthy@voxpopuli.im>
Reviewed-on: https://git.eclipse.org/r/54272
Reviewed-by: Hudson CI
Reviewed-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
Tested-by: Marc-Andre Laperle <marc-andre.laperle@ericsson.com>
lttng/org.eclipse.tracecompass.lttng2.ust.core.tests/src/org/eclipse/tracecompass/lttng2/ust/core/tests/analysis/memory/UstMemoryAnalysisModuleTest.java
lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/internal/lttng2/ust/core/LttngUstEventStrings.java [deleted file]
lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/internal/lttng2/ust/core/analysis/memory/UstMemoryStateProvider.java
lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/lttng2/ust/core/analysis/memory/UstMemoryAnalysisModule.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAbstractAnalysisModule.java

index 1bd4ea85cfd910eff2342a160ad041c02a5a97d6..e8584d20bade90c8a63f01137b5b1894c2c87a99 100644 (file)
 
 package org.eclipse.tracecompass.lttng2.ust.core.tests.analysis.memory;
 
-import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-import java.util.Set;
-
-import org.eclipse.tracecompass.internal.lttng2.ust.core.LttngUstEventStrings;
-import org.eclipse.tracecompass.lttng2.control.core.session.SessionConfigStrings;
 import org.eclipse.tracecompass.lttng2.ust.core.analysis.memory.UstMemoryAnalysisModule;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement;
 import org.junit.Before;
+import org.junit.Ignore;
 import org.junit.Test;
 
-import com.google.common.collect.ImmutableSet;
-
 /**
  * Tests for the {@link UstMemoryAnalysisModule}
  *
@@ -48,54 +42,13 @@ public class UstMemoryAnalysisModuleTest {
     /**
      * Test for {@link UstMemoryAnalysisModule#getAnalysisRequirements()}
      */
+    @Ignore("Need to assign a trace to the module to check the requirements now")
     @Test
     public void testGetAnalysisRequirements() {
         Iterable<TmfAnalysisRequirement> requirements = fUstAnalysisModule.getAnalysisRequirements();
+
         assertNotNull(requirements);
         assertTrue(requirements.iterator().hasNext());
-
-        /* There should be the event and domain type */
-        TmfAnalysisRequirement eventReq = null;
-        TmfAnalysisRequirement domainReq = null;
-        int numberOfRequirement = 0;
-        for (TmfAnalysisRequirement requirement : requirements) {
-            ++numberOfRequirement;
-            if (requirement.getType().equals(SessionConfigStrings.CONFIG_ELEMENT_EVENT)) {
-                eventReq = requirement;
-            } else if (requirement.getType().equals(SessionConfigStrings.CONFIG_ELEMENT_DOMAIN)) {
-                domainReq = requirement;
-            }
-        }
-        assertNotNull(eventReq);
-        assertNotNull(domainReq);
-
-        /* There should be two requirements */
-        assertEquals(2, numberOfRequirement);
-
-        /* Verify the content of the requirements themselves */
-        /* Domain should be kernel */
-        assertEquals(1, domainReq.getValues().size());
-        for (String domain : domainReq.getValues()) {
-            assertEquals(SessionConfigStrings.CONFIG_DOMAIN_TYPE_UST, domain);
-        }
-
-        /* Events */
-        Set<String> expectedEvents = ImmutableSet.of(
-                LttngUstEventStrings.MALLOC,
-                LttngUstEventStrings.FREE,
-                LttngUstEventStrings.CALLOC,
-                LttngUstEventStrings.REALLOC,
-                LttngUstEventStrings.MEMALIGN,
-                LttngUstEventStrings.POSIX_MEMALIGN
-                );
-
-        assertEquals(6, eventReq.getValues().size());
-        for (String event : eventReq.getValues()) {
-            assertTrue("Unexpected event " + event, expectedEvents.contains(event));
-        }
-
-        Set<String> infos = eventReq.getInformation();
-        assertEquals(2, infos.size());
     }
 
 }
diff --git a/lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/internal/lttng2/ust/core/LttngUstEventStrings.java b/lttng/org.eclipse.tracecompass.lttng2.ust.core/src/org/eclipse/tracecompass/internal/lttng2/ust/core/LttngUstEventStrings.java
deleted file mode 100644 (file)
index 7658a14..0000000
+++ /dev/null
@@ -1,41 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2015 Ericsson, École Polytechnique de Montréal
- *
- * 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.internal.lttng2.ust.core;
-
-/**
- * Strings related to UST traces and convenience libraries.
- *
- * @author Matthew Khouzam
- * @author Geneviève Bastien
- * @noimplement This interface only contains static definitions.
- */
-@SuppressWarnings({ "nls", "javadoc" })
-public interface LttngUstEventStrings {
-
-    /* UST_libc event names */
-    String MALLOC = "ust_libc:malloc";
-    String CALLOC = "ust_libc:calloc";
-    String REALLOC = "ust_libc:realloc";
-    String FREE = "ust_libc:free";
-    String MEMALIGN = "ust_libc:memalign";
-    String POSIX_MEMALIGN = "ust_libc:posix_memalign";
-
-    /* Possible contexts */
-    String CONTEXT_VTID = "context._vtid";
-    String CONTEXT_PROCNAME = "context._procname";
-
-    /* Event fields */
-    String FIELD_PTR = "ptr";
-    String FIELD_NMEMB = "nmemb";
-    String FIELD_SIZE = "size";
-    String FIELD_OUTPTR = "out_ptr";
-    String FIELD_INPTR = "in_ptr";
-
-}
index 83917277229e16f6e7ae347f1b7c4edb88e68792..4834f9c0780e44df2d079eb1628b907f63d31546 100644 (file)
@@ -19,8 +19,8 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.tracecompass.internal.lttng2.ust.core.LttngUstEventStrings;
 import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace;
+import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystemBuilder;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
@@ -32,6 +32,8 @@ import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
 import org.eclipse.tracecompass.tmf.core.statesystem.AbstractTmfStateProvider;
 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
 
+import com.google.common.collect.ImmutableMap;
+
 /**
  * State provider to track the memory of the threads using the UST libc wrapper
  * memory events.
@@ -44,13 +46,23 @@ public class UstMemoryStateProvider extends AbstractTmfStateProvider {
     /* Version of this state provider */
     private static final int VERSION = 1;
 
-    /* Maps a pointer to a memory zone to the size of the memory */
-    private final Map<Long, Long> fMemory = new HashMap<>();
-
     private static final Long MINUS_ONE = Long.valueOf(-1);
     private static final Long ZERO = Long.valueOf(0);
     private static final String EMPTY_STRING = ""; //$NON-NLS-1$
 
+    private static final int MALLOC_INDEX = 1;
+    private static final int FREE_INDEX = 2;
+    private static final int CALLOC_INDEX = 3;
+    private static final int REALLOC_INDEX = 4;
+    private static final int MEMALIGN_INDEX = 5;
+    private static final int POSIX_MEMALIGN_INDEX = 6;
+
+    /** Map of a pointer to a memory zone to the size of the memory */
+    private final Map<Long, Long> fMemory = new HashMap<>();
+
+    private final @NonNull ILttngUstEventLayout fLayout;
+    private final @NonNull Map<String, Integer> fEventNames;
+
     /**
      * Constructor
      *
@@ -59,69 +71,86 @@ public class UstMemoryStateProvider extends AbstractTmfStateProvider {
      */
     public UstMemoryStateProvider(@NonNull LttngUstTrace trace) {
         super(trace, "Ust:Memory"); //$NON-NLS-1$
+        fLayout = trace.getEventLayout();
+        fEventNames = buildEventNames(fLayout);
+    }
+
+    private static @NonNull Map<String, Integer> buildEventNames(ILttngUstEventLayout layout) {
+        ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder();
+        builder.put(layout.eventLibcMalloc(), MALLOC_INDEX);
+        builder.put(layout.eventLibcFree(), FREE_INDEX);
+        builder.put(layout.eventLibcCalloc(), CALLOC_INDEX);
+        builder.put(layout.eventLibcRealloc(), REALLOC_INDEX);
+        builder.put(layout.eventLibcMemalign(), MEMALIGN_INDEX);
+        builder.put(layout.eventLibcPosixMemalign(), POSIX_MEMALIGN_INDEX);
+        return checkNotNull(builder.build());
     }
 
     @Override
     protected void eventHandle(ITmfEvent event) {
         String name = event.getName();
-        switch (name) {
-        case LttngUstEventStrings.MALLOC: {
-            Long ptr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_PTR).getValue();
+        Integer index = fEventNames.get(name);
+        int intIndex = (index == null ? -1 : index.intValue());
+
+        switch (intIndex) {
+        case MALLOC_INDEX: {
+            Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue();
             if (ZERO.equals(ptr)) {
                 return;
             }
-            Long size = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_SIZE).getValue();
+            Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue();
             setMem(event, ptr, size);
         }
             break;
-        case LttngUstEventStrings.FREE: {
-            Long ptr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_PTR).getValue();
+        case FREE_INDEX: {
+            Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue();
             if (ZERO.equals(ptr)) {
                 return;
             }
             setMem(event, ptr, ZERO);
         }
             break;
-        case LttngUstEventStrings.CALLOC: {
-            Long ptr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_PTR).getValue();
+        case CALLOC_INDEX: {
+            Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue();
             if (ZERO.equals(ptr)) {
                 return;
             }
-            Long nmemb = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_NMEMB).getValue();
-            Long size = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_SIZE).getValue();
+            Long nmemb = (Long) event.getContent().getField(fLayout.fieldNmemb()).getValue();
+            Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue();
             setMem(event, ptr, size * nmemb);
         }
             break;
-        case LttngUstEventStrings.REALLOC: {
-            Long ptr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_PTR).getValue();
+        case REALLOC_INDEX: {
+            Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue();
             if (ZERO.equals(ptr)) {
                 return;
             }
-            Long newPtr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_INPTR).getValue();
-            Long size = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_SIZE).getValue();
+            Long newPtr = (Long) event.getContent().getField(fLayout.fieldInPtr()).getValue();
+            Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue();
             setMem(event, ptr, ZERO);
             setMem(event, newPtr, size);
         }
             break;
-        case LttngUstEventStrings.MEMALIGN: {
-            Long ptr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_PTR).getValue();
+        case MEMALIGN_INDEX: {
+            Long ptr = (Long) event.getContent().getField(fLayout.fieldPtr()).getValue();
             if (ZERO.equals(ptr)) {
                 return;
             }
-            Long size = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_SIZE).getValue();
+            Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue();
             setMem(event, ptr, size);
         }
             break;
-        case LttngUstEventStrings.POSIX_MEMALIGN: {
-            Long ptr = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_OUTPTR).getValue();
+        case POSIX_MEMALIGN_INDEX: {
+            Long ptr = (Long) event.getContent().getField(fLayout.fieldOutPtr()).getValue();
             if (ZERO.equals(ptr)) {
                 return;
             }
-            Long size = (Long) event.getContent().getField(LttngUstEventStrings.FIELD_SIZE).getValue();
+            Long size = (Long) event.getContent().getField(fLayout.fieldSize()).getValue();
             setMem(event, ptr, size);
         }
             break;
         default:
+            /* Ignore other event types */
             break;
         }
 
@@ -142,16 +171,16 @@ public class UstMemoryStateProvider extends AbstractTmfStateProvider {
         return VERSION;
     }
 
-    private static Long getVtid(ITmfEvent event) {
-        ITmfEventField field = event.getContent().getField(LttngUstEventStrings.CONTEXT_VTID);
+    private Long getVtid(ITmfEvent event) {
+        ITmfEventField field = event.getContent().getField(fLayout.contextVtid());
         if (field == null) {
             return MINUS_ONE;
         }
         return (Long) field.getValue();
     }
 
-    private static String getProcname(ITmfEvent event) {
-        ITmfEventField field = event.getContent().getField(LttngUstEventStrings.CONTEXT_PROCNAME);
+    private String getProcname(ITmfEvent event) {
+        ITmfEventField field = event.getContent().getField(fLayout.contextProcname());
         if (field == null) {
             return EMPTY_STRING;
         }
index bc79cea99fa7a50e33e2fcba0705b37c3e42cd7f..0692772894935602a1c5b5bb8c2144ab06925b91 100644 (file)
@@ -18,10 +18,11 @@ import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 import java.util.Set;
 
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.tracecompass.internal.lttng2.ust.core.LttngUstEventStrings;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.internal.lttng2.ust.core.analysis.memory.UstMemoryStateProvider;
 import org.eclipse.tracecompass.lttng2.control.core.session.SessionConfigStrings;
 import org.eclipse.tracecompass.lttng2.ust.core.trace.LttngUstTrace;
+import org.eclipse.tracecompass.lttng2.ust.core.trace.layout.ILttngUstEventLayout;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement;
 import org.eclipse.tracecompass.tmf.core.analysis.TmfAnalysisRequirement.ValuePriorityLevel;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
@@ -44,34 +45,8 @@ public class UstMemoryAnalysisModule extends TmfStateSystemAnalysisModule {
      */
     public static final @NonNull String ID = "org.eclipse.linuxtools.lttng2.ust.analysis.memory"; //$NON-NLS-1$
 
-    private static final ImmutableSet<String> REQUIRED_EVENTS = ImmutableSet.of(
-            LttngUstEventStrings.MALLOC,
-            LttngUstEventStrings.FREE,
-            LttngUstEventStrings.CALLOC,
-            LttngUstEventStrings.REALLOC,
-            LttngUstEventStrings.MEMALIGN,
-            LttngUstEventStrings.POSIX_MEMALIGN
-            );
-
-    /** The requirements as an immutable set */
-    private static final @NonNull Set<TmfAnalysisRequirement> REQUIREMENTS;
-
-    static {
-        /* Initialize the requirements for the analysis: domain and events */
-        TmfAnalysisRequirement eventsReq = new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_EVENT, REQUIRED_EVENTS, ValuePriorityLevel.MANDATORY);
-        /*
-         * In order to have these events, the libc wrapper with probes should be
-         * loaded
-         */
-        eventsReq.addInformation(Messages.UstMemoryAnalysisModule_EventsLoadingInformation);
-        eventsReq.addInformation(Messages.UstMemoryAnalysisModule_EventsLoadingExampleInformation);
-
-        /* The domain type of the analysis */
-        TmfAnalysisRequirement domainReq = new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_DOMAIN);
-        domainReq.addValue(SessionConfigStrings.CONFIG_DOMAIN_TYPE_UST, ValuePriorityLevel.MANDATORY);
-
-        REQUIREMENTS = checkNotNull(ImmutableSet.of(domainReq, eventsReq));
-    }
+    /** The analysis's requirements. Only set after the trace is set. */
+    private @Nullable Set<TmfAnalysisRequirement> fAnalysisRequirements;
 
     @Override
     protected ITmfStateProvider createStateProvider() {
@@ -86,7 +61,17 @@ public class UstMemoryAnalysisModule extends TmfStateSystemAnalysisModule {
         if (!(trace instanceof LttngUstTrace)) {
             return false;
         }
-        return super.setTrace(trace);
+        /*
+         * setTrace() calls getAnalysisRequirements, so we need the field set
+         * for the check to work.
+         */
+        fAnalysisRequirements = requirementsForTrace((LttngUstTrace) trace);
+        boolean traceIsSet = super.setTrace(trace);
+        if (!traceIsSet) {
+            /* Unset the requirements, the trace was not good after all. */
+            fAnalysisRequirements = null;
+        }
+        return traceIsSet;
     }
 
     @Override
@@ -94,8 +79,43 @@ public class UstMemoryAnalysisModule extends TmfStateSystemAnalysisModule {
         return (LttngUstTrace) super.getTrace();
     }
 
+    private static Set<TmfAnalysisRequirement> requirementsForTrace(LttngUstTrace trace) {
+        /*
+         * Compute the list of required events, whose exact names can change
+         * depending on the tracer's version.
+         */
+        ILttngUstEventLayout layout = trace.getEventLayout();
+        Set<String> requiredEvents = ImmutableSet.of(
+                layout.eventLibcMalloc(),
+                layout.eventLibcFree(),
+                layout.eventLibcCalloc(),
+                layout.eventLibcRealloc(),
+                layout.eventLibcMemalign(),
+                layout.eventLibcPosixMemalign()
+              );
+
+        /* Initialize the requirements for the analysis: domain and events */
+        TmfAnalysisRequirement eventsReq = new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_EVENT, requiredEvents, ValuePriorityLevel.MANDATORY);
+        /*
+         * In order to have these events, the libc wrapper with probes should be
+         * loaded
+         */
+        eventsReq.addInformation(Messages.UstMemoryAnalysisModule_EventsLoadingInformation);
+        eventsReq.addInformation(Messages.UstMemoryAnalysisModule_EventsLoadingExampleInformation);
+
+        /* The domain type of the analysis */
+        TmfAnalysisRequirement domainReq = new TmfAnalysisRequirement(SessionConfigStrings.CONFIG_ELEMENT_DOMAIN);
+        domainReq.addValue(SessionConfigStrings.CONFIG_DOMAIN_TYPE_UST, ValuePriorityLevel.MANDATORY);
+
+        return checkNotNull(ImmutableSet.of(domainReq, eventsReq));
+    }
+
     @Override
     public Iterable<TmfAnalysisRequirement> getAnalysisRequirements() {
-        return REQUIREMENTS;
+        Set<TmfAnalysisRequirement> reqs = fAnalysisRequirements;
+        if (reqs == null) {
+            throw new IllegalStateException("Cannot get the analysis requirements without an assigned trace."); //$NON-NLS-1$
+        }
+        return reqs;
     }
 }
index 84972090a1fa9466a9126e899d11a96c0996a84e..fc1955e852e285b0d172c559080c2736dfe348bc 100644 (file)
@@ -123,11 +123,12 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent implements
         TmfCoreTracer.traceAnalysis(getId(), trace, "setting trace for analysis"); //$NON-NLS-1$
 
         /* Check that analysis can be executed */
+        fTrace = trace;
         if (!canExecute(trace)) {
+            fTrace = null;
             return false;
         }
 
-        fTrace = trace;
         /* Get the parameter providers for this trace */
         fParameterProviders = TmfAnalysisManager.getParameterProviders(this, trace);
         for (IAnalysisParameterProvider provider : fParameterProviders) {
This page took 0.032723 seconds and 5 git commands to generate.