1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
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
10 * Patrick Tasse - Initial API and implementation
11 * Geneviève Bastien - Move code to provide base classes for time graph view
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.ui
.views
.controlflow
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Comparator
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
22 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
23 import org
.eclipse
.jdt
.annotation
.Nullable
;
24 import org
.eclipse
.jface
.action
.IAction
;
25 import org
.eclipse
.jface
.action
.IToolBarManager
;
26 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
27 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.Attributes
;
28 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.ui
.Activator
;
29 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.ui
.Messages
;
30 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.analysis
.LttngKernelAnalysisModule
;
31 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
54 * The Control Flow view main object
57 public class ControlFlowView
extends AbstractTimeGraphView
{
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
66 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
68 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
69 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
70 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
71 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
72 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
74 private static final String
[] COLUMN_NAMES
= new String
[] {
82 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
87 // Timeout between updates in the build thread in ms
88 private static final long BUILD_UPDATE_TIMEOUT
= 500;
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
97 public ControlFlowView() {
98 super(ID
, new ControlFlowPresentationProvider());
99 setTreeColumns(COLUMN_NAMES
);
100 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
101 setFilterColumns(FILTER_COLUMN_NAMES
);
102 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
103 setEntryComparator(new ControlFlowEntryComparator());
107 protected void fillLocalToolBar(IToolBarManager manager
) {
108 super.fillLocalToolBar(manager
);
109 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
110 IDialogSettings section
= settings
.getSection(getClass().getName());
111 if (section
== null) {
112 section
= settings
.addNewSection(getClass().getName());
115 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
116 manager
.add(hideArrowsAction
);
118 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
119 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
120 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
121 manager
.add(followArrowBwdAction
);
123 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
124 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
125 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
126 manager
.add(followArrowFwdAction
);
130 protected String
getNextText() {
131 return Messages
.ControlFlowView_nextProcessActionNameText
;
135 protected String
getNextTooltip() {
136 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
140 protected String
getPrevText() {
141 return Messages
.ControlFlowView_previousProcessActionNameText
;
145 protected String
getPrevTooltip() {
146 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
149 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
152 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
156 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
157 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
158 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
159 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
161 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
164 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
169 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
180 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
183 public String
getColumnText(Object element
, int columnIndex
) {
184 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
186 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
187 return entry
.getName();
188 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
189 return Integer
.toString(entry
.getThreadId());
190 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
191 if (entry
.getParentThreadId() > 0) {
192 return Integer
.toString(entry
.getParentThreadId());
194 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
195 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
196 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
197 return entry
.getTrace().getName();
199 return ""; //$NON-NLS-1$
204 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
207 public String
getColumnText(Object element
, int columnIndex
) {
208 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
210 if (columnIndex
== 0) {
211 return entry
.getName();
212 } else if (columnIndex
== 1) {
213 return Integer
.toString(entry
.getThreadId());
215 return ""; //$NON-NLS-1$
220 // ------------------------------------------------------------------------
222 // ------------------------------------------------------------------------
225 protected void buildEventList(final ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
229 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysisModule
.ID
);
234 List
<ControlFlowEntry
> entryList
= new ArrayList
<>();
235 Map
<Integer
, ControlFlowEntry
> entryMap
= new HashMap
<>();
237 long start
= ssq
.getStartTime();
238 setStartTime(Math
.min(getStartTime(), start
));
240 boolean complete
= false;
242 if (monitor
.isCanceled()) {
245 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
246 if (ssq
.isCancelled()) {
249 long end
= ssq
.getCurrentEndTime();
250 if (start
== end
&& !complete
) { // when complete execute one last time regardless of end time
253 setEndTime(Math
.max(getEndTime(), end
+ 1));
254 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
255 for (int threadQuark
: threadQuarks
) {
256 if (monitor
.isCanceled()) {
259 String threadName
= ssq
.getAttributeName(threadQuark
);
262 threadId
= Integer
.parseInt(threadName
);
263 } catch (NumberFormatException e1
) {
266 if (threadId
<= 0) { // ignore the 'unknown' (-1) and swapper (0) threads
271 List
<ITmfStateInterval
> execNameIntervals
;
273 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
274 execNameIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, execNameQuark
, start
, end
);
275 } catch (AttributeNotFoundException e
) {
276 /* No information on this thread (yet?), skip it for now */
278 } catch (StateSystemDisposedException e
) {
279 /* State system is closing down, no point continuing */
283 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
284 if (monitor
.isCanceled()) {
287 ControlFlowEntry entry
= entryMap
.get(threadId
);
288 if (!execNameInterval
.getStateValue().isNull() &&
289 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
290 String execName
= execNameInterval
.getStateValue().unboxStr();
291 long startTime
= execNameInterval
.getStartTime();
292 long endTime
= execNameInterval
.getEndTime() + 1;
294 ITmfStateInterval ppidInterval
= null;
296 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
297 ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
298 } catch (AttributeNotFoundException e
) {
299 /* No info, keep PPID at -1 */
300 } catch (StateSystemDisposedException e
) {
301 /* SS is closing down, time to bail */
305 if (!(ppidInterval
== null) && !ppidInterval
.getStateValue().isNull()) {
306 ppid
= ppidInterval
.getStateValue().unboxInt();
308 entry
= new ControlFlowEntry(threadQuark
, trace
, execName
, threadId
, ppid
, startTime
, endTime
);
309 entryList
.add(entry
);
310 entryMap
.put(threadId
, entry
);
312 // update the name of the entry to the latest
314 entry
.setName(execName
);
315 entry
.updateEndTime(endTime
);
318 entryMap
.remove(threadId
);
323 updateTree(entryList
, parentTrace
);
325 if (parentTrace
.equals(getTrace())) {
329 for (ControlFlowEntry entry
: entryList
) {
330 if (monitor
.isCanceled()) {
333 buildStatusEvents(entry
.getTrace(), entry
, monitor
, start
, end
);
340 private void updateTree(List
<ControlFlowEntry
> entryList
, ITmfTrace parentTrace
) {
341 List
<TimeGraphEntry
> rootListToAdd
= new ArrayList
<>();
342 List
<TimeGraphEntry
> rootListToRemove
= new ArrayList
<>();
343 List
<TimeGraphEntry
> rootList
= getEntryList(parentTrace
);
345 for (ControlFlowEntry entry
: entryList
) {
346 boolean root
= (entry
.getParent() == null);
347 if (root
&& entry
.getParentThreadId() > 0) {
348 for (ControlFlowEntry parent
: entryList
) {
349 if (parent
.getThreadId() == entry
.getParentThreadId() &&
350 entry
.getStartTime() >= parent
.getStartTime() &&
351 entry
.getStartTime() <= parent
.getEndTime()) {
352 parent
.addChild(entry
);
354 if (rootList
!= null && rootList
.contains(entry
)) {
355 rootListToRemove
.add(entry
);
361 if (root
&& (rootList
== null || !rootList
.contains(entry
))) {
362 rootListToAdd
.add(entry
);
366 addToEntryList(parentTrace
, rootListToAdd
);
367 removeFromEntryList(parentTrace
, rootListToRemove
);
370 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
, long start
, long end
) {
371 if (start
< entry
.getEndTime() && end
> entry
.getStartTime()) {
372 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysisModule
.ID
);
377 long startTime
= Math
.max(start
, entry
.getStartTime());
378 long endTime
= Math
.min(end
+ 1, entry
.getEndTime());
379 long resolution
= Math
.max(1, (end
- ssq
.getStartTime()) / getDisplayWidth());
380 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, monitor
);
381 if (eventList
== null) {
384 for (ITimeEvent event
: eventList
) {
385 entry
.addEvent(event
);
387 if (trace
.equals(getTrace())) {
391 for (ITimeGraphEntry child
: entry
.getChildren()) {
392 if (monitor
.isCanceled()) {
395 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
, start
, end
);
400 protected @Nullable List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
401 List
<ITimeEvent
> eventList
= null;
402 if (!(tgentry
instanceof ControlFlowEntry
)) {
405 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
406 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
407 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
408 if (realEnd
<= realStart
) {
411 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysisModule
.ID
);
416 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
417 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
418 eventList
= new ArrayList
<>(statusIntervals
.size());
419 long lastEndTime
= -1;
420 for (ITmfStateInterval statusInterval
: statusIntervals
) {
421 if (monitor
.isCanceled()) {
424 long time
= statusInterval
.getStartTime();
425 long duration
= statusInterval
.getEndTime() - time
+ 1;
428 status
= statusInterval
.getStateValue().unboxInt();
429 } catch (StateValueTypeException e
) {
432 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
433 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
435 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
436 lastEndTime
= time
+ duration
;
438 } catch (AttributeNotFoundException
| TimeRangeException e
) {
440 } catch (StateSystemDisposedException e
) {
447 * Returns a value corresponding to the selected entry.
449 * Used in conjunction with synchingToTime to change the selected entry. If
450 * one of these methods is overridden in child class, then both should be.
453 * The currently selected time
454 * @return a value identifying the entry
456 private int getSelectionValue(long time
) {
458 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
459 if (traces
== null) {
462 for (ITmfTrace trace
: traces
) {
469 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysisModule
.ID
);
473 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
474 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
475 for (int currentThreadQuark
: currentThreadQuarks
) {
477 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
478 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
479 if (currentThread
> 0) {
480 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
481 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
482 if (statusInterval
.getStartTime() == time
) {
483 thread
= currentThread
;
487 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
489 } catch (StateSystemDisposedException e
) {
499 protected void synchingToTime(long time
) {
500 int selected
= getSelectionValue(time
);
502 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
503 if (element
instanceof ControlFlowEntry
) {
504 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
505 if (entry
.getThreadId() == selected
) {
506 getTimeGraphCombo().setSelection(entry
);
515 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
516 List
<ILinkEvent
> list
= new ArrayList
<>();
517 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
518 List
<TimeGraphEntry
> entryList
= getEntryList(getTrace());
519 if (traces
== null || entryList
== null) {
522 for (ITmfTrace trace
: traces
) {
526 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysisModule
.ID
);
531 long start
= Math
.max(startTime
, ssq
.getStartTime());
532 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
536 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
537 for (int currentThreadQuark
: currentThreadQuarks
) {
538 // adjust the query range to include the previous and following intervals
539 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
540 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
541 List
<ITmfStateInterval
> currentThreadIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
545 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
546 if (monitor
.isCanceled()) {
549 long time
= currentThreadInterval
.getStartTime();
550 if (time
!= lastEnd
) {
551 // don't create links where there are gaps in intervals due to the resolution
555 int thread
= currentThreadInterval
.getStateValue().unboxInt();
556 if (thread
> 0 && prevThread
> 0) {
557 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
558 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
559 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
561 lastEnd
= currentThreadInterval
.getEndTime() + 1;
568 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
570 } catch (StateSystemDisposedException e
) {
577 private ControlFlowEntry
findEntry(List
<?
extends ITimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
578 for (ITimeGraphEntry entry
: entryList
) {
579 if (entry
instanceof ControlFlowEntry
) {
580 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
581 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
582 return controlFlowEntry
;
583 } else if (entry
.hasChildren()) {
584 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
585 if (controlFlowEntry
!= null) {
586 return controlFlowEntry
;