tmf: Bug 496504: Fix duplicate child entries in Control Flow view
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.os.linux.ui / src / org / eclipse / tracecompass / internal / analysis / os / linux / ui / views / controlflow / ControlFlowView.java
CommitLineData
6151d86c 1/*******************************************************************************
f8f46a52 2 * Copyright (c) 2012, 2016 Ericsson, École Polytechnique de Montréal and others.
6151d86c
PT
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
4999a196 11 * Geneviève Bastien - Move code to provide base classes for time graph view
b97d61f0 12 * Christian Mansky - Add check active / uncheck inactive buttons
bf415887 13 * Mahdi Zolnouri & Samuel Gagnon - Add flat / hierarchical button
6151d86c
PT
14 *******************************************************************************/
15
ace7140d 16package org.eclipse.tracecompass.internal.analysis.os.linux.ui.views.controlflow;
e363eae1 17
6151d86c 18import java.util.ArrayList;
0267cc9d 19import java.util.Collection;
3553c912 20import java.util.Collections;
6151d86c 21import java.util.Comparator;
1cf25311 22import java.util.HashMap;
bf415887 23import java.util.HashSet;
6151d86c 24import java.util.List;
1cf25311 25import java.util.Map;
bf415887 26import java.util.Set;
0267cc9d 27import java.util.function.Function;
fb023ce6 28import java.util.function.Predicate;
bf415887
MZ
29import java.util.stream.Collectors;
30import java.util.stream.Stream;
6151d86c
PT
31
32import org.eclipse.core.runtime.IProgressMonitor;
fb023ce6
AM
33import org.eclipse.core.runtime.IStatus;
34import org.eclipse.core.runtime.Status;
35import org.eclipse.core.runtime.jobs.ISchedulingRule;
36import org.eclipse.core.runtime.jobs.Job;
dfa0ef96 37import org.eclipse.jdt.annotation.NonNull;
d2120fb6 38import org.eclipse.jdt.annotation.Nullable;
bf415887 39import org.eclipse.jface.action.Action;
03ab8eeb 40import org.eclipse.jface.action.IAction;
90bb3a0c 41import org.eclipse.jface.action.IMenuManager;
79ec0b89 42import org.eclipse.jface.action.IToolBarManager;
bf415887 43import org.eclipse.jface.action.MenuManager;
d13d217e 44import org.eclipse.jface.action.Separator;
79ec0b89 45import org.eclipse.jface.dialogs.IDialogSettings;
90bb3a0c
BH
46import org.eclipse.jface.viewers.ISelection;
47import org.eclipse.jface.viewers.StructuredSelection;
b97d61f0 48import org.eclipse.swt.widgets.Composite;
d13d217e 49import org.eclipse.swt.widgets.Event;
0f7a12d3 50import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelAnalysisModule;
fb023ce6 51import org.eclipse.tracecompass.analysis.os.linux.core.kernel.KernelTidAspect;
bf415887 52import org.eclipse.tracecompass.common.core.StreamUtils.StreamFlattener;
f69045e2 53import org.eclipse.tracecompass.internal.analysis.os.linux.core.kernel.Attributes;
e363eae1
AM
54import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Activator;
55import org.eclipse.tracecompass.internal.analysis.os.linux.ui.Messages;
9620ac26 56import org.eclipse.tracecompass.internal.analysis.os.linux.ui.actions.FollowThreadAction;
e894a508
AM
57import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
58import org.eclipse.tracecompass.statesystem.core.exceptions.AttributeNotFoundException;
59import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
60import org.eclipse.tracecompass.statesystem.core.exceptions.StateValueTypeException;
61import org.eclipse.tracecompass.statesystem.core.exceptions.TimeRangeException;
62import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
63import org.eclipse.tracecompass.statesystem.core.statevalue.ITmfStateValue;
fb023ce6
AM
64import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
65import org.eclipse.tracecompass.tmf.core.signal.TmfSelectionRangeUpdatedSignal;
bf415887 66import org.eclipse.tracecompass.tmf.core.signal.TmfSignalHandler;
fb023ce6 67import org.eclipse.tracecompass.tmf.core.signal.TmfSignalManager;
bf415887 68import org.eclipse.tracecompass.tmf.core.signal.TmfTraceSelectedSignal;
2bdf0193 69import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
fb023ce6
AM
70import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
71import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
2bdf0193
AM
72import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
73import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
fb023ce6 74import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
8a0bbebf 75import org.eclipse.tracecompass.tmf.core.util.Pair;
8321a699 76import org.eclipse.tracecompass.tmf.ui.views.timegraph.AbstractStateSystemTimeGraphView;
2bdf0193
AM
77import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ILinkEvent;
78import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeEvent;
79import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.ITimeGraphEntry;
8321a699 80import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.NullTimeEvent;
2bdf0193
AM
81import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeEvent;
82import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeGraphEntry;
83import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.model.TimeLinkEvent;
fb023ce6 84import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.TimeGraphControl;
2bdf0193
AM
85import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils;
86import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.Resolution;
87import org.eclipse.tracecompass.tmf.ui.widgets.timegraph.widgets.Utils.TimeFormat;
6151d86c 88
a4cddcbc
BH
89import com.google.common.collect.ImmutableList;
90
6151d86c
PT
91/**
92 * The Control Flow view main object
93 *
94 */
8321a699 95public class ControlFlowView extends AbstractStateSystemTimeGraphView {
6151d86c
PT
96
97 // ------------------------------------------------------------------------
98 // Constants
99 // ------------------------------------------------------------------------
6151d86c
PT
100 /**
101 * View ID.
102 */
143e0680 103 public static final @NonNull String ID = "org.eclipse.tracecompass.analysis.os.linux.views.controlflow"; //$NON-NLS-1$
6151d86c 104
d13d217e
SG
105 private static final String ICONS_PATH = "icons/"; //$NON-NLS-1$
106 private static final String OPTIMIZE_ICON = ICONS_PATH + "elcl16/Optimization.png"; //$NON-NLS-1$
107
4999a196
GB
108 private static final String PROCESS_COLUMN = Messages.ControlFlowView_processColumn;
109 private static final String TID_COLUMN = Messages.ControlFlowView_tidColumn;
110 private static final String PTID_COLUMN = Messages.ControlFlowView_ptidColumn;
6151d86c 111 private static final String BIRTH_TIME_COLUMN = Messages.ControlFlowView_birthTimeColumn;
d13d217e
SG
112 private static final String INVISIBLE_COLUMN = Messages.ControlFlowView_invisibleColumn;
113 private Action fOptimizationAction;
6151d86c 114
fb023ce6
AM
115 private static final String NEXT_EVENT_ICON_PATH = "icons/elcl16/shift_r_edit.gif"; //$NON-NLS-1$
116 private static final String PREV_EVENT_ICON_PATH = "icons/elcl16/shift_l_edit.gif"; //$NON-NLS-1$
117
4999a196 118 private static final String[] COLUMN_NAMES = new String[] {
6151d86c
PT
119 PROCESS_COLUMN,
120 TID_COLUMN,
121 PTID_COLUMN,
3553c912 122 BIRTH_TIME_COLUMN
6151d86c
PT
123 };
124
4999a196 125 private static final String[] FILTER_COLUMN_NAMES = new String[] {
6ac5a950
AM
126 PROCESS_COLUMN,
127 TID_COLUMN
128 };
129
aae89862
PT
130 // Timeout between updates in the build thread in ms
131 private static final long BUILD_UPDATE_TIMEOUT = 500;
132
a4cddcbc
BH
133 private static final Comparator<ITimeGraphEntry>[] COLUMN_COMPARATORS;
134
97ab652e 135 private final Function<Collection<ILinkEvent>, Map<Integer, Long>> UPDATE_SCHEDULING_COLUMN_ALGO = new NaiveOptimizationAlgorithm();
0267cc9d 136
a14d7bd5
BH
137 private static final int INITIAL_SORT_COLUMN_INDEX = 3;
138
a4cddcbc
BH
139 static {
140 ImmutableList.Builder<Comparator<ITimeGraphEntry>> builder = ImmutableList.builder();
141 builder.add(ControlFlowColumnComparators.PROCESS_NAME_COLUMN_COMPARATOR)
9620ac26
MK
142 .add(ControlFlowColumnComparators.TID_COLUMN_COMPARATOR)
143 .add(ControlFlowColumnComparators.PTID_COLUMN_COMPARATOR)
3553c912 144 .add(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR);
a4cddcbc
BH
145 List<Comparator<ITimeGraphEntry>> l = builder.build();
146 COLUMN_COMPARATORS = l.toArray(new Comparator[l.size()]);
147 }
148
fb023ce6
AM
149 /**
150 * Mutex rule for search action jobs, making sure they execute sequentially
151 */
152 private final ISchedulingRule fSearchActionMutexRule = new ISchedulingRule() {
153 @Override
154 public boolean isConflicting(ISchedulingRule rule) {
155 return (rule == this);
156 }
0267cc9d 157
fb023ce6
AM
158 @Override
159 public boolean contains(ISchedulingRule rule) {
160 return (rule == this);
161 }
162 };
163
bf415887
MZ
164 private final Set<ITmfTrace> fFlatTraces = new HashSet<>();
165
166 private IAction fFlatAction;
167
168 private IAction fHierarchicalAction;
169
6151d86c 170 // ------------------------------------------------------------------------
4999a196 171 // Constructors
6151d86c
PT
172 // ------------------------------------------------------------------------
173
4999a196
GB
174 /**
175 * Constructor
176 */
177 public ControlFlowView() {
747adf5c 178 super(ID, new ControlFlowPresentationProvider());
a14d7bd5 179 setTreeColumns(COLUMN_NAMES, COLUMN_COMPARATORS, INITIAL_SORT_COLUMN_INDEX);
4999a196 180 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
747adf5c 181 setFilterColumns(FILTER_COLUMN_NAMES);
a03b7ee4 182 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
a4cddcbc 183 setEntryComparator(ControlFlowColumnComparators.BIRTH_TIME_COLUMN_COMPARATOR);
6151d86c
PT
184 }
185
b97d61f0
CM
186 @Override
187 public void createPartControl(Composite parent) {
188 super.createPartControl(parent);
189 // add "Check active" Button to TimeGraphFilterDialog
190 super.getTimeGraphCombo().addTimeGraphFilterCheckActiveButton(
191 new ControlFlowCheckActiveProvider(Messages.ControlFlowView_checkActiveLabel, Messages.ControlFlowView_checkActiveToolTip));
192 // add "Uncheck inactive" Button to TimeGraphFilterDialog
193 super.getTimeGraphCombo().addTimeGraphFilterUncheckInactiveButton(
194 new ControlFlowCheckActiveProvider(Messages.ControlFlowView_uncheckInactiveLabel, Messages.ControlFlowView_uncheckInactiveToolTip));
9620ac26
MK
195 }
196
90bb3a0c
BH
197 /**
198 * @since 2.0
199 */
200 @Override
201 protected void fillTimeGraphEntryContextMenu(@NonNull IMenuManager menuManager) {
202 ISelection selection = getSite().getSelectionProvider().getSelection();
203 if (selection instanceof StructuredSelection) {
204 StructuredSelection sSel = (StructuredSelection) selection;
205 if (sSel.getFirstElement() instanceof ControlFlowEntry) {
206 ControlFlowEntry entry = (ControlFlowEntry) sSel.getFirstElement();
207 menuManager.add(new FollowThreadAction(ControlFlowView.this, entry.getName(), entry.getThreadId(), entry.getTrace()));
9620ac26 208 }
90bb3a0c 209 }
b97d61f0
CM
210 }
211
79ec0b89
PT
212 @Override
213 protected void fillLocalToolBar(IToolBarManager manager) {
d13d217e
SG
214 // add "Optimization" Button to local tool bar of Controlflow
215 IAction optimizationAction = getOptimizationAction();
216 manager.add(optimizationAction);
217
218 // add a separator to local tool bar
219 manager.add(new Separator());
220
086f21ae 221 super.fillLocalToolBar(manager);
79ec0b89
PT
222 IDialogSettings settings = Activator.getDefault().getDialogSettings();
223 IDialogSettings section = settings.getSection(getClass().getName());
224 if (section == null) {
225 section = settings.addNewSection(getClass().getName());
226 }
03ab8eeb
PT
227
228 IAction hideArrowsAction = getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section);
229 manager.add(hideArrowsAction);
230
231 IAction followArrowBwdAction = getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
232 followArrowBwdAction.setText(Messages.ControlFlowView_followCPUBwdText);
233 followArrowBwdAction.setToolTipText(Messages.ControlFlowView_followCPUBwdText);
234 manager.add(followArrowBwdAction);
235
236 IAction followArrowFwdAction = getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
237 followArrowFwdAction.setText(Messages.ControlFlowView_followCPUFwdText);
238 followArrowFwdAction.setToolTipText(Messages.ControlFlowView_followCPUFwdText);
239 manager.add(followArrowFwdAction);
fb023ce6
AM
240
241 IAction previousEventAction = new SearchEventAction(false, PackageMessages.ControlFlowView_PreviousEventJobName);
242 previousEventAction.setText(PackageMessages.ControlFlowView_PreviousEventActionName);
243 previousEventAction.setToolTipText(PackageMessages.ControlFlowView_PreviousEventActionTooltip);
244 previousEventAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(PREV_EVENT_ICON_PATH));
245 manager.add(previousEventAction);
246
247 IAction nextEventAction = new SearchEventAction(true, PackageMessages.ControlFlowView_NextEventJobName);
248 nextEventAction.setText(PackageMessages.ControlFlowView_NextEventActionName);
249 nextEventAction.setToolTipText(PackageMessages.ControlFlowView_NextEventActionTooltip);
250 nextEventAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(NEXT_EVENT_ICON_PATH));
251 manager.add(nextEventAction);
79ec0b89
PT
252 }
253
d13d217e
SG
254 private IAction getOptimizationAction() {
255 if (fOptimizationAction == null) {
256 fOptimizationAction = new OptimizationAction();
257 fOptimizationAction.setImageDescriptor(Activator.getDefault().getImageDescripterFromPath(OPTIMIZE_ICON));
258 fOptimizationAction.setText(Messages.ControlFlowView_optimizeLabel);
259 fOptimizationAction.setToolTipText(Messages.ControlFlowView_optimizeToolTip);
260 }
261 return fOptimizationAction;
262 }
263
bf415887
MZ
264 @Override
265 protected void fillLocalMenu(IMenuManager manager) {
266 super.fillLocalMenu(manager);
267 final MenuManager item = new MenuManager(Messages.ControlFlowView_threadPresentation);
268 fFlatAction = createFlatAction();
269 item.add(fFlatAction);
270
271 fHierarchicalAction = createHierarchicalAction();
272 item.add(fHierarchicalAction);
273 manager.add(item);
274
275 }
276
fb023ce6
AM
277 /**
278 * Base Action for the "Go to Next/Previous Event for thread" actions
279 */
280 private class SearchEventAction extends Action {
281
282 private final boolean ifDirection;
283 private final String ifJobName;
284
285 /**
286 * Constructor
287 *
288 * @param direction
289 * The direction of the search, "true" for forwards and
290 * "false" for backwards.
291 * @param jobName
292 * The name of the job that will be spawned
293 */
294 public SearchEventAction(boolean direction, String jobName) {
295 ifDirection = direction;
296 ifJobName = jobName;
297 }
298
299 @Override
300 public void run() {
301 Job job = new Job(ifJobName) {
302 @Override
303 protected IStatus run(IProgressMonitor monitor) {
304 TimeGraphControl ctrl = getTimeGraphViewer().getTimeGraphControl();
305 ITimeGraphEntry traceEntry = ctrl.getSelectedTrace();
306
307 long ts = getTimeGraphViewer().getSelectionBegin();
308 ITimeEvent selectedState = Utils.findEvent(traceEntry, ts, 0);
309
310 if (selectedState == null) {
311 /* No selection currently in the view, do nothing */
312 return Status.OK_STATUS;
313 }
314 ITimeGraphEntry entry = selectedState.getEntry();
315 if (!(entry instanceof ControlFlowEntry)) {
316 return Status.OK_STATUS;
317 }
318 ControlFlowEntry cfEntry = (ControlFlowEntry) entry;
319 int tid = cfEntry.getThreadId();
320
321 ITmfTrace trace = cfEntry.getTrace();
322 ITmfContext ctx = trace.seekEvent(TmfTimestamp.fromNanos(ts));
323 long rank = ctx.getRank();
324 ctx.dispose();
325
326 Predicate<@NonNull ITmfEvent> predicate = event -> {
327 /*
328 * TODO Specific to the Control Flow View and kernel
329 * traces for now. Could be eventually generalized to
330 * anything represented by the time graph row.
331 */
332 Integer eventTid = KernelTidAspect.INSTANCE.resolve(event);
333 return (eventTid != null && eventTid.intValue() == tid);
334 };
335
336 ITmfEvent event = (ifDirection ?
337 TmfTraceUtils.getNextEventMatching(cfEntry.getTrace(), rank, predicate, monitor) :
338 TmfTraceUtils.getPreviousEventMatching(cfEntry.getTrace(), rank, predicate, monitor));
339 if (event != null) {
340 TmfSignalManager.dispatchSignal(new TmfSelectionRangeUpdatedSignal(this, event.getTimestamp()));
341 }
342 return Status.OK_STATUS;
343
344 }
345 };
346 /*
347 * Make subsequent jobs not run concurrently, but wait after one
348 * another.
349 */
350 job.setRule(fSearchActionMutexRule);
351 job.schedule();
352 }
353 }
354
bf415887
MZ
355 private IAction createHierarchicalAction() {
356 IAction action = new Action(Messages.ControlFlowView_hierarchicalViewLabel, IAction.AS_RADIO_BUTTON) {
357 @Override
358 public void run() {
359 ITmfTrace parentTrace = getTrace();
360 synchronized (fFlatTraces) {
361 fFlatTraces.remove(parentTrace);
362 for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
363 final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
3553c912
PT
364 for (TimeGraphEntry traceEntry : getEntryList(ss)) {
365 List<ControlFlowEntry> currentRootList = traceEntry.getChildren().stream()
366 .filter(e -> e instanceof ControlFlowEntry)
d13d217e
SG
367 .map(e -> (ControlFlowEntry) e)
368 .collect(Collectors.toList());
3553c912
PT
369 addEntriesToHierarchicalTree(currentRootList, traceEntry);
370 }
bf415887
MZ
371 }
372 }
373 refresh();
374 }
375 };
376 action.setChecked(true);
377 action.setToolTipText(Messages.ControlFlowView_hierarchicalViewToolTip);
378 return action;
379 }
380
381 private IAction createFlatAction() {
382 IAction action = new Action(Messages.ControlFlowView_flatViewLabel, IAction.AS_RADIO_BUTTON) {
383 @Override
384 public void run() {
385 ITmfTrace parentTrace = getTrace();
386 synchronized (fFlatTraces) {
387 fFlatTraces.add(parentTrace);
388 for (ITmfTrace trace : TmfTraceManager.getTraceSet(parentTrace)) {
389 final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
85ab2844
MK
390 List<@NonNull TimeGraphEntry> entryList = getEntryList(ss);
391 if (entryList != null) {
392 for (TimeGraphEntry traceEntry : entryList) {
393 hierarchicalToFlatTree(traceEntry);
394 }
3553c912 395 }
bf415887
MZ
396 }
397 }
398 refresh();
399 }
400 };
401 action.setToolTipText(Messages.ControlFlowView_flatViewToolTip);
402 return action;
403 }
404
4999a196
GB
405 @Override
406 protected String getNextText() {
407 return Messages.ControlFlowView_nextProcessActionNameText;
408 }
6151d86c 409
4999a196
GB
410 @Override
411 protected String getNextTooltip() {
412 return Messages.ControlFlowView_nextProcessActionToolTipText;
413 }
6151d86c 414
4999a196
GB
415 @Override
416 protected String getPrevText() {
417 return Messages.ControlFlowView_previousProcessActionNameText;
418 }
6151d86c 419
4999a196
GB
420 @Override
421 protected String getPrevTooltip() {
422 return Messages.ControlFlowView_previousProcessActionToolTipText;
6151d86c
PT
423 }
424
0267cc9d
MK
425 /**
426 * Get the optimization function for the scheduling column. In the base
427 * implementation, this optimizes by Line arrows, but can be overidden.
428 * <p>
429 * It takes a collection of link events, looking at the entries being
430 * linked, and returns a list of the proposed order. The list of indexes
431 * should be in ascending order. There can be duplicates, but the values and
432 * order should always be the same for the same input.
433 *
434 * @return the returned column order, where the integer is the tid of the
435 * entry, and the return value is the position, there can be
436 * duplicates.
437 */
438 public Function<Collection<ILinkEvent>, Map<Integer, Long>> getUpdatedSchedulingColumn() {
439 return UPDATE_SCHEDULING_COLUMN_ALGO;
440 }
441
d13d217e
SG
442 /**
443 * This is an optimization action used to find cliques of entries due to
444 * links and put them closer together
445 *
446 * @author Samuel Gagnon
447 */
448 private final class OptimizationAction extends Action {
0267cc9d 449
d13d217e
SG
450 @Override
451 public void runWithEvent(Event event) {
452 ITmfTrace trace = getTrace();
453 if (trace == null) {
454 return;
455 }
456
457 createFlatAction().run();
458
459 /*
0267cc9d
MK
460 * This method only returns the arrows in the current time interval
461 * [a,b] of ControlFlowView. Thus, we only optimize for that time
462 * interval
d13d217e 463 */
0267cc9d
MK
464 List<ILinkEvent> arrows = getTimeGraphViewer().getTimeGraphControl().getArrows();
465 final ITmfStateSystem ss = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
466 List<TimeGraphEntry> currentList = getEntryList(ss);
467
468 Map<Integer, Long> orderedTidMap = getUpdatedSchedulingColumn().apply(arrows);
d13d217e
SG
469
470 /*
0267cc9d
MK
471 * Now that we have our list of ordered tid, it's time to assign a
472 * position for each threads in the view. For this, we assign a
473 * value to an invisible column and sort according to the values in
474 * this column.
d13d217e 475 */
0267cc9d
MK
476 for (TimeGraphEntry entry : currentList) {
477 if (entry instanceof TraceEntry) {
478 for (TimeGraphEntry child : ((TraceEntry) entry).getChildren()) {
479 if (child instanceof ControlFlowEntry) {
480 ControlFlowEntry cEntry = (ControlFlowEntry) child;
481 /*
482 * If the thread is in our list, we give it a
483 * position. Otherwise, it means there's no activity
484 * in the current interval for that thread. We set
485 * its position to Long.MAX_VALUE so it goes to the
486 * bottom.
487 */
488 cEntry.setSchedulingPosition(orderedTidMap.getOrDefault(cEntry.getThreadId(), Long.MAX_VALUE));
489 }
490 }
491 }
492 }
493
494 setEntryComparator(ControlFlowColumnComparators.SCHEDULING_COLUMN_COMPARATOR);
495 refresh();
496 }
497
498 }
499
6151d86c 500 /**
4999a196 501 * @author gbastien
6151d86c 502 *
6151d86c 503 */
4999a196 504 protected static class ControlFlowTreeLabelProvider extends TreeLabelProvider {
6151d86c 505
4999a196
GB
506 @Override
507 public String getColumnText(Object element, int columnIndex) {
3553c912
PT
508 if (element instanceof TraceEntry) {
509 if (columnIndex == 0) {
510 return ((TraceEntry) element).getName();
511 }
512 return ""; //$NON-NLS-1$
513 }
4999a196 514 ControlFlowEntry entry = (ControlFlowEntry) element;
6151d86c 515
4999a196
GB
516 if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_processColumn)) {
517 return entry.getName();
518 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_tidColumn)) {
519 return Integer.toString(entry.getThreadId());
520 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_ptidColumn)) {
521 if (entry.getParentThreadId() > 0) {
522 return Integer.toString(entry.getParentThreadId());
6151d86c 523 }
4999a196
GB
524 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_birthTimeColumn)) {
525 return Utils.formatTime(entry.getStartTime(), TimeFormat.CALENDAR, Resolution.NANOSEC);
d13d217e
SG
526 } else if (COLUMN_NAMES[columnIndex].equals(Messages.ControlFlowView_traceColumn)) {
527 return entry.getTrace().getName();
528 } else if (COLUMN_NAMES[columnIndex].equals(INVISIBLE_COLUMN)) {
529 return Long.toString(entry.getSchedulingPosition());
6151d86c 530 }
4999a196 531 return ""; //$NON-NLS-1$
6151d86c 532 }
6151d86c 533
6151d86c
PT
534 }
535
a03b7ee4
PT
536 private static class ControlFlowFilterLabelProvider extends TreeLabelProvider {
537
538 @Override
539 public String getColumnText(Object element, int columnIndex) {
3553c912
PT
540 if (element instanceof TraceEntry) {
541 if (columnIndex == 0) {
542 return ((TraceEntry) element).getName();
543 }
544 return ""; //$NON-NLS-1$
545 }
a03b7ee4
PT
546 ControlFlowEntry entry = (ControlFlowEntry) element;
547
548 if (columnIndex == 0) {
549 return entry.getName();
550 } else if (columnIndex == 1) {
551 return Integer.toString(entry.getThreadId());
552 }
553 return ""; //$NON-NLS-1$
554 }
555
556 }
557
3553c912
PT
558 private static class TraceEntry extends TimeGraphEntry {
559
560 public TraceEntry(String name, long startTime, long endTime) {
561 super(name, startTime, endTime);
562 }
563
564 @Override
565 public boolean hasTimeEvents() {
566 return false;
567 }
568 }
569
bf415887
MZ
570 @TmfSignalHandler
571 @Override
572 public void traceClosed(org.eclipse.tracecompass.tmf.core.signal.TmfTraceClosedSignal signal) {
573 super.traceClosed(signal);
574 synchronized (fFlatTraces) {
575 fFlatTraces.remove(signal.getTrace());
576 }
577 }
578
579 @TmfSignalHandler
580 @Override
581 public void traceSelected(TmfTraceSelectedSignal signal) {
582 super.traceSelected(signal);
583 synchronized (fFlatTraces) {
584 if (fFlatTraces.contains(signal.getTrace())) {
585 fHierarchicalAction.setChecked(false);
586 fFlatAction.setChecked(true);
587 } else {
588 fFlatAction.setChecked(false);
589 fHierarchicalAction.setChecked(true);
590 }
591 }
592 }
593
6151d86c
PT
594 // ------------------------------------------------------------------------
595 // Internal
596 // ------------------------------------------------------------------------
597
4999a196 598 @Override
f8f46a52 599 protected void buildEntryList(final ITmfTrace trace, final ITmfTrace parentTrace, final IProgressMonitor monitor) {
8321a699 600 final ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
1cf25311
PT
601 if (ssq == null) {
602 return;
603 }
fec1ac0b 604
8321a699 605 final List<ControlFlowEntry> entryList = new ArrayList<>();
3553c912
PT
606 /** Map of trace entries */
607 Map<ITmfTrace, TraceEntry> traceEntryMap = new HashMap<>();
608 /** Map of control flow entries, key is a pair [threadId, cpuId] */
8a0bbebf 609 final Map<Pair<Integer, Integer>, ControlFlowEntry> entryMap = new HashMap<>();
1cf25311
PT
610
611 long start = ssq.getStartTime();
612 setStartTime(Math.min(getStartTime(), start));
613
614 boolean complete = false;
615 while (!complete) {
faa38350
PT
616 if (monitor.isCanceled()) {
617 return;
618 }
aae89862 619 complete = ssq.waitUntilBuilt(BUILD_UPDATE_TIMEOUT);
1cf25311
PT
620 if (ssq.isCancelled()) {
621 return;
622 }
623 long end = ssq.getCurrentEndTime();
624 if (start == end && !complete) { // when complete execute one last time regardless of end time
625 continue;
626 }
3553c912
PT
627
628 TraceEntry aTraceEntry = traceEntryMap.get(trace);
629 if (aTraceEntry == null) {
630 aTraceEntry = new TraceEntry(trace.getName(), start, end + 1);
631 traceEntryMap.put(trace, aTraceEntry);
632 addToEntryList(parentTrace, ssq, Collections.singletonList(aTraceEntry));
633 } else {
634 aTraceEntry.updateEndTime(end + 1);
635 }
636 final TraceEntry traceEntry = aTraceEntry;
637
8321a699 638 final long resolution = Math.max(1, (end - ssq.getStartTime()) / getDisplayWidth());
1cf25311 639 setEndTime(Math.max(getEndTime(), end + 1));
8321a699 640 final List<Integer> threadQuarks = ssq.getQuarks(Attributes.THREADS, "*"); //$NON-NLS-1$
8d5d4aa4 641 queryFullStates(ssq, start, end, resolution, monitor, new IQueryHandler() {
8321a699
PT
642 @Override
643 public void handle(List<List<ITmfStateInterval>> fullStates, List<ITmfStateInterval> prevFullState) {
644 for (int threadQuark : threadQuarks) {
8a0bbebf
MJ
645 String threadAttributeName = ssq.getAttributeName(threadQuark);
646
642b4947 647 Pair<Integer, Integer> entryKey = Attributes.parseThreadAttributeName(threadAttributeName);
8a0bbebf
MJ
648 int threadId = entryKey.getFirst();
649
650 if (threadId < 0) { // ignore the 'unknown' (-1) thread
8321a699
PT
651 continue;
652 }
1c471b9e 653
fd7961ac
LPD
654 int execNameQuark = ssq.optQuarkRelative(threadQuark, Attributes.EXEC_NAME);
655 int ppidQuark = ssq.optQuarkRelative(threadQuark, Attributes.PPID);
656 if (execNameQuark == ITmfStateSystem.INVALID_ATTRIBUTE) {
8321a699
PT
657 /* No information on this thread (yet?), skip it for now */
658 continue;
659 }
660 ITmfStateInterval lastExecNameInterval = prevFullState == null || execNameQuark >= prevFullState.size() ? null : prevFullState.get(execNameQuark);
661 long lastExecNameStartTime = lastExecNameInterval == null ? -1 : lastExecNameInterval.getStartTime();
662 long lastExecNameEndTime = lastExecNameInterval == null ? -1 : lastExecNameInterval.getEndTime() + 1;
fd7961ac 663 long lastPpidStartTime = prevFullState == null || ppidQuark >= prevFullState.size() || ppidQuark == ITmfStateSystem.INVALID_ATTRIBUTE ? -1 : prevFullState.get(ppidQuark).getStartTime();
8321a699
PT
664 for (List<ITmfStateInterval> fullState : fullStates) {
665 if (monitor.isCanceled()) {
666 return;
6151d86c 667 }
8321a699
PT
668 if (execNameQuark >= fullState.size() || ppidQuark >= fullState.size()) {
669 /* No information on this thread (yet?), skip it for now */
670 continue;
671 }
672 ITmfStateInterval execNameInterval = fullState.get(execNameQuark);
fd7961ac 673 ITmfStateInterval ppidInterval = ppidQuark == ITmfStateSystem.INVALID_ATTRIBUTE ? null : fullState.get(ppidQuark);
8321a699
PT
674 long startTime = execNameInterval.getStartTime();
675 long endTime = execNameInterval.getEndTime() + 1;
fd7961ac 676 if (startTime == lastExecNameStartTime && ppidInterval != null && ppidInterval.getStartTime() == lastPpidStartTime) {
8321a699
PT
677 continue;
678 }
679 boolean isNull = execNameInterval.getStateValue().isNull();
680 if (isNull && lastExecNameEndTime < startTime && lastExecNameEndTime != -1) {
681 /*
682 * There was a non-null interval in between the
683 * full states, try to use it.
684 */
685 try {
686 execNameInterval = ssq.querySingleState(startTime - 1, execNameQuark);
fd7961ac 687 ppidInterval = ppidQuark == ITmfStateSystem.INVALID_ATTRIBUTE ? null : ssq.querySingleState(startTime - 1, ppidQuark);
8321a699
PT
688 startTime = execNameInterval.getStartTime();
689 endTime = execNameInterval.getEndTime() + 1;
8321a699
PT
690 } catch (StateSystemDisposedException e) {
691 /* ignored */
692 }
1c471b9e 693 }
8321a699
PT
694 if (!execNameInterval.getStateValue().isNull() &&
695 execNameInterval.getStateValue().getType() == ITmfStateValue.Type.STRING) {
696 String execName = execNameInterval.getStateValue().unboxStr();
fd7961ac 697 int ppid = ppidInterval == null ? -1 : ppidInterval.getStateValue().unboxInt();
8a0bbebf 698 ControlFlowEntry entry = entryMap.get(entryKey);
8321a699
PT
699 if (entry == null) {
700 entry = new ControlFlowEntry(threadQuark, trace, execName, threadId, ppid, startTime, endTime);
701 entryList.add(entry);
8a0bbebf 702 entryMap.put(entryKey, entry);
8321a699
PT
703 } else {
704 /*
705 * Update the name of the entry to the
706 * latest execName and the parent thread id
707 * to the latest ppid.
708 */
709 entry.setName(execName);
710 entry.setParentThreadId(ppid);
711 entry.updateEndTime(endTime);
712 }
713 }
714 if (isNull) {
8a0bbebf 715 entryMap.remove(entryKey);
8321a699
PT
716 }
717 lastExecNameStartTime = startTime;
718 lastExecNameEndTime = endTime;
fd7961ac 719 lastPpidStartTime = ppidInterval == null ? -1 : ppidInterval.getStartTime();
6151d86c 720 }
6151d86c 721 }
bf415887
MZ
722 synchronized (fFlatTraces) {
723 if (fFlatTraces.contains(parentTrace)) {
3553c912 724 addEntriesToFlatTree(entryList, traceEntry);
bf415887 725 } else {
3553c912 726 addEntriesToHierarchicalTree(entryList, traceEntry);
bf415887
MZ
727 }
728 }
8d5d4aa4
PT
729 }
730 });
4999a196 731
8d5d4aa4
PT
732 queryFullStates(ssq, ssq.getStartTime(), end, resolution, monitor, new IQueryHandler() {
733 @Override
734 public void handle(@NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState) {
3553c912 735 for (final TimeGraphEntry entry : traceEntry.getChildren()) {
8321a699
PT
736 if (monitor.isCanceled()) {
737 return;
738 }
8d5d4aa4 739 buildStatusEvents(trace, parentTrace, ssq, fullStates, prevFullState, (ControlFlowEntry) entry, monitor, ssq.getStartTime(), end);
8321a699
PT
740 }
741 }
742 });
1cf25311
PT
743
744 if (parentTrace.equals(getTrace())) {
d7ee91bb 745 refresh();
6151d86c 746 }
1cf25311 747
1cf25311 748 start = end;
6151d86c
PT
749 }
750 }
751
bf415887 752 /**
3553c912 753 * Add entries to the traces's child list in a flat fashion (no hierarchy).
bf415887 754 * If one entry has children, we do a depth first search to add each child
3553c912 755 * to the trace's child list and update the parent and child relations.
bf415887 756 */
3553c912
PT
757 private static void hierarchicalToFlatTree(TimeGraphEntry traceEntry) {
758 List<@NonNull TimeGraphEntry> rootList = traceEntry.getChildren();
bf415887
MZ
759 // We visit the children of every entry to add
760 StreamFlattener<TimeGraphEntry> sf = new StreamFlattener<>(entry -> entry.getChildren().stream());
761 Stream<TimeGraphEntry> allEntries = rootList.stream().flatMap(entry -> sf.flatten(entry));
762
763 // We add every entry that is missing from the trace's entry list
764 List<@NonNull TimeGraphEntry> rootListToAdd = allEntries
765 .filter(entry -> !rootList.contains(entry))
766 .collect(Collectors.toList());
767 rootList.forEach(entry -> {
768 entry.clearChildren();
769 });
770 rootListToAdd.forEach(entry -> {
3553c912 771 traceEntry.addChild(entry);
bf415887
MZ
772 entry.clearChildren();
773 });
bf415887
MZ
774 }
775
776 /**
3553c912 777 * Add entries to the traces's child list in a flat fashion (no hierarchy).
bf415887 778 */
3553c912
PT
779 private static void addEntriesToFlatTree(List<@NonNull ControlFlowEntry> entryList, TimeGraphEntry traceEntry) {
780 List<TimeGraphEntry> rootList = traceEntry.getChildren();
781 for (ControlFlowEntry entry : entryList) {
782 if (!rootList.contains(entry)) {
783 traceEntry.addChild(entry);
784 }
785 }
bf415887
MZ
786 }
787
788 /**
3553c912 789 * Add entries to the trace's child list in a hierarchical fashion.
bf415887 790 */
3553c912
PT
791 private static void addEntriesToHierarchicalTree(List<ControlFlowEntry> entryList, TimeGraphEntry traceEntry) {
792 List<TimeGraphEntry> rootList = traceEntry.getChildren();
1cf25311 793
1d46dc38 794 for (ControlFlowEntry entry : entryList) {
3553c912 795 boolean root = (entry.getParent() == null || entry.getParent() == traceEntry);
1cf25311 796 if (root && entry.getParentThreadId() > 0) {
1d46dc38 797 for (ControlFlowEntry parent : entryList) {
0a35a36f
GB
798 /*
799 * Associate the parent entry only if their time overlap. A
800 * child entry may start before its parent, for example at
801 * the beginning of the trace if a parent has not yet
802 * appeared in the state system. We just want to make sure
803 * that the entry didn't start after the parent ended or
804 * ended before the parent started.
805 */
6151d86c 806 if (parent.getThreadId() == entry.getParentThreadId() &&
0a35a36f
GB
807 !(entry.getStartTime() > parent.getEndTime() ||
808 entry.getEndTime() < parent.getStartTime())) {
6151d86c 809 root = false;
3553c912
PT
810 if (rootList.contains(entry)) {
811 traceEntry.removeChild(entry);
1cf25311 812 }
7c094b3e 813 parent.addChild(entry);
6151d86c
PT
814 break;
815 }
816 }
817 }
3553c912
PT
818 if (root && (!rootList.contains(entry))) {
819 traceEntry.addChild(entry);
6151d86c
PT
820 }
821 }
822 }
823
8321a699
PT
824 private void buildStatusEvents(ITmfTrace trace, ITmfTrace parentTrace, ITmfStateSystem ss, @NonNull List<List<ITmfStateInterval>> fullStates,
825 @Nullable List<ITmfStateInterval> prevFullState, ControlFlowEntry entry, @NonNull IProgressMonitor monitor, long start, long end) {
1cf25311 826 if (start < entry.getEndTime() && end > entry.getStartTime()) {
8321a699 827 List<ITimeEvent> eventList = getEventList(entry, ss, fullStates, prevFullState, monitor);
d2120fb6 828 if (eventList == null) {
1cf25311
PT
829 return;
830 }
8d5d4aa4
PT
831 /* Start a new event list on first iteration, then append to it */
832 if (prevFullState == null) {
833 entry.setEventList(eventList);
834 } else {
835 for (ITimeEvent event : eventList) {
836 entry.addEvent(event);
837 }
1cf25311 838 }
8321a699 839 if (parentTrace.equals(getTrace())) {
1cf25311
PT
840 redraw();
841 }
faa38350 842 }
6151d86c 843 for (ITimeGraphEntry child : entry.getChildren()) {
faa38350
PT
844 if (monitor.isCanceled()) {
845 return;
846 }
8321a699 847 buildStatusEvents(trace, parentTrace, ss, fullStates, prevFullState, (ControlFlowEntry) child, monitor, start, end);
6151d86c
PT
848 }
849 }
850
4999a196 851 @Override
8321a699
PT
852 protected @Nullable List<ITimeEvent> getEventList(@NonNull TimeGraphEntry tgentry, ITmfStateSystem ss,
853 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
4999a196
GB
854 List<ITimeEvent> eventList = null;
855 if (!(tgentry instanceof ControlFlowEntry)) {
856 return eventList;
857 }
858 ControlFlowEntry entry = (ControlFlowEntry) tgentry;
6151d86c 859 try {
d3cc952f 860 int statusQuark = entry.getThreadQuark();
8321a699
PT
861 eventList = new ArrayList<>(fullStates.size());
862 ITmfStateInterval lastInterval = prevFullState == null || statusQuark >= prevFullState.size() ? null : prevFullState.get(statusQuark);
863 long lastStartTime = lastInterval == null ? -1 : lastInterval.getStartTime();
864 long lastEndTime = lastInterval == null ? -1 : lastInterval.getEndTime() + 1;
865 for (List<ITmfStateInterval> fullState : fullStates) {
6151d86c
PT
866 if (monitor.isCanceled()) {
867 return null;
868 }
8321a699
PT
869 if (statusQuark >= fullState.size()) {
870 /* No information on this thread (yet?), skip it for now */
871 continue;
872 }
873 ITmfStateInterval statusInterval = fullState.get(statusQuark);
6151d86c 874 long time = statusInterval.getStartTime();
8321a699
PT
875 if (time == lastStartTime) {
876 continue;
877 }
6151d86c
PT
878 long duration = statusInterval.getEndTime() - time + 1;
879 int status = -1;
880 try {
881 status = statusInterval.getStateValue().unboxInt();
882 } catch (StateValueTypeException e) {
8321a699 883 Activator.getDefault().logError(e.getMessage());
6151d86c
PT
884 }
885 if (lastEndTime != time && lastEndTime != -1) {
af10fe06 886 eventList.add(new TimeEvent(entry, lastEndTime, time - lastEndTime));
6151d86c 887 }
8321a699
PT
888 if (!statusInterval.getStateValue().isNull()) {
889 eventList.add(new TimeEvent(entry, time, duration, status));
890 } else {
891 eventList.add(new NullTimeEvent(entry, time, duration));
892 }
893 lastStartTime = time;
6151d86c
PT
894 lastEndTime = time + duration;
895 }
d3cc952f 896 } catch (TimeRangeException e) {
50a47aa6 897 Activator.getDefault().logError(e.getMessage());
6151d86c
PT
898 }
899 return eventList;
900 }
901
4999a196
GB
902 /**
903 * Returns a value corresponding to the selected entry.
904 *
1cf25311
PT
905 * Used in conjunction with synchingToTime to change the selected entry. If
906 * one of these methods is overridden in child class, then both should be.
4999a196
GB
907 *
908 * @param time
909 * The currently selected time
910 * @return a value identifying the entry
911 */
912 private int getSelectionValue(long time) {
913 int thread = -1;
c14c0757 914 for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) {
4999a196
GB
915 if (thread > 0) {
916 break;
917 }
6d16f5a9 918 ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(trace, KernelAnalysisModule.ID);
1cf25311
PT
919 if (ssq == null) {
920 continue;
921 }
922 if (time >= ssq.getStartTime() && time <= ssq.getCurrentEndTime()) {
923 List<Integer> currentThreadQuarks = ssq.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
924 for (int currentThreadQuark : currentThreadQuarks) {
925 try {
926 ITmfStateInterval currentThreadInterval = ssq.querySingleState(time, currentThreadQuark);
927 int currentThread = currentThreadInterval.getStateValue().unboxInt();
928 if (currentThread > 0) {
d3cc952f 929 int statusQuark = ssq.getQuarkAbsolute(Attributes.THREADS, Integer.toString(currentThread));
1cf25311
PT
930 ITmfStateInterval statusInterval = ssq.querySingleState(time, statusQuark);
931 if (statusInterval.getStartTime() == time) {
932 thread = currentThread;
933 break;
4999a196 934 }
4999a196 935 }
1cf25311 936 } catch (AttributeNotFoundException | TimeRangeException | StateValueTypeException e) {
8321a699 937 Activator.getDefault().logError(e.getMessage());
1cf25311
PT
938 } catch (StateSystemDisposedException e) {
939 /* Ignored */
faa38350 940 }
6151d86c 941 }
6151d86c 942 }
4999a196
GB
943 }
944 return thread;
6151d86c
PT
945 }
946
4999a196
GB
947 @Override
948 protected void synchingToTime(long time) {
949 int selected = getSelectionValue(time);
950 if (selected > 0) {
747adf5c 951 for (Object element : getTimeGraphViewer().getExpandedElements()) {
4999a196
GB
952 if (element instanceof ControlFlowEntry) {
953 ControlFlowEntry entry = (ControlFlowEntry) element;
954 if (entry.getThreadId() == selected) {
955 getTimeGraphCombo().setSelection(entry);
956 break;
6151d86c
PT
957 }
958 }
959 }
6151d86c 960 }
6151d86c 961 }
79ec0b89
PT
962
963 @Override
8321a699 964 protected @NonNull List<ILinkEvent> getLinkList(ITmfStateSystem ss,
143217ee 965 @NonNull List<List<ITmfStateInterval>> fullStates, @Nullable List<ITmfStateInterval> prevFullState, @NonNull IProgressMonitor monitor) {
e0838ca1 966 List<ILinkEvent> list = new ArrayList<>();
8321a699 967 List<TimeGraphEntry> entryList = getEntryList(ss);
c14c0757 968 if (entryList == null) {
79ec0b89
PT
969 return list;
970 }
c14c0757 971 for (ITmfTrace trace : TmfTraceManager.getTraceSet(getTrace())) {
8321a699
PT
972 List<Integer> currentThreadQuarks = ss.getQuarks(Attributes.CPUS, "*", Attributes.CURRENT_THREAD); //$NON-NLS-1$
973 for (int currentThreadQuark : currentThreadQuarks) {
974 if (currentThreadQuark >= fullStates.get(0).size()) {
975 /* No information on this cpu (yet?), skip it for now */
4bc53929
GB
976 continue;
977 }
8321a699
PT
978 List<ITmfStateInterval> currentThreadIntervals = new ArrayList<>(fullStates.size() + 2);
979 try {
143217ee
PT
980 /*
981 * Add the previous interval if it is the first query
982 * iteration and the first interval has currentThread=0. Add
983 * the following interval if the last interval has
984 * currentThread=0. These are diagonal arrows crossing the
985 * query iteration range.
986 */
987 if (prevFullState == null) {
988 ITmfStateInterval currentThreadInterval = fullStates.get(0).get(currentThreadQuark);
989 if (currentThreadInterval.getStateValue().unboxInt() == 0) {
990 long start = Math.max(currentThreadInterval.getStartTime() - 1, ss.getStartTime());
991 currentThreadIntervals.add(ss.querySingleState(start, currentThreadQuark));
992 }
993 }
8321a699
PT
994 for (List<ITmfStateInterval> fullState : fullStates) {
995 currentThreadIntervals.add(fullState.get(currentThreadQuark));
996 }
143217ee
PT
997 ITmfStateInterval currentThreadInterval = fullStates.get(fullStates.size() - 1).get(currentThreadQuark);
998 if (currentThreadInterval.getStateValue().unboxInt() == 0) {
999 long end = Math.min(currentThreadInterval.getEndTime() + 1, ss.getCurrentEndTime());
1000 currentThreadIntervals.add(ss.querySingleState(end, currentThreadQuark));
1001 }
8321a699
PT
1002 } catch (StateSystemDisposedException e) {
1003 /* Ignored */
1004 return list;
1005 }
1006 int prevThread = 0;
1007 long prevEnd = 0;
1008 long lastEnd = 0;
1009 for (ITmfStateInterval currentThreadInterval : currentThreadIntervals) {
1010 if (monitor.isCanceled()) {
1011 return list;
1012 }
1013 if (currentThreadInterval.getEndTime() + 1 == lastEnd) {
1014 continue;
1015 }
1016 long time = currentThreadInterval.getStartTime();
1017 if (time != lastEnd) {
1018 // don't create links where there are gaps in intervals due to the resolution
1019 prevThread = 0;
1020 prevEnd = 0;
1021 }
1022 int thread = currentThreadInterval.getStateValue().unboxInt();
1023 if (thread > 0 && prevThread > 0) {
1024 ITimeGraphEntry prevEntry = findEntry(entryList, trace, prevThread);
1025 ITimeGraphEntry nextEntry = findEntry(entryList, trace, thread);
1026 list.add(new TimeLinkEvent(prevEntry, nextEntry, prevEnd, time - prevEnd, 0));
1027 }
1028 lastEnd = currentThreadInterval.getEndTime() + 1;
1029 if (thread != 0) {
1030 prevThread = thread;
1031 prevEnd = lastEnd;
79ec0b89 1032 }
79ec0b89
PT
1033 }
1034 }
1035 }
1036 return list;
1037 }
1038
3553c912
PT
1039 private ControlFlowEntry findEntry(List<TimeGraphEntry> entryList, ITmfTrace trace, int threadId) {
1040 for (TimeGraphEntry entry : entryList) {
79ec0b89
PT
1041 if (entry instanceof ControlFlowEntry) {
1042 ControlFlowEntry controlFlowEntry = (ControlFlowEntry) entry;
1043 if (controlFlowEntry.getThreadId() == threadId && controlFlowEntry.getTrace() == trace) {
1044 return controlFlowEntry;
3553c912
PT
1045 }
1046 }
1047 if (entry.hasChildren()) {
1048 ControlFlowEntry controlFlowEntry = findEntry(entry.getChildren(), trace, threadId);
1049 if (controlFlowEntry != null) {
1050 return controlFlowEntry;
79ec0b89
PT
1051 }
1052 }
1053 }
1054 return null;
1055 }
6151d86c 1056}
This page took 0.144312 seconds and 5 git commands to generate.