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
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.jface
.action
.IAction
;
26 import org
.eclipse
.jface
.action
.IToolBarManager
;
27 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
28 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.core
.Attributes
;
29 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.ui
.Activator
;
30 import org
.eclipse
.tracecompass
.internal
.lttng2
.kernel
.ui
.Messages
;
31 import org
.eclipse
.tracecompass
.lttng2
.kernel
.core
.analysis
.kernel
.LttngKernelAnalysis
;
32 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
33 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
34 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
55 * The Control Flow view main object
58 public class ControlFlowView
extends AbstractTimeGraphView
{
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
67 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
69 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
70 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
71 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
72 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
73 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
75 private static final String
[] COLUMN_NAMES
= new String
[] {
83 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
88 // Timeout between updates in the build thread in ms
89 private static final long BUILD_UPDATE_TIMEOUT
= 500;
91 // ------------------------------------------------------------------------
93 // ------------------------------------------------------------------------
98 public ControlFlowView() {
99 super(ID
, new ControlFlowPresentationProvider());
100 setTreeColumns(COLUMN_NAMES
);
101 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
102 setFilterColumns(FILTER_COLUMN_NAMES
);
103 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
104 setEntryComparator(new ControlFlowEntryComparator());
108 protected void fillLocalToolBar(IToolBarManager manager
) {
109 super.fillLocalToolBar(manager
);
110 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
111 IDialogSettings section
= settings
.getSection(getClass().getName());
112 if (section
== null) {
113 section
= settings
.addNewSection(getClass().getName());
116 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
117 manager
.add(hideArrowsAction
);
119 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
120 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
121 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
122 manager
.add(followArrowBwdAction
);
124 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
125 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
126 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
127 manager
.add(followArrowFwdAction
);
131 protected String
getNextText() {
132 return Messages
.ControlFlowView_nextProcessActionNameText
;
136 protected String
getNextTooltip() {
137 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
141 protected String
getPrevText() {
142 return Messages
.ControlFlowView_previousProcessActionNameText
;
146 protected String
getPrevTooltip() {
147 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
150 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
153 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
157 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
158 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
159 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
160 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
162 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
165 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
170 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
181 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
184 public String
getColumnText(Object element
, int columnIndex
) {
185 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
187 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
188 return entry
.getName();
189 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
190 return Integer
.toString(entry
.getThreadId());
191 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
192 if (entry
.getParentThreadId() > 0) {
193 return Integer
.toString(entry
.getParentThreadId());
195 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
196 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
197 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
198 return entry
.getTrace().getName();
200 return ""; //$NON-NLS-1$
205 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
208 public String
getColumnText(Object element
, int columnIndex
) {
209 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
211 if (columnIndex
== 0) {
212 return entry
.getName();
213 } else if (columnIndex
== 1) {
214 return Integer
.toString(entry
.getThreadId());
216 return ""; //$NON-NLS-1$
221 // ------------------------------------------------------------------------
223 // ------------------------------------------------------------------------
226 protected void buildEventList(final ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
227 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysis
.ID
);
232 List
<ControlFlowEntry
> entryList
= new ArrayList
<>();
233 Map
<Integer
, ControlFlowEntry
> entryMap
= new HashMap
<>();
235 long start
= ssq
.getStartTime();
236 setStartTime(Math
.min(getStartTime(), start
));
238 boolean complete
= false;
240 if (monitor
.isCanceled()) {
243 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
244 if (ssq
.isCancelled()) {
247 long end
= ssq
.getCurrentEndTime();
248 if (start
== end
&& !complete
) { // when complete execute one last time regardless of end time
251 setEndTime(Math
.max(getEndTime(), end
+ 1));
252 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
253 for (int threadQuark
: threadQuarks
) {
254 if (monitor
.isCanceled()) {
257 String threadName
= ssq
.getAttributeName(threadQuark
);
260 threadId
= Integer
.parseInt(threadName
);
261 } catch (NumberFormatException e1
) {
264 if (threadId
<= 0) { // ignore the 'unknown' (-1) and swapper (0) threads
269 List
<ITmfStateInterval
> execNameIntervals
;
271 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
272 execNameIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, execNameQuark
, start
, end
);
273 } catch (AttributeNotFoundException e
) {
274 /* No information on this thread (yet?), skip it for now */
276 } catch (StateSystemDisposedException e
) {
277 /* State system is closing down, no point continuing */
281 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
282 if (monitor
.isCanceled()) {
285 ControlFlowEntry entry
= entryMap
.get(threadId
);
286 if (!execNameInterval
.getStateValue().isNull() &&
287 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
288 String execName
= execNameInterval
.getStateValue().unboxStr();
289 long startTime
= execNameInterval
.getStartTime();
290 long endTime
= execNameInterval
.getEndTime() + 1;
293 ITmfStateInterval ppidInterval
= null;
295 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
296 ppidInterval
= StateSystemUtils
.queryUntilNonNullValue(ssq
, ppidQuark
, startTime
, endTime
);
297 } catch (AttributeNotFoundException e
) {
298 /* No info, keep PPID at -1 */
302 if (ppidInterval
!= null) {
303 ppid
= ppidInterval
.getStateValue().unboxInt();
305 entry
= new ControlFlowEntry(threadQuark
, trace
, execName
, threadId
, ppid
, startTime
, endTime
);
306 entryList
.add(entry
);
307 entryMap
.put(threadId
, entry
);
309 // update the name of the entry to the latest
311 entry
.setName(execName
);
312 entry
.updateEndTime(endTime
);
315 entryMap
.remove(threadId
);
320 updateTree(entryList
, parentTrace
);
322 if (parentTrace
.equals(getTrace())) {
326 for (ControlFlowEntry entry
: entryList
) {
327 if (monitor
.isCanceled()) {
330 buildStatusEvents(entry
.getTrace(), entry
, monitor
, start
, end
);
337 private void updateTree(List
<ControlFlowEntry
> entryList
, ITmfTrace parentTrace
) {
338 List
<TimeGraphEntry
> rootListToAdd
= new ArrayList
<>();
339 List
<TimeGraphEntry
> rootListToRemove
= new ArrayList
<>();
340 List
<TimeGraphEntry
> rootList
= getEntryList(parentTrace
);
342 for (ControlFlowEntry entry
: entryList
) {
343 boolean root
= (entry
.getParent() == null);
344 if (root
&& entry
.getParentThreadId() > 0) {
345 for (ControlFlowEntry parent
: entryList
) {
347 * Associate the parent entry only if their time overlap. A
348 * child entry may start before its parent, for example at
349 * the beginning of the trace if a parent has not yet
350 * appeared in the state system. We just want to make sure
351 * that the entry didn't start after the parent ended or
352 * ended before the parent started.
354 if (parent
.getThreadId() == entry
.getParentThreadId() &&
355 !(entry
.getStartTime() > parent
.getEndTime() ||
356 entry
.getEndTime() < parent
.getStartTime())) {
357 parent
.addChild(entry
);
359 if (rootList
!= null && rootList
.contains(entry
)) {
360 rootListToRemove
.add(entry
);
366 if (root
&& (rootList
== null || !rootList
.contains(entry
))) {
367 rootListToAdd
.add(entry
);
371 addToEntryList(parentTrace
, rootListToAdd
);
372 removeFromEntryList(parentTrace
, rootListToRemove
);
375 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, @NonNull IProgressMonitor monitor
, long start
, long end
) {
376 if (start
< entry
.getEndTime() && end
> entry
.getStartTime()) {
377 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysis
.ID
);
382 long startTime
= Math
.max(start
, entry
.getStartTime());
383 long endTime
= Math
.min(end
+ 1, entry
.getEndTime());
384 long resolution
= Math
.max(1, (end
- ssq
.getStartTime()) / getDisplayWidth());
385 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, monitor
);
386 if (eventList
== null) {
389 for (ITimeEvent event
: eventList
) {
390 entry
.addEvent(event
);
392 if (trace
.equals(getTrace())) {
396 for (ITimeGraphEntry child
: entry
.getChildren()) {
397 if (monitor
.isCanceled()) {
400 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
, start
, end
);
405 protected @Nullable List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
406 List
<ITimeEvent
> eventList
= null;
407 if (!(tgentry
instanceof ControlFlowEntry
)) {
410 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
411 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
412 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
413 if (realEnd
<= realStart
) {
416 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysis
.ID
);
421 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
422 List
<ITmfStateInterval
> statusIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
423 eventList
= new ArrayList
<>(statusIntervals
.size());
424 long lastEndTime
= -1;
425 for (ITmfStateInterval statusInterval
: statusIntervals
) {
426 if (monitor
.isCanceled()) {
429 long time
= statusInterval
.getStartTime();
430 long duration
= statusInterval
.getEndTime() - time
+ 1;
433 status
= statusInterval
.getStateValue().unboxInt();
434 } catch (StateValueTypeException e
) {
437 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
438 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
440 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
441 lastEndTime
= time
+ duration
;
443 } catch (AttributeNotFoundException
| TimeRangeException e
) {
445 } catch (StateSystemDisposedException e
) {
452 * Returns a value corresponding to the selected entry.
454 * Used in conjunction with synchingToTime to change the selected entry. If
455 * one of these methods is overridden in child class, then both should be.
458 * The currently selected time
459 * @return a value identifying the entry
461 private int getSelectionValue(long time
) {
463 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
464 if (traces
== null) {
467 for (ITmfTrace trace
: traces
) {
474 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysis
.ID
);
478 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
479 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
480 for (int currentThreadQuark
: currentThreadQuarks
) {
482 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
483 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
484 if (currentThread
> 0) {
485 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
486 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
487 if (statusInterval
.getStartTime() == time
) {
488 thread
= currentThread
;
492 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
494 } catch (StateSystemDisposedException e
) {
504 protected void synchingToTime(long time
) {
505 int selected
= getSelectionValue(time
);
507 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
508 if (element
instanceof ControlFlowEntry
) {
509 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
510 if (entry
.getThreadId() == selected
) {
511 getTimeGraphCombo().setSelection(entry
);
520 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
521 List
<ILinkEvent
> list
= new ArrayList
<>();
522 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
523 List
<TimeGraphEntry
> entryList
= getEntryList(getTrace());
524 if (traces
== null || entryList
== null) {
527 for (ITmfTrace trace
: traces
) {
531 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysis
.ID
);
536 long start
= Math
.max(startTime
, ssq
.getStartTime());
537 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
541 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
542 for (int currentThreadQuark
: currentThreadQuarks
) {
543 // adjust the query range to include the previous and following intervals
544 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
545 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
546 List
<ITmfStateInterval
> currentThreadIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
550 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
551 if (monitor
.isCanceled()) {
554 long time
= currentThreadInterval
.getStartTime();
555 if (time
!= lastEnd
) {
556 // don't create links where there are gaps in intervals due to the resolution
560 int thread
= currentThreadInterval
.getStateValue().unboxInt();
561 if (thread
> 0 && prevThread
> 0) {
562 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
563 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
564 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
566 lastEnd
= currentThreadInterval
.getEndTime() + 1;
573 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
575 } catch (StateSystemDisposedException e
) {
582 private ControlFlowEntry
findEntry(List
<?
extends ITimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
583 for (ITimeGraphEntry entry
: entryList
) {
584 if (entry
instanceof ControlFlowEntry
) {
585 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
586 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
587 return controlFlowEntry
;
588 } else if (entry
.hasChildren()) {
589 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
590 if (controlFlowEntry
!= null) {
591 return controlFlowEntry
;