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