tmf: Give proper names to SD messages variables
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / CTFTraceReader.java
CommitLineData
866e5b51 1/*******************************************************************************
4bd7f2db 2 * Copyright (c) 2011, 2013 Ericsson, Ecole Polytechnique de Montreal and others
866e5b51
FC
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 *
4311ac8b
MAL
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Alexandre Montplaisir - Initial API and implementation
866e5b51
FC
12 *******************************************************************************/
13
14package org.eclipse.linuxtools.ctf.core.trace;
15
16import java.util.Collection;
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.StreamInputReaderTimestampComparator;
24
866e5b51 25/**
d37aaa7f 26 * A CTF trace reader. Reads the events of a trace.
1d7277f3 27 *
d37aaa7f
FC
28 * @version 1.0
29 * @author Matthew Khouzam
30 * @author Alexandre Montplaisir
866e5b51 31 */
866e5b51
FC
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 */
bfe038ff 56 private long[] eventCountPerTraceFile;
866e5b51
FC
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.
866e5b51
FC
77 */
78 public CTFTraceReader(CTFTrace trace) {
79 this.trace = trace;
bfe038ff 80 streamInputReaders.clear();
866e5b51
FC
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 /**
bfe038ff
MK
93 * Get the start Time of this trace bear in mind that the trace could be
94 * empty.
866e5b51 95 */
33656d8e
MK
96 this.startTime = 0;// prio.peek().getPacketReader().getCurrentPacket().getTimestampBegin();
97 if (hasMoreEvents()) {
aa572e22 98 this.startTime = prio.peek().getCurrentEvent().getTimestamp();
1191a574 99 this.setEndTime(this.startTime);
33656d8e 100 }
866e5b51
FC
101 }
102
103 /**
104 * Copy constructor
be6df2d8
AM
105 *
106 * @return The new CTFTraceReader
866e5b51
FC
107 */
108 public CTFTraceReader copyFrom() {
109 CTFTraceReader newReader = null;
110
111 newReader = new CTFTraceReader(this.trace);
112 newReader.startTime = this.startTime;
1191a574 113 newReader.setEndTime(this.endTime);
866e5b51
FC
114 return newReader;
115 }
116
5d1c6919
PT
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
866e5b51
FC
130 // ------------------------------------------------------------------------
131 // Getters/Setters/Predicates
132 // ------------------------------------------------------------------------
133
134 /**
135 * Return the start time of this trace (== timestamp of the first event)
136 *
0d9a6d76 137 * @return the trace start time
866e5b51
FC
138 */
139 public long getStartTime() {
140 return this.startTime;
141 }
142
6f4e8ec0
AM
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
866e5b51
FC
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 */
bfe038ff 190 this.eventCountPerTraceFile = new long[this.streamInputReaders.size()];
866e5b51
FC
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 /*
199 * Create the priority queue with a size twice as bigger as the number
200 * of reader in order to avoid constant resizing.
201 */
202 this.prio = new PriorityQueue<StreamInputReader>(
203 this.streamInputReaders.size() * 2,
204 new StreamInputReaderTimestampComparator());
205
206 int pos = 0;
207
208 for (StreamInputReader reader : this.streamInputReaders) {
209 /*
210 * Add each trace file reader in the priority queue, if we are able
211 * to read an event from it.
212 */
bfe038ff 213 reader.setParent(this);
866e5b51
FC
214 if (reader.readNextEvent()) {
215 this.prio.add(reader);
216
217 this.eventCountPerTraceFile[pos] = 0;
218 reader.setName(pos);
219
220 pos++;
221 }
222 }
223 }
224
225 /**
226 * Get the current event, which is the current event of the trace file
227 * reader with the lowest timestamp.
228 *
229 * @return An event definition, or null of the trace reader reached the end
230 * of the trace.
231 */
232 public EventDefinition getCurrentEventDef() {
ce2388e0 233 StreamInputReader top = getTopStream();
866e5b51
FC
234
235 return (top != null) ? top.getCurrentEvent() : null;
236 }
237
238 /**
239 * Go to the next event.
240 *
241 * @return True if an event was read.
242 */
243 public boolean advance() {
ce2388e0
FC
244 /*
245 * Index the
246 */
866e5b51
FC
247 /*
248 * Remove the reader from the top of the priority queue.
249 */
250 StreamInputReader top = this.prio.poll();
251
252 /*
253 * If the queue was empty.
254 */
255 if (top == null) {
256 return false;
257 }
866e5b51
FC
258 /*
259 * Read the next event of this reader.
260 */
261 if (top.readNextEvent()) {
262 /*
263 * Add it back in the queue.
264 */
265 this.prio.add(top);
1d7277f3 266 final long topEnd = this.trace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp());
9ac2eb62 267 this.setEndTime(Math.max(topEnd, this.getEndTime()));
866e5b51 268 this.eventCountPerTraceFile[top.getName()]++;
866e5b51 269
bfe038ff
MK
270 if (top.getCurrentEvent() != null) {
271 this.endTime = Math.max(top.getCurrentEvent().getTimestamp(),
272 this.endTime);
273 }
274 }
866e5b51
FC
275 /*
276 * If there is no reader in the queue, it means the trace reader reached
277 * the end of the trace.
278 */
bfe038ff 279 return hasMoreEvents();
866e5b51
FC
280 }
281
282 /**
283 * Go to the last event in the trace.
866e5b51 284 */
bfe038ff
MK
285 public void goToLastEvent() {
286 seek(this.getEndTime());
287 while (this.prio.size() > 1) {
288 this.advance();
866e5b51
FC
289 }
290 }
291
292 /**
293 * Seeks to a given timestamp It will go to the event just after the
294 * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and
295 * you're looking for 19, it'll give you 20, it you want 20, you'll get 20,
296 * if you want 21, you'll get 30. You want -inf, you'll get the first
297 * element, you want +inf, you'll get the end of the file with no events.
298 *
299 * @param timestamp
300 * the timestamp to seek to
0d9a6d76 301 * @return true if the trace has more events following the timestamp
866e5b51
FC
302 */
303 public boolean seek(long timestamp) {
304 /*
305 * Remove all the trace readers from the priority queue
306 */
307 this.prio.clear();
866e5b51
FC
308 for (StreamInputReader streamInputReader : this.streamInputReaders) {
309 /*
310 * Seek the trace reader.
311 */
bfe038ff 312 streamInputReader.seek(timestamp);
ce2388e0
FC
313
314 /*
315 * Add it to the priority queue if there is a current event.
316 */
317
318 }
319 for (StreamInputReader streamInputReader : this.streamInputReaders) {
320 if (streamInputReader.getCurrentEvent() != null) {
321 this.prio.add(streamInputReader);
866e5b51 322
ce2388e0
FC
323 }
324 }
866e5b51
FC
325 return hasMoreEvents();
326 }
327
be6df2d8
AM
328// /**
329// * Go to the first entry of a trace
330// *
331// * @return 0, the first index.
332// */
333// private long goToZero() {
334// long tempIndex;
335// for (StreamInputReader streamInputReader : this.streamInputReaders) {
336// /*
337// * Seek the trace reader.
338// */
339// streamInputReader.seek(0);
340// }
341// tempIndex = 0;
342// return tempIndex;
343// }
2b7f6f09 344
9ac2eb62
MK
345 /**
346 * gets the stream with the oldest event
347 *
348 * @return the stream with the oldest event
349 */
ce2388e0
FC
350 public StreamInputReader getTopStream() {
351 return this.prio.peek();
352 }
353
866e5b51
FC
354 /**
355 * Does the trace have more events?
356 *
357 * @return true if yes.
358 */
359 public boolean hasMoreEvents() {
360 return this.prio.size() > 0;
361 }
362
363 /**
364 * Prints the event count stats.
365 */
366 public void printStats() {
367 printStats(60);
368 }
369
370 /**
371 * Prints the event count stats.
372 *
373 * @param width
374 * Width of the display.
375 */
376 public void printStats(int width) {
377 int numEvents = 0;
378 if (width == 0) {
379 return;
380 }
381
bfe038ff 382 for (long i : this.eventCountPerTraceFile) {
866e5b51
FC
383 numEvents += i;
384 }
385
386 for (int j = 0; j < this.eventCountPerTraceFile.length; j++) {
387 StreamInputReader se = this.streamInputReaders.get(j);
388
bfe038ff 389 long len = (width * this.eventCountPerTraceFile[se.getName()])
866e5b51
FC
390 / numEvents;
391
ce2388e0 392 StringBuilder sb = new StringBuilder(se.getFilename() + "\t["); //$NON-NLS-1$
866e5b51
FC
393
394 for (int i = 0; i < len; i++) {
395 sb.append('+');
396 }
397
bfe038ff 398 for (long i = len; i < width; i++) {
866e5b51
FC
399 sb.append(' ');
400 }
401
402 sb.append("]\t" + this.eventCountPerTraceFile[se.getName()] + " Events"); //$NON-NLS-1$//$NON-NLS-2$
4311ac8b 403 Activator.log(sb.toString());
866e5b51
FC
404 }
405 }
406
9ac2eb62
MK
407 /**
408 * gets the last event timestamp that was read. This is NOT necessarily the
409 * last event in a trace, just the last one read so far.
410 *
411 * @return the last event
412 */
866e5b51
FC
413 public long getEndTime() {
414 return this.endTime;
415 }
416
417 @Override
418 public int hashCode() {
419 final int prime = 31;
420 int result = 1;
866e5b51 421 result = (prime * result) + (int) (startTime ^ (startTime >>> 32));
77fdc5df 422 result = (prime * result) + streamInputReaders.hashCode();
866e5b51
FC
423 result = (prime * result) + ((trace == null) ? 0 : trace.hashCode());
424 return result;
425 }
426
427 @Override
428 public boolean equals(Object obj) {
429 if (this == obj) {
430 return true;
431 }
432 if (obj == null) {
433 return false;
434 }
07002e0a 435 if (!(obj instanceof CTFTraceReader)) {
866e5b51
FC
436 return false;
437 }
438 CTFTraceReader other = (CTFTraceReader) obj;
77fdc5df 439 if (!streamInputReaders.equals(other.streamInputReaders)) {
866e5b51
FC
440 return false;
441 }
442 if (trace == null) {
443 if (other.trace != null) {
444 return false;
445 }
446 } else if (!trace.equals(other.trace)) {
447 return false;
448 }
449 return true;
450 }
451
866e5b51
FC
452 @Override
453 public String toString() {
454 /* Only for debugging, shouldn't be externalized */
455 return "CTFTraceReader [trace=" + trace + ']'; //$NON-NLS-1$
456 }
457
9ac2eb62
MK
458 /**
459 * Gets the parent trace
460 *
461 * @return the parent trace
462 */
866e5b51
FC
463 public CTFTrace getTrace() {
464 return trace;
465 }
466}
This page took 0.054217 seconds and 5 git commands to generate.