private static final String testTracePath1 = "traces/kernel"; //$NON-NLS-1$
private static CTFTrace testTrace1 = null;
private static CTFTrace testTraceFromFile1 = null;
- private static final File testTraceFile1 = new File(testTracePath1);
+ private static final File testTraceFile1 = new File(testTracePath1+"/channel0_0"); //$NON-NLS-1$
private static final File emptyFile = new File(""); //$NON-NLS-1$
private static CTFTrace emptyTrace = null;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
+import java.util.Map;
import java.util.Vector;
import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
+import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
public class ReadTrace {
final String TRACE_PATH = "traces/kernel";
// Change this to enable text output
- final boolean USE_TEXT = true;
+ final boolean USE_TEXT = false;
final int LOOP_COUNT = 1;
while (traceReader.hasMoreEvents()) {
EventDefinition ed = traceReader.getCurrentEventDef();
nbEvent++;
- if (prev == traceReader.getIndex()) {
- System.out.println("Error on events " + prev);
- }
- prev = traceReader.getIndex();
if (USE_TEXT) {
String output = formatDate(ed.getTimestamp()
+ trace.getOffset());
- System.out.println(traceReader.getIndex() + ", "
+ System.out.println(nbEvent + ", "
+ output + ", " + ed.getDeclaration().getName()
+ ", " + ed.getCPU() + ed.getFields().toString()) ;
}
-
+ long endTime = traceReader.getEndTime();
+ long timestamp = traceReader.getCurrentEventDef().getTimestamp();
traceReader.advance();
}
+ Map<Long, Stream> streams = traceReader.getTrace().getStreams();
}
stop = System.nanoTime();
+
System.out.print('.');
double time = (stop - start) / (double) nbEvent;
benchs.add(time);
}
avg /= benchs.size();
System.out.println("Time to read " + nbEvent + " events = " + avg
- + " events/ns");
+ + " ns/event");
for (Double val : benchs) {
System.out.print(val);
System.out.print(", ");
}
- testSeekIndex(trace);
- testSeekIndex(trace);
- }
-
- /**
- * @return
- */
- private static long getTimestamp(CTFTraceReader fixture) {
- if (fixture.getCurrentEventDef() != null) {
- return fixture.getCurrentEventDef().getTimestamp();
- }
- return Long.MIN_VALUE;
- }
-
- public static void testSeekIndex(CTFTrace trace) {
- CTFTraceReader fixture = new CTFTraceReader(trace);
- long rank = 300000L;
- long timeRank = 4281275394331L;
- long nearEnd = 4287422858132L;
- long seekTime_0;
- long seekIndex_0 = 0;
- long seekNext_300000 = 0;
- long seekIndex_300000 = 0;
- long seekTime_300000 = 0;
- String cr = "\n"; //$NON-NLS-1$
- fixture.seek(0);
- for (int i = 0; i < 100; i++) {
- fixture.advance();
- }
-
- fixture.seek(nearEnd);
- /*
- * we need to read the trace before seeking
- */
- fixture.seek(0);
- seekTime_0 = getTimestamp(fixture);
- for (int i = 0; i < rank; i++) {
- fixture.advance();
- }
- seekNext_300000 = getTimestamp(fixture);
- fixture.seek(timeRank);
- seekTime_300000 = getTimestamp(fixture);
- fixture.seekIndex(0);
- seekIndex_0 = getTimestamp(fixture);
-
- fixture.seekIndex(rank);
- seekIndex_300000 = getTimestamp(fixture);
- System.out.print(cr);
- System.out.println("seek(0) " + seekTime_0 + cr + //$NON-NLS-1$
- "seekIndex(0) " + seekIndex_0 + cr + //$NON-NLS-1$
- "Next(300000) " + seekNext_300000 + cr + //$NON-NLS-1$
- "seek(time(300000)) " + seekTime_300000 + cr + //$NON-NLS-1$
- "seekIndex(300000) " + seekIndex_300000 //$NON-NLS-1$
- );
}
/**
/**
* Perform pre-test initialization.
+ *
* @throws CTFReaderException
*/
@Before
/**
* Run the CTFTraceReader(CTFTrace) constructor test. Open a known good
* trace.
+ *
* @throws CTFReaderException
*/
@Test
/**
* Run the boolean advance() method test. Test advancing when we're at the
* end, so we expect that there is no more events.
- *
- * @throws CTFReaderException
*/
@Test
- public void testAdvance_end() throws CTFReaderException {
+ public void testAdvance_end() {
+ int i = 0;
+ boolean result = fixture.advance();
+ while (result) {
+ result = fixture.advance();
+ i++;
+ }
+ fixture.seek(0);
+ fixture.advance();
fixture.goToLastEvent();
- while (fixture.hasMoreEvents()) {
- fixture.advance();
+ i = 1;
+ result = fixture.advance();
+ while (result) {
+ result = fixture.advance();
+ i++;
}
- boolean result = fixture.advance();
assertFalse(result);
+ assertEquals(i, 1);
}
/**
*
* Both trace reader are different objects, so they shouldn't "equals" each
* other.
+ *
* @throws CTFReaderException
*/
@Test
public void testEquals() throws CTFReaderException {
CTFTraceReader fixture2 = new CTFTraceReader(TestParams.createTrace());
- assertTrue(fixture.equals(fixture2));
+ assertEquals(fixture, fixture2);
}
/**
/**
* Run the getCurrentEventDef() method test. Get the last event's
* definition.
- *
- * @throws CTFReaderException
*/
@Test
- public void testGetCurrentEventDef_last() throws CTFReaderException {
+ public void testGetCurrentEventDef_last() {
fixture.goToLastEvent();
EventDefinition result = fixture.getCurrentEventDef();
assertNotNull(result);
fixture.goToLastEvent();
long ts1 = getTimestamp();
long ts2 = fixture.getEndTime();
- // the end time can be later than the last event.
- assertTrue(ts1 <= ts2);
+ assertEquals(ts1, ts2);
}
/**
}
- /**
- * Run the boolean seek(long) method test.
- * @throws CTFReaderException
- */
- @Test
- public void testSeekIndex() throws CTFReaderException {
- long rank = 30000L;
- long first, second = 0, third , fourth;
- /*
- * we need to read the trace before seeking
- */
- first = getTimestamp();
- for( int i = 0 ; i < 60000; i++ )
- {
- if( i == rank) {
- second = getTimestamp();
- }
- fixture.advance();
- }
- boolean result= fixture.seekIndex(0);
-
- third = getTimestamp();
- boolean result2 = fixture.seekIndex(rank);
- fourth = getTimestamp();
- /*
- * in blocks to see where code coverage fails
- */
- {
- assertTrue(result);
- }
- {
- assertTrue(result2);
- }
- {
- assertEquals( first , third);
- }
- {
- assertEquals( second , fourth);
- }
- }
/**
* @return
*/
private long getTimestamp() {
- return fixture.getCurrentEventDef().getTimestamp();
+ if (fixture.getCurrentEventDef() != null) {
+ return fixture.getCurrentEventDef().getTimestamp()+ this.fixture.getTrace().getOffset();
+ }
+ return -1;
}
}
@Test
public void testStreamInputPacketIndexEntry_1() {
String expectedResult = "StreamInputPacketIndexEntry [offsetBytes=1, " + //$NON-NLS-1$
- "timestampBegin=0, timestampEnd=0, " + //$NON-NLS-1$
- "indexBegin=9223372036854775807, indexEnd=9223372036854775807]"; //$NON-NLS-1$
+ "timestampBegin=0, timestampEnd=0]"; //$NON-NLS-1$
assertNotNull(fixture);
assertEquals(expectedResult, fixture.toString());
@Test
public void testToString() {
String expectedResult = "StreamInputPacketIndexEntry [offsetBytes=1,"+ //$NON-NLS-1$
- " timestampBegin=1, timestampEnd=1, indexBegin=9223372036854775807,"+ //$NON-NLS-1$
- " indexEnd=9223372036854775807]"; //$NON-NLS-1$
+ " timestampBegin=1, timestampEnd=1]"; //$NON-NLS-1$
+
fixture.setContentSizeBits(1);
fixture.setDataOffsetBits(1);
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
-import java.nio.channels.FileChannel;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
* Run the EventDefinition createDefinition(StreamInputReader) method test.
*
* @throws CTFReaderException
+ * @throws FileNotFoundException
*/
@Test
- public void testCreateDefinition() throws CTFReaderException {
+ public void testCreateDefinition() throws CTFReaderException, FileNotFoundException {
StreamInputReader streamInputReader = new StreamInputReader(
new StreamInput(new Stream(TestParams.createTrace()),
- (FileChannel) null, TestParams.getTraceFile()));
+ (new FileInputStream(TestParams.getTraceFile())).getChannel(), TestParams.getTraceFile()));
EventDefinition result = fixture.createDefinition(streamInputReader);
assertNotNull(result);
/*
* index the trace
*/
- s.createIndex();
+ s.setupIndex();
}
}
}
package org.eclipse.linuxtools.ctf.core.trace;
import java.util.Collection;
-import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.Vector;
import org.eclipse.linuxtools.internal.ctf.core.Activator;
import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
-import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
/**
/**
* Array to count the number of event per trace file.
*/
- private int[] eventCountPerTraceFile;
+ private long[] eventCountPerTraceFile;
/**
* Timestamp of the first event in the trace
*/
private long endTime;
-
+
protected void setEndTime(long endTime) {
this.endTime = endTime;
}
- /**
- * Current event index
- */
- private long fIndex;
-
- private final HashMap<Integer, Long> startIndex;
-
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
*/
public CTFTraceReader(CTFTrace trace) {
this.trace = trace;
+ streamInputReaders.clear();
/**
* Create the trace file readers.
populateStreamInputReaderHeap();
/**
- * Get the start Time of this trace
- * bear in mind that the trace could be empty.
+ * Get the start Time of this trace bear in mind that the trace could be
+ * empty.
*/
this.startTime = 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
if (hasMoreEvents()) {
this.startTime = prio.peek().getCurrentEvent().getTimestamp();
this.setEndTime(this.startTime);
- this.fIndex = 0;
}
- startIndex = new HashMap<Integer, Long>();
}
/**
return this.startTime;
}
- /**
- * @return the index
- */
- public long getIndex() {
- return fIndex;
- }
-
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
* For each trace file of the stream.
*/
for (StreamInput streamInput : streamInputs) {
+ streamInput.getIndex().getEntries().clear();
/*
* Create a reader.
*/
/*
* Create the array to count the number of event per trace file.
*/
- this.eventCountPerTraceFile = new int[this.streamInputReaders.size()];
+ this.eventCountPerTraceFile = new long[this.streamInputReaders.size()];
}
/**
* Add each trace file reader in the priority queue, if we are able
* to read an event from it.
*/
+ reader.setParent(this);
if (reader.readNextEvent()) {
this.prio.add(reader);
if (top == null) {
return false;
}
- /*
- * index if needed
- */
- if (hasMoreEvents()) {
- StreamInputPacketReader packetReader = top.getPacketReader();
- boolean packetHasMoreEvents = packetReader.hasMoreEvents();
- StreamInputPacketIndexEntry currentPacket = packetReader
- .getCurrentPacket();
- if (!packetHasMoreEvents) {
- int n = this.streamInputReaders.indexOf(top);
- if (!startIndex.containsKey(n)) {
- startIndex.put(n, 0L);
- }
- currentPacket.setIndexBegin(startIndex.get(n));
- currentPacket.setIndexEnd(fIndex);
- startIndex.put(n, fIndex + 1);
- }
- }
/*
* Read the next event of this reader.
*/
/*
* increment the index
*/
- fIndex++;
- }
- boolean hasMoreEvents = hasMoreEvents();
+ if (top.getCurrentEvent() != null) {
+ this.endTime = Math.max(top.getCurrentEvent().getTimestamp(),
+ this.endTime);
+ }
+ }
/*
* If there is no reader in the queue, it means the trace reader reached
* the end of the trace.
*/
- return hasMoreEvents;
+ return hasMoreEvents();
}
/**
*
* @throws CTFReaderException
*/
- public void goToLastEvent() throws CTFReaderException {
-
- for (StreamInputReader streamInputReader : this.streamInputReaders) {
- /*
- * Seek the trace reader.
- */
- streamInputReader.goToLastEvent();
- }
- int count = prio.size();
- for (int i = 0; i < (count - 1); i++) {
- /*
- * Cull all the streams aside from the last one
- */
- prio.poll();
+ public void goToLastEvent() {
+ seek(this.getEndTime());
+ while (this.prio.size() > 1) {
+ this.advance();
}
}
* Remove all the trace readers from the priority queue
*/
this.prio.clear();
- fIndex = 0;
- long offset = 0;
for (StreamInputReader streamInputReader : this.streamInputReaders) {
/*
* Seek the trace reader.
*/
- offset += streamInputReader.seek(timestamp);
+ streamInputReader.seek(timestamp);
/*
* Add it to the priority queue if there is a current event.
for (StreamInputReader streamInputReader : this.streamInputReaders) {
if (streamInputReader.getCurrentEvent() != null) {
this.prio.add(streamInputReader);
- fIndex = Math.max(fIndex, streamInputReader.getPacketReader()
- .getCurrentPacket().getIndexBegin()
- + offset);
- }
- }
- return hasMoreEvents();
- }
-
- public boolean seekIndex(long index) {
- this.prio.clear();
-
- long tempIndex = Long.MIN_VALUE;
- long tempTimestamp = Long.MIN_VALUE;
- try {
- for (StreamInputReader streamInputReader : this.streamInputReaders) {
- /*
- * Seek the trace reader.
- */
- final long streamIndex = streamInputReader.seekIndex(index);
- if (streamInputReader.getCurrentEvent() != null) {
- tempIndex = Math.max(tempIndex, streamIndex);
- EventDefinition currentEvent = streamInputReader
- .getCurrentEvent();
- /*
- * Maybe we're at the beginning of a trace.
- */
- if (currentEvent == null) {
- streamInputReader.readNextEvent();
- currentEvent = streamInputReader.getCurrentEvent();
- }
- if (currentEvent != null) {
- tempTimestamp = Math.max(tempTimestamp,
- currentEvent.getTimestamp());
- } else {
- /*
- * probably beyond the last event
- */
- tempIndex = goToZero();
- }
- }
-
- }
- } catch (CTFReaderException e) {
- /*
- * Important, if it failed, it's because it's not yet indexed, so we
- * have to manually advance to the right value.
- */
- tempIndex = goToZero();
- }
- for (StreamInputReader streamInputReader : this.streamInputReaders) {
- /*
- * Add it to the priority queue if there is a current event.
- */
-
- if (streamInputReader.getCurrentEvent() != null) {
- this.prio.add(streamInputReader);
- }
- }
- if (tempIndex == Long.MAX_VALUE) {
- tempIndex = 0;
- }
- long pos = tempIndex;
- if (index > tempIndex) {
- /*
- * advance for offset
- */
- while ((prio.peek().getCurrentEvent().getTimestamp() < tempTimestamp)
- && hasMoreEvents()) {
- this.advance();
- }
- for (pos = tempIndex; (pos < index) && hasMoreEvents(); pos++) {
- this.advance();
}
}
- this.fIndex = pos;
return hasMoreEvents();
}
return;
}
- for (int i : this.eventCountPerTraceFile) {
+ for (long i : this.eventCountPerTraceFile) {
numEvents += i;
}
for (int j = 0; j < this.eventCountPerTraceFile.length; j++) {
StreamInputReader se = this.streamInputReaders.get(j);
- int len = (width * this.eventCountPerTraceFile[se.getName()])
+ long len = (width * this.eventCountPerTraceFile[se.getName()])
/ numEvents;
StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$
sb.append('+');
}
- for (int i = len; i < width; i++) {
+ for (long i = len; i < width; i++) {
sb.append(' ');
}
public int hashCode() {
final int prime = 31;
int result = 1;
- result = (prime * result) + (int) (endTime ^ (endTime >>> 32));
result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
result = (prime * result)
+ ((streamInputReaders == null) ? 0 : streamInputReaders
return false;
}
CTFTraceReader other = (CTFTraceReader) obj;
- if (endTime != other.endTime) {
- return false;
- }
- if (startTime != other.startTime) {
- return false;
- }
if (streamInputReaders == null) {
if (other.streamInputReaders != null) {
return false;
* <p>
* Reads the events of a packet of a trace file.
*/
-class StreamInputPacketReader implements IDefinitionScope {
+public class StreamInputPacketReader implements IDefinitionScope {
// ------------------------------------------------------------------------
// Constants
package org.eclipse.linuxtools.ctf.core.trace;
import java.nio.ByteOrder;
-import java.util.ListIterator;
import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
/**
* Iterator on the packet index
*/
- private ListIterator<StreamInputPacketIndexEntry> packetIndexIt;
+ private int packetIndex;
/**
* Reference to the current event of this trace file (iow, the last on that
private int name;
+ private CTFTraceReader parent;
+
+ private final long prevIndex;
+
+ private final boolean isthisthefirsttimewerereadingthisgivenstream;
+
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
public StreamInputReader(StreamInput streamInput) {
this.streamInput = streamInput;
this.packetReader = new StreamInputPacketReader(this);
-
+ this.isthisthefirsttimewerereadingthisgivenstream = true;
/*
* Get the iterator on the packet index.
*/
- this.packetIndexIt = streamInput.getIndex().listIterator();
+ this.packetIndex = 0;
+ this.prevIndex = 0;
/*
* Make first packet the current one.
*/
* @return If an event has been successfully read.
*/
public boolean readNextEvent() {
+
+
/*
* Change packet if needed
*/
if (!this.packetReader.hasMoreEvents()) {
- goToNextPacket();
+ final StreamInputPacketIndexEntry prevPacket = this.packetReader
+ .getCurrentPacket();
+ if (prevPacket != null) {
+ goToNextPacket();
+ final StreamInputPacketIndexEntry currentPacket = this.packetReader
+ .getCurrentPacket();
+ }
}
/*
try {
this.setCurrentEvent(this.packetReader.readNextEvent());
} catch (CTFReaderException e) {
- /* Some problem happened, we'll assume there is no more events */
+ /*
+ * Some problem happened, we'll assume that there are no more
+ * events
+ */
return false;
}
return true;
}
+ final StreamInputPacketIndexEntry currentPacket = this
+ .getPacketReader().getCurrentPacket();
this.setCurrentEvent(null);
return false;
}
* Change the current packet of the packet reader to the next one.
*/
private void goToNextPacket() {
- if (getPacketIndexIt().hasNext()) {
- StreamInputPacketIndexEntry nextPacket = getPacketIndexIt().next();
- this.packetReader.setCurrentPacket(nextPacket);
+ packetIndex++;
+ if (getPacketSize() >= (packetIndex + 1)) {
+ this.packetReader.setCurrentPacket(getPacket());
} else {
- this.packetReader.setCurrentPacket(null);
+ try {
+ if (this.streamInput.addPacketHeaderIndex()) {
+ packetIndex = getPacketSize() - 1;
+ this.packetReader.setCurrentPacket(getPacket());
+
+ } else {
+ this.packetReader.setCurrentPacket(null);
+ }
+
+ } catch (CTFReaderException e) {
+ this.packetReader.setCurrentPacket(null);
+ }
}
}
+ /**
+ * @return
+ */
+ private int getPacketSize() {
+ return streamInput.getIndex().getEntries().size();
+ }
+
/**
* Changes the location of the trace file reader so that the current event
* is the first event with a timestamp greater than the given timestamp.
/*
* Search in the index for the packet to search in.
*/
- this.packetIndexIt = this.streamInput.getIndex().search(timestamp);
+ this.packetIndex = this.streamInput.getIndex().search(timestamp)
+ .previousIndex();
/*
* Switch to this packet.
return offset;
}
- public long seekIndex(long index) throws CTFReaderException {
- /*
- * we need to check if a trace is empty too.
- */
- StreamInputPacketIndexEntry sipie = null;
- /*
- * Search in the index for the packet to search in.
- */
- this.packetIndexIt = this.streamInput.getIndex().searchIndex(index);
- /*
- * Switch to this packet.
- */
- goToNextPacket();
-
- sipie = this.packetReader.getCurrentPacket();
- /*
- * Read the first packet
- */
- readNextEvent();
- /*
- * get the current index
- */
- if (this.packetReader.getCurrentPacket() == null) {
- if( !((sipie.getIndexBegin() == 0) && (sipie.getIndexEnd() == Long.MAX_VALUE))) {
- throw new CTFReaderException(
- "Current packet null in index seek, did you index your trace yet?"); //$NON-NLS-1$
- }
- return 0;
- }
- return this.packetReader.getCurrentPacket().getIndexBegin();
-
- }
-
- public void goToLastEvent() throws CTFReaderException {
+ public void goToLastEvent() {
/*
* Search in the index for the packet to search in.
*/
/*
* if the trace is empty.
*/
- if((len == 0) ||(this.packetReader.hasMoreEvents() == false)) {
+ if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) {
/*
* This means the trace is empty. abort.
*/
/*
* Go to the last packet that contains events.
*/
- for( int pos = len -1 ; pos > 0 ; pos--){
- entry = this.streamInput.getIndex().getEntries().get(pos);
- this.packetReader.setCurrentPacket(entry);
- if(this.packetReader.hasMoreEvents()) {
+ for (int pos = len - 1; pos > 0; pos--) {
+ packetIndex = pos;
+ this.packetReader.setCurrentPacket(getPacket());
+ if (this.packetReader.hasMoreEvents()) {
break;
}
}
this.setCurrentEvent(prevEvent);
}
+ /**
+ * @return the parent
+ */
+ public CTFTraceReader getParent() {
+ return parent;
+ }
+
+ /**
+ * @param parent
+ * the parent to set
+ */
+ public void setParent(CTFTraceReader parent) {
+ this.parent = parent;
+ }
+
public void setCurrentEvent(EventDefinition currentEvent) {
this.currentEvent = currentEvent;
}
/**
* @return the packetIndexIt
*/
- private ListIterator<StreamInputPacketIndexEntry> getPacketIndexIt() {
- return packetIndexIt;
+ private int getPacketIndex() {
+ return packetIndex;
+ }
+
+ private StreamInputPacketIndexEntry getPacket() {
+ return streamInput.getIndex().getEntries().get(getPacketIndex());
}
/**
return packetReader;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#hashCode()
*/
@Override
return result;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
/**
* The packet index of this input
*/
- private final StreamInputPacketIndex index = new StreamInputPacketIndex();
+ private final StreamInputPacketIndex index;
private long timestampEnd;
+ /*
+ * Definition of trace packet header
+ */
+ StructDefinition tracePacketHeaderDef = null;
+
+ /*
+ * Definition of trace stream packet context
+ */
+ StructDefinition streamPacketContextDef = null;
+
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
this.stream = stream;
this.fileChannel = fileChannel;
this.file = file;
+ index = new StreamInputPacketIndex();
}
// ------------------------------------------------------------------------
/**
* Create the index for this trace file.
- *
+ */
+ public void setupIndex() {
+
+
+ /*
+ * The BitBuffer to extract data from the StreamInput
+ */
+ BitBuffer bitBuffer = new BitBuffer();
+ bitBuffer.order(this.getStream().getTrace().getByteOrder());
+
+ /*
+ * Create the definitions we need to read the packet headers + contexts
+ */
+ if (getStream().getTrace().getPacketHeader() != null) {
+ tracePacketHeaderDef = getStream().getTrace().getPacketHeader()
+ .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
+ }
+
+ if (getStream().getPacketContextDecl() != null) {
+ streamPacketContextDef = getStream().getPacketContextDecl()
+ .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
+ }
+
+ }
+
+ public boolean addPacketHeaderIndex() throws CTFReaderException {
+ long currentPos = 0L;
+ if (!index.getEntries().isEmpty()) {
+ StreamInputPacketIndexEntry pos = index.getEntries().lastElement();
+ currentPos = computeNextOffset(pos);
+ }
+ long fileSize = getStreamSize();
+ if (currentPos < fileSize) {
+ BitBuffer bitBuffer = new BitBuffer();
+ bitBuffer.order(this.getStream().getTrace().getByteOrder());
+ StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry(
+ currentPos);
+ createPacketIndexEntry(fileSize, currentPos, packetIndex,
+ tracePacketHeaderDef, streamPacketContextDef, bitBuffer);
+ index.addEntry(packetIndex);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * @param fileSizeBytes
+ * @return
+ */
+ private long getStreamSize() {
+ return file.length();
+ }
+
+ /**
+ * @param fileSizeBytes
+ * @param packetOffsetBytes
+ * @param packetIndex
+ * @param tracePacketHeaderDef
+ * @param streamPacketContextDef
+ * @param bitBuffer
+ * @return
* @throws CTFReaderException
*/
- public void createIndex() throws CTFReaderException {
+ private long createPacketIndexEntry(long fileSizeBytes,
+ long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
+ StructDefinition tracePacketHeaderDef,
+ StructDefinition streamPacketContextDef, BitBuffer bitBuffer)
+ throws CTFReaderException {
+ MappedByteBuffer bb = createPacketBitBuffer(fileSizeBytes,
+ packetOffsetBytes, packetIndex, bitBuffer);
+
/*
- * The size of the file in bytes
+ * Read the trace packet header if it exists.
*/
- long fileSizeBytes = 0;
- try {
- fileSizeBytes = fileChannel.size();
- } catch (IOException e) {
- throw new CTFReaderException(e);
+ if (tracePacketHeaderDef != null) {
+ parseTracePacketHeader(tracePacketHeaderDef, bitBuffer);
}
/*
- * Offset of the current packet in bytes
+ * Read the stream packet context if it exists.
*/
- long packetOffsetBytes = 0;
+ if (streamPacketContextDef != null) {
+ parsePacketContext(fileSizeBytes, streamPacketContextDef,
+ bitBuffer, packetIndex);
+ } else {
+ setPacketContextNull(fileSizeBytes, packetIndex);
+ }
+
+ /* Basic validation */
+ if (packetIndex.getContentSizeBits() > packetIndex.getPacketSizeBits()) {
+ throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
+ }
+ if (packetIndex.getPacketSizeBits() > ((fileSizeBytes - packetIndex
+ .getOffsetBytes()) * 8)) {
+ throw new CTFReaderException(
+ "Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
+ }
+
+ /*
+ * Offset in the file, in bits
+ */
+ packetIndex.setDataOffsetBits(bitBuffer.position());
+
+ /*
+ * Update the counting packet offset
+ */
+ packetOffsetBytes = computeNextOffset(packetIndex);
+ return packetOffsetBytes;
+ }
+
+ /**
+ * @param packetIndex
+ * @return
+ */
+ private static long computeNextOffset(
+ StreamInputPacketIndexEntry packetIndex) {
+ return packetIndex.getOffsetBytes()
+ + ((packetIndex.getPacketSizeBits() + 7) / 8);
+ }
+
+ /**
+ * @param fileSizeBytes
+ * @param packetOffsetBytes
+ * @param packetIndex
+ * @param bitBuffer
+ * @return
+ * @throws CTFReaderException
+ */
+ private MappedByteBuffer createPacketBitBuffer(long fileSizeBytes,
+ long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
+ BitBuffer bitBuffer) throws CTFReaderException {
/*
* Initial size, it should map at least the packet header + context
* size.
* TODO: use a less arbitrary size.
*/
long mapSize = 4096;
-
/*
- * Definition of trace packet header
+ * If there is less data remaining than what we want to map, reduce the
+ * map size.
*/
- StructDefinition tracePacketHeaderDef = null;
+ if ((fileSizeBytes - packetIndex.getOffsetBytes()) < mapSize) {
+ mapSize = fileSizeBytes - packetIndex.getOffsetBytes();
+ }
/*
- * Definition of trace stream packet context
+ * Map the packet.
*/
- StructDefinition streamPacketContextDef = null;
+ MappedByteBuffer bb;
- /*
- * The BitBuffer to extract data from the StreamInput
- */
- BitBuffer bitBuffer = new BitBuffer();
- bitBuffer.order(this.getStream().getTrace().getByteOrder());
+ try {
+ bb = fileChannel.map(MapMode.READ_ONLY, packetOffsetBytes, mapSize);
+ } catch (IOException e) {
+ throw new CTFReaderException(e);
+ }
+ bitBuffer.setByteBuffer(bb);
+ return bb;
+ }
+
+ /**
+ * @param tracePacketHeaderDef
+ * @param bitBuffer
+ * @throws CTFReaderException
+ */
+ private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef,
+ BitBuffer bitBuffer) throws CTFReaderException {
+ tracePacketHeaderDef.read(bitBuffer);
/*
- * Create the definitions we need to read the packet headers + contexts
+ * Check the CTF magic number
*/
- if (getStream().getTrace().getPacketHeader() != null) {
- tracePacketHeaderDef = getStream().getTrace().getPacketHeader()
- .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
- }
+ IntegerDefinition magicDef = (IntegerDefinition) tracePacketHeaderDef
+ .lookupDefinition("magic"); //$NON-NLS-1$
+ if (magicDef != null) {
+ int magic = (int) magicDef.getValue();
+ if (magic != Utils.CTF_MAGIC) {
+ throw new CTFReaderException(
+ "CTF magic mismatch " + Integer.toHexString(magic) + " vs " + Integer.toHexString(Utils.CTF_MAGIC)); //$NON-NLS-1$//$NON-NLS-2$
+ }
- if (getStream().getPacketContextDecl() != null) {
- streamPacketContextDef = getStream().getPacketContextDecl()
- .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
}
/*
- * Scan through the packets of the file.
+ * Check the trace UUID
*/
- while (packetOffsetBytes < fileSizeBytes) {
- /*
- * If there is less data remaining than what we want to map, reduce
- * the map size.
- */
- if ((fileSizeBytes - packetOffsetBytes) < mapSize) {
- mapSize = fileSizeBytes - packetOffsetBytes;
- }
-
- /*
- * Map the packet.
- */
- MappedByteBuffer bb;
- try {
- bb = fileChannel.map(MapMode.READ_ONLY, packetOffsetBytes,
- mapSize);
- } catch (IOException e) {
- throw new CTFReaderException(e);
+ ArrayDefinition uuidDef = (ArrayDefinition) tracePacketHeaderDef
+ .lookupDefinition("uuid"); //$NON-NLS-1$
+ if (uuidDef != null) {
+ byte[] uuidArray = new byte[16];
+
+ for (int i = 0; i < 16; i++) {
+ IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef
+ .getElem(i);
+ uuidArray[i] = (byte) uuidByteDef.getValue();
}
- bitBuffer.setByteBuffer(bb);
- /*
- * Create the index entry
- */
- StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry(
- packetOffsetBytes);
-
- /*
- * Read the trace packet header if it exists.
- */
- if (tracePacketHeaderDef != null) {
- tracePacketHeaderDef.read(bitBuffer);
-
- /*
- * Check the CTF magic number
- */
- IntegerDefinition magicDef = (IntegerDefinition) tracePacketHeaderDef
- .lookupDefinition("magic"); //$NON-NLS-1$
- if (magicDef != null) {
- int magic = (int) magicDef.getValue();
- if (magic != Utils.CTF_MAGIC) {
- throw new CTFReaderException(
- "CTF magic mismatch " + Integer.toHexString(magic) + " vs " + Integer.toHexString(Utils.CTF_MAGIC)); //$NON-NLS-1$//$NON-NLS-2$
- }
-
- }
-
- /*
- * Check the trace UUID
- */
- ArrayDefinition uuidDef = (ArrayDefinition) tracePacketHeaderDef
- .lookupDefinition("uuid"); //$NON-NLS-1$
- if (uuidDef != null) {
- byte[] uuidArray = new byte[16];
-
- for (int i = 0; i < 16; i++) {
- IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef
- .getElem(i);
- uuidArray[i] = (byte) uuidByteDef.getValue();
- }
-
- UUID uuid = Utils.makeUUID(uuidArray);
-
- if (!getStream().getTrace().getUUID().equals(uuid)) {
- throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
- }
- }
-
- /*
- * Check that the stream id did not change
- */
- IntegerDefinition streamIDDef = (IntegerDefinition) tracePacketHeaderDef
- .lookupDefinition("stream_id"); //$NON-NLS-1$
- if (streamIDDef != null) {
- long streamID = streamIDDef.getValue();
-
- if (streamID != getStream().getId()) {
- throw new CTFReaderException(
- "Stream ID changing within a StreamInput"); //$NON-NLS-1$
- }
- }
- }
+ UUID uuid = Utils.makeUUID(uuidArray);
- /*
- * Read the stream packet context if it exists.
- */
- if (streamPacketContextDef != null) {
- streamPacketContextDef.read(bitBuffer);
-
- /*
- * Read the content size in bits
- */
- IntegerDefinition contentSizeDef = (IntegerDefinition) streamPacketContextDef
- .lookupDefinition("content_size"); //$NON-NLS-1$
- if (contentSizeDef != null) {
- packetIndex.setContentSizeBits((int) contentSizeDef
- .getValue());
- } else {
- packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
- }
-
- /*
- * Read the packet size in bits
- */
- IntegerDefinition packetSizeDef = (IntegerDefinition) streamPacketContextDef
- .lookupDefinition("packet_size"); //$NON-NLS-1$
- if (packetSizeDef != null) {
- packetIndex.setPacketSizeBits((int) packetSizeDef
- .getValue());
- } else {
- if (packetIndex.getContentSizeBits() != 0) {
- packetIndex.setPacketSizeBits(packetIndex
- .getContentSizeBits());
- } else {
- packetIndex
- .setPacketSizeBits((int) (fileSizeBytes * 8));
- }
- }
-
- /*
- * Read the begin timestamp
- */
- IntegerDefinition timestampBeginDef = (IntegerDefinition) streamPacketContextDef
- .lookupDefinition("timestamp_begin"); //$NON-NLS-1$
- if (timestampBeginDef != null) {
- packetIndex.setTimestampBegin( timestampBeginDef.getValue());
- }
-
- /*
- * Read the end timestamp
- */
- IntegerDefinition timestampEndDef = (IntegerDefinition) streamPacketContextDef
- .lookupDefinition("timestamp_end"); //$NON-NLS-1$
- if (timestampEndDef != null) {
- packetIndex.setTimestampEnd(timestampEndDef
- .getValue());
- setTimestampEnd(packetIndex.getTimestampEnd());
- }
- } else {
- /*
- * If there is no packet context, infer the content and packet
- * size from the file size (assume that there is only one packet
- * and no padding)
- */
- packetIndex.setContentSizeBits( (int) (fileSizeBytes * 8));
- packetIndex.setPacketSizeBits( (int) (fileSizeBytes * 8));
+ if (!getStream().getTrace().getUUID().equals(uuid)) {
+ throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
}
+ }
- /* Basic validation */
- if (packetIndex.getContentSizeBits() > packetIndex.getPacketSizeBits()) {
- throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
- }
+ /*
+ * Check that the stream id did not change
+ */
+ IntegerDefinition streamIDDef = (IntegerDefinition) tracePacketHeaderDef
+ .lookupDefinition("stream_id"); //$NON-NLS-1$
+ if (streamIDDef != null) {
+ long streamID = streamIDDef.getValue();
- if (packetIndex.getPacketSizeBits() > ((fileSizeBytes - packetIndex.getOffsetBytes()) * 8)) {
+ if (streamID != getStream().getId()) {
throw new CTFReaderException(
- "Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
+ "Stream ID changing within a StreamInput"); //$NON-NLS-1$
}
+ }
+ }
- /*
- * Offset in the file, in bits
- */
- packetIndex.setDataOffsetBits( bitBuffer.position());
+ /**
+ * @param fileSizeBytes
+ * @param packetIndex
+ */
+ private static void setPacketContextNull(long fileSizeBytes,
+ StreamInputPacketIndexEntry packetIndex) {
+ /*
+ * If there is no packet context, infer the content and packet size from
+ * the file size (assume that there is only one packet and no padding)
+ */
+ packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
+ packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8));
+ }
- /*
- * Add the packet index entry to the index
- */
- index.addEntry(packetIndex);
+ /**
+ * @param fileSizeBytes
+ * @param streamPacketContextDef
+ * @param bitBuffer
+ * @param packetIndex
+ */
+ private void parsePacketContext(long fileSizeBytes,
+ StructDefinition streamPacketContextDef, BitBuffer bitBuffer,
+ StreamInputPacketIndexEntry packetIndex) {
+ streamPacketContextDef.read(bitBuffer);
- /*
- * Update the counting packet offset
- */
- packetOffsetBytes += (packetIndex.getPacketSizeBits() + 7) / 8;
+ /*
+ * Read the content size in bits
+ */
+ IntegerDefinition contentSizeDef = (IntegerDefinition) streamPacketContextDef
+ .lookupDefinition("content_size"); //$NON-NLS-1$
+ if (contentSizeDef != null) {
+ packetIndex.setContentSizeBits((int) contentSizeDef.getValue());
+ } else {
+ packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
+ }
+ /*
+ * Read the packet size in bits
+ */
+ IntegerDefinition packetSizeDef = (IntegerDefinition) streamPacketContextDef
+ .lookupDefinition("packet_size"); //$NON-NLS-1$
+ if (packetSizeDef != null) {
+ packetIndex.setPacketSizeBits((int) packetSizeDef.getValue());
+ } else {
+ if (packetIndex.getContentSizeBits() != 0) {
+ packetIndex.setPacketSizeBits(packetIndex.getContentSizeBits());
+ } else {
+ packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8));
+ }
+ }
+
+ /*
+ * Read the begin timestamp
+ */
+ IntegerDefinition timestampBeginDef = (IntegerDefinition) streamPacketContextDef
+ .lookupDefinition("timestamp_begin"); //$NON-NLS-1$
+ if (timestampBeginDef != null) {
+ packetIndex.setTimestampBegin(timestampBeginDef.getValue());
+ }
+
+ /*
+ * Read the end timestamp
+ */
+ IntegerDefinition timestampEndDef = (IntegerDefinition) streamPacketContextDef
+ .lookupDefinition("timestamp_end"); //$NON-NLS-1$
+ if (timestampEndDef != null) {
+ packetIndex.setTimestampEnd(timestampEndDef.getValue());
+ setTimestampEnd(packetIndex.getTimestampEnd());
}
- index.getEntries().get(0).setIndexBegin(0L);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#hashCode()
*/
@Override
return result;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
}
if (!this.entries.isEmpty()) {
- if (entry.getTimestampBegin() < this.entries.lastElement().getTimestampBegin()) {
+ if (entry.getTimestampBegin() < this.entries.lastElement()
+ .getTimestampBegin()) {
throw new CTFReaderException(
"Packets begin timestamp decreasing"); //$NON-NLS-1$
}
}
}
- return this.entries.listIterator(guessI);
- }
- /**
- * Given a rank, this methods returns the first PacketIndexEntry that
- * could include the rank, that is the last packet with a begin
- * rank smaller than the given rank.
- *
- * @param index
- * The rank to look for.
- * @return The StreamInputPacketEntry that corresponds to the packet that
- * includes the given timestamp.
- */
- public ListIterator<StreamInputPacketIndexEntry> searchIndex(final long index) {
- /*
- * Start with min and max covering all the elements.
- */
- int max = this.entries.size() - 1;
- int min = 0;
-
- int guessI;
- StreamInputPacketIndexEntry guessEntry = null;
-
- if (index < 0) {
- throw new IllegalArgumentException("rank is negative"); //$NON-NLS-1$
- }
-
- for (;;) {
- /*
- * Guess in the middle of min and max. The +1 is so that in case
- * (min + 1 == max), we choose the packet at the subscript "max"
- * instead of the one at "min". Otherwise, it would give an infinite
- * loop.
- */
- guessI = (max + min + 1) / 2;
- guessEntry = this.entries.get(guessI);
-
- /*
- * If we reached the point where we focus on a single packet, our
- * search is done.
- */
- if (min == max) {
- break;
- }
-
- if (index < guessEntry.getIndexBegin()) {
- /*
- * If the timestamp if before the begin timestamp, we know that
- * the packet to return is before the guess.
- */
- max = guessI - 1;
- } else if (index >= guessEntry.getIndexBegin()) {
- /*
- * If the timestamp is after the begin timestamp, we know that
- * the packet to return is after the guess or is the guess.
- */
- min = guessI;
- }
- }
-
return this.entries.listIterator(guessI);
}
}
// Attributes
// ------------------------------------------------------------------------
-
+ public static final long INVALID_INDEX = Long.MAX_VALUE;
/**
* Offset of the packet in the file, in bytes
*/
private long timestampEnd = 0;
-
- private long indexBegin = Long.MAX_VALUE;
-
- private long indexEnd = Long.MAX_VALUE;
-
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
return (ts >= timestampBegin) && (ts <= timestampEnd);
}
- boolean includesIndex(long index){
- return (index >= indexBegin) && (index <= indexEnd);
- }
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "StreamInputPacketIndexEntry [offsetBytes=" + offsetBytes //$NON-NLS-1$
+ ", timestampBegin=" + timestampBegin + ", timestampEnd=" //$NON-NLS-1$ //$NON-NLS-2$
- + timestampEnd + ", indexBegin=" + indexBegin + ", indexEnd=" //$NON-NLS-1$ //$NON-NLS-2$
- + indexEnd + "]"; //$NON-NLS-1$
+ + timestampEnd + "]"; //$NON-NLS-1$
}
// ------------------------------------------------------------------------
}
/**
- * @param dataOffsetBits the dataOffsetBits to set
+ * @param dataOffsetBits
+ * the dataOffsetBits to set
*/
public void setDataOffsetBits(int dataOffsetBits) {
this.dataOffsetBits = dataOffsetBits;
}
/**
- * @param packetSizeBits the packetSizeBits to set
+ * @param packetSizeBits
+ * the packetSizeBits to set
*/
public void setPacketSizeBits(int packetSizeBits) {
this.packetSizeBits = packetSizeBits;
}
/**
- * @param contentSizeBits the contentSizeBits to set
+ * @param contentSizeBits
+ * the contentSizeBits to set
*/
public void setContentSizeBits(int contentSizeBits) {
this.contentSizeBits = contentSizeBits;
}
/**
- * @param timestampBegin the timestampBegin to set
+ * @param timestampBegin
+ * the timestampBegin to set
*/
public void setTimestampBegin(long timestampBegin) {
this.timestampBegin = timestampBegin;
}
/**
- * @param timestampEnd the timestampEnd to set
+ * @param timestampEnd
+ * the timestampEnd to set
*/
public void setTimestampEnd(long timestampEnd) {
this.timestampEnd = timestampEnd;
}
- /**
- * @return the indexBegin
- */
- public long getIndexBegin() {
- return indexBegin;
- }
-
- /**
- * @param indexBegin the indexBegin to set
- */
- public void setIndexBegin(long indexBegin) {
- this.indexBegin = indexBegin;
- }
-
- /**
- * @return the indexEnd
- */
- public long getIndexEnd() {
- return indexEnd;
- }
-
- /**
- * @param indexEnd the indexEnd to set
- */
- public void setIndexEnd(long indexEnd) {
- this.indexEnd = indexEnd;
- }
}
assertTrue(result);
}
- /**
- * Run the boolean seekRank(long) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testSeekRank_1()
- throws Exception {
- CtfIterator fixture = new CtfIterator(createTrace());
- CtfLocation ctfLocation = new CtfLocation(Long.valueOf(1));
- ctfLocation.setLocation(Long.valueOf(1));
- fixture.setLocation(ctfLocation);
- fixture.increaseRank();
- long rank = 1L;
-
- boolean result = fixture.seekRank(rank);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // java.lang.NullPointerException
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.createStreamInputReaders(CTFTraceReader.java:152)
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.<init>(CTFTraceReader.java:92)
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator.<init>(CtfIterator.java:40)
- assertTrue(result);
- }
-
- /**
- * Run the boolean seekRank(long) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testSeekRank_2()
- throws Exception {
- CtfIterator fixture = new CtfIterator(createTrace());
- CtfLocation ctfLocation = new CtfLocation(Long.valueOf(1));
- ctfLocation.setLocation(Long.valueOf(1));
- fixture.setLocation(ctfLocation);
- fixture.increaseRank();
- long rank = 1L;
-
- boolean result = fixture.seekRank(rank);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // java.lang.NullPointerException
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.createStreamInputReaders(CTFTraceReader.java:152)
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.<init>(CTFTraceReader.java:92)
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator.<init>(CtfIterator.java:40)
- assertTrue(result);
- }
/**
* Run the void setLocation(ITmfLocation<?>) method test.
// at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator.<init>(CtfIterator.java:40)
}
- /**
- * Run the void setRank(long) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testSetRank_1()
- throws Exception {
- CtfIterator fixture = new CtfIterator(createTrace());
- CtfLocation ctfLocation = new CtfLocation(Long.valueOf(1));
- ctfLocation.setLocation(Long.valueOf(1));
- fixture.setLocation(ctfLocation);
- fixture.increaseRank();
- long rank = 1L;
-
- fixture.setRank(rank);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // java.lang.NullPointerException
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.createStreamInputReaders(CTFTraceReader.java:152)
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.<init>(CTFTraceReader.java:92)
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator.<init>(CtfIterator.java:40)
- }
-
- /**
- * Run the void setRank(long) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testSetRank_2()
- throws Exception {
- CtfIterator fixture = new CtfIterator(createTrace());
- CtfLocation ctfLocation = new CtfLocation(Long.valueOf(1));
- ctfLocation.setLocation(Long.valueOf(1));
- fixture.setLocation(ctfLocation);
- fixture.increaseRank();
- long rank = 1L;
-
- fixture.setRank(rank);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // java.lang.NullPointerException
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.createStreamInputReaders(CTFTraceReader.java:152)
- // at org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader.<init>(CTFTraceReader.java:92)
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfIterator.<init>(CtfIterator.java:40)
- }
/**
* Perform pre-test initialization.
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
-import org.eclipse.linuxtools.internal.tmf.core.request.TmfCoalescedEventRequest;
import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfLocation;
import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfEvent;
import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp;
import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace;
-import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
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.exceptions.TmfTraceException;
-import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
// add additional test code here
assertNotNull(result);
assertEquals(null, result.getEventType());
- assertEquals(50000, result.getCacheSize());
+ assertEquals(1000, result.getCacheSize());
assertEquals(0L, result.getNbEvents());
assertEquals(0L, result.getStreamingInterval());
assertEquals(null, result.getStateSystem());
assertEquals(null, result.getType());
}
- /**
- * Run the ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent>) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testArmRequest_1()
- throws Exception {
- CtfTmfTrace fixture = initTrace();
- @SuppressWarnings({ "rawtypes", "unchecked" })
- ITmfDataRequest<CtfTmfEvent> request = new TmfCoalescedEventRequest(ITmfEvent.class);
-
- ITmfContext result = fixture.armRequest(request);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException: Path must be a valid directory
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace.initTrace(CtfTmfTrace.java:98)
- assertNotNull(result);
- }
-
-
/**
* @return
* @throws TmfTraceException
fixture.initTrace((IResource) null, PATH, CtfTmfEvent.class);
return fixture;
}
-
- /**
- * Run the ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent>) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testArmRequest_2()
- throws Exception {
- CtfTmfTrace fixture = initTrace();
- @SuppressWarnings({ "rawtypes", "unchecked" })
- ITmfDataRequest<CtfTmfEvent> request = new TmfCoalescedEventRequest(ITmfEvent.class);
-
- ITmfContext result = fixture.armRequest(request);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException: Path must be a valid directory
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace.initTrace(CtfTmfTrace.java:98)
- assertNotNull(result);
- }
-
- /**
- * Run the ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent>) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testArmRequest_3()
- throws Exception {
- CtfTmfTrace fixture = initTrace();
- @SuppressWarnings({ "rawtypes", "unchecked" })
- ITmfDataRequest<CtfTmfEvent> request = new TmfCoalescedEventRequest(ITmfEvent.class);
-
- ITmfContext result = fixture.armRequest(request);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException: Path must be a valid directory
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace.initTrace(CtfTmfTrace.java:98)
- assertNotNull(result);
- }
-
- /**
- * Run the ITmfContext armRequest(ITmfDataRequest<CtfTmfEvent>) method test.
- *
- * @throws Exception
- *
- * @generatedBy CodePro at 03/05/12 2:29 PM
- */
- @Test
- public void testArmRequest_4()
- throws Exception {
- CtfTmfTrace fixture = initTrace();
- @SuppressWarnings({ "rawtypes", "unchecked" })
- ITmfDataRequest<CtfTmfEvent> request = new TmfCoalescedEventRequest(ITmfEvent.class);
-
- ITmfContext result = fixture.armRequest(request);
-
- // add additional test code here
- // An unexpected exception was thrown in user code while executing this test:
- // org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException: Path must be a valid directory
- // at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace.initTrace(CtfTmfTrace.java:98)
- assertNotNull(result);
- }
-
-
/**
* Run the void broadcast(TmfSignal) method test.
*
// An unexpected exception was thrown in user code while executing this test:
// org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException: Path must be a valid directory
// at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace.initTrace(CtfTmfTrace.java:98)
- assertEquals(50000, result);
+ assertEquals(1000, result);
}
/**
// An unexpected exception was thrown in user code while executing this test:
// org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException: Path must be a valid directory
// at org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTrace.initTrace(CtfTmfTrace.java:98)
- assertNull(result);
+ assertNotNull(result);
}
/**
public boolean seek(final long timestamp) {
boolean ret = false;
final long offsetTimestamp = timestamp
- - this.getCtfTmfTrace().getCTFTrace().getOffset();
+ - this.getTrace().getOffset();
if (offsetTimestamp < 0) {
ret = super.seek(timestamp);
} else {
return ret;
}
- /**
- * Method seekRank. seeks to a given rank
- * @param rank long the rank to seek to
- * @return boolean
- */
- public boolean seekRank(final long rank) {
- boolean ret = false;
- ret = super.seekIndex(rank);
-
- if (ret) {
- curLocation.setLocation(getCurrentEvent().getTimestampValue());
- } else {
- curLocation = NULL_LOCATION;
- }
- return ret;
- }
-
/**
* Method getRank.
* @return long
import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
-import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
-public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser{
+public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{
//-------------------------------------------
// Fields
return this.fTrace.getEnvironment().get(key);
}
+ //-------------------------------------------
+ // Clocks
+ //-------------------------------------------
+
+ public long getOffset(){
+ if( fTrace != null ) {
+ return fTrace.getOffset();
+ }
+ return 0;
+ }
+
//-------------------------------------------
// Parser
//-------------------------------------------
@Override
- public ITmfEvent parseEvent(ITmfContext context) {
+ public CtfTmfEvent parseEvent(ITmfContext context) {
CtfTmfEvent event = null;
if( context instanceof CtfIterator ){
CtfIterator itt = (CtfIterator) context;
/*******************************************************************************
* Copyright (c) 2011, 2012 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:
* Francois Chouinard - Initial API and implementation
* Bernd Hufmann - Changed to updated histogram data model
// Check if we are outside the time range; if so, just set the current
// event
long timestamp = getTimestamp(event.x);
- if (timestamp < fZoom.getStartTime() || timestamp > fZoom.getEndTime()) {
+ if ((timestamp < fZoom.getStartTime()) || (timestamp > fZoom.getEndTime())) {
super.mouseDown(event);
return;
}
int nbBuckets = event.x - fStartPosition;
long delta = nbBuckets * fScaledData.fBucketDuration;
long newStart = fZoom.getStartTime() + delta;
- if (newStart < getStartTime())
+ if (newStart < getStartTime()) {
newStart = getStartTime();
+ }
long newEnd = newStart + fZoom.getDuration();
if (newEnd > getEndTime()) {
newEnd = getEndTime();
Image rangeRectangleImage = new Image(image.getDevice(), image, SWT.IMAGE_COPY);
GC rangeWindowGC = new GC(rangeRectangleImage);
- if (fScaledData != null && fRangeStartTime != 0) {
+ if ((fScaledData != null) && (fRangeStartTime != 0)) {
drawTimeRangeWindow(rangeWindowGC, rangeRectangleImage);
}
private void drawTimeRangeWindow(GC imageGC, Image image) {
// Map times to histogram coordinates
- long bucketSpan = fScaledData.fBucketDuration;
+ long bucketSpan = Math.max(fScaledData.fBucketDuration,1);
int rangeWidth = (int) (fRangeDuration / bucketSpan);
int left = (int) ((fRangeStartTime - fDataModel.getFirstBucketTime()) / bucketSpan);
imageGC.setLineWidth(1);
imageGC.setLineStyle(SWT.LINE_SOLID);
- int chHalfWidth = ((rangeWidth < 60) ? rangeWidth * 2 / 3 : 40) / 2;
+ int chHalfWidth = ((rangeWidth < 60) ? (rangeWidth * 2) / 3 : 40) / 2;
imageGC.drawLine(center - chHalfWidth, height / 2, center + chHalfWidth, height / 2);
- imageGC.drawLine(center, height / 2 - chHalfWidth, center, height / 2 + chHalfWidth);
+ imageGC.drawLine(center, (height / 2) - chHalfWidth, center, (height / 2) + chHalfWidth);
}
}
/*******************************************************************************
* Copyright (c) 2011, 2012 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:
* Francois Chouinard - Initial API and implementation
- * Bernd Hufmann - Implementation of new interfaces/listeners and support for
+ * Bernd Hufmann - Implementation of new interfaces/listeners and support for
* time stamp in any order
* Francois Chouinard - Moved from LTTng to TMF
*******************************************************************************/
* 2<i>d</i>). This compaction happens as needed as the trace is read.
* <p>
* The model allows for timestamps in not increasing order. The timestamps can
- * be fed to the model in any order. If an event has a timestamp less than the
+ * be fed to the model in any order. If an event has a timestamp less than the
* <i>basetime</i>, the buckets will be moved to the right to account for the
- * new smaller timestamp. The new <i>basetime</i> is a multiple of the bucket
+ * new smaller timestamp. The new <i>basetime</i> is a multiple of the bucket
* duration smaller then the previous <i>basetime</i>. Note that the <i>basetime</i>
* might not be anymore a timestamp of an event. If necessary, the buckets will
- * be compacted before moving to the right. This might be necessary to not
+ * be compacted before moving to the right. This might be necessary to not
* loose any event counts at the end of the buckets array.
* <p>
* The mapping from the model to the UI is performed by the <i>scaleTo()</i>
public static final int DEFAULT_NUMBER_OF_BUCKETS = 16 * 1000;
public static final int REFRESH_FREQUENCY = DEFAULT_NUMBER_OF_BUCKETS;
-
+
// ------------------------------------------------------------------------
// Attributes
// ------------------------------------------------------------------------
private long fLastEventTime;
private long fCurrentEventTime;
private long fTimeLimit;
-
+
// Private listener lists
private final ListenerList fModelListeners;
-
+
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
public HistogramDataModel(HistogramDataModel other) {
fNbBuckets = other.fNbBuckets;
fBuckets = Arrays.copyOf(other.fBuckets, fNbBuckets);
- fBucketDuration = other.fBucketDuration;
+ fBucketDuration = Math.max(other.fBucketDuration,1);
fNbEvents = other.fNbEvents;
fLastBucket = other.fLastBucket;
fFirstBucketTime = other.fFirstBucketTime;
public long getBucketDuration() {
return fBucketDuration;
}
-
+
public long getFirstBucketTime() {
return fFirstBucketTime;
}
public long getStartTime() {
return fFirstEventTime;
}
-
+
public long getEndTime() {
return fLastEventTime;
}
public long getTimeLimit() {
return fTimeLimit;
}
-
+
// ------------------------------------------------------------------------
// Listener handling
// ------------------------------------------------------------------------
-
+
public void addHistogramListener(IHistogramModelListener listener) {
- fModelListeners.add(listener);
+ fModelListeners.add(listener);
}
-
+
public void removeHistogramListener(IHistogramModelListener listener) {
fModelListeners.remove(listener);
}
private void fireModelUpdateNotification() {
fireModelUpdateNotification(0);
}
-
+
private void fireModelUpdateNotification(long count) {
- if (count % REFRESH_FREQUENCY == 0) {
+ if ((count % REFRESH_FREQUENCY) == 0) {
Object[] listeners = fModelListeners.getListeners();
- for (int i = 0; i < listeners.length; i++) {
- IHistogramModelListener listener = (IHistogramModelListener) listeners[i];
+ for (Object listener2 : listeners) {
+ IHistogramModelListener listener = (IHistogramModelListener) listener2;
listener.modelUpdated();
}
}
}
-
+
// ------------------------------------------------------------------------
// Operations
// ------------------------------------------------------------------------
/**
* Sets the current event time
- *
+ *
* @param timestamp
*/
public void setCurrentEvent(long timestamp) {
/**
* Sets the current event time
- *
+ *
* @param timestamp
*/
public void setCurrentEventNotifyListeners(long timestamp) {
fCurrentEventTime = timestamp;
fireModelUpdateNotification();
}
-
+
/**
* Add event to the correct bucket, compacting the if needed.
- *
+ *
* @param timestamp the timestamp of the event to count
*/
@Override
public void countEvent(long eventCount, long timestamp) {
-
+
// Validate
if (timestamp < 0) {
return;
}
-
+
// Set the start/end time if not already done
- if (fLastBucket == 0 && fBuckets[0] == 0 && timestamp > 0) {
+ if ((fLastBucket == 0) && (fBuckets[0] == 0) && (timestamp > 0)) {
fFirstBucketTime = timestamp;
fFirstEventTime = timestamp;
updateEndTime();
}
-
+
if (timestamp < fFirstEventTime) {
fFirstEventTime = timestamp;
}
-
+
if (fLastEventTime < timestamp) {
fLastEventTime = timestamp;
}
-
+
if (timestamp >= fFirstBucketTime) {
// Compact as needed
}
} else {
-
+
// get offset for adjustment
int offset = getOffset(timestamp);
// Compact as needed
- while(fLastBucket + offset >= fNbBuckets) {
+ while((fLastBucket + offset) >= fNbBuckets) {
mergeBuckets();
offset = getOffset(timestamp);
}
-
+
moveBuckets(offset);
fLastBucket = fLastBucket + offset;
- fFirstBucketTime = fFirstBucketTime - offset*fBucketDuration;
+ fFirstBucketTime = fFirstBucketTime - (offset*fBucketDuration);
updateEndTime();
}
-
+
// Increment the right bucket
int index = (int) ((timestamp - fFirstBucketTime) / fBucketDuration);
fBuckets[index]++;
fNbEvents++;
- if (fLastBucket < index)
+ if (fLastBucket < index) {
fLastBucket = index;
-
+ }
+
fireModelUpdateNotification(eventCount);
}
/**
* Scale the model data to the width, height and bar width requested.
- *
+ *
* @param width
* @param height
* @param barWidth
@Override
public HistogramScaledData scaleTo(int width, int height, int barWidth) {
// Basic validation
- if (width <= 0 || height <= 0 || barWidth <= 0)
+ if ((width <= 0) || (height <= 0) || (barWidth <= 0))
+ {
throw new AssertionError("Invalid histogram dimensions (" + width + "x" + height + ", barWidth=" + barWidth + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
+ }
// The result structure
HistogramScaledData result = new HistogramScaledData(width, height, barWidth);
// Scale horizontally
result.fMaxValue = 0;
-
+
int nbBars = width / barWidth;
- int bucketsPerBar = fLastBucket / nbBars + 1;
- result.fBucketDuration = bucketsPerBar * fBucketDuration;
+ int bucketsPerBar = (fLastBucket / nbBars) + 1;
+ result.fBucketDuration = Math.max(bucketsPerBar * fBucketDuration,1);
for (int i = 0; i < nbBars; i++) {
int count = 0;
- for (int j = i * bucketsPerBar; j < (i + 1) * bucketsPerBar; j++) {
- if (fNbBuckets <= j)
+ for (int j = i * bucketsPerBar; j < ((i + 1) * bucketsPerBar); j++) {
+ if (fNbBuckets <= j) {
break;
+ }
count += fBuckets[j];
}
result.fData[i] = count;
result.fLastBucket = i;
- if (result.fMaxValue < count)
+ if (result.fMaxValue < count) {
result.fMaxValue = count;
+ }
}
// Scale vertically
result.fScalingFactor = (double) height / result.fMaxValue;
}
+ fBucketDuration = Math.max(fBucketDuration, 1);
// Set the current event index in the scaled histogram
- if (fCurrentEventTime >= fFirstBucketTime && fCurrentEventTime <= fLastEventTime)
+ if ((fCurrentEventTime >= fFirstBucketTime) && (fCurrentEventTime <= fLastEventTime)) {
result.fCurrentBucket = (int) ((fCurrentEventTime - fFirstBucketTime) / fBucketDuration) / bucketsPerBar;
- else
+ } else {
result.fCurrentBucket = HistogramScaledData.OUT_OF_RANGE_BUCKET;
+ }
result.fFirstBucketTime = fFirstBucketTime;
result.fFirstEventTime = fFirstEventTime;
// ------------------------------------------------------------------------
private void updateEndTime() {
- fTimeLimit = fFirstBucketTime + fNbBuckets * fBucketDuration;
+ fTimeLimit = fFirstBucketTime + (fNbBuckets * fBucketDuration);
}
private void mergeBuckets() {
- for (int i = 0; i < fNbBuckets / 2; i++) {
- fBuckets[i] = fBuckets[2 * i] + fBuckets[2 * i + 1];
+ for (int i = 0; i < (fNbBuckets / 2); i++) {
+ fBuckets[i] = fBuckets[2 * i] + fBuckets[(2 * i) + 1];
}
Arrays.fill(fBuckets, fNbBuckets / 2, fNbBuckets, 0);
fBucketDuration *= 2;
updateEndTime();
- fLastBucket = fNbBuckets / 2 - 1;
+ fLastBucket = (fNbBuckets / 2) - 1;
}
-
+
private void moveBuckets(int offset) {
for(int i = fNbBuckets - 1; i >= offset; i--) {
- fBuckets[i] = fBuckets[i-offset];
+ fBuckets[i] = fBuckets[i-offset];
}
for (int i = 0; i < offset; i++) {
private int getOffset(long timestamp) {
int offset = (int) ((fFirstBucketTime - timestamp) / fBucketDuration);
- if ((fFirstBucketTime - timestamp) % fBucketDuration != 0) {
+ if (((fFirstBucketTime - timestamp) % fBucketDuration) != 0) {
offset++;
}
return offset;