TMF: Add support of aspects to TmfXmlStubTrace
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Thu, 20 Nov 2014 18:47:14 +0000 (13:47 -0500)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Wed, 3 Dec 2014 20:11:58 +0000 (15:11 -0500)
Change-Id: Id729a9284477a44fd1e142e6fd19e3bad3b1894a
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/36803
Tested-by: Hudson CI
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
org.eclipse.tracecompass.tmf.core.tests/META-INF/MANIFEST.MF
org.eclipse.tracecompass.tmf.core.tests/plugin.xml
org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java
org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/Messages.java
org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xml
org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xsd
org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java
org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/package-info.java [new file with mode: 0644]
org.eclipse.tracecompass.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml

index 4b03dbc5aa6d9478e5a0f205ca4df66f6955e172..1784f17c2328ef4520ccbae888367b2cc600b0b2 100644 (file)
@@ -18,4 +18,5 @@ Export-Package: org.eclipse.tracecompass.tmf.core.tests,
  org.eclipse.tracecompass.tmf.core.tests.shared,
  org.eclipse.tracecompass.tmf.tests.stubs.trace,
  org.eclipse.tracecompass.tmf.tests.stubs.trace.xml
-Import-Package: com.google.common.collect
+Import-Package: com.google.common.collect,
+ com.google.common.primitives;version="15.0.0"
index c7f9461521c2c88edf020e45a0a151d2926b30b9..077c0ec294067afa72b8f1ec4855c58f8e81a866 100644 (file)
             name="Test Syslog"
             trace_type="org.eclipse.tracecompass.tmf.tests.stubs.trace.text.SyslogTrace">
       </type>
+      <type
+            category="org.eclipse.linuxtools.tmf.core.tests.category"
+            event_type="org.eclipse.tracecompass.tmf.core.event.TmfEvent"
+            id="org.eclipse.linuxtools.tmf.core.tests.xmlstub"
+            name="XML Trace Stub"
+            trace_type="org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub">
+      </type>
    </extension>
 
 </plugin>
index 91dbb54991f63317750b8fbf648ec9bf330392e9..15361b6c3a3c4e85715d789759c54f2cc976fcf6 100644 (file)
@@ -13,6 +13,7 @@
 package org.eclipse.tracecompass.tmf.core.tests.trace.stub;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
 
@@ -29,11 +30,14 @@ import org.eclipse.core.runtime.Status;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
+import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.request.TmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.tests.TmfCoreTestPlugin;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.TmfXmlTraceStub;
 import org.junit.Test;
@@ -124,6 +128,60 @@ public class XmlStubTraceTest {
         assertEquals(4, req.getCount());
     }
 
