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