| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others |
| 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; |
| 15 | import java.nio.MappedByteBuffer; |
| 16 | import java.nio.channels.FileChannel.MapMode; |
| 17 | import java.util.Collection; |
| 18 | import java.util.HashMap; |
| 19 | |
| 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; |
| 32 | |
| 33 | /** |
| 34 | * <b><u>StreamInputPacketReader</u></b> |
| 35 | * <p> |
| 36 | * Reads the events of a packet of a trace file. |
| 37 | */ |
| 38 | public class StreamInputPacketReader implements IDefinitionScope { |
| 39 | |
| 40 | // ------------------------------------------------------------------------ |
| 41 | // Constants |
| 42 | // ------------------------------------------------------------------------ |
| 43 | |
| 44 | /** |
| 45 | * Reference to the index entry of the current packet. |
| 46 | */ |
| 47 | private StreamInputPacketIndexEntry currentPacket = null; |
| 48 | |
| 49 | /** |
| 50 | * BitBuffer used to read the trace file. |
| 51 | */ |
| 52 | private final BitBuffer bitBuffer = new BitBuffer(); |
| 53 | |
| 54 | /** |
| 55 | * StreamInputReader that uses this StreamInputPacketReader. |
| 56 | */ |
| 57 | private final StreamInputReader streamInputReader; |
| 58 | |
| 59 | /** |
| 60 | * Last timestamp recorded. |
| 61 | * |
| 62 | * Needed to calculate the complete timestamp values for the events with |
| 63 | * compact headers. |
| 64 | */ |
| 65 | private long lastTimestamp = 0; |
| 66 | |
| 67 | /** |
| 68 | * Trace packet header. |
| 69 | */ |
| 70 | private StructDefinition tracePacketHeaderDef = null; |
| 71 | |
| 72 | /** |
| 73 | * Stream packet context definition. |
| 74 | */ |
| 75 | private StructDefinition streamPacketContextDef = null; |
| 76 | |
| 77 | /** |
| 78 | * Stream event header definition. |
| 79 | */ |
| 80 | private StructDefinition streamEventHeaderDef = null; |
| 81 | |
| 82 | /** |
| 83 | * Stream event context definition. |
| 84 | */ |
| 85 | private StructDefinition streamEventContextDef = null; |
| 86 | |
| 87 | /** |
| 88 | * Maps event ID to event definitions. |
| 89 | */ |
| 90 | private final HashMap<Long, EventDefinition> events; |
| 91 | |
| 92 | /** |
| 93 | * CPU id of current packet. |
| 94 | */ |
| 95 | private int currentCpu = 0; |
| 96 | |
| 97 | /** |
| 98 | * number of lost events in this packet |
| 99 | */ |
| 100 | private int lostEvents; |
| 101 | |
| 102 | private int lostSoFar; |
| 103 | |
| 104 | // ------------------------------------------------------------------------ |
| 105 | // Attributes |
| 106 | // ------------------------------------------------------------------------ |
| 107 | |
| 108 | /** |
| 109 | * Constructs a StreamInputPacketReader. |
| 110 | * |
| 111 | * @param streamInputReader |
| 112 | * The StreamInputReader to which this packet reader belongs to. |
| 113 | */ |
| 114 | public StreamInputPacketReader(StreamInputReader streamInputReader) { |
| 115 | this.streamInputReader = streamInputReader; |
| 116 | |
| 117 | /* |
| 118 | * Set the BitBuffer's byte order. |
| 119 | */ |
| 120 | getBitBuffer().setByteOrder(streamInputReader.getByteOrder()); |
| 121 | |
| 122 | events = streamInputReader.getStreamInput().getStream().getTrace() |
| 123 | .getEventDefs(streamInputReader.getStreamInput()); |
| 124 | /* |
| 125 | * Create definitions needed to read the events. |
| 126 | */ |
| 127 | createDefinitions(); |
| 128 | |
| 129 | lostEvents = 0; |
| 130 | lostSoFar = 0; |
| 131 | } |
| 132 | |
| 133 | // ------------------------------------------------------------------------ |
| 134 | // Constructors |
| 135 | // ------------------------------------------------------------------------ |
| 136 | |
| 137 | // ------------------------------------------------------------------------ |
| 138 | // Getters/Setters/Predicates |
| 139 | // ------------------------------------------------------------------------ |
| 140 | |
| 141 | /** |
| 142 | * Gets the current packet |
| 143 | * |
| 144 | * @return the current packet |
| 145 | */ |
| 146 | public StreamInputPacketIndexEntry getCurrentPacket() { |
| 147 | return this.currentPacket; |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Gets the steamPacketContext Definition |
| 152 | * |
| 153 | * @return steamPacketContext Definition |
| 154 | */ |
| 155 | public StructDefinition getStreamPacketContextDef() { |
| 156 | return this.streamPacketContextDef; |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Gets the CPU (core) number |
| 161 | * |
| 162 | * @return the CPU (core) number |
| 163 | */ |
| 164 | public int getCPU() { |
| 165 | return this.currentCpu; |
| 166 | } |
| 167 | |
| 168 | @Override |
| 169 | public String getPath() { |
| 170 | return ""; //$NON-NLS-1$ |
| 171 | } |
| 172 | |
| 173 | // ------------------------------------------------------------------------ |
| 174 | // Operations |
| 175 | // ------------------------------------------------------------------------ |
| 176 | |
| 177 | /** |
| 178 | * Creates definitions needed to read events (stream-defined and |
| 179 | * event-defined). |
| 180 | */ |
| 181 | private void createDefinitions() { |
| 182 | /* |
| 183 | * Create trace packet header definition. |
| 184 | */ |
| 185 | final Stream currentStream = getStreamInputReader().getStreamInput() |
| 186 | .getStream(); |
| 187 | StructDeclaration tracePacketHeaderDecl = currentStream.getTrace() |
| 188 | .getPacketHeader(); |
| 189 | if (tracePacketHeaderDecl != null) { |
| 190 | setTracePacketHeaderDef(tracePacketHeaderDecl.createDefinition( |
| 191 | this, "trace.packet.header")); //$NON-NLS-1$ |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Create stream packet context definition. |
| 196 | */ |
| 197 | StructDeclaration streamPacketContextDecl = currentStream |
| 198 | .getPacketContextDecl(); |
| 199 | if (streamPacketContextDecl != null) { |
| 200 | setStreamPacketContextDef(streamPacketContextDecl.createDefinition( |
| 201 | this, "stream.packet.context")); //$NON-NLS-1$ |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Create stream event header definition. |
| 206 | */ |
| 207 | StructDeclaration streamEventHeaderDecl = currentStream |
| 208 | .getEventHeaderDecl(); |
| 209 | if (streamEventHeaderDecl != null) { |
| 210 | setStreamEventHeaderDef(streamEventHeaderDecl.createDefinition( |
| 211 | this, "stream.event.header")); //$NON-NLS-1$ |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * Create stream event context definition. |
| 216 | */ |
| 217 | StructDeclaration streamEventContextDecl = currentStream |
| 218 | .getEventContextDecl(); |
| 219 | if (streamEventContextDecl != null) { |
| 220 | setStreamEventContextDef(streamEventContextDecl.createDefinition( |
| 221 | this, "stream.event.context")); //$NON-NLS-1$ |
| 222 | } |
| 223 | |
| 224 | createEventDefinitions(); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Creates definitions needed to read the event. (event-defined). |
| 229 | */ |
| 230 | private void createEventDefinitions() { |
| 231 | Collection<EventDeclaration> eventDecls = getStreamInputReader() |
| 232 | .getStreamInput().getStream().getEvents().values(); |
| 233 | |
| 234 | /* |
| 235 | * Create definitions for each event. |
| 236 | */ |
| 237 | for (EventDeclaration event : eventDecls) { |
| 238 | if (!events.containsKey(event.getId())) { |
| 239 | EventDefinition eventDef = event |
| 240 | .createDefinition(getStreamInputReader()); |
| 241 | events.put(event.getId(), eventDef); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /** |
| 247 | * Changes the current packet to the given one. |
| 248 | * |
| 249 | * @param currentPacket |
| 250 | * The index entry of the packet to switch to. |
| 251 | */ |
| 252 | public void setCurrentPacket(StreamInputPacketIndexEntry currentPacket) { |
| 253 | this.currentPacket = currentPacket; |
| 254 | |
| 255 | if (this.currentPacket != null) { |
| 256 | /* |
| 257 | * Change the map of the BitBuffer. |
| 258 | */ |
| 259 | MappedByteBuffer bb = null; |
| 260 | try { |
| 261 | bb = getStreamInputReader() |
| 262 | .getStreamInput() |
| 263 | .getFileChannel() |
| 264 | .map(MapMode.READ_ONLY, |
| 265 | this.currentPacket.getOffsetBytes(), |
| 266 | (this.currentPacket.getPacketSizeBits() + 7) / 8); |
| 267 | } catch (IOException e) { |
| 268 | /* |
| 269 | * The streamInputReader object is already allocated, so this |
| 270 | * shouldn't fail bar some very bad kernel or RAM errors... |
| 271 | */ |
| 272 | e.printStackTrace(); |
| 273 | } |
| 274 | |
| 275 | getBitBuffer().setByteBuffer(bb); |
| 276 | |
| 277 | /* |
| 278 | * Read trace packet header. |
| 279 | */ |
| 280 | if (getTracePacketHeaderDef() != null) { |
| 281 | getTracePacketHeaderDef().read(getBitBuffer()); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Read stream packet context. |
| 286 | */ |
| 287 | if (getStreamPacketContextDef() != null) { |
| 288 | getStreamPacketContextDef().read(getBitBuffer()); |
| 289 | /* |
| 290 | * Read CPU ID |
| 291 | */ |
| 292 | |
| 293 | Definition cpuiddef = getStreamPacketContextDef() |
| 294 | .lookupDefinition("cpu_id"); //$NON-NLS-1$ |
| 295 | if (cpuiddef instanceof IntegerDefinition) { |
| 296 | currentCpu = (int) ((IntegerDefinition) cpuiddef) |
| 297 | .getValue(); |
| 298 | } |
| 299 | /* |
| 300 | * Read number of lost events |
| 301 | */ |
| 302 | Definition lostEventsdef = getStreamPacketContextDef() |
| 303 | .lookupDefinition("events_discarded"); //$NON-NLS-1$ |
| 304 | if (cpuiddef instanceof IntegerDefinition) { |
| 305 | lostEvents = (int) ((IntegerDefinition) lostEventsdef) |
| 306 | .getValue(); |
| 307 | } |
| 308 | |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Use the timestamp begin of the packet as the reference for the |
| 313 | * timestamp reconstitution. |
| 314 | */ |
| 315 | lastTimestamp = currentPacket.getTimestampBegin(); |
| 316 | } else { |
| 317 | getBitBuffer().setByteBuffer(null); |
| 318 | |
| 319 | lastTimestamp = 0; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Returns whether it is possible to read any more events from this packet. |
| 325 | * |
| 326 | * @return True if it is possible to read any more events from this packet. |
| 327 | */ |
| 328 | public boolean hasMoreEvents() { |
| 329 | if (currentPacket != null) { |
| 330 | return getBitBuffer().position() < currentPacket |
| 331 | .getContentSizeBits(); |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Reads the next event of the packet into the right event definition. |
| 338 | * |
| 339 | * @return The event definition containing the event data that was just |
| 340 | * read. |
| 341 | * @throws CTFReaderException |
| 342 | */ |
| 343 | public EventDefinition readNextEvent() throws CTFReaderException { |
| 344 | /* WARNING: This is very LTTng-specific. */ |
| 345 | Long eventID = null; |
| 346 | long timestamp = 0; |
| 347 | |
| 348 | if (lostEvents > lostSoFar) { |
| 349 | EventDefinition eventDef = EventDeclaration |
| 350 | .getLostEventDeclaration().createDefinition( |
| 351 | streamInputReader); |
| 352 | eventDef.setTimestamp(this.lastTimestamp); |
| 353 | ++lostSoFar; |
| 354 | return eventDef; |
| 355 | } |
| 356 | StructDefinition sehd = getStreamEventHeaderDef(); // acronym for a long |
| 357 | // variable name |
| 358 | BitBuffer currentBitBuffer = getBitBuffer(); |
| 359 | /* |
| 360 | * Read the stream event header. |
| 361 | */ |
| 362 | |
| 363 | if (sehd != null) { |
| 364 | sehd.read(currentBitBuffer); |
| 365 | |
| 366 | /* |
| 367 | * Check for an event id. |
| 368 | */ |
| 369 | EnumDefinition idEnumDef = (EnumDefinition) sehd |
| 370 | .lookupDefinition("id"); //$NON-NLS-1$ |
| 371 | assert (idEnumDef != null); |
| 372 | |
| 373 | eventID = idEnumDef.getIntegerValue(); |
| 374 | |
| 375 | /* |
| 376 | * Check for the variant v. |
| 377 | */ |
| 378 | VariantDefinition variantDef = (VariantDefinition) sehd |
| 379 | .lookupDefinition("v"); //$NON-NLS-1$ |
| 380 | assert (variantDef != null); |
| 381 | |
| 382 | /* |
| 383 | * Get the variant current field |
| 384 | */ |
| 385 | StructDefinition variantCurrentField = (StructDefinition) variantDef |
| 386 | .getCurrentField(); |
| 387 | assert (variantCurrentField != null); |
| 388 | |
| 389 | /* |
| 390 | * Try to get the id field in the current field of the variant. If |
| 391 | * it is present, it overrides the previously read event id. |
| 392 | */ |
| 393 | IntegerDefinition idIntegerDef = (IntegerDefinition) variantCurrentField |
| 394 | .lookupDefinition("id"); //$NON-NLS-1$ |
| 395 | if (idIntegerDef != null) { |
| 396 | eventID = idIntegerDef.getValue(); |
| 397 | } |
| 398 | |
| 399 | /* |
| 400 | * Get the timestamp. |
| 401 | */ |
| 402 | IntegerDefinition timestampDef = (IntegerDefinition) variantCurrentField |
| 403 | .lookupDefinition("timestamp"); //$NON-NLS-1$ |
| 404 | assert (timestampDef != null); |
| 405 | |
| 406 | /* |
| 407 | * Calculate the event timestamp. |
| 408 | */ |
| 409 | timestamp = calculateTimestamp(timestampDef); |
| 410 | } |
| 411 | |
| 412 | /* |
| 413 | * Read the stream event context. |
| 414 | */ |
| 415 | if (getStreamEventContextDef() != null) { |
| 416 | getStreamEventContextDef().read(currentBitBuffer); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Get the right event definition using the event id. |
| 421 | */ |
| 422 | EventDefinition eventDef = events.get(eventID); |
| 423 | if (eventDef == null) { |
| 424 | throw new CTFReaderException("Incorrect event id : " + eventID); //$NON-NLS-1$ |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Read the event context. |
| 429 | */ |
| 430 | if (eventDef.getContext() != null) { |
| 431 | eventDef.getContext().read(currentBitBuffer); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Read the event fields. |
| 436 | */ |
| 437 | if (eventDef.getFields() != null) { |
| 438 | eventDef.getFields().read(currentBitBuffer); |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Set the event timestamp using the timestamp calculated by |
| 443 | * updateTimestamp. |
| 444 | */ |
| 445 | eventDef.setTimestamp(timestamp); |
| 446 | |
| 447 | return eventDef; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Calculates the timestamp value of the event, possibly using the timestamp |
| 452 | * from the last event. |
| 453 | * |
| 454 | * @param timestampDef |
| 455 | * Integer definition of the timestamp. |
| 456 | * @return The calculated timestamp value. |
| 457 | */ |
| 458 | private long calculateTimestamp(IntegerDefinition timestampDef) { |
| 459 | long newval; |
| 460 | long majorasbitmask; |
| 461 | int len = timestampDef.getDeclaration().getLength(); |
| 462 | |
| 463 | /* |
| 464 | * If the timestamp length is 64 bits, it is a full timestamp. |
| 465 | */ |
| 466 | if (timestampDef.getDeclaration().getLength() == 64) { |
| 467 | lastTimestamp = timestampDef.getValue(); |
| 468 | return lastTimestamp; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Bit mask to keep / remove all old / new bits. |
| 473 | */ |
| 474 | majorasbitmask = (1L << len) - 1; |
| 475 | |
| 476 | /* |
| 477 | * If the new value is smaller than the corresponding bits of the last |
| 478 | * timestamp, we assume an overflow of the compact representation. |
| 479 | */ |
| 480 | newval = timestampDef.getValue(); |
| 481 | if (newval < (lastTimestamp & majorasbitmask)) { |
| 482 | newval = newval + (1L << len); |
| 483 | } |
| 484 | |
| 485 | /* Keep only the high bits of the old value */ |
| 486 | lastTimestamp = lastTimestamp & ~majorasbitmask; |
| 487 | |
| 488 | /* Then add the low bits of the new value */ |
| 489 | lastTimestamp = lastTimestamp + newval; |
| 490 | |
| 491 | return lastTimestamp; |
| 492 | } |
| 493 | |
| 494 | @SuppressWarnings("unused") |
| 495 | @Override |
| 496 | public Definition lookupDefinition(String lookupPath) { |
| 497 | // TODO Auto-generated method stub |
| 498 | return null; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Gets the stream event context definition (see CTF specs) |
| 503 | * |
| 504 | * @return the definition of the stream event context (the form not the |
| 505 | * content) |
| 506 | */ |
| 507 | public StructDefinition getStreamEventContextDef() { |
| 508 | return this.streamEventContextDef; |
| 509 | } |
| 510 | |
| 511 | /** |
| 512 | * Sets the stream event context definition |
| 513 | * |
| 514 | * @param streamEventContextDef |
| 515 | * The stream event context definition |
| 516 | */ |
| 517 | public void setStreamEventContextDef(StructDefinition streamEventContextDef) { |
| 518 | this.streamEventContextDef = streamEventContextDef; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Gets the stream event header definition |
| 523 | * |
| 524 | * @return the stream event header definition |
| 525 | */ |
| 526 | public StructDefinition getStreamEventHeaderDef() { |
| 527 | return this.streamEventHeaderDef; |
| 528 | } |
| 529 | |
| 530 | /** |
| 531 | * Sets the stream event header definition |
| 532 | * |
| 533 | * @param streamEventHeaderDef |
| 534 | * the stream event header definition |
| 535 | */ |
| 536 | public void setStreamEventHeaderDef(StructDefinition streamEventHeaderDef) { |
| 537 | this.streamEventHeaderDef = streamEventHeaderDef; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Sets the stream packet context definition |
| 542 | * |
| 543 | * @param streamPacketContextDef |
| 544 | * the stream packet context definition |
| 545 | */ |
| 546 | public void setStreamPacketContextDef( |
| 547 | StructDefinition streamPacketContextDef) { |
| 548 | this.streamPacketContextDef = streamPacketContextDef; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * Gets the trace packet header definition |
| 553 | * |
| 554 | * @return the trace packet header definition |
| 555 | */ |
| 556 | public StructDefinition getTracePacketHeaderDef() { |
| 557 | return this.tracePacketHeaderDef; |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Sets the trace packet header definition |
| 562 | * |
| 563 | * @param tracePacketHeaderDef |
| 564 | * the trace packet header definition |
| 565 | */ |
| 566 | public void setTracePacketHeaderDef(StructDefinition tracePacketHeaderDef) { |
| 567 | this.tracePacketHeaderDef = tracePacketHeaderDef; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * @return the parent stream input reader |
| 572 | */ |
| 573 | public StreamInputReader getStreamInputReader() { |
| 574 | return this.streamInputReader; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * |
| 579 | * @return THe bit buffer that reads the file. |
| 580 | */ |
| 581 | public BitBuffer getBitBuffer() { |
| 582 | return bitBuffer; |
| 583 | } |
| 584 | } |