Commit | Line | Data |
---|---|---|
866e5b51 FC |
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 | ||
866e5b51 | 20 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
a9d52b8f | 21 | import org.eclipse.linuxtools.internal.ctf.core.Activator; |
ce2388e0 FC |
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.StreamInputPacketIndexEntry; | |
25 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator; | |
26 | ||
866e5b51 FC |
27 | /** |
28 | * Reads the events of a trace. | |
29 | */ | |
30 | ||
31 | public class CTFTraceReader { | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
37 | /** | |
38 | * The trace to read from. | |
39 | */ | |
40 | private final CTFTrace trace; | |
41 | ||
42 | /** | |
43 | * Vector of all the trace file readers. | |
44 | */ | |
45 | private final Vector<StreamInputReader> streamInputReaders = new Vector<StreamInputReader>(); | |
46 | ||
47 | /** | |
48 | * Priority queue to order the trace file readers by timestamp. | |
49 | */ | |
50 | protected PriorityQueue<StreamInputReader> prio; | |
51 | ||
52 | /** | |
53 | * Array to count the number of event per trace file. | |
54 | */ | |
55 | private int[] eventCountPerTraceFile; | |
56 | ||
57 | /** | |
58 | * Timestamp of the first event in the trace | |
59 | */ | |
60 | private long startTime; | |
61 | ||
62 | /** | |
63 | * Timestamp of the last event read so far | |
64 | */ | |
65 | private long endTime; | |
66 | ||
ce2388e0 FC |
67 | /** |
68 | * Current event index | |
69 | */ | |
79cb3749 | 70 | private long fIndex; |
ce2388e0 FC |
71 | |
72 | private final long startIndex[]; | |
73 | ||
866e5b51 FC |
74 | // ------------------------------------------------------------------------ |
75 | // Constructors | |
76 | // ------------------------------------------------------------------------ | |
77 | ||
78 | /** | |
79 | * Constructs a TraceReader to read a trace. | |
80 | * | |
81 | * @param trace | |
82 | * The trace to read from. | |
83 | * @throws CTFReaderException | |
84 | */ | |
85 | public CTFTraceReader(CTFTrace trace) { | |
86 | this.trace = trace; | |
87 | ||
88 | /** | |
89 | * Create the trace file readers. | |
90 | */ | |
91 | createStreamInputReaders(); | |
92 | ||
93 | /** | |
94 | * Populate the timestamp-based priority queue. | |
95 | */ | |
96 | populateStreamInputReaderHeap(); | |
97 | ||
98 | /** | |
99 | * Get the start Time of this trace | |
100 | */ | |
101 | this.startTime = prio.peek().getCurrentEvent().timestamp; | |
102 | this.endTime = this.startTime; | |
79cb3749 | 103 | this.fIndex = 0; |
ce2388e0 FC |
104 | startIndex = new long[prio.size()]; |
105 | for (int i = 0; i < prio.size(); i++) { | |
106 | startIndex[i] = 0; | |
107 | } | |
866e5b51 FC |
108 | } |
109 | ||
110 | /** | |
111 | * Copy constructor | |
866e5b51 FC |
112 | */ |
113 | public CTFTraceReader copyFrom() { | |
114 | CTFTraceReader newReader = null; | |
115 | ||
116 | newReader = new CTFTraceReader(this.trace); | |
117 | newReader.startTime = this.startTime; | |
118 | newReader.endTime = this.endTime; | |
119 | return newReader; | |
120 | } | |
121 | ||
122 | // ------------------------------------------------------------------------ | |
123 | // Getters/Setters/Predicates | |
124 | // ------------------------------------------------------------------------ | |
125 | ||
126 | /** | |
127 | * Return the start time of this trace (== timestamp of the first event) | |
128 | * | |
0d9a6d76 | 129 | * @return the trace start time |
866e5b51 FC |
130 | */ |
131 | public long getStartTime() { | |
132 | return this.startTime; | |
133 | } | |
134 | ||
ce2388e0 FC |
135 | /** |
136 | * @return the index | |
137 | */ | |
138 | public long getIndex() { | |
79cb3749 | 139 | return fIndex; |
ce2388e0 FC |
140 | } |
141 | ||
866e5b51 FC |
142 | // ------------------------------------------------------------------------ |
143 | // Operations | |
144 | // ------------------------------------------------------------------------ | |
145 | ||
146 | /** | |
147 | * Creates one trace file reader per trace file contained in the trace. | |
148 | */ | |
149 | private void createStreamInputReaders() { | |
150 | Collection<Stream> streams = this.trace.getStreams().values(); | |
151 | ||
152 | /* | |
153 | * For each stream. | |
154 | */ | |
155 | for (Stream stream : streams) { | |
156 | Set<StreamInput> streamInputs = stream.getStreamInputs(); | |
157 | ||
158 | /* | |
159 | * For each trace file of the stream. | |
160 | */ | |
161 | for (StreamInput streamInput : streamInputs) { | |
162 | /* | |
163 | * Create a reader. | |
164 | */ | |
165 | StreamInputReader streamInputReader = new StreamInputReader( | |
166 | streamInput); | |
167 | ||
168 | /* | |
169 | * Add it to the group. | |
170 | */ | |
171 | this.streamInputReaders.add(streamInputReader); | |
172 | } | |
173 | } | |
174 | ||
175 | /* | |
176 | * Create the array to count the number of event per trace file. | |
177 | */ | |
178 | this.eventCountPerTraceFile = new int[this.streamInputReaders.size()]; | |
179 | } | |
180 | ||
181 | /** | |
182 | * Initializes the priority queue used to choose the trace file with the | |
183 | * lower next event timestamp. | |
184 | */ | |
185 | private void populateStreamInputReaderHeap() { | |
186 | /* | |
187 | * Create the priority queue with a size twice as bigger as the number | |
188 | * of reader in order to avoid constant resizing. | |
189 | */ | |
190 | this.prio = new PriorityQueue<StreamInputReader>( | |
191 | this.streamInputReaders.size() * 2, | |
192 | new StreamInputReaderTimestampComparator()); | |
193 | ||
194 | int pos = 0; | |
195 | ||
196 | for (StreamInputReader reader : this.streamInputReaders) { | |
197 | /* | |
198 | * Add each trace file reader in the priority queue, if we are able | |
199 | * to read an event from it. | |
200 | */ | |
201 | if (reader.readNextEvent()) { | |
202 | this.prio.add(reader); | |
203 | ||
204 | this.eventCountPerTraceFile[pos] = 0; | |
205 | reader.setName(pos); | |
206 | ||
207 | pos++; | |
208 | } | |
209 | } | |
210 | } | |
211 | ||
212 | /** | |
213 | * Get the current event, which is the current event of the trace file | |
214 | * reader with the lowest timestamp. | |
215 | * | |
216 | * @return An event definition, or null of the trace reader reached the end | |
217 | * of the trace. | |
218 | */ | |
219 | public EventDefinition getCurrentEventDef() { | |
ce2388e0 | 220 | StreamInputReader top = getTopStream(); |
866e5b51 FC |
221 | |
222 | return (top != null) ? top.getCurrentEvent() : null; | |
223 | } | |
224 | ||
225 | /** | |
226 | * Go to the next event. | |
227 | * | |
228 | * @return True if an event was read. | |
229 | */ | |
230 | public boolean advance() { | |
ce2388e0 FC |
231 | /* |
232 | * Index the | |
233 | */ | |
866e5b51 FC |
234 | /* |
235 | * Remove the reader from the top of the priority queue. | |
236 | */ | |
237 | StreamInputReader top = this.prio.poll(); | |
238 | ||
239 | /* | |
240 | * If the queue was empty. | |
241 | */ | |
242 | if (top == null) { | |
243 | return false; | |
244 | } | |
ce2388e0 FC |
245 | /* |
246 | * index if needed | |
247 | */ | |
248 | if (hasMoreEvents()) { | |
249 | StreamInputPacketReader packetReader = top.getPacketReader(); | |
250 | boolean packetHasMoreEvents = packetReader.hasMoreEvents(); | |
251 | StreamInputPacketIndexEntry currentPacket = packetReader | |
252 | .getCurrentPacket(); | |
253 | if (!packetHasMoreEvents) { | |
254 | int n = this.streamInputReaders.indexOf(top); | |
255 | currentPacket.setIndexBegin(startIndex[n]); | |
79cb3749 AM |
256 | currentPacket.setIndexEnd(fIndex); |
257 | startIndex[n] = fIndex + 1; | |
ce2388e0 FC |
258 | } |
259 | } | |
866e5b51 FC |
260 | /* |
261 | * Read the next event of this reader. | |
262 | */ | |
263 | if (top.readNextEvent()) { | |
264 | /* | |
265 | * Add it back in the queue. | |
266 | */ | |
267 | this.prio.add(top); | |
268 | final long topEnd = top.getCurrentEvent().timestamp; | |
269 | this.endTime = Math.max(topEnd, this.endTime); | |
270 | this.eventCountPerTraceFile[top.getName()]++; | |
ce2388e0 FC |
271 | /* |
272 | * increment the index | |
273 | */ | |
79cb3749 | 274 | fIndex++; |
866e5b51 | 275 | } |
ce2388e0 | 276 | boolean hasMoreEvents = hasMoreEvents(); |
866e5b51 FC |
277 | |
278 | /* | |
279 | * If there is no reader in the queue, it means the trace reader reached | |
280 | * the end of the trace. | |
281 | */ | |
ce2388e0 | 282 | return hasMoreEvents; |
866e5b51 FC |
283 | } |
284 | ||
285 | /** | |
286 | * Go to the last event in the trace. | |
287 | * | |
288 | * @throws CTFReaderException | |
289 | */ | |
290 | public void goToLastEvent() throws CTFReaderException { | |
291 | ||
866e5b51 FC |
292 | for (StreamInputReader streamInputReader : this.streamInputReaders) { |
293 | /* | |
294 | * Seek the trace reader. | |
295 | */ | |
296 | streamInputReader.goToLastEvent(); | |
297 | } | |
298 | int count = prio.size(); | |
ce2388e0 | 299 | for (int i = 0; i < (count-1); i++) { |
866e5b51 FC |
300 | advance(); |
301 | } | |
302 | } | |
303 | ||
304 | /** | |
305 | * Seeks to a given timestamp It will go to the event just after the | |
306 | * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and | |
307 | * you're looking for 19, it'll give you 20, it you want 20, you'll get 20, | |
308 | * if you want 21, you'll get 30. You want -inf, you'll get the first | |
309 | * element, you want +inf, you'll get the end of the file with no events. | |
310 | * | |
311 | * @param timestamp | |
312 | * the timestamp to seek to | |
0d9a6d76 | 313 | * @return true if the trace has more events following the timestamp |
866e5b51 FC |
314 | */ |
315 | public boolean seek(long timestamp) { | |
316 | /* | |
317 | * Remove all the trace readers from the priority queue | |
318 | */ | |
319 | this.prio.clear(); | |
79cb3749 | 320 | fIndex = 0; |
ce2388e0 | 321 | long offset = 0; |
866e5b51 FC |
322 | for (StreamInputReader streamInputReader : this.streamInputReaders) { |
323 | /* | |
324 | * Seek the trace reader. | |
325 | */ | |
ce2388e0 FC |
326 | offset += streamInputReader.seek(timestamp); |
327 | ||
328 | /* | |
329 | * Add it to the priority queue if there is a current event. | |
330 | */ | |
331 | ||
332 | } | |
333 | for (StreamInputReader streamInputReader : this.streamInputReaders) { | |
334 | if (streamInputReader.getCurrentEvent() != null) { | |
335 | this.prio.add(streamInputReader); | |
79cb3749 | 336 | fIndex = Math.max(fIndex, streamInputReader.getPacketReader() |
ce2388e0 FC |
337 | .getCurrentPacket().getIndexBegin() |
338 | + offset); | |
339 | } | |
340 | } | |
341 | return hasMoreEvents(); | |
342 | } | |
343 | ||
344 | public boolean seekIndex(long index) { | |
345 | this.prio.clear(); | |
346 | ||
347 | long tempIndex = Long.MIN_VALUE; | |
348 | long tempTimestamp = Long.MIN_VALUE; | |
349 | try { | |
350 | for (StreamInputReader streamInputReader : this.streamInputReaders) { | |
351 | /* | |
352 | * Seek the trace reader. | |
353 | */ | |
354 | final long streamIndex = streamInputReader.seekIndex(index); | |
459ebacd MK |
355 | if( streamIndex != -1 ) |
356 | { | |
357 | tempIndex = Math.max(tempIndex, streamIndex); | |
358 | EventDefinition currentEvent = streamInputReader.getCurrentEvent(); | |
2b7f6f09 | 359 | /* |
459ebacd | 360 | * Maybe we're at the beginning of a trace. |
2b7f6f09 | 361 | */ |
459ebacd MK |
362 | if( currentEvent == null ){ |
363 | streamInputReader.readNextEvent(); | |
364 | currentEvent = streamInputReader.getCurrentEvent(); | |
365 | } | |
366 | if( currentEvent != null ) { | |
367 | tempTimestamp = Math.max(tempTimestamp, | |
368 | currentEvent.timestamp); | |
369 | } else { | |
370 | /* | |
371 | * probably beyond the last event | |
372 | */ | |
373 | tempIndex = goToZero(); | |
374 | } | |
2b7f6f09 | 375 | } |
866e5b51 | 376 | |
459ebacd | 377 | |
ce2388e0 FC |
378 | } |
379 | } catch (CTFReaderException e) { | |
380 | /* | |
381 | * Important, if it failed, it's because it's not yet indexed, so we | |
382 | * have to manually advance to the right value. | |
383 | */ | |
2b7f6f09 | 384 | tempIndex = goToZero(); |
ce2388e0 FC |
385 | } |
386 | for (StreamInputReader streamInputReader : this.streamInputReaders) { | |
866e5b51 FC |
387 | /* |
388 | * Add it to the priority queue if there is a current event. | |
389 | */ | |
ce2388e0 | 390 | |
866e5b51 FC |
391 | if (streamInputReader.getCurrentEvent() != null) { |
392 | this.prio.add(streamInputReader); | |
393 | } | |
394 | } | |
ce2388e0 FC |
395 | if (tempIndex == Long.MAX_VALUE) { |
396 | tempIndex = 0; | |
397 | } | |
398 | long pos = tempIndex; | |
399 | if (index > tempIndex) { | |
400 | /* | |
401 | * advance for offset | |
402 | */ | |
403 | while ((prio.peek().getCurrentEvent().timestamp < tempTimestamp) | |
404 | && hasMoreEvents()) { | |
405 | this.advance(); | |
406 | } | |
866e5b51 | 407 | |
ce2388e0 FC |
408 | for (pos = tempIndex; (pos < index) && hasMoreEvents(); pos++) { |
409 | this.advance(); | |
410 | } | |
411 | } | |
79cb3749 | 412 | this.fIndex = pos; |
866e5b51 FC |
413 | return hasMoreEvents(); |
414 | } | |
415 | ||
2b7f6f09 MK |
416 | /** |
417 | * Go to the first entry of a trace | |
418 | * @return 0, the first index. | |
419 | */ | |
420 | private long goToZero() { | |
421 | long tempIndex; | |
422 | for (StreamInputReader streamInputReader : this.streamInputReaders) { | |
423 | /* | |
424 | * Seek the trace reader. | |
425 | */ | |
426 | streamInputReader.seek(0); | |
427 | } | |
428 | tempIndex = 0; | |
429 | return tempIndex; | |
430 | } | |
431 | ||
ce2388e0 FC |
432 | public StreamInputReader getTopStream() { |
433 | return this.prio.peek(); | |
434 | } | |
435 | ||
866e5b51 FC |
436 | /** |
437 | * Does the trace have more events? | |
438 | * | |
439 | * @return true if yes. | |
440 | */ | |
441 | public boolean hasMoreEvents() { | |
442 | return this.prio.size() > 0; | |
443 | } | |
444 | ||
445 | /** | |
446 | * Prints the event count stats. | |
447 | */ | |
448 | public void printStats() { | |
449 | printStats(60); | |
450 | } | |
451 | ||
452 | /** | |
453 | * Prints the event count stats. | |
454 | * | |
455 | * @param width | |
456 | * Width of the display. | |
457 | */ | |
458 | public void printStats(int width) { | |
459 | int numEvents = 0; | |
460 | if (width == 0) { | |
461 | return; | |
462 | } | |
463 | ||
464 | for (int i : this.eventCountPerTraceFile) { | |
465 | numEvents += i; | |
466 | } | |
467 | ||
468 | for (int j = 0; j < this.eventCountPerTraceFile.length; j++) { | |
469 | StreamInputReader se = this.streamInputReaders.get(j); | |
470 | ||
471 | int len = (width * this.eventCountPerTraceFile[se.getName()]) | |
472 | / numEvents; | |
473 | ||
ce2388e0 | 474 | StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$ |
866e5b51 FC |
475 | |
476 | for (int i = 0; i < len; i++) { | |
477 | sb.append('+'); | |
478 | } | |
479 | ||
480 | for (int i = len; i < width; i++) { | |
481 | sb.append(' '); | |
482 | } | |
483 | ||
484 | sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$ | |
a9d52b8f | 485 | Activator.getDefault().log(sb.toString()); |
866e5b51 FC |
486 | } |
487 | } | |
488 | ||
489 | public long getEndTime() { | |
490 | return this.endTime; | |
491 | } | |
492 | ||
493 | @Override | |
494 | public int hashCode() { | |
495 | final int prime = 31; | |
496 | int result = 1; | |
497 | result = (prime * result) + (int) (endTime ^ (endTime >>> 32)); | |
498 | result = (prime * result) + (int) (startTime ^ (startTime >>> 32)); | |
499 | result = (prime * result) | |
ce2388e0 FC |
500 | + ((streamInputReaders == null) ? 0 : streamInputReaders |
501 | .hashCode()); | |
866e5b51 FC |
502 | result = (prime * result) + ((trace == null) ? 0 : trace.hashCode()); |
503 | return result; | |
504 | } | |
505 | ||
506 | @Override | |
507 | public boolean equals(Object obj) { | |
508 | if (this == obj) { | |
509 | return true; | |
510 | } | |
511 | if (obj == null) { | |
512 | return false; | |
513 | } | |
514 | if (getClass() != obj.getClass()) { | |
515 | return false; | |
516 | } | |
517 | CTFTraceReader other = (CTFTraceReader) obj; | |
518 | if (endTime != other.endTime) { | |
519 | return false; | |
520 | } | |
521 | if (startTime != other.startTime) { | |
522 | return false; | |
523 | } | |
524 | if (streamInputReaders == null) { | |
525 | if (other.streamInputReaders != null) { | |
526 | return false; | |
527 | } | |
528 | } else if (!streamInputReaders.equals(other.streamInputReaders)) { | |
529 | return false; | |
530 | } | |
531 | if (trace == null) { | |
532 | if (other.trace != null) { | |
533 | return false; | |
534 | } | |
535 | } else if (!trace.equals(other.trace)) { | |
536 | return false; | |
537 | } | |
538 | return true; | |
539 | } | |
540 | ||
541 | /* | |
542 | * (non-Javadoc) | |
543 | * | |
544 | * @see java.lang.Object#toString() | |
545 | */ | |
546 | @Override | |
547 | public String toString() { | |
548 | /* Only for debugging, shouldn't be externalized */ | |
549 | return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$ | |
550 | } | |
551 | ||
552 | public CTFTrace getTrace() { | |
553 | return trace; | |
554 | } | |
555 | } |