tmf : Add Leaf nodes to the History Tree
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statistics / TmfStateStatistics.java
index 4ddecf723dbb18b46dcb1368a3f58ad3c181eb58..d75aaa5dd110f99ac25b197e0e2b5e0ea8d013b5 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2012, 2013 Ericsson
+ * Copyright (c) 2012, 2014 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -8,11 +8,11 @@
  *
  * Contributors:
  *   Alexandre Montplaisir - Initial API and implementation
+ *   Patrick Tasse - Fix TimeRangeException
  ******************************************************************************/
 
 package org.eclipse.linuxtools.tmf.core.statistics;
 
-import java.io.File;
 import java.util.HashMap;
 import java.util.LinkedList;
 import java.util.List;
@@ -23,12 +23,8 @@ import org.eclipse.linuxtools.tmf.core.exceptions.AttributeNotFoundException;
 import org.eclipse.linuxtools.tmf.core.exceptions.StateSystemDisposedException;
 import org.eclipse.linuxtools.tmf.core.exceptions.StateValueTypeException;
 import org.eclipse.linuxtools.tmf.core.exceptions.TimeRangeException;
-import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
 import org.eclipse.linuxtools.tmf.core.interval.ITmfStateInterval;
-import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateProvider;
 import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
-import org.eclipse.linuxtools.tmf.core.statesystem.TmfStateSystemFactory;
-import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
 
 /**
  * Implementation of ITmfStatistics which uses a state history for storing its
@@ -74,30 +70,6 @@ public class TmfStateStatistics implements ITmfStatistics {
         this.typesStats = eventTypes;
     }
 
-    /**
-     * Manual constructor. This should be used if the trace's Resource is null
-     * (ie, for unit tests). It requires specifying the location of the history
-     * files manually.
-     *
-     * @param trace
-     *            The trace for which we build these statistics
-     * @param totalsHistoryFile
-     *            The location of the totals state history file
-     * @param typesHistoryFile
-     *            The location of the types state history file
-     * @throws TmfTraceException
-     *             If the file could not be written to
-     * @since 2.2
-     */
-    @Deprecated
-    public TmfStateStatistics(ITmfTrace trace, File totalsHistoryFile,
-            File typesHistoryFile) throws TmfTraceException {
-        final ITmfStateProvider totalsInput = new TmfStatisticsTotalsModule().new StatsProviderTotals(trace);
-        final ITmfStateProvider typesInput = new TmfStatisticsEventTypesModule().new StatsProviderEventTypes(trace);
-        this.totalsStats = TmfStateSystemFactory.newFullHistory(totalsHistoryFile, totalsInput, true);
-        this.typesStats = TmfStateSystemFactory.newFullHistory(typesHistoryFile, typesInput, true);
-    }
-
     /**
      * Return the state system containing the "totals" values
      *
@@ -133,7 +105,6 @@ public class TmfStateStatistics implements ITmfStatistics {
         final List<Long> list = new LinkedList<>();
         final long increment = (end - start) / nb;
 
-        totalsStats.waitUntilBuilt();
         if (totalsStats.isCancelled()) {
             return list;
         }
@@ -168,9 +139,6 @@ public class TmfStateStatistics implements ITmfStatistics {
 
     @Override
     public long getEventsTotal() {
-        /* We need the complete state history to be built to answer this. */
-        totalsStats.waitUntilBuilt();
-
         long endTime = totalsStats.getCurrentEndTime();
         int count = 0;
 
@@ -181,11 +149,7 @@ public class TmfStateStatistics implements ITmfStatistics {
         } catch (TimeRangeException e) {
             /* Assume there is no events for that range */
             return 0;
-        } catch (AttributeNotFoundException e) {
-            e.printStackTrace();
-        } catch (StateValueTypeException e) {
-            e.printStackTrace();
-        } catch (StateSystemDisposedException e) {
+        } catch (AttributeNotFoundException | StateValueTypeException | StateSystemDisposedException e) {
             e.printStackTrace();
         }
 
