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 5db607983f0e34c69e5f13ef7d66eb88edb06480..fa71123d540655decb6b5c4c018145aeec127366 100644 (file)
  *   Patrick Tasse - Initial API and implementation
  *   Geneviève Bastien - Move code to provide base classes for time graph view
  *   Christian Mansky - Add check active / uncheck inactive buttons
+ *   Mahdi Zolnouri & Samuel Gagnon - Add flat / hierarchical button
  *******************************************************************************/
 
 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.List;
 import java.util.Map;
+import java.util.Set;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+import org.eclipse.core.runtime.jobs.ISchedulingRule;
+import org.eclipse.core.runtime.jobs.Job;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.jface.action.Action;
 import org.eclipse.jface.action.IAction;
 import org.eclipse.jface.action.IMenuManager;
 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;
+import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelTidAspect;
+import org.eclipse.tracecompass.common.core.StreamUtils.StreamFlattener;
 import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
 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;
@@ -42,9 +70,17 @@ import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeExcept
 import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
 import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
 import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
+import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
+import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
+import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
+import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
+import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
+import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.util.Pair;
 import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
@@ -54,10 +90,13 @@ import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
 import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeLinkEvent;
+import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
 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;
 
 /**
@@ -72,20 +111,26 @@ 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$
 
     private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn;
     private static final String TID_COLUMN = Messages.ControlFlowView_tidColumn;
     private static final String PTID_COLUMN = Messages.ControlFlowView_ptidColumn;
     private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn;
-    private static final String TRACE_COLUMN = Messages.ControlFlowView_traceColumn;
+    private static final String INVISIBLE_COLUMN = Messages.ControlFlowView_invisibleColumn;
+    private Action fOptimizationAction;
+
+    private static final String NEXT_EVENT_ICON_PATH = "icons/elcl16/shift_r_edit.gif"; //$NON-NLS-1$
+    private static final String PREV_EVENT_ICON_PATH = "icons/elcl16/shift_l_edit.gif"; //$NON-NLS-1$
 
     private static final String[] COLUMN_NAMES = new String[] {
             PROCESS_COLUMN,
             TID_COLUMN,
             PTID_COLUMN,
-            BIRTH_TIME_COLUMN,
-            TRACE_COLUMN
+            BIRTH_TIME_COLUMN
     };
 
     private static final String[] FILTER_COLUMN_NAMES = new String[] {
@@ -98,6 +143,8 @@ 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 NaiveOptimizationAlgorithm();
+
     private static final int INITIAL_SORT_COLUMN_INDEX = 3;
 
     static {
@@ -105,12 +152,72 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         builder.add(ControlFlowColumnComparators.PROCESS_NAME_COLUMN_COMPARATOR)
             .add(ControlFlowColumnComparators.TID_COLUMN_COMPARATOR)
             .add(ControlFlowColumnComparators.PTID_COLUMN_COMPARATOR)
-            .add(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR)
-            .add(ControlFlowColumnComparators.TRACE_COLUMN_COMPARATOR);
+            .add(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR);
         List<Comparator<ITimeGraphEntry>> l = builder.build();
         COLUMN_COMPARATORS = l.toArray(new Comparator[l.size()]);
     }
 
+    /**
+     * Mutex rule for search action jobs, making sure they execute sequentially
+     */
+    private final ISchedulingRule fSearchActionMutexRule = new ISchedulingRule() {
+        @Override
+        public boolean isConflicting(ISchedulingRule rule) {
+            return (rule == this);
+        }
+
+        @Override
+        public boolean contains(ISchedulingRule rule) {
+            return (rule == this);
+        }
+    };
+
+    private final Set<ITmfTrace> fFlatTraces = new HashSet<>();
+
+    private IAction fFlatAction;
+
+    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
     // ------------------------------------------------------------------------
