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