Fix ranks in CtfTmfTrace as part of bug #389051
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
index 1534b4bc57b4932f5ecbc828d9cd5d611ace63a5..be3f94d45792899f06e4a2e407a6d41bccbd3b9c 100644 (file)
@@ -9,68 +9,49 @@
  * Contributors: Matthew Khouzam - Initial API and implementation
  *******************************************************************************/
 
+
 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
 
 import org.eclipse.core.resources.IProject;
 import org.eclipse.core.resources.IResource;
 import org.eclipse.core.runtime.CoreException;
-import org.eclipse.core.runtime.IPath;
 import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
-import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
-import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
-import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
-import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
-import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
-import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
-import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
 import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
+import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
-import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
-
-/**
- */
-public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTrace<CtfTmfEvent> {
-
-    // ------------------------------------------------------------------------
-    // Constants
-    // ------------------------------------------------------------------------
-
-    // ------------------------------------------------------------------------
-    // Attributes
-    // ------------------------------------------------------------------------
+import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
 
-    // the Ctf Trace
-    private CTFTrace fTrace;
-
-    // The number of events collected
-    protected long fNbEvents = 0;
+public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{
 
-    // The time span of the event stream
-    private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
-    private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
-
-    // The trace resource
-    private IResource fResource;
+    //-------------------------------------------
+    //        Constants
+    //-------------------------------------------
+    /**
+     * Default cache size for CTF traces
+     */
+    protected static final int DEFAULT_CACHE_SIZE = 50000;
+    
+    //-------------------------------------------
+    //        Fields
+    //-------------------------------------------
 
-    /* Reference to the state system assigned to this trace */
+    /** Reference to the state system assigned to this trace */
     protected IStateSystemQuerier ss = null;
 
-    // ------------------------------------------------------------------------
-    // Constructors
-    // ------------------------------------------------------------------------
-
-    public CtfTmfTrace() {
-        super();
-    }
+    /* Reference to the CTF Trace */
+    private CTFTrace fTrace;
 
+    //-------------------------------------------
+    //        TmfTrace Overrides
+    //-------------------------------------------
     /**
      * Method initTrace.
      * @param resource IResource
@@ -82,12 +63,18 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
     @Override
     public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
             throws TmfTraceException {
+        /*
+         * Set the cache size. This has to be done before the call to super()
+         * because the super needs to know the cache size.
+         */
+        setCacheSize();
+        super.initTrace(resource, path, eventType);
         EventDeclaration ed;
         ITmfEventField eventField;
+
         @SuppressWarnings("unused")
         CtfTmfEventType type;
 
-        this.fResource = resource;
         try {
             this.fTrace = new CTFTrace(path);
             for( int i =0 ; i< this.fTrace.getNbEventTypes(); i++) {
@@ -107,7 +94,6 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
                 this.setStartTime(TmfTimestamp.BIG_BANG);
             } else {
                 this.setStartTime(iterator.getCurrentEvent().getTimestamp());
-                iterator.goToLastEvent();
                 this.setEndTime(iterator.getCurrentEvent().getTimestamp());
             }
 
@@ -117,10 +103,9 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
              * was not found or was not recognized as a CTF trace. Throw into
              * the new type of exception expected by the rest of TMF.
              */
-            throw new TmfTraceException(e.getMessage());
+            throw new TmfTraceException(e.getMessage(), e);
         }
 
-        TmfSignalManager.register(this);
         //FIXME This should be called via the ExperimentUpdated signal
         buildStateSystem();
 
@@ -129,36 +114,11 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
             try {
                 resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
             } catch (CoreException e) {
-                throw new TmfTraceException(e.getMessage());
+                throw new TmfTraceException(e.getMessage(), e);
             }
         }
     }
 
-    private static ITmfEventField parseDeclaration(EventDeclaration ed) {
-        EventDefinition eventDef = ed.createDefinition(null);
-        return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
-                CtfTmfEvent.parseFields(eventDef));
-    }
-
-    /**
-     * Method dispose.
-     * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#dispose()
-     */
-    @Override
-    public void dispose() {
-        TmfSignalManager.deregister(this);
-    }
-
-    /**
-     * Method broadcast.
-     * @param signal TmfSignal
-     * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#broadcast(TmfSignal)
-     */
-    @Override
-    public void broadcast(final TmfSignal signal) {
-        TmfSignalManager.dispatchSignal(signal);
-    }
-
     /**
      * Method validate.
      * @param project IProject
@@ -167,7 +127,7 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
      * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
      */
     @Override
