os.linux & tmf.ui: introduce dynamic filter for cfv: Active threads and Threads on...
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / internal / analysis / os / linux / ui / views / controlflow / ControlFlowView.java
index e0df2aff7da20ddbf7aa2262699a221c5cedd7e8..fa71123d540655decb6b5c4c018145aeec127366 100644 (file)
 
 package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow;
 
+import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
+
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -44,8 +46,12 @@ import org.eclipse.jface.action.IToolBarManager;
 import org.eclipse.jface.action.MenuManager;
 import org.eclipse.jface.action.Separator;
 import org.eclipse.jface.dialogs.IDialogSettings;
+import org.eclipse.jface.util.IPropertyChangeListener;
+import org.eclipse.jface.util.PropertyChangeEvent;
 import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
+import org.eclipse.jface.viewers.ViewerFilter;
+import org.eclipse.jface.window.Window;
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Event;
 import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
@@ -55,6 +61,8 @@ import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attribute
 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
 import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.FollowThreadAction;
+import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.filters.ActiveThreadsFilter;
+import org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow.filters.DynamicFilterDialog;
 import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
 import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
 import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
@@ -86,7 +94,9 @@ import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphContro
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
+import org.eclipse.ui.PlatformUI;
 
+import com.google.common.base.Objects;
 import com.google.common.collect.ImmutableList;
 
 /**
@@ -101,7 +111,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
     /**
      * View ID.
      */
-    public static final String ID = "org.eclipse.tracecompass.analysis.os.linux.views.controlflow"; //$NON-NLS-1$
+    public static final @NonNull String ID = "org.eclipse.tracecompass.analysis.os.linux.views.controlflow"; //$NON-NLS-1$
 
     private static final String ICONS_PATH = "icons/"; //$NON-NLS-1$
     private static final String OPTIMIZE_ICON = ICONS_PATH + "elcl16/Optimization.png"; //$NON-NLS-1$
@@ -133,7 +143,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
     private static final Comparator<ITimeGraphEntry>[] COLUMN_COMPARATORS;
 
-    private final Function<Collection<ILinkEvent>, Map<Integer, Long>> UPDATE_SCHEDULING_COLUMN_ALGO = new OptimizationAlgorithm();
+    private final Function<Collection<ILinkEvent>, Map<Integer, Long>> UPDATE_SCHEDULING_COLUMN_ALGO = new NaiveOptimizationAlgorithm();
 
     private static final int INITIAL_SORT_COLUMN_INDEX = 3;
 
@@ -168,6 +178,46 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
     private IAction fHierarchicalAction;
 
+    private @NonNull ActiveThreadsFilter fActiveThreadsFilter = new ActiveThreadsFilter(null, false);
+
+    private final ActiveThreadsFilterAction fActiveThreadsRapidToggle = new ActiveThreadsFilterAction();
+
+    class ActiveThreadsFilterAction extends Action {
+        public ActiveThreadsFilterAction() {
+            super(PackageMessages.ControlFlowView_DynamicFiltersActiveThreadToggleLabel, IAction.AS_CHECK_BOX);
+            setToolTipText(PackageMessages.ControlFlowView_DynamicFiltersActiveThreadToggleToolTip);
+            addPropertyChangeListener(new IPropertyChangeListener() {
+                @Override
+                public void propertyChange(PropertyChangeEvent event) {
+                    if (!(event.getNewValue() instanceof Boolean)) {
+                        return;
+                    }
+
+                    Boolean enabled = (Boolean) event.getNewValue();
+
+                    /* Always remove the previous Active Threads filter */
+                    getTimeGraphCombo().removeFilter(fActiveThreadsFilter);
+
+                    if (enabled) {
+                        fActiveThreadsFilter.setEnabled(true);
+                        getTimeGraphCombo().addFilter(fActiveThreadsFilter);
+
+                        /* Use flat representation */
+                        if (fFlatAction != null) {
+                            applyFlatPresentation();
+                            fFlatAction.setChecked(true);
+                            fHierarchicalAction.setChecked(false);
+                        }
+                    } else {
+                        fActiveThreadsFilter.setEnabled(false);
+                    }
+
+                    refresh();
+                }
+            });
+        }
+    }
+
     // ------------------------------------------------------------------------
     // Constructors
     // ------------------------------------------------------------------------
