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