0590798ed089108ec29f8d214982d1b30dfbb214
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTraceReader.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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:
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.ctf.core.trace;
15
16 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.PriorityQueue;
21 import java.util.Set;
22
23 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
24 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
25 import org.eclipse.linuxtools.internal.ctf.core.Activator;
26 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
27
28 import com.google.common.collect.ImmutableSet;
29 import com.google.common.collect.ImmutableSet.Builder;
30
31 /**
32 * A CTF trace reader. Reads the events of a trace.
33 *
34 * @version 1.0
35 * @author Matthew Khouzam
36 * @author Alexandre Montplaisir
37 */
38 public class CTFTraceReader implements AutoCloseable {
39
40 private static final int MIN_PRIO_SIZE = 16;
41
42 // ------------------------------------------------------------------------
43 // Attributes
44 // ------------------------------------------------------------------------
45
46 /**
47 * The trace to read from.
48 */
49 private final CTFTrace fTrace;
50
51 /**
52 * Vector of all the trace file readers.
53 */
54 private final List<CTFStreamInputReader> fStreamInputReaders = new ArrayList<>();
55
56 /**
57 * Priority queue to order the trace file readers by timestamp.
58 */
59 private PriorityQueue<CTFStreamInputReader> fPrio;
60
61 /**
62 * Array to count the number of event per trace file.
63 */
64 private long[] fEventCountPerTraceFile;
65
66 /**
67 * Timestamp of the first event in the trace
68 */
69 private long fStartTime;
70
71 /**
72 * Timestamp of the last event read so far
73 */
74 private long fEndTime;
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.
85 * @throws CTFReaderException
86 * if an error occurs
87 */
88 public CTFTraceReader(CTFTrace trace) throws CTFReaderException {
89 fTrace = trace;
90 fStreamInputReaders.clear();
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 /**
103 * Get the start Time of this trace bear in mind that the trace could be
104 * empty.
105 */
106 fStartTime = 0;
107 if (hasMoreEvents()) {
108 fStartTime = getTopStream().getCurrentEvent().getTimestamp();
109 setEndTime(fStartTime);
110 }
111 }
112
113 /**
114 * Copy constructor
115 *
116 * @return The new CTFTraceReader
117 * @throws CTFReaderException
118 * if an error occurs
119 */
120 public CTFTraceReader copyFrom() throws CTFReaderException {
121 CTFTraceReader newReader = null;
122
123 newReader = new CTFTraceReader(fTrace);
124 newReader.fStartTime = fStartTime;
125 newReader.setEndTime(fEndTime);
126 return newReader;
127 }
128
129 /**
130 * Dispose the CTFTraceReader
131 *
132 * @since 3.0
133 */
134 @Override
135 public void close() {
136 for (CTFStreamInputReader reader : fStreamInputReaders) {
137 if (reader != null) {
138 try {
139 reader.close();
140 } catch (IOException e) {
141 Activator.logError(e.getMessage(), e);
142 }
143 }
144 }
145 fStreamInputReaders.clear();
146 }
147
148 // ------------------------------------------------------------------------
149 // Getters/Setters/Predicates
150 // ------------------------------------------------------------------------
151
152 /**
153 * Return the start time of this trace (== timestamp of the first event)
154 *
155 * @return the trace start time
156 */
157 public long getStartTime() {
158 return fStartTime;
159 }
160
161 /**
162 * Set the trace's end time
163 *
164 * @param endTime
165 * The end time to use
166 */
167 protected final void setEndTime(long endTime) {
168 fEndTime = endTime;
169 }
170
171 /**
172 * Get the priority queue of this trace reader.
173 *
174 * @return The priority queue of input readers
175 * @since 2.0
176 */
177 protected PriorityQueue<CTFStreamInputReader> getPrio() {
178 return fPrio;
179 }
180
181 // ------------------------------------------------------------------------
182 // Operations
183 // ------------------------------------------------------------------------
184
185 /**
186 * Creates one trace file reader per trace file contained in the trace.
187 *
188 * @throws CTFReaderException
189 * if an error occurs
190 */
191 private void createStreamInputReaders() throws CTFReaderException {
192 /*
193 * For each stream.
194 */
195 for (CTFStream stream : fTrace.getStreams()) {
196 Set<CTFStreamInput> streamInputs = stream.getStreamInputs();
197
198 /*
199 * For each trace file of the stream.
200 */
201 for (CTFStreamInput streamInput : streamInputs) {
202
203 /*
204 * Create a reader and add it to the group.
205 */
206 fStreamInputReaders.add(new CTFStreamInputReader(streamInput));
207 }
208 }
209
210 /*
211 * Create the array to count the number of event per trace file.
212 */
213 fEventCountPerTraceFile = new long[fStreamInputReaders.size()];
214 }
215
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 {
225 Set<CTFStreamInputReader> readers = new HashSet<>();
226 for (CTFStream stream : fTrace.getStreams()) {
227 Set<CTFStreamInput> streamInputs = stream.getStreamInputs();
228 for (CTFStreamInput streamInput : streamInputs) {
229 /*
230 * Create a reader.
231 */
232 CTFStreamInputReader streamInputReader = new CTFStreamInputReader(
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];
247 for (CTFStreamInputReader reader : readers) {
248 fPrio.add(reader);
249 }
250 for (int i = 0; i < temp.length; i++) {
251 fEventCountPerTraceFile[i] = temp[i];
252 }
253 }
254
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<>();
263 for (CTFStreamInputReader sir : fStreamInputReaders) {
264 builder.addAll(sir.getEventDeclarations());
265 }
266 return builder.build();
267 }
268
269 /**
270 * Initializes the priority queue used to choose the trace file with the
271 * lower next event timestamp.
272 *
273 * @throws CTFReaderException
274 * if an error occurs
275 */
276 private void populateStreamInputReaderHeap() throws CTFReaderException {
277 if (fStreamInputReaders.isEmpty()) {
278 fPrio = new PriorityQueue<>(MIN_PRIO_SIZE,
279 new StreamInputReaderTimestampComparator());
280 return;
281 }
282
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 */
287 fPrio = new PriorityQueue<>(
288 Math.max(fStreamInputReaders.size() * 2, MIN_PRIO_SIZE),
289 new StreamInputReaderTimestampComparator());
290
291 int pos = 0;
292
293 for (CTFStreamInputReader reader : fStreamInputReaders) {
294 /*
295 * Add each trace file reader in the priority queue, if we are able
296 * to read an event from it.
297 */
298 reader.setParent(this);
299 CTFResponse readNextEvent = reader.readNextEvent();
300 if (readNextEvent == CTFResponse.OK || readNextEvent == CTFResponse.WAIT) {
301 fPrio.add(reader);
302
303 fEventCountPerTraceFile[pos] = 0;
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() {
319 CTFStreamInputReader top = getTopStream();
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.
327 * @throws CTFReaderException
328 * if an error occurs
329 */
330 public boolean advance() throws CTFReaderException {
331 /*
332 * Remove the reader from the top of the priority queue.
333 */
334 CTFStreamInputReader top = fPrio.poll();
335
336 /*
337 * If the queue was empty.
338 */
339 if (top == null) {
340 return false;
341 }
342 /*
343 * Read the next event of this reader.
344 */
345 switch (top.readNextEvent()) {
346 case OK: {
347 /*
348 * Add it back in the queue.
349 */
350 fPrio.add(top);
351 final long topEnd = fTrace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
352 setEndTime(Math.max(topEnd, getEndTime()));
353 fEventCountPerTraceFile[top.getName()]++;
354
355 if (top.getCurrentEvent() != null) {
356 fEndTime = Math.max(top.getCurrentEvent().getTimestamp(),
357 fEndTime);
358 }
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
370 }
371 /*
372 * If there is no reader in the queue, it means the trace reader reached
373 * the end of the trace.
374 */
375 return hasMoreEvents();
376 }
377
378 /**
379 * Go to the last event in the trace.
380 *
381 * @throws CTFReaderException
382 * if an error occurs
383 */
384 public void goToLastEvent() throws CTFReaderException {
385 seek(getEndTime());
386 while (fPrio.size() > 1) {
387 advance();
388 }
389 }
390
391 /**
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).
397 *
398 * @param timestamp
399 * the timestamp to seek to
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).
402 * @throws CTFReaderException
403 * if an error occurs
404 */
405 public boolean seek(long timestamp) throws CTFReaderException {
406 /*
407 * Remove all the trace readers from the priority queue
408 */
409 fPrio.clear();
410 for (CTFStreamInputReader streamInputReader : fStreamInputReaders) {
411 /*
412 * Seek the trace reader.
413 */
414 streamInputReader.seek(timestamp);
415
416 /*
417 * Add it to the priority queue if there is a current event.
418 */
419 if (streamInputReader.getCurrentEvent() != null) {
420 fPrio.add(streamInputReader);
421 }
422 }
423 return hasMoreEvents();
424 }
425
426 /**
427 * Gets the stream with the oldest event
428 *
429 * @return the stream with the oldest event
430 * @since 3.0
431 */
432 public CTFStreamInputReader getTopStream() {
433 return fPrio.peek();
434 }
435
436 /**
437 * Does the trace have more events?
438 *
439 * @return true if yes.
440 */
441 public final boolean hasMoreEvents() {
442 return fPrio.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 (long i : fEventCountPerTraceFile) {
465 numEvents += i;
466 }
467
468 for (int j = 0; j < fEventCountPerTraceFile.length; j++) {
469 CTFStreamInputReader se = fStreamInputReaders.get(j);
470
471 long len = (width * fEventCountPerTraceFile[se.getName()])
472 / numEvents;
473
474 StringBuilder sb = new StringBuilder(se.getFilename());
475 sb.append("\t["); //$NON-NLS-1$
476
477 for (int i = 0; i < len; i++) {
478 sb.append('+');
479 }
480
481 for (long i = len; i < width; i++) {
482 sb.append(' ');
483 }
484
485 sb.append("]\t" + fEventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
486 Activator.log(sb.toString());
487 }
488 }
489
490 /**
491 * Gets the last event timestamp that was read. This is NOT necessarily the
492 * last event in a trace, just the last one read so far.
493 *
494 * @return the last event
495 */
496 public long getEndTime() {
497 return fEndTime;
498 }
499
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) {
508 for (CTFStreamInputReader s : fPrio) {
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() {
521 return getTopStream().isLive();
522 }
523
524 @Override
525 public int hashCode() {
526 final int prime = 31;
527 int result = 1;
528 result = (prime * result) + (int) (fStartTime ^ (fStartTime >>> 32));
529 result = (prime * result) + fStreamInputReaders.hashCode();
530 result = (prime * result) + ((fTrace == null) ? 0 : fTrace.hashCode());
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 }
542 if (!(obj instanceof CTFTraceReader)) {
543 return false;
544 }
545 CTFTraceReader other = (CTFTraceReader) obj;
546 if (!fStreamInputReaders.equals(other.fStreamInputReaders)) {
547 return false;
548 }
549 if (fTrace == null) {
550 if (other.fTrace != null) {
551 return false;
552 }
553 } else if (!fTrace.equals(other.fTrace)) {
554 return false;
555 }
556 return true;
557 }
558
559 @Override
560 public String toString() {
561 /* Only for debugging, shouldn't be externalized */
562 return "CTFTraceReader [trace=" + fTrace + ']'; //$NON-NLS-1$
563 }
564
565 /**
566 * Gets the parent trace
567 *
568 * @return the parent trace
569 */
570 public CTFTrace getTrace() {
571 return fTrace;
572 }
573 }
This page took 0.055408 seconds and 4 git commands to generate.