@@ -265,7 +315,8 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
     @Override
     protected void fillLocalMenu(IMenuManager manager) {
         super.fillLocalMenu(manager);
-        final MenuManager item = new MenuManager(Messages.ControlFlowView_threadPresentation);
+
+        MenuManager item = new MenuManager(Messages.ControlFlowView_threadPresentation);
         fFlatAction = createFlatAction();
         item.add(fFlatAction);
 
@@ -273,8 +324,18 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         item.add(fHierarchicalAction);
         manager.add(item);
 
+        item = new MenuManager(PackageMessages.ControlFlowView_DynamicFiltersMenuLabel);
+        item.add(fActiveThreadsRapidToggle);
+        item.add(new Separator());
+
+        IAction dynamicFiltersConfigureAction = createDynamicFilterConfigureAction();
+        item.add(dynamicFiltersConfigureAction);
+
+        manager.add(item);
     }
 
+
+
     /**
      * Base Action for the "Go to Next/Previous Event for thread" actions
      */
@@ -353,6 +414,44 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         }
     }
 
+    private IAction createDynamicFilterConfigureAction() {
+        return new Action(PackageMessages.ControlFlowView_DynamicFiltersConfigureLabel, IAction.AS_PUSH_BUTTON) {
+            @Override
+            public void run() {
+                DynamicFilterDialog dialog = new DynamicFilterDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), fActiveThreadsFilter);
+                if (dialog.open() == Window.OK) {
+                    /* Remove the previous Active Threads filter */
+                    checkNotNull(getTimeGraphCombo()).removeFilter(fActiveThreadsFilter);
+
+                    ActiveThreadsFilter newFilter = dialog.getActiveThreadsResult();
+                    ActiveThreadsFilter previousFilter = fActiveThreadsFilter;
+
+                    /* Set the filter to the view */
+                    fActiveThreadsFilter = newFilter;
+
+                    boolean enabled = fActiveThreadsFilter.isEnabled();
+                    if (enabled) {
+                        checkNotNull(getTimeGraphCombo()).addFilter(newFilter);
+                    }
+
+                    /*
+                     * Prevent double refresh from change state of setChecked
+                     * and ensure that a refresh is done if the mode of the
+                     * filter is changed or options are changed
+                     */
+                    if (previousFilter.isEnabled() && newFilter.isEnabled()) {
+                        boolean changed = !Objects.equal(previousFilter.getCpuRanges(), newFilter.getCpuRanges()) || previousFilter.isCpuRangesBased() != newFilter.isCpuRangesBased();
+                        if (changed) {
+                            refresh();
+                        }
+                    } else {
+                        fActiveThreadsRapidToggle.setChecked(enabled);
+                    }
+                }
+            }
+        };
+    }
+
     private IAction createHierarchicalAction() {
         IAction action = new Action(Messages.ControlFlowView_hierarchicalViewLabel, IAction.AS_RADIO_BUTTON) {
             @Override
@@ -383,23 +482,31 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         IAction action = new Action(Messages.ControlFlowView_flatViewLabel, IAction.AS_RADIO_BUTTON) {
             @Override
             public void run() {
-                ITmfTrace parentTrace = getTrace();
-                synchronized (fFlatTraces) {
-                    fFlatTraces.add(parentTrace);
-                    for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
-                        final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
-                        for (TimeGraphEntry traceEntry : getEntryList(ss)) {
-                            hierarchicalToFlatTree(traceEntry);
-                        }
-                    }
-                }
+                applyFlatPresentation();
                 refresh();
             }
         };
+        action.setChecked(true);
         action.setToolTipText(Messages.ControlFlowView_flatViewToolTip);
         return action;
     }
 
