02d0822ad5e302f60b376ef62bfe236fecdaff16
[deliverable/tracecompass.git] / org.eclipse.linuxtools.ctf.core / src / org / eclipse / linuxtools / ctf / core / trace / StreamInput.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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.io.File;
16 import java.io.IOException;
17 import java.nio.ByteBuffer;
18 import java.nio.channels.FileChannel;
19 import java.nio.channels.FileChannel.MapMode;
20 import java.util.UUID;
21
22 import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
23 import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
24 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
25 import org.eclipse.linuxtools.ctf.core.event.types.EnumDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.types.FloatDefinition;
27 import org.eclipse.linuxtools.ctf.core.event.types.IDefinitionScope;
28 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
29 import org.eclipse.linuxtools.ctf.core.event.types.StringDefinition;
30 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
31 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndex;
32 import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntry;
33
34 /**
35 * <b><u>StreamInput</u></b>
36 * <p>
37 * Represents a trace file that belongs to a certain stream.
38 *
39 * @since 2.0
40 */
41 public class StreamInput implements IDefinitionScope {
42
43 // ------------------------------------------------------------------------
44 // Attributes
45 // ------------------------------------------------------------------------
46
47 /**
48 * The associated Stream
49 */
50 private final Stream fStream;
51
52 /**
53 * FileChannel to the trace file
54 */
55 private final FileChannel fFileChannel;
56
57 /**
58 * Information on the file (used for debugging)
59 */
60 private final File fFile;
61
62 /**
63 * The packet index of this input
64 */
65 private final StreamInputPacketIndex fIndex;
66
67 private long fTimestampEnd;
68
69 /*
70 * Definition of trace packet header
71 */
72 private StructDefinition fTracePacketHeaderDef = null;
73
74 /*
75 * Definition of trace stream packet context
76 */
77 private StructDefinition fStreamPacketContextDef = null;
78
79 /*
80 * Total number of lost events in this stream
81 */
82 private long fLostSoFar = 0;
83
84 // ------------------------------------------------------------------------
85 // Constructors
86 // ------------------------------------------------------------------------
87
88 /**
89 * Constructs a StreamInput.
90 *
91 * @param stream
92 * The stream to which this StreamInput belongs to.
93 * @param fileChannel
94 * The FileChannel to the trace file.
95 * @param file
96 * Information about the trace file (for debugging purposes).
97 */
98 public StreamInput(Stream stream, FileChannel fileChannel, File file) {
99 fStream = stream;
100 fFileChannel = fileChannel;
101 fFile = file;
102 fIndex = new StreamInputPacketIndex();
103 }
104
105 // ------------------------------------------------------------------------
106 // Getters/Setters/Predicates
107 // ------------------------------------------------------------------------
108
109 /**
110 * Gets the stream the streamInput wrapper is wrapping
111 *
112 * @return the stream the streamInput wrapper is wrapping
113 */
114 public Stream getStream() {
115 return fStream;
116 }
117
118 /**
119 * The common streamInput Index
120 *
121 * @return the stream input Index
122 */
123 StreamInputPacketIndex getIndex() {
124 return fIndex;
125 }
126
127 /**
128 * Gets the filename of the streamInput file.
129 *
130 * @return the filename of the streaminput file.
131 */
132 public String getFilename() {
133 return fFile.getName();
134 }
135
136 /**
137 * Gets the last read timestamp of a stream. (this is not necessarily the
138 * last time in the stream.)
139 *
140 * @return the last read timestamp
141 */
142 public long getTimestampEnd() {
143 return fTimestampEnd;
144 }
145
146 /**
147 * Sets the last read timestamp of a stream. (this is not necessarily the
148 * last time in the stream.)
149 *
150 * @param timestampEnd
151 * the last read timestamp
152 */
153 public void setTimestampEnd(long timestampEnd) {
154 fTimestampEnd = timestampEnd;
155 }
156
157 /**
158 * Useless for streaminputs
159 */
160 @Override
161 public String getPath() {
162 return ""; //$NON-NLS-1$
163 }
164
165 // ------------------------------------------------------------------------
166 // Operations
167 // ------------------------------------------------------------------------
168
169 @Override
170 public Definition lookupDefinition(String lookupPath) {
171 /* TODO: lookup in different dynamic scopes is not supported yet. */
172 return null;
173 }
174
175 /**
176 * Create the index for this trace file.
177 */
178 public void setupIndex() {
179
180 /*
181 * The BitBuffer to extract data from the StreamInput
182 */
183 BitBuffer bitBuffer = new BitBuffer();
184 bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());
185
186 /*
187 * Create the definitions we need to read the packet headers + contexts
188 */
189 if (getStream().getTrace().getPacketHeader() != null) {
190 fTracePacketHeaderDef = getStream().getTrace().getPacketHeader()
191 .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$
192 }
193
194 if (getStream().getPacketContextDecl() != null) {
195 fStreamPacketContextDef = getStream().getPacketContextDecl()
196 .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$
197 }
198
199 }
200
201 /**
202 * Adds the next packet header index entry to the index of a stream input.
203 *
204 * @warning slow, can corrupt data if not used properly
205 * @return true if there are more packets to add
206 * @throws CTFReaderException
207 * If there was a problem reading the packed header
208 */
209 public boolean addPacketHeaderIndex() throws CTFReaderException {
210 long currentPos = 0L;
211 if (!fIndex.getEntries().isEmpty()) {
212 StreamInputPacketIndexEntry pos = fIndex.getEntries().lastElement();
213 currentPos = computeNextOffset(pos);
214 }
215 long fileSize = getStreamSize();
216 if (currentPos < fileSize) {
217 BitBuffer bitBuffer = new BitBuffer();
218 bitBuffer.setByteOrder(getStream().getTrace().getByteOrder());
219 StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry(
220 currentPos);
221 createPacketIndexEntry(fileSize, currentPos, packetIndex,
222 fTracePacketHeaderDef, fStreamPacketContextDef, bitBuffer);
223 fIndex.addEntry(packetIndex);
224 return true;
225 }
226 return false;
227 }
228
229 private long getStreamSize() {
230 return fFile.length();
231 }
232
233 private long createPacketIndexEntry(long fileSizeBytes,
234 long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
235 StructDefinition tracePacketHeaderDef,
236 StructDefinition streamPacketContextDef, BitBuffer bitBuffer)
237 throws CTFReaderException {
238
239 /*
240 * Ignoring the return value, but this call is needed to initialize the
241 * input
242 */
243 createPacketBitBuffer(fileSizeBytes, packetOffsetBytes, packetIndex, bitBuffer);
244
245 /*
246 * Read the trace packet header if it exists.
247 */
248 if (tracePacketHeaderDef != null) {
249 parseTracePacketHeader(tracePacketHeaderDef, bitBuffer);
250 }
251
252 /*
253 * Read the stream packet context if it exists.
254 */
255 if (streamPacketContextDef != null) {
256 parsePacketContext(fileSizeBytes, streamPacketContextDef,
257 bitBuffer, packetIndex);
258 } else {
259 setPacketContextNull(fileSizeBytes, packetIndex);
260 }
261
262 /* Basic validation */
263 if (packetIndex.getContentSizeBits() > packetIndex.getPacketSizeBits()) {
264 throw new CTFReaderException("Content size > packet size"); //$NON-NLS-1$
265 }
266
267 if (packetIndex.getPacketSizeBits() > ((fileSizeBytes - packetIndex
268 .getOffsetBytes()) * 8)) {
269 throw new CTFReaderException("Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$
270 }
271
272 /*
273 * Offset in the file, in bits
274 */
275 packetIndex.setDataOffsetBits(bitBuffer.position());
276
277 /*
278 * Update the counting packet offset
279 */
280 return computeNextOffset(packetIndex);
281 }
282
283 /**
284 * @param packetIndex
285 * @return
286 */
287 private static long computeNextOffset(
288 StreamInputPacketIndexEntry packetIndex) {
289 return packetIndex.getOffsetBytes()
290 + ((packetIndex.getPacketSizeBits() + 7) / 8);
291 }
292
293 ByteBuffer getByteBufferAt(long position, long size) throws IOException {
294 return fFileChannel.map(MapMode.READ_ONLY, position, size);
295 }
296
297 /**
298 * @param fileSizeBytes
299 * @param packetOffsetBytes
300 * @param packetIndex
301 * @param bitBuffer
302 * @return
303 * @throws CTFReaderException
304 */
305 private ByteBuffer createPacketBitBuffer(long fileSizeBytes,
306 long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex,
307 BitBuffer bitBuffer) throws CTFReaderException {
308 /*
309 * Initial size, it should map at least the packet header + context
310 * size.
311 *
312 * TODO: use a less arbitrary size.
313 */
314 long mapSize = 4096;
315 /*
316 * If there is less data remaining than what we want to map, reduce the
317 * map size.
318 */
319 if ((fileSizeBytes - packetIndex.getOffsetBytes()) < mapSize) {
320 mapSize = fileSizeBytes - packetIndex.getOffsetBytes();
321 }
322
323 /*
324 * Map the packet.
325 */
326 ByteBuffer bb;
327
328 try {
329 bb = getByteBufferAt(packetOffsetBytes, mapSize);
330 } catch (IOException e) {
331 throw new CTFReaderException(e);
332 }
333 bitBuffer.setByteBuffer(bb);
334 return bb;
335 }
336
337 private void parseTracePacketHeader(StructDefinition tracePacketHeaderDef,
338 BitBuffer bitBuffer) throws CTFReaderException {
339 tracePacketHeaderDef.read(bitBuffer);
340
341 /*
342 * Check the CTF magic number
343 */
344 IntegerDefinition magicDef = (IntegerDefinition) tracePacketHeaderDef
345 .lookupDefinition("magic"); //$NON-NLS-1$
346 if (magicDef != null) {
347 int magic = (int) magicDef.getValue();
348 if (magic != Utils.CTF_MAGIC) {
349 throw new CTFReaderException(
350 "CTF magic mismatch " + Integer.toHexString(magic) + " vs " + Integer.toHexString(Utils.CTF_MAGIC)); //$NON-NLS-1$//$NON-NLS-2$
351 }
352 }
353
354 /*
355 * Check the trace UUID
356 */
357 ArrayDefinition uuidDef =
358 (ArrayDefinition) tracePacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
359 if (uuidDef != null) {
360 byte[] uuidArray = new byte[16];
361
362 for (int i = 0; i < uuidArray.length; i++) {
363 IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i);
364 uuidArray[i] = (byte) uuidByteDef.getValue();
365 }
366
367 UUID uuid = Utils.makeUUID(uuidArray);
368
369 if (!getStream().getTrace().getUUID().equals(uuid)) {
370 throw new CTFReaderException("UUID mismatch"); //$NON-NLS-1$
371 }
372 }
373
374 /*
375 * Check that the stream id did not change
376 */
377 IntegerDefinition streamIDDef = (IntegerDefinition) tracePacketHeaderDef
378 .lookupDefinition("stream_id"); //$NON-NLS-1$
379 if (streamIDDef != null) {
380 long streamID = streamIDDef.getValue();
381
382 if (streamID != getStream().getId()) {
383 throw new CTFReaderException("Stream ID changing within a StreamInput"); //$NON-NLS-1$
384 }
385 }
386 }
387
388 private static void setPacketContextNull(long fileSizeBytes,
389 StreamInputPacketIndexEntry packetIndex) {
390 /*
391 * If there is no packet context, infer the content and packet size from
392 * the file size (assume that there is only one packet and no padding)
393 */
394 packetIndex.setContentSizeBits(fileSizeBytes * 8);
395 packetIndex.setPacketSizeBits(fileSizeBytes * 8);
396 }
397
398 private void parsePacketContext(long fileSizeBytes,
399 StructDefinition streamPacketContextDef, BitBuffer bitBuffer,
400 StreamInputPacketIndexEntry packetIndex) throws CTFReaderException {
401 streamPacketContextDef.read(bitBuffer);
402
403 for (String field : streamPacketContextDef.getDeclaration()
404 .getFieldsList()) {
405 Definition id = streamPacketContextDef.lookupDefinition(field);
406 if (id instanceof IntegerDefinition) {
407 packetIndex.addAttribute(field,
408 ((IntegerDefinition) id).getValue());
409 } else if (id instanceof FloatDefinition) {
410 packetIndex.addAttribute(field,
411 ((FloatDefinition) id).getValue());
412 } else if (id instanceof EnumDefinition) {
413 packetIndex.addAttribute(field,
414 ((EnumDefinition) id).getValue());
415 } else if (id instanceof StringDefinition) {
416 packetIndex.addAttribute(field,
417 ((StringDefinition) id).getValue());
418 }
419 }
420
421 Long contentSize = (Long) packetIndex.lookupAttribute("content_size"); //$NON-NLS-1$
422 Long packetSize = (Long) packetIndex.lookupAttribute("packet_size"); //$NON-NLS-1$
423 Long tsBegin = (Long) packetIndex.lookupAttribute("timestamp_begin"); //$NON-NLS-1$
424 Long tsEnd = (Long) packetIndex.lookupAttribute("timestamp_end"); //$NON-NLS-1$
425 String device = (String) packetIndex.lookupAttribute("device"); //$NON-NLS-1$
426 // LTTng Specific
427 Long cpuId = (Long) packetIndex.lookupAttribute("cpu_id"); //$NON-NLS-1$
428 Long lostEvents = (Long) packetIndex.lookupAttribute("events_discarded"); //$NON-NLS-1$
429
430 /* Read the content size in bits */
431 if (contentSize != null) {
432 packetIndex.setContentSizeBits(contentSize.intValue());
433 } else if (packetSize != null) {
434 packetIndex.setContentSizeBits(packetSize.longValue());
435 } else {
436 packetIndex.setContentSizeBits((int) (fileSizeBytes * 8));
437 }
438
439
440 /* Read the packet size in bits */
441 if (packetSize != null) {
442 packetIndex.setPacketSizeBits(packetSize.intValue());
443 } else if (packetIndex.getContentSizeBits() != 0) {
444 packetIndex.setPacketSizeBits(packetIndex.getContentSizeBits());
445 } else {
446 packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8));
447 }
448
449
450 /* Read the begin timestamp */
451 if (tsBegin != null) {
452 packetIndex.setTimestampBegin(tsBegin.longValue());
453 }
454
455 /* Read the end timestamp */
456 if (tsEnd != null) {
457 if (tsEnd == -1) {
458 tsEnd = Long.MAX_VALUE;
459 }
460 packetIndex.setTimestampEnd(tsEnd.longValue());
461 setTimestampEnd(packetIndex.getTimestampEnd());
462 }
463
464 if (device != null) {
465 packetIndex.setTarget(device);
466 }
467
468 if (cpuId != null) {
469 packetIndex.setTarget("CPU" + cpuId.toString()); //$NON-NLS-1$
470 }
471
472 if (lostEvents != null) {
473 packetIndex.setLostEvents(lostEvents - fLostSoFar);
474 fLostSoFar = lostEvents;
475 }
476 }
477
478 @Override
479 public int hashCode() {
480 final int prime = 31;
481 int result = 1;
482 result = (prime * result) + ((fFile == null) ? 0 : fFile.hashCode());
483 return result;
484 }
485
486 @Override
487 public boolean equals(Object obj) {
488 if (this == obj) {
489 return true;
490 }
491 if (obj == null) {
492 return false;
493 }
494 if (!(obj instanceof StreamInput)) {
495 return false;
496 }
497 StreamInput other = (StreamInput) obj;
498 if (fFile == null) {
499 if (other.fFile != null) {
500 return false;
501 }
502 } else if (!fFile.equals(other.fFile)) {
503 return false;
504 }
505 return true;
506 }
507
508 }
This page took 0.042397 seconds and 4 git commands to generate.