-    public boolean validate(final IProject project, final String path) {
+    public boolean validate(@SuppressWarnings("unused") final IProject project, final String path) {
         try {
             final CTFTrace temp = new CTFTrace(path);
             return temp.majortIsSet(); // random test
@@ -177,124 +137,6 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
         }
     }
 
-    // ------------------------------------------------------------------------
-    // Accessors
-    // ------------------------------------------------------------------------
-
-    /**
-     * Method getEventType.
-     * @return the trace path
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
-     */
-    @Override
-    public Class<CtfTmfEvent> getEventType() {
-        return fType;
-    }
-
-    /**
-     * Method getNbEnvVars.
-     * @return int
-     */
-    public int getNbEnvVars() {
-        return this.fTrace.getEnvironment().size();
-    }
-
-
-    /**
-     * Method getEnvNames.
-     * @return String[]
-     */
-    public String[] getEnvNames() {
-        final String[] s = new String[getNbEnvVars()];
-        return this.fTrace.getEnvironment().keySet().toArray(s);
-    }
-
-    /**
-     * Method getEnvValue.
-     * @param key String
-     * @return String
-     */
-    public String getEnvValue(final String key)    {
-        return this.fTrace.getEnvironment().get(key);
-    }
-
-
-    /**
-
-     * @return the trace path * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
-     */
-    @Override
-    public String getPath() {
-        return this.fTrace.getPath();
-    }
-
-    /**
-     * Method getName.
-     * @return String
-     * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#getName()
-     */
-    @Override
-    public String getName() {
-        String traceName = (fResource != null) ? fResource.getName() : null;
-        // If no resource was provided, extract the display name the trace path
-        if (traceName == null) {
-            final String path = this.fTrace.getPath();
-            final int sep = path.lastIndexOf(IPath.SEPARATOR);
-            traceName = (sep >= 0) ? path.substring(sep + 1) : path;
-        }
-        return traceName;
-    }
-
-    /**
-     * Method getCacheSize.
-     * @return int
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCacheSize()
-     */
-    @Override
-    public int getCacheSize() {
-        return 50000; // not true, but it works
-    }
-
-    /**
-     * Method getNbEvents.
-     * @return long
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
-     */
-    @Override
-    public long getNbEvents() {
-        return this.fNbEvents;
-    }
-
-    /**
-     * Method getTimeRange.
-     * @return TmfTimeRange
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
-     */
-    @Override
-    public TmfTimeRange getTimeRange() {
-        return new TmfTimeRange(this.fStartTime, this.fEndTime);
-    }
-
-    /**
-     * Method getStartTime.
-     * @return ITmfTimestamp
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
-     */
-    @Override
-    public ITmfTimestamp getStartTime() {
-        return this.fStartTime;
-    }
-
-    /**
-     * Method getEndTime.
-     * @return ITmfTimestamp
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
-     */
-    @Override
-    public ITmfTimestamp getEndTime() {
-        return this.fEndTime;
-    }
-
     /**
      * Method getCurrentLocation. This is not applicable in CTF
      * @return null, since the trace has no knowledge of the current location
@@ -305,81 +147,18 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
         return null;
     }
 
-    // ------------------------------------------------------------------------
-    // Operators
-    // ------------------------------------------------------------------------
 
-    /**
-     * Method setTimeRange.
-     * @param range TmfTimeRange
-     */
-    protected void setTimeRange(final TmfTimeRange range) {
-        this.fStartTime = range.getStartTime();
-        this.fEndTime = range.getEndTime();
-    }
-
-    /**
-     * Method setStartTime.
-     * @param startTime ITmfTimestamp
-     */
-    protected void setStartTime(final ITmfTimestamp startTime) {
-        this.fStartTime = startTime;
-    }
 
-    /**
-     * Method setEndTime.
-     * @param endTime ITmfTimestamp
-     */
-    protected void setEndTime(final ITmfTimestamp endTime) {
-        this.fEndTime = endTime;
-    }
-
-    // ------------------------------------------------------------------------
-    // TmfProvider
-    // ------------------------------------------------------------------------
-
-    /**
-     * Method armRequest.
-     * @param request ITmfDataRequest<CtfTmfEvent>
-     * @return ITmfContext
-     */
     @Override
-    public ITmfContext armRequest(final ITmfDataRequest<CtfTmfEvent> request) {
-        if ((request instanceof ITmfEventRequest<?>)
-                && !TmfTimestamp.BIG_BANG
-                .equals(((ITmfEventRequest<CtfTmfEvent>) request)
-                        .getRange().getStartTime())
-                        && (request.getIndex() == 0)) {
-            final ITmfContext context = seekEvent(((ITmfEventRequest<CtfTmfEvent>) request)
-                    .getRange().getStartTime());
-            ((ITmfEventRequest<CtfTmfEvent>) request)
-            .setStartIndex((int) context.getRank());
-            return context;
-        }
-        return seekEvent(request.getIndex());
+    public double getLocationRatio(ITmfLocation<?> location) {
+        final CtfLocation curLocation = (CtfLocation) location;
+        CtfIterator iterator = new CtfIterator(this);
+        iterator.seek(curLocation.getLocation());
+        return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
+                .getStartTime())
+                / (iterator.getEndTime() - iterator.getStartTime());
     }
 
-//    /**
-//     * The trace reader keeps its own iterator: the "context" parameter here
-//     * will be ignored.
-//     *
-//     * If you wish to specify a new context, instantiate a new CtfIterator and
-//     * seek() it to where you want, and use that to read events.
-//     *
-//     * FIXME merge with getNextEvent below once they both use the same parameter
-//     * type.
-//     * @param context ITmfContext
-//     * @return CtfTmfEvent
-//     */
-//    @Override
-//    public CtfTmfEvent getNext(final ITmfContext context) {
-//        return readNextEvent(context);
-//    }
-
-    // ------------------------------------------------------------------------
-    // ITmfTrace
-    // ------------------------------------------------------------------------
-
     /**
      * Method seekEvent.
      * @param location ITmfLocation<?>
@@ -389,84 +168,31 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
     @Override
     public ITmfContext seekEvent(final ITmfLocation<?> location) {
         CtfLocation currentLocation = (CtfLocation) location;
+        CtfIterator context = new CtfIterator(this);
+        /*
+         * The rank is set to 0 if the iterator seeks the beginning. If not, it
+         * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
+         * by rank for now.
+         */
         if (currentLocation == null) {
             currentLocation = new CtfLocation(0L);
+            context.setRank(0);
         }
-        CtfIterator context = new CtfIterator(this);
-        
         if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
-            ((CtfTmfTimestamp)getEndTime()).setType(TimestampType.NANOS);
-            currentLocation.setLocation( getEndTime().getValue() + 1);
+            ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
+            currentLocation.setLocation(getEndTime().getValue() + 1);
         }
         context.setLocation(currentLocation);
