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
.jface
.action
.IAction
;
23 import org
.eclipse
.jface
.action
.IToolBarManager
;
24 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
25 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.core
.Attributes
;
26 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Activator
;
27 import org
.eclipse
.linuxtools
.internal
.lttng2
.kernel
.ui
.Messages
;
28 import org
.eclipse
.linuxtools
.lttng2
.kernel
.core
.trace
.LttngKernelTrace
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.AttributeNotFoundException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateSystemDisposedException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.StateValueTypeException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TimeRangeException
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.interval
.ITmfStateInterval
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.ITmfStateSystem
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.statevalue
.ITmfStateValue
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTraceManager
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
41 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
42 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
43 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
45 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
47 import org
.eclipse
.linuxtools
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
50 * The Control Flow view main object
53 public class ControlFlowView
extends AbstractTimeGraphView
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
62 public static final String ID
= "org.eclipse.linuxtools.lttng2.kernel.ui.views.controlflow"; //$NON-NLS-1$
64 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
65 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
66 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
67 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
68 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
70 private static final String
[] COLUMN_NAMES
= new String
[] {
78 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
90 public ControlFlowView() {
91 super(ID
, new ControlFlowPresentationProvider());
92 setTreeColumns(COLUMN_NAMES
);
93 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
94 setFilterColumns(FILTER_COLUMN_NAMES
);
95 setEntryComparator(new ControlFlowEntryComparator());
99 protected void fillLocalToolBar(IToolBarManager manager
) {
100 super.fillLocalToolBar(manager
);
101 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
102 IDialogSettings section
= settings
.getSection(getClass().getName());
103 if (section
== null) {
104 section
= settings
.addNewSection(getClass().getName());
107 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
108 manager
.add(hideArrowsAction
);
110 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
111 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
112 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
113 manager
.add(followArrowBwdAction
);
115 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
116 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
117 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
118 manager
.add(followArrowFwdAction
);
122 protected String
getNextText() {
123 return Messages
.ControlFlowView_nextProcessActionNameText
;
127 protected String
getNextTooltip() {
128 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
132 protected String
getPrevText() {
133 return Messages
.ControlFlowView_previousProcessActionNameText
;
137 protected String
getPrevTooltip() {
138 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
141 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
144 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
148 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
149 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
150 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
151 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
153 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
156 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
161 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
172 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
175 public String
getColumnText(Object element
, int columnIndex
) {
176 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
178 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
179 return entry
.getName();
180 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
181 return Integer
.toString(entry
.getThreadId());
182 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
183 if (entry
.getParentThreadId() > 0) {
184 return Integer
.toString(entry
.getParentThreadId());
186 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
187 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
188 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
189 return entry
.getTrace().getName();
191 return ""; //$NON-NLS-1$
196 // ------------------------------------------------------------------------
198 // ------------------------------------------------------------------------
201 protected void buildEventList(final ITmfTrace trace
, IProgressMonitor monitor
) {
202 setStartTime(Long
.MAX_VALUE
);
203 setEndTime(Long
.MIN_VALUE
);
205 ArrayList
<ControlFlowEntry
> rootList
= new ArrayList
<>();
206 for (ITmfTrace aTrace
: TmfTraceManager
.getTraceSet(trace
)) {
207 if (monitor
.isCanceled()) {
210 if (aTrace
instanceof LttngKernelTrace
) {
211 ArrayList
<ControlFlowEntry
> entryList
= new ArrayList
<>();
212 LttngKernelTrace ctfKernelTrace
= (LttngKernelTrace
) aTrace
;
213 ITmfStateSystem ssq
= ctfKernelTrace
.getStateSystems().get(LttngKernelTrace
.STATE_ID
);
217 ssq
.waitUntilBuilt();
218 if (ssq
.isCancelled()) {
221 long start
= ssq
.getStartTime();
222 long end
= ssq
.getCurrentEndTime() + 1;
223 setStartTime(Math
.min(getStartTime(), start
));
224 setEndTime(Math
.max(getEndTime(), end
));
225 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
226 for (int threadQuark
: threadQuarks
) {
227 if (monitor
.isCanceled()) {
230 String threadName
= ssq
.getAttributeName(threadQuark
);
233 threadId
= Integer
.parseInt(threadName
);
234 } catch (NumberFormatException e1
) {
237 if (threadId
== 0) { // ignore the swapper thread
240 int execNameQuark
= -1;
243 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
244 } catch (AttributeNotFoundException e
) {
247 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
248 List
<ITmfStateInterval
> execNameIntervals
= ssq
.queryHistoryRange(execNameQuark
, start
, end
- 1);
249 // use monitor when available in api
250 if (monitor
.isCanceled()) {
253 ControlFlowEntry entry
= null;
254 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
255 if (monitor
.isCanceled()) {
258 if (!execNameInterval
.getStateValue().isNull() &&
259 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
260 String execName
= execNameInterval
.getStateValue().unboxStr();
261 long startTime
= execNameInterval
.getStartTime();
262 long endTime
= execNameInterval
.getEndTime() + 1;
264 if (ppidQuark
!= -1) {
265 ITmfStateInterval ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
266 ppid
= ppidInterval
.getStateValue().unboxInt();
269 entry
= new ControlFlowEntry(threadQuark
, ctfKernelTrace
, execName
, threadId
, ppid
, startTime
, endTime
);
270 entryList
.add(entry
);
272 // update the name of the entry to the
274 entry
.setName(execName
);
276 entry
.addEvent(new TimeEvent(entry
, startTime
, endTime
- startTime
));
281 } catch (AttributeNotFoundException e
) {
283 } catch (TimeRangeException e
) {
285 } catch (StateValueTypeException e
) {
287 } catch (StateSystemDisposedException e
) {
291 buildTree(entryList
, rootList
);
293 Collections
.sort(rootList
, getEntryComparator());
294 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(rootList
));
296 if (trace
.equals(getTrace())) {
300 for (ControlFlowEntry entry
: rootList
) {
301 if (monitor
.isCanceled()) {
304 buildStatusEvents(entry
.getTrace(), entry
, monitor
);
308 private static void buildTree(ArrayList
<ControlFlowEntry
> entryList
,
309 ArrayList
<ControlFlowEntry
> rootList
) {
310 for (ControlFlowEntry entry
: entryList
) {
312 if (entry
.getParentThreadId() > 0) {
313 for (ControlFlowEntry parent
: entryList
) {
314 if (parent
.getThreadId() == entry
.getParentThreadId() &&
315 entry
.getStartTime() >= parent
.getStartTime() &&
316 entry
.getStartTime() <= parent
.getEndTime()) {
317 parent
.addChild(entry
);
329 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
) {
330 ITmfStateSystem ssq
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
332 long start
= ssq
.getStartTime();
333 long end
= ssq
.getCurrentEndTime() + 1;
334 long resolution
= Math
.max(1, (end
- start
) / getDisplayWidth());
335 List
<ITimeEvent
> eventList
= getEventList(entry
, entry
.getStartTime(), entry
.getEndTime(), resolution
, monitor
);
336 if (monitor
.isCanceled()) {
339 entry
.setEventList(eventList
);
340 if (trace
.equals(getTrace())) {
343 for (ITimeGraphEntry child
: entry
.getChildren()) {
344 if (monitor
.isCanceled()) {
347 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
);
352 protected List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
353 List
<ITimeEvent
> eventList
= null;
354 if (!(tgentry
instanceof ControlFlowEntry
)) {
357 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
358 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
359 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
360 if (realEnd
<= realStart
) {
363 ITmfStateSystem ssq
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
365 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
366 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
367 eventList
= new ArrayList
<>(statusIntervals
.size());
368 long lastEndTime
= -1;
369 for (ITmfStateInterval statusInterval
: statusIntervals
) {
370 if (monitor
.isCanceled()) {
373 long time
= statusInterval
.getStartTime();
374 long duration
= statusInterval
.getEndTime() - time
+ 1;
377 status
= statusInterval
.getStateValue().unboxInt();
378 } catch (StateValueTypeException e
) {
381 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
382 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
384 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
385 lastEndTime
= time
+ duration
;
387 } catch (AttributeNotFoundException e
) {
389 } catch (TimeRangeException e
) {
391 } catch (StateSystemDisposedException e
) {
398 * Returns a value corresponding to the selected entry.
400 * Used in conjunction with selectEntry to change the selected entry. If one
401 * of these methods is overridden in child class, then both should be.
404 * The currently selected time
405 * @return a value identifying the entry
407 private int getSelectionValue(long time
) {
409 for (ITmfTrace trace
: fTraceManager
.getActiveTraceSet()) {
413 if (trace
instanceof LttngKernelTrace
) {
414 LttngKernelTrace ctfKernelTrace
= (LttngKernelTrace
) trace
;
415 ITmfStateSystem ssq
= ctfKernelTrace
.getStateSystems().get(LttngKernelTrace
.STATE_ID
);
416 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
417 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
418 for (int currentThreadQuark
: currentThreadQuarks
) {
420 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
421 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
422 if (currentThread
> 0) {
423 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
424 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
425 if (statusInterval
.getStartTime() == time
) {
426 thread
= currentThread
;
430 } catch (AttributeNotFoundException e
) {
432 } catch (TimeRangeException e
) {
434 } catch (StateValueTypeException e
) {
436 } catch (StateSystemDisposedException e
) {
447 protected void synchingToTime(long time
) {
448 int selected
= getSelectionValue(time
);
450 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
451 if (element
instanceof ControlFlowEntry
) {
452 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
453 if (entry
.getThreadId() == selected
) {
454 getTimeGraphCombo().setSelection(entry
);
463 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
464 List
<ILinkEvent
> list
= new ArrayList
<>();
465 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
466 List
<TimeGraphEntry
> entryList
= getEntryListMap().get(getTrace());
467 if (traces
== null || entryList
== null) {
470 for (ITmfTrace trace
: traces
) {
471 if (trace
instanceof LttngKernelTrace
) {
472 ITmfStateSystem ssq
= trace
.getStateSystems().get(LttngKernelTrace
.STATE_ID
);
474 long start
= Math
.max(startTime
, ssq
.getStartTime());
475 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
479 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
480 for (int currentThreadQuark
: currentThreadQuarks
) {
481 // adjust the query range to include the previous and following intervals
482 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
483 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
484 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
488 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
489 if (monitor
.isCanceled()) {
492 long time
= currentThreadInterval
.getStartTime();
493 if (time
!= lastEnd
) {
494 // don't create links where there are gaps in intervals due to the resolution
498 int thread
= currentThreadInterval
.getStateValue().unboxInt();
499 if (thread
> 0 && prevThread
> 0) {
500 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
501 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
502 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
504 lastEnd
= currentThreadInterval
.getEndTime() + 1;
511 } catch (TimeRangeException e
) {
513 } catch (AttributeNotFoundException e
) {
515 } catch (StateValueTypeException e
) {
517 } catch (StateSystemDisposedException e
) {
525 private ControlFlowEntry
findEntry(List
<TimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
526 for (TimeGraphEntry entry
: entryList
) {
527 if (entry
instanceof ControlFlowEntry
) {
528 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
529 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
530 return controlFlowEntry
;
531 } else if (entry
.hasChildren()) {
532 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
533 if (controlFlowEntry
!= null) {
534 return controlFlowEntry
;