tmf: Do not define base aspects in the interface
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / trace / CtfTmfTrace.java
index e2ba2258253e2a84ece5c387e0acbfcc7ce98a3e..52fb5e7b520dd2d214b06bfead0ba0c5f58300e2 100644 (file)
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -32,11 +33,10 @@ import org.eclipse.core.runtime.CoreException;
 import org.eclipse.core.runtime.IStatus;
 import org.eclipse.core.runtime.Status;
 import org.eclipse.jdt.annotation.NonNull;
-import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.tracecompass.ctf.core.CTFException;
-import org.eclipse.tracecompass.ctf.core.event.CTFCallsite;
 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
+import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
 import org.eclipse.tracecompass.ctf.core.trace.Metadata;
@@ -46,13 +46,13 @@ import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator
 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
+import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
+import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
-import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
-import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
@@ -67,10 +67,10 @@ import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext;
 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
+import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventFactory;
 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
-import org.eclipse.tracecompass.tmf.ctf.core.event.lookup.CtfTmfCallsite;
 
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableSet;
@@ -82,13 +82,19 @@ import com.google.common.collect.ImmutableSet;
  * @author Matthew khouzam
  */
 public class CtfTmfTrace extends TmfTrace
-        implements ITmfTraceProperties, ITmfPersistentlyIndexable,
-        ITmfTraceWithPreDefinedEvents, AutoCloseable {
+        implements ITmfPropertiesProvider, ITmfPersistentlyIndexable,
+        ITmfTraceWithPreDefinedEvents {
 
     // -------------------------------------------
     // Constants
     // -------------------------------------------
 
+    /**
+     * Clock offset property
+     * @since 2.0
+     */
+    public static final String CLOCK_OFFSET = "clock_offset"; //$NON-NLS-1$
+
     /**
      * Default cache size for CTF traces
      */
@@ -98,14 +104,14 @@ public class CtfTmfTrace extends TmfTrace
      * Event aspects available for all CTF traces
      * @since 1.0
      */
-    protected static final @NonNull Collection<ITmfEventAspect> CTF_ASPECTS =
-            checkNotNull(ImmutableList.of(
-                    ITmfEventAspect.BaseAspects.TIMESTAMP,
+    protected static final @NonNull Collection<@NonNull ITmfEventAspect<?>> CTF_ASPECTS =
+            ImmutableList.of(
+                    TmfBaseAspects.getTimestampAspect(),
                     new CtfChannelAspect(),
                     new CtfCpuAspect(),
-                    ITmfEventAspect.BaseAspects.EVENT_TYPE,
-                    ITmfEventAspect.BaseAspects.CONTENTS
-                    ));
+                    TmfBaseAspects.getEventTypeAspect(),
+                    TmfBaseAspects.getContentsAspect()
+                    );
 
     /**
      * The Ctf clock unique identifier field
@@ -118,15 +124,42 @@ public class CtfTmfTrace extends TmfTrace
     // Fields
     // -------------------------------------------
 
-    private final Map<String, CtfTmfEventType> fContainedEventTypes =
-            Collections.synchronizedMap(new HashMap<String, CtfTmfEventType>());
+    private final Map<@NonNull String, @NonNull CtfTmfEventType> fContainedEventTypes =
+            Collections.synchronizedMap(new HashMap<>());
 
-    private final CtfIteratorManager fIteratorManager =
-            new CtfIteratorManager(this);
+    private final CtfIteratorManager fIteratorManager = new CtfIteratorManager(this);
 
-    /* Reference to the CTF Trace */
+    private final @NonNull CtfTmfEventFactory fEventFactory;
+
+    /** Reference to the CTF Trace */
     private CTFTrace fTrace;
 
+    // -------------------------------------------
+    // Constructor
+    // -------------------------------------------
+
+    /**
+     * Default constructor
+     */
+    public CtfTmfTrace() {
+        super();
+
+        /* Use default event factory */
+        fEventFactory = CtfTmfEventFactory.instance();
+    }
+
+    /**
+     * Constructor for sub-classes to specify their own event factory.
+     *
+     * @param eventFactory
+     *            The event factory to use to generate trace events
+     * @since 2.0
+     */
+    protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory) {
+        super();
+        fEventFactory = eventFactory;
+    }
+
     // -------------------------------------------
     // TmfTrace Overrides
     // -------------------------------------------
@@ -172,25 +205,44 @@ public class CtfTmfTrace extends TmfTrace
              * register a trace to that type in the TmfEventTypeManager
              */
             try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
+                Set<@NonNull ITmfEventField> streamContextNames = new HashSet<>();
                 for (IEventDeclaration ied : iter.getEventDeclarations()) {
                     CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
                     if (ctfTmfEventType == null) {
                         List<ITmfEventField> content = new ArrayList<>();
+
                         /* Should only return null the first time */
-                        for (String fieldName : ied.getFields().getFieldsList()) {
-                            content.add(new TmfEventField(fieldName, null, null));
+                        final StructDeclaration fields = ied.getFields();
+                        if (fields != null) {
+                            for (String fieldName : fields.getFieldsList()) {
+                                content.add(new TmfEventField(checkNotNull(fieldName), null, null));
+                            }
+                        }
+
+                        /* Add stream contexts */
+                        final StructDeclaration streamContexts = ied.getStream().getEventContextDecl();
+                        if (streamContextNames.isEmpty()) {
+                            if (streamContexts != null) {
+                                for (String fieldName : streamContexts.getFieldsList()) {
+                                    streamContextNames.add(new TmfEventField(checkNotNull(CtfConstants.CONTEXT_FIELD_PREFIX + fieldName), null, null));
+                                }
+                            }
+                        }
+                        content.addAll(streamContextNames);
+
+                        if (!content.isEmpty()) {
+                            ITmfEventField contentTree = new TmfEventField(
+                                    ITmfEventField.ROOT_FIELD_ID,
+                                    null,
+                                    content.toArray(new ITmfEventField[content.size()]));
+
+                            ctfTmfEventType = new CtfTmfEventType(checkNotNull(ied.getName()), contentTree);
+                            fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
                         }
-                        ITmfEventField contentTree = new TmfEventField(
-                                ITmfEventField.ROOT_FIELD_ID,
-                                null,
-                                content.toArray(new ITmfEventField[content.size()])
-                                );
-
-                        ctfTmfEventType = new CtfTmfEventType(ied.getName(), contentTree);
-                        fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
                     }
                 }
             }
+            ctx.dispose();
         } catch (final CTFException e) {
             /*
              * If it failed at the init(), we can assume it's because the file
@@ -201,11 +253,6 @@ public class CtfTmfTrace extends TmfTrace
         }
     }
 
-    @Override
-    public void close() {
-        dispose();
-    }
-
     @Override
     public synchronized void dispose() {
         fIteratorManager.dispose();
@@ -267,7 +314,7 @@ public class CtfTmfTrace extends TmfTrace
     }
 
     @Override
-    public Iterable<ITmfEventAspect> getEventAspects() {
+    public Iterable<ITmfEventAspect<?>> getEventAspects() {
         return CTF_ASPECTS;
     }
 
@@ -317,9 +364,6 @@ public class CtfTmfTrace extends TmfTrace
             context.setRank(0);
         } else {
             context.setRank(ITmfContext.UNKNOWN_RANK);
-            if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
-                currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
-            }
         }
         /* This will seek and update the location after the seek */
         context.setLocation(currentLocation);
@@ -365,7 +409,7 @@ public class CtfTmfTrace extends TmfTrace
             event = ctfContext.getCurrentEvent();
 
             if (event != null) {
-                updateAttributes(context, event.getTimestamp());
+                updateAttributes(context, event);
                 ctfContext.advance();
                 ctfContext.increaseRank();
             }
@@ -393,38 +437,6 @@ public class CtfTmfTrace extends TmfTrace
         return super.getHostId();
     }
 
