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
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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.Collection;
17 import java.util.PriorityQueue;
18 import java.util.Set;
19 import java.util.Vector;
20
21 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
22 import org.eclipse.linuxtools.internal.ctf.core.Activator;
23 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
24
25 /**
26 * A CTF trace reader. Reads the events of a trace.
27 *
28 * @version 1.0
29 * @author Matthew Khouzam
30 * @author Alexandre Montplaisir
31 */
32 public 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 */
56 private long[] eventCountPerTraceFile;
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.
77 */
78 public CTFTraceReader(CTFTrace trace) {
79 this.trace = trace;
80 streamInputReaders.clear();
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 /**
93 * Get the start Time of this trace bear in mind that the trace could be
94 * empty.
95 */
96 this.startTime = 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
97 if (hasMoreEvents()) {
98 this.startTime = prio.peek().getCurrentEvent().getTimestamp();
99 this.setEndTime(this.startTime);
100 }
101 }
102
103 /**
104 * Copy constructor
105 *
106 * @return The new CTFTraceReader
107 */
108 public CTFTraceReader copyFrom() {
109 CTFTraceReader newReader = null;
110
111 newReader = new CTFTraceReader(this.trace);
112 newReader.startTime = this.startTime;
113 newReader.setEndTime(this.endTime);
114 return newReader;
115 }
116
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
130 // ------------------------------------------------------------------------
131 // Getters/Setters/Predicates
132 // ------------------------------------------------------------------------
133
134 /**
135 * Return the start time of this trace (== timestamp of the first event)
136 *
137 * @return the trace start time
138 */
139 public long getStartTime() {
140 return this.startTime;
141 }
142
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
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 */
190 this.eventCountPerTraceFile = new long[this.streamInputReaders.size()];
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() {
198 if (this.streamInputReaders.isEmpty()) {
199 this.prio = new PriorityQueue<StreamInputReader>();
200 return;
201 }
202
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 */
218 reader.setParent(this);
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() {
238 StreamInputReader top = getTopStream();
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() {
249 /*
250 * Index the
251 */
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 }
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);
271 final long topEnd = this.trace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
272 this.setEndTime(Math.max(topEnd, this.getEndTime()));
273 this.eventCountPerTraceFile[top.getName()]++;
274
275 if (top.getCurrentEvent() != null) {
276 this.endTime = Math.max(top.getCurrentEvent().getTimestamp(),
277 this.endTime);
278 }
279 }
280 /*
281 * If there is no reader in the queue, it means the trace reader reached
282 * the end of the trace.
283 */
284 return hasMoreEvents();
285 }
286
287 /**
288 * Go to the last event in the trace.
289 */
290 public void goToLastEvent() {
291 seek(this.getEndTime());
292 while (this.prio.size() > 1) {
293 this.advance();
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
306 * @return true if the trace has more events following the timestamp
307 */
308 public boolean seek(long timestamp) {
309 /*
310 * Remove all the trace readers from the priority queue
311 */
312 this.prio.clear();
313 for (StreamInputReader streamInputReader : this.streamInputReaders) {
314 /*
315 * Seek the trace reader.
316 */
317 streamInputReader.seek(timestamp);
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);
327
328 }
329 }
330 return hasMoreEvents();
331 }
332
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 // }
349
350 /**
351 * gets the stream with the oldest event
352 *
353 * @return the stream with the oldest event
354 */
355 public StreamInputReader getTopStream() {
356 return this.prio.peek();
357 }
358
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
387 for (long i : this.eventCountPerTraceFile) {
388 numEvents += i;
389 }
390
391 for (int j = 0; j < this.eventCountPerTraceFile.length; j++) {
392 StreamInputReader se = this.streamInputReaders.get(j);
393
394 long len = (width * this.eventCountPerTraceFile[se.getName()])
395 / numEvents;
396
397 StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$
398
399 for (int i = 0; i < len; i++) {
400 sb.append('+');
401 }
402
403 for (long i = len; i < width; i++) {
404 sb.append(' ');
405 }
406
407 sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
408 Activator.log(sb.toString());
409 }
410 }
411
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 */
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;
426 result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
427 result = (prime * result) + streamInputReaders.hashCode();
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 }
440 if (!(obj instanceof CTFTraceReader)) {
441 return false;
442 }
443 CTFTraceReader other = (CTFTraceReader) obj;
444 if (!streamInputReaders.equals(other.streamInputReaders)) {
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
457 @Override
458 public String toString() {
459 /* Only for debugging, shouldn't be externalized */
460 return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$
461 }
462
463 /**
464 * Gets the parent trace
465 *
466 * @return the parent trace
467 */
468 public CTFTrace getTrace() {
469 return trace;
470 }
471 }
This page took 0.042205 seconds and 6 git commands to generate.