Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
93a45b54 | 2 | * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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 | * | |
4311ac8b MAL |
9 | * Contributors: |
10 | * Matthew Khouzam - Initial API and implementation | |
11 | * Alexandre Montplaisir - Initial API and implementation | |
866e5b51 FC |
12 | *******************************************************************************/ |
13 | ||
14 | package org.eclipse.linuxtools.ctf.core.trace; | |
15 | ||
0594c61c | 16 | import java.util.ArrayList; |
fe75d403 | 17 | import java.util.HashSet; |
0594c61c | 18 | import java.util.List; |
866e5b51 FC |
19 | import java.util.PriorityQueue; |
20 | import java.util.Set; | |
866e5b51 | 21 | |
866e5b51 | 22 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
a9d52b8f | 23 | import org.eclipse.linuxtools.internal.ctf.core.Activator; |
ce2388e0 FC |
24 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator; |
25 | ||
866e5b51 | 26 | /** |
d37aaa7f | 27 | * A CTF trace reader. Reads the events of a trace. |
1d7277f3 | 28 | * |
d37aaa7f FC |
29 | * @version 1.0 |
30 | * @author Matthew Khouzam | |
31 | * @author Alexandre Montplaisir | |
866e5b51 | 32 | */ |
dd9752d5 | 33 | public class CTFTraceReader implements AutoCloseable { |
866e5b51 | 34 | |
fe75d403 MK |
35 | private static final int MIN_PRIO_SIZE = 16; |
36 | ||
866e5b51 FC |
37 | // ------------------------------------------------------------------------ |
38 | // Attributes | |
39 | // ------------------------------------------------------------------------ | |
40 | ||
41 | /** | |
42 | * The trace to read from. | |
43 | */ | |
93a45b54 | 44 | private final CTFTrace fTrace; |
866e5b51 FC |
45 | |
46 | /** | |
47 | * Vector of all the trace file readers. | |
48 | */ | |
93a45b54 | 49 | private final List<StreamInputReader> fStreamInputReaders = new ArrayList<>(); |
866e5b51 FC |
50 | |
51 | /** | |
52 | * Priority queue to order the trace file readers by timestamp. | |
53 | */ | |
93a45b54 | 54 | private PriorityQueue<StreamInputReader> fPrio; |
866e5b51 FC |
55 | |
56 | /** | |
57 | * Array to count the number of event per trace file. | |
58 | */ | |
93a45b54 | 59 | private long[] fEventCountPerTraceFile; |
866e5b51 FC |
60 | |
61 | /** | |
62 | * Timestamp of the first event in the trace | |
63 | */ | |
93a45b54 | 64 | private long fStartTime; |
866e5b51 FC |
65 | |
66 | /** | |
67 | * Timestamp of the last event read so far | |
68 | */ | |
93a45b54 | 69 | private long fEndTime; |
866e5b51 FC |
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. | |
db8e8f7d AM |
80 | * @throws CTFReaderException |
81 | * if an error occurs | |
866e5b51 | 82 | */ |
db8e8f7d | 83 | public CTFTraceReader(CTFTrace trace) throws CTFReaderException { |
93a45b54 MK |
84 | fTrace = trace; |
85 | fStreamInputReaders.clear(); | |
866e5b51 FC |
86 | |
87 | /** | |
88 | * Create the trace file readers. | |
89 | */ | |
90 | createStreamInputReaders(); | |
91 | ||
92 | /** | |
93 | * Populate the timestamp-based priority queue. | |
94 | */ | |
95 | populateStreamInputReaderHeap(); | |
96 | ||
97 | /** | |
bfe038ff MK |
98 | * Get the start Time of this trace bear in mind that the trace could be |
99 | * empty. | |
866e5b51 | 100 | */ |
93a45b54 | 101 | fStartTime = 0; |
33656d8e | 102 | if (hasMoreEvents()) { |
93a45b54 MK |
103 | fStartTime = fPrio.peek().getCurrentEvent().getTimestamp(); |
104 | setEndTime(fStartTime); | |
33656d8e | 105 | } |
866e5b51 FC |
106 | } |
107 | ||
108 | /** | |
109 | * Copy constructor | |
be6df2d8 AM |
110 | * |
111 | * @return The new CTFTraceReader | |
6a5251eb MK |
112 | * @throws CTFReaderException |
113 | * if an error occurs | |
866e5b51 | 114 | */ |
db8e8f7d | 115 | public CTFTraceReader copyFrom() throws CTFReaderException { |
866e5b51 FC |
116 | CTFTraceReader newReader = null; |
117 | ||
93a45b54 MK |
118 | newReader = new CTFTraceReader(fTrace); |
119 | newReader.fStartTime = fStartTime; | |
120 | newReader.setEndTime(fEndTime); | |
866e5b51 FC |
121 | return newReader; |
122 | } | |
123 | ||
5d1c6919 PT |
124 | /** |
125 | * Dispose the CTFTraceReader | |
db8e8f7d | 126 | * |
dd9752d5 | 127 | * @since 3.0 |
5d1c6919 | 128 | */ |
dd9752d5 AM |
129 | @Override |
130 | public void close() { | |
93a45b54 | 131 | for (StreamInputReader reader : fStreamInputReaders) { |
5d1c6919 | 132 | if (reader != null) { |
dd9752d5 | 133 | reader.close(); |
5d1c6919 PT |
134 | } |
135 | } | |
93a45b54 | 136 | fStreamInputReaders.clear(); |
5d1c6919 PT |
137 | } |
138 | ||
866e5b51 FC |
139 | // ------------------------------------------------------------------------ |
140 | // Getters/Setters/Predicates | |
141 | // ------------------------------------------------------------------------ | |
142 | ||
143 | /** | |
144 | * Return the start time of this trace (== timestamp of the first event) | |
145 | * | |
0d9a6d76 | 146 | * @return the trace start time |
866e5b51 FC |
147 | */ |
148 | public long getStartTime() { | |
93a45b54 | 149 | return fStartTime; |
866e5b51 FC |
150 | } |
151 | ||
6f4e8ec0 AM |
152 | /** |
153 | * Set the trace's end time | |
154 | * | |
155 | * @param endTime | |
156 | * The end time to use | |
157 | */ | |
0594c61c | 158 | protected final void setEndTime(long endTime) { |
93a45b54 | 159 | fEndTime = endTime; |
6f4e8ec0 AM |
160 | } |
161 | ||
0594c61c AM |
162 | /** |
163 | * Get the priority queue of this trace reader. | |
164 | * | |
165 | * @return The priority queue of input readers | |
166 | * @since 2.0 | |
167 | */ | |
168 | protected PriorityQueue<StreamInputReader> getPrio() { | |
93a45b54 | 169 | return fPrio; |
0594c61c AM |
170 | } |
171 | ||
866e5b51 FC |
172 | // ------------------------------------------------------------------------ |
173 | // Operations | |
174 | // ------------------------------------------------------------------------ | |
175 | ||
176 | /** | |
177 | * Creates one trace file reader per trace file contained in the trace. | |
db8e8f7d AM |
178 | * |
179 | * @throws CTFReaderException | |
180 | * if an error occurs | |
866e5b51 | 181 | */ |
db8e8f7d | 182 | private void createStreamInputReaders() throws CTFReaderException { |
866e5b51 FC |
183 | /* |
184 | * For each stream. | |
185 | */ | |
93a45b54 | 186 | for (Stream stream : fTrace.getStreams()) { |
866e5b51 FC |
187 | Set<StreamInput> streamInputs = stream.getStreamInputs(); |
188 | ||
189 | /* | |
190 | * For each trace file of the stream. | |
191 | */ | |
192 | for (StreamInput streamInput : streamInputs) { | |
193 | /* | |
194 | * Create a reader. | |
195 | */ | |
196 | StreamInputReader streamInputReader = new StreamInputReader( | |
197 | streamInput); | |
198 | ||
199 | /* | |
200 | * Add it to the group. | |
201 | */ | |
93a45b54 | 202 | fStreamInputReaders.add(streamInputReader); |
866e5b51 FC |
203 | } |
204 | } | |
205 | ||
206 | /* | |
207 | * Create the array to count the number of event per trace file. | |
208 | */ | |
93a45b54 | 209 | fEventCountPerTraceFile = new long[fStreamInputReaders.size()]; |
866e5b51 FC |
210 | } |
211 | ||
fe75d403 MK |
212 | /** |
213 | * Update the priority queue to make it match the parent trace | |
214 | * | |
215 | * @throws CTFReaderException | |
216 | * An error occured | |
217 | * | |
218 | * @since 3.0 | |
219 | */ | |
220 | public void update() throws CTFReaderException { | |
221 | Set<StreamInputReader> readers = new HashSet<>(); | |
222 | for (Stream stream : fTrace.getStreams()) { | |
223 | Set<StreamInput> streamInputs = stream.getStreamInputs(); | |
224 | for (StreamInput streamInput : streamInputs) { | |
225 | /* | |
226 | * Create a reader. | |
227 | */ | |
228 | StreamInputReader streamInputReader = new StreamInputReader( | |
229 | streamInput); | |
230 | ||
231 | /* | |
232 | * Add it to the group. | |
233 | */ | |
234 | if (!fStreamInputReaders.contains(streamInputReader)) { | |
235 | streamInputReader.readNextEvent(); | |
236 | fStreamInputReaders.add(streamInputReader); | |
237 | readers.add(streamInputReader); | |
238 | } | |
239 | } | |
240 | } | |
241 | long[] temp = fEventCountPerTraceFile; | |
242 | fEventCountPerTraceFile = new long[readers.size() + temp.length]; | |
243 | for (StreamInputReader reader : readers) { | |
244 | fPrio.add(reader); | |
245 | } | |
246 | for (int i = 0; i < temp.length; i++) { | |
247 | fEventCountPerTraceFile[i] = temp[i]; | |
248 | } | |
249 | } | |
250 | ||
866e5b51 FC |
251 | /** |
252 | * Initializes the priority queue used to choose the trace file with the | |
253 | * lower next event timestamp. | |
db8e8f7d AM |
254 | * |
255 | * @throws CTFReaderException | |
256 | * if an error occurs | |
866e5b51 | 257 | */ |
db8e8f7d | 258 | private void populateStreamInputReaderHeap() throws CTFReaderException { |
93a45b54 | 259 | if (fStreamInputReaders.isEmpty()) { |
fe75d403 MK |
260 | fPrio = new PriorityQueue<>(MIN_PRIO_SIZE, |
261 | new StreamInputReaderTimestampComparator()); | |
b5354daa MAL |
262 | return; |
263 | } | |
264 | ||
866e5b51 FC |
265 | /* |
266 | * Create the priority queue with a size twice as bigger as the number | |
267 | * of reader in order to avoid constant resizing. | |
268 | */ | |
93a45b54 | 269 | fPrio = new PriorityQueue<>( |
fe75d403 | 270 | Math.max(fStreamInputReaders.size() * 2, MIN_PRIO_SIZE), |
866e5b51 FC |
271 | new StreamInputReaderTimestampComparator()); |
272 | ||
273 | int pos = 0; | |
274 | ||
93a45b54 | 275 | for (StreamInputReader reader : fStreamInputReaders) { |
866e5b51 FC |
276 | /* |
277 | * Add each trace file reader in the priority queue, if we are able | |
278 | * to read an event from it. | |
279 | */ | |
bfe038ff | 280 | reader.setParent(this); |
6a5251eb MK |
281 | CTFResponse readNextEvent = reader.readNextEvent(); |
282 | if (readNextEvent == CTFResponse.OK || readNextEvent == CTFResponse.WAIT) { | |
93a45b54 | 283 | fPrio.add(reader); |
866e5b51 | 284 | |
93a45b54 | 285 | fEventCountPerTraceFile[pos] = 0; |
866e5b51 FC |
286 | reader.setName(pos); |
287 | ||
288 | pos++; | |
289 | } | |
290 | } | |
291 | } | |
292 | ||
293 | /** | |
294 | * Get the current event, which is the current event of the trace file | |
295 | * reader with the lowest timestamp. | |
296 | * | |
297 | * @return An event definition, or null of the trace reader reached the end | |
298 | * of the trace. | |
299 | */ | |
300 | public EventDefinition getCurrentEventDef() { | |
ce2388e0 | 301 | StreamInputReader top = getTopStream(); |
866e5b51 FC |
302 | |
303 | return (top != null) ? top.getCurrentEvent() : null; | |
304 | } | |
305 | ||
306 | /** | |
307 | * Go to the next event. | |
308 | * | |
309 | * @return True if an event was read. | |
db8e8f7d AM |
310 | * @throws CTFReaderException |
311 | * if an error occurs | |
866e5b51 | 312 | */ |
db8e8f7d | 313 | public boolean advance() throws CTFReaderException { |
866e5b51 FC |
314 | /* |
315 | * Remove the reader from the top of the priority queue. | |
316 | */ | |
93a45b54 | 317 | StreamInputReader top = fPrio.poll(); |
866e5b51 FC |
318 | |
319 | /* | |
320 | * If the queue was empty. | |
321 | */ | |
322 | if (top == null) { | |
323 | return false; | |
324 | } | |
866e5b51 FC |
325 | /* |
326 | * Read the next event of this reader. | |
327 | */ | |
6a5251eb MK |
328 | switch (top.readNextEvent()) { |
329 | case OK: { | |
866e5b51 FC |
330 | /* |
331 | * Add it back in the queue. | |
332 | */ | |
93a45b54 MK |
333 | fPrio.add(top); |
334 | final long topEnd = fTrace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp()); | |
335 | setEndTime(Math.max(topEnd, getEndTime())); | |
336 | fEventCountPerTraceFile[top.getName()]++; | |
866e5b51 | 337 | |
bfe038ff | 338 | if (top.getCurrentEvent() != null) { |
93a45b54 MK |
339 | fEndTime = Math.max(top.getCurrentEvent().getTimestamp(), |
340 | fEndTime); | |
bfe038ff | 341 | } |
6a5251eb MK |
342 | break; |
343 | } | |
344 | case WAIT: { | |
345 | fPrio.add(top); | |
346 | break; | |
347 | } | |
348 | case FINISH: | |
349 | break; | |
350 | case ERROR: | |
351 | default: | |
352 | // something bad happend | |
bfe038ff | 353 | } |
866e5b51 FC |
354 | /* |
355 | * If there is no reader in the queue, it means the trace reader reached | |
356 | * the end of the trace. | |
357 | */ | |
bfe038ff | 358 | return hasMoreEvents(); |
866e5b51 FC |
359 | } |
360 | ||
361 | /** | |
362 | * Go to the last event in the trace. | |
db8e8f7d AM |
363 | * |
364 | * @throws CTFReaderException | |
365 | * if an error occurs | |
866e5b51 | 366 | */ |
db8e8f7d | 367 | public void goToLastEvent() throws CTFReaderException { |
93a45b54 MK |
368 | seek(getEndTime()); |
369 | while (fPrio.size() > 1) { | |
370 | advance(); | |
866e5b51 FC |
371 | } |
372 | } | |
373 | ||
374 | /** | |
ecb12461 EB |
375 | * Seeks to a given timestamp. It will seek to the nearest event greater or |
376 | * equal to timestamp. If a trace is [10 20 30 40] and you are looking for | |
377 | * 19, it will give you 20. If you want 20, you will get 20, if you want 21, | |
378 | * you will get 30. The value -inf will seek to the first element and the | |
379 | * value +inf will seek to the end of the file (past the last event). | |
866e5b51 FC |
380 | * |
381 | * @param timestamp | |
382 | * the timestamp to seek to | |
6a5251eb MK |
383 | * @return true if there are events above or equal the seek timestamp, false |
384 | * if seek at the end of the trace (no valid event). | |
db8e8f7d AM |
385 | * @throws CTFReaderException |
386 | * if an error occurs | |
866e5b51 | 387 | */ |
db8e8f7d | 388 | public boolean seek(long timestamp) throws CTFReaderException { |
866e5b51 FC |
389 | /* |
390 | * Remove all the trace readers from the priority queue | |
391 | */ | |
93a45b54 MK |
392 | fPrio.clear(); |
393 | for (StreamInputReader streamInputReader : fStreamInputReaders) { | |
866e5b51 FC |
394 | /* |
395 | * Seek the trace reader. | |
396 | */ | |
bfe038ff | 397 | streamInputReader.seek(timestamp); |
ce2388e0 FC |
398 | |
399 | /* | |
400 | * Add it to the priority queue if there is a current event. | |
401 | */ | |
ce2388e0 | 402 | if (streamInputReader.getCurrentEvent() != null) { |
93a45b54 | 403 | fPrio.add(streamInputReader); |
ce2388e0 FC |
404 | } |
405 | } | |
866e5b51 FC |
406 | return hasMoreEvents(); |
407 | } | |
408 | ||
9ac2eb62 | 409 | /** |
ecb12461 | 410 | * Gets the stream with the oldest event |
9ac2eb62 MK |
411 | * |
412 | * @return the stream with the oldest event | |
413 | */ | |
ce2388e0 | 414 | public StreamInputReader getTopStream() { |
93a45b54 | 415 | return fPrio.peek(); |
ce2388e0 FC |
416 | } |
417 | ||
866e5b51 FC |
418 | /** |
419 | * Does the trace have more events? | |
420 | * | |
421 | * @return true if yes. | |
422 | */ | |
0594c61c | 423 | public final boolean hasMoreEvents() { |
93a45b54 | 424 | return fPrio.size() > 0; |
866e5b51 FC |
425 | } |
426 | ||
427 | /** | |
428 | * Prints the event count stats. | |
429 | */ | |
430 | public void printStats() { | |
431 | printStats(60); | |
432 | } | |
433 | ||
434 | /** | |
435 | * Prints the event count stats. | |
436 | * | |
437 | * @param width | |
438 | * Width of the display. | |
439 | */ | |
440 | public void printStats(int width) { | |
441 | int numEvents = 0; | |
442 | if (width == 0) { | |
443 | return; | |
444 | } | |
445 | ||
93a45b54 | 446 | for (long i : fEventCountPerTraceFile) { |
866e5b51 FC |
447 | numEvents += i; |
448 | } | |
449 | ||
93a45b54 MK |
450 | for (int j = 0; j < fEventCountPerTraceFile.length; j++) { |
451 | StreamInputReader se = fStreamInputReaders.get(j); | |
866e5b51 | 452 | |
93a45b54 | 453 | long len = (width * fEventCountPerTraceFile[se.getName()]) |
866e5b51 FC |
454 | / numEvents; |
455 | ||
0594c61c AM |
456 | StringBuilder sb = new StringBuilder(se.getFilename()); |
457 | sb.append("\t["); //$NON-NLS-1$ | |
866e5b51 FC |
458 | |
459 | for (int i = 0; i < len; i++) { | |
460 | sb.append('+'); | |
461 | } | |
462 | ||
bfe038ff | 463 | for (long i = len; i < width; i++) { |
866e5b51 FC |
464 | sb.append(' '); |
465 | } | |
466 | ||
93a45b54 | 467 | sb.append("]\t" + fEventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$ |
4311ac8b | 468 | Activator.log(sb.toString()); |
866e5b51 FC |
469 | } |
470 | } | |
471 | ||
9ac2eb62 | 472 | /** |
ecb12461 | 473 | * Gets the last event timestamp that was read. This is NOT necessarily the |
9ac2eb62 MK |
474 | * last event in a trace, just the last one read so far. |
475 | * | |
476 | * @return the last event | |
477 | */ | |
866e5b51 | 478 | public long getEndTime() { |
93a45b54 | 479 | return fEndTime; |
866e5b51 FC |
480 | } |
481 | ||
6a5251eb MK |
482 | /** |
483 | * Sets a trace to be live or not | |
484 | * | |
485 | * @param live | |
486 | * whether the trace is live | |
487 | * @since 3.0 | |
488 | */ | |
489 | public void setLive(boolean live) { | |
490 | for (StreamInputReader s : fPrio) { | |
491 | s.setLive(live); | |
492 | } | |
493 | } | |
494 | ||
495 | /** | |
496 | * Get if the trace is to read live or not | |
497 | * | |
498 | * @return whether the trace is live or not | |
499 | * @since 3.0 | |
500 | * | |
501 | */ | |
502 | public boolean isLive() { | |
503 | return fPrio.peek().isLive(); | |
504 | } | |
505 | ||
866e5b51 FC |
506 | @Override |
507 | public int hashCode() { | |
508 | final int prime = 31; | |
509 | int result = 1; | |
93a45b54 MK |
510 | result = (prime * result) + (int) (fStartTime ^ (fStartTime >>> 32)); |
511 | result = (prime * result) + fStreamInputReaders.hashCode(); | |
512 | result = (prime * result) + ((fTrace == null) ? 0 : fTrace.hashCode()); | |
866e5b51 FC |
513 | return result; |
514 | } | |
515 | ||
516 | @Override | |
517 | public boolean equals(Object obj) { | |
518 | if (this == obj) { | |
519 | return true; | |
520 | } | |
521 | if (obj == null) { | |
522 | return false; | |
523 | } | |
07002e0a | 524 | if (!(obj instanceof CTFTraceReader)) { |
866e5b51 FC |
525 | return false; |
526 | } | |
527 | CTFTraceReader other = (CTFTraceReader) obj; | |
93a45b54 | 528 | if (!fStreamInputReaders.equals(other.fStreamInputReaders)) { |
866e5b51 FC |
529 | return false; |
530 | } | |
93a45b54 MK |
531 | if (fTrace == null) { |
532 | if (other.fTrace != null) { | |
866e5b51 FC |
533 | return false; |
534 | } | |
93a45b54 | 535 | } else if (!fTrace.equals(other.fTrace)) { |
866e5b51 FC |
536 | return false; |
537 | } | |
538 | return true; | |
539 | } | |
540 | ||
866e5b51 FC |
541 | @Override |
542 | public String toString() { | |
543 | /* Only for debugging, shouldn't be externalized */ | |
93a45b54 | 544 | return "CTFTraceReader [trace=" + fTrace + ']'; //$NON-NLS-1$ |
866e5b51 FC |
545 | } |
546 | ||
9ac2eb62 MK |
547 | /** |
548 | * Gets the parent trace | |
549 | * | |
550 | * @return the parent trace | |
551 | */ | |
866e5b51 | 552 | public CTFTrace getTrace() { |
93a45b54 | 553 | return fTrace; |
866e5b51 FC |
554 | } |
555 | } |