ctf/tmf: allow multiple traces to be open with name clashing events
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEventFactory.java
index 6e3dbb463a125b23e5df79cf2c83ce9c3bea4879..a7832ca99616d4c5e1bbfcd8223b86e6bfa8cbd4 100644 (file)
@@ -16,11 +16,15 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
 
+import org.eclipse.linuxtools.ctf.core.CTFStrings;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
+import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
+import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
+import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
 
 /**
@@ -54,8 +58,10 @@ public final class CtfTmfEventFactory {
             String fileName, CtfTmfTrace originTrace) {
 
         /* Prepare what to pass to CtfTmfEvent's constructor */
-        long ts = eventDef.getTimestamp();
-        CtfTmfTimestamp timestamp = originTrace.createTimestamp(originTrace.getCTFTrace().timestampCyclesToNanos(ts));
+        final IEventDeclaration eventDecl = eventDef.getDeclaration();
+        final long ts = eventDef.getTimestamp();
+        final CtfTmfTimestamp timestamp = originTrace.createTimestamp(
+                originTrace.getCTFTrace().timestampCyclesToNanos(ts));
 
         int sourceCPU = eventDef.getCPU();
 
@@ -64,16 +70,43 @@ public final class CtfTmfEventFactory {
 
         String reference = fileName == null ? CtfTmfEvent.NO_STREAM : fileName;
 
-        /* Construct and return the object */
+        /* Handle the special case of lost events */
+        if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
+            Definition nbLostEventsDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_FIELD);
+            Definition durationDef = eventDef.getFields().getDefinitions().get(CTFStrings.LOST_EVENTS_DURATION);
+            if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) {
+                /*
+                 * One or both of these fields doesn't exist, or is not of the
+                 * right type. The event claims to be a "lost event", but is
+                 * malformed. Log it and return a null event instead.
+                 */
+                return getNullEvent();
+            }
+            long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue();
+            long duration = ((IntegerDefinition) durationDef).getValue();
+            CtfTmfTimestamp timestampEnd = new CtfTmfTimestamp(
+                    originTrace.getCTFTrace().timestampCyclesToNanos(ts) + duration);
+
+            CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(originTrace,
+                    ITmfContext.UNKNOWN_RANK,
+                    content,
+                    reference, // filename
+                    sourceCPU,
+                    eventDecl,
+                    new TmfTimeRange(timestamp, timestampEnd),
+                    nbLostEvents);
+            return lostEvent;
+        }
+
+        /* Handle standard event types */
         CtfTmfEvent event = new CtfTmfEvent(
                 originTrace,
                 ITmfContext.UNKNOWN_RANK,
                 timestamp,
                 content,
-                reference,
+                reference, // filename
                 sourceCPU,
-                eventDef.getDeclaration()
-        );
+                eventDecl);
         return event;
     }
 
@@ -97,7 +130,7 @@ public final class CtfTmfEventFactory {
      * mess, and put them into something ITmfEventField can cope with.
      */
     private static CtfTmfEventField[] parseFields(EventDefinition eventDef) {
-        List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
+        List<CtfTmfEventField> fields = new ArrayList<>();
 
         StructDefinition structFields = eventDef.getFields();
         for (Map.Entry<String, Definition> entry : structFields.getDefinitions().entrySet()) {
This page took 0.025157 seconds and 5 git commands to generate.