ctf: simplify search
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / trace / CTFStreamInputReader.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 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.tracecompass.ctf.core.trace;
14
15 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
16
17 import java.io.File;
18 import java.io.IOException;
19 import java.nio.channels.FileChannel;
20 import java.nio.file.StandardOpenOption;
21
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.eclipse.tracecompass.common.core.NonNullUtils;
25 import org.eclipse.tracecompass.ctf.core.CTFException;
26 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
27 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
28 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
29 import org.eclipse.tracecompass.internal.ctf.core.Activator;
30
31 import com.google.common.collect.ImmutableList;
32
33 /**
34 * A CTF trace event reader. Reads the events of a trace file.
35 *
36 * @author Matthew Khouzam
37 * @author Simon Marchi
38 * @since 2.0
39 */
40 @NonNullByDefault
41 public class CTFStreamInputReader implements AutoCloseable {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 /**
48 * The StreamInput we are reading.
49 */
50 private final File fFile;
51
52 private final CTFStreamInput fStreamInput;
53
54 private final @Nullable FileChannel fFileChannel;
55
56 /**
57 * The packet reader used to read packets from this trace file.
58 */
59 private final CTFStreamInputPacketReader fPacketReader;
60
61 /**
62 * Iterator on the packet index
63 */
64 private int fPacketIndex;
65
66 /**
67 * Reference to the current event of this trace file (iow, the last on that
68 * was read, the next one to be returned)
69 */
70 private @Nullable EventDefinition fCurrentEvent = null;
71
72 private int fId;
73
74 /**
75 * Live trace reading
76 */
77 private boolean fLive = false;
78
79 // ------------------------------------------------------------------------
80 // Constructors
81 // ------------------------------------------------------------------------
82 /**
83 * Constructs a StreamInputReader that reads a StreamInput.
84 *
85 * @param streamInput
86 * The StreamInput to read.
87 * @throws CTFException
88 * If the file cannot be opened
89 */
90 public CTFStreamInputReader(CTFStreamInput streamInput) throws CTFException {
91 fStreamInput = streamInput;
92 fFile = fStreamInput.getFile();
93 try {
94 fFileChannel = FileChannel.open(fFile.toPath(), StandardOpenOption.READ);
95 } catch (IOException e) {
96 throw new CTFIOException(e);
97 }
98 try {
99 fPacketReader = new CTFStreamInputPacketReader(this);
100 /*
101 * Get the iterator on the packet index.
102 */
103 fPacketIndex = 0;
104 /*
105 * Make first packet the current one.
106 */
107 goToNextPacket();
108 } catch (Exception e) {
109 try {
110 close();
111 } catch (IOException e1) {
112 // Ignore
113 }
114 throw e;
115 }
116 }
117
118 /**
119 * Dispose the StreamInputReader, closes the file channel and its packet
120 * reader
121 *
122 * @throws IOException
123 * If an I/O error occurs
124 */
125 @Override
126 public void close() throws IOException {
127 if (fFileChannel != null) {
128 fFileChannel.close();
129 }
130 fPacketReader.close();
131 }
132
133 // ------------------------------------------------------------------------
134 // Getters/Setters/Predicates
135 // ------------------------------------------------------------------------
136
137 /**
138 * Gets the current event in this stream
139 *
140 * @return the current event in the stream, null if the stream is
141 * finished/empty/malformed
142 */
143 public @Nullable EventDefinition getCurrentEvent() {
144 return fCurrentEvent;
145 }
146
147 /**
148 * Gets the name of the stream (it's an id and a number)
149 *
150 * @return gets the stream name (it's a number)
151 */
152 public int getName() {
153 return fId;
154 }
155
156 /**
157 * Sets the name of the stream
158 *
159 * @param name
160 * the name of the stream, (it's a number)
161 */
162 public void setName(int name) {
163 fId = name;
164 }
165
166 /**
167 * Gets the CPU of a stream. It's the same as the one in /proc or running
168 * the asm CPUID instruction
169 *
170 * @return The CPU id (a number)
171 */
172 public int getCPU() {
173 return fPacketReader.getCPU();
174 }
175
176 /**
177 * Gets the filename of the stream being read
178 *
179 * @return The filename of the stream being read
180 */
181 public String getFilename() {
182 return fStreamInput.getFilename();
183 }
184
185 /*
186 * for internal use only
187 */
188 CTFStreamInput getStreamInput() {
189 return fStreamInput;
190 }
191
192 /**
193 * Gets the event definition set for this StreamInput
194 *
195 * @return Unmodifiable set with the event definitions
196 */
197 public Iterable<IEventDeclaration> getEventDeclarations() {
198 return checkNotNull(ImmutableList.copyOf(fStreamInput.getStream().getEventDeclarations()));
199 }
200
201 /**
202 * Set the trace to live mode
203 *
204 * @param live
205 * whether the trace is read live or not
206 */
207 public void setLive(boolean live) {
208 fLive = live;
209 }
210
211 /**
212 * Get if the trace is to read live or not
213 *
214 * @return whether the trace is live or not
215 */
216 public boolean isLive() {
217 return fLive;
218 }
219
220 /**
221 * Get the event context of the stream
222 *
223 * @return the event context declaration of the stream
224 */
225 public @Nullable StructDeclaration getStreamEventContextDecl() {
226 return getStreamInput().getStream().getEventContextDecl();
227 }
228
229 // ------------------------------------------------------------------------
230 // Operations
231 // ------------------------------------------------------------------------
232 /**
233 * Reads the next event in the current event variable.
234 *
235 * @return If an event has been successfully read.
236 * @throws CTFException
237 * if an error occurs
238 */
239 public CTFResponse readNextEvent() throws CTFException {
240
241 /*
242 * Change packet if needed
243 */
244 if (!fPacketReader.hasMoreEvents()) {
245 final ICTFPacketDescriptor prevPacket = fPacketReader.getCurrentPacket();
246 if (prevPacket != null || fLive) {
247 goToNextPacket();
248 }
249
250 }
251
252 /*
253 * If an event is available, read it.
254 */
255 if (fPacketReader.hasMoreEvents()) {
256 setCurrentEvent(fPacketReader.readNextEvent());
257 return CTFResponse.OK;
258 }
259 this.setCurrentEvent(null);
260 return fLive ? CTFResponse.WAIT : CTFResponse.FINISH;
261 }
262
263 /**
264 * Change the current packet of the packet reader to the next one.
265 *
266 * @throws CTFException
267 * if an error occurs
268 */
269 private void goToNextPacket() throws CTFException {
270 fPacketIndex++;
271 // did we already index the packet?
272 if (getPacketSize() >= (fPacketIndex + 1)) {
273 fPacketReader.setCurrentPacket(getPacket());
274 } else {
275 // go to the next packet if there is one, index it at the same time
276 if (fStreamInput.addPacketHeaderIndex()) {
277 fPacketIndex = getPacketSize() - 1;
278 fPacketReader.setCurrentPacket(getPacket());
279 } else {
280 // out of packets
281 fPacketReader.setCurrentPacket(null);
282 }
283 }
284 }
285
286 /**
287 * @return
288 */
289 private int getPacketSize() {
290 return fStreamInput.getIndex().size();
291 }
292
293 /**
294 * Changes the location of the trace file reader so that the current event
295 * is the first event with a timestamp greater or equal the given timestamp.
296 *
297 * @param timestamp
298 * The timestamp to seek to.
299 * @return The offset compared to the current position
300 * @throws CTFException
301 * if an error occurs
302 */
303 public long seek(long timestamp) throws CTFException {
304 long offset = 0;
305
306 gotoPacket(timestamp);
307
308 /*
309 * index up to the desired timestamp.
310 */
311 while ((fPacketReader.getCurrentPacket() != null)
312 && (fPacketReader.getCurrentPacket().getTimestampEnd() < timestamp)) {
313 try {
314 fStreamInput.addPacketHeaderIndex();
315 goToNextPacket();
316 } catch (CTFException e) {
317 // do nothing here
318 Activator.log(e.getMessage());
319 }
320 }
321 if (fPacketReader.getCurrentPacket() == null) {
322 gotoPacket(timestamp);
323 }
324
325 /*
326 * Advance until either of these conditions are met:
327 *
328 * - reached the end of the trace file (the given timestamp is after the
329 * last event)
330 *
331 * - found the first event with a timestamp greater or equal the given
332 * timestamp.
333 */
334 readNextEvent();
335 EventDefinition currentEvent = getCurrentEvent();
336 while (currentEvent != null && (currentEvent.getTimestamp() < timestamp)) {
337 readNextEvent();
338 currentEvent = getCurrentEvent();
339 offset++;
340 }
341 return offset;
342 }
343
344 /**
345 * @param timestamp
346 * the time to seek
347 * @throws CTFException
348 * if an error occurs
349 */
350 private void gotoPacket(long timestamp) throws CTFException {
351 fPacketIndex = fStreamInput.getIndex().search(timestamp) - 1;
352 /*
353 * Switch to this packet.
354 */
355 goToNextPacket();
356 }
357
358 /**
359 * Seeks the last event of a stream and returns it.
360 *
361 * @throws CTFException
362 * if an error occurs
363 */
364 public void goToLastEvent() throws CTFException {
365
366 /*
367 * Go to the beginning of the trace
368 */
369 seek(0);
370
371 /*
372 * Check that there is at least one event
373 */
374 if ((fStreamInput.getIndex().isEmpty()) || (!fPacketReader.hasMoreEvents())) {
375 /*
376 * This means the trace is empty. abort.
377 */
378 return;
379 }
380
381 fPacketIndex = fStreamInput.getIndex().size() - 1;
382 /*
383 * Go to last indexed packet
384 */
385 fPacketReader.setCurrentPacket(getPacket());
386
387 /*
388 * Keep going until you cannot
389 */
390 while (fPacketReader.getCurrentPacket() != null) {
391 goToNextPacket();
392 }
393
394 final int lastPacketIndex = fStreamInput.getIndex().size() - 1;
395 /*
396 * Go to the last packet that contains events.
397 */
398 for (int pos = lastPacketIndex; pos > 0; pos--) {
399 fPacketIndex = pos;
400 fPacketReader.setCurrentPacket(getPacket());
401
402 if (fPacketReader.hasMoreEvents()) {
403 break;
404 }
405 }
406
407 /*
408 * Go until the end of that packet
409 */
410 EventDefinition prevEvent = null;
411 while (fCurrentEvent != null) {
412 prevEvent = fCurrentEvent;
413 this.readNextEvent();
414 }
415 /*
416 * Go back to the previous event
417 */
418 this.setCurrentEvent(prevEvent);
419 }
420
421 /**
422 * Sets the current event in a stream input reader
423 *
424 * @param currentEvent
425 * the event to set
426 */
427 public void setCurrentEvent(@Nullable EventDefinition currentEvent) {
428 fCurrentEvent = currentEvent;
429 }
430
431 /**
432 * @return the packetIndexIt
433 */
434 private int getPacketIndex() {
435 return fPacketIndex;
436 }
437
438 private @Nullable ICTFPacketDescriptor getPacket() {
439 if (getPacketIndex() >= fStreamInput.getIndex().size()) {
440 return null;
441 }
442 return fStreamInput.getIndex().getElement(getPacketIndex());
443 }
444
445 /**
446 * Get the file channel wrapped by this reader
447 *
448 * @return the file channel
449 */
450 @Nullable
451 FileChannel getFc() {
452 return fFileChannel;
453 }
454
455 /**
456 * @return the packetReader
457 */
458 public CTFStreamInputPacketReader getPacketReader() {
459 return fPacketReader;
460 }
461
462 @Override
463 public int hashCode() {
464 final int prime = 31;
465 int result = 1;
466 result = (prime * result) + fId;
467 result = (prime * result)
468 + fFile.hashCode();
469 return result;
470 }
471
472 @Override
473 public boolean equals(@Nullable Object obj) {
474 if (this == obj) {
475 return true;
476 }
477 if (obj == null) {
478 return false;
479 }
480 if (!(obj instanceof CTFStreamInputReader)) {
481 return false;
482 }
483 CTFStreamInputReader other = (CTFStreamInputReader) obj;
484 if (fId != other.fId) {
485 return false;
486 }
487 return fFile.equals(other.fFile);
488 }
489
490 @Override
491 public String toString() {
492 // this helps debugging
493 return fId + ' ' + NonNullUtils.nullToEmptyString(fCurrentEvent);
494 }
495
496 }
This page took 0.054273 seconds and 5 git commands to generate.