| 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: Alexandre Montplaisir - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.ctf.core.trace; |
| 14 | |
| 15 | import java.util.Collection; |
| 16 | import java.util.PriorityQueue; |
| 17 | import java.util.Set; |
| 18 | import java.util.Vector; |
| 19 | |
| 20 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
| 21 | import org.eclipse.linuxtools.internal.ctf.core.Activator; |
| 22 | import org.eclipse.linuxtools.internal.ctf.core.trace.Stream; |
| 23 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput; |
| 24 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator; |
| 25 | |
| 26 | /** |
| 27 | * Reads the events of a trace. |
| 28 | */ |
| 29 | |
| 30 | public class CTFTraceReader { |
| 31 | |
| 32 | // ------------------------------------------------------------------------ |
| 33 | // Attributes |
| 34 | // ------------------------------------------------------------------------ |
| 35 | |
| 36 | /** |
| 37 | * The trace to read from. |
| 38 | */ |
| 39 | private final CTFTrace trace; |
| 40 | |
| 41 | /** |
| 42 | * Vector of all the trace file readers. |
| 43 | */ |
| 44 | private final Vector<StreamInputReader> streamInputReaders = new Vector<StreamInputReader>(); |
| 45 | |
| 46 | /** |
| 47 | * Priority queue to order the trace file readers by timestamp. |
| 48 | */ |
| 49 | protected PriorityQueue<StreamInputReader> prio; |
| 50 | |
| 51 | /** |
| 52 | * Array to count the number of event per trace file. |
| 53 | */ |
| 54 | private long[] eventCountPerTraceFile; |
| 55 | |
| 56 | /** |
| 57 | * Timestamp of the first event in the trace |
| 58 | */ |
| 59 | private long startTime; |
| 60 | |
| 61 | /** |
| 62 | * Timestamp of the last event read so far |
| 63 | */ |
| 64 | private long endTime; |
| 65 | |
| 66 | |
| 67 | protected void setEndTime(long endTime) { |
| 68 | this.endTime = endTime; |
| 69 | } |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | // Constructors |
| 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | /** |
| 76 | * Constructs a TraceReader to read a trace. |
| 77 | * |
| 78 | * @param trace |
| 79 | * The trace to read from. |
| 80 | * @throws CTFReaderException |
| 81 | */ |
| 82 | public CTFTraceReader(CTFTrace trace) { |
| 83 | this.trace = trace; |
| 84 | streamInputReaders.clear(); |
| 85 | |
| 86 | /** |
| 87 | * Create the trace file readers. |
| 88 | */ |
| 89 | createStreamInputReaders(); |
| 90 | |
| 91 | /** |
| 92 | * Populate the timestamp-based priority queue. |
| 93 | */ |
| 94 | populateStreamInputReaderHeap(); |
| 95 | |
| 96 | /** |
| 97 | * Get the start Time of this trace bear in mind that the trace could be |
| 98 | * empty. |
| 99 | */ |
| 100 | this.startTime = 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin(); |
| 101 | if (hasMoreEvents()) { |
| 102 | this.startTime = prio.peek().getCurrentEvent().getTimestamp(); |
| 103 | this.setEndTime(this.startTime); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Copy constructor |
| 109 | */ |
| 110 | public CTFTraceReader copyFrom() { |
| 111 | CTFTraceReader newReader = null; |
| 112 | |
| 113 | newReader = new CTFTraceReader(this.trace); |
| 114 | newReader.startTime = this.startTime; |
| 115 | newReader.setEndTime(this.endTime); |
| 116 | return newReader; |
| 117 | } |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | // Getters/Setters/Predicates |
| 121 | // ------------------------------------------------------------------------ |
| 122 | |
| 123 | /** |
| 124 | * Return the start time of this trace (== timestamp of the first event) |
| 125 | * |
| 126 | * @return the trace start time |
| 127 | */ |
| 128 | public long getStartTime() { |
| 129 | return this.startTime; |
| 130 | } |
| 131 | |
| 132 | // ------------------------------------------------------------------------ |
| 133 | // Operations |
| 134 | // ------------------------------------------------------------------------ |
| 135 | |
| 136 | /** |
| 137 | * Creates one trace file reader per trace file contained in the trace. |
| 138 | */ |
| 139 | private void createStreamInputReaders() { |
| 140 | Collection<Stream> streams = this.trace.getStreams().values(); |
| 141 | |
| 142 | /* |
| 143 | * For each stream. |
| 144 | */ |
| 145 | for (Stream stream : streams) { |
| 146 | Set<StreamInput> streamInputs = stream.getStreamInputs(); |
| 147 | |
| 148 | /* |
| 149 | * For each trace file of the stream. |
| 150 | */ |
| 151 | for (StreamInput streamInput : streamInputs) { |
| 152 | /* |
| 153 | * Create a reader. |
| 154 | */ |
| 155 | StreamInputReader streamInputReader = new StreamInputReader( |
| 156 | streamInput); |
| 157 | |
| 158 | /* |
| 159 | * Add it to the group. |
| 160 | */ |
| 161 | this.streamInputReaders.add(streamInputReader); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Create the array to count the number of event per trace file. |
| 167 | */ |
| 168 | this.eventCountPerTraceFile = new long[this.streamInputReaders.size()]; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Initializes the priority queue used to choose the trace file with the |
| 173 | * lower next event timestamp. |
| 174 | */ |
| 175 | private void populateStreamInputReaderHeap() { |
| 176 | /* |
| 177 | * Create the priority queue with a size twice as bigger as the number |
| 178 | * of reader in order to avoid constant resizing. |
| 179 | */ |
| 180 | this.prio = new PriorityQueue<StreamInputReader>( |
| 181 | this.streamInputReaders.size() * 2, |
| 182 | new StreamInputReaderTimestampComparator()); |
| 183 | |
| 184 | int pos = 0; |
| 185 | |
| 186 | for (StreamInputReader reader : this.streamInputReaders) { |
| 187 | /* |
| 188 | * Add each trace file reader in the priority queue, if we are able |
| 189 | * to read an event from it. |
| 190 | */ |
| 191 | reader.setParent(this); |
| 192 | if (reader.readNextEvent()) { |
| 193 | this.prio.add(reader); |
| 194 | |
| 195 | this.eventCountPerTraceFile[pos] = 0; |
| 196 | reader.setName(pos); |
| 197 | |
| 198 | pos++; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Get the current event, which is the current event of the trace file |
| 205 | * reader with the lowest timestamp. |
| 206 | * |
| 207 | * @return An event definition, or null of the trace reader reached the end |
| 208 | * of the trace. |
| 209 | */ |
| 210 | public EventDefinition getCurrentEventDef() { |
| 211 | StreamInputReader top = getTopStream(); |
| 212 | |
| 213 | return (top != null) ? top.getCurrentEvent() : null; |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * Go to the next event. |
| 218 | * |
| 219 | * @return True if an event was read. |
| 220 | */ |
| 221 | public boolean advance() { |
| 222 | /* |
| 223 | * Index the |
| 224 | */ |
| 225 | /* |
| 226 | * Remove the reader from the top of the priority queue. |
| 227 | */ |
| 228 | StreamInputReader top = this.prio.poll(); |
| 229 | |
| 230 | /* |
| 231 | * If the queue was empty. |
| 232 | */ |
| 233 | if (top == null) { |
| 234 | return false; |
| 235 | } |
| 236 | /* |
| 237 | * Read the next event of this reader. |
| 238 | */ |
| 239 | if (top.readNextEvent()) { |
| 240 | /* |
| 241 | * Add it back in the queue. |
| 242 | */ |
| 243 | this.prio.add(top); |
| 244 | final long topEnd = top.getCurrentEvent().getTimestamp() + this.getTrace().getOffset(); |
| 245 | this.setEndTime( Math.max(topEnd, this.getEndTime())); |
| 246 | this.eventCountPerTraceFile[top.getName()]++; |
| 247 | |
| 248 | if (top.getCurrentEvent() != null) { |
| 249 | this.endTime = Math.max(top.getCurrentEvent().getTimestamp(), |
| 250 | this.endTime); |
| 251 | } |
| 252 | } |
| 253 | /* |
| 254 | * If there is no reader in the queue, it means the trace reader reached |
| 255 | * the end of the trace. |
| 256 | */ |
| 257 | return hasMoreEvents(); |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Go to the last event in the trace. |
| 262 | * |
| 263 | * @throws CTFReaderException |
| 264 | */ |
| 265 | public void goToLastEvent() { |
| 266 | seek(this.getEndTime()); |
| 267 | while (this.prio.size() > 1) { |
| 268 | this.advance(); |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
| 273 | * Seeks to a given timestamp It will go to the event just after the |
| 274 | * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and |
| 275 | * you're looking for 19, it'll give you 20, it you want 20, you'll get 20, |
| 276 | * if you want 21, you'll get 30. You want -inf, you'll get the first |
| 277 | * element, you want +inf, you'll get the end of the file with no events. |
| 278 | * |
| 279 | * @param timestamp |
| 280 | * the timestamp to seek to |
| 281 | * @return true if the trace has more events following the timestamp |
| 282 | */ |
| 283 | public boolean seek(long timestamp) { |
| 284 | /* |
| 285 | * Remove all the trace readers from the priority queue |
| 286 | */ |
| 287 | this.prio.clear(); |
| 288 | for (StreamInputReader streamInputReader : this.streamInputReaders) { |
| 289 | /* |
| 290 | * Seek the trace reader. |
| 291 | */ |
| 292 | streamInputReader.seek(timestamp); |
| 293 | |
| 294 | /* |
| 295 | * Add it to the priority queue if there is a current event. |
| 296 | */ |
| 297 | |
| 298 | } |
| 299 | for (StreamInputReader streamInputReader : this.streamInputReaders) { |
| 300 | if (streamInputReader.getCurrentEvent() != null) { |
| 301 | this.prio.add(streamInputReader); |
| 302 | |
| 303 | } |
| 304 | } |
| 305 | return hasMoreEvents(); |
| 306 | } |
| 307 | |
| 308 | /** |
| 309 | * Go to the first entry of a trace |
| 310 | * |
| 311 | * @return 0, the first index. |
| 312 | */ |
| 313 | private long goToZero() { |
| 314 | long tempIndex; |
| 315 | for (StreamInputReader streamInputReader : this.streamInputReaders) { |
| 316 | /* |
| 317 | * Seek the trace reader. |
| 318 | */ |
| 319 | streamInputReader.seek(0); |
| 320 | } |
| 321 | tempIndex = 0; |
| 322 | return tempIndex; |
| 323 | } |
| 324 | |
| 325 | public StreamInputReader getTopStream() { |
| 326 | return this.prio.peek(); |
| 327 | } |
| 328 | |
| 329 | /** |
| 330 | * Does the trace have more events? |
| 331 | * |
| 332 | * @return true if yes. |
| 333 | */ |
| 334 | public boolean hasMoreEvents() { |
| 335 | return this.prio.size() > 0; |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Prints the event count stats. |
| 340 | */ |
| 341 | public void printStats() { |
| 342 | printStats(60); |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Prints the event count stats. |
| 347 | * |
| 348 | * @param width |
| 349 | * Width of the display. |
| 350 | */ |
| 351 | public void printStats(int width) { |
| 352 | int numEvents = 0; |
| 353 | if (width == 0) { |
| 354 | return; |
| 355 | } |
| 356 | |
| 357 | for (long i : this.eventCountPerTraceFile) { |
| 358 | numEvents += i; |
| 359 | } |
| 360 | |
| 361 | for (int j = 0; j < this.eventCountPerTraceFile.length; j++) { |
| 362 | StreamInputReader se = this.streamInputReaders.get(j); |
| 363 | |
| 364 | long len = (width * this.eventCountPerTraceFile[se.getName()]) |
| 365 | / numEvents; |
| 366 | |
| 367 | StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$ |
| 368 | |
| 369 | for (int i = 0; i < len; i++) { |
| 370 | sb.append('+'); |
| 371 | } |
| 372 | |
| 373 | for (long i = len; i < width; i++) { |
| 374 | sb.append(' '); |
| 375 | } |
| 376 | |
| 377 | sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$ |
| 378 | Activator.getDefault().log(sb.toString()); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | public long getEndTime() { |
| 383 | return this.endTime; |
| 384 | } |
| 385 | |
| 386 | @Override |
| 387 | public int hashCode() { |
| 388 | final int prime = 31; |
| 389 | int result = 1; |
| 390 | result = (prime * result) + (int) (startTime ^ (startTime >>> 32)); |
| 391 | result = (prime * result) |
| 392 | + ((streamInputReaders == null) ? 0 : streamInputReaders |
| 393 | .hashCode()); |
| 394 | result = (prime * result) + ((trace == null) ? 0 : trace.hashCode()); |
| 395 | return result; |
| 396 | } |
| 397 | |
| 398 | @Override |
| 399 | public boolean equals(Object obj) { |
| 400 | if (this == obj) { |
| 401 | return true; |
| 402 | } |
| 403 | if (obj == null) { |
| 404 | return false; |
| 405 | } |
| 406 | if (!(obj instanceof CTFTraceReader)) { |
| 407 | return false; |
| 408 | } |
| 409 | CTFTraceReader other = (CTFTraceReader) obj; |
| 410 | if (streamInputReaders == null) { |
| 411 | if (other.streamInputReaders != null) { |
| 412 | return false; |
| 413 | } |
| 414 | } else if (!streamInputReaders.equals(other.streamInputReaders)) { |
| 415 | return false; |
| 416 | } |
| 417 | if (trace == null) { |
| 418 | if (other.trace != null) { |
| 419 | return false; |
| 420 | } |
| 421 | } else if (!trace.equals(other.trace)) { |
| 422 | return false; |
| 423 | } |
| 424 | return true; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * (non-Javadoc) |
| 429 | * |
| 430 | * @see java.lang.Object#toString() |
| 431 | */ |
| 432 | @Override |
| 433 | public String toString() { |
| 434 | /* Only for debugging, shouldn't be externalized */ |
| 435 | return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$ |
| 436 | } |
| 437 | |
| 438 | public CTFTrace getTrace() { |
| 439 | return trace; |
| 440 | } |
| 441 | } |