ctf: Remove a backward dependency into StreamInput to CTF-Trace.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTraceReader.java
CommitLineData
866e5b51 1/*******************************************************************************
4bd7f2db 2 * Copyright (c) 2011, 2013 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;
866e5b51 17import java.util.Collection;
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
35 // ------------------------------------------------------------------------
36 // Attributes
37 // ------------------------------------------------------------------------
38
39 /**
40 * The trace to read from.
41 */
42 private final CTFTrace trace;
43
44 /**
45 * Vector of all the trace file readers.
46 */
0594c61c 47 private final List<StreamInputReader> streamInputReaders = new ArrayList<StreamInputReader>();
866e5b51
FC
48
49 /**
50 * Priority queue to order the trace file readers by timestamp.
51 */
0594c61c 52 private PriorityQueue<StreamInputReader> prio;
866e5b51
FC
53
54 /**
55 * Array to count the number of event per trace file.
56 */
bfe038ff 57 private long[] eventCountPerTraceFile;
866e5b51
FC
58
59 /**
60 * Timestamp of the first event in the trace
61 */
62 private long startTime;
63
64 /**
65 * Timestamp of the last event read so far
66 */
67 private long endTime;
68
69 // ------------------------------------------------------------------------
70 // Constructors
71 // ------------------------------------------------------------------------
72
73 /**
74 * Constructs a TraceReader to read a trace.
75 *
76 * @param trace
77 * The trace to read from.
db8e8f7d
AM
78 * @throws CTFReaderException
79 * if an error occurs
866e5b51 80 */
db8e8f7d 81 public CTFTraceReader(CTFTrace trace) throws CTFReaderException {
866e5b51 82 this.trace = trace;
bfe038ff 83 streamInputReaders.clear();
866e5b51
FC
84
85 /**
86 * Create the trace file readers.
87 */
88 createStreamInputReaders();
89
90 /**
91 * Populate the timestamp-based priority queue.
92 */
93 populateStreamInputReaderHeap();
94
95 /**
bfe038ff
MK
96 * Get the start Time of this trace bear in mind that the trace could be
97 * empty.
866e5b51 98 */
0594c61c 99 this.startTime = 0;
33656d8e 100 if (hasMoreEvents()) {
aa572e22 101 this.startTime = prio.peek().getCurrentEvent().getTimestamp();
1191a574 102 this.setEndTime(this.startTime);
33656d8e 103 }
866e5b51
FC
104 }
105
106 /**
107 * Copy constructor
be6df2d8
AM
108 *
109 * @return The new CTFTraceReader
db8e8f7d 110 * @throws CTFReaderException if an error occurs
866e5b51 111 */
db8e8f7d 112 public CTFTraceReader copyFrom() throws CTFReaderException {
866e5b51
FC
113 CTFTraceReader newReader = null;
114
115 newReader = new CTFTraceReader(this.trace);
116 newReader.startTime = this.startTime;
1191a574 117 newReader.setEndTime(this.endTime);
866e5b51
FC
118 return newReader;
119 }
120
5d1c6919
PT
121 /**
122 * Dispose the CTFTraceReader
db8e8f7d 123 *
5d1c6919
PT
124 * @since 2.0
125 */
126 public void dispose() {
127 for (StreamInputReader reader : streamInputReaders) {
128 if (reader != null) {
129 reader.dispose();
130 }
131 }
132 streamInputReaders.clear();
133 }
134
866e5b51
FC
135 // ------------------------------------------------------------------------
136 // Getters/Setters/Predicates
137 // ------------------------------------------------------------------------
138
139 /**
140 * Return the start time of this trace (== timestamp of the first event)
141 *
0d9a6d76 142 * @return the trace start time
866e5b51
FC
143 */
144 public long getStartTime() {
145 return this.startTime;
146 }
147
6f4e8ec0
AM
148 /**
149 * Set the trace's end time
150 *
151 * @param endTime
152 * The end time to use
153 */
0594c61c 154 protected final void setEndTime(long endTime) {
6f4e8ec0
AM
155 this.endTime = endTime;
156 }
157
0594c61c
AM
158 /**
159 * Get the priority queue of this trace reader.
160 *
161 * @return The priority queue of input readers
162 * @since 2.0
163 */
164 protected PriorityQueue<StreamInputReader> getPrio() {
165 return prio;
166 }
167
866e5b51
FC
168 // ------------------------------------------------------------------------
169 // Operations
170 // ------------------------------------------------------------------------
171
172 /**
173 * Creates one trace file reader per trace file contained in the trace.
db8e8f7d
AM
174 *
175 * @throws CTFReaderException
176 * if an error occurs
866e5b51 177 */
db8e8f7d 178 private void createStreamInputReaders() throws CTFReaderException {
866e5b51
FC
179 Collection<Stream> streams = this.trace.getStreams().values();
180
181 /*
182 * For each stream.
183 */
184 for (Stream stream : streams) {
185 Set<StreamInput> streamInputs = stream.getStreamInputs();
186
187 /*
188 * For each trace file of the stream.
189 */
190 for (StreamInput streamInput : streamInputs) {
191 /*
192 * Create a reader.
193 */
194 StreamInputReader streamInputReader = new StreamInputReader(
195 streamInput);
196
197 /*
198 * Add it to the group.
199 */
200 this.streamInputReaders.add(streamInputReader);
201 }
202 }
203
204 /*
205 * Create the array to count the number of event per trace file.
206 */
bfe038ff 207 this.eventCountPerTraceFile = new long[this.streamInputReaders.size()];
866e5b51
FC
208 }
209
210 /**
211 * Initializes the priority queue used to choose the trace file with the
212 * lower next event timestamp.
db8e8f7d
AM
213 *
214 * @throws CTFReaderException
215 * if an error occurs
866e5b51 216 */
db8e8f7d 217 private void populateStreamInputReaderHeap() throws CTFReaderException {
b5354daa
MAL
218 if (this.streamInputReaders.isEmpty()) {
219 this.prio = new PriorityQueue<StreamInputReader>();
220 return;
221 }
222
866e5b51
FC
223 /*
224 * Create the priority queue with a size twice as bigger as the number
225 * of reader in order to avoid constant resizing.
226 */
227 this.prio = new PriorityQueue<StreamInputReader>(
228 this.streamInputReaders.size() * 2,
229 new StreamInputReaderTimestampComparator());
230
231 int pos = 0;
232
233 for (StreamInputReader reader : this.streamInputReaders) {
234 /*
235 * Add each trace file reader in the priority queue, if we are able
236 * to read an event from it.
237 */
bfe038ff 238 reader.setParent(this);
866e5b51
FC
239 if (reader.readNextEvent()) {
240 this.prio.add(reader);
241
242 this.eventCountPerTraceFile[pos] = 0;
243 reader.setName(pos);
244
245 pos++;
246 }
247 }
248 }
249
250 /**
251 * Get the current event, which is the current event of the trace file
252 * reader with the lowest timestamp.
253 *
254 * @return An event definition, or null of the trace reader reached the end
255 * of the trace.
256 */
257 public EventDefinition getCurrentEventDef() {
ce2388e0 258 StreamInputReader top = getTopStream();
866e5b51
FC
259
260 return (top != null) ? top.getCurrentEvent() : null;
261 }
262
263 /**
264 * Go to the next event.
265 *
266 * @return True if an event was read.
db8e8f7d
AM
267 * @throws CTFReaderException
268 * if an error occurs
866e5b51 269 */
db8e8f7d 270 public boolean advance() throws CTFReaderException {
866e5b51
FC
271 /*
272 * Remove the reader from the top of the priority queue.
273 */
274 StreamInputReader top = this.prio.poll();
275
276 /*
277 * If the queue was empty.
278 */
279 if (top == null) {
280 return false;
281 }
866e5b51
FC
282 /*
283 * Read the next event of this reader.
284 */
285 if (top.readNextEvent()) {
286 /*
287 * Add it back in the queue.
288 */
289 this.prio.add(top);
1d7277f3 290 final long topEnd = this.trace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
9ac2eb62 291 this.setEndTime(Math.max(topEnd, this.getEndTime()));
866e5b51 292 this.eventCountPerTraceFile[top.getName()]++;
866e5b51 293
bfe038ff
MK
294 if (top.getCurrentEvent() != null) {
295 this.endTime = Math.max(top.getCurrentEvent().getTimestamp(),
296 this.endTime);
297 }
298 }
866e5b51
FC
299 /*
300 * If there is no reader in the queue, it means the trace reader reached
301 * the end of the trace.
302 */
bfe038ff 303 return hasMoreEvents();
866e5b51
FC
304 }
305
306 /**
307 * Go to the last event in the trace.
db8e8f7d
AM
308 *
309 * @throws CTFReaderException
310 * if an error occurs
866e5b51 311 */
db8e8f7d 312 public void goToLastEvent() throws CTFReaderException {
bfe038ff
MK
313 seek(this.getEndTime());
314 while (this.prio.size() > 1) {
315 this.advance();
866e5b51
FC
316 }
317 }
318
319 /**
ecb12461
EB
320 * Seeks to a given timestamp. It will seek to the nearest event greater or
321 * equal to timestamp. If a trace is [10 20 30 40] and you are looking for
322 * 19, it will give you 20. If you want 20, you will get 20, if you want 21,
323 * you will get 30. The value -inf will seek to the first element and the
324 * value +inf will seek to the end of the file (past the last event).
866e5b51
FC
325 *
326 * @param timestamp
327 * the timestamp to seek to
ecb12461
EB
328 * @return true if there are events above or equal the seek timestamp,
329 * false if seek at the end of the trace (no valid event).
db8e8f7d
AM
330 * @throws CTFReaderException
331 * if an error occurs
866e5b51 332 */
db8e8f7d 333 public boolean seek(long timestamp) throws CTFReaderException {
866e5b51
FC
334 /*
335 * Remove all the trace readers from the priority queue
336 */
337 this.prio.clear();
866e5b51
FC
338 for (StreamInputReader streamInputReader : this.streamInputReaders) {
339 /*
340 * Seek the trace reader.
341 */
bfe038ff 342 streamInputReader.seek(timestamp);
ce2388e0
FC
343
344 /*
345 * Add it to the priority queue if there is a current event.
346 */
ce2388e0
FC
347 if (streamInputReader.getCurrentEvent() != null) {
348 this.prio.add(streamInputReader);
ce2388e0
FC
349 }
350 }
866e5b51
FC
351 return hasMoreEvents();
352 }
353
9ac2eb62 354 /**
ecb12461 355 * Gets the stream with the oldest event
9ac2eb62
MK
356 *
357 * @return the stream with the oldest event
358 */
ce2388e0
FC
359 public StreamInputReader getTopStream() {
360 return this.prio.peek();
361 }
362
866e5b51
FC
363 /**
364 * Does the trace have more events?
365 *
366 * @return true if yes.
367 */
0594c61c 368 public final boolean hasMoreEvents() {
866e5b51
FC
369 return this.prio.size() > 0;
370 }
371
372 /**
373 * Prints the event count stats.
374 */
375 public void printStats() {
376 printStats(60);
377 }
378
379 /**
380 * Prints the event count stats.
381 *
382 * @param width
383 * Width of the display.
384 */
385 public void printStats(int width) {
386 int numEvents = 0;
387 if (width == 0) {
388 return;
389 }
390
bfe038ff 391 for (long i : this.eventCountPerTraceFile) {
866e5b51
FC
392 numEvents += i;
393 }
394
395 for (int j = 0; j < this.eventCountPerTraceFile.length; j++) {
396 StreamInputReader se = this.streamInputReaders.get(j);
397
bfe038ff 398 long len = (width * this.eventCountPerTraceFile[se.getName()])
866e5b51
FC
399 / numEvents;
400
0594c61c
AM
401 StringBuilder sb = new StringBuilder(se.getFilename());
402 sb.append("\t["); //$NON-NLS-1$
866e5b51
FC
403
404 for (int i = 0; i < len; i++) {
405 sb.append('+');
406 }
407
bfe038ff 408 for (long i = len; i < width; i++) {
866e5b51
FC
409 sb.append(' ');
410 }
411
412 sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
4311ac8b 413 Activator.log(sb.toString());
866e5b51
FC
414 }
415 }
416
9ac2eb62 417 /**
ecb12461 418 * Gets the last event timestamp that was read. This is NOT necessarily the
9ac2eb62
MK
419 * last event in a trace, just the last one read so far.
420 *
421 * @return the last event
422 */
866e5b51
FC
423 public long getEndTime() {
424 return this.endTime;
425 }
426
427 @Override
428 public int hashCode() {
429 final int prime = 31;
430 int result = 1;
866e5b51 431 result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
77fdc5df 432 result = (prime * result) + streamInputReaders.hashCode();
866e5b51
FC
433 result = (prime * result) + ((trace == null) ? 0 : trace.hashCode());
434 return result;
435 }
436
437 @Override
438 public boolean equals(Object obj) {
439 if (this == obj) {
440 return true;
441 }
442 if (obj == null) {
443 return false;
444 }
07002e0a 445 if (!(obj instanceof CTFTraceReader)) {
866e5b51
FC
446 return false;
447 }
448 CTFTraceReader other = (CTFTraceReader) obj;
77fdc5df 449 if (!streamInputReaders.equals(other.streamInputReaders)) {
866e5b51
FC
450 return false;
451 }
452 if (trace == null) {
453 if (other.trace != null) {
454 return false;
455 }
456 } else if (!trace.equals(other.trace)) {
457 return false;
458 }
459 return true;
460 }
461
866e5b51
FC
462 @Override
463 public String toString() {
464 /* Only for debugging, shouldn't be externalized */
465 return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$
466 }
467
9ac2eb62
MK
468 /**
469 * Gets the parent trace
470 *
471 * @return the parent trace
472 */
866e5b51
FC
473 public CTFTrace getTrace() {
474 return trace;
475 }
476}
This page took 0.057503 seconds and 5 git commands to generate.