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