@@ -155,6 +262,13 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
     @Override
     protected void fillLocalToolBar(IToolBarManager manager) {
+        // add "Optimization" Button to local tool bar of Controlflow
+        IAction optimizationAction = getOptimizationAction();
+        manager.add(optimizationAction);
+
+        // add a separator to local tool bar
+        manager.add(new Separator());
+
         super.fillLocalToolBar(manager);
         IDialogSettings settings = Activator.getDefault().getDialogSettings();
         IDialogSettings section = settings.getSection(getClass().getName());
@@ -174,6 +288,223 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         followArrowFwdAction.setText(Messages.ControlFlowView_followCPUFwdText);
         followArrowFwdAction.setToolTipText(Messages.ControlFlowView_followCPUFwdText);
         manager.add(followArrowFwdAction);
+
+        IAction previousEventAction = new SearchEventAction(false, PackageMessages.ControlFlowView_PreviousEventJobName);
+        previousEventAction.setText(PackageMessages.ControlFlowView_PreviousEventActionName);
+        previousEventAction.setToolTipText(PackageMessages.ControlFlowView_PreviousEventActionTooltip);
+        previousEventAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(PREV_EVENT_ICON_PATH));
+        manager.add(previousEventAction);
+
+        IAction nextEventAction = new SearchEventAction(true, PackageMessages.ControlFlowView_NextEventJobName);
+        nextEventAction.setText(PackageMessages.ControlFlowView_NextEventActionName);
+        nextEventAction.setToolTipText(PackageMessages.ControlFlowView_NextEventActionTooltip);
+        nextEventAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(NEXT_EVENT_ICON_PATH));
+        manager.add(nextEventAction);
+    }
+
+    private IAction getOptimizationAction() {
+        if (fOptimizationAction == null) {
+            fOptimizationAction = new OptimizationAction();
+            fOptimizationAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(OPTIMIZE_ICON));
+            fOptimizationAction.setText(Messages.ControlFlowView_optimizeLabel);
+            fOptimizationAction.setToolTipText(Messages.ControlFlowView_optimizeToolTip);
+        }
+        return fOptimizationAction;
+    }
+
+    @Override
+    protected void fillLocalMenu(IMenuManager manager) {
+        super.fillLocalMenu(manager);
+
+        MenuManager item = new MenuManager(Messages.ControlFlowView_threadPresentation);
+        fFlatAction = createFlatAction();
+        item.add(fFlatAction);
+
+        fHierarchicalAction = createHierarchicalAction();
+        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
+     */
+    private class SearchEventAction extends Action {
+
+        private final boolean ifDirection;
+        private final String ifJobName;
+
+        /**
+         * Constructor
+         *
+         * @param direction
+         *            The direction of the search, "true" for forwards and
+         *            "false" for backwards.
+         * @param jobName
+         *            The name of the job that will be spawned
+         */
+        public SearchEventAction(boolean direction, String jobName) {
+            ifDirection = direction;
+            ifJobName = jobName;
+        }
+
+        @Override
+        public void run() {
+            Job job = new Job(ifJobName) {
+                @Override
+                protected IStatus run(IProgressMonitor monitor) {
+                    TimeGraphControl ctrl = getTimeGraphViewer().getTimeGraphControl();
+                    ITimeGraphEntry traceEntry = ctrl.getSelectedTrace();
+
+                    long ts = getTimeGraphViewer().getSelectionBegin();
+                    ITimeEvent selectedState = Utils.findEvent(traceEntry, ts, 0);
+
+                    if (selectedState == null) {
+                        /* No selection currently in the view, do nothing */
+                        return Status.OK_STATUS;
+                    }
+                    ITimeGraphEntry entry = selectedState.getEntry();
+                    if (!(entry instanceof ControlFlowEntry)) {
+                        return Status.OK_STATUS;
+                    }
+                    ControlFlowEntry cfEntry = (ControlFlowEntry) entry;
+                    int tid = cfEntry.getThreadId();
+
+                    ITmfTrace trace = cfEntry.getTrace();
+                    ITmfContext ctx = trace.seekEvent(TmfTimestamp.fromNanos(ts));
+                    long rank = ctx.getRank();
+                    ctx.dispose();
+
+                    Predicate<@NonNull ITmfEvent> predicate = event -> {
+                        /*
+                         * TODO Specific to the Control Flow View and kernel
+                         * traces for now. Could be eventually generalized to
+                         * anything represented by the time graph row.
+                         */
+                        Integer eventTid = KernelTidAspect.INSTANCE.resolve(event);
+                        return (eventTid != null && eventTid.intValue() == tid);
+                    };
+
+                    ITmfEvent event = (ifDirection ?
+                            TmfTraceUtils.getNextEventMatching(cfEntry.getTrace(), rank, predicate, monitor) :
+                            TmfTraceUtils.getPreviousEventMatching(cfEntry.getTrace(), rank, predicate, monitor));
+                    if (event != null) {
+                        TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, event.getTimestamp()));
+                    }
+                    return Status.OK_STATUS;
+
+                }
+            };
+            /*
+             * Make subsequent jobs not run concurrently, but wait after one
+             * another.
+             */
+            job.setRule(fSearchActionMutexRule);
+            job.schedule();
+        }
+    }
+
+    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
+            public void run() {
+                ITmfTrace parentTrace = getTrace();
+                synchronized (fFlatTraces) {
+                    fFlatTraces.remove(parentTrace);
+                    for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
+                        final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
+                        for (TimeGraphEntry traceEntry : getEntryList(ss)) {
+                            List<ControlFlowEntry> currentRootList = traceEntry.getChildren().stream()
+                                    .filter(e -> e instanceof ControlFlowEntry)
+                                    .map(e -> (ControlFlowEntry) e)
+                                    .collect(Collectors.toList());
+                            addEntriesToHierarchicalTree(currentRootList, traceEntry);
+                        }
+                    }
+                }
+                refresh();
+            }
+        };
+        action.setChecked(true);
+        action.setToolTipText(Messages.ControlFlowView_hierarchicalViewToolTip);
+        return action;
+    }
+
+    private IAction createFlatAction() {
+        IAction action = new Action(Messages.ControlFlowView_flatViewLabel, IAction.AS_RADIO_BUTTON) {
+            @Override
+            public void run() {
+                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
@@ -196,6 +527,81 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         return Messages.ControlFlowView_previousProcessActionToolTipText;
     }
 
+    /**
+     * Get the optimization function for the scheduling column. In the base
+     * implementation, this optimizes by Line arrows, but can be overidden.
+     * <p>
+     * It takes a collection of link events, looking at the entries being
+     * linked, and returns a list of the proposed order. The list of indexes
+     * should be in ascending order. There can be duplicates, but the values and
+     * order should always be the same for the same input.
+     *
+     * @return the returned column order, where the integer is the tid of the
+     *         entry, and the return value is the position, there can be
+     *         duplicates.
+     */
+    public Function<Collection<ILinkEvent>, Map<Integer, Long>> getUpdatedSchedulingColumn() {
+        return UPDATE_SCHEDULING_COLUMN_ALGO;
+    }
+
+    /**
+     * This is an optimization action used to find cliques of entries due to
+     * links and put them closer together
+     *
+     * @author Samuel Gagnon
+     */
+    private final class OptimizationAction extends Action {
+
+        @Override
+        public void runWithEvent(Event event) {
+            ITmfTrace trace = getTrace();
+            if (trace == null) {
+                return;
+            }
+
+            createFlatAction().run();
+
+            /*
+             * This method only returns the arrows in the current time interval
+             * [a,b] of ControlFlowView. Thus, we only optimize for that time
+             * interval
+             */
+            List<ILinkEvent> arrows = getTimeGraphViewer().getTimeGraphControl().getArrows();
+            final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
+            List<TimeGraphEntry> currentList = getEntryList(ss);
+
+            Map<Integer, Long> orderedTidMap = getUpdatedSchedulingColumn().apply(arrows);
+
+            /*
+             * Now that we have our list of ordered tid, it's time to assign a
+             * position for each threads in the view. For this, we assign a
+             * value to an invisible column and sort according to the values in
+             * this column.
+             */
+            for (TimeGraphEntry entry : currentList) {
+                if (entry instanceof TraceEntry) {
+                    for (TimeGraphEntry child : ((TraceEntry) entry).getChildren()) {
+                        if (child instanceof ControlFlowEntry) {
+                            ControlFlowEntry cEntry = (ControlFlowEntry) child;
+                            /*
+                             * If the thread is in our list, we give it a
+                             * position. Otherwise, it means there's no activity
+                             * in the current interval for that thread. We set
+                             * its position to Long.MAX_VALUE so it goes to the
+                             * bottom.
+                             */
+                            cEntry.setSchedulingPosition(orderedTidMap.getOrDefault(cEntry.getThreadId(), Long.MAX_VALUE));
+                        }
+                    }
+                }
+            }
+
+            setEntryComparator(ControlFlowColumnComparators.SCHEDULING_COLUMN_COMPARATOR);
+            refresh();
+        }
+
+    }
+
     /**
      * @author gbastien
      *
@@ -204,6 +610,12 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
         @Override
         public String getColumnText(Object element, int columnIndex) {
+            if (element instanceof TraceEntry) {
+                if (columnIndex == 0) {
+                    return ((TraceEntry) element).getName();
+                }
+                return ""; //$NON-NLS-1$
+            }
             ControlFlowEntry entry = (ControlFlowEntry) element;
 
             if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_processColumn)) {
@@ -218,6 +630,8 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                 return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC);
             } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_traceColumn)) {
                 return entry.getTrace().getName();
+            } else if (COLUMN_NAMES[columnIndex].equals(INVISIBLE_COLUMN)) {
+                return Long.toString(entry.getSchedulingPosition());
             }
             return ""; //$NON-NLS-1$
         }
@@ -228,6 +642,12 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
         @Override
         public String getColumnText(Object element, int columnIndex) {
+            if (element instanceof TraceEntry) {
+                if (columnIndex == 0) {
+                    return ((TraceEntry) element).getName();
+                }
+                return ""; //$NON-NLS-1$
+            }
             ControlFlowEntry entry = (ControlFlowEntry) element;
 
             if (columnIndex == 0) {
@@ -240,6 +660,59 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
     }
 
+    private static class TraceEntry extends TimeGraphEntry {
+
+        public TraceEntry(String name, long startTime, long endTime) {
+            super(name, startTime, endTime);
+        }
+
+        @Override
+        public boolean hasTimeEvents() {
+            return false;
+        }
+    }
+
+    @TmfSignalHandler
+    @Override
+    public void traceClosed(org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal signal) {
+        super.traceClosed(signal);
+        synchronized (fFlatTraces) {
+            fFlatTraces.remove(signal.getTrace());
+        }
+    }
+
+    @TmfSignalHandler
+    @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);
+                fFlatAction.setChecked(true);
+            } else {
+                fFlatAction.setChecked(false);
+                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());
+    }
+
     // ------------------------------------------------------------------------
     // Internal
     // ------------------------------------------------------------------------
@@ -252,7 +725,9 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         }
 
         final List<ControlFlowEntry> entryList = new ArrayList<>();
-        /** Map of view entries, key is a pair [threadId, cpuId] */
+        /** Map of trace entries */
+        Map<ITmfTrace, TraceEntry> traceEntryMap = new HashMap<>();
+        /** Map of control flow entries, key is a pair [threadId, cpuId] */
         final Map<Pair<Integer, Integer>, ControlFlowEntry> entryMap = new HashMap<>();
 
         long start = ssq.getStartTime();
@@ -271,6 +746,17 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
             if (start == end && !complete) { // when complete execute one last time regardless of end time
                 continue;
             }
+
+            TraceEntry aTraceEntry = traceEntryMap.get(trace);
+            if (aTraceEntry == null) {
+                aTraceEntry = new TraceEntry(trace.getName(), start, end + 1);
+                traceEntryMap.put(trace, aTraceEntry);
+                addToEntryList(parentTrace, ssq, Collections.singletonList(aTraceEntry));
+            } else {
+                aTraceEntry.updateEndTime(end + 1);
+            }
+            final TraceEntry traceEntry = aTraceEntry;
+
             final long resolution = Math.max(1, (end - ssq.getStartTime()) / getDisplayWidth());
             setEndTime(Math.max(getEndTime(), end + 1));
             final List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
@@ -287,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;
@@ -309,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();
@@ -323,11 +806,9 @@ 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 (AttributeNotFoundException e) {
-                                    Activator.getDefault().logError(e.getMessage());
                                 } catch (StateSystemDisposedException e) {
                                     /* ignored */
                                 }
@@ -335,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);
@@ -357,17 +838,23 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                             }
                             lastExecNameStartTime = startTime;
                             lastExecNameEndTime = endTime;
