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