tmf: Cache CallStackEvent names
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / views / callstack / CallStackPresentationProvider.java
index 4b7f68ab2c75a791fe928d0277aa50de23390069..b6478f028afb0a3c7823b7e89b9b55bf5875dba3 100644 (file)
@@ -1,5 +1,5 @@
 /*******************************************************************************
- * Copyright (c) 2013, 2014 Ericsson
+ * Copyright (c) 2013, 2016 Ericsson
  *
  * All rights reserved. This program and the accompanying materials are
  * made available under the terms of the Eclipse Public License v1.0 which
@@ -12,6 +12,8 @@
 
 package org.eclipse.tracecompass.tmf.ui.views.callstack;
 
+import java.util.Optional;
+
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.graphics.GC;
 import org.eclipse.swt.graphics.RGB;
@@ -19,7 +21,6 @@ import org.eclipse.swt.graphics.Rectangle;
 import org.eclipse.tracecompass.internal.tmf.ui.Activator;
 import org.eclipse.tracecompass.internal.tmf.ui.Messages;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
-import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
@@ -30,6 +31,10 @@ import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
 /**
  * Presentation provider for the Call Stack view, based on the generic TMF
  * presentation provider.
@@ -45,6 +50,28 @@ public class CallStackPresentationProvider extends TimeGraphPresentationProvider
 
     private Integer fAverageCharWidth;
 
+    private final LoadingCache<CallStackEvent, Optional<String>> fTimeEventNames = CacheBuilder.newBuilder()
+            .maximumSize(1000)
+            .build(new CacheLoader<CallStackEvent, Optional<String>>() {
+                @Override
+                public Optional<String> load(CallStackEvent event) {
+                    CallStackEntry entry = event.getEntry();
+                    ITmfStateSystem ss = entry.getStateSystem();
+                    try {
+                        ITmfStateValue value = ss.querySingleState(event.getTime(), entry.getQuark()).getStateValue();
+                        if (!value.isNull()) {
+                            String name = fView.getFunctionName(entry.getTrace(), entry.getProcessId(), event.getTime(), value);
+                            return Optional.ofNullable(name);
+                        }
+                    } catch (TimeRangeException e) {
+                        Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
+                    } catch (StateSystemDisposedException e) {
+                        /* Ignored */
+                    }
+                    return Optional.empty();
+                }
+            });
+
     private enum State {
         MULTIPLE (new RGB(100, 100, 100)),
         EXEC     (new RGB(0, 200, 0));
@@ -59,7 +86,7 @@ public class CallStackPresentationProvider extends TimeGraphPresentationProvider
     /**
      * Constructor
      *
-     * @since 2.0
+     * @since 1.2
      */
     public CallStackPresentationProvider() {
     }
@@ -69,7 +96,7 @@ public class CallStackPresentationProvider extends TimeGraphPresentationProvider
      *
      * @param view
      *            The call stack view that will contain the time events
-     * @since 2.0
+     * @since 1.2
      */
     public void setCallStackView(CallStackView view) {
         fView = view;
@@ -107,21 +134,7 @@ public class CallStackPresentationProvider extends TimeGraphPresentationProvider
     @Override
     public String getEventName(ITimeEvent event) {
         if (event instanceof CallStackEvent) {
-            CallStackEntry entry = (CallStackEntry) event.getEntry();
-            ITmfStateSystem ss = entry.getStateSystem();
-            try {
-                ITmfStateValue value = ss.querySingleState(event.getTime(), entry.getQuark()).getStateValue();
-                if (!value.isNull()) {
-                    return fView.getFunctionName(entry.getTrace(), value);
-                }
-            } catch (AttributeNotFoundException e) {
-                Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
-            } catch (TimeRangeException e) {
-                Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
-            } catch (StateSystemDisposedException e) {
-                /* Ignored */
-            }
-            return null;
+            return fTimeEventNames.getUnchecked((CallStackEvent) event).orElse(null);
         }
         return State.MULTIPLE.toString();
     }
@@ -137,22 +150,17 @@ public class CallStackPresentationProvider extends TimeGraphPresentationProvider
         if (!(event instanceof CallStackEvent)) {
             return;
         }
-        CallStackEntry entry = (CallStackEntry) event.getEntry();
-        ITmfStateSystem ss = entry.getStateSystem();
-        try {
-            ITmfStateValue value = ss.querySingleState(event.getTime(), entry.getQuark()).getStateValue();
-            if (!value.isNull()) {
-                String name = fView.getFunctionName(entry.getTrace(), value);
-                gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
-                Utils.drawText(gc, name, bounds.x, bounds.y, bounds.width, bounds.height, true, true);
-            }
-        } catch (AttributeNotFoundException e) {
-            Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
-        } catch (TimeRangeException e) {
-            Activator.getDefault().logError("Error querying state system", e); //$NON-NLS-1$
-        } catch (StateSystemDisposedException e) {
-            /* Ignored */
-        }
+        String name = fTimeEventNames.getUnchecked((CallStackEvent) event).orElse(null);
+        gc.setForeground(gc.getDevice().getSystemColor(SWT.COLOR_WHITE));
+        Utils.drawText(gc, name, bounds.x, bounds.y, bounds.width, bounds.height, true, true);
+    }
+
+    /**
+     * Indicate that the provider of function names has changed, so any cached
+     * values must be reset.
+     */
+    void resetFunctionNames() {
+        fTimeEventNames.invalidateAll();
     }
 
 }
This page took 0.031032 seconds and 5 git commands to generate.