-                            lastPpidStartTime = ppidInterval.getStartTime();
+                            lastPpidStartTime = ppidInterval == null ? -1 : ppidInterval.getStartTime();
+                        }
+                    }
+                    synchronized (fFlatTraces) {
+                        if (fFlatTraces.contains(parentTrace)) {
+                            addEntriesToFlatTree(entryList, traceEntry);
+                        } else {
+                            addEntriesToHierarchicalTree(entryList, traceEntry);
                         }
                     }
-                    updateTree(entryList, parentTrace, ssq);
                 }
             });
 
             queryFullStates(ssq, ssq.getStartTime(), end, resolution, monitor, new IQueryHandler() {
                 @Override
                 public void handle(@NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState) {
-                    for (final TimeGraphEntry entry : getEntryList(ssq)) {
+                    for (final TimeGraphEntry entry : traceEntry.getChildren()) {
                         if (monitor.isCanceled()) {
                             return;
                         }
@@ -384,13 +871,50 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         }
     }
 
-    private void updateTree(List<ControlFlowEntry> entryList, ITmfTrace parentTrace, ITmfStateSystem ss) {
-        List<TimeGraphEntry> rootListToAdd = new ArrayList<>();
-        List<TimeGraphEntry> rootListToRemove = new ArrayList<>();
-        List<TimeGraphEntry> rootList = getEntryList(ss);
+    /**
+     * Add entries to the traces's child list in a flat fashion (no hierarchy).
+     * If one entry has children, we do a depth first search to add each child
+     * to the trace's child list and update the parent and child relations.
+     */
+    private static void hierarchicalToFlatTree(TimeGraphEntry traceEntry) {
+        List<@NonNull TimeGraphEntry> rootList = traceEntry.getChildren();
+        // We visit the children of every entry to add
+        StreamFlattener<TimeGraphEntry> sf = new StreamFlattener<>(entry -> entry.getChildren().stream());
+        Stream<TimeGraphEntry> allEntries = rootList.stream().flatMap(entry -> sf.flatten(entry));
+
+        // We add every entry that is missing from the trace's entry list
+        List<@NonNull TimeGraphEntry> rootListToAdd = allEntries
+                .filter(entry -> !rootList.contains(entry))
+                .collect(Collectors.toList());
+        rootList.forEach(entry -> {
+            entry.clearChildren();
+        });
+        rootListToAdd.forEach(entry -> {
+            traceEntry.addChild(entry);
+            entry.clearChildren();
+        });
+    }
+
+    /**
+     * Add entries to the traces's child list in a flat fashion (no hierarchy).
+     */
+    private static void addEntriesToFlatTree(List<@NonNull ControlFlowEntry> entryList, TimeGraphEntry traceEntry) {
+        List<TimeGraphEntry> rootList = traceEntry.getChildren();
+        for (ControlFlowEntry entry : entryList) {
+            if (!rootList.contains(entry)) {
+                traceEntry.addChild(entry);
+            }
+        }
+    }
+
+    /**
+     * Add entries to the trace's child list in a hierarchical fashion.
+     */
+    private static void addEntriesToHierarchicalTree(List<ControlFlowEntry> entryList, TimeGraphEntry traceEntry) {
+        List<TimeGraphEntry> rootList = traceEntry.getChildren();
 
         for (ControlFlowEntry entry : entryList) {
-            boolean root = (entry.getParent() == null);
+            boolean root = (entry.getParent() == null || entry.getParent() == traceEntry);
             if (root && entry.getParentThreadId() > 0) {
                 for (ControlFlowEntry parent : entryList) {
                     /*
@@ -404,22 +928,19 @@ 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 != null && rootList.contains(entry)) {
-                            rootListToRemove.add(entry);
+                        if (rootList.contains(entry)) {
+                            traceEntry.removeChild(entry);
                         }
+                        parent.addChild(entry);
                         break;
                     }
                 }
             }
-            if (root && (rootList == null || !rootList.contains(entry))) {
-                rootListToAdd.add(entry);
+            if (root && (!rootList.contains(entry))) {
+                traceEntry.addChild(entry);
             }
         }
-
-        addToEntryList(parentTrace, ss, rootListToAdd);
-        removeFromEntryList(parentTrace, ss, rootListToRemove);
     }
 
     private void buildStatusEvents(ITmfTrace trace, ITmfTrace parentTrace, ITmfStateSystem ss, @NonNull List<List<ITmfStateInterval>> fullStates,
@@ -458,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();
@@ -495,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;
@@ -528,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;
@@ -601,9 +1121,6 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                         long end = Math.min(currentThreadInterval.getEndTime() + 1, ss.getCurrentEndTime());
                         currentThreadIntervals.add(ss.querySingleState(end, currentThreadQuark));
                     }
-                } catch (AttributeNotFoundException e) {
-                    Activator.getDefault().logError(e.getMessage());
-                    return list;
                 } catch (StateSystemDisposedException e) {
                     /* Ignored */
                     return list;
@@ -641,20 +1158,32 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
         return list;
     }
 
-    private ControlFlowEntry findEntry(List<? extends ITimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
-        for (ITimeGraphEntry entry : entryList) {
+    private ControlFlowEntry findEntry(List<TimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
+        for (TimeGraphEntry entry : entryList) {
             if (entry instanceof ControlFlowEntry) {
                 ControlFlowEntry controlFlowEntry = (ControlFlowEntry) entry;
                 if (controlFlowEntry.getThreadId() == threadId && controlFlowEntry.getTrace() == trace) {
                     return controlFlowEntry;
-                } else if (entry.hasChildren()) {
-                    controlFlowEntry = findEntry(entry.getChildren(), trace, threadId);
-                    if (controlFlowEntry != null) {
-                        return controlFlowEntry;
-                    }
+                }
+            }
+            if (entry.hasChildren()) {
+                ControlFlowEntry controlFlowEntry = findEntry(entry.getChildren(), trace, threadId);
+                if (controlFlowEntry != null) {
+                    return controlFlowEntry;
                 }
             }
         }
         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.051773 seconds and 5 git commands to generate.