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