-    /**
-     * Get the first callsite that matches the event name
-     *
-     * @param eventName The event name to look for
-     * @return The best callsite candidate
-     */
-    public @Nullable CtfTmfCallsite getCallsite(String eventName) {
-        CTFCallsite callsite = fTrace.getCallsite(eventName);
-        if (callsite != null) {
-            return new CtfTmfCallsite(callsite);
-        }
-        return null;
-    }
-
-    /**
-     * Get the closest matching callsite for given event name and instruction
-     * pointer
-     *
-     * @param eventName
-     *            The event name
-     * @param ip
-     *            The instruction pointer
-     * @return The closest matching callsite
-     */
-    public @Nullable CtfTmfCallsite getCallsite(String eventName, long ip) {
-        CTFCallsite calliste = fTrace.getCallsite(eventName, ip);
-        if (calliste != null) {
-            return new CtfTmfCallsite(calliste);
-        }
-        return null;
-    }
-
     /**
      * Get the CTF environment variables defined in this CTF trace, in <name,
      * value> form. This comes from the trace's CTF metadata.
@@ -436,13 +448,17 @@ public class CtfTmfTrace extends TmfTrace
     }
 
     // -------------------------------------------
-    // ITmfTraceProperties
+    // ITmfPropertiesProvider
     // -------------------------------------------
 
+    /**
+     * @since 2.0
+     */
     @Override
-    public Map<String, String> getTraceProperties() {
+    public Map<String, String> getProperties() {
         Map<String, String> properties = new HashMap<>();
         properties.putAll(fTrace.getEnvironment());
+        properties.put(CLOCK_OFFSET, Long.toUnsignedString(fTrace.getOffset()));
         properties.put(Messages.CtfTmfTrace_HostID, getHostId());
         return properties;
     }
@@ -491,7 +507,7 @@ public class CtfTmfTrace extends TmfTrace
      * Gets the list of declared events
      */
     @Override
-    public Set<CtfTmfEventType> getContainedEventTypes() {
+    public Set<@NonNull CtfTmfEventType> getContainedEventTypes() {
         return ImmutableSet.copyOf(fContainedEventTypes.values());
     }
 
@@ -534,6 +550,16 @@ public class CtfTmfTrace extends TmfTrace
     // CtfIterator factory methods
     // -------------------------------------------
 
+    /**
+     * Get the event factory for this trace to generate new events for it.
+     *
+     * @return The event factory
+     * @since 2.0
+     */
+    public @NonNull CtfTmfEventFactory getEventFactory() {
+        return fEventFactory;
+    }
+
     /**
      * Get an iterator to the trace
      *
@@ -600,8 +626,8 @@ public class CtfTmfTrace extends TmfTrace
      * @since 1.0
      */
     @Override
-    public @NonNull TmfNanoTimestamp createTimestamp(long ts) {
-        return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
+    public @NonNull ITmfTimestamp createTimestamp(long ts) {
+        return TmfTimestamp.fromNanos(getTimestampTransform().transform(ts));
     }
 
     private static int fCheckpointSize = -1;
@@ -609,7 +635,7 @@ public class CtfTmfTrace extends TmfTrace
     @Override
     public synchronized int getCheckpointSize() {
         if (fCheckpointSize == -1) {
-            TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
+            TmfCheckpoint c = new TmfCheckpoint(TmfTimestamp.fromNanos(0), new CtfLocation(0, 0), 0);
             ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
             b.clear();
             c.serialize(b);
This page took 0.028127 seconds and 5 git commands to generate.