+    /**
+     * Test the presence and resolve of the aspects for this trace
+     */
+    @Test
+    public void testAspects() {
+        TmfXmlTraceStub trace = new TmfXmlTraceStub();
+        IStatus status = trace.validate(null, getAbsolutePath(VALID_FILE).toOSString());
+        if (!status.isOK()) {
+            fail(status.getException().getMessage());
+        }
+
+        try {
+            trace.initTrace(null, getAbsolutePath(VALID_FILE).toOSString(), TmfEvent.class);
+        } catch (TmfTraceException e1) {
+            fail(e1.getMessage());
+        }
+
+        ITmfEventAspect cpuAspect = null;
+        ITmfEventAspect testAspect = null;
+        int aspectCount = 0;
+        for (ITmfEventAspect aspect : trace.getEventAspects()) {
+            aspectCount++;
+            if (aspect instanceof TmfCpuAspect) {
+                cpuAspect = aspect;
+            } else if (aspect.getName().equals("test")) {
+                testAspect = aspect;
+            }
+        }
+        /* Check the presence of the cpu and test aspects */
+        assertEquals("Number of aspects", 5, aspectCount);
+        assertNotNull(cpuAspect);
+        assertNotNull(testAspect);
+
+        ITmfContext ctx;
+        ctx = trace.seekEvent(0L);
+        assertNotNull(ctx);
+        ITmfEvent event = trace.getNext(ctx);
+        assertNotNull(event);
+        assertEquals("Cpu aspect of event 1", 1, cpuAspect.resolve(event));
+        assertEquals("Test aspect of event 1", "abc", testAspect.resolve(event));
+        event = trace.getNext(ctx);
+        assertNotNull(event);
+        assertEquals("Cpu aspect of event 2", 1, cpuAspect.resolve(event));
+        assertEquals("Test aspect of event 2", "abc", testAspect.resolve(event));
+        event = trace.getNext(ctx);
+        assertNotNull(event);
+        assertEquals("Cpu aspect of event 3", 2, cpuAspect.resolve(event));
+        assertEquals("Test aspect of event 3", "def", testAspect.resolve(event));
+        event = trace.getNext(ctx);
+        assertNotNull(event);
+        assertEquals("Cpu aspect of event 4", 1, cpuAspect.resolve(event));
+        assertEquals("Test aspect of event 4", "def", testAspect.resolve(event));
+    }
+
     private static IStatus testEvent(ITmfEvent event) {
         switch (event.getType().getName()) {
         case EVENT_A: {
index 30b12bd31000e76e599d05ba4039d15fb3a7f781..5256634b86740ee0ab2856659ce7e3498076c332 100644 (file)
@@ -12,6 +12,7 @@
 
 package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
 
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.osgi.util.NLS;
 
 /**
@@ -24,9 +25,9 @@ import org.eclipse.osgi.util.NLS;
 public class Messages extends NLS {
     private static final String BUNDLE_NAME = "org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.messages"; //$NON-NLS-1$
 
-    public static String TmfDevelopmentTrace_FileNotFound;
-    public static String TmfDevelopmentTrace_IoError;
-    public static String TmfDevelopmentTrace_ValidationError;
+    public static @Nullable String TmfDevelopmentTrace_FileNotFound;
+    public static @Nullable String TmfDevelopmentTrace_IoError;
+    public static @Nullable String TmfDevelopmentTrace_ValidationError;
     static {
         // initialize resource bundle
         NLS.initializeMessages(BUNDLE_NAME, Messages.class);
index 96ec2b9b5dbec7eeb09074636840c520adc0e5b1..3a94a57720c89dd16f0aea792ce8f4a9c267a23a 100644 (file)
@@ -8,9 +8,6 @@
 <Attribute name="name">
 <InputData action="0" format="" name="Message"/>
 </Attribute>
-<Attribute name="source">
-<InputData action="0" format="" name="source"/>
-</Attribute>
 <Attribute name="timestamp">
 <InputData action="0" format="T" name="Time Stamp"/>
 </Attribute>
index fe8abdfd9e8abeefadcdadb99a57228faf4822ec..b77f0f9a1d12f5719a8dace8db4aadbbcfe17a5f 100644 (file)
@@ -27,7 +27,6 @@
                </xs:sequence>
                <xs:attribute name="timestamp" type="xs:integer" use="required" />
                <xs:attribute name="name" type="xs:string" use="required" />
-               <xs:attribute name="source" type="xs:integer" use="required" />
        </xs:complexType>
 
        <xs:complexType name="field">
index 04aec45d13a9e9dada494d1b89c11d63af94c583..85cc45fc08cc97f161489693d02f138b7f9b1b0e 100644 (file)
@@ -16,6 +16,7 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.net.URL;
+import java.util.Collection;
 
 import javax.xml.XMLConstants;
 import javax.xml.transform.Source;
@@ -29,6 +30,7 @@ import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
+import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.osgi.util.NLS;
 import org.eclipse.tracecompass.internal.tmf.core.Activator;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
@@ -37,6 +39,9 @@ import org.eclipse.tracecompass.tmf.core.event.ITmfEventType;
 import org.eclipse.tracecompass.tmf.core.event.TmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventType;
+import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfCpuAspect;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfEventFieldAspect;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomEventContent;
 import org.eclipse.tracecompass.tmf.core.parsers.custom.CustomXmlEvent;
@@ -50,6 +55,9 @@ import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
 import org.xml.sax.SAXException;
 
+import com.google.common.collect.ImmutableList;
+import com.google.common.primitives.Ints;
+
 /**
  * An XML development trace using a custom XML trace definition and schema.
  *
@@ -79,9 +87,13 @@ public class TmfXmlTraceStub extends TmfTrace {
     private static final String VALUES_SEPARATOR = " \\| "; //$NON-NLS-1$
     private static final String TYPE_INTEGER = "int"; //$NON-NLS-1$
     private static final String TYPE_LONG = "long"; //$NON-NLS-1$
+    private static final String ASPECT_SPECIAL_EVENT = "set_aspects";
+    private static final String ASPECT_CPU = "cpu";
 
     private final CustomXmlTrace fTrace;
 
+    private Collection<ITmfEventAspect> fAspects;
+
     /**
      * Constructor. Constructs the custom XML trace with the appropriate
      * definition.
@@ -98,6 +110,9 @@ public class TmfXmlTraceStub extends TmfTrace {
             /* Deregister the custom XML trace */
             TmfSignalManager.deregister(fTrace);
             this.setParser(fTrace);
+
+            Collection<ITmfEventAspect> aspects = TmfTrace.BASE_ASPECTS;
+            fAspects = aspects;
         } catch (IOException e) {
             throw new IllegalStateException("Cannot open the trace parser for development traces"); //$NON-NLS-1$
         }
@@ -105,12 +120,15 @@ public class TmfXmlTraceStub extends TmfTrace {
     }
 
     @Override
-    public void initTrace(IResource resource, String path, Class<? extends ITmfEvent> type) throws TmfTraceException {
+    public void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class<? extends ITmfEvent> type) throws TmfTraceException {
         super.initTrace(resource, path, type);
         fTrace.initTrace(resource, path, type);
         ITmfContext ctx;
         /* Set the start and (current) end times for this trace */
         ctx = seekEvent(0L);
+        if (ctx == null) {
+            return;
+        }
         ITmfEvent event = getNext(ctx);
         if (event != null) {
             final ITmfTimestamp curTime = event.getTimestamp();
@@ -120,27 +138,27 @@ public class TmfXmlTraceStub extends TmfTrace {
     }
 
     @Override
-    public ITmfLocation getCurrentLocation() {
+    public @Nullable ITmfLocation getCurrentLocation() {
         return fTrace.getCurrentLocation();
     }
 
     @Override
-    public double getLocationRatio(ITmfLocation location) {
+    public double getLocationRatio(@Nullable ITmfLocation location) {
         return fTrace.getLocationRatio(location);
     }
 
     @Override
-    public ITmfContext seekEvent(ITmfLocation location) {
+    public @Nullable ITmfContext seekEvent(@Nullable ITmfLocation location) {
         return fTrace.seekEvent(location);
     }
 
     @Override
-    public ITmfContext seekEvent(double ratio) {
+    public @Nullable ITmfContext seekEvent(double ratio) {
         return fTrace.seekEvent(ratio);
     }
 
     @Override
-    public IStatus validate(IProject project, String path) {
+    public IStatus validate(@Nullable IProject project, @Nullable String path) {
         File xmlFile = new File(path);
         if (!xmlFile.exists() || !xmlFile.isFile() || !xmlFile.canRead()) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_FileNotFound, path));
@@ -160,10 +178,12 @@ public class TmfXmlTraceStub extends TmfTrace {
         } catch (IOException e) {
             return new Status(IStatus.ERROR, Activator.PLUGIN_ID, NLS.bind(org.eclipse.tracecompass.tmf.tests.stubs.trace.xml.Messages.TmfDevelopmentTrace_IoError, path), e);
         }
-        return Status.OK_STATUS;
+        @SuppressWarnings("null")
+        @NonNull IStatus status = Status.OK_STATUS;
+        return status;
     }
 
-    private static String getStringValue(@NonNull ITmfEventField content, String fieldName) {
+    private static String getStringValue(ITmfEventField content, String fieldName) {
         ITmfEventField field = content.getField(fieldName);
         if (field == null) {
             return EMPTY;
@@ -176,7 +196,10 @@ public class TmfXmlTraceStub extends TmfTrace {
     }
 
     @Override
-    public synchronized ITmfEvent getNext(ITmfContext context) {
+    public synchronized @Nullable ITmfEvent getNext(@Nullable ITmfContext context) {
+        if (context == null) {
+            return null;
+        }
         final ITmfContext savedContext = new TmfContext(context.getLocation(), context.getRank());
         CustomXmlEvent event = fTrace.getNext(context);
         if (event == null) {
@@ -191,6 +214,7 @@ public class TmfXmlTraceStub extends TmfTrace {
         if (content == null) {
             return null;
         }
+
         String fieldString = getStringValue(content, FIELD_NAMES_FIELD);
         String valueString = getStringValue(content, VALUES_FIELD);
         String typeString = getStringValue(content, TYPES_FIELD);
@@ -237,9 +261,16 @@ public class TmfXmlTraceStub extends TmfTrace {
             fieldsArray[i] = new TmfEventField(fields[i], val, null);
         }
 
+        /* Generate the aspects for this trace if it is the aspects special event */
+        String eventName = getStringValue(content, EVENT_NAME_FIELD);
+        if (eventName.equals(ASPECT_SPECIAL_EVENT)) {
+            generateAspects(fieldsArray);
+            return getNext(context);
+        }
+
         /* Create a new event with new fields and name */
         ITmfEventType customEventType = event.getType();
-        TmfEventType eventType = new TmfEventType(getStringValue(content, EVENT_NAME_FIELD), customEventType.getRootField());
+        TmfEventType eventType = new TmfEventType(eventName, customEventType.getRootField());
         ITmfEventField eventFields = new CustomEventContent(content.getName(), content.getValue(), fieldsArray);
         // FIXME We used to use getSource() to get the CPU. Now this will have
         // to be done differently.
@@ -250,4 +281,63 @@ public class TmfXmlTraceStub extends TmfTrace {
         return newEvent;
     }
 
+    private static final class XmlStubCpuAspect extends TmfCpuAspect {
+
+        private final TmfEventFieldAspect fAspect;
+
+        public XmlStubCpuAspect(TmfEventFieldAspect aspect) {
+            fAspect = aspect;
+        }
+
+        @Override
+        public @Nullable String getFilterId() {
+            return getName();
+        }
+
+        @Override
+        public Integer resolve(ITmfEvent event) {
+            Integer cpu = Ints.tryParse(fAspect.resolve(event));
+            if (cpu == null) {
+                return TmfCpuAspect.CPU_UNAVAILABLE;
+            }
+            return cpu;
+        }
+
+    }
+
+    private void generateAspects(ITmfEventField[] fieldsArray) {
+        ImmutableList.Builder<ITmfEventAspect> builder = new ImmutableList.Builder<>();
+
+        /* Initialize the first default trace aspects */
+        builder.add(ITmfEventAspect.BaseAspects.TIMESTAMP);
+        builder.add(ITmfEventAspect.BaseAspects.EVENT_TYPE);
+
+        /* Add custom aspects in between */
+        for (ITmfEventField field : fieldsArray) {
+            String name = field.getName();
+            if (name == null) {
+                break;
+            }
+            ITmfEventAspect aspect = new TmfEventFieldAspect(name, name);
+            if (name.equals(ASPECT_CPU)) {
+                aspect = new XmlStubCpuAspect((TmfEventFieldAspect) aspect);
+            }
+            builder.add(aspect);
+        }
+
+        /* Add the big content aspect */
+        builder.add(ITmfEventAspect.BaseAspects.CONTENTS);
+
+        @SuppressWarnings("null")
+        @NonNull Collection<ITmfEventAspect> aspectList = builder.build();
+        fAspects = aspectList;
+    }
+
+    @Override
+    public Iterable<ITmfEventAspect> getEventAspects() {
+        return fAspects;
+    }
+
+
+
 }
diff --git a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/package-info.java b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/package-info.java
new file mode 100644 (file)
index 0000000..70a7178
--- /dev/null
@@ -0,0 +1,14 @@
+/*******************************************************************************
+ * Copyright (c) 2014 É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
+ *
+ * Contributors:
+ *    École Polytechnique de Montréal - Initial API and implementation
+ *******************************************************************************/
+
+@org.eclipse.jdt.annotation.NonNullByDefault
+package org.eclipse.tracecompass.tmf.tests.stubs.trace.xml;
\ No newline at end of file
index 2912f319cd1d63bc887c5fc245430e6d4266436d..e7874e642bda1b244bd76bd36d34c58fac082a21 100644 (file)
@@ -1,17 +1,29 @@
 <trace>
-<event timestamp="1" name="A" source="1">
+<event timestamp="0" name="set_aspects">
+<field name="cpu" value="1" type="int" />
+<field name="test" value="abc" type="string" />
+</event>
+<event timestamp="1" name="A">
+<field name="cpu" value="1" type="int" />
+<field name="test" value="abc" type="string" />
 <field name="b" value="1" type="int" />
 <field name="d" value="3" type="int" />
 </event>
-<event timestamp="3" name="B" source="2">
+<event timestamp="3" name="B">
+<field name="cpu" value="1" type="int" />
+<field name="test" value="abc" type="string" />
 <field name="f" value="c" type="string" />
 <field name="g" value="e" type="string" />
 </event>
-<event timestamp="4" name="A" source="1">
+<event timestamp="4" name="A">
+<field name="cpu" value="2" type="int" />
+<field name="test" value="def" type="string" />
 <field name="b" value="3" type="int" />
 <field name="d" value="7" type="int" />
 </event>
-<event timestamp="5" name="B" source="2">
+<event timestamp="5" name="B">
+<field name="cpu" value="1" type="int" />
+<field name="test" value="def" type="string" />
 <field name="f" value="c" type="string" />
 <field name="g" value="e" type="string" />
 </event>
This page took 0.049677 seconds and 5 git commands to generate.