+    private void applyFlatPresentation() {
+        ITmfTrace parentTrace = getTrace();
+        synchronized (fFlatTraces) {
+            fFlatTraces.add(parentTrace);
+            for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
+                final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
+                List<@NonNull TimeGraphEntry> entryList = getEntryList(ss);
+                if (entryList != null) {
+                    for (TimeGraphEntry traceEntry : entryList) {
+                        hierarchicalToFlatTree(traceEntry);
+                    }
+                }
+            }
+        }
+    }
+
     @Override
     protected String getNextText() {
         return Messages.ControlFlowView_nextProcessActionNameText;
@@ -495,81 +602,6 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
     }
 
-    /**
-     * Optimization algorithm, overridable.
-     *
-     * @author Matthew Khouzam
-     * @author Samuel Gagnon
-     */
-    public static class OptimizationAlgorithm implements Function<Collection<ILinkEvent>, Map<Integer, Long>> {
-
-        /**
-         * Get the scheduling column order by arrows
-         *
-         * @param arrows
-         *            the list of visible links
-         * @return the list of weights, by thread ID
-         */
-        @Override
-        public Map<Integer, Long> apply(Collection<ILinkEvent> arrows) {
-            /*
-             * "transitions" contains the count of every arrows between two tids
-             * (Pair<Integer, Integer>). For constructing the Pair, we always
-             * put the smallest tid first
-             */
-            Map<Pair<Integer, Integer>, Integer> transitions = new HashMap<>();
-
-            /*
-             * We iterate in arrows to count the number of transitions between
-             * every pair (tid,tid) in the current view
-             */
-            for (ILinkEvent arrow : arrows) {
-                ITimeGraphEntry from = arrow.getEntry();
-                ITimeGraphEntry to = arrow.getDestinationEntry();
-                if (!(from instanceof ControlFlowEntry) || !(to instanceof ControlFlowEntry)) {
-                    continue;
-                }
-                int fromTid = ((ControlFlowEntry) from).getThreadId();
-                int toTid = ((ControlFlowEntry) to).getThreadId();
-                if (fromTid != toTid) {
-                    Pair<Integer, Integer> key = new Pair<>(Math.min(fromTid, toTid), Math.max(fromTid, toTid));
-                    Integer count = transitions.getOrDefault(key, 0);
-                    transitions.put(key, count + 1);
-                }
-            }
-
-            /*
-             * We now have a transition count for every pair (tid,tid). The next
-             * step is to sort every pair according to its count in decreasing
-             * order
-             */
-            List<Pair<Integer, Integer>> sortedTransitionsByCount = transitions.entrySet().stream().sorted(Map.Entry.<Pair<Integer, Integer>, Integer> comparingByValue().reversed()).map(Map.Entry::getKey).collect(Collectors.toList());
-
-            /*
-             * Next, we find the order in which we want to display our threads.
-             * We simply iterate in every pair (tid,tid) in orderedTidList. Each
-             * time we see a new tid, we add it at the end of orderedTidList.
-             * This way, threads with lots of transitions will be grouped in the
-             * top. While very naive, this algorithm is fast, simple and gives
-             * decent results.
-             */
-            Map<Integer, Long> orderedTidMap = new LinkedHashMap<>();
-            long pos = 0;
-            for (Pair<Integer, Integer> threadPair : sortedTransitionsByCount) {
-                if (orderedTidMap.get(threadPair.getFirst()) == null) {
-                    orderedTidMap.put(threadPair.getFirst(), pos);
-                    pos++;
-                }
-                if (orderedTidMap.get(threadPair.getSecond()) == null) {
-                    orderedTidMap.put(threadPair.getSecond(), pos);
-                    pos++;
-                }
-            }
-
-            return orderedTidMap;
-        }
-    }
-
     /**
      * @author gbastien
      *
@@ -653,6 +685,8 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
     @Override
     public void traceSelected(TmfTraceSelectedSignal signal) {
         super.traceSelected(signal);
+
+        /* Update the Flat and Hierarchical actions */
         synchronized (fFlatTraces) {
             if (fFlatTraces.contains(signal.getTrace())) {
                 fHierarchicalAction.setChecked(false);
@@ -662,6 +696,21 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                 fHierarchicalAction.setChecked(true);
             }
         }
