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
);
214 if (!ssq
.waitUntilBuilt()) {
217 long start
= ssq
.getStartTime();
218 long end
= ssq
.getCurrentEndTime() + 1;
219 setStartTime(Math
.min(getStartTime(), start
));
220 setEndTime(Math
.max(getEndTime(), end
));
221 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
222 for (int threadQuark
: threadQuarks
) {
223 if (monitor
.isCanceled()) {
226 String threadName
= ssq
.getAttributeName(threadQuark
);
229 threadId
= Integer
.parseInt(threadName
);
230 } catch (NumberFormatException e1
) {
233 if (threadId
== 0) { // ignore the swapper thread
236 int execNameQuark
= -1;
239 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
240 } catch (AttributeNotFoundException e
) {
243 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
244 List
<ITmfStateInterval
> execNameIntervals
= ssq
.queryHistoryRange(execNameQuark
, start
, end
- 1);
245 // use monitor when available in api
246 if (monitor
.isCanceled()) {
249 ControlFlowEntry entry
= null;
250 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
251 if (monitor
.isCanceled()) {
254 if (!execNameInterval
.getStateValue().isNull() &&
255 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
256 String execName
= execNameInterval
.getStateValue().unboxStr();
257 long startTime
= execNameInterval
.getStartTime();
258 long endTime
= execNameInterval
.getEndTime() + 1;
260 if (ppidQuark
!= -1) {
261 ITmfStateInterval ppidInterval
= ssq
.querySingleState(startTime
, ppidQuark
);
262 ppid
= ppidInterval
.getStateValue().unboxInt();
265 entry
= new ControlFlowEntry(threadQuark
, ctfKernelTrace
, execName
, threadId
, ppid
, startTime
, endTime
);
266 entryList
.add(entry
);
268 // update the name of the entry to the
270 entry
.setName(execName
);
272 entry
.addEvent(new TimeEvent(entry
, startTime
, endTime
- startTime
));
277 } catch (AttributeNotFoundException e
) {
279 } catch (TimeRangeException e
) {
281 } catch (StateValueTypeException e
) {
283 } catch (StateSystemDisposedException e
) {
287 buildTree(entryList
, rootList
);
289 Collections
.sort(rootList
, getEntryComparator());
290 putEntryList(trace
, new ArrayList
<TimeGraphEntry
>(rootList
));
292 if (trace
.equals(getTrace())) {
296 for (ControlFlowEntry entry
: rootList
) {
297 if (monitor
.isCanceled()) {
300 buildStatusEvents(entry
.getTrace(), entry
, monitor
);
304 private static void buildTree(ArrayList
<ControlFlowEntry
> entryList
,
305 ArrayList
<ControlFlowEntry
> rootList
) {
306 for (ControlFlowEntry entry
: entryList
) {
308 if (entry
.getParentThreadId() > 0) {
309 for (ControlFlowEntry parent
: entryList
) {
310 if (parent
.getThreadId() == entry
.getParentThreadId() &&
311 entry
.getStartTime() >= parent
.getStartTime() &&
312 entry
.getStartTime() <= parent
.getEndTime()) {
313 parent
.addChild(entry
);
325 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, IProgressMonitor monitor
) {
326 ITmfStateSystem ssq
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
328 long start
= ssq
.getStartTime();
329 long end
= ssq
.getCurrentEndTime() + 1;
330 long resolution
= Math
.max(1, (end
- start
) / getDisplayWidth());
331 List
<ITimeEvent
> eventList
= getEventList(entry
, entry
.getStartTime(), entry
.getEndTime(), resolution
, monitor
);
332 if (monitor
.isCanceled()) {
335 entry
.setEventList(eventList
);
336 if (trace
.equals(getTrace())) {
339 for (ITimeGraphEntry child
: entry
.getChildren()) {
340 if (monitor
.isCanceled()) {
343 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
);
348 protected List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
349 List
<ITimeEvent
> eventList
= null;
350 if (!(tgentry
instanceof ControlFlowEntry
)) {
353 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
354 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
355 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
356 if (realEnd
<= realStart
) {
359 ITmfStateSystem ssq
= entry
.getTrace().getStateSystems().get(LttngKernelTrace
.STATE_ID
);
361 int statusQuark
= ssq
.getQuarkRelative(entry
.getThreadQuark(), Attributes
.STATUS
);
362 List
<ITmfStateInterval
> statusIntervals
= ssq
.queryHistoryRange(statusQuark
, realStart
, realEnd
- 1, resolution
, monitor
);
363 eventList
= new ArrayList
<>(statusIntervals
.size());
364 long lastEndTime
= -1;
365 for (ITmfStateInterval statusInterval
: statusIntervals
) {
366 if (monitor
.isCanceled()) {
369 long time
= statusInterval
.getStartTime();
370 long duration
= statusInterval
.getEndTime() - time
+ 1;
373 status
= statusInterval
.getStateValue().unboxInt();
374 } catch (StateValueTypeException e
) {
377 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
378 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
380 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
381 lastEndTime
= time
+ duration
;
383 } catch (AttributeNotFoundException e
) {
385 } catch (TimeRangeException e
) {
387 } catch (StateSystemDisposedException e
) {
394 * Returns a value corresponding to the selected entry.
396 * Used in conjunction with selectEntry to change the selected entry. If one
397 * of these methods is overridden in child class, then both should be.
400 * The currently selected time
401 * @return a value identifying the entry
403 private int getSelectionValue(long time
) {
405 for (ITmfTrace trace
: fTraceManager
.getActiveTraceSet()) {
409 if (trace
instanceof LttngKernelTrace
) {
410 LttngKernelTrace ctfKernelTrace
= (LttngKernelTrace
) trace
;
411 ITmfStateSystem ssq
= ctfKernelTrace
.getStateSystems().get(LttngKernelTrace
.STATE_ID
);
412 if (time
>= ssq
.getStartTime() && time
<= ssq
.getCurrentEndTime()) {
413 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
414 for (int currentThreadQuark
: currentThreadQuarks
) {
416 ITmfStateInterval currentThreadInterval
= ssq
.querySingleState(time
, currentThreadQuark
);
417 int currentThread
= currentThreadInterval
.getStateValue().unboxInt();
418 if (currentThread
> 0) {
419 int statusQuark
= ssq
.getQuarkAbsolute(Attributes
.THREADS
, Integer
.toString(currentThread
), Attributes
.STATUS
);
420 ITmfStateInterval statusInterval
= ssq
.querySingleState(time
, statusQuark
);
421 if (statusInterval
.getStartTime() == time
) {
422 thread
= currentThread
;
426 } catch (AttributeNotFoundException e
) {
428 } catch (TimeRangeException e
) {
430 } catch (StateValueTypeException e
) {
432 } catch (StateSystemDisposedException e
) {
443 protected void synchingToTime(long time
) {
444 int selected
= getSelectionValue(time
);
446 for (Object element
: getTimeGraphViewer().getExpandedElements()) {
447 if (element
instanceof ControlFlowEntry
) {
448 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
449 if (entry
.getThreadId() == selected
) {
450 getTimeGraphCombo().setSelection(entry
);
459 protected List
<ILinkEvent
> getLinkList(long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
460 List
<ILinkEvent
> list
= new ArrayList
<>();
461 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
462 List
<TimeGraphEntry
> entryList
= getEntryListMap().get(getTrace());
463 if (traces
== null || entryList
== null) {
466 for (ITmfTrace trace
: traces
) {
467 if (trace
instanceof LttngKernelTrace
) {
468 ITmfStateSystem ssq
= trace
.getStateSystems().get(LttngKernelTrace
.STATE_ID
);
470 long start
= Math
.max(startTime
, ssq
.getStartTime());
471 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
475 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
476 for (int currentThreadQuark
: currentThreadQuarks
) {
477 // adjust the query range to include the previous and following intervals
478 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
479 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
480 List
<ITmfStateInterval
> currentThreadIntervals
= ssq
.queryHistoryRange(currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
484 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
485 if (monitor
.isCanceled()) {
488 long time
= currentThreadInterval
.getStartTime();
489 if (time
!= lastEnd
) {
490 // don't create links where there are gaps in intervals due to the resolution
494 int thread
= currentThreadInterval
.getStateValue().unboxInt();
495 if (thread
> 0 && prevThread
> 0) {
496 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
497 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
498 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
500 lastEnd
= currentThreadInterval
.getEndTime() + 1;
507 } catch (TimeRangeException e
) {
509 } catch (AttributeNotFoundException e
) {
511 } catch (StateValueTypeException e
) {
513 } catch (StateSystemDisposedException e
) {
521 private ControlFlowEntry
findEntry(List
<TimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
522 for (TimeGraphEntry entry
: entryList
) {
523 if (entry
instanceof ControlFlowEntry
) {
524 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
525 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
526 return controlFlowEntry
;
527 } else if (entry
.hasChildren()) {
528 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
529 if (controlFlowEntry
!= null) {
530 return controlFlowEntry
;