+        if(context.getRank() != 0)
         context.setRank(ITmfContext.UNKNOWN_RANK);
         return context;
     }
 
-    /**
-     * Method getLocationRatio.
-     * @param location ITmfLocation<?>
-     * @return double
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(ITmfLocation<?>)
-     */
-    @Override
-    public double getLocationRatio(final ITmfLocation<?> location) {
-        final CtfLocation curLocation = (CtfLocation) location;
-        CtfIterator iterator = new CtfIterator(this);
-        iterator.seek(curLocation.getLocation());
-        return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
-                .getStartTime())
-                / (iterator.getEndTime() - iterator.getStartTime());
-    }
-
-    /**
-     * Method getStreamingInterval.
-     * @return long
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
-     */
-    @Override
-    public long getStreamingInterval() {
-        return 0;
-    }
-
-    /**
-     * Method seekEvent.
-     * @param timestamp ITmfTimestamp
-     * @return ITmfContext
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfTimestamp)
-     */
-    @Override
-    public ITmfContext seekEvent(final ITmfTimestamp timestamp) {
-        CtfIterator context = new CtfIterator(this);
-        context.seek(timestamp.getValue());
-        context.setRank(ITmfContext.UNKNOWN_RANK);
-        return context;
-    }
-
-    /**
-     * Seek by rank
-     * @param rank long
-     * @return ITmfContext
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
-     */
-    @Override
-    public ITmfContext seekEvent(final long rank) {
-        CtfIterator context = new CtfIterator(this);
-        context.seekRank(rank);
-        context.setRank(rank);
-        return context;
-    }
 
-    /**
-     * Seek rank ratio
-     * @param ratio double
-     * @return ITmfContext
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
-     */
     @Override
