tmf: Split "CTF adaptor" into separate plugins/feature
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTraceReader.java
CommitLineData
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
14package org.eclipse.linuxtools.ctf.core.trace;
15
0594c61c 16import java.util.ArrayList;
fe75d403 17import java.util.HashSet;
0594c61c 18import java.util.List;
866e5b51
FC
19import java.util.PriorityQueue;
20import java.util.Set;
866e5b51 21
866e5b51 22import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
a9d52b8f 23import org.eclipse.linuxtools.internal.ctf.core.Activator;
ce2388e0
FC
24import 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 */
866e5b51
FC
33public class CTFTraceReader {
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 *
5d1c6919
PT
127 * @since 2.0
128 */
129 public void dispose() {
93a45b54 130 for (StreamInputReader reader : fStreamInputReaders) {
5d1c6919
PT
131 if (reader != null) {
132 reader.dispose();
133 }
134 }
93a45b54 135 fStreamInputReaders.clear();
5d1c6919
PT
136 }
137
866e5b51
FC
138 // ------------------------------------------------------------------------
139 // Getters/Setters/Predicates
140 // ------------------------------------------------------------------------
141
142 /**
143 * Return the start time of this trace (== timestamp of the first event)
144 *
0d9a6d76 145 * @return the trace start time
866e5b51
FC
146 */
147 public long getStartTime() {
93a45b54 148 return fStartTime;
866e5b51
FC
149 }
150
6f4e8ec0
AM
151 /**
152 * Set the trace's end time
153 *
154 * @param endTime
155 * The end time to use
156 */
0594c61c 157 protected final void setEndTime(long endTime) {
93a45b54 158 fEndTime = endTime;
6f4e8ec0
AM
159 }
160
0594c61c
AM
161 /**
162 * Get the priority queue of this trace reader.
163 *
164 * @return The priority queue of input readers
165 * @since 2.0
166 */
167 protected PriorityQueue<StreamInputReader> getPrio() {
93a45b54 168 return fPrio;
0594c61c
AM
169 }
170
866e5b51
FC
171 // ------------------------------------------------------------------------
172 // Operations
173 // ------------------------------------------------------------------------
174
175 /**
176 * Creates one trace file reader per trace file contained in the trace.
db8e8f7d
AM
177 *
178 * @throws CTFReaderException
179 * if an error occurs
866e5b51 180 */
db8e8f7d 181 private void createStreamInputReaders() throws CTFReaderException {
866e5b51
FC
182 /*
183 * For each stream.
184 */
93a45b54 185 for (Stream stream : fTrace.getStreams()) {
866e5b51
FC
186 Set<StreamInput> streamInputs = stream.getStreamInputs();
187
188 /*
189 * For each trace file of the stream.
190 */
191 for (StreamInput streamInput : streamInputs) {
192 /*
193 * Create a reader.
194 */
195 StreamInputReader streamInputReader = new StreamInputReader(
196 streamInput);
197
198 /*
199 * Add it to the group.
200 */
93a45b54 201 fStreamInputReaders.add(streamInputReader);
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 {
220 Set<StreamInputReader> readers = new HashSet<>();
221 for (Stream stream : fTrace.getStreams()) {
222 Set<StreamInput> streamInputs = stream.getStreamInputs();
223 for (StreamInput streamInput : streamInputs) {
224 /*
225 * Create a reader.
226 */
227 StreamInputReader streamInputReader = new StreamInputReader(
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];
242 for (StreamInputReader reader : readers) {
243 fPrio.add(reader);
244 }
245 for (int i = 0; i < temp.length; i++) {
246 fEventCountPerTraceFile[i] = temp[i];
247 }
248 }
249
866e5b51
FC
250 /**
251 * Initializes the priority queue used to choose the trace file with the
252 * lower next event timestamp.
db8e8f7d
AM
253 *
254 * @throws CTFReaderException
255 * if an error occurs
866e5b51 256 */
db8e8f7d 257 private void populateStreamInputReaderHeap() throws CTFReaderException {
93a45b54 258 if (fStreamInputReaders.isEmpty()) {
fe75d403
MK
259 fPrio = new PriorityQueue<>(MIN_PRIO_SIZE,
260 new StreamInputReaderTimestampComparator());
b5354daa
MAL
261 return;
262 }
263
866e5b51
FC
264 /*
265 * Create the priority queue with a size twice as bigger as the number
266 * of reader in order to avoid constant resizing.
267 */
93a45b54 268 fPrio = new PriorityQueue<>(
fe75d403 269 Math.max(fStreamInputReaders.size() * 2, MIN_PRIO_SIZE),
866e5b51
FC
270 new StreamInputReaderTimestampComparator());
271
272 int pos = 0;
273
93a45b54 274 for (StreamInputReader reader : fStreamInputReaders) {
866e5b51
FC
275 /*
276 * Add each trace file reader in the priority queue, if we are able
277 * to read an event from it.
278 */
bfe038ff 279 reader.setParent(this);
6a5251eb
MK
280 CTFResponse readNextEvent = reader.readNextEvent();
281 if (readNextEvent == CTFResponse.OK || readNextEvent == CTFResponse.WAIT) {
93a45b54 282 fPrio.add(reader);
866e5b51 283
93a45b54 284 fEventCountPerTraceFile[pos] = 0;
866e5b51
FC
285 reader.setName(pos);
286
287 pos++;
288 }
289 }
290 }
291
292 /**
293 * Get the current event, which is the current event of the trace file
294 * reader with the lowest timestamp.
295 *
296 * @return An event definition, or null of the trace reader reached the end
297 * of the trace.
298 */
299 public EventDefinition getCurrentEventDef() {
ce2388e0 300 StreamInputReader top = getTopStream();
866e5b51
FC
301
302 return (top != null) ? top.getCurrentEvent() : null;
303 }
304
305 /**
306 * Go to the next event.
307 *
308 * @return True if an event was read.
db8e8f7d
AM
309 * @throws CTFReaderException
310 * if an error occurs
866e5b51 311 */
db8e8f7d 312 public boolean advance() throws CTFReaderException {
866e5b51
FC
313 /*
314 * Remove the reader from the top of the priority queue.
315 */
93a45b54 316 StreamInputReader top = fPrio.poll();
866e5b51
FC
317
318 /*
319 * If the queue was empty.
320 */
321 if (top == null) {
322 return false;
323 }
866e5b51
FC
324 /*
325 * Read the next event of this reader.
326 */
6a5251eb
MK
327 switch (top.readNextEvent()) {
328 case OK: {
866e5b51
FC
329 /*
330 * Add it back in the queue.
331 */
93a45b54
MK
332 fPrio.add(top);
333 final long topEnd = fTrace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
334 setEndTime(Math.max(topEnd, getEndTime()));
335 fEventCountPerTraceFile[top.getName()]++;
866e5b51 336
bfe038ff 337 if (top.getCurrentEvent() != null) {
93a45b54
MK
338 fEndTime = Math.max(top.getCurrentEvent().getTimestamp(),
339 fEndTime);
bfe038ff 340 }
6a5251eb
MK
341 break;
342 }
343 case WAIT: {
344 fPrio.add(top);
345 break;
346 }
347 case FINISH:
348 break;
349 case ERROR:
350 default:
351 // something bad happend
bfe038ff 352 }
866e5b51
FC
353 /*
354 * If there is no reader in the queue, it means the trace reader reached
355 * the end of the trace.
356 */
bfe038ff 357 return hasMoreEvents();
866e5b51
FC
358 }
359
360 /**
361 * Go to the last event in the trace.
db8e8f7d
AM
362 *
363 * @throws CTFReaderException
364 * if an error occurs
866e5b51 365 */
db8e8f7d 366 public void goToLastEvent() throws CTFReaderException {
93a45b54
MK
367 seek(getEndTime());
368 while (fPrio.size() > 1) {
369 advance();
866e5b51
FC
370 }
371 }
372
373 /**
ecb12461
EB
374 * Seeks to a given timestamp. It will seek to the nearest event greater or
375 * equal to timestamp. If a trace is [10 20 30 40] and you are looking for
376 * 19, it will give you 20. If you want 20, you will get 20, if you want 21,
377 * you will get 30. The value -inf will seek to the first element and the
378 * value +inf will seek to the end of the file (past the last event).
866e5b51
FC
379 *
380 * @param timestamp
381 * the timestamp to seek to
6a5251eb
MK
382 * @return true if there are events above or equal the seek timestamp, false
383 * if seek at the end of the trace (no valid event).
db8e8f7d
AM
384 * @throws CTFReaderException
385 * if an error occurs
866e5b51 386 */
db8e8f7d 387 public boolean seek(long timestamp) throws CTFReaderException {
866e5b51
FC
388 /*
389 * Remove all the trace readers from the priority queue
390 */
93a45b54
MK
391 fPrio.clear();
392 for (StreamInputReader streamInputReader : fStreamInputReaders) {
866e5b51
FC
393 /*
394 * Seek the trace reader.
395 */
bfe038ff 396 streamInputReader.seek(timestamp);
ce2388e0
FC
397
398 /*
399 * Add it to the priority queue if there is a current event.
400 */
ce2388e0 401 if (streamInputReader.getCurrentEvent() != null) {
93a45b54 402 fPrio.add(streamInputReader);
ce2388e0
FC
403 }
404 }
866e5b51
FC
405 return hasMoreEvents();
406 }
407
9ac2eb62 408 /**
ecb12461 409 * Gets the stream with the oldest event
9ac2eb62
MK
410 *
411 * @return the stream with the oldest event
412 */
ce2388e0 413 public StreamInputReader getTopStream() {
93a45b54 414 return fPrio.peek();
ce2388e0
FC
415 }
416
866e5b51
FC
417 /**
418 * Does the trace have more events?
419 *
420 * @return true if yes.
421 */
0594c61c 422 public final boolean hasMoreEvents() {
93a45b54 423 return fPrio.size() > 0;
866e5b51
FC
424 }
425
426 /**
427 * Prints the event count stats.
428 */
429 public void printStats() {
430 printStats(60);
431 }
432
433 /**
434 * Prints the event count stats.
435 *
436 * @param width
437 * Width of the display.
438 */
439 public void printStats(int width) {
440 int numEvents = 0;
441 if (width == 0) {
442 return;
443 }
444
93a45b54 445 for (long i : fEventCountPerTraceFile) {
866e5b51
FC
446 numEvents += i;
447 }
448
93a45b54
MK
449 for (int j = 0; j < fEventCountPerTraceFile.length; j++) {
450 StreamInputReader se = fStreamInputReaders.get(j);
866e5b51 451
93a45b54 452 long len = (width * fEventCountPerTraceFile[se.getName()])
866e5b51
FC
453 / numEvents;
454
0594c61c
AM
455 StringBuilder sb = new StringBuilder(se.getFilename());
456 sb.append("\t["); //$NON-NLS-1$
866e5b51
FC
457
458 for (int i = 0; i < len; i++) {
459 sb.append('+');
460 }
461
bfe038ff 462 for (long i = len; i < width; i++) {
866e5b51
FC
463 sb.append(' ');
464 }
465
93a45b54 466 sb.append("]\t" + fEventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
4311ac8b 467 Activator.log(sb.toString());
866e5b51
FC
468 }
469 }
470
9ac2eb62 471 /**
ecb12461 472 * Gets the last event timestamp that was read. This is NOT necessarily the
9ac2eb62
MK
473 * last event in a trace, just the last one read so far.
474 *
475 * @return the last event
476 */
866e5b51 477 public long getEndTime() {
93a45b54 478 return fEndTime;
866e5b51
FC
479 }
480
6a5251eb
MK
481 /**
482 * Sets a trace to be live or not
483 *
484 * @param live
485 * whether the trace is live
486 * @since 3.0
487 */
488 public void setLive(boolean live) {
489 for (StreamInputReader s : fPrio) {
490 s.setLive(live);
491 }
492 }
493
494 /**
495 * Get if the trace is to read live or not
496 *
497 * @return whether the trace is live or not
498 * @since 3.0
499 *
500 */
501 public boolean isLive() {
502 return fPrio.peek().isLive();
503 }
504
866e5b51
FC
505 @Override
506 public int hashCode() {
507 final int prime = 31;
508 int result = 1;
93a45b54
MK
509 result = (prime * result) + (int) (fStartTime ^ (fStartTime >>> 32));
510 result = (prime * result) + fStreamInputReaders.hashCode();
511 result = (prime * result) + ((fTrace == null) ? 0 : fTrace.hashCode());
866e5b51
FC
512 return result;
513 }
514
515 @Override
516 public boolean equals(Object obj) {
517 if (this == obj) {
518 return true;
519 }
520 if (obj == null) {
521 return false;
522 }
07002e0a 523 if (!(obj instanceof CTFTraceReader)) {
866e5b51
FC
524 return false;
525 }
526 CTFTraceReader other = (CTFTraceReader) obj;
93a45b54 527 if (!fStreamInputReaders.equals(other.fStreamInputReaders)) {
866e5b51
FC
528 return false;
529 }
93a45b54
MK
530 if (fTrace == null) {
531 if (other.fTrace != null) {
866e5b51
FC
532 return false;
533 }
93a45b54 534 } else if (!fTrace.equals(other.fTrace)) {
866e5b51
FC
535 return false;
536 }
537 return true;
538 }
539
866e5b51
FC
540 @Override
541 public String toString() {
542 /* Only for debugging, shouldn't be externalized */
93a45b54 543 return "CTFTraceReader [trace=" + fTrace + ']'; //$NON-NLS-1$
866e5b51
FC
544 }
545
9ac2eb62
MK
546 /**
547 * Gets the parent trace
548 *
549 * @return the parent trace
550 */
866e5b51 551 public CTFTrace getTrace() {
93a45b54 552 return fTrace;
866e5b51
FC
553 }
554}
This page took 0.068128 seconds and 5 git commands to generate.