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