| 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 | @SuppressWarnings("unused") |
| 183 | final StreamInputPacketIndexEntry currentPacket = this.packetReader.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 | @SuppressWarnings("unused") |
| 302 | StreamInputPacketIndexEntry entry = null; |
| 303 | /* |
| 304 | * Go to beginning of trace. |
| 305 | */ |
| 306 | seek(0); |
| 307 | /* |
| 308 | * if the trace is empty. |
| 309 | */ |
| 310 | if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) { |
| 311 | /* |
| 312 | * This means the trace is empty. abort. |
| 313 | */ |
| 314 | return; |
| 315 | } |
| 316 | /* |
| 317 | * Go to the last packet that contains events. |
| 318 | */ |
| 319 | for (int pos = len - 1; pos > 0; pos--) { |
| 320 | packetIndex = pos; |
| 321 | this.packetReader.setCurrentPacket(getPacket()); |
| 322 | if (this.packetReader.hasMoreEvents()) { |
| 323 | break; |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /* |
| 328 | * Go until the end of that packet |
| 329 | */ |
| 330 | EventDefinition prevEvent = null; |
| 331 | while (this.currentEvent != null) { |
| 332 | prevEvent = this.currentEvent; |
| 333 | this.readNextEvent(); |
| 334 | } |
| 335 | /* |
| 336 | * Go back to the previous event |
| 337 | */ |
| 338 | this.setCurrentEvent(prevEvent); |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * @return the parent |
| 343 | */ |
| 344 | public CTFTraceReader getParent() { |
| 345 | return parent; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * @param parent |
| 350 | * the parent to set |
| 351 | */ |
| 352 | public void setParent(CTFTraceReader parent) { |
| 353 | this.parent = parent; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Sets the current event in a stream input reader |
| 358 | * @param currentEvent the event to set |
| 359 | */ |
| 360 | public void setCurrentEvent(EventDefinition currentEvent) { |
| 361 | this.currentEvent = currentEvent; |
| 362 | } |
| 363 | |
| 364 | /** |
| 365 | * @return the packetIndexIt |
| 366 | */ |
| 367 | private int getPacketIndex() { |
| 368 | return packetIndex; |
| 369 | } |
| 370 | |
| 371 | private StreamInputPacketIndexEntry getPacket() { |
| 372 | return streamInput.getIndex().getEntries().get(getPacketIndex()); |
| 373 | } |
| 374 | |
| 375 | /** |
| 376 | * @return the packetReader |
| 377 | */ |
| 378 | public StreamInputPacketReader getPacketReader() { |
| 379 | return packetReader; |
| 380 | } |
| 381 | |
| 382 | /* |
| 383 | * (non-Javadoc) |
| 384 | * |
| 385 | * @see java.lang.Object#hashCode() |
| 386 | */ |
| 387 | @Override |
| 388 | public int hashCode() { |
| 389 | final int prime = 31; |
| 390 | int result = 1; |
| 391 | result = (prime * result) + name; |
| 392 | result = (prime * result) |
| 393 | + ((streamInput == null) ? 0 : streamInput.hashCode()); |
| 394 | return result; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * (non-Javadoc) |
| 399 | * |
| 400 | * @see java.lang.Object#equals(java.lang.Object) |
| 401 | */ |
| 402 | @Override |
| 403 | public boolean equals(Object obj) { |
| 404 | if (this == obj) { |
| 405 | return true; |
| 406 | } |
| 407 | if (obj == null) { |
| 408 | return false; |
| 409 | } |
| 410 | if (!(obj instanceof StreamInputReader)) { |
| 411 | return false; |
| 412 | } |
| 413 | StreamInputReader other = (StreamInputReader) obj; |
| 414 | if (name != other.name) { |
| 415 | return false; |
| 416 | } |
| 417 | if (streamInput == null) { |
| 418 | if (other.streamInput != null) { |
| 419 | return false; |
| 420 | } |
| 421 | } else if (!streamInput.equals(other.streamInput)) { |
| 422 | return false; |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | } |