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
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
49 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
52 * The Control Flow view main object
55 public class ControlFlowView
extends AbstractTimeGraphView
{
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
64 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
66 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
67 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
68 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
69 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
70 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
72 private static final String
[] COLUMN_NAMES
= new String
[] {
80 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
85 // Timeout between updates in the build thread in ms
86 private static final long BUILD_UPDATE_TIMEOUT
= 500;
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
95 public ControlFlowView() {
96 super(ID
, new ControlFlowPresentationProvider());
97 setTreeColumns(COLUMN_NAMES
);
98 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
99 setFilterColumns(FILTER_COLUMN_NAMES
);
100 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
101 setEntryComparator(new ControlFlowEntryComparator());
105 protected void fillLocalToolBar(IToolBarManager manager
) {
106 super.fillLocalToolBar(manager
);
107 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
108 IDialogSettings section
= settings
.getSection(getClass().getName());
109 if (section
== null) {
110 section
= settings
.addNewSection(getClass().getName());
113 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
114 manager
.add(hideArrowsAction
);
116 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
117 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
118 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
119 manager
.add(followArrowBwdAction
);
121 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
122 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
123 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
124 manager
.add(followArrowFwdAction
);
128 protected String
getNextText() {
129 return Messages
.ControlFlowView_nextProcessActionNameText
;
133 protected String
getNextTooltip() {
134 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
138 protected String
getPrevText() {
139 return Messages
.ControlFlowView_previousProcessActionNameText
;
143 protected String
getPrevTooltip() {
144 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
147 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
150 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
154 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
155 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
156 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
157 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
159 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
162 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
167 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
178 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
181 public String
getColumnText(Object element
, int columnIndex
) {
182 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
184 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
185 return entry
.getName();
186 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
187 return Integer
.toString(entry
.getThreadId());
188 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
189 if (entry
.getParentThreadId() > 0) {
190 return Integer
.toString(entry
.getParentThreadId());
192 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
193 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
194 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
195 return entry
.getTrace().getName();
197 return ""; //$NON-NLS-1$
202 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
205 public String
getColumnText(Object element
, int columnIndex
) {
206 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
208 if (columnIndex
== 0) {
209 return entry
.getName();
210 } else if (columnIndex
== 1) {
211 return Integer
.toString(entry
.getThreadId());
213 return ""; //$NON-NLS-1$
218 // ------------------------------------------------------------------------
220 // ------------------------------------------------------------------------
223 protected void buildEventList(final ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
224 LttngKernelAnalysisModule module
= trace
.getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
225 if (module
== null) {
229 module
.waitForInitialization();
230 ITmfStateSystem ssq
= module
.getStateSystem();
235 List
<ControlFlowEntry
> entryList
= new ArrayList
<>();
236 Map
<Integer
, ControlFlowEntry
> entryMap
= new HashMap
<>();
238 long start
= ssq
.getStartTime();
239 setStartTime(Math
.min(getStartTime(), start
));
241 boolean complete
= false;
243 if (monitor
.isCanceled()) {
246 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
247 if (ssq
.isCancelled()) {
250 long end
= ssq
.getCurrentEndTime();
251 if (start
== end
&& !complete
) { // when complete execute one last time regardless of end time
254 setEndTime(Math
.max(getEndTime(), end
+ 1));
255 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
256 for (int threadQuark
: threadQuarks
) {
257 if (monitor
.isCanceled()) {
260 String threadName
= ssq
.getAttributeName(threadQuark
);
263 threadId
= Integer
.parseInt(threadName
);
264 } catch (NumberFormatException e1
) {
267 if (threadId
<= 0) { // ignore the 'unknown' (-1) and swapper (0) threads
272 List
<ITmfStateInterval
> execNameIntervals
;
274 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
275 execNameIntervals
= ssq
.queryHistoryRange(execNameQuark
, start
, end
);
276 } catch (AttributeNotFoundException e
) {
277 /* No information on this thread (yet?), skip it for now */
279 } catch (StateSystemDisposedException e
) {
280 /* State system is closing down, no point continuing */
284 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
285 if (monitor
.isCanceled()) {
288 ControlFlowEntry entry
= entryMap
.get(threadId
);
289 if (!execNameInterval
.getStateValue().isNull() &&
290 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
291 String execName
= execNameInterval
.getStateValue().unboxStr();
292 long startTime
= execNameInterval
.getStartTime();
293 long endTime
= execNameInterval
.getEndTime() + 1;
295 ITmfStateInterval ppidInterval
= null;
297 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
298 ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
299 } catch (AttributeNotFoundException e
) {
300 /* No info, keep PPID at -1 */
301 } catch (StateSystemDisposedException e
) {
302 /* SS is closing down, time to bail */
306 if (!(ppidInterval
== null) && !ppidInterval
.getStateValue().isNull()) {
307 ppid
= ppidInterval
.getStateValue().unboxInt();
309 entry
= new ControlFlowEntry(threadQuark
, trace
, execName
, threadId
, ppid
, startTime
, endTime
);
310 entryList
.add(entry
);
311 entryMap
.put(threadId
, entry
);
313 // update the name of the entry to the latest
315 entry
.setName(execName
);
316 entry
.updateEndTime(endTime
);
319 entryMap
.remove(threadId
);
324 updateTree(entryList
, parentTrace
);
326 if (parentTrace
.equals(getTrace())) {
330 for (ControlFlowEntry entry
: entryList
) {
331 if (monitor
.isCanceled()) {
334 buildStatusEvents(entry
.getTrace(), entry
, monitor
, start
, end
);
341 private void updateTree(List
<ControlFlowEntry
> entryList
, ITmfTrace parentTrace
) {
342 List
<TimeGraphEntry
> rootListToAdd
= new ArrayList
<>();
343 List
<TimeGraphEntry
> rootListToRemove
= new ArrayList
<>();
344 List
<TimeGraphEntry
> rootList
= getEntryList(parentTrace
);
346 for (ControlFlowEntry entry
: entryList
) {
347 boolean root
= (entry
.getParent() == null);
348 if (root
&& entry
.getParentThreadId() > 0) {
349 for (ControlFlowEntry parent
: entryList
) {
350 if (parent
.getThreadId() == entry
.getParentThreadId() &&
351 entry
.getStartTime() >= parent
.getStartTime() &&
352 entry
.getStartTime() <= parent
.getEndTime()) {
353 parent
.addChild(entry
);
355 if (rootList
!= null && rootList
.contains(entry
)) {
356 rootListToRemove
.add(entry
);
362 if (root
&& (rootList
== null || !rootList
.contains(entry
))) {
363 rootListToAdd
.add(entry
);
367 addToEntryList(parentTrace
, rootListToAdd
);
368 removeFromEntryList(parentTrace
, rootListToRemove
);
371 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
, long start
, long end
) {
372 if (start
< entry
.getEndTime() && end
> entry
.getStartTime()) {
373 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
374 if (module
== null) {
377 ITmfStateSystem ssq
= module
.getStateSystem();
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 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
417 if (module
== null) {
420 ITmfStateSystem ssq
= module
.getStateSystem();
425 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
426 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
427 eventList
= new ArrayList
<>(statusIntervals
.size());
428 long lastEndTime
= -1;
429 for (ITmfStateInterval statusInterval
: statusIntervals
) {
430 if (monitor
.isCanceled()) {
433 long time
= statusInterval
.getStartTime();
434 long duration
= statusInterval
.getEndTime() - time
+ 1;
437 status
= statusInterval
.getStateValue().unboxInt();
438 } catch (StateValueTypeException e
) {
441 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
442 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
444 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
445 lastEndTime
= time
+ duration
;
447 } catch (AttributeNotFoundException
| TimeRangeException e
) {
449 } catch (StateSystemDisposedException e
) {
456 * Returns a value corresponding to the selected entry.
458 * Used in conjunction with synchingToTime to change the selected entry. If
459 * one of these methods is overridden in child class, then both should be.
462 * The currently selected time
463 * @return a value identifying the entry
465 private int getSelectionValue(long time
) {
467 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
468 if (traces
== null) {
471 for (ITmfTrace trace
: traces
) {
475 LttngKernelAnalysisModule module
= trace
.getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
476 if (module
== null) {
479 ITmfStateSystem ssq
= module
.getStateSystem();
483 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
484 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
485 for (int currentThreadQuark
: currentThreadQuarks
) {
487 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
488 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
489 if (currentThread
> 0) {
490 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
491 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
492 if (statusInterval
.getStartTime() == time
) {
493 thread
= currentThread
;
497 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
499 } catch (StateSystemDisposedException e
) {
509 protected void synchingToTime(long time
) {
510 int selected
= getSelectionValue(time
);
512 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
513 if (element
instanceof ControlFlowEntry
) {
514 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
515 if (entry
.getThreadId() == selected
) {
516 getTimeGraphCombo().setSelection(entry
);
525 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
526 List
<ILinkEvent
> list
= new ArrayList
<>();
527 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
528 List
<TimeGraphEntry
> entryList
= getEntryList(getTrace());
529 if (traces
== null || entryList
== null) {
532 for (ITmfTrace trace
: traces
) {
533 LttngKernelAnalysisModule module
= trace
.getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
534 if (module
== null) {
537 ITmfStateSystem ssq
= module
.getStateSystem();
542 long start
= Math
.max(startTime
, ssq
.getStartTime());
543 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
547 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
548 for (int currentThreadQuark
: currentThreadQuarks
) {
549 // adjust the query range to include the previous and following intervals
550 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
551 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
552 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
556 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
557 if (monitor
.isCanceled()) {
560 long time
= currentThreadInterval
.getStartTime();
561 if (time
!= lastEnd
) {
562 // don't create links where there are gaps in intervals due to the resolution
566 int thread
= currentThreadInterval
.getStateValue().unboxInt();
567 if (thread
> 0 && prevThread
> 0) {
568 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
569 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
570 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
572 lastEnd
= currentThreadInterval
.getEndTime() + 1;
579 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
581 } catch (StateSystemDisposedException e
) {
588 private ControlFlowEntry
findEntry(List
<TimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
589 for (TimeGraphEntry entry
: entryList
) {
590 if (entry
instanceof ControlFlowEntry
) {
591 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
592 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
593 return controlFlowEntry
;
594 } else if (entry
.hasChildren()) {
595 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
596 if (controlFlowEntry
!= null) {
597 return controlFlowEntry
;