X-Git-Url: http://drtracing.org/?a=blobdiff_plain;f=org.eclipse.linuxtools.ctf.core%2Fsrc%2Forg%2Feclipse%2Flinuxtools%2Fctf%2Fcore%2Ftrace%2FStreamInput.java;h=02d0822ad5e302f60b376ef62bfe236fecdaff16;hb=60ae41e177bc783d3a400742bb4728b94dcf5c97;hp=6e15940ddbca0cb81450e1c0e0b8a29fe1185116;hpb=486efb2e13e1bea489a6196734e61d8ade1fc81b;p=deliverable%2Ftracecompass.git diff --git a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java index 6e15940ddb..02d0822ad5 100644 --- a/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java +++ b/org.eclipse.linuxtools.ctf.core/src/org/eclipse/linuxtools/ctf/core/trace/StreamInput.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2011-2012 Ericsson, Ecole Polytechnique de Montreal and others + * Copyright (c) 2011, 2014 Ericsson, Ecole Polytechnique de Montreal and others * * All rights reserved. This program and the accompanying materials are made * available under the terms of the Eclipse Public License v1.0 which @@ -14,7 +14,7 @@ package org.eclipse.linuxtools.ctf.core.trace; import java.io.File; import java.io.IOException; -import java.nio.MappedByteBuffer; +import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.FileChannel.MapMode; import java.util.UUID; @@ -35,6 +35,7 @@ import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndexEntr * StreamInput *

