Commit | Line | Data |
---|---|---|
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 | ||
14 | package org.eclipse.linuxtools.ctf.core.trace; | |
15 | ||
0594c61c | 16 | import java.util.ArrayList; |
866e5b51 | 17 | import java.util.Collection; |
0594c61c | 18 | import java.util.List; |
866e5b51 FC |
19 | import java.util.PriorityQueue; |
20 | import java.util.Set; | |
866e5b51 | 21 | |
866e5b51 | 22 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
a9d52b8f | 23 | import org.eclipse.linuxtools.internal.ctf.core.Activator; |
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 | */ | |
0594c61c | 47 | private final List<StreamInputReader> streamInputReaders = new ArrayList<StreamInputReader>(); |
866e5b51 FC |
48 | |
49 | /** | |
50 | * Priority queue to order the trace file readers by timestamp. | |
51 | */ | |
0594c61c | 52 | private PriorityQueue<StreamInputReader> prio; |
866e5b51 FC |
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 | */ |
0594c61c | 97 | this.startTime = 0; |
33656d8e | 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 | ||
5d1c6919 PT |
118 | /** |
119 | * Dispose the CTFTraceReader | |
120 | * @since 2.0 | |
121 | */ | |
122 | public void dispose() { | |
123 | for (StreamInputReader reader : streamInputReaders) { | |
124 | if (reader != null) { | |
125 | reader.dispose(); | |
126 | } | |
127 | } | |
128 | streamInputReaders.clear(); | |
129 | } | |
130 | ||
866e5b51 FC |
131 | // ------------------------------------------------------------------------ |
132 | // Getters/Setters/Predicates | |
133 | // ------------------------------------------------------------------------ | |
134 | ||
135 | /** | |
136 | * Return the start time of this trace (== timestamp of the first event) | |
137 | * | |
0d9a6d76 | 138 | * @return the trace start time |
866e5b51 FC |
139 | */ |
140 | public long getStartTime() { | |
141 | return this.startTime; | |
142 | } | |
143 | ||
6f4e8ec0 AM |
144 | /** |
145 | * Set the trace's end time | |
146 | * | |
147 | * @param endTime | |
148 | * The end time to use | |
149 | */ | |
0594c61c | 150 | protected final void setEndTime(long endTime) { |
6f4e8ec0 AM |
151 | this.endTime = endTime; |
152 | } | |
153 | ||
0594c61c AM |
154 | /** |
155 | * Get the priority queue of this trace reader. | |
156 | * | |
157 | * @return The priority queue of input readers | |
158 | * @since 2.0 | |
159 | */ | |
160 | protected PriorityQueue<StreamInputReader> getPrio() { | |
161 | return prio; | |
162 | } | |
163 | ||
6f4e8ec0 | 164 | |
866e5b51 FC |
165 | // ------------------------------------------------------------------------ |
166 | // Operations | |
167 | // ------------------------------------------------------------------------ | |
168 | ||
169 | /** | |
170 | * Creates one trace file reader per trace file contained in the trace. | |
171 | */ | |
172 | private void createStreamInputReaders() { | |
173 | Collection<Stream> streams = this.trace.getStreams().values(); | |
174 | ||
175 | /* | |
176 | * For each stream. | |
177 | */ | |
178 | for (Stream stream : streams) { | |
179 | Set<StreamInput> streamInputs = stream.getStreamInputs(); | |
180 | ||
181 | /* | |
182 | * For each trace file of the stream. | |
183 | */ | |
184 | for (StreamInput streamInput : streamInputs) { | |
185 | /* | |
186 | * Create a reader. | |
187 | */ | |
188 | StreamInputReader streamInputReader = new StreamInputReader( | |
189 | streamInput); | |
190 | ||
191 | /* | |
192 | * Add it to the group. | |
193 | */ | |
194 | this.streamInputReaders.add(streamInputReader); | |
195 | } | |
196 | } | |
197 | ||
198 | /* | |
199 | * Create the array to count the number of event per trace file. | |
200 | */ | |
bfe038ff | 201 | this.eventCountPerTraceFile = new long[this.streamInputReaders.size()]; |
866e5b51 FC |
202 | } |
203 | ||
204 | /** | |
205 | * Initializes the priority queue used to choose the trace file with the | |
206 | * lower next event timestamp. | |
207 | */ | |
208 | private void populateStreamInputReaderHeap() { | |
b5354daa MAL |
209 | if (this.streamInputReaders.isEmpty()) { |
210 | this.prio = new PriorityQueue<StreamInputReader>(); | |
211 | return; | |
212 | } | |
213 | ||
866e5b51 FC |
214 | /* |
215 | * Create the priority queue with a size twice as bigger as the number | |
216 | * of reader in order to avoid constant resizing. | |
217 | */ | |
218 | this.prio = new PriorityQueue<StreamInputReader>( | |
219 | this.streamInputReaders.size() * 2, | |
220 | new StreamInputReaderTimestampComparator()); | |
221 | ||
222 | int pos = 0; | |
223 | ||
224 | for (StreamInputReader reader : this.streamInputReaders) { | |
225 | /* | |
226 | * Add each trace file reader in the priority queue, if we are able | |
227 | * to read an event from it. | |
228 | */ | |
bfe038ff | 229 | reader.setParent(this); |
866e5b51 FC |
230 | if (reader.readNextEvent()) { |
231 | this.prio.add(reader); | |
232 | ||
233 | this.eventCountPerTraceFile[pos] = 0; | |
234 | reader.setName(pos); | |
235 | ||
236 | pos++; | |
237 | } | |
238 | } | |
239 | } | |
240 | ||
241 | /** | |
242 | * Get the current event, which is the current event of the trace file | |
243 | * reader with the lowest timestamp. | |
244 | * | |
245 | * @return An event definition, or null of the trace reader reached the end | |
246 | * of the trace. | |
247 | */ | |
248 | public EventDefinition getCurrentEventDef() { | |
ce2388e0 | 249 | StreamInputReader top = getTopStream(); |
866e5b51 FC |
250 | |
251 | return (top != null) ? top.getCurrentEvent() : null; | |
252 | } | |
253 | ||
254 | /** | |
255 | * Go to the next event. | |
256 | * | |
257 | * @return True if an event was read. | |
258 | */ | |
259 | public boolean advance() { | |
ce2388e0 FC |
260 | /* |
261 | * Index the | |
262 | */ | |
866e5b51 FC |
263 | /* |
264 | * Remove the reader from the top of the priority queue. | |
265 | */ | |
266 | StreamInputReader top = this.prio.poll(); | |
267 | ||
268 | /* | |
269 | * If the queue was empty. | |
270 | */ | |
271 | if (top == null) { | |
272 | return false; | |
273 | } | |
866e5b51 FC |
274 | /* |
275 | * Read the next event of this reader. | |
276 | */ | |
277 | if (top.readNextEvent()) { | |
278 | /* | |
279 | * Add it back in the queue. | |
280 | */ | |
281 | this.prio.add(top); | |
1d7277f3 | 282 | final long topEnd = this.trace.timestampCyclesToNanos(top.getCurrentEvent().getTimestamp()); |
9ac2eb62 | 283 | this.setEndTime(Math.max(topEnd, this.getEndTime())); |
866e5b51 | 284 | this.eventCountPerTraceFile[top.getName()]++; |
866e5b51 | 285 | |
bfe038ff MK |
286 | if (top.getCurrentEvent() != null) { |
287 | this.endTime = Math.max(top.getCurrentEvent().getTimestamp(), | |
288 | this.endTime); | |
289 | } | |
290 | } | |
866e5b51 FC |
291 | /* |
292 | * If there is no reader in the queue, it means the trace reader reached | |
293 | * the end of the trace. | |
294 | */ | |
bfe038ff | 295 | return hasMoreEvents(); |
866e5b51 FC |
296 | } |
297 | ||
298 | /** | |
299 | * Go to the last event in the trace. | |
866e5b51 | 300 | */ |
bfe038ff MK |
301 | public void goToLastEvent() { |
302 | seek(this.getEndTime()); | |
303 | while (this.prio.size() > 1) { | |
304 | this.advance(); | |
866e5b51 FC |
305 | } |
306 | } | |
307 | ||
308 | /** | |
309 | * Seeks to a given timestamp It will go to the event just after the | |
310 | * timestamp or the timestamp itself. if a if a trace is 10 20 30 40 and | |
311 | * you're looking for 19, it'll give you 20, it you want 20, you'll get 20, | |
312 | * if you want 21, you'll get 30. You want -inf, you'll get the first | |
313 | * element, you want +inf, you'll get the end of the file with no events. | |
314 | * | |
315 | * @param timestamp | |
316 | * the timestamp to seek to | |
0d9a6d76 | 317 | * @return true if the trace has more events following the timestamp |
866e5b51 FC |
318 | */ |
319 | public boolean seek(long timestamp) { | |
320 | /* | |
321 | * Remove all the trace readers from the priority queue | |
322 | */ | |
323 | this.prio.clear(); | |
866e5b51 FC |
324 | for (StreamInputReader streamInputReader : this.streamInputReaders) { |
325 | /* | |
326 | * Seek the trace reader. | |
327 | */ | |
bfe038ff | 328 | streamInputReader.seek(timestamp); |
ce2388e0 FC |
329 | |
330 | /* | |
331 | * Add it to the priority queue if there is a current event. | |
332 | */ | |
333 | ||
334 | } | |
335 | for (StreamInputReader streamInputReader : this.streamInputReaders) { | |
336 | if (streamInputReader.getCurrentEvent() != null) { | |
337 | this.prio.add(streamInputReader); | |
866e5b51 | 338 | |
ce2388e0 FC |
339 | } |
340 | } | |
866e5b51 FC |
341 | return hasMoreEvents(); |
342 | } | |
343 | ||
9ac2eb62 MK |
344 | /** |
345 | * gets the stream with the oldest event | |
346 | * | |
347 | * @return the stream with the oldest event | |
348 | */ | |
ce2388e0 FC |
349 | public StreamInputReader getTopStream() { |
350 | return this.prio.peek(); | |
351 | } | |
352 | ||
866e5b51 FC |
353 | /** |
354 | * Does the trace have more events? | |
355 | * | |
356 | * @return true if yes. | |
357 | */ | |
0594c61c | 358 | public final boolean hasMoreEvents() { |
866e5b51 FC |
359 | return this.prio.size() > 0; |
360 | } | |
361 | ||
362 | /** | |
363 | * Prints the event count stats. | |
364 | */ | |
365 | public void printStats() { | |
366 | printStats(60); | |
367 | } | |
368 | ||
369 | /** | |
370 | * Prints the event count stats. | |
371 | * | |
372 | * @param width | |
373 | * Width of the display. | |
374 | */ | |
375 | public void printStats(int width) { | |
376 | int numEvents = 0; | |
377 | if (width == 0) { | |
378 | return; | |
379 | } | |
380 | ||
bfe038ff | 381 | for (long i : this.eventCountPerTraceFile) { |
866e5b51 FC |
382 | numEvents += i; |
383 | } | |
384 | ||
385 | for (int j = 0; j < this.eventCountPerTraceFile.length; j++) { | |
386 | StreamInputReader se = this.streamInputReaders.get(j); | |
387 | ||
bfe038ff | 388 | long len = (width * this.eventCountPerTraceFile[se.getName()]) |
866e5b51 FC |
389 | / numEvents; |
390 | ||
0594c61c AM |
391 | StringBuilder sb = new StringBuilder(se.getFilename()); |
392 | sb.append("\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 | } |