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 | * | |
9 | * Contributors: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
13 | package org.eclipse.linuxtools.ctf.core.trace; | |
14 | ||
ce2388e0 | 15 | import java.nio.ByteOrder; |
7ff6d3cf MK |
16 | import java.util.Collections; |
17 | import java.util.HashMap; | |
18 | import java.util.Map; | |
866e5b51 FC |
19 | |
20 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; | |
21 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; | |
ce2388e0 | 22 | import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry; |
866e5b51 FC |
23 | |
24 | /** | |
d37aaa7f | 25 | * A CTF trace event reader. Reads the events of a trace file. |
32ede2ec | 26 | * |
d37aaa7f FC |
27 | * @version 1.0 |
28 | * @author Matthew Khouzam | |
29 | * @author Simon Marchi | |
866e5b51 FC |
30 | */ |
31 | public class StreamInputReader { | |
32 | ||
33 | // ------------------------------------------------------------------------ | |
34 | // Attributes | |
35 | // ------------------------------------------------------------------------ | |
36 | ||
37 | /** | |
38 | * The StreamInput we are reading. | |
39 | */ | |
40 | private final StreamInput streamInput; | |
41 | ||
42 | /** | |
43 | * The packet reader used to read packets from this trace file. | |
44 | */ | |
45 | private final StreamInputPacketReader packetReader; | |
46 | ||
47 | /** | |
48 | * Iterator on the packet index | |
49 | */ | |
bfe038ff | 50 | private int packetIndex; |
866e5b51 FC |
51 | |
52 | /** | |
53 | * Reference to the current event of this trace file (iow, the last on that | |
54 | * was read, the next one to be returned) | |
55 | */ | |
56 | private EventDefinition currentEvent = null; | |
57 | ||
58 | private int name; | |
59 | ||
bfe038ff MK |
60 | private CTFTraceReader parent; |
61 | ||
7ff6d3cf | 62 | /** Map of all the event types */ |
db8e8f7d | 63 | private final Map<Long, EventDefinition> eventDefs = new HashMap<Long, EventDefinition>(); |
bfe038ff | 64 | |
866e5b51 FC |
65 | // ------------------------------------------------------------------------ |
66 | // Constructors | |
67 | // ------------------------------------------------------------------------ | |
68 | ||
69 | /** | |
70 | * Constructs a StreamInputReader that reads a StreamInput. | |
71 | * | |
72 | * @param streamInput | |
73 | * The StreamInput to read. | |
db8e8f7d AM |
74 | * @throws CTFReaderException |
75 | * if an error occurs | |
e6809677 | 76 | * @since 2.0 |
866e5b51 | 77 | */ |
db8e8f7d | 78 | public StreamInputReader(StreamInput streamInput) throws CTFReaderException { |
866e5b51 FC |
79 | this.streamInput = streamInput; |
80 | this.packetReader = new StreamInputPacketReader(this); | |
866e5b51 FC |
81 | /* |
82 | * Get the iterator on the packet index. | |
83 | */ | |
bfe038ff | 84 | this.packetIndex = 0; |
866e5b51 FC |
85 | /* |
86 | * Make first packet the current one. | |
87 | */ | |
88 | goToNextPacket(); | |
89 | } | |
90 | ||
5d1c6919 PT |
91 | /** |
92 | * Dispose the StreamInputReader | |
db8e8f7d | 93 | * |
5d1c6919 PT |
94 | * @since 2.0 |
95 | */ | |
96 | public void dispose() { | |
97 | packetReader.dispose(); | |
98 | } | |
99 | ||
866e5b51 FC |
100 | // ------------------------------------------------------------------------ |
101 | // Getters/Setters/Predicates | |
102 | // ------------------------------------------------------------------------ | |
103 | ||
9ac2eb62 MK |
104 | /** |
105 | * Gets the current event in this stream | |
106 | * | |
107 | * @return the current event in the stream, null if the stream is | |
108 | * finished/empty/malformed | |
109 | */ | |
866e5b51 FC |
110 | public EventDefinition getCurrentEvent() { |
111 | return this.currentEvent; | |
112 | } | |
113 | ||
9ac2eb62 | 114 | /** |
ecb12461 | 115 | * Gets the current packet context |
9ac2eb62 MK |
116 | * |
117 | * @return the current packet context (size, lost events and such) | |
118 | */ | |
866e5b51 FC |
119 | public StructDefinition getCurrentPacketContext() { |
120 | return this.packetReader.getStreamPacketContextDef(); | |
121 | } | |
122 | ||
9ac2eb62 MK |
123 | /** |
124 | * Gets the byte order for a trace | |
125 | * | |
126 | * @return the trace byte order | |
127 | */ | |
ce2388e0 FC |
128 | public ByteOrder getByteOrder() { |
129 | return streamInput.getStream().getTrace().getByteOrder(); | |
866e5b51 FC |
130 | } |
131 | ||
9ac2eb62 MK |
132 | /** |
133 | * Gets the name of the stream (it's an id and a number) | |
134 | * | |
135 | * @return gets the stream name (it's a number) | |
136 | */ | |
866e5b51 FC |
137 | public int getName() { |
138 | return this.name; | |
139 | } | |
140 | ||
9ac2eb62 MK |
141 | /** |
142 | * Sets the name of the stream | |
143 | * | |
144 | * @param name | |
145 | * the name of the stream, (it's a number) | |
146 | */ | |
866e5b51 FC |
147 | public void setName(int name) { |
148 | this.name = name; | |
149 | } | |
150 | ||
9ac2eb62 MK |
151 | /** |
152 | * Gets the CPU of a stream. It's the same as the one in /proc or running | |
153 | * the asm CPUID instruction | |
154 | * | |
155 | * @return The CPU id (a number) | |
156 | */ | |
866e5b51 FC |
157 | public int getCPU() { |
158 | return this.packetReader.getCPU(); | |
159 | } | |
160 | ||
9ac2eb62 MK |
161 | /** |
162 | * Gets the filename of the stream being read | |
db8e8f7d | 163 | * |
9ac2eb62 MK |
164 | * @return The filename of the stream being read |
165 | */ | |
ce2388e0 FC |
166 | public String getFilename() { |
167 | return streamInput.getFilename(); | |
168 | } | |
169 | ||
170 | /* | |
171 | * for internal use only | |
172 | */ | |
173 | StreamInput getStreamInput() { | |
174 | return streamInput; | |
175 | } | |
176 | ||
7ff6d3cf MK |
177 | /** |
178 | * Gets the event definition hashmap for this StreamInput | |
179 | * | |
180 | * @return Unmodifiable map with the event definitions | |
181 | * @since 2.1 | |
182 | */ | |
183 | public Map<Long, EventDefinition> getEventDefinitions() { | |
184 | return Collections.unmodifiableMap(eventDefs); | |
185 | } | |
186 | ||
187 | /** | |
188 | * Add an event definition to this stream input reader. | |
189 | * | |
190 | * @param id | |
191 | * The id of the event definition. This will overwrite any | |
192 | * existing definition with the same id. | |
193 | * @param def | |
194 | * The matching event definition | |
195 | * @since 2.1 | |
196 | */ | |
197 | public void addEventDefinition(Long id, EventDefinition def) { | |
198 | eventDefs.put(id, def); | |
199 | } | |
200 | ||
866e5b51 FC |
201 | // ------------------------------------------------------------------------ |
202 | // Operations | |
203 | // ------------------------------------------------------------------------ | |
204 | /** | |
205 | * Reads the next event in the current event variable. | |
206 | * | |
207 | * @return If an event has been successfully read. | |
db8e8f7d AM |
208 | * @throws CTFReaderException |
209 | * if an error occurs | |
866e5b51 | 210 | */ |
db8e8f7d | 211 | public boolean readNextEvent() throws CTFReaderException { |
bfe038ff | 212 | |
866e5b51 FC |
213 | /* |
214 | * Change packet if needed | |
215 | */ | |
216 | if (!this.packetReader.hasMoreEvents()) { | |
bfe038ff MK |
217 | final StreamInputPacketIndexEntry prevPacket = this.packetReader |
218 | .getCurrentPacket(); | |
219 | if (prevPacket != null) { | |
220 | goToNextPacket(); | |
bfe038ff | 221 | } |
866e5b51 FC |
222 | } |
223 | ||
32ede2ec | 224 | /* |
866e5b51 FC |
225 | * If an event is available, read it. |
226 | */ | |
227 | if (this.packetReader.hasMoreEvents()) { | |
59b86108 | 228 | this.setCurrentEvent(this.packetReader.readNextEvent()); |
866e5b51 FC |
229 | return true; |
230 | } | |
231 | this.setCurrentEvent(null); | |
232 | return false; | |
233 | } | |
234 | ||
235 | /** | |
236 | * Change the current packet of the packet reader to the next one. | |
db8e8f7d AM |
237 | * |
238 | * @throws CTFReaderException | |
239 | * if an error occurs | |
866e5b51 | 240 | */ |
db8e8f7d | 241 | private void goToNextPacket() throws CTFReaderException { |
bfe038ff MK |
242 | packetIndex++; |
243 | if (getPacketSize() >= (packetIndex + 1)) { | |
244 | this.packetReader.setCurrentPacket(getPacket()); | |
866e5b51 | 245 | } else { |
cf9a28da MK |
246 | if (this.streamInput.addPacketHeaderIndex()) { |
247 | packetIndex = getPacketSize() - 1; | |
248 | this.packetReader.setCurrentPacket(getPacket()); | |
249 | } else { | |
bfe038ff MK |
250 | this.packetReader.setCurrentPacket(null); |
251 | } | |
866e5b51 FC |
252 | } |
253 | } | |
254 | ||
bfe038ff MK |
255 | /** |
256 | * @return | |
257 | */ | |
258 | private int getPacketSize() { | |
259 | return streamInput.getIndex().getEntries().size(); | |
260 | } | |
261 | ||
866e5b51 FC |
262 | /** |
263 | * Changes the location of the trace file reader so that the current event | |
ecb12461 | 264 | * is the first event with a timestamp greater or equal the given timestamp. |
866e5b51 FC |
265 | * |
266 | * @param timestamp | |
267 | * The timestamp to seek to. | |
be6df2d8 | 268 | * @return The offset compared to the current position |
db8e8f7d AM |
269 | * @throws CTFReaderException |
270 | * if an error occurs | |
866e5b51 | 271 | */ |
db8e8f7d | 272 | public long seek(long timestamp) throws CTFReaderException { |
ce2388e0 | 273 | long offset = 0; |
866e5b51 | 274 | |
eb94f9c9 | 275 | gotoPacket(timestamp); |
866e5b51 | 276 | |
0c59c1a6 MK |
277 | /* |
278 | * index up to the desired timestamp. | |
279 | */ | |
280 | while ((this.packetReader.getCurrentPacket() != null) | |
281 | && (this.packetReader.getCurrentPacket().getTimestampEnd() < timestamp)) { | |
282 | try { | |
283 | this.streamInput.addPacketHeaderIndex(); | |
284 | goToNextPacket(); | |
285 | } catch (CTFReaderException e) { | |
286 | // do nothing here | |
287 | } | |
288 | } | |
9ac2eb62 | 289 | if (this.packetReader.getCurrentPacket() == null) { |
eb94f9c9 MK |
290 | gotoPacket(timestamp); |
291 | } | |
0c59c1a6 | 292 | |
866e5b51 | 293 | /* |
ecb12461 EB |
294 | * Advance until either of these conditions are met |
295 | * <ul> | |
cf9a28da MK |
296 | * <li> reached the end of the trace file (the given timestamp is after the last event), </li> |
297 | * <li> found the first event with a timestamp greater or equal the given timestamp. </li> | |
ecb12461 | 298 | * </ul> |
866e5b51 FC |
299 | */ |
300 | readNextEvent(); | |
301 | boolean done = (this.getCurrentEvent() == null); | |
aa572e22 | 302 | while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) { |
866e5b51 FC |
303 | readNextEvent(); |
304 | done = (this.getCurrentEvent() == null); | |
ce2388e0 | 305 | offset++; |
866e5b51 | 306 | } |
ce2388e0 FC |
307 | return offset; |
308 | } | |
309 | ||
eb94f9c9 MK |
310 | /** |
311 | * @param timestamp | |
db8e8f7d AM |
312 | * @throws CTFReaderException |
313 | * if an error occurs | |
eb94f9c9 | 314 | */ |
db8e8f7d | 315 | private void gotoPacket(long timestamp) throws CTFReaderException { |
eb94f9c9 MK |
316 | this.packetIndex = this.streamInput.getIndex().search(timestamp) |
317 | .previousIndex(); | |
318 | /* | |
319 | * Switch to this packet. | |
320 | */ | |
321 | goToNextPacket(); | |
322 | } | |
323 | ||
9ac2eb62 MK |
324 | /** |
325 | * Seeks the last event of a stream and returns it. | |
db8e8f7d AM |
326 | * |
327 | * @throws CTFReaderException | |
328 | * if an error occurs | |
9ac2eb62 | 329 | */ |
db8e8f7d | 330 | public void goToLastEvent() throws CTFReaderException { |
866e5b51 FC |
331 | /* |
332 | * Search in the index for the packet to search in. | |
333 | */ | |
ec6f5beb MK |
334 | final int len = this.streamInput.getIndex().getEntries().size(); |
335 | ||
866e5b51 | 336 | /* |
ec6f5beb | 337 | * Go to beginning of trace. |
866e5b51 | 338 | */ |
ec6f5beb MK |
339 | seek(0); |
340 | /* | |
341 | * if the trace is empty. | |
342 | */ | |
bfe038ff | 343 | if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) { |
ec6f5beb MK |
344 | /* |
345 | * This means the trace is empty. abort. | |
346 | */ | |
347 | return; | |
ce2388e0 | 348 | } |
ec6f5beb MK |
349 | /* |
350 | * Go to the last packet that contains events. | |
351 | */ | |
bfe038ff MK |
352 | for (int pos = len - 1; pos > 0; pos--) { |
353 | packetIndex = pos; | |
354 | this.packetReader.setCurrentPacket(getPacket()); | |
355 | if (this.packetReader.hasMoreEvents()) { | |
ec6f5beb MK |
356 | break; |
357 | } | |
866e5b51 | 358 | } |
ec6f5beb MK |
359 | |
360 | /* | |
361 | * Go until the end of that packet | |
362 | */ | |
363 | EventDefinition prevEvent = null; | |
364 | while (this.currentEvent != null) { | |
365 | prevEvent = this.currentEvent; | |
366 | this.readNextEvent(); | |
367 | } | |
368 | /* | |
369 | * Go back to the previous event | |
370 | */ | |
371 | this.setCurrentEvent(prevEvent); | |
866e5b51 FC |
372 | } |
373 | ||
bfe038ff MK |
374 | /** |
375 | * @return the parent | |
376 | */ | |
377 | public CTFTraceReader getParent() { | |
378 | return parent; | |
379 | } | |
380 | ||
381 | /** | |
382 | * @param parent | |
383 | * the parent to set | |
384 | */ | |
385 | public void setParent(CTFTraceReader parent) { | |
386 | this.parent = parent; | |
387 | } | |
388 | ||
9ac2eb62 MK |
389 | /** |
390 | * Sets the current event in a stream input reader | |
db8e8f7d AM |
391 | * |
392 | * @param currentEvent | |
393 | * the event to set | |
9ac2eb62 | 394 | */ |
866e5b51 FC |
395 | public void setCurrentEvent(EventDefinition currentEvent) { |
396 | this.currentEvent = currentEvent; | |
397 | } | |
398 | ||
ce2388e0 FC |
399 | /** |
400 | * @return the packetIndexIt | |
401 | */ | |
bfe038ff MK |
402 | private int getPacketIndex() { |
403 | return packetIndex; | |
404 | } | |
405 | ||
406 | private StreamInputPacketIndexEntry getPacket() { | |
407 | return streamInput.getIndex().getEntries().get(getPacketIndex()); | |
ce2388e0 FC |
408 | } |
409 | ||
410 | /** | |
411 | * @return the packetReader | |
412 | */ | |
413 | public StreamInputPacketReader getPacketReader() { | |
414 | return packetReader; | |
415 | } | |
416 | ||
81c8e6f7 MK |
417 | @Override |
418 | public int hashCode() { | |
419 | final int prime = 31; | |
420 | int result = 1; | |
421 | result = (prime * result) + name; | |
422 | result = (prime * result) | |
423 | + ((streamInput == null) ? 0 : streamInput.hashCode()); | |
424 | return result; | |
425 | } | |
426 | ||
81c8e6f7 MK |
427 | @Override |
428 | public boolean equals(Object obj) { | |
429 | if (this == obj) { | |
430 | return true; | |
431 | } | |
432 | if (obj == null) { | |
433 | return false; | |
434 | } | |
435 | if (!(obj instanceof StreamInputReader)) { | |
436 | return false; | |
437 | } | |
438 | StreamInputReader other = (StreamInputReader) obj; | |
439 | if (name != other.name) { | |
440 | return false; | |
441 | } | |
442 | if (streamInput == null) { | |
443 | if (other.streamInput != null) { | |
444 | return false; | |
445 | } | |
446 | } else if (!streamInput.equals(other.streamInput)) { | |
447 | return false; | |
448 | } | |
449 | return true; | |
450 | } | |
451 | ||
87b60a47 MK |
452 | @Override |
453 | public String toString() { | |
454 | // this helps debugging | |
455 | return this.name + ' ' + this.currentEvent.toString(); | |
456 | } | |
866e5b51 | 457 | } |