* Represents a trace file that belongs to a certain stream. + * * @since 2.0 */ public class StreamInput implements IDefinitionScope { @@ -46,39 +47,39 @@ public class StreamInput implements IDefinitionScope { /** * The associated Stream */ - private final Stream stream; + private final Stream fStream; /** * FileChannel to the trace file */ - private final FileChannel fileChannel; + private final FileChannel fFileChannel; /** * Information on the file (used for debugging) */ - public final File file; + private final File fFile; /** * The packet index of this input */ - private final StreamInputPacketIndex index; + private final StreamInputPacketIndex fIndex; - private long timestampEnd; + private long fTimestampEnd; /* * Definition of trace packet header */ - StructDefinition tracePacketHeaderDef = null; + private StructDefinition fTracePacketHeaderDef = null; /* * Definition of trace stream packet context */ - StructDefinition streamPacketContextDef = null; + private StructDefinition fStreamPacketContextDef = null; /* * Total number of lost events in this stream */ - long lostSoFar = 0; + private long fLostSoFar = 0; // ------------------------------------------------------------------------ // Constructors @@ -95,10 +96,10 @@ public class StreamInput implements IDefinitionScope { * Information about the trace file (for debugging purposes). */ public StreamInput(Stream stream, FileChannel fileChannel, File file) { - this.stream = stream; - this.fileChannel = fileChannel; - this.file = file; - this.index = stream.getTrace().getIndex(this); + fStream = stream; + fFileChannel = fileChannel; + fFile = file; + fIndex = new StreamInputPacketIndex(); } // ------------------------------------------------------------------------ @@ -111,26 +112,16 @@ public class StreamInput implements IDefinitionScope { * @return the stream the streamInput wrapper is wrapping */ public Stream getStream() { - return stream; + return fStream; } /** - * the common streamInput Index + * The common streamInput Index * * @return the stream input Index */ StreamInputPacketIndex getIndex() { - return index; - } - - /** - * Gets the filechannel of the streamInput. This is a limited Java - * ressource. - * - * @return the filechannel - */ - public FileChannel getFileChannel() { - return fileChannel; + return fIndex; } /** @@ -139,17 +130,17 @@ public class StreamInput implements IDefinitionScope { * @return the filename of the streaminput file. */ public String getFilename() { - return file.getName(); + return fFile.getName(); } /** - * gets the last read timestamp of a stream. (this is not necessarily the + * Gets the last read timestamp of a stream. (this is not necessarily the * last time in the stream.) * * @return the last read timestamp */ public long getTimestampEnd() { - return timestampEnd; + return fTimestampEnd; } /** @@ -160,11 +151,11 @@ public class StreamInput implements IDefinitionScope { * the last read timestamp */ public void setTimestampEnd(long timestampEnd) { - this.timestampEnd = timestampEnd; + fTimestampEnd = timestampEnd; } /** - * useless for streaminputs + * Useless for streaminputs */ @Override public String getPath() { @@ -190,18 +181,18 @@ public class StreamInput implements IDefinitionScope { * The BitBuffer to extract data from the StreamInput */ BitBuffer bitBuffer = new BitBuffer(); - bitBuffer.setByteOrder(this.getStream().getTrace().getByteOrder()); + bitBuffer.setByteOrder(getStream().getTrace().getByteOrder()); /* * Create the definitions we need to read the packet headers + contexts */ if (getStream().getTrace().getPacketHeader() != null) { - tracePacketHeaderDef = getStream().getTrace().getPacketHeader() + fTracePacketHeaderDef = getStream().getTrace().getPacketHeader() .createDefinition(this, "trace.packet.header"); //$NON-NLS-1$ } if (getStream().getPacketContextDecl() != null) { - streamPacketContextDef = getStream().getPacketContextDecl() + fStreamPacketContextDef = getStream().getPacketContextDecl() .createDefinition(this, "stream.packet.context"); //$NON-NLS-1$ } @@ -217,26 +208,26 @@ public class StreamInput implements IDefinitionScope { */ public boolean addPacketHeaderIndex() throws CTFReaderException { long currentPos = 0L; - if (!index.getEntries().isEmpty()) { - StreamInputPacketIndexEntry pos = index.getEntries().lastElement(); + if (!fIndex.getEntries().isEmpty()) { + StreamInputPacketIndexEntry pos = fIndex.getEntries().lastElement(); currentPos = computeNextOffset(pos); } long fileSize = getStreamSize(); if (currentPos < fileSize) { BitBuffer bitBuffer = new BitBuffer(); - bitBuffer.setByteOrder(this.getStream().getTrace().getByteOrder()); + bitBuffer.setByteOrder(getStream().getTrace().getByteOrder()); StreamInputPacketIndexEntry packetIndex = new StreamInputPacketIndexEntry( currentPos); createPacketIndexEntry(fileSize, currentPos, packetIndex, - tracePacketHeaderDef, streamPacketContextDef, bitBuffer); - index.addEntry(packetIndex); + fTracePacketHeaderDef, fStreamPacketContextDef, bitBuffer); + fIndex.addEntry(packetIndex); return true; } return false; } private long getStreamSize() { - return file.length(); + return fFile.length(); } private long createPacketIndexEntry(long fileSizeBytes, @@ -244,9 +235,12 @@ public class StreamInput implements IDefinitionScope { StructDefinition tracePacketHeaderDef, StructDefinition streamPacketContextDef, BitBuffer bitBuffer) throws CTFReaderException { - @SuppressWarnings("unused") - MappedByteBuffer bb = createPacketBitBuffer(fileSizeBytes, - packetOffsetBytes, packetIndex, bitBuffer); + + /* + * Ignoring the return value, but this call is needed to initialize the + * input + */ + createPacketBitBuffer(fileSizeBytes, packetOffsetBytes, packetIndex, bitBuffer); /* * Read the trace packet header if it exists. @@ -272,8 +266,7 @@ public class StreamInput implements IDefinitionScope { if (packetIndex.getPacketSizeBits() > ((fileSizeBytes - packetIndex .getOffsetBytes()) * 8)) { - throw new CTFReaderException( - "Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$ + throw new CTFReaderException("Not enough data remaining in the file for the size of this packet"); //$NON-NLS-1$ } /* @@ -297,6 +290,10 @@ public class StreamInput implements IDefinitionScope { + ((packetIndex.getPacketSizeBits() + 7) / 8); } + ByteBuffer getByteBufferAt(long position, long size) throws IOException { + return fFileChannel.map(MapMode.READ_ONLY, position, size); + } + /** * @param fileSizeBytes * @param packetOffsetBytes @@ -305,7 +302,7 @@ public class StreamInput implements IDefinitionScope { * @return * @throws CTFReaderException */ - private MappedByteBuffer createPacketBitBuffer(long fileSizeBytes, + private ByteBuffer createPacketBitBuffer(long fileSizeBytes, long packetOffsetBytes, StreamInputPacketIndexEntry packetIndex, BitBuffer bitBuffer) throws CTFReaderException { /* @@ -326,10 +323,10 @@ public class StreamInput implements IDefinitionScope { /* * Map the packet. */ - MappedByteBuffer bb; + ByteBuffer bb; try { - bb = fileChannel.map(MapMode.READ_ONLY, packetOffsetBytes, mapSize); + bb = getByteBufferAt(packetOffsetBytes, mapSize); } catch (IOException e) { throw new CTFReaderException(e); } @@ -352,20 +349,18 @@ public class StreamInput implements IDefinitionScope { throw new CTFReaderException( "CTF magic mismatch " + Integer.toHexString(magic) + " vs " + Integer.toHexString(Utils.CTF_MAGIC)); //$NON-NLS-1$//$NON-NLS-2$ } - } /* * Check the trace UUID */ - ArrayDefinition uuidDef = (ArrayDefinition) tracePacketHeaderDef - .lookupDefinition("uuid"); //$NON-NLS-1$ + ArrayDefinition uuidDef = + (ArrayDefinition) tracePacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$ if (uuidDef != null) { byte[] uuidArray = new byte[16]; - for (int i = 0; i < 16; i++) { - IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef - .getElem(i); + for (int i = 0; i < uuidArray.length; i++) { + IntegerDefinition uuidByteDef = (IntegerDefinition) uuidDef.getElem(i); uuidArray[i] = (byte) uuidByteDef.getValue(); } @@ -385,8 +380,7 @@ public class StreamInput implements IDefinitionScope { long streamID = streamIDDef.getValue(); if (streamID != getStream().getId()) { - throw new CTFReaderException( - "Stream ID changing within a StreamInput"); //$NON-NLS-1$ + throw new CTFReaderException("Stream ID changing within a StreamInput"); //$NON-NLS-1$ } } } @@ -397,13 +391,13 @@ public class StreamInput implements IDefinitionScope { * If there is no packet context, infer the content and packet size from * the file size (assume that there is only one packet and no padding) */ - packetIndex.setContentSizeBits((int) (fileSizeBytes * 8)); - packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8)); + packetIndex.setContentSizeBits(fileSizeBytes * 8); + packetIndex.setPacketSizeBits(fileSizeBytes * 8); } private void parsePacketContext(long fileSizeBytes, StructDefinition streamPacketContextDef, BitBuffer bitBuffer, - StreamInputPacketIndexEntry packetIndex) { + StreamInputPacketIndexEntry packetIndex) throws CTFReaderException { streamPacketContextDef.read(bitBuffer); for (String field : streamPacketContextDef.getDeclaration() @@ -426,42 +420,44 @@ public class StreamInput implements IDefinitionScope { Long contentSize = (Long) packetIndex.lookupAttribute("content_size"); //$NON-NLS-1$ Long packetSize = (Long) packetIndex.lookupAttribute("packet_size"); //$NON-NLS-1$ - Long timestampBegin = (Long) packetIndex.lookupAttribute("timestamp_begin"); //$NON-NLS-1$ - Long timestampEnd = (Long) packetIndex.lookupAttribute("timestamp_end"); //$NON-NLS-1$ + Long tsBegin = (Long) packetIndex.lookupAttribute("timestamp_begin"); //$NON-NLS-1$ + Long tsEnd = (Long) packetIndex.lookupAttribute("timestamp_end"); //$NON-NLS-1$ String device = (String) packetIndex.lookupAttribute("device"); //$NON-NLS-1$ // LTTng Specific - Long CPU_ID = (Long) packetIndex.lookupAttribute("cpu_id"); //$NON-NLS-1$ - Long lostEvents = (Long) packetIndex.lookupAttribute("events_discarded"); //$NON-NLS-1$ + Long cpuId = (Long) packetIndex.lookupAttribute("cpu_id"); //$NON-NLS-1$ + Long lostEvents = (Long) packetIndex.lookupAttribute("events_discarded"); //$NON-NLS-1$ /* Read the content size in bits */ if (contentSize != null) { packetIndex.setContentSizeBits(contentSize.intValue()); + } else if (packetSize != null) { + packetIndex.setContentSizeBits(packetSize.longValue()); } else { packetIndex.setContentSizeBits((int) (fileSizeBytes * 8)); } + /* Read the packet size in bits */ if (packetSize != null) { packetIndex.setPacketSizeBits(packetSize.intValue()); + } else if (packetIndex.getContentSizeBits() != 0) { + packetIndex.setPacketSizeBits(packetIndex.getContentSizeBits()); } else { - if (packetIndex.getContentSizeBits() != 0) { - packetIndex.setPacketSizeBits(packetIndex.getContentSizeBits()); - } else { - packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8)); - } + packetIndex.setPacketSizeBits((int) (fileSizeBytes * 8)); } + /* Read the begin timestamp */ - if (timestampBegin != null) { - packetIndex.setTimestampBegin(timestampBegin.longValue()); + if (tsBegin != null) { + packetIndex.setTimestampBegin(tsBegin.longValue()); } /* Read the end timestamp */ - if (timestampEnd != null) { - if( timestampEnd == -1 ) { - timestampEnd = Long.MAX_VALUE; + if (tsEnd != null) { + if (tsEnd == -1) { + tsEnd = Long.MAX_VALUE; } - packetIndex.setTimestampEnd(timestampEnd.longValue()); + packetIndex.setTimestampEnd(tsEnd.longValue()); setTimestampEnd(packetIndex.getTimestampEnd()); } @@ -469,34 +465,24 @@ public class StreamInput implements IDefinitionScope { packetIndex.setTarget(device); } - if (CPU_ID != null) { - packetIndex.setTarget("CPU" + CPU_ID.toString()); //$NON-NLS-1$ + if (cpuId != null) { + packetIndex.setTarget("CPU" + cpuId.toString()); //$NON-NLS-1$ } if (lostEvents != null) { - packetIndex.setLostEvents(lostEvents - lostSoFar); - this.lostSoFar = lostEvents; + packetIndex.setLostEvents(lostEvents - fLostSoFar); + fLostSoFar = lostEvents; } } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ @Override public int hashCode() { final int prime = 31; int result = 1; - result = (prime * result) + ((file == null) ? 0 : file.hashCode()); + result = (prime * result) + ((fFile == null) ? 0 : fFile.hashCode()); return result; } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ @Override public boolean equals(Object obj) { if (this == obj) { @@ -509,11 +495,11 @@ public class StreamInput implements IDefinitionScope { return false; } StreamInput other = (StreamInput) obj; - if (file == null) { - if (other.file != null) { + if (fFile == null) { + if (other.fFile != null) { return false; } - } else if (!file.equals(other.file)) { + } else if (!fFile.equals(other.fFile)) { return false; } return true;