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
.getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, 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
271 List
<ITmfStateInterval
> execNameIntervals
;
273 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
274 execNameIntervals
= ssq
.queryHistoryRange(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 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
373 if (module
== null) {
376 ITmfStateSystem ssq
= module
.getStateSystem();
381 long startTime
= Math
.max(start
, entry
.getStartTime());
382 long endTime
= Math
.min(end
+ 1, entry
.getEndTime());
383 long resolution
= Math
.max(1, (end
- ssq
.getStartTime()) / getDisplayWidth());
384 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, monitor
);
385 if (monitor
.isCanceled()) {
388 for (ITimeEvent event
: eventList
) {
389 entry
.addEvent(event
);
391 if (trace
.equals(getTrace())) {
395 for (ITimeGraphEntry child
: entry
.getChildren()) {
396 if (monitor
.isCanceled()) {
399 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
, start
, end
);
404 protected List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
405 List
<ITimeEvent
> eventList
= null;
406 if (!(tgentry
instanceof ControlFlowEntry
)) {
409 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
410 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
411 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
412 if (realEnd
<= realStart
) {
415 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
416 if (module
== null) {
419 ITmfStateSystem ssq
= module
.getStateSystem();
424 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
425 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
426 eventList
= new ArrayList
<>(statusIntervals
.size());
427 long lastEndTime
= -1;
428 for (ITmfStateInterval statusInterval
: statusIntervals
) {
429 if (monitor
.isCanceled()) {
432 long time
= statusInterval
.getStartTime();
433 long duration
= statusInterval
.getEndTime() - time
+ 1;
436 status
= statusInterval
.getStateValue().unboxInt();
437 } catch (StateValueTypeException e
) {
440 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
441 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
443 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
444 lastEndTime
= time
+ duration
;
446 } catch (AttributeNotFoundException
| TimeRangeException e
) {
448 } catch (StateSystemDisposedException e
) {
455 * Returns a value corresponding to the selected entry.
457 * Used in conjunction with synchingToTime to change the selected entry. If
458 * one of these methods is overridden in child class, then both should be.
461 * The currently selected time
462 * @return a value identifying the entry
464 private int getSelectionValue(long time
) {
466 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
467 if (traces
== null) {
470 for (ITmfTrace trace
: traces
) {
474 LttngKernelAnalysisModule module
= trace
.getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
475 if (module
== null) {
478 ITmfStateSystem ssq
= module
.getStateSystem();
482 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
483 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
484 for (int currentThreadQuark
: currentThreadQuarks
) {
486 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
487 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
488 if (currentThread
> 0) {
489 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
490 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
491 if (statusInterval
.getStartTime() == time
) {
492 thread
= currentThread
;
496 } catch (AttributeNotFoundException
| TimeRangeException
| StateValueTypeException e
) {
498 } catch (StateSystemDisposedException e
) {
508 protected void synchingToTime(long time
) {
509 int selected
= getSelectionValue(time
);
511 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
512 if (element
instanceof ControlFlowEntry
) {
513 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
514 if (entry
.getThreadId() == selected
) {
515 getTimeGraphCombo().setSelection(entry
);
524 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
525 List
<ILinkEvent
> list
= new ArrayList
<>();
526 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
527 List
<TimeGraphEntry
> entryList
= getEntryList(getTrace());
528 if (traces
== null || entryList
== null) {
531 for (ITmfTrace trace
: traces
) {
532 LttngKernelAnalysisModule module
= trace
.getAnalysisModuleOfClass(LttngKernelAnalysisModule
.class, LttngKernelAnalysisModule
.ID
);
533 if (module
== null) {
536 ITmfStateSystem ssq
= module
.getStateSystem();
541 long start
= Math
.max(startTime
, ssq
.getStartTime());
542 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
546 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
547 for (int currentThreadQuark
: currentThreadQuarks
) {
548 // adjust the query range to include the previous and following intervals
549 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
550 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
551 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
555 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
556 if (monitor
.isCanceled()) {
559 long time
= currentThreadInterval
.getStartTime();
560 if (time
!= lastEnd
) {
561 // don't create links where there are gaps in intervals due to the resolution
565 int thread
= currentThreadInterval
.getStateValue().unboxInt();
566 if (thread
> 0 && prevThread
> 0) {
567 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
568 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
569 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
571 lastEnd
= currentThreadInterval
.getEndTime() + 1;
578 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
580 } catch (StateSystemDisposedException e
) {
587 private ControlFlowEntry
findEntry(List
<TimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
588 for (TimeGraphEntry entry
: entryList
) {
589 if (entry
instanceof ControlFlowEntry
) {
590 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
591 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
592 return controlFlowEntry
;
593 } else if (entry
.hasChildren()) {
594 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
595 if (controlFlowEntry
!= null) {
596 return controlFlowEntry
;