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