-    public ITmfContext seekEvent(final double ratio) {
+    public ITmfContext seekEvent(double ratio) {
         CtfIterator context = new CtfIterator(this);
-        context.seek((long) (this.fNbEvents * ratio));
+        context.seek((long) (this.getNbEvents() * ratio));
         context.setRank(ITmfContext.UNKNOWN_RANK);
         return context;
     }
@@ -478,28 +204,39 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
      * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
      */
     @Override
-    public CtfTmfEvent getNext(final ITmfContext context) {
+    public synchronized CtfTmfEvent getNext(final ITmfContext context) {
         CtfTmfEvent event = null;
         if (context instanceof CtfIterator) {
             CtfIterator ctfIterator = (CtfIterator) context;
             event = ctfIterator.getCurrentEvent();
-            ctfIterator.advance();
+
+            if (event != null) {
+                updateAttributes(context, event.getTimestamp());
+                ctfIterator.advance();
+                ctfIterator.increaseRank();
+            }
         }
+
         return event;
     }
 
     /**
-     * Method getResource.
-     * @return IResource
-     * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
+     * Suppressing the warning, because the 'throws' will usually happen in
+     * sub-classes.
+     * @throws TmfTraceException
      */
-    @Override
-    public IResource getResource() {
-        return this.fResource;
+    @SuppressWarnings({ "static-method", "unused" })
+    protected void buildStateSystem() throws TmfTraceException {
+        /*
+         * Nothing is done in the basic implementation, please specify
+         * how/if to build a state system in derived classes.
+         */
+        return;
     }
 
     /**
      * Method getStateSystem.
+     *
      * @return IStateSystemQuerier
      */
     public IStateSystemQuerier getStateSystem() {
@@ -507,25 +244,88 @@ public class CtfTmfTrace extends TmfEventProvider<CtfTmfEvent> implements ITmfTr
     }
 
     /**
-     * Method getCTFTrace.
-     * @return CTFTrace
+     *
+     * @param ed
+     * @return
      */
-    CTFTrace getCTFTrace() {
+    private static ITmfEventField parseDeclaration(EventDeclaration ed) {
+        EventDefinition eventDef = ed.createDefinition(null);
+        return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
+                CtfTmfEvent.parseFields(eventDef));
+    }
+
+    /**
+     * gets the CTFtrace that this is wrapping
+     * @return the CTF trace
+     */
+    public CTFTrace getCTFTrace() {
         return fTrace;
     }
 
 
+    //-------------------------------------------
+    //        Environment Parameters
+    //-------------------------------------------
     /**
-     * Suppressing the warning, because the 'throws' will usually happen in
-     * sub-classes.
-     * @throws TmfTraceException
+     * Method getNbEnvVars.
+     *
+     * @return int
      */
-    protected void buildStateSystem() throws TmfTraceException {
-        /*
-         * Nothing is done in the basic implementation, please specify
-         * how/if to build a state system in derived classes.
-         */
-        return;
+    public int getNbEnvVars() {
+        return this.fTrace.getEnvironment().size();
+    }
+
+    /**
+     * Method getEnvNames.
+     *
+     * @return String[]
+     */
+    public String[] getEnvNames() {
+        final String[] s = new String[getNbEnvVars()];
+        return this.fTrace.getEnvironment().keySet().toArray(s);
+    }
+
+    /**
+     * Method getEnvValue.
+     *
+     * @param key
+     *            String
+     * @return String
+     */
+    public String getEnvValue(final String key) {
+        return this.fTrace.getEnvironment().get(key);
+    }
+
+    //-------------------------------------------
+    //        Clocks
+    //-------------------------------------------
+
+    public long getOffset(){
+        if( fTrace != null ) {
+            return fTrace.getOffset();
+        }
+        return 0;
+    }
+
+    //-------------------------------------------
+    //        Parser
+    //-------------------------------------------
+
+    @Override
+    public CtfTmfEvent parseEvent(ITmfContext context) {
+        CtfTmfEvent event = null;
+        if( context instanceof CtfIterator ){
+            CtfIterator itt = (CtfIterator) context;
+            event = itt.getCurrentEvent();
+        }
+        return event;
+    }
+    
+    /**
+     * Sets the cache size for a CtfTmfTrace. 
+     */
+    protected void setCacheSize() {
+        setCacheSize(DEFAULT_CACHE_SIZE);
     }
 
 }
This page took 0.030055 seconds and 5 git commands to generate.