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 | */ | |
93a45b54 | 40 | private final StreamInput fStreamInput; |
866e5b51 FC |
41 | |
42 | /** | |
43 | * The packet reader used to read packets from this trace file. | |
44 | */ | |
93a45b54 | 45 | private final StreamInputPacketReader fPacketReader; |
866e5b51 FC |
46 | |
47 | /** | |
48 | * Iterator on the packet index | |
49 | */ | |
93a45b54 | 50 | private int fPacketIndex; |
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 | */ | |
93a45b54 | 56 | private EventDefinition fCurrentEvent = null; |
866e5b51 | 57 | |
93a45b54 | 58 | private int fId; |
866e5b51 | 59 | |
93a45b54 | 60 | private CTFTraceReader fParent; |
bfe038ff | 61 | |
7ff6d3cf | 62 | /** Map of all the event types */ |
93a45b54 | 63 | private final Map<Long, EventDefinition> fEventDefs = new HashMap<>(); |
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 { |
93a45b54 MK |
79 | fStreamInput = streamInput; |
80 | fPacketReader = new StreamInputPacketReader(this); | |
866e5b51 FC |
81 | /* |
82 | * Get the iterator on the packet index. | |
83 | */ | |
93a45b54 | 84 | fPacketIndex = 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() { | |
93a45b54 | 97 | fPacketReader.dispose(); |
5d1c6919 PT |
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 | 110 | public EventDefinition getCurrentEvent() { |
93a45b54 | 111 | return fCurrentEvent; |
866e5b51 FC |
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 | 119 | public StructDefinition getCurrentPacketContext() { |
93a45b54 | 120 | return fPacketReader.getStreamPacketContextDef(); |
866e5b51 FC |
121 | } |
122 | ||
9ac2eb62 MK |
123 | /** |
124 | * Gets the byte order for a trace | |
125 | * | |
126 | * @return the trace byte order | |
127 | */ | |
ce2388e0 | 128 | public ByteOrder getByteOrder() { |
93a45b54 | 129 | return fStreamInput.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 | 137 | public int getName() { |
93a45b54 | 138 | return fId; |
866e5b51 FC |
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 | 147 | public void setName(int name) { |
93a45b54 | 148 | fId = name; |
866e5b51 FC |
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 | 157 | public int getCPU() { |
93a45b54 | 158 | return fPacketReader.getCPU(); |
866e5b51 FC |
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 | 166 | public String getFilename() { |
93a45b54 | 167 | return fStreamInput.getFilename(); |
ce2388e0 FC |
168 | } |
169 | ||
170 | /* | |
171 | * for internal use only | |
172 | */ | |
173 | StreamInput getStreamInput() { | |
93a45b54 | 174 | return fStreamInput; |
ce2388e0 FC |
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() { | |
93a45b54 | 184 | return Collections.unmodifiableMap(fEventDefs); |
7ff6d3cf MK |
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) { | |
93a45b54 | 198 | fEventDefs.put(id, def); |
7ff6d3cf MK |
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 | */ | |
93a45b54 MK |
216 | if (!fPacketReader.hasMoreEvents()) { |
217 | final StreamInputPacketIndexEntry prevPacket = fPacketReader | |
bfe038ff MK |
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 | */ | |
93a45b54 MK |
227 | if (fPacketReader.hasMoreEvents()) { |
228 | this.setCurrentEvent(fPacketReader.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 { |
93a45b54 MK |
242 | fPacketIndex++; |
243 | if (getPacketSize() >= (fPacketIndex + 1)) { | |
244 | fPacketReader.setCurrentPacket(getPacket()); | |
866e5b51 | 245 | } else { |
93a45b54 MK |
246 | if (fStreamInput.addPacketHeaderIndex()) { |
247 | fPacketIndex = getPacketSize() - 1; | |
248 | fPacketReader.setCurrentPacket(getPacket()); | |
cf9a28da | 249 | } else { |
93a45b54 | 250 | fPacketReader.setCurrentPacket(null); |
bfe038ff | 251 | } |
866e5b51 FC |
252 | } |
253 | } | |
254 | ||
bfe038ff MK |
255 | /** |
256 | * @return | |
257 | */ | |
258 | private int getPacketSize() { | |
93a45b54 | 259 | return fStreamInput.getIndex().getEntries().size(); |
bfe038ff MK |
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 | */ | |
93a45b54 MK |
280 | while ((fPacketReader.getCurrentPacket() != null) |
281 | && (fPacketReader.getCurrentPacket().getTimestampEnd() < timestamp)) { | |
0c59c1a6 | 282 | try { |
93a45b54 | 283 | fStreamInput.addPacketHeaderIndex(); |
0c59c1a6 MK |
284 | goToNextPacket(); |
285 | } catch (CTFReaderException e) { | |
286 | // do nothing here | |
287 | } | |
288 | } | |
93a45b54 | 289 | if (fPacketReader.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 { |
93a45b54 | 316 | fPacketIndex = fStreamInput.getIndex().search(timestamp) |
eb94f9c9 MK |
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 | */ | |
93a45b54 | 334 | final int len = fStreamInput.getIndex().getEntries().size(); |
ec6f5beb | 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 | */ | |
93a45b54 | 343 | if ((len == 0) || (fPacketReader.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 | 352 | for (int pos = len - 1; pos > 0; pos--) { |
93a45b54 MK |
353 | fPacketIndex = pos; |
354 | fPacketReader.setCurrentPacket(getPacket()); | |
355 | if (fPacketReader.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; | |
93a45b54 MK |
364 | while (fCurrentEvent != null) { |
365 | prevEvent = fCurrentEvent; | |
ec6f5beb MK |
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() { | |
93a45b54 | 378 | return fParent; |
bfe038ff MK |
379 | } |
380 | ||
381 | /** | |
382 | * @param parent | |
383 | * the parent to set | |
384 | */ | |
385 | public void setParent(CTFTraceReader parent) { | |
93a45b54 | 386 | fParent = parent; |
bfe038ff MK |
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 | 395 | public void setCurrentEvent(EventDefinition currentEvent) { |
93a45b54 | 396 | fCurrentEvent = currentEvent; |
866e5b51 FC |
397 | } |
398 | ||
ce2388e0 FC |
399 | /** |
400 | * @return the packetIndexIt | |
401 | */ | |
bfe038ff | 402 | private int getPacketIndex() { |
93a45b54 | 403 | return fPacketIndex; |
bfe038ff MK |
404 | } |
405 | ||
406 | private StreamInputPacketIndexEntry getPacket() { | |
93a45b54 | 407 | return fStreamInput.getIndex().getEntries().get(getPacketIndex()); |
ce2388e0 FC |
408 | } |
409 | ||
410 | /** | |
411 | * @return the packetReader | |
412 | */ | |
413 | public StreamInputPacketReader getPacketReader() { | |
93a45b54 | 414 | return fPacketReader; |
ce2388e0 FC |
415 | } |
416 | ||
81c8e6f7 MK |
417 | @Override |
418 | public int hashCode() { | |
419 | final int prime = 31; | |
420 | int result = 1; | |
93a45b54 | 421 | result = (prime * result) + fId; |
81c8e6f7 | 422 | result = (prime * result) |
93a45b54 | 423 | + ((fStreamInput == null) ? 0 : fStreamInput.hashCode()); |
81c8e6f7 MK |
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; | |
93a45b54 | 439 | if (fId != other.fId) { |
81c8e6f7 MK |
440 | return false; |
441 | } | |
93a45b54 MK |
442 | if (fStreamInput == null) { |
443 | if (other.fStreamInput != null) { | |
81c8e6f7 MK |
444 | return false; |
445 | } | |
93a45b54 | 446 | } else if (!fStreamInput.equals(other.fStreamInput)) { |
81c8e6f7 MK |
447 | return false; |
448 | } | |
449 | return true; | |
450 | } | |
451 | ||
87b60a47 MK |
452 | @Override |
453 | public String toString() { | |
454 | // this helps debugging | |
93a45b54 | 455 | return fId + ' ' + fCurrentEvent.toString(); |
87b60a47 | 456 | } |
866e5b51 | 457 | } |