Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
60ae41e1 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
3 | * |
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 | |
8 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
f357bcd4 | 12 | package org.eclipse.tracecompass.ctf.core.trace; |
866e5b51 FC |
13 | |
14 | import java.io.IOException; | |
526c823c | 15 | import java.nio.ByteBuffer; |
b3151232 | 16 | import java.nio.channels.FileChannel.MapMode; |
866e5b51 | 17 | |
a4fa4e36 | 18 | import org.eclipse.jdt.annotation.NonNull; |
733c614c | 19 | import org.eclipse.jdt.annotation.Nullable; |
453a59f0 | 20 | import org.eclipse.tracecompass.ctf.core.CTFReaderException; |
f357bcd4 AM |
21 | import org.eclipse.tracecompass.ctf.core.CTFStrings; |
22 | import org.eclipse.tracecompass.ctf.core.event.EventDefinition; | |
23 | import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration; | |
24 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; | |
25 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; | |
26 | import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope; | |
27 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
30 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; | |
31 | import org.eclipse.tracecompass.ctf.core.event.types.IEventHeaderDeclaration; | |
32 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
33 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition; | |
34 | import org.eclipse.tracecompass.ctf.core.event.types.SimpleDatatypeDefinition; | |
35 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
36 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
37 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDefinition; | |
38 | import org.eclipse.tracecompass.internal.ctf.core.SafeMappedByteBuffer; | |
39 | import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration; | |
40 | import org.eclipse.tracecompass.internal.ctf.core.event.types.composite.EventHeaderDefinition; | |
41 | import org.eclipse.tracecompass.internal.ctf.core.trace.StreamInputPacketIndexEntry; | |
866e5b51 | 42 | |
a4fa4e36 MK |
43 | import com.google.common.collect.ImmutableList; |
44 | ||
866e5b51 | 45 | /** |
d37aaa7f | 46 | * CTF trace packet reader. Reads the events of a packet of a trace file. |
5c7202b5 | 47 | * |
d37aaa7f FC |
48 | * @author Matthew Khouzam |
49 | * @author Simon Marchi | |
866e5b51 | 50 | */ |
d84419e1 | 51 | public class CTFStreamInputPacketReader implements IDefinitionScope, AutoCloseable { |
866e5b51 FC |
52 | |
53 | // ------------------------------------------------------------------------ | |
6bdc9fac | 54 | // Attributes |
866e5b51 FC |
55 | // ------------------------------------------------------------------------ |
56 | ||
f068c622 MK |
57 | private static final int BITS_PER_BYTE = Byte.SIZE; |
58 | ||
6bdc9fac | 59 | /** BitBuffer used to read the trace file. */ |
733c614c MK |
60 | @Nullable |
61 | private BitBuffer fBitBuffer; | |
866e5b51 | 62 | |
6bdc9fac | 63 | /** StreamInputReader that uses this StreamInputPacketReader. */ |
d84419e1 | 64 | private final CTFStreamInputReader fStreamInputReader; |
866e5b51 | 65 | |
6bdc9fac | 66 | /** Trace packet header. */ |
a4fa4e36 | 67 | private final StructDeclaration fTracePacketHeaderDecl; |
866e5b51 | 68 | |
6bdc9fac | 69 | /** Stream packet context definition. */ |
a4fa4e36 | 70 | private final StructDeclaration fStreamPacketContextDecl; |
866e5b51 | 71 | |
6bdc9fac | 72 | /** Stream event header definition. */ |
6c7592e1 | 73 | private final IDeclaration fStreamEventHeaderDecl; |
866e5b51 | 74 | |
c26d0fe0 | 75 | /** Stream event context definition. */ |
a4fa4e36 | 76 | private final StructDeclaration fStreamEventContextDecl; |
866e5b51 | 77 | |
6c7592e1 MK |
78 | private ICompositeDefinition fCurrentTracePacketHeaderDef; |
79 | private ICompositeDefinition fCurrentStreamEventHeaderDef; | |
80 | private ICompositeDefinition fCurrentStreamPacketContextDef; | |
6bdc9fac | 81 | /** Reference to the index entry of the current packet. */ |
2882273c | 82 | private StreamInputPacketIndexEntry fCurrentPacket = null; |
6bdc9fac | 83 | |
866e5b51 | 84 | /** |
6bdc9fac AM |
85 | * Last timestamp recorded. |
86 | * | |
87 | * Needed to calculate the complete timestamp values for the events with | |
88 | * compact headers. | |
866e5b51 | 89 | */ |
2882273c | 90 | private long fLastTimestamp = 0; |
6bdc9fac AM |
91 | |
92 | /** CPU id of current packet. */ | |
2882273c | 93 | private int fCurrentCpu = 0; |
866e5b51 | 94 | |
2882273c | 95 | private int fLostEventsInThisPacket; |
5c7202b5 | 96 | |
2882273c | 97 | private long fLostEventsDuration; |
c26d0fe0 | 98 | |
2882273c | 99 | private boolean fHasLost = false; |
c26d0fe0 | 100 | |
866e5b51 | 101 | // ------------------------------------------------------------------------ |
6bdc9fac | 102 | // Constructors |
866e5b51 FC |
103 | // ------------------------------------------------------------------------ |
104 | ||
105 | /** | |
106 | * Constructs a StreamInputPacketReader. | |
107 | * | |
108 | * @param streamInputReader | |
109 | * The StreamInputReader to which this packet reader belongs to. | |
110 | */ | |
d84419e1 | 111 | public CTFStreamInputPacketReader(CTFStreamInputReader streamInputReader) { |
2882273c | 112 | fStreamInputReader = streamInputReader; |
866e5b51 | 113 | |
6bdc9fac | 114 | /* Set the BitBuffer's byte order. */ |
6c7592e1 MK |
115 | ByteBuffer allocateDirect = ByteBuffer.allocateDirect(0); |
116 | if (allocateDirect == null) { | |
117 | throw new IllegalStateException("Unable to allocate 0 bytes!"); //$NON-NLS-1$ | |
118 | } | |
119 | fBitBuffer = new BitBuffer(allocateDirect); | |
33656d8e | 120 | |
d84419e1 | 121 | final CTFStream currentStream = streamInputReader.getStreamInput().getStream(); |
a4fa4e36 MK |
122 | fTracePacketHeaderDecl = currentStream.getTrace().getPacketHeader(); |
123 | fStreamPacketContextDecl = currentStream.getPacketContextDecl(); | |
6c7592e1 | 124 | fStreamEventHeaderDecl = currentStream.getEventHeaderDeclaration(); |
a4fa4e36 MK |
125 | fStreamEventContextDecl = currentStream.getEventContextDecl(); |
126 | } | |
6bdc9fac | 127 | |
a4fa4e36 MK |
128 | /** |
129 | * Get the event context defintiion | |
130 | * | |
131 | * @param input | |
132 | * the bitbuffer to read from | |
133 | * @return an context definition, can be null | |
134 | * @throws CTFReaderException | |
135 | * out of bounds exception or such | |
a4fa4e36 MK |
136 | */ |
137 | public StructDefinition getEventContextDefinition(@NonNull BitBuffer input) throws CTFReaderException { | |
70f60307 | 138 | return fStreamEventContextDecl.createDefinition(fStreamInputReader.getStreamInput(), LexicalScope.STREAM_EVENT_CONTEXT, input); |
a4fa4e36 | 139 | } |
6bdc9fac | 140 | |
a4fa4e36 MK |
141 | /** |
142 | * Get the stream context defintiion | |
143 | * | |
144 | * @param input | |
145 | * the bitbuffer to read from | |
146 | * @return an context definition, can be null | |
147 | * @throws CTFReaderException | |
148 | * out of bounds exception or such | |
6c7592e1 | 149 | * @deprecated it was not used |
a4fa4e36 | 150 | */ |
6c7592e1 | 151 | @Deprecated |
a4fa4e36 | 152 | public StructDefinition getStreamEventHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException { |
6c7592e1 MK |
153 | if (!(fStreamEventHeaderDecl instanceof StructDeclaration)) { |
154 | throw new IllegalStateException("Definition is not a struct definition, this is a deprecated method that doesn't work so well, stop using it."); //$NON-NLS-1$ | |
155 | } | |
733e6767 | 156 | return ((StructDeclaration) fStreamEventHeaderDecl).createDefinition(this, LexicalScope.STREAM_EVENT_HEADER, input); |
a4fa4e36 | 157 | } |
6bdc9fac | 158 | |
a4fa4e36 MK |
159 | /** |
160 | * Get the packet context defintiion | |
161 | * | |
162 | * @param input | |
163 | * the bitbuffer to read from | |
164 | * @return an context definition, can be null | |
165 | * @throws CTFReaderException | |
166 | * out of bounds exception or such | |
a4fa4e36 MK |
167 | */ |
168 | public StructDefinition getStreamPacketContextDefinition(@NonNull BitBuffer input) throws CTFReaderException { | |
70f60307 | 169 | return fStreamPacketContextDecl.createDefinition(fStreamInputReader.getStreamInput(), LexicalScope.STREAM_PACKET_CONTEXT, input); |
a4fa4e36 | 170 | } |
6bdc9fac | 171 | |
a4fa4e36 MK |
172 | /** |
173 | * Get the event header defintiion | |
174 | * | |
175 | * @param input | |
176 | * the bitbuffer to read from | |
177 | * @return an header definition, can be null | |
178 | * @throws CTFReaderException | |
179 | * out of bounds exception or such | |
a4fa4e36 MK |
180 | */ |
181 | public StructDefinition getTracePacketHeaderDefinition(@NonNull BitBuffer input) throws CTFReaderException { | |
70f60307 | 182 | return fTracePacketHeaderDecl.createDefinition(fStreamInputReader.getStreamInput().getStream().getTrace(), LexicalScope.TRACE_PACKET_HEADER, input); |
6bdc9fac | 183 | } |
866e5b51 | 184 | |
5d1c6919 PT |
185 | /** |
186 | * Dispose the StreamInputPacketReader | |
5d1c6919 | 187 | */ |
dd9752d5 AM |
188 | @Override |
189 | public void close() { | |
733c614c | 190 | fBitBuffer = null; |
5d1c6919 PT |
191 | } |
192 | ||
866e5b51 FC |
193 | // ------------------------------------------------------------------------ |
194 | // Getters/Setters/Predicates | |
195 | // ------------------------------------------------------------------------ | |
196 | ||
9ac2eb62 MK |
197 | /** |
198 | * Gets the current packet | |
199 | * | |
200 | * @return the current packet | |
201 | */ | |
486efb2e | 202 | StreamInputPacketIndexEntry getCurrentPacket() { |
2882273c | 203 | return fCurrentPacket; |
866e5b51 FC |
204 | } |
205 | ||
9ac2eb62 MK |
206 | /** |
207 | * Gets the CPU (core) number | |
208 | * | |
209 | * @return the CPU (core) number | |
210 | */ | |
866e5b51 | 211 | public int getCPU() { |
2882273c | 212 | return fCurrentCpu; |
866e5b51 FC |
213 | } |
214 | ||
215 | @Override | |
a4fa4e36 MK |
216 | public LexicalScope getScopePath() { |
217 | return LexicalScope.PACKET; | |
866e5b51 FC |
218 | } |
219 | ||
220 | // ------------------------------------------------------------------------ | |
221 | // Operations | |
222 | // ------------------------------------------------------------------------ | |
223 | ||
b3151232 MK |
224 | @NonNull |
225 | private ByteBuffer getByteBufferAt(long position, long size) throws CTFReaderException, IOException { | |
f4a474e3 | 226 | ByteBuffer map = SafeMappedByteBuffer.map(fStreamInputReader.getFc(), MapMode.READ_ONLY, position, size); |
b3151232 MK |
227 | if (map == null) { |
228 | throw new CTFReaderException("Failed to allocate mapped byte buffer"); //$NON-NLS-1$ | |
229 | } | |
230 | return map; | |
231 | } | |
f068c622 | 232 | |
866e5b51 FC |
233 | /** |
234 | * Changes the current packet to the given one. | |
235 | * | |
236 | * @param currentPacket | |
237 | * The index entry of the packet to switch to. | |
db8e8f7d AM |
238 | * @throws CTFReaderException |
239 | * If we get an error reading the packet | |
866e5b51 | 240 | */ |
db8e8f7d | 241 | void setCurrentPacket(StreamInputPacketIndexEntry currentPacket) throws CTFReaderException { |
c26d0fe0 | 242 | StreamInputPacketIndexEntry prevPacket = null; |
2882273c | 243 | fCurrentPacket = currentPacket; |
866e5b51 | 244 | |
2882273c | 245 | if (fCurrentPacket != null) { |
866e5b51 FC |
246 | /* |
247 | * Change the map of the BitBuffer. | |
248 | */ | |
526c823c | 249 | ByteBuffer bb = null; |
866e5b51 | 250 | try { |
b3151232 | 251 | bb = getByteBufferAt( |
a4fa4e36 | 252 | fCurrentPacket.getOffsetBytes(), |
f068c622 | 253 | (fCurrentPacket.getPacketSizeBits() + BITS_PER_BYTE - 1) / BITS_PER_BYTE); |
866e5b51 | 254 | } catch (IOException e) { |
cf9a28da | 255 | throw new CTFReaderException(e.getMessage(), e); |
866e5b51 FC |
256 | } |
257 | ||
733c614c MK |
258 | BitBuffer bitBuffer = new BitBuffer(bb); |
259 | fBitBuffer = bitBuffer; | |
866e5b51 FC |
260 | /* |
261 | * Read trace packet header. | |
262 | */ | |
a4fa4e36 | 263 | if (fTracePacketHeaderDecl != null) { |
733c614c | 264 | fCurrentTracePacketHeaderDef = getTracePacketHeaderDefinition(bitBuffer); |
866e5b51 FC |
265 | } |
266 | ||
267 | /* | |
268 | * Read stream packet context. | |
269 | */ | |
a4fa4e36 | 270 | if (fStreamPacketContextDecl != null) { |
733c614c | 271 | fCurrentStreamPacketContextDef = getStreamPacketContextDefinition(bitBuffer); |
33656d8e | 272 | |
132a02b0 | 273 | /* Read CPU ID */ |
2882273c MK |
274 | if (getCurrentPacket().getTarget() != null) { |
275 | fCurrentCpu = (int) getCurrentPacket().getTargetId(); | |
866e5b51 | 276 | } |
21fb02fa | 277 | |
132a02b0 | 278 | /* Read number of lost events */ |
2882273c MK |
279 | fLostEventsInThisPacket = (int) getCurrentPacket().getLostEvents(); |
280 | if (fLostEventsInThisPacket != 0) { | |
281 | fHasLost = true; | |
c26d0fe0 AM |
282 | /* |
283 | * Compute the duration of the lost event time range. If the | |
284 | * current packet is the first packet, duration will be set | |
285 | * to 1. | |
286 | */ | |
287 | long lostEventsStartTime; | |
3f02ac64 | 288 | int index = fStreamInputReader.getStreamInput().getIndex().indexOf(currentPacket); |
c26d0fe0 AM |
289 | if (index == 0) { |
290 | lostEventsStartTime = currentPacket.getTimestampBegin() + 1; | |
291 | } else { | |
3f02ac64 | 292 | prevPacket = fStreamInputReader.getStreamInput().getIndex().getElement(index - 1); |
c26d0fe0 AM |
293 | lostEventsStartTime = prevPacket.getTimestampEnd(); |
294 | } | |
2882273c | 295 | fLostEventsDuration = Math.abs(lostEventsStartTime - currentPacket.getTimestampBegin()); |
c26d0fe0 | 296 | } |
866e5b51 FC |
297 | } |
298 | ||
299 | /* | |
300 | * Use the timestamp begin of the packet as the reference for the | |
301 | * timestamp reconstitution. | |
302 | */ | |
2882273c | 303 | fLastTimestamp = currentPacket.getTimestampBegin(); |
866e5b51 | 304 | } else { |
733c614c | 305 | fBitBuffer = null; |
2882273c | 306 | fLastTimestamp = 0; |
866e5b51 FC |
307 | } |
308 | } | |
309 | ||
310 | /** | |
311 | * Returns whether it is possible to read any more events from this packet. | |
312 | * | |
313 | * @return True if it is possible to read any more events from this packet. | |
314 | */ | |
315 | public boolean hasMoreEvents() { | |
733c614c MK |
316 | BitBuffer bitBuffer = fBitBuffer; |
317 | StreamInputPacketIndexEntry currentPacket = fCurrentPacket; | |
318 | if (currentPacket != null && bitBuffer != null) { | |
319 | return fHasLost || (bitBuffer.position() < currentPacket.getContentSizeBits()); | |
866e5b51 FC |
320 | } |
321 | return false; | |
322 | } | |
323 | ||
324 | /** | |
325 | * Reads the next event of the packet into the right event definition. | |
326 | * | |
327 | * @return The event definition containing the event data that was just | |
328 | * read. | |
329 | * @throws CTFReaderException | |
be6df2d8 | 330 | * If there was a problem reading the trace |
866e5b51 FC |
331 | */ |
332 | public EventDefinition readNextEvent() throws CTFReaderException { | |
1fbaecd1 | 333 | /* Default values for those fields */ |
5f715709 MK |
334 | // compromise since we cannot have 64 bit addressing of arrays yet. |
335 | int eventID = (int) EventDeclaration.UNSET_EVENT_ID; | |
866e5b51 | 336 | long timestamp = 0; |
2882273c MK |
337 | if (fHasLost) { |
338 | fHasLost = false; | |
a4fa4e36 MK |
339 | EventDeclaration lostEventDeclaration = EventDeclaration.getLostEventDeclaration(); |
340 | StructDeclaration lostFields = lostEventDeclaration.getFields(); | |
341 | // this is a hard coded map, we know it's not null | |
2db699c2 | 342 | IntegerDeclaration lostFieldsDecl = (IntegerDeclaration) lostFields.getField(CTFStrings.LOST_EVENTS_FIELD); |
a4fa4e36 MK |
343 | if (lostFieldsDecl == null) |
344 | { | |
345 | throw new IllegalStateException("Lost events count not declared!"); //$NON-NLS-1$ | |
346 | } | |
2db699c2 | 347 | IntegerDeclaration lostEventsDurationDecl = (IntegerDeclaration) lostFields.getField(CTFStrings.LOST_EVENTS_DURATION); |
a4fa4e36 MK |
348 | if (lostEventsDurationDecl == null) { |
349 | throw new IllegalStateException("Lost events duration not declared!"); //$NON-NLS-1$ | |
350 | } | |
351 | IntegerDefinition lostDurationDef = new IntegerDefinition(lostFieldsDecl, null, CTFStrings.LOST_EVENTS_DURATION, fLostEventsDuration); | |
352 | IntegerDefinition lostCountDef = new IntegerDefinition(lostEventsDurationDecl, null, CTFStrings.LOST_EVENTS_FIELD, fLostEventsInThisPacket); | |
353 | IntegerDefinition[] fields = new IntegerDefinition[] { lostCountDef, lostDurationDef }; | |
354 | /* this is weird notation, but it's the java notation */ | |
355 | final ImmutableList<String> fieldNameList = ImmutableList.<String> builder().add(CTFStrings.LOST_EVENTS_FIELD).add(CTFStrings.LOST_EVENTS_DURATION).build(); | |
356 | return new EventDefinition( | |
357 | lostEventDeclaration, | |
358 | fStreamInputReader, | |
359 | fLastTimestamp, | |
360 | null, | |
361 | null, | |
362 | null, | |
363 | new StructDefinition( | |
364 | lostFields, | |
365 | this, "fields", //$NON-NLS-1$ | |
366 | fieldNameList, | |
367 | fields | |
368 | )); | |
369 | ||
33656d8e | 370 | } |
132a02b0 | 371 | |
2882273c | 372 | final BitBuffer currentBitBuffer = fBitBuffer; |
6c7592e1 | 373 | if (currentBitBuffer == null) { |
733c614c MK |
374 | return null; |
375 | } | |
cf9a28da | 376 | final long posStart = currentBitBuffer.position(); |
1fbaecd1 | 377 | /* Read the stream event header. */ |
a4fa4e36 | 378 | if (fStreamEventHeaderDecl != null) { |
6c7592e1 MK |
379 | if (fStreamEventHeaderDecl instanceof IEventHeaderDeclaration) { |
380 | fCurrentStreamEventHeaderDef = (ICompositeDefinition) fStreamEventHeaderDecl.createDefinition(null, "", currentBitBuffer); //$NON-NLS-1$ | |
381 | EventHeaderDefinition ehd = (EventHeaderDefinition) fCurrentStreamEventHeaderDef; | |
382 | eventID = ehd.getId(); | |
383 | timestamp = calculateTimestamp(ehd.getTimestamp(), ehd.getTimestampLength()); | |
384 | } else { | |
385 | fCurrentStreamEventHeaderDef = ((StructDeclaration) fStreamEventHeaderDecl).createDefinition(null, LexicalScope.EVENT_HEADER, currentBitBuffer); | |
386 | StructDefinition StructEventHeaderDef = (StructDefinition) fCurrentStreamEventHeaderDef; | |
387 | /* Check for the event id. */ | |
388 | IDefinition idDef = StructEventHeaderDef.lookupDefinition("id"); //$NON-NLS-1$ | |
389 | SimpleDatatypeDefinition simpleIdDef = null; | |
390 | if (idDef instanceof SimpleDatatypeDefinition) { | |
391 | simpleIdDef = ((SimpleDatatypeDefinition) idDef); | |
392 | } else if (idDef != null) { | |
393 | throw new CTFReaderException("Id defintion not an integer, enum or float definiton in event header."); //$NON-NLS-1$ | |
394 | } | |
866e5b51 | 395 | |
6c7592e1 MK |
396 | /* |
397 | * Get the timestamp from the event header (may be overridden | |
398 | * later on) | |
399 | */ | |
400 | IntegerDefinition timestampDef = StructEventHeaderDef.lookupInteger("timestamp"); //$NON-NLS-1$ | |
1fbaecd1 | 401 | |
6c7592e1 MK |
402 | /* Check for the variant v. */ |
403 | IDefinition variantDef = StructEventHeaderDef.lookupDefinition("v"); //$NON-NLS-1$ | |
404 | if (variantDef instanceof VariantDefinition) { | |
866e5b51 | 405 | |
6c7592e1 MK |
406 | /* Get the variant current field */ |
407 | StructDefinition variantCurrentField = (StructDefinition) ((VariantDefinition) variantDef).getCurrentField(); | |
866e5b51 | 408 | |
6c7592e1 MK |
409 | /* |
410 | * Try to get the id field in the current field of the | |
411 | * variant. If it is present, it overrides the previously | |
412 | * read event id. | |
413 | */ | |
414 | IDefinition vIdDef = variantCurrentField.lookupDefinition("id"); //$NON-NLS-1$ | |
415 | if (vIdDef instanceof IntegerDefinition) { | |
416 | simpleIdDef = (SimpleDatatypeDefinition) vIdDef; | |
417 | } | |
866e5b51 | 418 | |
6c7592e1 MK |
419 | /* |
420 | * Get the timestamp. This would overwrite any previous | |
421 | * timestamp definition | |
422 | */ | |
423 | timestampDef = variantCurrentField.lookupInteger("timestamp"); //$NON-NLS-1$ | |
6bdc9fac | 424 | } |
6c7592e1 | 425 | if (simpleIdDef != null) { |
5f715709 | 426 | eventID = simpleIdDef.getIntegerValue().intValue(); |
6c7592e1 MK |
427 | } |
428 | if (timestampDef != null) { | |
429 | timestamp = calculateTimestamp(timestampDef); | |
430 | } // else timestamp remains 0 | |
21fb02fa | 431 | } |
866e5b51 | 432 | } |
1fbaecd1 | 433 | /* Get the right event definition using the event id. */ |
5f715709 | 434 | IEventDeclaration eventDeclaration = fStreamInputReader.getStreamInput().getStream().getEventDeclaration(eventID); |
a4fa4e36 | 435 | if (eventDeclaration == null) { |
866e5b51 FC |
436 | throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$ |
437 | } | |
733c614c | 438 | EventDefinition eventDef = eventDeclaration.createDefinition(fStreamInputReader, currentBitBuffer, timestamp); |
866e5b51 FC |
439 | |
440 | /* | |
441 | * Set the event timestamp using the timestamp calculated by | |
442 | * updateTimestamp. | |
443 | */ | |
866e5b51 | 444 | |
cf9a28da MK |
445 | if (posStart == currentBitBuffer.position()) { |
446 | throw new CTFReaderException("Empty event not allowed, event: " + eventDef.getDeclaration().getName()); //$NON-NLS-1$ | |
447 | } | |
448 | ||
866e5b51 FC |
449 | return eventDef; |
450 | } | |
451 | ||
452 | /** | |
453 | * Calculates the timestamp value of the event, possibly using the timestamp | |
454 | * from the last event. | |
455 | * | |
456 | * @param timestampDef | |
457 | * Integer definition of the timestamp. | |
458 | * @return The calculated timestamp value. | |
459 | */ | |
460 | private long calculateTimestamp(IntegerDefinition timestampDef) { | |
866e5b51 | 461 | int len = timestampDef.getDeclaration().getLength(); |
6c7592e1 | 462 | final long value = timestampDef.getValue(); |
866e5b51 | 463 | |
6c7592e1 MK |
464 | return calculateTimestamp(value, len); |
465 | } | |
466 | ||
467 | private long calculateTimestamp(final long value, int len) { | |
468 | long newval; | |
469 | long majorasbitmask; | |
866e5b51 FC |
470 | /* |
471 | * If the timestamp length is 64 bits, it is a full timestamp. | |
472 | */ | |
f068c622 | 473 | if (len == Long.SIZE) { |
6c7592e1 | 474 | fLastTimestamp = value; |
2882273c | 475 | return fLastTimestamp; |
866e5b51 FC |
476 | } |
477 | ||
478 | /* | |
479 | * Bit mask to keep / remove all old / new bits. | |
480 | */ | |
481 | majorasbitmask = (1L << len) - 1; | |
482 | ||
483 | /* | |
484 | * If the new value is smaller than the corresponding bits of the last | |
485 | * timestamp, we assume an overflow of the compact representation. | |
486 | */ | |
6c7592e1 | 487 | newval = value; |
2882273c | 488 | if (newval < (fLastTimestamp & majorasbitmask)) { |
866e5b51 FC |
489 | newval = newval + (1L << len); |
490 | } | |
491 | ||
492 | /* Keep only the high bits of the old value */ | |
2882273c | 493 | fLastTimestamp = fLastTimestamp & ~majorasbitmask; |
866e5b51 FC |
494 | |
495 | /* Then add the low bits of the new value */ | |
2882273c | 496 | fLastTimestamp = fLastTimestamp + newval; |
866e5b51 | 497 | |
2882273c | 498 | return fLastTimestamp; |
866e5b51 FC |
499 | } |
500 | ||
501 | @Override | |
502 | public Definition lookupDefinition(String lookupPath) { | |
f159df11 | 503 | if (lookupPath.equals(LexicalScope.STREAM_PACKET_CONTEXT.toString())) { |
6c7592e1 | 504 | return (Definition) fCurrentStreamPacketContextDef; |
a4fa4e36 | 505 | } |
f159df11 | 506 | if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER.toString())) { |
6c7592e1 | 507 | return (Definition) fCurrentTracePacketHeaderDef; |
a4fa4e36 | 508 | } |
866e5b51 FC |
509 | return null; |
510 | } | |
a4fa4e36 MK |
511 | |
512 | /** | |
513 | * Get stream event header | |
514 | * | |
515 | * @return the stream event header | |
733e6767 MK |
516 | * @deprecated use |
517 | * {@link CTFStreamInputPacketReader#getStreamEventHeaderDefinition()} | |
a4fa4e36 | 518 | */ |
6c7592e1 | 519 | @Deprecated |
a4fa4e36 | 520 | public StructDefinition getCurrentStreamEventHeader() { |
6c7592e1 MK |
521 | return (StructDefinition) ((fCurrentStreamEventHeaderDef instanceof StructDefinition) ? fCurrentStreamEventHeaderDef : null); |
522 | } | |
523 | ||
524 | /** | |
525 | * Get stream event header | |
526 | * | |
527 | * @return the stream event header | |
6c7592e1 MK |
528 | */ |
529 | public ICompositeDefinition getStreamEventHeaderDefinition() { | |
a4fa4e36 MK |
530 | return fCurrentStreamEventHeaderDef; |
531 | } | |
532 | ||
533 | /** | |
534 | * Get the current packet event header | |
535 | * | |
536 | * @return the current packet event header | |
a4fa4e36 MK |
537 | */ |
538 | public StructDefinition getCurrentPacketEventHeader() { | |
6c7592e1 MK |
539 | if (fCurrentTracePacketHeaderDef instanceof StructDefinition) { |
540 | return (StructDefinition) fCurrentTracePacketHeaderDef; | |
541 | } | |
542 | return null; | |
a4fa4e36 | 543 | } |
866e5b51 | 544 | } |