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
.linuxtools
.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
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Activator
;
29 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Messages
;
30 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.analysis
.LttngKernelAnalysisModule
;
31 import org
.eclipse
.linuxtools
.statesystem
.core
.ITmfStateSystem
;
32 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
33 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
34 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.StateValueTypeException
;
35 import org
.eclipse
.linuxtools
.statesystem
.core
.exceptions
.TimeRangeException
;
36 import org
.eclipse
.linuxtools
.statesystem
.core
.interval
.ITmfStateInterval
;
37 import org
.eclipse
.linuxtools
.statesystem
.core
.statevalue
.ITmfStateValue
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
41 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
42 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
43 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
44 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
45 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
46 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
53 * The Control Flow view main object
56 public class ControlFlowView
extends AbstractTimeGraphView
{
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
65 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
67 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
68 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
69 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
70 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
71 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
73 private static final String
[] COLUMN_NAMES
= new String
[] {
81 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
86 // Timeout between updates in the build thread in ms
87 private static final long BUILD_UPDATE_TIMEOUT
= 500;
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
96 public ControlFlowView() {
97 super(ID
, new ControlFlowPresentationProvider());
98 setTreeColumns(COLUMN_NAMES
);
99 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
100 setFilterColumns(FILTER_COLUMN_NAMES
);
101 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
102 setEntryComparator(new ControlFlowEntryComparator());
106 protected void fillLocalToolBar(IToolBarManager manager
) {
107 super.fillLocalToolBar(manager
);
108 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
109 IDialogSettings section
= settings
.getSection(getClass().getName());
110 if (section
== null) {
111 section
= settings
.addNewSection(getClass().getName());
114 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
115 manager
.add(hideArrowsAction
);
117 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
118 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
119 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
120 manager
.add(followArrowBwdAction
);
122 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
123 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
124 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
125 manager
.add(followArrowFwdAction
);
129 protected String
getNextText() {
130 return Messages
.ControlFlowView_nextProcessActionNameText
;
134 protected String
getNextTooltip() {
135 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
139 protected String
getPrevText() {
140 return Messages
.ControlFlowView_previousProcessActionNameText
;
144 protected String
getPrevTooltip() {
145 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
148 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
151 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
155 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
156 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
157 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
158 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
160 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
163 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
168 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
179 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
182 public String
getColumnText(Object element
, int columnIndex
) {
183 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
185 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
186 return entry
.getName();
187 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
188 return Integer
.toString(entry
.getThreadId());
189 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
190 if (entry
.getParentThreadId() > 0) {
191 return Integer
.toString(entry
.getParentThreadId());
193 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
194 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
195 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
196 return entry
.getTrace().getName();
198 return ""; //$NON-NLS-1$
203 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
206 public String
getColumnText(Object element
, int columnIndex
) {
207 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
209 if (columnIndex
== 0) {
210 return entry
.getName();
211 } else if (columnIndex
== 1) {
212 return Integer
.toString(entry
.getThreadId());
214 return ""; //$NON-NLS-1$
219 // ------------------------------------------------------------------------
221 // ------------------------------------------------------------------------
224 protected void buildEventList(final ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
228 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysisModule
.ID
);
233 List
<ControlFlowEntry
> entryList
= new ArrayList
<>();
234 Map
<Integer
, ControlFlowEntry
> entryMap
= new HashMap
<>();
236 long start
= ssq
.getStartTime();
237 setStartTime(Math
.min(getStartTime(), start
));
239 boolean complete
= false;
241 if (monitor
.isCanceled()) {
244 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
245 if (ssq
.isCancelled()) {
248 long end
= ssq
.getCurrentEndTime();
249 if (start
== end
&& !complete
) { // when complete execute one last time regardless of end time
252 setEndTime(Math
.max(getEndTime(), end
+ 1));
253 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
254 for (int threadQuark
: threadQuarks
) {
255 if (monitor
.isCanceled()) {
258 String threadName
= ssq
.getAttributeName(threadQuark
);
261 threadId
= Integer
.parseInt(threadName
);
262 } catch (NumberFormatException e1
) {
265 if (threadId
<= 0) { // ignore the 'unknown' (-1) and swapper (0) threads
270 List
<ITmfStateInterval
> execNameIntervals
;
272 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
273 execNameIntervals
= ssq
.queryHistoryRange(execNameQuark
, start
, end
);
274 } catch (AttributeNotFoundException e
) {
275 /* No information on this thread (yet?), skip it for now */
277 } catch (StateSystemDisposedException e
) {
278 /* State system is closing down, no point continuing */
282 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
283 if (monitor
.isCanceled()) {
286 ControlFlowEntry entry
= entryMap
.get(threadId
);
287 if (!execNameInterval
.getStateValue().isNull() &&
288 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
289 String execName
= execNameInterval
.getStateValue().unboxStr();
290 long startTime
= execNameInterval
.getStartTime();
291 long endTime
= execNameInterval
.getEndTime() + 1;
293 ITmfStateInterval ppidInterval
= null;
295 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
296 ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
297 } catch (AttributeNotFoundException e
) {
298 /* No info, keep PPID at -1 */
299 } catch (StateSystemDisposedException e
) {
300 /* SS is closing down, time to bail */
304 if (!(ppidInterval
== null) && !ppidInterval
.getStateValue().isNull()) {
305 ppid
= ppidInterval
.getStateValue().unboxInt();
307 entry
= new ControlFlowEntry(threadQuark
, trace
, execName
, threadId
, ppid
, startTime
, endTime
);
308 entryList
.add(entry
);
309 entryMap
.put(threadId
, entry
);
311 // update the name of the entry to the latest
313 entry
.setName(execName
);
314 entry
.updateEndTime(endTime
);
317 entryMap
.remove(threadId
);
322 updateTree(entryList
, parentTrace
);
324 if (parentTrace
.equals(getTrace())) {
328 for (ControlFlowEntry entry
: entryList
) {
329 if (monitor
.isCanceled()) {
332 buildStatusEvents(entry
.getTrace(), entry
, monitor
, start
, end
);
339 private void updateTree(List
<ControlFlowEntry
> entryList
, ITmfTrace parentTrace
) {
340 List
<TimeGraphEntry
> rootListToAdd
= new ArrayList
<>();
341 List
<TimeGraphEntry
> rootListToRemove
= new ArrayList
<>();
342 List
<TimeGraphEntry
> rootList
= getEntryList(parentTrace
);
344 for (ControlFlowEntry entry
: entryList
) {
345 boolean root
= (entry
.getParent() == null);
346 if (root
&& entry
.getParentThreadId() > 0) {
347 for (ControlFlowEntry parent
: entryList
) {
348 if (parent
.getThreadId() == entry
.getParentThreadId() &&
349 entry
.getStartTime() >= parent
.getStartTime() &&
350 entry
.getStartTime() <= parent
.getEndTime()) {
351 parent
.addChild(entry
);
353 if (rootList
!= null && rootList
.contains(entry
)) {
354 rootListToRemove
.add(entry
);
360 if (root
&& (rootList
== null || !rootList
.contains(entry
))) {
361 rootListToAdd
.add(entry
);
365 addToEntryList(parentTrace
, rootListToAdd
);
366 removeFromEntryList(parentTrace
, rootListToRemove
);
369 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
, long start
, long end
) {
370 if (start
< entry
.getEndTime() && end
> entry
.getStartTime()) {
371 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysisModule
.ID
);
376 long startTime
= Math
.max(start
, entry
.getStartTime());
377 long endTime
= Math
.min(end
+ 1, entry
.getEndTime());
378 long resolution
= Math
.max(1, (end
- ssq
.getStartTime()) / getDisplayWidth());
379 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, monitor
);
380 if (eventList
== null) {
383 for (ITimeEvent event
: eventList
) {
384 entry
.addEvent(event
);
386 if (trace
.equals(getTrace())) {
390 for (ITimeGraphEntry child
: entry
.getChildren()) {
391 if (monitor
.isCanceled()) {
394 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
, start
, end
);
399 protected @Nullable List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
400 List
<ITimeEvent
> eventList
= null;
401 if (!(tgentry
instanceof ControlFlowEntry
)) {
404 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
405 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
406 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
407 if (realEnd
<= realStart
) {
410 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), LttngKernelAnalysisModule
.ID
);
415 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
416 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
417 eventList
= new ArrayList
<>(statusIntervals
.size());
418 long lastEndTime
= -1;
419 for (ITmfStateInterval statusInterval
: statusIntervals
) {
420 if (monitor
.isCanceled()) {
423 long time
= statusInterval
.getStartTime();
424 long duration
= statusInterval
.getEndTime() - time
+ 1;
427 status
= statusInterval
.getStateValue().unboxInt();
428 } catch (StateValueTypeException e
) {
431 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
432 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
434 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
435 lastEndTime
= time
+ duration
;
437 } catch (AttributeNotFoundException
| TimeRangeException e
) {
439 } catch (StateSystemDisposedException e
) {
446 * Returns a value corresponding to the selected entry.
448 * Used in conjunction with synchingToTime to change the selected entry. If
449 * one of these methods is overridden in child class, then both should be.
452 * The currently selected time
453 * @return a value identifying the entry
455 private int getSelectionValue(long time
) {
457 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
458 if (traces
== null) {
461 for (ITmfTrace trace
: traces
) {
468 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysisModule
.ID
);
472 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
473 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
474 for (int currentThreadQuark
: currentThreadQuarks
) {
476 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
477 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
478 if (currentThread
> 0) {
479 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
480 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
481 if (statusInterval
.getStartTime() == time
) {
482 thread
= currentThread
;
486 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
488 } catch (StateSystemDisposedException e
) {
498 protected void synchingToTime(long time
) {
499 int selected
= getSelectionValue(time
);
501 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
502 if (element
instanceof ControlFlowEntry
) {
503 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
504 if (entry
.getThreadId() == selected
) {
505 getTimeGraphCombo().setSelection(entry
);
514 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
515 List
<ILinkEvent
> list
= new ArrayList
<>();
516 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
517 List
<TimeGraphEntry
> entryList
= getEntryList(getTrace());
518 if (traces
== null || entryList
== null) {
521 for (ITmfTrace trace
: traces
) {
525 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, LttngKernelAnalysisModule
.ID
);
530 long start
= Math
.max(startTime
, ssq
.getStartTime());
531 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
535 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
536 for (int currentThreadQuark
: currentThreadQuarks
) {
537 // adjust the query range to include the previous and following intervals
538 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
539 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
540 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
544 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
545 if (monitor
.isCanceled()) {
548 long time
= currentThreadInterval
.getStartTime();
549 if (time
!= lastEnd
) {
550 // don't create links where there are gaps in intervals due to the resolution
554 int thread
= currentThreadInterval
.getStateValue().unboxInt();
555 if (thread
> 0 && prevThread
> 0) {
556 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
557 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
558 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
560 lastEnd
= currentThreadInterval
.getEndTime() + 1;
567 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
569 } catch (StateSystemDisposedException e
) {
576 private ControlFlowEntry
findEntry(List
<?
extends ITimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
577 for (ITimeGraphEntry entry
: entryList
) {
578 if (entry
instanceof ControlFlowEntry
) {
579 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
580 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
581 return controlFlowEntry
;
582 } else if (entry
.hasChildren()) {
583 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
584 if (controlFlowEntry
!= null) {
585 return controlFlowEntry
;