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
.jface
.action
.IAction
;
24 import org
.eclipse
.jface
.action
.IToolBarManager
;
25 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
26 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Activator
;
28 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Messages
;
29 import org
.eclipse
.linuxtools
.lttng2
.kernel
.ui
.analysis
.LttngKernelAnalysisModule
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateSystemDisposedException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
48 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
51 * The Control Flow view main object
54 public class ControlFlowView
extends AbstractTimeGraphView
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
63 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
65 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
66 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
67 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
68 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
69 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
71 private static final String
[] COLUMN_NAMES
= new String
[] {
79 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
84 // Timeout between updates in the build thread in ms
85 private static final long BUILD_UPDATE_TIMEOUT
= 500;
87 // ------------------------------------------------------------------------
89 // ------------------------------------------------------------------------
94 public ControlFlowView() {
95 super(ID
, new ControlFlowPresentationProvider());
96 setTreeColumns(COLUMN_NAMES
);
97 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
98 setFilterColumns(FILTER_COLUMN_NAMES
);
99 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
100 setEntryComparator(new ControlFlowEntryComparator());
104 protected void fillLocalToolBar(IToolBarManager manager
) {
105 super.fillLocalToolBar(manager
);
106 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
107 IDialogSettings section
= settings
.getSection(getClass().getName());
108 if (section
== null) {
109 section
= settings
.addNewSection(getClass().getName());
112 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
113 manager
.add(hideArrowsAction
);
115 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
116 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
117 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
118 manager
.add(followArrowBwdAction
);
120 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
121 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
122 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
123 manager
.add(followArrowFwdAction
);
127 protected String
getNextText() {
128 return Messages
.ControlFlowView_nextProcessActionNameText
;
132 protected String
getNextTooltip() {
133 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
137 protected String
getPrevText() {
138 return Messages
.ControlFlowView_previousProcessActionNameText
;
142 protected String
getPrevTooltip() {
143 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
146 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
149 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
153 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
154 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
155 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
156 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
158 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
161 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
166 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
177 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
180 public String
getColumnText(Object element
, int columnIndex
) {
181 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
183 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
184 return entry
.getName();
185 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
186 return Integer
.toString(entry
.getThreadId());
187 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
188 if (entry
.getParentThreadId() > 0) {
189 return Integer
.toString(entry
.getParentThreadId());
191 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
192 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
193 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
194 return entry
.getTrace().getName();
196 return ""; //$NON-NLS-1$
201 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
204 public String
getColumnText(Object element
, int columnIndex
) {
205 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
207 if (columnIndex
== 0) {
208 return entry
.getName();
209 } else if (columnIndex
== 1) {
210 return Integer
.toString(entry
.getThreadId());
212 return ""; //$NON-NLS-1$
217 // ------------------------------------------------------------------------
219 // ------------------------------------------------------------------------
222 protected void buildEventList(final ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
223 LttngKernelAnalysisModule module
= trace
.getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
224 if (module
== null) {
228 module
.waitForInitialization();
229 ITmfStateSystem ssq
= module
.getStateSystem();
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
270 int execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
271 List
<ITmfStateInterval
> execNameIntervals
= ssq
.queryHistoryRange(execNameQuark
, start
, end
);
272 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
273 if (monitor
.isCanceled()) {
276 ControlFlowEntry entry
= entryMap
.get(threadId
);
277 if (!execNameInterval
.getStateValue().isNull() &&
278 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
279 String execName
= execNameInterval
.getStateValue().unboxStr();
280 long startTime
= execNameInterval
.getStartTime();
281 long endTime
= execNameInterval
.getEndTime() + 1;
284 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
285 ITmfStateInterval ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
286 if (!ppidInterval
.getStateValue().isNull()) {
287 ppid
= ppidInterval
.getStateValue().unboxInt();
289 entry
= new ControlFlowEntry(threadQuark
, trace
, execName
, threadId
, ppid
, startTime
, endTime
);
290 entryList
.add(entry
);
291 entryMap
.put(threadId
, entry
);
293 // update the name of the entry to the latest execName
294 entry
.setName(execName
);
295 entry
.updateEndTime(endTime
);
298 entryMap
.remove(threadId
);
301 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
303 } catch (StateSystemDisposedException e
) {
308 updateTree(entryList
, parentTrace
);
310 if (parentTrace
.equals(getTrace())) {
314 for (ControlFlowEntry entry
: entryList
) {
315 if (monitor
.isCanceled()) {
318 buildStatusEvents(entry
.getTrace(), entry
, monitor
, start
, end
);
325 private void updateTree(List
<ControlFlowEntry
> entryList
, ITmfTrace parentTrace
) {
326 List
<TimeGraphEntry
> rootListToAdd
= new ArrayList
<>();
327 List
<TimeGraphEntry
> rootListToRemove
= new ArrayList
<>();
328 List
<TimeGraphEntry
> rootList
= getEntryList(parentTrace
);
330 for (ControlFlowEntry entry
: entryList
) {
331 boolean root
= (entry
.getParent() == null);
332 if (root
&& entry
.getParentThreadId() > 0) {
333 for (ControlFlowEntry parent
: entryList
) {
334 if (parent
.getThreadId() == entry
.getParentThreadId() &&
335 entry
.getStartTime() >= parent
.getStartTime() &&
336 entry
.getStartTime() <= parent
.getEndTime()) {
337 parent
.addChild(entry
);
339 if (rootList
!= null && rootList
.contains(entry
)) {
340 rootListToRemove
.add(entry
);
346 if (root
&& (rootList
== null || !rootList
.contains(entry
))) {
347 rootListToAdd
.add(entry
);
351 addToEntryList(parentTrace
, rootListToAdd
);
352 removeFromEntryList(parentTrace
, rootListToRemove
);
355 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
, long start
, long end
) {
356 if (start
< entry
.getEndTime() && end
> entry
.getStartTime()) {
357 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
358 if (module
== null) {
361 ITmfStateSystem ssq
= module
.getStateSystem();
366 long startTime
= Math
.max(start
, entry
.getStartTime());
367 long endTime
= Math
.min(end
+ 1, entry
.getEndTime());
368 long resolution
= Math
.max(1, (end
- ssq
.getStartTime()) / getDisplayWidth());
369 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, monitor
);
370 if (monitor
.isCanceled()) {
373 for (ITimeEvent event
: eventList
) {
374 entry
.addEvent(event
);
376 if (trace
.equals(getTrace())) {
380 for (ITimeGraphEntry child
: entry
.getChildren()) {
381 if (monitor
.isCanceled()) {
384 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
, start
, end
);
389 protected List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
390 List
<ITimeEvent
> eventList
= null;
391 if (!(tgentry
instanceof ControlFlowEntry
)) {
394 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
395 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
396 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
397 if (realEnd
<= realStart
) {
400 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
401 if (module
== null) {
404 ITmfStateSystem ssq
= module
.getStateSystem();
409 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
410 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
411 eventList
= new ArrayList
<>(statusIntervals
.size());
412 long lastEndTime
= -1;
413 for (ITmfStateInterval statusInterval
: statusIntervals
) {
414 if (monitor
.isCanceled()) {
417 long time
= statusInterval
.getStartTime();
418 long duration
= statusInterval
.getEndTime() - time
+ 1;
421 status
= statusInterval
.getStateValue().unboxInt();
422 } catch (StateValueTypeException e
) {
425 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
426 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
428 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
429 lastEndTime
= time
+ duration
;
431 } catch (AttributeNotFoundException
| TimeRangeException e
) {
433 } catch (StateSystemDisposedException e
) {
440 * Returns a value corresponding to the selected entry.
442 * Used in conjunction with synchingToTime to change the selected entry. If
443 * one of these methods is overridden in child class, then both should be.
446 * The currently selected time
447 * @return a value identifying the entry
449 private int getSelectionValue(long time
) {
451 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
452 if (traces
== null) {
455 for (ITmfTrace trace
: traces
) {
459 LttngKernelAnalysisModule module
= trace
.getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
460 if (module
== null) {
463 ITmfStateSystem ssq
= module
.getStateSystem();
467 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
468 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
469 for (int currentThreadQuark
: currentThreadQuarks
) {
471 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
472 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
473 if (currentThread
> 0) {
474 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
475 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
476 if (statusInterval
.getStartTime() == time
) {
477 thread
= currentThread
;
481 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
483 } catch (StateSystemDisposedException e
) {
493 protected void synchingToTime(long time
) {
494 int selected
= getSelectionValue(time
);
496 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
497 if (element
instanceof ControlFlowEntry
) {
498 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
499 if (entry
.getThreadId() == selected
) {
500 getTimeGraphCombo().setSelection(entry
);
509 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
510 List
<ILinkEvent
> list
= new ArrayList
<>();
511 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
512 List
<TimeGraphEntry
> entryList
= getEntryList(getTrace());
513 if (traces
== null || entryList
== null) {
516 for (ITmfTrace trace
: traces
) {
517 LttngKernelAnalysisModule module
= trace
.getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
518 if (module
== null) {
521 ITmfStateSystem ssq
= module
.getStateSystem();
526 long start
= Math
.max(startTime
, ssq
.getStartTime());
527 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
531 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
532 for (int currentThreadQuark
: currentThreadQuarks
) {
533 // adjust the query range to include the previous and following intervals
534 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
535 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
536 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
540 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
541 if (monitor
.isCanceled()) {
544 long time
= currentThreadInterval
.getStartTime();
545 if (time
!= lastEnd
) {
546 // don't create links where there are gaps in intervals due to the resolution
550 int thread
= currentThreadInterval
.getStateValue().unboxInt();
551 if (thread
> 0 && prevThread
> 0) {
552 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
553 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
554 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
556 lastEnd
= currentThreadInterval
.getEndTime() + 1;
563 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
565 } catch (StateSystemDisposedException e
) {
572 private ControlFlowEntry
findEntry(List
<TimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
573 for (TimeGraphEntry entry
: entryList
) {
574 if (entry
instanceof ControlFlowEntry
) {
575 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
576 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
577 return controlFlowEntry
;
578 } else if (entry
.hasChildren()) {
579 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
580 if (controlFlowEntry
!= null) {
581 return controlFlowEntry
;