lttng: Add Next/Previous TID event action in CFV
[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 9a63d869a7ca9ce7f9ef75010d80b43dfdcb3dec..adb293bfc8d4270640004772965d291c89261a3b 100644 (file)
 package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow;
 
 import java.util.ArrayList;
+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.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.Job;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 import org.eclipse.jface.action.Action;
@@ -38,6 +43,7 @@ import org.eclipse.jface.viewers.ISelection;
 import org.eclipse.jface.viewers.StructuredSelection;
 import org.eclipse.swt.widgets.Composite;
 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;
@@ -50,11 +56,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.ITmfTimestamp;
+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;
@@ -64,6 +76,7 @@ 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;
@@ -88,14 +101,15 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
     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 NEXT_EVENT_ICON_PATH = "icons/obj16/shift_r_edit.gif"; //$NON-NLS-1$
+    private static final String PREV_EVENT_ICON_PATH = "icons/obj16/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[] {
@@ -115,8 +129,7 @@ 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()]);
     }
@@ -190,6 +203,18 @@ 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);
     }
 
     @Override
@@ -205,6 +230,72 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
 
     }
 
+    /**
+     * 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() {
+            TimeGraphControl ctrl = getTimeGraphViewer().getTimeGraphControl();
+
+            ITimeGraphEntry traceEntry = ctrl.getSelectedTrace();
+            long ts = ctrl.getTimeDataProvider().getSelectionBegin();
+            ITmfTimestamp ts2 = TmfTraceManager.getInstance().getCurrentTraceContext().getSelectionRange().getStartTime();
+            ITimeEvent selectedState = Utils.findEvent(traceEntry, ts, 0);
+
+            if (selectedState == null) {
+                return;
+            }
+            ITimeGraphEntry entry = selectedState.getEntry();
+            if (!(entry instanceof ControlFlowEntry)) {
+                return;
+            }
+            ControlFlowEntry cfEntry = (ControlFlowEntry) entry;
+            int tid = cfEntry.getThreadId();
+
+            Job job = new Job(ifJobName) {
+                @Override
+                protected IStatus run(IProgressMonitor monitor) {
+                    ITmfTrace trace = cfEntry.getTrace();
+                    ITmfContext ctx = trace.seekEvent(ts2);
+                    long rank = ctx.getRank();
+
+                    Predicate<@NonNull ITmfEvent> predicate = event -> {
+                        Integer eventTid = KernelTidAspect.INSTANCE.resolve(event);
+                        return (eventTid != null && eventTid.intValue() == tid);
+                    };
+
+                    ITmfEvent event = (ifDirection ?
+                            TmfTraceUtils.getNextEventMatching(cfEntry.getTrace(), rank, predicate) :
+                            TmfTraceUtils.getPreviousEventMatching(cfEntry.getTrace(), rank, predicate));
+                    if (event != null) {
+                        TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, event.getTimestamp()));
+                    }
+                    return Status.OK_STATUS;
+                }
+            };
+            job.schedule();
+        }
+    }
+
     private IAction createHierarchicalAction() {
         IAction action = new Action(Messages.ControlFlowView_hierarchicalViewLabel, IAction.AS_RADIO_BUTTON) {
             @Override
@@ -214,8 +305,12 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                     fFlatTraces.remove(parentTrace);
                     for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
                         final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
-                        List<ControlFlowEntry> currentRootList = getEntryList(ss).stream().filter(e -> e instanceof ControlFlowEntry).map(e -> (ControlFlowEntry) e).collect(Collectors.toList());
-                        addEntriesToHierarchicalTree(currentRootList, parentTrace, ss);
+                        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();
@@ -235,7 +330,9 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                     fFlatTraces.add(parentTrace);
                     for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
                         final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
-                        hierarchicalToFlatTree(parentTrace, ss);
+                        for (TimeGraphEntry traceEntry : getEntryList(ss)) {
+                            hierarchicalToFlatTree(traceEntry);
+                        }
                     }
                 }
                 refresh();
@@ -273,6 +370,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)) {
@@ -285,8 +388,6 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                 }
             } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_birthTimeColumn)) {
                 return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC);
-            } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_traceColumn)) {
-                return entry.getTrace().getName();
             }
             return ""; //$NON-NLS-1$
         }
@@ -297,6 +398,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) {
@@ -309,6 +416,18 @@ 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) {
@@ -345,7 +464,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();
@@ -364,6 +485,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$
@@ -419,8 +551,6 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                                     ppidInterval = ssq.querySingleState(startTime - 1, ppidQuark);
                                     startTime = execNameInterval.getStartTime();
                                     endTime = execNameInterval.getEndTime() + 1;
-                                } catch (AttributeNotFoundException e) {
-                                    Activator.getDefault().logError(e.getMessage());
                                 } catch (StateSystemDisposedException e) {
                                     /* ignored */
                                 }
@@ -455,9 +585,9 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                     }
                     synchronized (fFlatTraces) {
                         if (fFlatTraces.contains(parentTrace)) {
-                            addEntriesToFlatTree(entryList, parentTrace, ssq);
+                            addEntriesToFlatTree(entryList, traceEntry);
                         } else {
-                            addEntriesToHierarchicalTree(entryList, parentTrace, ssq);
+                            addEntriesToHierarchicalTree(entryList, traceEntry);
                         }
                     }
                 }
@@ -466,7 +596,7 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
             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;
                         }
@@ -484,12 +614,12 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
     }
 
     /**
-     * Add entries to the traces's entry list in a flat fashion (no hierarchy).
+     * 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 entry list and update the parent and child relations.
+     * to the trace's child list and update the parent and child relations.
      */
-    private void hierarchicalToFlatTree(ITmfTrace parentTrace, ITmfStateSystem ss) {
-        List<@NonNull TimeGraphEntry> rootList = getEntryList(ss);
+    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));
@@ -502,33 +632,31 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
             entry.clearChildren();
         });
         rootListToAdd.forEach(entry -> {
-            entry.setParent(null);
+            traceEntry.addChild(entry);
             entry.clearChildren();
         });
-        addToEntryList(parentTrace, ss, rootListToAdd);
     }
 
     /**
-     * Add entries to the traces's entry list in a flat fashion (no hierarchy).
+     * Add entries to the traces's child list in a flat fashion (no hierarchy).
      */
-    private void addEntriesToFlatTree(List<@NonNull ControlFlowEntry> entryList, ITmfTrace parentTrace, ITmfStateSystem ss) {
-        List<TimeGraphEntry> rootList = getEntryList(ss);
-        List<@NonNull TimeGraphEntry> rootListToAdd = entryList.stream()
-                .filter(entry -> !rootList.contains(entry))
-                .collect(Collectors.toList());
-        addToEntryList(parentTrace, ss, rootListToAdd);
+    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 entry list in a hierarchical fashion.
+     * Add entries to the trace's child list in a hierarchical fashion.
      */
-    private void addEntriesToHierarchicalTree(List<ControlFlowEntry> entryList, ITmfTrace parentTrace, ITmfStateSystem ss) {
-        List<@NonNull TimeGraphEntry> rootListToAdd = new ArrayList<>();
-        List<TimeGraphEntry> rootListToRemove = new ArrayList<>();
-        List<TimeGraphEntry> rootList = getEntryList(ss);
+    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) {
                     /*
@@ -544,20 +672,17 @@ public class ControlFlowView extends AbstractStateSystemTimeGraphView {
                             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);
                         }
                         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,
@@ -739,9 +864,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;
@@ -779,17 +901,18 @@ 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;
                 }
             }
         }
This page took 0.040421 seconds and 5 git commands to generate.