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