1 /*******************************************************************************
2 * Copyright (c) 2011-2013 Ericsson, Ecole Polytechnique de Montreal and others
4 * All rights reserved. This program and the accompanying materials are made
5 * available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 * Contributors: Simon Marchi - Initial API and implementation
11 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.ctf
.core
.trace
;
14 import java
.io
.IOException
;
15 import java
.nio
.MappedByteBuffer
;
16 import java
.nio
.channels
.FileChannel
.MapMode
;
17 import java
.util
.Collection
;
18 import java
.util
.HashMap
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinitionScope
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.SimpleDatatypeDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
29 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDefinition
;
30 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.EventDeclaration
;
31 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
34 * CTF trace packet reader. Reads the events of a packet of a trace file.
37 * @author Matthew Khouzam
38 * @author Simon Marchi
40 public class StreamInputPacketReader
implements IDefinitionScope
{
42 // ------------------------------------------------------------------------
44 // ------------------------------------------------------------------------
46 /** BitBuffer used to read the trace file. */
47 private final BitBuffer bitBuffer
;
49 /** StreamInputReader that uses this StreamInputPacketReader. */
50 private final StreamInputReader streamInputReader
;
52 /** Trace packet header. */
53 private final StructDefinition tracePacketHeaderDef
;
55 /** Stream packet context definition. */
56 private final StructDefinition streamPacketContextDef
;
58 /** Stream event header definition. */
59 private final StructDefinition streamEventHeaderDef
;
61 /** Stream event context definition.*/
62 private final StructDefinition streamEventContextDef
;
64 /** Maps event ID to event definitions. */
65 private final HashMap
<Long
, EventDefinition
> events
;
67 /** Reference to the index entry of the current packet. */
68 private StreamInputPacketIndexEntry currentPacket
= null;
71 * Last timestamp recorded.
73 * Needed to calculate the complete timestamp values for the events with
76 private long lastTimestamp
= 0;
78 /** CPU id of current packet. */
79 private int currentCpu
= 0;
81 /** number of lost events in this packet */
82 private int lostSoFar
;
84 private int lostEventsInThisPacket
;
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 * Constructs a StreamInputPacketReader.
93 * @param streamInputReader
94 * The StreamInputReader to which this packet reader belongs to.
96 public StreamInputPacketReader(StreamInputReader streamInputReader
) {
97 this.streamInputReader
= streamInputReader
;
99 /* Set the BitBuffer's byte order. */
100 bitBuffer
= new BitBuffer();
101 bitBuffer
.setByteOrder(streamInputReader
.getByteOrder());
103 events
= streamInputReader
.getStreamInput().getStream().getTrace().getEventDefs(streamInputReader
.getStreamInput());
106 /* Create trace packet header definition. */
107 final Stream currentStream
= streamInputReader
.getStreamInput().getStream();
108 StructDeclaration tracePacketHeaderDecl
= currentStream
.getTrace().getPacketHeader();
109 if (tracePacketHeaderDecl
!= null) {
110 tracePacketHeaderDef
= tracePacketHeaderDecl
.createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
112 tracePacketHeaderDef
= null;
115 /* Create stream packet context definition. */
116 StructDeclaration streamPacketContextDecl
= currentStream
.getPacketContextDecl();
117 if (streamPacketContextDecl
!= null) {
118 streamPacketContextDef
= streamPacketContextDecl
.createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
120 streamPacketContextDef
= null;
123 /* Create stream event header definition. */
124 StructDeclaration streamEventHeaderDecl
= currentStream
.getEventHeaderDecl();
125 if (streamEventHeaderDecl
!= null) {
126 streamEventHeaderDef
= streamEventHeaderDecl
.createDefinition(this, "stream.event.header"); //$NON-NLS-1$
128 streamEventHeaderDef
= null;
131 /* Create stream event context definition. */
132 StructDeclaration streamEventContextDecl
= currentStream
.getEventContextDecl();
133 if (streamEventContextDecl
!= null) {
134 streamEventContextDef
= streamEventContextDecl
.createDefinition(this, "stream.event.context"); //$NON-NLS-1$
136 streamEventContextDef
= null;
139 /* Create event definitions */
140 Collection
<IEventDeclaration
> eventDecls
= streamInputReader
.getStreamInput().getStream().getEvents().values();
142 for (IEventDeclaration event
: eventDecls
) {
143 if (!events
.containsKey(event
.getId())) {
144 EventDefinition eventDef
= event
.createDefinition(streamInputReader
);
145 events
.put(event
.getId(), eventDef
);
151 * Dispose the StreamInputPacketReader
154 public void dispose() {
155 bitBuffer
.setByteBuffer(null);
158 // ------------------------------------------------------------------------
159 // Getters/Setters/Predicates
160 // ------------------------------------------------------------------------
163 * Gets the current packet
165 * @return the current packet
167 StreamInputPacketIndexEntry
getCurrentPacket() {
168 return this.currentPacket
;
172 * Gets the steamPacketContext Definition
174 * @return steamPacketContext Definition
176 public StructDefinition
getStreamPacketContextDef() {
177 return this.streamPacketContextDef
;
181 * Gets the stream's event context definition.
183 * @return The streamEventContext definition
185 public StructDefinition
getStreamEventContextDef() {
186 return streamEventContextDef
;
190 * Gets the CPU (core) number
192 * @return the CPU (core) number
194 public int getCPU() {
195 return this.currentCpu
;
199 public String
getPath() {
200 return ""; //$NON-NLS-1$
203 // ------------------------------------------------------------------------
205 // ------------------------------------------------------------------------
208 * Changes the current packet to the given one.
210 * @param currentPacket
211 * The index entry of the packet to switch to.
213 void setCurrentPacket(StreamInputPacketIndexEntry currentPacket
) {
214 this.currentPacket
= currentPacket
;
216 if (this.currentPacket
!= null) {
218 * Change the map of the BitBuffer.
220 MappedByteBuffer bb
= null;
222 bb
= streamInputReader
.getStreamInput().getFileChannel()
223 .map(MapMode
.READ_ONLY
,
224 this.currentPacket
.getOffsetBytes(),
225 (this.currentPacket
.getPacketSizeBits() + 7) / 8);
226 } catch (IOException e
) {
228 * The streamInputReader object is already allocated, so this
229 * shouldn't fail bar some very bad kernel or RAM errors...
234 bitBuffer
.setByteBuffer(bb
);
237 * Read trace packet header.
239 if (tracePacketHeaderDef
!= null) {
240 tracePacketHeaderDef
.read(bitBuffer
);
244 * Read stream packet context.
246 if (getStreamPacketContextDef() != null) {
247 getStreamPacketContextDef().read(bitBuffer
);
250 if (this.getCurrentPacket().getTarget() != null) {
251 this.currentCpu
= (int) this.getCurrentPacket().getTargetId();
254 /* Read number of lost events */
255 lostEventsInThisPacket
= (int) this.getCurrentPacket().getLostEvents();
261 * Use the timestamp begin of the packet as the reference for the
262 * timestamp reconstitution.
264 lastTimestamp
= currentPacket
.getTimestampBegin();
266 bitBuffer
.setByteBuffer(null);
273 * Returns whether it is possible to read any more events from this packet.
275 * @return True if it is possible to read any more events from this packet.
277 public boolean hasMoreEvents() {
278 if (currentPacket
!= null) {
279 return bitBuffer
.position() < currentPacket
.getContentSizeBits();
285 * Reads the next event of the packet into the right event definition.
287 * @return The event definition containing the event data that was just
289 * @throws CTFReaderException
290 * If there was a problem reading the trace
292 public EventDefinition
readNextEvent() throws CTFReaderException
{
293 /* Default values for those fields */
297 if (lostEventsInThisPacket
> lostSoFar
) {
298 EventDefinition eventDef
= EventDeclaration
.getLostEventDeclaration().createDefinition(streamInputReader
);
299 eventDef
.setTimestamp(this.lastTimestamp
);
304 final StructDefinition sehd
= streamEventHeaderDef
;
305 final BitBuffer currentBitBuffer
= bitBuffer
;
307 /* Read the stream event header. */
309 sehd
.read(currentBitBuffer
);
311 /* Check for the event id. */
312 Definition idDef
= sehd
.lookupDefinition("id"); //$NON-NLS-1$
313 if (idDef
instanceof SimpleDatatypeDefinition
) {
314 eventID
= ((SimpleDatatypeDefinition
) idDef
).getIntegerValue();
315 } // else, eventID remains 0
317 /* Get the timestamp from the event header (may be overridden later on) */
318 Definition timestampDef
= sehd
.lookupInteger("timestamp"); //$NON-NLS-1$
319 if (timestampDef
instanceof IntegerDefinition
) {
320 timestamp
= calculateTimestamp((IntegerDefinition
) timestampDef
);
321 } // else timestamp remains 0
323 /* Check for the variant v. */
324 Definition variantDef
= sehd
.lookupDefinition("v"); //$NON-NLS-1$
325 if (variantDef
instanceof VariantDefinition
) {
327 /* Get the variant current field */
328 StructDefinition variantCurrentField
= (StructDefinition
) ((VariantDefinition
) variantDef
).getCurrentField();
331 * Try to get the id field in the current field of the variant.
332 * If it is present, it overrides the previously read event id.
334 Definition idIntegerDef
= variantCurrentField
.lookupDefinition("id"); //$NON-NLS-1$
335 if (idIntegerDef
instanceof IntegerDefinition
) {
336 eventID
= ((IntegerDefinition
) idIntegerDef
).getValue();
339 /* Get the timestamp. This would overwrite any previous timestamp definition */
340 timestampDef
= variantCurrentField
.lookupDefinition("timestamp"); //$NON-NLS-1$
341 if (timestampDef
instanceof IntegerDefinition
) {
342 timestamp
= calculateTimestamp((IntegerDefinition
) timestampDef
);
347 /* Read the stream event context. */
348 if (streamEventContextDef
!= null) {
349 streamEventContextDef
.read(currentBitBuffer
);
352 /* Get the right event definition using the event id. */
353 EventDefinition eventDef
= events
.get(eventID
);
354 if (eventDef
== null) {
355 throw new CTFReaderException("Incorrect event id : " + eventID
); //$NON-NLS-1$
358 /* Read the event context. */
359 if (eventDef
.getContext() != null) {
360 eventDef
.getContext().read(currentBitBuffer
);
363 /* Read the event fields. */
364 if (eventDef
.getFields() != null) {
365 eventDef
.getFields().read(currentBitBuffer
);
369 * Set the event timestamp using the timestamp calculated by
372 eventDef
.setTimestamp(timestamp
);
378 * Calculates the timestamp value of the event, possibly using the timestamp
379 * from the last event.
381 * @param timestampDef
382 * Integer definition of the timestamp.
383 * @return The calculated timestamp value.
385 private long calculateTimestamp(IntegerDefinition timestampDef
) {
388 int len
= timestampDef
.getDeclaration().getLength();
391 * If the timestamp length is 64 bits, it is a full timestamp.
393 if (timestampDef
.getDeclaration().getLength() == 64) {
394 lastTimestamp
= timestampDef
.getValue();
395 return lastTimestamp
;
399 * Bit mask to keep / remove all old / new bits.
401 majorasbitmask
= (1L << len
) - 1;
404 * If the new value is smaller than the corresponding bits of the last
405 * timestamp, we assume an overflow of the compact representation.
407 newval
= timestampDef
.getValue();
408 if (newval
< (lastTimestamp
& majorasbitmask
)) {
409 newval
= newval
+ (1L << len
);
412 /* Keep only the high bits of the old value */
413 lastTimestamp
= lastTimestamp
& ~majorasbitmask
;
415 /* Then add the low bits of the new value */
416 lastTimestamp
= lastTimestamp
+ newval
;
418 return lastTimestamp
;
422 public Definition
lookupDefinition(String lookupPath
) {
423 // TODO Auto-generated method stub