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;
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;
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: {
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;
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;
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;
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.
*
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.
/* 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$
}
}
@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();
}
@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));
} 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;
}
@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) {
if (content == null) {
return null;
}
+
String fieldString = getStringValue(content, FIELD_NAMES_FIELD);
String valueString = getStringValue(content, VALUES_FIELD);
String typeString = getStringValue(content, TYPES_FIELD);
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.
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;
+ }
+
+
+
}