import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.linuxtools.ctf.core.event.io.BitBuffer;
import org.eclipse.linuxtools.ctf.core.event.scope.IDefinitionScope;
import org.eclipse.linuxtools.ctf.core.event.scope.LexicalScope;
-import org.eclipse.linuxtools.ctf.core.event.types.ArrayDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
import org.eclipse.linuxtools.ctf.core.event.types.StructDeclaration;
import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
import org.eclipse.linuxtools.internal.ctf.core.event.CTFCallsiteComparator;
import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
+import org.eclipse.linuxtools.internal.ctf.core.event.types.ArrayDefinition;
/**
* A CTF trace on the file system.
/**
* The clock of the trace
*/
- private CTFClock fSingleClock;
+ private CTFClock fSingleClock = null;
/**
* Packet header structure definition
*/
private final Map<String, CTFClock> fClocks = new HashMap<>();
- /** FileInputStreams to the streams */
- private final List<FileInputStream> fFileInputStreams = new LinkedList<>();
-
/** Handlers for the metadata files */
private static final FileFilter METADATA_FILE_FILTER = new MetadataFileFilter();
private static final Comparator<File> METADATA_COMPARATOR = new MetadataComparator();
/**
* Dispose the trace
*
+ * FIXME Not needed anymore, class doesn't need to be AutoCloseable.
+ *
* @since 3.0
*/
@Override
public void close() {
- for (FileInputStream fis : fFileInputStreams) {
- if (fis != null) {
- try {
- fis.close();
- } catch (IOException e) {
- // do nothing it's ok, we tried to close it.
- }
- }
- }
}
// ------------------------------------------------------------------------
return (fPath != null) ? fPath.getPath() : ""; //$NON-NLS-1$
}
- /**
- * @since 3.0
- */
- @Override
- public LexicalScope getScopePath() {
- return LexicalScope.TRACE;
- }
// ------------------------------------------------------------------------
// Operations
MappedByteBuffer byteBuffer;
BitBuffer streamBitBuffer;
CTFStream stream;
- FileChannel fc;
if (!streamFile.canRead()) {
throw new CTFReaderException("Unreadable file : " //$NON-NLS-1$
+ streamFile.getPath());
}
- FileInputStream fis = null;
- try {
- /* Open the file and get the FileChannel */
- fis = new FileInputStream(streamFile);
- fFileInputStreams.add(fis);
- fc = fis.getChannel();
-
+ try (FileInputStream fis = new FileInputStream(streamFile);
+ FileChannel fc = fis.getChannel()) {
/* Map one memory page of 4 kiB */
byteBuffer = fc.map(MapMode.READ_ONLY, 0, (int) Math.min(fc.size(), 4096L));
- } catch (IOException e) {
- if (fis != null) {
- fFileInputStreams.remove(fis);
+ if( byteBuffer == null){
+ throw new IllegalStateException("Failed to allocate memory"); //$NON-NLS-1$
}
+ } catch (IOException e) {
/* Shouldn't happen at this stage if every other check passed */
throw new CTFReaderException(e);
}
if (fPacketHeaderDecl != null) {
/* Read the packet header */
- fPacketHeaderDef = fPacketHeaderDecl.createDefinition(null, LexicalScope.PACKET_HEADER.getName(), streamBitBuffer);
+ fPacketHeaderDef = fPacketHeaderDecl.createDefinition(this, LexicalScope.PACKET_HEADER, streamBitBuffer);
/* Check the magic number */
IntegerDefinition magicDef = (IntegerDefinition) fPacketHeaderDef.lookupDefinition("magic"); //$NON-NLS-1$
}
/* Check UUID */
- Definition lookupDefinition = fPacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
+ IDefinition lookupDefinition = fPacketHeaderDef.lookupDefinition("uuid"); //$NON-NLS-1$
ArrayDefinition uuidDef = (ArrayDefinition) lookupDefinition;
if (uuidDef != null) {
UUID otheruuid = Utils.getUUIDfromDefinition(uuidDef);
}
/* Read the stream ID */
- Definition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
+ IDefinition streamIDDef = fPacketHeaderDef.lookupDefinition("stream_id"); //$NON-NLS-1$
if (streamIDDef instanceof IntegerDefinition) { // this doubles as a
// null check
return stream;
}
+ // ------------------------------------------------------------------------
+ // IDefinitionScope
+ // ------------------------------------------------------------------------
+
+ /**
+ * @since 3.0
+ */
+ @Override
+ public LexicalScope getScopePath() {
+ return LexicalScope.TRACE;
+ }
+
/**
* Looks up a definition from packet
*
*/
@Override
public Definition lookupDefinition(String lookupPath) {
- if (lookupPath.equals("trace.packet.header")) { //$NON-NLS-1$
+ if (lookupPath.equals(LexicalScope.TRACE_PACKET_HEADER.toString())) {
return fPacketHeaderDef;
}
return null;
}
+ // ------------------------------------------------------------------------
+ // Live trace reading
+ // ------------------------------------------------------------------------
+
/**
* Add a new stream file to support new streams while the trace is being
* read.
* @return the clock
*/
public final CTFClock getClock() {
+ if (fSingleClock != null && fClocks.size() == 1) {
+ return fSingleClock;
+ }
if (fClocks.size() == 1) {
fSingleClock = fClocks.get(fClocks.keySet().iterator().next());
return fSingleClock;
*/
public CTFCallsite getCallsite(String eventName, long ip) {
final TreeSet<CTFCallsite> candidates = fCallsitesByName.get(eventName);
+ if (candidates == null) {
+ return null;
+ }
final CTFCallsite dummyCs = new CTFCallsite(null, null, ip, null, -1);
final CTFCallsite callsite = candidates.ceiling(dummyCs);
if (callsite == null) {