ctf: support traces with no content but a packet size
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInputReader.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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
15 import java.nio.ByteOrder;
16 import java.util.Collections;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
21 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
22 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
23
24 /**
25 * A CTF trace event reader. Reads the events of a trace file.
26 *
27 * @version 1.0
28 * @author Matthew Khouzam
29 * @author Simon Marchi
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 */
50 private int packetIndex;
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
60 private CTFTraceReader parent;
61
62 /** Map of all the event types */
63 private final Map<Long, EventDefinition> eventDefs = new HashMap<Long, EventDefinition>();
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
69 /**
70 * Constructs a StreamInputReader that reads a StreamInput.
71 *
72 * @param streamInput
73 * The StreamInput to read.
74 * @throws CTFReaderException
75 * if an error occurs
76 * @since 2.0
77 */
78 public StreamInputReader(StreamInput streamInput) throws CTFReaderException {
79 this.streamInput = streamInput;
80 this.packetReader = new StreamInputPacketReader(this);
81 /*
82 * Get the iterator on the packet index.
83 */
84 this.packetIndex = 0;
85 /*
86 * Make first packet the current one.
87 */
88 goToNextPacket();
89 }
90
91 /**
92 * Dispose the StreamInputReader
93 *
94 * @since 2.0
95 */
96 public void dispose() {
97 packetReader.dispose();
98 }
99
100 // ------------------------------------------------------------------------
101 // Getters/Setters/Predicates
102 // ------------------------------------------------------------------------
103
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 */
110 public EventDefinition getCurrentEvent() {
111 return this.currentEvent;
112 }
113
114 /**
115 * Gets the current packet context
116 *
117 * @return the current packet context (size, lost events and such)
118 */
119 public StructDefinition getCurrentPacketContext() {
120 return this.packetReader.getStreamPacketContextDef();
121 }
122
123 /**
124 * Gets the byte order for a trace
125 *
126 * @return the trace byte order
127 */
128 public ByteOrder getByteOrder() {
129 return streamInput.getStream().getTrace().getByteOrder();
130 }
131
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 */
137 public int getName() {
138 return this.name;
139 }
140
141 /**
142 * Sets the name of the stream
143 *
144 * @param name
145 * the name of the stream, (it's a number)
146 */
147 public void setName(int name) {
148 this.name = name;
149 }
150
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 */
157 public int getCPU() {
158 return this.packetReader.getCPU();
159 }
160
161 /**
162 * Gets the filename of the stream being read
163 *
164 * @return The filename of the stream being read
165 */
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
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
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.
208 * @throws CTFReaderException
209 * if an error occurs
210 */
211 public boolean readNextEvent() throws CTFReaderException {
212
213 /*
214 * Change packet if needed
215 */
216 if (!this.packetReader.hasMoreEvents()) {
217 final StreamInputPacketIndexEntry prevPacket = this.packetReader
218 .getCurrentPacket();
219 if (prevPacket != null) {
220 goToNextPacket();
221 }
222 }
223
224 /*
225 * If an event is available, read it.
226 */
227 if (this.packetReader.hasMoreEvents()) {
228 this.setCurrentEvent(this.packetReader.readNextEvent());
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.
237 *
238 * @throws CTFReaderException
239 * if an error occurs
240 */
241 private void goToNextPacket() throws CTFReaderException {
242 packetIndex++;
243 if (getPacketSize() >= (packetIndex + 1)) {
244 this.packetReader.setCurrentPacket(getPacket());
245 } else {
246 if (this.streamInput.addPacketHeaderIndex()) {
247 packetIndex = getPacketSize() - 1;
248 this.packetReader.setCurrentPacket(getPacket());
249 } else {
250 this.packetReader.setCurrentPacket(null);
251 }
252 }
253 }
254
255 /**
256 * @return
257 */
258 private int getPacketSize() {
259 return streamInput.getIndex().getEntries().size();
260 }
261
262 /**
263 * Changes the location of the trace file reader so that the current event
264 * is the first event with a timestamp greater or equal the given timestamp.
265 *
266 * @param timestamp
267 * The timestamp to seek to.
268 * @return The offset compared to the current position
269 * @throws CTFReaderException
270 * if an error occurs
271 */
272 public long seek(long timestamp) throws CTFReaderException {
273 long offset = 0;
274
275 gotoPacket(timestamp);
276
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 }
289 if (this.packetReader.getCurrentPacket() == null) {
290 gotoPacket(timestamp);
291 }
292
293 /*
294 * Advance until either of these conditions are met
295 * <ul>
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>
298 * </ul>
299 */
300 readNextEvent();
301 boolean done = (this.getCurrentEvent() == null);
302 while (!done && (this.getCurrentEvent().getTimestamp() < timestamp)) {
303 readNextEvent();
304 done = (this.getCurrentEvent() == null);
305 offset++;
306 }
307 return offset;
308 }
309
310 /**
311 * @param timestamp
312 * @throws CTFReaderException
313 * if an error occurs
314 */
315 private void gotoPacket(long timestamp) throws CTFReaderException {
316 this.packetIndex = this.streamInput.getIndex().search(timestamp)
317 .previousIndex();
318 /*
319 * Switch to this packet.
320 */
321 goToNextPacket();
322 }
323
324 /**
325 * Seeks the last event of a stream and returns it.
326 *
327 * @throws CTFReaderException
328 * if an error occurs
329 */
330 public void goToLastEvent() throws CTFReaderException {
331 /*
332 * Search in the index for the packet to search in.
333 */
334 final int len = this.streamInput.getIndex().getEntries().size();
335
336 /*
337 * Go to beginning of trace.
338 */
339 seek(0);
340 /*
341 * if the trace is empty.
342 */
343 if ((len == 0) || (this.packetReader.hasMoreEvents() == false)) {
344 /*
345 * This means the trace is empty. abort.
346 */
347 return;
348 }
349 /*
350 * Go to the last packet that contains events.
351 */
352 for (int pos = len - 1; pos > 0; pos--) {
353 packetIndex = pos;
354 this.packetReader.setCurrentPacket(getPacket());
355 if (this.packetReader.hasMoreEvents()) {
356 break;
357 }
358 }
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);
372 }
373
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
389 /**
390 * Sets the current event in a stream input reader
391 *
392 * @param currentEvent
393 * the event to set
394 */
395 public void setCurrentEvent(EventDefinition currentEvent) {
396 this.currentEvent = currentEvent;
397 }
398
399 /**
400 * @return the packetIndexIt
401 */
402 private int getPacketIndex() {
403 return packetIndex;
404 }
405
406 private StreamInputPacketIndexEntry getPacket() {
407 return streamInput.getIndex().getEntries().get(getPacketIndex());
408 }
409
410 /**
411 * @return the packetReader
412 */
413 public StreamInputPacketReader getPacketReader() {
414 return packetReader;
415 }
416
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
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
452 @Override
453 public String toString() {
454 // this helps debugging
455 return this.name + ' ' + this.currentEvent.toString();
456 }
457 }
This page took 0.049822 seconds and 5 git commands to generate.