| author | Alexandre Montplaisir | 2012-03-14 16:55:48 (EDT) |
|---|---|---|
| committer | Francois Chouinard | 2012-03-19 17:43:45 (EDT) |
| commit | 4cc0a4c7e7e0f17b65f9ee96a7ae880f54e81f59 (patch) (side-by-side diff) | |
| tree | cc139adfeb6c560022a2a5405c3b35caa81c26d3 | |
| parent | b5bf72ca186cc78f842e637580b64a74c36f38c4 (diff) | |
| download | org.eclipse.linuxtools-4cc0a4c7e7e0f17b65f9ee96a7ae880f54e81f59.zip org.eclipse.linuxtools-4cc0a4c7e7e0f17b65f9ee96a7ae880f54e81f59.tar.gz org.eclipse.linuxtools-4cc0a4c7e7e0f17b65f9ee96a7ae880f54e81f59.tar.bz2 | |
Integrate CtfAdaptor package in TMF
8 files changed, 1279 insertions, 1 deletions
diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java new file mode 100644 index 0000000..b3d5d45 --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/CtfTmfEventTest.java @@ -0,0 +1,168 @@ +package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; + +import static org.junit.Assert.*; + +import java.io.FileNotFoundException; + +import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator; +import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; +import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; +import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +/** + * The class <code>CTFEventTest</code> contains tests for the class + * <code>{@link CTFEvent}</code>. + * + * @author ematkho + * @version $Revision: 1.0 $ + */ +public class CtfTmfEventTest { + + private CtfTmfEvent fixture; + + /** + * Launch the test. + * + * @param args + * the command line arguments + */ + public static void main(String[] args) { + new org.junit.runner.JUnitCore().run(CtfTmfEventTest.class); + } + + /** + * Perform pre-test initialization. + * + * @throws FileNotFoundException + */ + @Before + public void setUp() throws FileNotFoundException { + CtfTmfTrace trace = TestParams.createTrace(); + CtfIterator tr = new CtfIterator(trace); + tr.advance(); + fixture = tr.getCurrentEvent(); + } + + /** + * Perform post-test clean-up. + */ + @After + public void tearDown() { + // Add additional tear down code here + } + + /** + * Run the CTFEvent(EventDefinition,StreamInputReader) constructor test. + */ + @Test + public void testCTFEvent_read() { + assertNotNull(fixture); + } + + /** + * Run the int getCPU() method test. + */ + @Test + public void testGetCPU() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + int result = nullEvent.getCPU(); + + assertEquals(-1, result); + } + + /** + * Run the String getChannelName() method test. + */ + @Test + public void testGetChannelName() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + String result = nullEvent.getChannelName(); + + assertEquals("No stream", result); //$NON-NLS-1$ + } + + /** + * Run the String getEventName() method test. + */ + @Test + public void testGetEventName() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + String result = nullEvent.getEventName(); + + assertEquals("Empty CTF event", result); //$NON-NLS-1$ + } + + /** + * Run the ArrayList<String> getFieldNames() method test. + */ + @Test + public void testGetFieldNames() { + String[] result = fixture.getContent().getFieldNames(); + assertNotNull(result); + } + + /** + * Run the Object getFieldValue(String) method test. + */ + @Test + public void testGetFieldValue() { + String fieldName = "id"; //$NON-NLS-1$ + Object result = fixture.getContent().getField(fieldName).getValue(); + + assertNotNull(result); + } + + /** + * Run the HashMap<String, CTFEventField> getFields() method test. + */ + @Test + public void testGetFields() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + ITmfEventField[] fields = nullEvent.getContent().getFields(); + + assertArrayEquals(null, fields); + } + + /** + * Run the long getID() method test. + */ + @Test + public void testGetID() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + long result = nullEvent.getID(); + + assertEquals(-1L, result); + } + + + /** + * Run the CTFEvent getNullEvent() method test. + */ + @Test + public void testGetNullEvent() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + + assertNotNull(nullEvent); + assertEquals(-1, nullEvent.getCPU()); + assertEquals("Empty CTF event", nullEvent.getEventName()); //$NON-NLS-1$ + assertEquals("No stream", nullEvent.getChannelName()); //$NON-NLS-1$ + assertArrayEquals(null, nullEvent.getContent().getFields()); + assertEquals(-1L, nullEvent.getID()); + assertEquals(-1L, nullEvent.getTimestampValue()); + } + + /** + * Run the long getTimestamp() method test. + * + */ + @Test + public void testGetTimestamp() { + CtfTmfEvent nullEvent = CtfTmfEvent.getNullEvent(); + long result = nullEvent.getTimestampValue(); + + assertEquals(-1L, result); + } +} diff --git a/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/TestParams.java b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/TestParams.java new file mode 100644 index 0000000..5deda66 --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core.tests/src/org/eclipse/linuxtools/tmf/core/tests/ctfadaptor/TestParams.java @@ -0,0 +1,34 @@ +package org.eclipse.linuxtools.tmf.core.tests.ctfadaptor; + +import java.io.File; +import java.io.FileNotFoundException; + +import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent; +import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace; + + +public abstract class TestParams { + + /* Path to test traces */ + private static final String testTracePath1 = "Tests/traces/trace20m"; //$NON-NLS-1$ + private static CtfTmfTrace testTrace1 = null; + + private static final File emptyFile = new File(""); //$NON-NLS-1$ + private static CtfTmfTrace emptyTrace = new CtfTmfTrace(); + + public static File getEmptyFile() { + return emptyFile; + } + + public static CtfTmfTrace getEmptyTrace() { + return emptyTrace; + } + + public static CtfTmfTrace createTrace() throws FileNotFoundException { + if ( testTrace1 == null ) { + testTrace1 = new CtfTmfTrace(); + testTrace1.initTrace("test-trace", testTracePath1, CtfTmfEvent.class); //$NON-NLS-1$ + } + return testTrace1; + } +} diff --git a/lttng/org.eclipse.linuxtools.tmf.core/META-INF/MANIFEST.MF b/lttng/org.eclipse.linuxtools.tmf.core/META-INF/MANIFEST.MF index 201129f..41fe314 100644 --- a/lttng/org.eclipse.linuxtools.tmf.core/META-INF/MANIFEST.MF +++ b/lttng/org.eclipse.linuxtools.tmf.core/META-INF/MANIFEST.MF @@ -6,11 +6,13 @@ Bundle-Version: 0.5.0.qualifier Bundle-Activator: org.eclipse.linuxtools.tmf.core.TmfCorePlugin Bundle-Vendor: %Bundle-Vendor Require-Bundle: org.eclipse.core.runtime;bundle-version="3.7.0", - org.eclipse.core.resources;bundle-version="3.7.100" + org.eclipse.core.resources;bundle-version="3.7.100", + org.eclipse.linuxtools.ctf.core;bundle-version="0.1.0" Bundle-RequiredExecutionEnvironment: JavaSE-1.6 Bundle-ActivationPolicy: lazy Export-Package: org.eclipse.linuxtools.tmf.core, org.eclipse.linuxtools.tmf.core.component, + org.eclipse.linuxtools.tmf.core.ctfadaptor, org.eclipse.linuxtools.tmf.core.event, org.eclipse.linuxtools.tmf.core.experiment, org.eclipse.linuxtools.tmf.core.filter, diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java new file mode 100644 index 0000000..8a7a12b --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfIterator.java @@ -0,0 +1,135 @@ +package org.eclipse.linuxtools.tmf.core.ctfadaptor; + +import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader; +import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; +import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; +import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; + +public class CtfIterator extends CTFTraceReader implements ITmfContext, + Comparable<CtfIterator> { + + private final CtfTmfTrace ctfTmfTrace; + + private CtfLocation curLocation; + private long curRank; + + /** + * Create a new CTF trace iterator, which initially points at the first + * event in the trace. + * + * @param trace + */ + public CtfIterator(CtfTmfTrace trace) { + super(trace.getCTFTrace()); + this.ctfTmfTrace = trace; + + // FIXME put the real stuff here... + this.curLocation = new CtfLocation(trace.getStartTime()); + this.curRank = 0; + } + + public CtfIterator(CtfTmfTrace trace, long timestampValue, long rank) { + super(trace.getCTFTrace()); + this.ctfTmfTrace = trace; + this.curLocation = (new CtfLocation( + this.getCurrentEvent().getTimestampValue())); + if (this.getCurrentEvent().getTimestampValue() != timestampValue) { + this.seek(timestampValue); + } + + this.curRank = rank; + } + + public CtfTmfTrace getCtfTmfTrace() { + return ctfTmfTrace; + } + + public CtfTmfEvent getCurrentEvent() { + StreamInputReader top = super.prio.peek(); + if (top != null) { + return new CtfTmfEvent(top.getCurrentEvent(), top, ctfTmfTrace); + } + return null; + } + + @Override + public boolean seek(long timestamp) { + boolean ret = false; + ret = super.seek(timestamp); + + if (ret) { + curLocation.setLocation(getCurrentEvent().getTimestampValue()); + } + return ret; + } + + @Override + public long getRank() { + final CtfTmfEvent current = getCurrentEvent(); + if (current != null) { + return getCurrentEvent().getRank(); + } + return 0; + } + + @Override + public void setRank(long rank) { + // FIXME NYI + } + + /* + * (non-Javadoc) + * + * @see org.eclipse.linuxtools.tmf.core.trace.TmfContext#clone() + */ + @Override + public CtfIterator clone() { + CtfIterator clone = null; + clone = new CtfIterator(ctfTmfTrace, + this.getCurrentEvent().getTimestampValue(), curRank); + return clone; + } + + @Override + public void dispose() { + // FIXME add dispose() stuff to CTFTrace and call it here... + + } + + @Override + public void setLocation(ITmfLocation<?> location) { + // FIXME alex: isn't there a cleaner way than a cast here? + this.curLocation = (CtfLocation) location; + } + + @Override + public CtfLocation getLocation() { + return curLocation; + } + + @Override + public void updateRank(int rank) { + curRank = rank; + } + + @Override + public boolean isValidRank() { + return true; + } + + @Override + public boolean advance() { + return super.advance(); + } + + @Override + public int compareTo(CtfIterator o) { + if (this.getRank() < o.getRank()) { + return -1; + } else if (this.getRank() > o.getRank()) { + return 1; + } + return 0; + } + +}
\ No newline at end of file diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java new file mode 100644 index 0000000..5cab5cb --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfLocation.java @@ -0,0 +1,33 @@ +package org.eclipse.linuxtools.tmf.core.ctfadaptor; + +import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; +import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; + +public class CtfLocation implements ITmfLocation<Long> { + + public CtfLocation(Long location) { + setLocation(location); + } + + public CtfLocation(ITmfTimestamp timestamp) { + setLocation(timestamp.getValue()); + } + + private Long fTimestamp; + + @Override + public void setLocation(Long location) { + this.fTimestamp = location; + } + + @Override + public Long getLocation() { + return this.fTimestamp; + } + + @Override + public CtfLocation clone() { + return new CtfLocation(getLocation()); + } + +} diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java new file mode 100644 index 0000000..65e1e02 --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEvent.java @@ -0,0 +1,278 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * 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: Alexandre Montplaisir - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.core.ctfadaptor; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map.Entry; + +import org.eclipse.linuxtools.ctf.core.event.EventDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.Definition; +import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; +import org.eclipse.linuxtools.ctf.core.trace.StreamInputReader; +import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; +import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; +import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; +import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; +import org.eclipse.linuxtools.tmf.core.event.TmfEventField; +import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; + +/** + * <b><u>CTFEvent</u></b> + * <p> + * This is a wrapper class around CTF's Event Definition/Declaration so that we + * can map all types of Declaration to native Java types. + */ +public final class CtfTmfEvent implements ITmfEvent { + + // ------------------------------------------------------------------------ + // Constants + // ------------------------------------------------------------------------ + + private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ + private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ + + // ------------------------------------------------------------------------ + // Attributes + // ------------------------------------------------------------------------ + + private final CtfTmfTrace fTrace; + private final long timestamp; + private final int sourceCPU; + private final long typeId; + private final String eventName; + private final String fileName; + + private final TmfEventField fContent; + + // ------------------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------------------ + + /** + * Usual CTFEvent constructor, where we read an event from the trace (via + * the StreamInputReader). + * + * @param eventDef + * @param top + */ + public CtfTmfEvent(EventDefinition eventDef, StreamInputReader top, + CtfTmfTrace originTrace) { + this.fTrace = originTrace; + + if (eventDef == null) { + this.timestamp = -1; + this.sourceCPU = -1; + this.typeId = -1; + this.fileName = NO_STREAM; + this.eventName = EMPTY_CTF_EVENT_NAME; + this.fContent = null; + return; + } + + /* Read the base event info */ + // FIXME restore once the CTF parser with clocks gets merged + //Long offset = originTrace.getCTFTrace().getOffset(); + Long offset = 0L; + this.timestamp = eventDef.timestamp + offset; + this.sourceCPU = eventDef.getCPU(); + this.typeId = eventDef.getDeclaration().getId(); + this.eventName = eventDef.getDeclaration().getName(); + this.fileName = top.getStreamInput().getFilename(); + + /* Read the fields */ + this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, + parseFields(eventDef)); + } + + /** + * Extract the field information from the structDefinition haze-inducing + * mess, and put them into something ITmfEventField can cope with. + * + * @param eventDef + * @return + */ + private static CtfTmfEventField[] parseFields(EventDefinition eventDef) { + List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); + + StructDefinition structFields = eventDef.getFields(); + HashMap<String, Definition> definitions = structFields.getDefinitions(); + String curFieldName; + Definition curFieldDef; + CtfTmfEventField curField; + + for (Entry<String, Definition> entry : definitions.entrySet()) { + curFieldName = entry.getKey(); + curFieldDef = entry.getValue(); + curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); + if (curField == null) { +// TmfCorePlugin.getDefault().log( +// "We've parsed an unimplemented field type for event \"" + this.eventName //$NON-NLS-1$ +// + "\", field \"" + curFieldName + "\" of type " + curFieldDef.getClass().toString()); //$NON-NLS-1$ //$NON-NLS-2$ + } + fields.add(curField); + } + + return fields.toArray(new CtfTmfEventField[fields.size()]); + } + + /** + * Copy constructor + * + * @param other + */ + public CtfTmfEvent(CtfTmfEvent other) { + this.fTrace = other.getTrace(); + /* Primitives, those will be copied by value */ + this.timestamp = other.timestamp; + this.sourceCPU = other.sourceCPU; + this.typeId = other.typeId; + + /* Strings are immutable, it's safe to shallow-copy them */ + this.eventName = other.eventName; + this.fileName = other.fileName; + + /* Copy the fields over */ + this.fContent = other.fContent.clone(); + } + + /** + * Inner constructor to create "null" events. Don't use this directly, use + * CTFEvent.getNullEvent(); + */ + private CtfTmfEvent() { + this.fTrace = null; + this.timestamp = -1; + this.sourceCPU = -1; + this.typeId = -1; + this.fileName = NO_STREAM; + this.eventName = EMPTY_CTF_EVENT_NAME; + this.fContent = null; + } + + // ------------------------------------------------------------------------ + // Getters/Setters/Predicates + // ------------------------------------------------------------------------ + + private static CtfTmfEvent nullEvent = null; + + /** + * Get a null event + * + * @return an empty event. + */ + public static CtfTmfEvent getNullEvent() { + if (nullEvent == null) { + nullEvent = new CtfTmfEvent(); + } + return nullEvent; + } + + /** + * Gets the current timestamp of the event + * + * @return the current timestamp (long) + */ + public long getTimestampValue() { + return this.timestamp; + } + + /** + * Gets the cpu core the event was recorded on. + * + * @return the cpu id for a given source. In lttng it's from CPUINFO + */ + public int getCPU() { + return this.sourceCPU; + } + + /** + * Return this event's ID, according to the trace's metadata. Watch out, + * this ID is not constant from one trace to another for the same event + * types! Use "getEventName()" for a constant reference. + * + * @return + */ + public long getID() { + return this.typeId; + } + + /** + * Gets the name of a current event. + * + * @return the event name + */ + public String getEventName() { + return eventName; + } + + /** + * Gets the channel name of a field. + * + * @return the channel name. + */ + public String getChannelName() { + return this.fileName; + } + + @Override + public CtfTmfTrace getTrace() { + return fTrace; + } + + @Override + public long getRank() { + // TODO Auto-generated method stub + return 0; + } + + private ITmfTimestamp fTimestamp = null; + + // TODO Benchmark if the singleton approach is faster than just + // instantiating a final fTimestramp right away at creation time + @Override + public ITmfTimestamp getTimestamp() { + if (fTimestamp == null) { + fTimestamp = new TmfTimestamp(timestamp); + } + return fTimestamp; + } + + @Override + public String getSource() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ITmfEventType getType() { + // TODO Auto-generated method stub + return null; + } + + @Override + public ITmfEventField getContent() { + return fContent; + } + + @Override + public String getReference() { + // TODO Auto-generated method stub + return null; + } + + @Override + public CtfTmfEvent clone() { + return new CtfTmfEvent(this); + } +} diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java new file mode 100644 index 0000000..bf33ef2 --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfEventField.java @@ -0,0 +1,287 @@ +/******************************************************************************* + * Copyright (c) 2011 Ericsson + * + * 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: Matthew Khouzam - Initial API and implementation + * Contributors: Alexendre Montplaisir - Initial API and implementation + *******************************************************************************/ + +package org.eclipse.linuxtools.tmf.core.ctfadaptor; + +import org.eclipse.linuxtools.ctf.core.event.types.ArrayDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.Definition; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.SequenceDeclaration; +import org.eclipse.linuxtools.ctf.core.event.types.SequenceDefinition; +import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition; +import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; + +/** + * <b><u>CTFEventField</u></b> + */ +public abstract class CtfTmfEventField implements ITmfEventField { + + // ------------------------------------------------------------------------ + // Attributes + // ------------------------------------------------------------------------ + + protected final String name; + + // ------------------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------------------ + + protected CtfTmfEventField(String name) { + this.name = name; + } + + // ------------------------------------------------------------------------ + // Getters/Setters/Predicates + // ------------------------------------------------------------------------ + + @Override + public String getName() { + return this.name; + } + + // ------------------------------------------------------------------------ + // Operations + // ------------------------------------------------------------------------ + + public static CtfTmfEventField parseField(Definition fieldDef, + String fieldName) { + CtfTmfEventField field = null; + + /* Determine the Definition type */ + if (fieldDef instanceof IntegerDefinition) { + field = new CTFIntegerField( + ((IntegerDefinition) fieldDef).getValue(), fieldName); + + } else if (fieldDef instanceof StringDefinition) { + field = new CTFStringField( + ((StringDefinition) fieldDef).getValue(), fieldName); + + } else if (fieldDef instanceof ArrayDefinition) { + ArrayDefinition arrayDef = (ArrayDefinition) fieldDef; + ArrayDeclaration arrayDecl = arrayDef.getDeclaration(); + + if (arrayDef.isString()) { + /* This is an array of UTF-8 bytes, a.k.a. a String! */ + field = new CTFStringField(fieldDef.toString(), fieldName); + + } else if (arrayDecl.getElementType() instanceof IntegerDeclaration) { + /* This is a an array of CTF Integers */ + long[] values = new long[arrayDecl.getLength()]; + for (int i = 0; i < arrayDecl.getLength(); i++) { + values[i] = ((IntegerDefinition) arrayDef.getElem(i)).getValue(); + } + field = new CTFIntegerArrayField(values, fieldName); + } + /* Add other types of arrays here */ + + } else if (fieldDef instanceof SequenceDefinition) { + SequenceDefinition seqDef = (SequenceDefinition) fieldDef; + SequenceDeclaration seqDecl = seqDef.getDeclaration(); + + if (seqDef.getLength() == 0) { + /* Some sequences have length = 0. Simply use an empty string */ + field = new CTFStringField("", fieldName); //$NON-NLS-1$ + } else if (seqDef.isString()) { + /* Interpret this sequence as a String */ + field = new CTFStringField(seqDef.toString(), fieldName); + } else if (seqDecl.getElementType() instanceof IntegerDeclaration) { + /* Sequence of integers => CTFIntegerArrayField */ + long[] values = new long[seqDef.getLength()]; + for (int i = 0; i < seqDef.getLength(); i++) { + values[i] = ((IntegerDefinition) seqDef.getElem(i)).getValue(); + } + field = new CTFIntegerArrayField(values, fieldName); + } + /* Add other Sequence types here */ + } + /* Add other field types here */ + + return field; + } + + public static CtfTmfEventField copyFrom(CtfTmfEventField other) { + switch (other.getFieldType()) { + case 0: + return new CTFIntegerField(((CTFIntegerField) other).getValue(), + other.name); + case 1: + return new CTFStringField(((CTFStringField) other).getValue(), + other.name); + case 2: + return new CTFIntegerArrayField( + ((CTFIntegerArrayField) other).getValue(), other.name); + default: + return null; + } + } + + @Override + public CtfTmfEventField clone() { + return CtfTmfEventField.copyFrom(this); + } + + /** + * Return the int representing this field's value type + * + * @return + */ + public abstract int getFieldType(); + + /** + * Return this field's value. You can cast it to the correct type depending + * on what getFieldType says. + * + * @return + */ + @Override + public abstract Object getValue(); + + /** + * @name Other methods defined by ITmfEventField, but not used here: the CTF + * fields do not have sub-fields (yet!) + */ + + @Override + public String[] getFieldNames() { + return null; + } + + @Override + public String getFieldName(int index) { + return null; + } + + @Override + public ITmfEventField[] getFields() { + return null; + } + + @Override + public ITmfEventField getField(String fieldName) { + return null; + } + + @Override + public ITmfEventField getField(int index) { + return null; + } +} + +/** + * <b><u>CTFIntegerField</u></b> + */ +final class CTFIntegerField extends CtfTmfEventField { + + private final long longValue; + + /** + * A CTF "IntegerDefinition" can be an integer of any byte size, so in the + * Java parser this is interpreted as a long. + */ + CTFIntegerField(long longValue, String name) { + super(name); + this.longValue = longValue; + } + + @Override + public int getFieldType() { + return 0; + } + + @Override + public Long getValue() { + return this.longValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return name + '=' + longValue; + } +} + +/** + * <b><u>CTFStringField</u></b> + */ +final class CTFStringField extends CtfTmfEventField { + + private final String strValue; + + CTFStringField(String strValue, String name) { + super(name); + this.strValue = strValue; + } + + @Override + public int getFieldType() { + return 1; + } + + @Override + public String getValue() { + return this.strValue; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return name + '=' + strValue; + } +} + +/** + * <b><u>CTFIntegerArrayField</u></b> + */ +final class CTFIntegerArrayField extends CtfTmfEventField { + + private final long[] longValues; + + CTFIntegerArrayField(long[] longValues, String name) { + super(name); + this.longValues = longValues; + } + + @Override + public int getFieldType() { + return 2; + } + + @Override + public long[] getValue() { + return this.longValues; + } + + @Override + public String toString() { + StringBuffer buffer = new StringBuffer(); + buffer.append("{ "); //$NON-NLS-1$ + + buffer.append(longValues[0]); + for (int i = 1; i < longValues.length; i++) { + buffer.append(", " + longValues[i]); //$NON-NLS-1$ + } + buffer.append('}'); + return name + '=' + buffer.toString(); + } +} + +/* Implement other possible fields types here... */ diff --git a/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java new file mode 100644 index 0000000..047aa64 --- a/dev/null +++ b/lttng/org.eclipse.linuxtools.tmf.core/src/org/eclipse/linuxtools/tmf/core/ctfadaptor/CtfTmfTrace.java @@ -0,0 +1,341 @@ +package org.eclipse.linuxtools.tmf.core.ctfadaptor; + +import java.io.FileNotFoundException; +import java.util.Vector; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; +import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; +import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider; +import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; +import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange; +import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; +import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; +import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; +import org.eclipse.linuxtools.tmf.core.signal.TmfSignal; +import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; +import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; +import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; +import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; +import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint; + +public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements + ITmfTrace<CtfTmfEvent> { + + // ------------------------------------------------------------------------ + // Constants + // ------------------------------------------------------------------------ + + // The default number of events to cache + // TODO: Make the DEFAULT_CACHE_SIZE a preference + public static final int DEFAULT_INDEX_PAGE_SIZE = 50000; + + // ------------------------------------------------------------------------ + // Attributes + // ------------------------------------------------------------------------ + + // the Ctf Trace + private CTFTrace fTrace; + + // The cache page size AND checkpoints interval + protected int fIndexPageSize = DEFAULT_INDEX_PAGE_SIZE; + + // The set of event stream checkpoints (for random access) + private Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>(); + + // The number of events collected + protected long fNbEvents = 0; + + // The time span of the event stream + private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH; + private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG; + + // The trace resource + private IResource fResource; + + /* + * Since in TMF, "traces" can read events, this trace here will have its own + * iterator. The user can instantiate extra iterator if they want to seek at + * many places at the same time. + */ + protected CtfIterator iterator; + + // ------------------------------------------------------------------------ + // Constructors + // ------------------------------------------------------------------------ + + public CtfTmfTrace() { + super(); + } + + @Override + public void initTrace(String name, String path, Class<CtfTmfEvent> eventType) + throws FileNotFoundException { + try { + this.fTrace = new CTFTrace(path); + } catch (CTFReaderException e) { + /* + * If it failed at the init(), we can assume it's because the file + * was not found or was not recognized as a CTF trace. Throw into + * the new type of exception expected by the rest of TMF. + */ + System.err.println("Cannot find file " + path); //$NON-NLS-1$ + throw new FileNotFoundException(e.getMessage()); + } + this.iterator = new CtfIterator(this, 0, 0); + setStartTime(iterator.getCurrentEvent().getTimestamp()); + TmfSignalManager.register(this); + // this.currLocation.setTimestamp(this.fEvent.getTimestamp().getValue()); + // this.fStartTime = new TmfSimpleTimestamp(this.currLocation + // .getLocation().getStartTime()); + // this.fEndTime = new TmfSimpleTimestamp(this.currLocation + // .getLocation().getEndTime()); + // setTimeRange(new TmfTimeRange(this.fStartTime.clone(), + // this.fEndTime.clone())); + } + + @Override + public void initTrace(String name, String path, + Class<CtfTmfEvent> eventType, int cacheSize) + throws FileNotFoundException { + this.initTrace(name, path, eventType); + } + + @Override + public void initTrace(String name, String path, + Class<CtfTmfEvent> eventType, boolean indexTrace) + throws FileNotFoundException { + this.initTrace(name, path, eventType); + } + + @Override + public void initTrace(String name, String path, + Class<CtfTmfEvent> eventType, int cacheSize, boolean indexTrace) + throws FileNotFoundException { + this.initTrace(name, path, eventType); + } + + @Override + public void dispose() { + TmfSignalManager.deregister(this); + } + + @Override + public void broadcast(TmfSignal signal) { + TmfSignalManager.dispatchSignal(signal); + } + + @Override + public boolean validate(IProject project, String path) { + try { + final CTFTrace temp = new CTFTrace(path); + return temp.majortIsSet(); // random test + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + return false; + } + + @Override + public CtfTmfTrace clone() throws CloneNotSupportedException { + CtfTmfTrace clone = null; + clone = (CtfTmfTrace) super.clone(); + clone.setfCheckpoints(this.fCheckpoints); + clone.fStartTime = this.fStartTime.clone(); + clone.fEndTime = this.fEndTime.clone(); + clone.fTrace = this.fTrace; + return clone; + } + + @Override + public ITmfTrace<CtfTmfEvent> copy() { + // FIXME not yet implemented + return null; + } + + // ------------------------------------------------------------------------ + // Accessors + // ------------------------------------------------------------------------ + + /** + * @return the trace path + */ + @Override + public String getPath() { + return this.fTrace.getPath(); + } + + @Override + public String getName() { + String temp[] = this.fTrace.getPath().split( + System.getProperty("file.separator")); //$NON-NLS-1$ + if (temp.length > 2) { + return temp[temp.length - 1]; + } + return temp[0]; + } + + @Override + public int getCacheSize() { + return this.fIndexPageSize; + } + + @Override + public long getNbEvents() { + return this.fNbEvents; + } + + @Override + public TmfTimeRange getTimeRange() { + return new TmfTimeRange(this.fStartTime, this.fEndTime); + } + + @Override + public ITmfTimestamp getStartTime() { + return this.fStartTime; + } + + @Override + public ITmfTimestamp getEndTime() { + return this.fEndTime; + } + + @Override + public ITmfLocation<?> getCurrentLocation() { + return iterator.getLocation(); + } + + @Override + public long getRank(ITmfTimestamp timestamp) { + ITmfContext context = seekEvent(timestamp); + return context.getRank(); + } + + // ------------------------------------------------------------------------ + // Operators + // ------------------------------------------------------------------------ + + protected void setTimeRange(TmfTimeRange range) { + this.fStartTime = range.getStartTime(); + this.fEndTime = range.getEndTime(); + } + + protected void setStartTime(ITmfTimestamp startTime) { + this.fStartTime = startTime; + } + + protected void setEndTime(ITmfTimestamp endTime) { + this.fEndTime = endTime; + } + + // ------------------------------------------------------------------------ + // TmfProvider + // ------------------------------------------------------------------------ + + @Override + public ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent> request) { + if ((request instanceof ITmfEventRequest<?>) + && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<CtfTmfEvent>) request).getRange().getStartTime()) + && (request.getIndex() == 0)) { + ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request).getRange().getStartTime()); + ((ITmfEventRequest<CtfTmfEvent>) request).setStartIndex((int) context.getRank()); + return context; + } + return seekEvent(request.getIndex()); + } + + /** + * The trace reader keeps its own iterator: the "context" parameter here + * will be ignored. + * + * If you wish to specify a new context, instantiate a new CtfIterator and + * seek() it to where you want, and use that to read events. + * + * FIXME merge with getNextEvent below once they both use the same parameter + * type. + */ + @Override + public CtfTmfEvent getNext(ITmfContext context) { + iterator.advance(); + return iterator.getCurrentEvent(); + } + + // ------------------------------------------------------------------------ + // ITmfTrace + // ------------------------------------------------------------------------ + + @Override + public ITmfContext seekLocation(ITmfLocation<?> location) { + iterator.setLocation(location); + return iterator; + } + + @Override + public double getLocationRatio(ITmfLocation<?> location) { + return 0; + } + + @Override + public long getStreamingInterval() { + return 0; + } + + @Override + public ITmfContext seekEvent(ITmfTimestamp timestamp) { + iterator.seek(timestamp.getValue()); + return iterator; + } + + /** + * Seek by rank + */ + @Override + public ITmfContext seekEvent(long rank) { + iterator.setRank(rank); + return iterator; + } + + /** + * Seek rank ratio + */ + @Override + public ITmfContext seekLocation(double ratio) { + iterator.seek((long) (this.fNbEvents * ratio)); + return iterator; + } + + @Override + public CtfTmfEvent getNextEvent(ITmfContext context) { + iterator.advance(); + return iterator.getCurrentEvent(); + } + + @Override + public CtfTmfEvent parseEvent(ITmfContext context) { + return iterator.getCurrentEvent(); + } + + public Vector<TmfCheckpoint> getfCheckpoints() { + return this.fCheckpoints; + } + + public void setfCheckpoints(Vector<TmfCheckpoint> fCheckpoints) { + this.fCheckpoints = fCheckpoints; + } + + @Override + public IResource getResource() { + return this.fResource; + } + + @Override + public void setResource(IResource fResource) { + this.fResource = fResource; + } + + public CTFTrace getCTFTrace() { + return fTrace; + } +} |

