import org.eclipse.linuxtools.ctf.core.event.CTFClock;
import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
+import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
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.IDefinitionScope;
import org.eclipse.linuxtools.internal.ctf.core.event.metadata.exceptions.ParseException;
import org.eclipse.linuxtools.internal.ctf.core.trace.Stream;
import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInput;
+import org.eclipse.linuxtools.internal.ctf.core.trace.StreamInputPacketIndex;
/**
* <b><u>CTFTrace</u></b>
/** Handlers for the metadata files */
private final static FileFilter metadataFileFilter = new MetadataFileFilter();
- private final static Comparator<File> metadataComparator = new MetadataComparator();
+ private final static Comparator<File> metadataComparator = new MetadataComparator(); // $codepro.audit.disable
+ // fieldJavadoc
/** map of all the event types */
- private final HashMap<Long, EventDeclaration> events;
+ private final HashMap<Long,HashMap<Long, EventDeclaration>> eventDecs;
+ /** map of all the event types */
+ private final HashMap<StreamInput,HashMap<Long, EventDefinition>> eventDefs;
+ /** map of all the indexes */
+ private final HashMap<StreamInput, StreamInputPacketIndex> indexes;
+
+
// ------------------------------------------------------------------------
// Constructors
* Trace constructor.
*
* @param path
- * Filesystem path of the trace directory.
- * @throws IOException
+ * Filesystem path of the trace directory
+ * @throws CTFReaderException
+ * If no CTF trace was found at the path
*/
public CTFTrace(String path) throws CTFReaderException {
this(new File(path));
* @param path
* Filesystem path of the trace directory.
* @throws CTFReaderException
+ * If no CTF trace was found at the path
*/
public CTFTrace(File path) throws CTFReaderException {
this.path = path;
environment = new HashMap<String, String>();
clocks = new HashMap<String, CTFClock>();
streamFileChannels = new LinkedList<FileChannel>();
+ eventDecs = new HashMap<Long, HashMap<Long, EventDeclaration>>();
+ eventDefs = new HashMap<StreamInput, HashMap<Long, EventDefinition>>();
if (!this.path.isDirectory()) {
throw new CTFReaderException("Path must be a valid directory"); //$NON-NLS-1$
/* List files not called metadata and not hidden. */
File[] files = path.listFiles(metadataFileFilter);
Arrays.sort(files, metadataComparator);
-
+ indexes = new HashMap<StreamInput, StreamInputPacketIndex>();
/* Try to open each file */
for (File streamFile : files) {
openStreamInput(streamFile);
}
- events = new HashMap<Long, EventDeclaration>();
+
/* Create their index */
for (Map.Entry<Long, Stream> stream : streams.entrySet()) {
Set<StreamInput> inputs = stream.getValue().getStreamInputs();
Map.Entry<Long, EventDeclaration> pairs = it.next();
Long eventNum = pairs.getKey();
EventDeclaration eventDec = pairs.getValue();
- events.put(eventNum, eventDec);
+ getEvents(s.getStream().getId()).put(eventNum, eventDec);
}
/*
* index the trace
*/
- s.createIndex();
+ s.setupIndex();
}
}
}
@Override
- protected void finalize() {
+ protected void finalize() throws Throwable {
/* If this trace gets closed, release the descriptors to the streams */
for (FileChannel fc : streamFileChannels) {
if (fc != null) {
}
}
}
+ super.finalize();
+
}
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
/**
- * Get an event by it's ID
+ * Gets an event declaration hash map for a given streamID
*
- * @param id
- * the ID of the event
- * @return the event declaration
+ * @param streamId
+ * The ID of the stream from which to read
+ * @return The Hash map with the event declarations
*/
- public EventDeclaration getEventType(long id) {
- return events.get(id);
+ public HashMap<Long, EventDeclaration> getEvents(Long streamId) {
+ return eventDecs.get(streamId);
}
/**
- * Get the number of events in the trace so far.
+ * Gets an index for a given StreamInput
+ * @param id the StreamInput
+ * @return The index
+ */
+ public StreamInputPacketIndex getIndex(StreamInput id){
+ if(! indexes.containsKey(id)){
+ indexes.put(id, new StreamInputPacketIndex());
+ }
+ return indexes.get(id);
+ }
+
+ /**
+ * Gets an event Declaration hashmap for a given StreamInput
+ * @param id the StreamInput
+ * @return the hashmap with the event definitions
+ */
+ public HashMap<Long, EventDefinition> getEventDefs(StreamInput id) {
+ if(! eventDefs.containsKey(id)){
+ eventDefs.put(id, new HashMap<Long, EventDefinition>());
+ }
+ return eventDefs.get(id);
+ }
+
+ /**
+ * Get an event by it's ID
*
- * @return the number of events in the trace
+ * @param streamId
+ * The ID of the stream from which to read
+ * @param id
+ * the ID of the event
+ * @return the event declaration
*/
- public int getNbEventTypes() {
- return events.size();
+ public EventDeclaration getEventType(long streamId, long id) {
+ return getEvents(streamId).get(id);
}
/**
/* Check UUID */
ArrayDefinition uuidDef = (ArrayDefinition) packetHeaderDef
.lookupDefinition("uuid"); //$NON-NLS-1$
- assert ((uuidDef != null) && (uuidDef.getDeclaration().getLength() == Utils.UUID_LEN));
if (uuidDef != null) {
byte[] uuidArray = new byte[Utils.UUID_LEN];
*
* @param stream
* A stream object.
- *
* @throws ParseException
+ * If there was some problem reading the metadata
*/
public void addStream(Stream stream) throws ParseException {
}
/*
- * If the stream we try to add has the null key, it must be the only
- * one. Thus, if the streams container is not empty, it is not valid.
+ * If the stream we try to add has the null key, it must be the onl * one. Thus, if the streams container is not empty, it is not valid.
*/
if ((stream.getId() == null) && (streams.size() != 0)) {
throw new ParseException("Stream without id with multiple streams"); //$NON-NLS-1$
/* It should be ok now. */
streams.put(stream.getId(), stream);
+ eventDecs.put(stream.getId(), new HashMap<Long,EventDeclaration>());
}
+ /**
+ * gets the Environment variables from the trace metadata (See CTF spec)
+ * @return the environment variables in a hashmap form (key value)
+ */
public HashMap<String, String> getEnvironment() {
return environment;
}
+ /**
+ * Look up a specific environment variable
+ * @param key the key to look for
+ * @return the value of the variable, can be null.
+ */
public String lookupEnvironment(String key) {
return environment.get(key);
}
+ /**
+ * Add a variable to the environment variables
+ * @param varName the name of the variable
+ * @param varValue the value of the variable
+ */
public void addEnvironmentVar(String varName, String varValue) {
environment.put(varName, varValue);
}
+ /**
+ * Add a clock to the clock list
+ * @param nameValue the name of the clock (full name with scope)
+ * @param ctfClock the clock
+ */
public void addClock(String nameValue, CTFClock ctfClock) {
clocks.put(nameValue, ctfClock);
}
+ /**
+ * gets the clock with a specific name
+ * @param name the name of the clock.
+ * @return the clock
+ */
public CTFClock getClock(String name) {
return clocks.get(name);
}
- public CTFClock getClock() {
+ private CTFClock singleClock;
+ private long singleOffset;
+
+ /**
+ * gets the clock if there is only one. (this is 100% of the use cases as of June 2012)
+ * @return the clock
+ */
+ public final CTFClock getClock() {
if (clocks.size() == 1) {
- String key = (String) clocks.keySet().toArray()[0];
- return clocks.get(key);
+ if (singleClock == null) {
+ singleClock = clocks.get(clocks.keySet().toArray()[0]);
+ singleOffset = (Long) getClock().getProperty("offset"); //$NON-NLS-1$
+ }
+ return singleClock;
}
return null;
}
- public long getOffset() {
+ /**
+ * gets the time offset of a clock with respect to UTC in nanoseconds
+ * @return the time offset of a clock with respect to UTC in nanoseconds
+ */
+ public final long getOffset() {
if (getClock() == null) {
return 0;
}
- return (Long) getClock().getProperty("offset"); //$NON-NLS-1$
+ return singleOffset;
+ }
+
+ /**
+ * Does a given stream contain any events?
+ * @param id the stream ID
+ * @return true if the stream has events.
+ */
+ public boolean hasEvents(Long id){
+ return eventDecs.containsKey(id);
+ }
+
+ /**
+ * Add an event declaration map to the events map.
+ * @param id the id of a stream
+ * @return the hashmap containing events.
+ */
+ public HashMap<Long, EventDeclaration> createEvents(Long id){
+ HashMap<Long, EventDeclaration> value = eventDecs.get(id);
+ if( value == null ) {
+ value = new HashMap<Long, EventDeclaration>();
+ eventDecs.put(id, value);
+ }
+ return value;
}
}
class MetadataComparator implements Comparator<File>, Serializable {
+ private static final long serialVersionUID = 1L;
+
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());