From 69ff4f2614f694ff1d84fc7673cd2f527597a3c9 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Genevi=C3=A8ve=20Bastien?= Date: Thu, 20 Nov 2014 13:47:14 -0500 Subject: [PATCH] TMF: Add support of aspects to TmfXmlStubTrace MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Change-Id: Id729a9284477a44fd1e142e6fd19e3bad3b1894a Signed-off-by: Geneviève Bastien Reviewed-on: https://git.eclipse.org/r/36803 Tested-by: Hudson CI Reviewed-by: Matthew Khouzam --- .../META-INF/MANIFEST.MF | 3 +- .../plugin.xml | 7 ++ .../tests/trace/stub/XmlStubTraceTest.java | 58 +++++++++ .../tmf/tests/stubs/trace/xml/Messages.java | 7 +- .../trace/xml/TmfXmlDevelopmentTrace.xml | 3 - .../trace/xml/TmfXmlDevelopmentTrace.xsd | 1 - .../stubs/trace/xml/TmfXmlTraceStub.java | 110 ++++++++++++++++-- .../tests/stubs/trace/xml/package-info.java | 14 +++ .../testfiles/stub_xml_traces/valid/test.xml | 20 +++- 9 files changed, 201 insertions(+), 22 deletions(-) create mode 100644 org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/package-info.java diff --git a/org.eclipse.tracecompass.tmf.core.tests/META-INF/MANIFEST.MF b/org.eclipse.tracecompass.tmf.core.tests/META-INF/MANIFEST.MF index 4b03dbc5aa..1784f17c23 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/META-INF/MANIFEST.MF +++ b/org.eclipse.tracecompass.tmf.core.tests/META-INF/MANIFEST.MF @@ -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" diff --git a/org.eclipse.tracecompass.tmf.core.tests/plugin.xml b/org.eclipse.tracecompass.tmf.core.tests/plugin.xml index c7f9461521..077c0ec294 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/plugin.xml +++ b/org.eclipse.tracecompass.tmf.core.tests/plugin.xml @@ -113,6 +113,13 @@ name="Test Syslog" trace_type="org.eclipse.tracecompass.tmf.tests.stubs.trace.text.SyslogTrace"> + + diff --git a/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java b/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java index 91dbb54991..15361b6c3a 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java +++ b/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/trace/stub/XmlStubTraceTest.java @@ -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: { diff --git a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/Messages.java b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/Messages.java index 30b12bd310..5256634b86 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/Messages.java +++ b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/Messages.java @@ -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); diff --git a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xml b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xml index 96ec2b9b5d..3a94a57720 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xml +++ b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xml @@ -8,9 +8,6 @@ - - - diff --git a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xsd b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xsd index fe8abdfd9e..b77f0f9a1d 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xsd +++ b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlDevelopmentTrace.xsd @@ -27,7 +27,6 @@ - diff --git a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java index 04aec45d13..85cc45fc08 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java +++ b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/TmfXmlTraceStub.java @@ -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 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 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 type) throws TmfTraceException { + public void initTrace(@Nullable IResource resource, @Nullable String path, @Nullable Class 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 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 aspectList = builder.build(); + fAspects = aspectList; + } + + @Override + public Iterable 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 index 0000000000..70a7178204 --- /dev/null +++ b/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/trace/xml/package-info.java @@ -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 diff --git a/org.eclipse.tracecompass.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml b/org.eclipse.tracecompass.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml index 2912f319cd..e7874e642b 100644 --- a/org.eclipse.tracecompass.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml +++ b/org.eclipse.tracecompass.tmf.core.tests/testfiles/stub_xml_traces/valid/test.xml @@ -1,17 +1,29 @@ - + + + + + + + - + + + - + + + - + + + -- 2.34.1