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