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