Change CtfTmfTrace to use TmfTrace. Bug 380951
[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;
3f036cf6 16import java.util.HashMap;
866e5b51
FC
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.Stream;
24import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
25import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
26import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputReaderTimestampComparator;
27
866e5b51
FC
28/**
29 * Reads the events of a trace.
30 */
31
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 */
56 private int[] 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
1191a574
FC
68
69 protected void setEndTime(long endTime) {
70 this.endTime = endTime;
71 }
72
ce2388e0
FC
73 /**
74 * Current event index
75 */
79cb3749 76 private long fIndex;
ce2388e0 77
3f036cf6 78 private final HashMap<Integer, Long> startIndex;
ce2388e0 79
866e5b51
FC
80 // ------------------------------------------------------------------------
81 // Constructors
82 // ------------------------------------------------------------------------
83
84 /**
85 * Constructs a TraceReader to read a trace.
86 *
87 * @param trace
88 * The trace to read from.
89 * @throws CTFReaderException
90 */
91 public CTFTraceReader(CTFTrace trace) {
92 this.trace = trace;
93
94 /**
95 * Create the trace file readers.
96 */
97 createStreamInputReaders();
98
99 /**
100 * Populate the timestamp-based priority queue.
101 */
102 populateStreamInputReaderHeap();
103
104 /**
105 * Get the start Time of this trace
33656d8e 106 * bear in mind that the trace could be empty.
866e5b51 107 */
33656d8e
MK
108 this.startTime = 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
109 if (hasMoreEvents()) {
aa572e22 110 this.startTime = prio.peek().getCurrentEvent().getTimestamp();
1191a574 111 this.setEndTime(this.startTime);
51209785 112 this.fIndex = 0;
33656d8e 113 }
3f036cf6 114 startIndex = new HashMap<Integer, Long>();
866e5b51
FC
115 }
116
117 /**
118 * Copy constructor
866e5b51
FC
119 */
120 public CTFTraceReader copyFrom() {
121 CTFTraceReader newReader = null;
122
123 newReader = new CTFTraceReader(this.trace);
124 newReader.startTime = this.startTime;
1191a574 125 newReader.setEndTime(this.endTime);
866e5b51
FC
126 return newReader;
127 }
128
129 // ------------------------------------------------------------------------
130 // Getters/Setters/Predicates
131 // ------------------------------------------------------------------------
132
133 /**
134 * Return the start time of this trace (== timestamp of the first event)
135 *
0d9a6d76 136 * @return the trace start time
866e5b51
FC
137 */
138 public long getStartTime() {
139 return this.startTime;
140 }
141
ce2388e0
FC
142 /**
143 * @return the index
144 */
145 public long getIndex() {
79cb3749 146 return fIndex;
ce2388e0
FC
147 }
148
866e5b51
FC
149 // ------------------------------------------------------------------------
150 // Operations
151 // ------------------------------------------------------------------------
152
153 /**
154 * Creates one trace file reader per trace file contained in the trace.
155 */
156 private void createStreamInputReaders() {
157 Collection<Stream> streams = this.trace.getStreams().values();
158
159 /*
160 * For each stream.
161 */
162 for (Stream stream : streams) {
163 Set<StreamInput> streamInputs = stream.getStreamInputs();
164
165 /*
166 * For each trace file of the stream.
167 */
168 for (StreamInput streamInput : streamInputs) {
169 /*
170 * Create a reader.
171 */
172 StreamInputReader streamInputReader = new StreamInputReader(
173 streamInput);
174
175 /*
176 * Add it to the group.
177 */
178 this.streamInputReaders.add(streamInputReader);
179 }
180 }
181
182 /*
183 * Create the array to count the number of event per trace file.
184 */
185 this.eventCountPerTraceFile = new int[this.streamInputReaders.size()];
186 }
187
188 /**
189 * Initializes the priority queue used to choose the trace file with the
190 * lower next event timestamp.
191 */
192 private void populateStreamInputReaderHeap() {
193 /*
194 * Create the priority queue with a size twice as bigger as the number
195 * of reader in order to avoid constant resizing.
196 */
197 this.prio = new PriorityQueue<StreamInputReader>(
198 this.streamInputReaders.size() * 2,
199 new StreamInputReaderTimestampComparator());
200
201 int pos = 0;
202
203 for (StreamInputReader reader : this.streamInputReaders) {
204 /*
205 * Add each trace file reader in the priority queue, if we are able
206 * to read an event from it.
207 */
208 if (reader.readNextEvent()) {
209 this.prio.add(reader);
210
211 this.eventCountPerTraceFile[pos] = 0;
212 reader.setName(pos);
213
214 pos++;
215 }
216 }
217 }
218
219 /**
220 * Get the current event, which is the current event of the trace file
221 * reader with the lowest timestamp.
222 *
223 * @return An event definition, or null of the trace reader reached the end
224 * of the trace.
225 */
226 public EventDefinition getCurrentEventDef() {
ce2388e0 227 StreamInputReader top = getTopStream();
866e5b51
FC
228
229 return (top != null) ? top.getCurrentEvent() : null;
230 }
231
232 /**
233 * Go to the next event.
234 *
235 * @return True if an event was read.
236 */
237 public boolean advance() {
ce2388e0
FC
238 /*
239 * Index the
240 */
866e5b51
FC
241 /*
242 * Remove the reader from the top of the priority queue.
243 */
244 StreamInputReader top = this.prio.poll();
245
246 /*
247 * If the queue was empty.
248 */
249 if (top == null) {
250 return false;
251 }
ce2388e0
FC
252 /*
253 * index if needed
254 */
255 if (hasMoreEvents()) {
256 StreamInputPacketReader packetReader = top.getPacketReader();
257 boolean packetHasMoreEvents = packetReader.hasMoreEvents();
258 StreamInputPacketIndexEntry currentPacket = packetReader
259 .getCurrentPacket();
260 if (!packetHasMoreEvents) {
261 int n = this.streamInputReaders.indexOf(top);
33656d8e 262 if (!startIndex.containsKey(n)) {
3f036cf6
MK
263 startIndex.put(n, 0L);
264 }
265 currentPacket.setIndexBegin(startIndex.get(n));
51209785
MK
266 currentPacket.setIndexEnd(fIndex);
267 startIndex.put(n, fIndex + 1);
ce2388e0
FC
268 }
269 }
866e5b51
FC
270 /*
271 * Read the next event of this reader.
272 */
273 if (top.readNextEvent()) {
274 /*
275 * Add it back in the queue.
276 */
277 this.prio.add(top);
1191a574
FC
278 final long topEnd = top.getCurrentEvent().getTimestamp() + this.getTrace().getOffset();
279 this.setEndTime( Math.max(topEnd, this.getEndTime()));
866e5b51 280 this.eventCountPerTraceFile[top.getName()]++;
ce2388e0
FC
281 /*
282 * increment the index
283 */
79cb3749 284 fIndex++;
866e5b51 285 }
ce2388e0 286 boolean hasMoreEvents = hasMoreEvents();
866e5b51
FC
287
288 /*
289 * If there is no reader in the queue, it means the trace reader reached
290 * the end of the trace.
291 */
ce2388e0 292 return hasMoreEvents;
866e5b51
FC
293 }
294
295 /**
296 * Go to the last event in the trace.
297 *
298 * @throws CTFReaderException
299 */
300 public void goToLastEvent() throws CTFReaderException {
301
866e5b51
FC
302 for (StreamInputReader streamInputReader : this.streamInputReaders) {
303 /*
304 * Seek the trace reader.
305 */
306 streamInputReader.goToLastEvent();
307 }
308 int count = prio.size();
33656d8e 309 for (int i = 0; i < (count - 1); i++) {
2d8b1224
MK
310 /*
311 * Cull all the streams aside from the last one
312 */
313 prio.poll();
866e5b51
FC
314 }
315 }
316
317 /**
318 * Seeks to a given timestamp It will go to the event just after the
319 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
320 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
321 * if you want 21, you'll get 30. You want -inf, you'll get the first
322 * element, you want +inf, you'll get the end of the file with no events.
323 *
324 * @param timestamp
325 * the timestamp to seek to
0d9a6d76 326 * @return true if the trace has more events following the timestamp
866e5b51
FC
327 */
328 public boolean seek(long timestamp) {
329 /*
330 * Remove all the trace readers from the priority queue
331 */
332 this.prio.clear();
79cb3749 333 fIndex = 0;
ce2388e0 334 long offset = 0;
866e5b51
FC
335 for (StreamInputReader streamInputReader : this.streamInputReaders) {
336 /*
337 * Seek the trace reader.
338 */
ce2388e0
FC
339 offset += streamInputReader.seek(timestamp);
340
341 /*
342 * Add it to the priority queue if there is a current event.
343 */
344
345 }
346 for (StreamInputReader streamInputReader : this.streamInputReaders) {
347 if (streamInputReader.getCurrentEvent() != null) {
348 this.prio.add(streamInputReader);
79cb3749 349 fIndex = Math.max(fIndex, streamInputReader.getPacketReader()
ce2388e0
FC
350 .getCurrentPacket().getIndexBegin()
351 + offset);
352 }
353 }
354 return hasMoreEvents();
355 }
356
357 public boolean seekIndex(long index) {
358 this.prio.clear();
359
360 long tempIndex = Long.MIN_VALUE;
361 long tempTimestamp = Long.MIN_VALUE;
362 try {
363 for (StreamInputReader streamInputReader : this.streamInputReaders) {
364 /*
365 * Seek the trace reader.
366 */
367 final long streamIndex = streamInputReader.seekIndex(index);
33656d8e 368 if (streamInputReader.getCurrentEvent() != null) {
459ebacd 369 tempIndex = Math.max(tempIndex, streamIndex);
33656d8e
MK
370 EventDefinition currentEvent = streamInputReader
371 .getCurrentEvent();
2b7f6f09 372 /*
459ebacd 373 * Maybe we're at the beginning of a trace.
2b7f6f09 374 */
33656d8e 375 if (currentEvent == null) {
459ebacd
MK
376 streamInputReader.readNextEvent();
377 currentEvent = streamInputReader.getCurrentEvent();
378 }
33656d8e 379 if (currentEvent != null) {
459ebacd 380 tempTimestamp = Math.max(tempTimestamp,
aa572e22 381 currentEvent.getTimestamp());
459ebacd
MK
382 } else {
383 /*
384 * probably beyond the last event
385 */
386 tempIndex = goToZero();
387 }
2b7f6f09 388 }
866e5b51 389
ce2388e0
FC
390 }
391 } catch (CTFReaderException e) {
392 /*
393 * Important, if it failed, it's because it's not yet indexed, so we
394 * have to manually advance to the right value.
395 */
2b7f6f09 396 tempIndex = goToZero();
ce2388e0
FC
397 }
398 for (StreamInputReader streamInputReader : this.streamInputReaders) {
866e5b51
FC
399 /*
400 * Add it to the priority queue if there is a current event.
401 */
ce2388e0 402
866e5b51
FC
403 if (streamInputReader.getCurrentEvent() != null) {
404 this.prio.add(streamInputReader);
405 }
406 }
ce2388e0
FC
407 if (tempIndex == Long.MAX_VALUE) {
408 tempIndex = 0;
409 }
410 long pos = tempIndex;
411 if (index > tempIndex) {
412 /*
413 * advance for offset
414 */
aa572e22 415 while ((prio.peek().getCurrentEvent().getTimestamp() < tempTimestamp)
ce2388e0
FC
416 && hasMoreEvents()) {
417 this.advance();
418 }
866e5b51 419
ce2388e0
FC
420 for (pos = tempIndex; (pos < index) && hasMoreEvents(); pos++) {
421 this.advance();
422 }
423 }
79cb3749 424 this.fIndex = pos;
866e5b51
FC
425 return hasMoreEvents();
426 }
427
2b7f6f09
MK
428 /**
429 * Go to the first entry of a trace
33656d8e 430 *
2b7f6f09
MK
431 * @return 0, the first index.
432 */
433 private long goToZero() {
434 long tempIndex;
435 for (StreamInputReader streamInputReader : this.streamInputReaders) {
436 /*
437 * Seek the trace reader.
438 */
439 streamInputReader.seek(0);
440 }
441 tempIndex = 0;
442 return tempIndex;
443 }
444
ce2388e0
FC
445 public StreamInputReader getTopStream() {
446 return this.prio.peek();
447 }
448
866e5b51
FC
449 /**
450 * Does the trace have more events?
451 *
452 * @return true if yes.
453 */
454 public boolean hasMoreEvents() {
455 return this.prio.size() > 0;
456 }
457
458 /**
459 * Prints the event count stats.
460 */
461 public void printStats() {
462 printStats(60);
463 }
464
465 /**
466 * Prints the event count stats.
467 *
468 * @param width
469 * Width of the display.
470 */
471 public void printStats(int width) {
472 int numEvents = 0;
473 if (width == 0) {
474 return;
475 }
476
477 for (int i : this.eventCountPerTraceFile) {
478 numEvents += i;
479 }
480
481 for (int j = 0; j < this.eventCountPerTraceFile.length; j++) {
482 StreamInputReader se = this.streamInputReaders.get(j);
483
484 int len = (width * this.eventCountPerTraceFile[se.getName()])
485 / numEvents;
486
ce2388e0 487 StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$
866e5b51
FC
488
489 for (int i = 0; i < len; i++) {
490 sb.append('+');
491 }
492
493 for (int i = len; i < width; i++) {
494 sb.append(' ');
495 }
496
497 sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
a9d52b8f 498 Activator.getDefault().log(sb.toString());
866e5b51
FC
499 }
500 }
501
502 public long getEndTime() {
503 return this.endTime;
504 }
505
506 @Override
507 public int hashCode() {
508 final int prime = 31;
509 int result = 1;
510 result = (prime * result) + (int) (endTime ^ (endTime >>> 32));
511 result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
512 result = (prime * result)
ce2388e0
FC
513 + ((streamInputReaders == null) ? 0 : streamInputReaders
514 .hashCode());
866e5b51
FC
515 result = (prime * result) + ((trace == null) ? 0 : trace.hashCode());
516 return result;
517 }
518
519 @Override
520 public boolean equals(Object obj) {
521 if (this == obj) {
522 return true;
523 }
524 if (obj == null) {
525 return false;
526 }
07002e0a 527 if (!(obj instanceof CTFTraceReader)) {
866e5b51
FC
528 return false;
529 }
530 CTFTraceReader other = (CTFTraceReader) obj;
531 if (endTime != other.endTime) {
532 return false;
533 }
534 if (startTime != other.startTime) {
535 return false;
536 }
537 if (streamInputReaders == null) {
538 if (other.streamInputReaders != null) {
539 return false;
540 }
541 } else if (!streamInputReaders.equals(other.streamInputReaders)) {
542 return false;
543 }
544 if (trace == null) {
545 if (other.trace != null) {
546 return false;
547 }
548 } else if (!trace.equals(other.trace)) {
549 return false;
550 }
551 return true;
552 }
553
554 /*
555 * (non-Javadoc)
556 *
557 * @see java.lang.Object#toString()
558 */
559 @Override
560 public String toString() {
561 /* Only for debugging, shouldn't be externalized */
562 return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$
563 }
564
565 public CTFTrace getTrace() {
566 return trace;
567 }
568}
This page took 0.052525 seconds and 5 git commands to generate.