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