tmf/lttng: Update 2014 copyrights
[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.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.PriorityQueue;
20 import java.util.Set;
21
22 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
23 import org.eclipse.linuxtools.internal.ctf.core.Activator;
24 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
25
26 /**
27 * A CTF trace reader. Reads the events of a trace.
28 *
29 * @version 1.0
30 * @author Matthew Khouzam
31 * @author Alexandre Montplaisir
32 */
33 public class CTFTraceReader {
34
35 private static final int MIN_PRIO_SIZE = 16;
36
37 // ------------------------------------------------------------------------
38 // Attributes
39 // ------------------------------------------------------------------------
40
41 /**
42 * The trace to read from.
43 */
44 private final CTFTrace fTrace;
45
46 /**
47 * Vector of all the trace file readers.
48 */
49 private final List<StreamInputReader> fStreamInputReaders = new ArrayList<>();
50
51 /**
52 * Priority queue to order the trace file readers by timestamp.
53 */
54 private PriorityQueue<StreamInputReader> fPrio;
55
56 /**
57 * Array to count the number of event per trace file.
58 */
59 private long[] fEventCountPerTraceFile;
60
61 /**
62 * Timestamp of the first event in the trace
63 */
64 private long fStartTime;
65
66 /**
67 * Timestamp of the last event read so far
68 */
69 private long fEndTime;
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.
80 * @throws CTFReaderException
81 * if an error occurs
82 */
83 public CTFTraceReader(CTFTrace trace) throws CTFReaderException {
84 fTrace = trace;
85 fStreamInputReaders.clear();
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 /**
98 * Get the start Time of this trace bear in mind that the trace could be
99 * empty.
100 */
101 fStartTime = 0;
102 if (hasMoreEvents()) {
103 fStartTime = fPrio.peek().getCurrentEvent().getTimestamp();
104 setEndTime(fStartTime);
105 }
106 }
107
108 /**
109 * Copy constructor
110 *
111 * @return The new CTFTraceReader
112 * @throws CTFReaderException
113 * if an error occurs
114 */
115 public CTFTraceReader copyFrom() throws CTFReaderException {
116 CTFTraceReader newReader = null;
117
118 newReader = new CTFTraceReader(fTrace);
119 newReader.fStartTime = fStartTime;
120 newReader.setEndTime(fEndTime);
121 return newReader;
122 }
123
124 /**
125 * Dispose the CTFTraceReader
126 *
127 * @since 2.0
128 */
129 public void dispose() {
130 for (StreamInputReader reader : fStreamInputReaders) {
131 if (reader != null) {
132 reader.dispose();
133 }
134 }
135 fStreamInputReaders.clear();
136 }
137
138 // ------------------------------------------------------------------------
139 // Getters/Setters/Predicates
140 // ------------------------------------------------------------------------
141
142 /**
143 * Return the start time of this trace (== timestamp of the first event)
144 *
145 * @return the trace start time
146 */
147 public long getStartTime() {
148 return fStartTime;
149 }
150
151 /**
152 * Set the trace's end time
153 *
154 * @param endTime
155 * The end time to use
156 */
157 protected final void setEndTime(long endTime) {
158 fEndTime = endTime;
159 }
160
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() {
168 return fPrio;
169 }
170
171 // ------------------------------------------------------------------------
172 // Operations
173 // ------------------------------------------------------------------------
174
175 /**
176 * Creates one trace file reader per trace file contained in the trace.
177 *
178 * @throws CTFReaderException
179 * if an error occurs
180 */
181 private void createStreamInputReaders() throws CTFReaderException {
182 /*
183 * For each stream.
184 */
185 for (Stream stream : fTrace.getStreams()) {
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 */
201 fStreamInputReaders.add(streamInputReader);
202 }
203 }
204
205 /*
206 * Create the array to count the number of event per trace file.
207 */
208 fEventCountPerTraceFile = new long[fStreamInputReaders.size()];
209 }
210
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
250 /**
251 * Initializes the priority queue used to choose the trace file with the
252 * lower next event timestamp.
253 *
254 * @throws CTFReaderException
255 * if an error occurs
256 */
257 private void populateStreamInputReaderHeap() throws CTFReaderException {
258 if (fStreamInputReaders.isEmpty()) {
259 fPrio = new PriorityQueue<>(MIN_PRIO_SIZE,
260 new StreamInputReaderTimestampComparator());
261 return;
262 }
263
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 */
268 fPrio = new PriorityQueue<>(
269 Math.max(fStreamInputReaders.size() * 2, MIN_PRIO_SIZE),
270 new StreamInputReaderTimestampComparator());
271
272 int pos = 0;
273
274 for (StreamInputReader reader : fStreamInputReaders) {
275 /*
276 * Add each trace file reader in the priority queue, if we are able
277 * to read an event from it.
278 */
279 reader.setParent(this);
280 CTFResponse readNextEvent = reader.readNextEvent();
281 if (readNextEvent == CTFResponse.OK || readNextEvent == CTFResponse.WAIT) {
282 fPrio.add(reader);
283
284 fEventCountPerTraceFile[pos] = 0;
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() {
300 StreamInputReader top = getTopStream();
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.
309 * @throws CTFReaderException
310 * if an error occurs
311 */
312 public boolean advance() throws CTFReaderException {
313 /*
314 * Remove the reader from the top of the priority queue.
315 */
316 StreamInputReader top = fPrio.poll();
317
318 /*
319 * If the queue was empty.
320 */
321 if (top == null) {
322 return false;
323 }
324 /*
325 * Read the next event of this reader.
326 */
327 switch (top.readNextEvent()) {
328 case OK: {
329 /*
330 * Add it back in the queue.
331 */
332 fPrio.add(top);
333 final long topEnd = fTrace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
334 setEndTime(Math.max(topEnd, getEndTime()));
335 fEventCountPerTraceFile[top.getName()]++;
336
337 if (top.getCurrentEvent() != null) {
338 fEndTime = Math.max(top.getCurrentEvent().getTimestamp(),
339 fEndTime);
340 }
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
352 }
353 /*
354 * If there is no reader in the queue, it means the trace reader reached
355 * the end of the trace.
356 */
357 return hasMoreEvents();
358 }
359
360 /**
361 * Go to the last event in the trace.
362 *
363 * @throws CTFReaderException
364 * if an error occurs
365 */
366 public void goToLastEvent() throws CTFReaderException {
367 seek(getEndTime());
368 while (fPrio.size() > 1) {
369 advance();
370 }
371 }
372
373 /**
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).
379 *
380 * @param timestamp
381 * the timestamp to seek to
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).
384 * @throws CTFReaderException
385 * if an error occurs
386 */
387 public boolean seek(long timestamp) throws CTFReaderException {
388 /*
389 * Remove all the trace readers from the priority queue
390 */
391 fPrio.clear();
392 for (StreamInputReader streamInputReader : fStreamInputReaders) {
393 /*
394 * Seek the trace reader.
395 */
396 streamInputReader.seek(timestamp);
397
398 /*
399 * Add it to the priority queue if there is a current event.
400 */
401 if (streamInputReader.getCurrentEvent() != null) {
402 fPrio.add(streamInputReader);
403 }
404 }
405 return hasMoreEvents();
406 }
407
408 /**
409 * Gets the stream with the oldest event
410 *
411 * @return the stream with the oldest event
412 */
413 public StreamInputReader getTopStream() {
414 return fPrio.peek();
415 }
416
417 /**
418 * Does the trace have more events?
419 *
420 * @return true if yes.
421 */
422 public final boolean hasMoreEvents() {
423 return fPrio.size() > 0;
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
445 for (long i : fEventCountPerTraceFile) {
446 numEvents += i;
447 }
448
449 for (int j = 0; j < fEventCountPerTraceFile.length; j++) {
450 StreamInputReader se = fStreamInputReaders.get(j);
451
452 long len = (width * fEventCountPerTraceFile[se.getName()])
453 / numEvents;
454
455 StringBuilder sb = new StringBuilder(se.getFilename());
456 sb.append("\t["); //$NON-NLS-1$
457
458 for (int i = 0; i < len; i++) {
459 sb.append('+');
460 }
461
462 for (long i = len; i < width; i++) {
463 sb.append(' ');
464 }
465
466 sb.append("]\t" + fEventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
467 Activator.log(sb.toString());
468 }
469 }
470
471 /**
472 * Gets the last event timestamp that was read. This is NOT necessarily the
473 * last event in a trace, just the last one read so far.
474 *
475 * @return the last event
476 */
477 public long getEndTime() {
478 return fEndTime;
479 }
480
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
505 @Override
506 public int hashCode() {
507 final int prime = 31;
508 int result = 1;
509 result = (prime * result) + (int) (fStartTime ^ (fStartTime >>> 32));
510 result = (prime * result) + fStreamInputReaders.hashCode();
511 result = (prime * result) + ((fTrace == null) ? 0 : fTrace.hashCode());
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 }
523 if (!(obj instanceof CTFTraceReader)) {
524 return false;
525 }
526 CTFTraceReader other = (CTFTraceReader) obj;
527 if (!fStreamInputReaders.equals(other.fStreamInputReaders)) {
528 return false;
529 }
530 if (fTrace == null) {
531 if (other.fTrace != null) {
532 return false;
533 }
534 } else if (!fTrace.equals(other.fTrace)) {
535 return false;
536 }
537 return true;
538 }
539
540 @Override
541 public String toString() {
542 /* Only for debugging, shouldn't be externalized */
543 return "CTFTraceReader [trace=" + fTrace + ']'; //$NON-NLS-1$
544 }
545
546 /**
547 * Gets the parent trace
548 *
549 * @return the parent trace
550 */
551 public CTFTrace getTrace() {
552 return fTrace;
553 }
554 }
This page took 0.044132 seconds and 5 git commands to generate.