@@ -194,10 +158,7 @@ public class TmfStateStatistics implements ITmfStatistics {
 
     @Override
     public Map<String, Long> getEventTypesTotal() {
-        /* We need the complete state history to be built to answer this. */
-        typesStats.waitUntilBuilt();
-
-        Map<String, Long> map = new HashMap<>();
+        final Map<String, Long> map = new HashMap<>();
         long endTime = typesStats.getCurrentEndTime();
 
         try {
@@ -226,10 +187,6 @@ public class TmfStateStatistics implements ITmfStatistics {
 
     @Override
     public long getEventsInRange(long start, long end) {
-        // FIXME Instead of waiting until the end, we could check the current
-        // end time, and answer as soon as possible...
-        totalsStats.waitUntilBuilt();
-
         long startCount;
         if (start == totalsStats.getStartTime()) {
             startCount = 0;
@@ -247,33 +204,36 @@ public class TmfStateStatistics implements ITmfStatistics {
 
     @Override
     public Map<String, Long> getEventTypesInRange(long start, long end) {
-        // FIXME Instead of waiting until the end, we could check the current
-        // end time, and answer as soon as possible...
-        typesStats.waitUntilBuilt();
-
-        Map<String, Long> map = new HashMap<>();
+        final Map<String, Long> map = new HashMap<>();
+        List<Integer> quarks;
 
         /* Make sure the start/end times are within the state history, so we
          * don't get TimeRange exceptions.
          */
-        long startTime = checkStartTime(start);
-        long endTime = checkEndTime(end);
+        long startTime = checkStartTime(start, typesStats);
+        long endTime = checkEndTime(end, typesStats);
 
         try {
             /* Get the list of quarks, one for each even type in the database */
             int quark = typesStats.getQuarkAbsolute(Attributes.EVENT_TYPES);
-            List<Integer> quarks = typesStats.getSubAttributes(quark, false);
+            quarks = typesStats.getSubAttributes(quark, false);
+        } catch (AttributeNotFoundException e) {
+            /*
+             * The state system does not (yet?) have the needed attributes, it
+             * probably means there are no events counted yet. Return the empty
+             * map.
+             */
+            return map;
+        }
 
+        try {
             List<ITmfStateInterval> endState = typesStats.queryFullState(endTime);
 
-            String curEventName;
-            long countAtStart, countAtEnd, eventCount;
-
             if (startTime == typesStats.getStartTime()) {
                 /* Only use the values picked up at the end time */
                 for (int typeQuark : quarks) {
-                    curEventName = typesStats.getAttributeName(typeQuark);
-                    eventCount = endState.get(typeQuark).getStateValue().unboxInt();
+                    String curEventName = typesStats.getAttributeName(typeQuark);
+                    long eventCount = endState.get(typeQuark).getStateValue().unboxInt();
                     if (eventCount == -1) {
                         eventCount = 0;
                     }
@@ -286,9 +246,9 @@ public class TmfStateStatistics implements ITmfStatistics {
                  */
                 List<ITmfStateInterval> startState = typesStats.queryFullState(startTime - 1);
                 for (int typeQuark : quarks) {
-                    curEventName = typesStats.getAttributeName(typeQuark);
-                    countAtStart = startState.get(typeQuark).getStateValue().unboxInt();
-                    countAtEnd = endState.get(typeQuark).getStateValue().unboxInt();
+                    String curEventName = typesStats.getAttributeName(typeQuark);
+                    long countAtStart = startState.get(typeQuark).getStateValue().unboxInt();
+                    long countAtEnd = endState.get(typeQuark).getStateValue().unboxInt();
 
                     if (countAtStart == -1) {
                         countAtStart = 0;
@@ -296,19 +256,19 @@ public class TmfStateStatistics implements ITmfStatistics {
                     if (countAtEnd == -1) {
                         countAtEnd = 0;
                     }
-                    eventCount = countAtEnd - countAtStart;
+                    long eventCount = countAtEnd - countAtStart;
                     map.put(curEventName, eventCount);
                 }
             }
 
-        } catch (TimeRangeException e) {
-            /* Assume there is no events, nothing will be put in the map. */
-        } catch (AttributeNotFoundException | StateValueTypeException | StateSystemDisposedException e) {
+        } catch (TimeRangeException | StateSystemDisposedException e) {
+            /* Assume there is no (more) events, nothing will be put in the map. */
+        } catch (StateValueTypeException e) {
             /*
-             * These other exception types would show a logic problem however,
+             * This exception type would show a logic problem however,
              * so they should not happen.
              */
-            e.printStackTrace();
+            throw new IllegalStateException();
         }
         return map;
     }
@@ -319,8 +279,8 @@ public class TmfStateStatistics implements ITmfStatistics {
 
     private long getEventCountAt(long timestamp) {
         /* Make sure the target time is within the range of the history */
-        long ts = checkStartTime(timestamp);
-        ts = checkEndTime(ts);
+        long ts = checkStartTime(timestamp, totalsStats);
+        ts = checkEndTime(ts, totalsStats);
 
         try {
             final int quark = totalsStats.getQuarkAbsolute(Attributes.TOTAL);
@@ -336,18 +296,18 @@ public class TmfStateStatistics implements ITmfStatistics {
         return 0;
     }
 
-    private long checkStartTime(long initialStart) {
+    private static long checkStartTime(long initialStart, ITmfStateSystem ss) {
         long start = initialStart;
-        if (start < totalsStats.getStartTime()) {
-            return totalsStats.getStartTime();
+        if (start < ss.getStartTime()) {
+            return ss.getStartTime();
         }
         return start;
     }
 
-    private long checkEndTime(long initialEnd) {
+    private static long checkEndTime(long initialEnd, ITmfStateSystem ss) {
         long end = initialEnd;
-        if (end > totalsStats.getCurrentEndTime()) {
-            return totalsStats.getCurrentEndTime();
+        if (end > ss.getCurrentEndTime()) {
+            return ss.getCurrentEndTime();
         }
         return end;
     }
This page took 0.028318 seconds and 5 git commands to generate.