1 /*******************************************************************************
2 * Copyright (c) 2011-2012 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
.EventDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Definition
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDefinition
;
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
.StructDeclaration
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDefinition
;
29 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.io
.BitBuffer
;
30 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.Stream
;
31 import org
.eclipse
.linuxtools
.internal
.ctf
.core
.trace
.StreamInputPacketIndexEntry
;
34 * <b><u>StreamInputPacketReader</u></b>
36 * Reads the events of a packet of a trace file.
38 class StreamInputPacketReader
implements IDefinitionScope
{
40 // ------------------------------------------------------------------------
42 // ------------------------------------------------------------------------
45 * Reference to the index entry of the current packet.
47 private StreamInputPacketIndexEntry currentPacket
= null;
50 * BitBuffer used to read the trace file.
52 private final BitBuffer bitBuffer
= new BitBuffer();
55 * StreamInputReader that uses this StreamInputPacketReader.
57 private final StreamInputReader streamInputReader
;
60 * Last timestamp recorded.
62 * Needed to calculate the complete timestamp values for the events with
65 private long lastTimestamp
= 0;
68 * Trace packet header.
70 private StructDefinition tracePacketHeaderDef
= null;
73 * Stream packet context definition.
75 private StructDefinition streamPacketContextDef
= null;
78 * Stream event header definition.
80 private StructDefinition streamEventHeaderDef
= null;
83 * Stream event context definition.
85 private StructDefinition streamEventContextDef
= null;
88 * Maps event ID to event definitions.
90 private final HashMap
<Long
, EventDefinition
> events
= new HashMap
<Long
, EventDefinition
>();
93 * CPU id of current packet.
95 private int currentCpu
= 0;
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
102 * Constructs a StreamInputPacketReader.
104 * @param streamInputReader
105 * The StreamInputReader to which this packet reader belongs to.
107 public StreamInputPacketReader(StreamInputReader streamInputReader
) {
108 this.streamInputReader
= streamInputReader
;
111 * Set the BitBuffer's byte order.
113 getBitBuffer().setByteOrder(streamInputReader
.getByteOrder());
116 * Create definitions needed to read the events.
121 // ------------------------------------------------------------------------
123 // ------------------------------------------------------------------------
125 // ------------------------------------------------------------------------
126 // Getters/Setters/Predicates
127 // ------------------------------------------------------------------------
129 /* Getters, setters and stuff. */
131 public StreamInputPacketIndexEntry
getCurrentPacket() {
132 return this.currentPacket
;
135 public StructDefinition
getStreamPacketContextDef() {
136 return this.streamPacketContextDef
;
139 public int getCPU() {
140 return this.currentCpu
;
144 public String
getPath() {
145 return ""; //$NON-NLS-1$
148 // ------------------------------------------------------------------------
150 // ------------------------------------------------------------------------
153 * Creates definitions needed to read events (stream-defined and
156 private void createDefinitions() {
158 * Create trace packet header definition.
160 final Stream currentStream
= getStreamInputReader().getStreamInput().getStream();
161 StructDeclaration tracePacketHeaderDecl
= currentStream
.getTrace().getPacketHeader();
162 if (tracePacketHeaderDecl
!= null) {
163 setTracePacketHeaderDef(tracePacketHeaderDecl
.createDefinition(this,
164 "trace.packet.header")); //$NON-NLS-1$
168 * Create stream packet context definition.
170 StructDeclaration streamPacketContextDecl
= currentStream
.getPacketContextDecl();
171 if (streamPacketContextDecl
!= null) {
172 setStreamPacketContextDef(streamPacketContextDecl
.createDefinition(
173 this, "stream.packet.context")); //$NON-NLS-1$
177 * Create stream event header definition.
179 StructDeclaration streamEventHeaderDecl
= currentStream
.getEventHeaderDecl();
180 if (streamEventHeaderDecl
!= null) {
181 setStreamEventHeaderDef(streamEventHeaderDecl
.createDefinition(this,
182 "stream.event.header")); //$NON-NLS-1$
186 * Create stream event context definition.
188 StructDeclaration streamEventContextDecl
= currentStream
.getEventContextDecl();
189 if (streamEventContextDecl
!= null) {
190 setStreamEventContextDef(streamEventContextDecl
.createDefinition(
191 this, "stream.event.context")); //$NON-NLS-1$
194 createEventDefinitions();
198 * Creates definitions needed to read the event. (event-defined).
200 private void createEventDefinitions() {
201 Collection
<EventDeclaration
> eventDecls
= getStreamInputReader().getStreamInput().getStream().getEvents().values();
204 * Create definitions for each event.
206 for (EventDeclaration event
: eventDecls
) {
207 EventDefinition eventDef
= event
.createDefinition(getStreamInputReader());
209 events
.put(event
.getId(), eventDef
);
214 * Changes the current packet to the given one.
216 * @param currentPacket
217 * The index entry of the packet to switch to.
219 public void setCurrentPacket(StreamInputPacketIndexEntry currentPacket
) {
220 this.currentPacket
= currentPacket
;
222 if (this.currentPacket
!= null) {
224 * Change the map of the BitBuffer.
226 MappedByteBuffer bb
= null;
228 bb
= getStreamInputReader().getStreamInput().getFileChannel().map(
229 MapMode
.READ_ONLY
, this.currentPacket
.getOffsetBytes(),
230 (this.currentPacket
.getPacketSizeBits() + 7) / 8);
231 } catch (IOException e
) {
233 * The streamInputReader object is already allocated, so this
234 * shouldn't fail bar some very bad kernel or RAM errors...
239 getBitBuffer().setByteBuffer(bb
);
242 * Read trace packet header.
244 if (getTracePacketHeaderDef() != null) {
245 getTracePacketHeaderDef().read(getBitBuffer());
249 * Read stream packet context.
251 if (getStreamPacketContextDef() != null) {
252 getStreamPacketContextDef().read(getBitBuffer());
253 Definition cpuiddef
= getStreamPacketContextDef().lookupDefinition("cpu_id"); //$NON-NLS-1$
254 if (cpuiddef
instanceof IntegerDefinition
) {
255 currentCpu
= (int) ((IntegerDefinition
) cpuiddef
).getValue();
260 * Use the timestamp begin of the packet as the reference for the
261 * timestamp reconstitution.
263 lastTimestamp
= currentPacket
.getTimestampBegin();
265 getBitBuffer().setByteBuffer(null);
272 * Returns whether it is possible to read any more events from this packet.
274 * @return True if it is possible to read any more events from this packet.
276 public boolean hasMoreEvents() {
277 if (currentPacket
!= null) {
278 return getBitBuffer().position() < currentPacket
.getContentSizeBits();
284 * Reads the next event of the packet into the right event definition.
286 * @return The event definition containing the event data that was just
288 * @throws CTFReaderException
290 public EventDefinition
readNextEvent() throws CTFReaderException
{
291 /* WARNING: This is very LTTng-specific. */
295 StructDefinition sehd
= getStreamEventHeaderDef(); // acronym for a long variable name
296 BitBuffer currentBitBuffer
= getBitBuffer();
298 * Read the stream event header.
302 sehd
.read(currentBitBuffer
);
305 * Check for an event id.
307 EnumDefinition idEnumDef
= (EnumDefinition
) sehd
.lookupDefinition("id"); //$NON-NLS-1$
308 assert (idEnumDef
!= null);
310 eventID
= idEnumDef
.getIntegerValue();
313 * Check for the variant v.
315 VariantDefinition variantDef
= (VariantDefinition
) sehd
.lookupDefinition("v"); //$NON-NLS-1$
316 assert (variantDef
!= null);
319 * Get the variant current field
321 StructDefinition variantCurrentField
= (StructDefinition
) variantDef
.getCurrentField();
322 assert (variantCurrentField
!= null);
325 * Try to get the id field in the current field of the variant. If
326 * it is present, it overrides the previously read event id.
328 IntegerDefinition idIntegerDef
= (IntegerDefinition
) variantCurrentField
.lookupDefinition("id"); //$NON-NLS-1$
329 if (idIntegerDef
!= null) {
330 eventID
= idIntegerDef
.getValue();
336 IntegerDefinition timestampDef
= (IntegerDefinition
) variantCurrentField
.lookupDefinition("timestamp"); //$NON-NLS-1$
337 assert (timestampDef
!= null);
340 * Calculate the event timestamp.
342 timestamp
= calculateTimestamp(timestampDef
);
346 * Read the stream event context.
348 if (getStreamEventContextDef() != null) {
349 getStreamEventContextDef().read(currentBitBuffer
);
353 * Get the right event definition using the event id.
355 EventDefinition eventDef
= events
.get(eventID
);
356 if (eventDef
== null) {
357 throw new CTFReaderException("Incorrect event id : " + eventID
); //$NON-NLS-1$
361 * Read the event context.
363 if (eventDef
.context
!= null) {
364 eventDef
.context
.read(currentBitBuffer
);
368 * Read the event fields.
370 if (eventDef
.fields
!= null) {
371 eventDef
.fields
.read(currentBitBuffer
);
375 * Set the event timestamp using the timestamp calculated by
378 eventDef
.timestamp
= timestamp
;
384 * Calculates the timestamp value of the event, possibly using the timestamp
385 * from the last event.
387 * @param timestampDef
388 * Integer definition of the timestamp.
389 * @return The calculated timestamp value.
391 private long calculateTimestamp(IntegerDefinition timestampDef
) {
394 int len
= timestampDef
.getDeclaration().getLength();
397 * If the timestamp length is 64 bits, it is a full timestamp.
399 if (timestampDef
.getDeclaration().getLength() == 64) {
400 lastTimestamp
= timestampDef
.getValue();
401 return lastTimestamp
;
405 * Bit mask to keep / remove all old / new bits.
407 majorasbitmask
= (1L << len
) - 1;
410 * If the new value is smaller than the corresponding bits of the last
411 * timestamp, we assume an overflow of the compact representation.
413 newval
= timestampDef
.getValue();
414 if (newval
< (lastTimestamp
& majorasbitmask
)) {
415 newval
= newval
+ (1L << len
);
418 /* Keep only the high bits of the old value */
419 lastTimestamp
= lastTimestamp
& ~majorasbitmask
;
421 /* Then add the low bits of the new value */
422 lastTimestamp
= lastTimestamp
+ newval
;
424 return lastTimestamp
;
428 public Definition
lookupDefinition(String lookupPath
) {
429 // TODO Auto-generated method stub
433 public StructDefinition
getStreamEventContextDef() {
434 return this.streamEventContextDef
;
437 public void setStreamEventContextDef(StructDefinition streamEventContextDef
) {
438 this.streamEventContextDef
= streamEventContextDef
;
441 public StructDefinition
getStreamEventHeaderDef() {
442 return this.streamEventHeaderDef
;
445 public void setStreamEventHeaderDef(StructDefinition streamEventHeaderDef
) {
446 this.streamEventHeaderDef
= streamEventHeaderDef
;
449 public void setStreamPacketContextDef(StructDefinition streamPacketContextDef
) {
450 this.streamPacketContextDef
= streamPacketContextDef
;
453 public StructDefinition
getTracePacketHeaderDef() {
454 return this.tracePacketHeaderDef
;
457 public void setTracePacketHeaderDef(StructDefinition tracePacketHeaderDef
) {
458 this.tracePacketHeaderDef
= tracePacketHeaderDef
;
461 public StreamInputReader
getStreamInputReader() {
462 return this.streamInputReader
;
465 public BitBuffer
getBitBuffer() {