1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
.Collections
;
18 import java
.util
.Comparator
;
19 import java
.util
.List
;
21 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
22 import org
.eclipse
.core
.runtime
.NullProgressMonitor
;
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
.core
.trace
.LttngKernelTrace
;
30 import org
.eclipse
.linuxtools
.lttng2
.kernel
.ui
.analysis
.LttngKernelAnalysisModule
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateSystemDisposedException
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
37 import org
.eclipse
.linuxtools
.tmf
.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 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
92 public ControlFlowView() {
93 super(ID
, new ControlFlowPresentationProvider());
94 setTreeColumns(COLUMN_NAMES
);
95 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
96 setFilterColumns(FILTER_COLUMN_NAMES
);
97 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
98 setEntryComparator(new ControlFlowEntryComparator());
102 protected void fillLocalToolBar(IToolBarManager manager
) {
103 super.fillLocalToolBar(manager
);
104 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
105 IDialogSettings section
= settings
.getSection(getClass().getName());
106 if (section
== null) {
107 section
= settings
.addNewSection(getClass().getName());
110 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
111 manager
.add(hideArrowsAction
);
113 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
114 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
115 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
116 manager
.add(followArrowBwdAction
);
118 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
119 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
120 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
121 manager
.add(followArrowFwdAction
);
125 protected String
getNextText() {
126 return Messages
.ControlFlowView_nextProcessActionNameText
;
130 protected String
getNextTooltip() {
131 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
135 protected String
getPrevText() {
136 return Messages
.ControlFlowView_previousProcessActionNameText
;
140 protected String
getPrevTooltip() {
141 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
144 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
147 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
151 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
152 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
153 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
154 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
156 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
159 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
164 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
175 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
178 public String
getColumnText(Object element
, int columnIndex
) {
179 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
181 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
182 return entry
.getName();
183 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
184 return Integer
.toString(entry
.getThreadId());
185 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
186 if (entry
.getParentThreadId() > 0) {
187 return Integer
.toString(entry
.getParentThreadId());
189 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
190 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
191 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
192 return entry
.getTrace().getName();
194 return ""; //$NON-NLS-1$
199 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
202 public String
getColumnText(Object element
, int columnIndex
) {
203 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
205 if (columnIndex
== 0) {
206 return entry
.getName();
207 } else if (columnIndex
== 1) {
208 return Integer
.toString(entry
.getThreadId());
210 return ""; //$NON-NLS-1$
215 // ------------------------------------------------------------------------
217 // ------------------------------------------------------------------------
220 protected void buildEventList(final ITmfTrace trace
, IProgressMonitor monitor
) {
221 setStartTime(Long
.MAX_VALUE
);
222 setEndTime(Long
.MIN_VALUE
);
224 ArrayList
<ControlFlowEntry
> rootList
= new ArrayList
<>();
225 for (ITmfTrace aTrace
: TmfTraceManager
.getTraceSet(trace
)) {
226 if (monitor
.isCanceled()) {
229 if (aTrace
instanceof LttngKernelTrace
) {
230 ArrayList
<ControlFlowEntry
> entryList
= new ArrayList
<>();
231 LttngKernelTrace ctfKernelTrace
= (LttngKernelTrace
) aTrace
;
232 LttngKernelAnalysisModule module
= ctfKernelTrace
.getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
234 if (!module
.waitForCompletion(new NullProgressMonitor())) {
237 ITmfStateSystem ssq
= module
.getStateSystem();
241 ssq
.waitUntilBuilt();
242 if (ssq
.isCancelled()) {
245 long start
= ssq
.getStartTime();
246 long end
= ssq
.getCurrentEndTime() + 1;
247 setStartTime(Math
.min(getStartTime(), start
));
248 setEndTime(Math
.max(getEndTime(), end
));
249 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
250 for (int threadQuark
: threadQuarks
) {
251 if (monitor
.isCanceled()) {
254 String threadName
= ssq
.getAttributeName(threadQuark
);
257 threadId
= Integer
.parseInt(threadName
);
258 } catch (NumberFormatException e1
) {
261 if (threadId
== 0) { // ignore the swapper thread
264 int execNameQuark
= -1;
267 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
268 } catch (AttributeNotFoundException e
) {
271 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
272 List
<ITmfStateInterval
> execNameIntervals
= ssq
.queryHistoryRange(execNameQuark
, start
, end
- 1);
273 // use monitor when available in api
274 if (monitor
.isCanceled()) {
277 ControlFlowEntry entry
= null;
278 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
279 if (monitor
.isCanceled()) {
282 if (!execNameInterval
.getStateValue().isNull() &&
283 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
284 String execName
= execNameInterval
.getStateValue().unboxStr();
285 long startTime
= execNameInterval
.getStartTime();
286 long endTime
= execNameInterval
.getEndTime() + 1;
288 if (ppidQuark
!= -1) {
289 ITmfStateInterval ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
290 ppid
= ppidInterval
.getStateValue().unboxInt();
293 entry
= new ControlFlowEntry(threadQuark
, ctfKernelTrace
, execName
, threadId
, ppid
, startTime
, endTime
);
294 entryList
.add(entry
);
296 // update the name of the entry to the
298 entry
.setName(execName
);
300 entry
.addEvent(new TimeEvent(entry
, startTime
, endTime
- startTime
));
305 } catch (AttributeNotFoundException e
) {
307 } catch (TimeRangeException e
) {
309 } catch (StateValueTypeException e
) {
311 } catch (StateSystemDisposedException e
) {
315 buildTree(entryList
, rootList
);
317 Collections
.sort(rootList
, getEntryComparator());
318 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(rootList
));
320 if (trace
.equals(getTrace())) {
324 for (ControlFlowEntry entry
: rootList
) {
325 if (monitor
.isCanceled()) {
328 buildStatusEvents(entry
.getTrace(), entry
, monitor
);
332 private static void buildTree(ArrayList
<ControlFlowEntry
> entryList
,
333 ArrayList
<ControlFlowEntry
> rootList
) {
334 for (ControlFlowEntry entry
: entryList
) {
336 if (entry
.getParentThreadId() > 0) {
337 for (ControlFlowEntry parent
: entryList
) {
338 if (parent
.getThreadId() == entry
.getParentThreadId() &&
339 entry
.getStartTime() >= parent
.getStartTime() &&
340 entry
.getStartTime() <= parent
.getEndTime()) {
341 parent
.addChild(entry
);
353 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
) {
354 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
355 ITmfStateSystem ssq
= module
.getStateSystem();
359 long start
= ssq
.getStartTime();
360 long end
= ssq
.getCurrentEndTime() + 1;
361 long resolution
= Math
.max(1, (end
- start
) / getDisplayWidth());
362 List
<ITimeEvent
> eventList
= getEventList(entry
, entry
.getStartTime(), entry
.getEndTime(), resolution
, monitor
);
363 if (monitor
.isCanceled()) {
366 entry
.setEventList(eventList
);
367 if (trace
.equals(getTrace())) {
370 for (ITimeGraphEntry child
: entry
.getChildren()) {
371 if (monitor
.isCanceled()) {
374 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
);
379 protected List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
380 List
<ITimeEvent
> eventList
= null;
381 if (!(tgentry
instanceof ControlFlowEntry
)) {
384 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
385 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
386 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
387 if (realEnd
<= realStart
) {
390 LttngKernelAnalysisModule module
= entry
.getTrace().getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
391 ITmfStateSystem ssq
= module
.getStateSystem();
396 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
397 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
398 eventList
= new ArrayList
<>(statusIntervals
.size());
399 long lastEndTime
= -1;
400 for (ITmfStateInterval statusInterval
: statusIntervals
) {
401 if (monitor
.isCanceled()) {
404 long time
= statusInterval
.getStartTime();
405 long duration
= statusInterval
.getEndTime() - time
+ 1;
408 status
= statusInterval
.getStateValue().unboxInt();
409 } catch (StateValueTypeException e
) {
412 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
413 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
415 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
416 lastEndTime
= time
+ duration
;
418 } catch (AttributeNotFoundException e
) {
420 } catch (TimeRangeException e
) {
422 } catch (StateSystemDisposedException e
) {
429 * Returns a value corresponding to the selected entry.
431 * Used in conjunction with selectEntry to change the selected entry. If one
432 * of these methods is overridden in child class, then both should be.
435 * The currently selected time
436 * @return a value identifying the entry
438 private int getSelectionValue(long time
) {
440 for (ITmfTrace trace
: fTraceManager
.getActiveTraceSet()) {
444 if (trace
instanceof LttngKernelTrace
) {
445 LttngKernelTrace ctfKernelTrace
= (LttngKernelTrace
) trace
;
446 LttngKernelAnalysisModule module
= ctfKernelTrace
.getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
447 ITmfStateSystem ssq
= module
.getStateSystem();
451 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
452 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
453 for (int currentThreadQuark
: currentThreadQuarks
) {
455 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
456 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
457 if (currentThread
> 0) {
458 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
459 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
460 if (statusInterval
.getStartTime() == time
) {
461 thread
= currentThread
;
465 } catch (AttributeNotFoundException e
) {
467 } catch (TimeRangeException e
) {
469 } catch (StateValueTypeException e
) {
471 } catch (StateSystemDisposedException e
) {
482 protected void synchingToTime(long time
) {
483 int selected
= getSelectionValue(time
);
485 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
486 if (element
instanceof ControlFlowEntry
) {
487 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
488 if (entry
.getThreadId() == selected
) {
489 getTimeGraphCombo().setSelection(entry
);
498 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
499 List
<ILinkEvent
> list
= new ArrayList
<>();
500 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
501 List
<TimeGraphEntry
> entryList
= getEntryListMap().get(getTrace());
502 if (traces
== null || entryList
== null) {
505 for (ITmfTrace trace
: traces
) {
506 if (trace
instanceof LttngKernelTrace
) {
507 LttngKernelAnalysisModule module
= trace
.getAnalysisModules(LttngKernelAnalysisModule
.class).get(LttngKernelAnalysisModule
.ID
);
508 ITmfStateSystem ssq
= module
.getStateSystem();
513 long start
= Math
.max(startTime
, ssq
.getStartTime());
514 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
518 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
519 for (int currentThreadQuark
: currentThreadQuarks
) {
520 // adjust the query range to include the previous and following intervals
521 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
522 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
523 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
527 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
528 if (monitor
.isCanceled()) {
531 long time
= currentThreadInterval
.getStartTime();
532 if (time
!= lastEnd
) {
533 // don't create links where there are gaps in intervals due to the resolution
537 int thread
= currentThreadInterval
.getStateValue().unboxInt();
538 if (thread
> 0 && prevThread
> 0) {
539 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
540 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
541 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
543 lastEnd
= currentThreadInterval
.getEndTime() + 1;
550 } catch (TimeRangeException e
) {
552 } catch (AttributeNotFoundException e
) {
554 } catch (StateValueTypeException e
) {
556 } catch (StateSystemDisposedException e
) {
564 private ControlFlowEntry
findEntry(List
<TimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
565 for (TimeGraphEntry entry
: entryList
) {
566 if (entry
instanceof ControlFlowEntry
) {
567 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
568 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
569 return controlFlowEntry
;
570 } else if (entry
.hasChildren()) {
571 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
572 if (controlFlowEntry
!= null) {
573 return controlFlowEntry
;