| 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 | |
| 13 | package org.eclipse.linuxtools.ctf.core.trace; |
| 14 | |
| 15 | import java.nio.ByteOrder; |
| 16 | |
| 17 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 18 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
| 19 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput; |
| 20 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry; |
| 21 | |
| 22 | /** |
| 23 | * A CTF trace event reader. Reads the events of a trace file. |
| 24 | * |
| 25 | * @version 1.0 |
| 26 | * @author Matthew Khouzam |
| 27 | * @author Simon Marchi |
| 28 | */ |
| 29 | public class StreamInputReader { |
| 30 | |
| 31 | // ------------------------------------------------------------------------ |
| 32 | // Attributes |
| 33 | // ------------------------------------------------------------------------ |
| 34 | |
| 35 | /** |
| 36 | * The StreamInput we are reading. |
| 37 | */ |
| 38 | private final StreamInput streamInput; |
| 39 | |
| 40 | /** |
| 41 | * The packet reader used to read packets from this trace file. |
| 42 | */ |
| 43 | private final StreamInputPacketReader packetReader; |
| 44 | |
| 45 | /** |
| 46 | * Iterator on the packet index |
| 47 | */ |
| 48 | private int packetIndex; |
| 49 | |
| 50 | /** |
| 51 | * Reference to the current event of this trace file (iow, the last on that |
| 52 | * was read, the next one to be returned) |
| 53 | */ |
| 54 | private EventDefinition currentEvent = null; |
| 55 | |
| 56 | private int name; |
| 57 | |
| 58 | private CTFTraceReader parent; |
| 59 | |
| 60 | @SuppressWarnings("unused") |
| 61 | private final long prevIndex; |
| 62 | |
| 63 | // ------------------------------------------------------------------------ |
| 64 | // Constructors |
| 65 | // ------------------------------------------------------------------------ |
| 66 | |
| 67 | /** |
| 68 | * Constructs a StreamInputReader that reads a StreamInput. |
| 69 | * |
| 70 | * @param streamInput |
| 71 | * The StreamInput to read. |
| 72 | */ |
| 73 | public StreamInputReader(StreamInput streamInput) { |
| 74 | this.streamInput = streamInput; |
| 75 | this.packetReader = new StreamInputPacketReader(this); |
| 76 | /* |
| 77 | * Get the iterator on the packet index. |
| 78 | */ |
| 79 | this.packetIndex = 0; |
| 80 | |
| 81 | this.prevIndex = 0; |
| 82 | /* |
| 83 | * Make first packet the current one. |
| 84 | */ |
| 85 | goToNextPacket(); |
| 86 | } |
| 87 | |
| 88 | // ------------------------------------------------------------------------ |
| 89 | // Getters/Setters/Predicates |
| 90 | // ------------------------------------------------------------------------ |
| 91 | |
| 92 | /** |
| 93 | * Gets the current event in this stream |
| 94 | * |
| 95 | * @return the current event in the stream, null if the stream is |
| 96 | * finished/empty/malformed |
| 97 | */ |
| 98 | public EventDefinition getCurrentEvent() { |
| 99 | return this.currentEvent; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * gets the current packet context |
| 104 | * |
| 105 | * @return the current packet context (size, lost events and such) |
| 106 | */ |
| 107 | public StructDefinition getCurrentPacketContext() { |
| 108 | return this.packetReader.getStreamPacketContextDef(); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Gets the byte order for a trace |
| 113 | * |
| 114 | * @return the trace byte order |
| 115 | */ |
| 116 | public ByteOrder getByteOrder() { |
| 117 | return streamInput.getStream().getTrace().getByteOrder(); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Gets the name of the stream (it's an id and a number) |
| 122 | * |
| 123 | * @return gets the stream name (it's a number) |
| 124 | */ |
| 125 | public int getName() { |
| 126 | return this.name; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Sets the name of the stream |
| 131 | * |
| 132 | * @param name |
| 133 | * the name of the stream, (it's a number) |
| 134 | */ |
| 135 | public void setName(int name) { |
| 136 | this.name = name; |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Gets the CPU of a stream. It's the same as the one in /proc or running |
| 141 | * the asm CPUID instruction |
| 142 | * |
| 143 | * @return The CPU id (a number) |
| 144 | */ |
| 145 | public int getCPU() { |
| 146 | return this.packetReader.getCPU(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Gets the filename of the stream being read |
| 151 | * @return The filename of the stream being read |
| 152 | */ |
| 153 | public String getFilename() { |
| 154 | return streamInput.getFilename(); |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * for internal use only |
| 159 | */ |
| 160 | StreamInput getStreamInput() { |
| 161 | return streamInput; |
| 162 | } |
| 163 | |
| 164 | // ------------------------------------------------------------------------ |
| 165 | // Operations |
| 166 | // ------------------------------------------------------------------------ |
| 167 | /** |
| 168 | * Reads the next event in the current event variable. |
| 169 | * |
| 170 | * @return If an event has been successfully read. |
| 171 | */ |
| 172 | public boolean readNextEvent() { |
| 173 | |
| 174 | /* |
| 175 | * Change packet if needed |
| 176 | */ |
| 177 | if (!this.packetReader.hasMoreEvents()) { |
| 178 | final StreamInputPacketIndexEntry prevPacket = this.packetReader |
| 179 | .getCurrentPacket(); |
| 180 | if (prevPacket != null) { |
| 181 | goToNextPacket(); |
| 182 | // final StreamInputPacketIndexEntry currentPacket = this.packetReader |
| 183 | // .getCurrentPacket(); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | /*autogenerate javadoc getter setter |
| 188 | * If an event is available, read it. |
| 189 | */ |
| 190 | if (this.packetReader.hasMoreEvents()) { |
| 191 | try { |
| 192 | this.setCurrentEvent(this.packetReader.readNextEvent()); |
| 193 | } catch (CTFReaderException e) { |
| 194 | /* |
| 195 | * Some problem happened, we'll assume that there are no more |
| 196 | * events |
| 197 | */ |
| 198 | return false; |
| 199 | } |
| 200 | return true; |
| 201 | } |
| 202 | this.setCurrentEvent(null); |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Change the current packet of the packet reader to the next one. |
| 208 | */ |
| 209 | private void goToNextPacket() { |
| 210 | packetIndex++; |
| 211 | if (getPacketSize() >= (packetIndex + 1)) { |
| 212 | this.packetReader.setCurrentPacket(getPacket()); |
| 213 | } else { |
| 214 | try { |
| 215 | if (this.streamInput.addPacketHeaderIndex()) { |
| 216 | packetIndex = getPacketSize() - 1; |
| 217 | this.packetReader.setCurrentPacket(getPacket()); |
| 218 | |
| 219 | } else { |
| 220 | this.packetReader.setCurrentPacket(null); |
| 221 | } |
| 222 | |
| 223 | } catch (CTFReaderException e) { |
| 224 | this.packetReader.setCurrentPacket(null); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /** |
| 230 | * @return |
| 231 | */ |
| 232 | private int getPacketSize() { |
| 233 | return streamInput.getIndex().getEntries().size(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Changes the location of the trace file reader so that the current event |
| 238 | * is the first event with a timestamp greater than the given timestamp. |
| 239 | * |
| 240 | * @param timestamp |
| 241 | * The timestamp to seek to. |
| 242 | * @return The offset compared to the current position |
| 243 | */ |
| 244 | public long seek(long timestamp) { |
| 245 | long offset = 0; |
| 246 | |
| 247 | gotoPacket(timestamp); |
| 248 | |
| 249 | /* |
| 250 | * index up to the desired timestamp. |
| 251 | */ |
| 252 | while ((this.packetReader.getCurrentPacket() != null) |
| 253 | && (this.packetReader.getCurrentPacket().getTimestampEnd() < timestamp)) { |
| 254 | try { |
| 255 | this.streamInput.addPacketHeaderIndex(); |
| 256 | goToNextPacket(); |
| 257 | } catch (CTFReaderException e) { |
| 258 | // do nothing here |
| 259 | } |
| 260 | } |
| 261 | if (this.packetReader.getCurrentPacket() == null) { |
| 262 | gotoPacket(timestamp); |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Advance until A. we reached the end of the trace file (which means |
| 267 | * the given timestamp is after the last event), or B. we found the |
| 268 | * first event with a timestamp greater than the given timestamp. |
| 269 | */ |
| 270 | readNextEvent(); |
| 271 | boolean done = (this.getCurrentEvent() == null); |
| 272 | while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) { |
| 273 | readNextEvent(); |
| 274 | done = (this.getCurrentEvent() == null); |
| 275 | offset++; |
| 276 | } |
| 277 | return offset; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * @param timestamp |
| 282 | */ |
| 283 | private void gotoPacket(long timestamp) { |
| 284 | this.packetIndex = this.streamInput.getIndex().search(timestamp) |
| 285 | .previousIndex(); |
| 286 | /* |
| 287 | * Switch to this packet. |
| 288 | */ |
| 289 | goToNextPacket(); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Seeks the last event of a stream and returns it. |
| 294 | */ |
| 295 | public void goToLastEvent() { |
| 296 | /* |
| 297 | * Search in the index for the packet to search in. |
| 298 | */ |
| 299 | final int len = this.streamInput.getIndex().getEntries().size(); |
| 300 | |
| 301 | // StreamInputPacketIndexEntry entry = null; |
| 302 | /* |
| 303 | * Go to beginning of trace. |
| 304 | */ |
| 305 | seek(0); |
| 306 | /* |
| 307 | * if the trace is empty. |
| 308 | */ |
| 309 | if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) { |
| 310 | /* |
| 311 | * This means the trace is empty. abort. |
| 312 | */ |
| 313 | return; |
| 314 | } |
| 315 | /* |
| 316 | * Go to the last packet that contains events. |
| 317 | */ |
| 318 | for (int pos = len - 1; pos > 0; pos--) { |
| 319 | packetIndex = pos; |
| 320 | this.packetReader.setCurrentPacket(getPacket()); |
| 321 | if (this.packetReader.hasMoreEvents()) { |
| 322 | break; |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * Go until the end of that packet |
| 328 | */ |
| 329 | EventDefinition prevEvent = null; |
| 330 | while (this.currentEvent != null) { |
| 331 | prevEvent = this.currentEvent; |
| 332 | this.readNextEvent(); |
| 333 | } |
| 334 | /* |
| 335 | * Go back to the previous event |
| 336 | */ |
| 337 | this.setCurrentEvent(prevEvent); |
| 338 | } |
| 339 | |
| 340 | /** |
| 341 | * @return the parent |
| 342 | */ |
| 343 | public CTFTraceReader getParent() { |
| 344 | return parent; |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * @param parent |
| 349 | * the parent to set |
| 350 | */ |
| 351 | public void setParent(CTFTraceReader parent) { |
| 352 | this.parent = parent; |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Sets the current event in a stream input reader |
| 357 | * @param currentEvent the event to set |
| 358 | */ |
| 359 | public void setCurrentEvent(EventDefinition currentEvent) { |
| 360 | this.currentEvent = currentEvent; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * @return the packetIndexIt |
| 365 | */ |
| 366 | private int getPacketIndex() { |
| 367 | return packetIndex; |
| 368 | } |
| 369 | |
| 370 | private StreamInputPacketIndexEntry getPacket() { |
| 371 | return streamInput.getIndex().getEntries().get(getPacketIndex()); |
| 372 | } |
| 373 | |
| 374 | /** |
| 375 | * @return the packetReader |
| 376 | */ |
| 377 | public StreamInputPacketReader getPacketReader() { |
| 378 | return packetReader; |
| 379 | } |
| 380 | |
| 381 | /* |
| 382 | * (non-Javadoc) |
| 383 | * |
| 384 | * @see java.lang.Object#hashCode() |
| 385 | */ |
| 386 | @Override |
| 387 | public int hashCode() { |
| 388 | final int prime = 31; |
| 389 | int result = 1; |
| 390 | result = (prime * result) + name; |
| 391 | result = (prime * result) |
| 392 | + ((streamInput == null) ? 0 : streamInput.hashCode()); |
| 393 | return result; |
| 394 | } |
| 395 | |
| 396 | /* |
| 397 | * (non-Javadoc) |
| 398 | * |
| 399 | * @see java.lang.Object#equals(java.lang.Object) |
| 400 | */ |
| 401 | @Override |
| 402 | public boolean equals(Object obj) { |
| 403 | if (this == obj) { |
| 404 | return true; |
| 405 | } |
| 406 | if (obj == null) { |
| 407 | return false; |
| 408 | } |
| 409 | if (!(obj instanceof StreamInputReader)) { |
| 410 | return false; |
| 411 | } |
| 412 | StreamInputReader other = (StreamInputReader) obj; |
| 413 | if (name != other.name) { |
| 414 | return false; |
| 415 | } |
| 416 | if (streamInput == null) { |
| 417 | if (other.streamInput != null) { |
| 418 | return false; |
| 419 | } |
| 420 | } else if (!streamInput.equals(other.streamInput)) { |
| 421 | return false; |
| 422 | } |
| 423 | return true; |
| 424 | } |
| 425 | |
| 426 | } |