+
+        /* Update the Dynamic Filters related actions */
+        ViewerFilter activeThreadFilter = null;
+        ViewerFilter[] traceFilters = getFiltersMap().get(signal.getTrace());
+        if (traceFilters != null) {
+            activeThreadFilter = getActiveThreadsFilter(traceFilters);
+        }
+
+        if (activeThreadFilter == null) {
+            fActiveThreadsFilter = new ActiveThreadsFilter(null, false);
+        } else {
+            fActiveThreadsFilter = (@NonNull ActiveThreadsFilter) checkNotNull(activeThreadFilter);
+        }
+
+        fActiveThreadsRapidToggle.setChecked(fActiveThreadsFilter.isEnabled());
     }
 
     // ------------------------------------------------------------------------
@@ -724,19 +773,16 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                             continue;
                         }
 
-                        int execNameQuark;
-                        int ppidQuark;
-                        try {
-                            execNameQuark = ssq.getQuarkRelative(threadQuark, Attributes.EXEC_NAME);
-                            ppidQuark = ssq.getQuarkRelative(threadQuark, Attributes.PPID);
-                        } catch (AttributeNotFoundException e) {
+                        int execNameQuark = ssq.optQuarkRelative(threadQuark, Attributes.EXEC_NAME);
+                        int ppidQuark = ssq.optQuarkRelative(threadQuark, Attributes.PPID);
+                        if (execNameQuark == ITmfStateSystem.INVALID_ATTRIBUTE) {
                             /* No information on this thread (yet?), skip it for now */
                             continue;
                         }
                         ITmfStateInterval lastExecNameInterval = prevFullState == null || execNameQuark >= prevFullState.size() ? null : prevFullState.get(execNameQuark);
                         long lastExecNameStartTime = lastExecNameInterval == null ? -1 : lastExecNameInterval.getStartTime();
                         long lastExecNameEndTime = lastExecNameInterval == null ? -1 : lastExecNameInterval.getEndTime() + 1;
-                        long lastPpidStartTime = prevFullState == null || ppidQuark >= prevFullState.size() ? -1 : prevFullState.get(ppidQuark).getStartTime();
+                        long lastPpidStartTime = prevFullState == null || ppidQuark >= prevFullState.size() || ppidQuark == ITmfStateSystem.INVALID_ATTRIBUTE ? -1 : prevFullState.get(ppidQuark).getStartTime();
                         for (List<ITmfStateInterval> fullState : fullStates) {
                             if (monitor.isCanceled()) {
                                 return;
@@ -746,10 +792,10 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                                 continue;
                             }
                             ITmfStateInterval execNameInterval = fullState.get(execNameQuark);
-                            ITmfStateInterval ppidInterval = fullState.get(ppidQuark);
+                            ITmfStateInterval ppidInterval = ppidQuark == ITmfStateSystem.INVALID_ATTRIBUTE ? null : fullState.get(ppidQuark);
                             long startTime = execNameInterval.getStartTime();
                             long endTime = execNameInterval.getEndTime() + 1;
-                            if (startTime == lastExecNameStartTime && ppidInterval.getStartTime() == lastPpidStartTime) {
+                            if (startTime == lastExecNameStartTime && ppidInterval != null && ppidInterval.getStartTime() == lastPpidStartTime) {
                                 continue;
                             }
                             boolean isNull = execNameInterval.getStateValue().isNull();
@@ -760,7 +806,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                                  */
                                 try {
                                     execNameInterval = ssq.querySingleState(startTime - 1, execNameQuark);
-                                    ppidInterval = ssq.querySingleState(startTime - 1, ppidQuark);
+                                    ppidInterval = ppidQuark == ITmfStateSystem.INVALID_ATTRIBUTE ? null : ssq.querySingleState(startTime - 1, ppidQuark);
                                     startTime = execNameInterval.getStartTime();
                                     endTime = execNameInterval.getEndTime() + 1;
                                 } catch (StateSystemDisposedException e) {
@@ -770,7 +816,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                             if (!execNameInterval.getStateValue().isNull() &&
                                     execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
                                 String execName = execNameInterval.getStateValue().unboxStr();
-                                int ppid = ppidInterval.getStateValue().unboxInt();
+                                int ppid = ppidInterval == null ? -1 : ppidInterval.getStateValue().unboxInt();
                                 ControlFlowEntry entry = entryMap.get(entryKey);
                                 if (entry == null) {
                                     entry = new ControlFlowEntry(threadQuark, trace, execName, threadId, ppid, startTime, endTime);
@@ -792,7 +838,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                             }
                             lastExecNameStartTime = startTime;
                             lastExecNameEndTime = endTime;
-                            lastPpidStartTime = ppidInterval.getStartTime();
+                            lastPpidStartTime = ppidInterval == null ? -1 : ppidInterval.getStartTime();
                         }
                     }
                     synchronized (fFlatTraces) {
@@ -882,11 +928,11 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                     if (parent.getThreadId() == entry.getParentThreadId() &&
                             !(entry.getStartTime() > parent.getEndTime() ||
                             entry.getEndTime() < parent.getStartTime())) {
-                        parent.addChild(entry);
                         root = false;
                         if (rootList.contains(entry)) {
                             traceEntry.removeChild(entry);
                         }
+                        parent.addChild(entry);
                         break;
                     }
                 }
@@ -933,8 +979,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         }
         ControlFlowEntry entry = (ControlFlowEntry) tgentry;
         try {
-            int threadQuark = entry.getThreadQuark();
-            int statusQuark = ss.getQuarkRelative(threadQuark, Attributes.STATUS);
+            int statusQuark = entry.getThreadQuark();
             eventList = new ArrayList<>(fullStates.size());
             ITmfStateInterval lastInterval = prevFullState == null || statusQuark >= prevFullState.size() ? null : prevFullState.get(statusQuark);
             long lastStartTime = lastInterval == null ? -1 : lastInterval.getStartTime();
@@ -970,7 +1015,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                 lastStartTime = time;
                 lastEndTime = time + duration;
             }
-        } catch (AttributeNotFoundException | TimeRangeException e) {
+        } catch (TimeRangeException e) {
             Activator.getDefault().logError(e.getMessage());
         }
         return eventList;
@@ -1003,7 +1048,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                         ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
                         int currentThread = currentThreadInterval.getStateValue().unboxInt();
                         if (currentThread > 0) {
-                            int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread), Attributes.STATUS);
+                            int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread));
                             ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
                             if (statusInterval.getStartTime() == time) {
                                 thread = currentThread;
@@ -1130,4 +1175,15 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         }
         return null;
     }
+
+    private static ActiveThreadsFilter getActiveThreadsFilter(ViewerFilter[] filters) {
+        return (ActiveThreadsFilter) Arrays.stream(filters).filter(filter -> filter instanceof ActiveThreadsFilter).findFirst().orElse(null);
+    }
+
+    @Override
+    protected void updateFilters() {
+        super.updateFilters();
+        fActiveThreadsFilter.updateData();
+    }
+
 }
This page took 0.034247 seconds and 5 git commands to generate.