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
.tracecompass
.analysis
.os
.linux
.ui
.views
.controlflow
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Comparator
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
24 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
25 import org
.eclipse
.jdt
.annotation
.NonNull
;
26 import org
.eclipse
.jdt
.annotation
.Nullable
;
27 import org
.eclipse
.jface
.action
.IAction
;
28 import org
.eclipse
.jface
.action
.IToolBarManager
;
29 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
30 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.Attributes
;
31 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelAnalysis
;
32 import org
.eclipse
.tracecompass
.analysis
.os
.linux
.core
.kernelanalysis
.KernelThreadInformationProvider
;
33 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Activator
;
34 import org
.eclipse
.tracecompass
.internal
.analysis
.os
.linux
.ui
.Messages
;
35 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
36 import org
.eclipse
.tracecompass
.statesystem
.core
.StateSystemUtils
;
37 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.AttributeNotFoundException
;
38 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateSystemDisposedException
;
39 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.StateValueTypeException
;
40 import org
.eclipse
.tracecompass
.statesystem
.core
.exceptions
.TimeRangeException
;
41 import org
.eclipse
.tracecompass
.statesystem
.core
.interval
.ITmfStateInterval
;
42 import org
.eclipse
.tracecompass
.statesystem
.core
.statevalue
.ITmfStateValue
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.statesystem
.TmfStateSystemAnalysisModule
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceManager
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
47 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.timegraph
.AbstractTimeGraphView
;
48 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ILinkEvent
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeEvent
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.ITimeGraphEntry
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeEvent
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeGraphEntry
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.model
.TimeLinkEvent
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.Resolution
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.Utils
.TimeFormat
;
59 * The Control Flow view main object
62 public class ControlFlowView
extends AbstractTimeGraphView
{
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
71 public static final String ID
= "org.eclipse.tracecompass.analysis.os.linux.views.controlflow"; //$NON-NLS-1$
73 private static final String PROCESS_COLUMN
= Messages
.ControlFlowView_processColumn
;
74 private static final String TID_COLUMN
= Messages
.ControlFlowView_tidColumn
;
75 private static final String PTID_COLUMN
= Messages
.ControlFlowView_ptidColumn
;
76 private static final String BIRTH_TIME_COLUMN
= Messages
.ControlFlowView_birthTimeColumn
;
77 private static final String TRACE_COLUMN
= Messages
.ControlFlowView_traceColumn
;
79 private static final String
[] COLUMN_NAMES
= new String
[] {
87 private static final String
[] FILTER_COLUMN_NAMES
= new String
[] {
92 // Timeout between updates in the build thread in ms
93 private static final long BUILD_UPDATE_TIMEOUT
= 500;
95 // ------------------------------------------------------------------------
97 // ------------------------------------------------------------------------
102 public ControlFlowView() {
103 super(ID
, new ControlFlowPresentationProvider());
104 setTreeColumns(COLUMN_NAMES
);
105 setTreeLabelProvider(new ControlFlowTreeLabelProvider());
106 setFilterColumns(FILTER_COLUMN_NAMES
);
107 setFilterLabelProvider(new ControlFlowFilterLabelProvider());
108 setEntryComparator(new ControlFlowEntryComparator());
112 protected void fillLocalToolBar(IToolBarManager manager
) {
113 super.fillLocalToolBar(manager
);
114 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
115 IDialogSettings section
= settings
.getSection(getClass().getName());
116 if (section
== null) {
117 section
= settings
.addNewSection(getClass().getName());
120 IAction hideArrowsAction
= getTimeGraphCombo().getTimeGraphViewer().getHideArrowsAction(section
);
121 manager
.add(hideArrowsAction
);
123 IAction followArrowBwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowBwdAction();
124 followArrowBwdAction
.setText(Messages
.ControlFlowView_followCPUBwdText
);
125 followArrowBwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUBwdText
);
126 manager
.add(followArrowBwdAction
);
128 IAction followArrowFwdAction
= getTimeGraphCombo().getTimeGraphViewer().getFollowArrowFwdAction();
129 followArrowFwdAction
.setText(Messages
.ControlFlowView_followCPUFwdText
);
130 followArrowFwdAction
.setToolTipText(Messages
.ControlFlowView_followCPUFwdText
);
131 manager
.add(followArrowFwdAction
);
135 protected String
getNextText() {
136 return Messages
.ControlFlowView_nextProcessActionNameText
;
140 protected String
getNextTooltip() {
141 return Messages
.ControlFlowView_nextProcessActionToolTipText
;
145 protected String
getPrevText() {
146 return Messages
.ControlFlowView_previousProcessActionNameText
;
150 protected String
getPrevTooltip() {
151 return Messages
.ControlFlowView_previousProcessActionToolTipText
;
154 private static class ControlFlowEntryComparator
implements Comparator
<ITimeGraphEntry
> {
157 public int compare(ITimeGraphEntry o1
, ITimeGraphEntry o2
) {
161 if ((o1
instanceof ControlFlowEntry
) && (o2
instanceof ControlFlowEntry
)) {
162 ControlFlowEntry entry1
= (ControlFlowEntry
) o1
;
163 ControlFlowEntry entry2
= (ControlFlowEntry
) o2
;
164 result
= entry1
.getTrace().getStartTime().compareTo(entry2
.getTrace().getStartTime());
166 result
= entry1
.getTrace().getName().compareTo(entry2
.getTrace().getName());
169 result
= entry1
.getThreadId() < entry2
.getThreadId() ?
-1 : entry1
.getThreadId() > entry2
.getThreadId() ?
1 : 0;
174 result
= o1
.getStartTime() < o2
.getStartTime() ?
-1 : o1
.getStartTime() > o2
.getStartTime() ?
1 : 0;
185 protected static class ControlFlowTreeLabelProvider
extends TreeLabelProvider
{
188 public String
getColumnText(Object element
, int columnIndex
) {
189 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
191 if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_processColumn
)) {
192 return entry
.getName();
193 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_tidColumn
)) {
194 return Integer
.toString(entry
.getThreadId());
195 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_ptidColumn
)) {
196 if (entry
.getParentThreadId() > 0) {
197 return Integer
.toString(entry
.getParentThreadId());
199 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_birthTimeColumn
)) {
200 return Utils
.formatTime(entry
.getStartTime(), TimeFormat
.CALENDAR
, Resolution
.NANOSEC
);
201 } else if (COLUMN_NAMES
[columnIndex
].equals(Messages
.ControlFlowView_traceColumn
)) {
202 return entry
.getTrace().getName();
204 return ""; //$NON-NLS-1$
209 private static class ControlFlowFilterLabelProvider
extends TreeLabelProvider
{
212 public String
getColumnText(Object element
, int columnIndex
) {
213 ControlFlowEntry entry
= (ControlFlowEntry
) element
;
215 if (columnIndex
== 0) {
216 return entry
.getName();
217 } else if (columnIndex
== 1) {
218 return Integer
.toString(entry
.getThreadId());
220 return ""; //$NON-NLS-1$
225 // ------------------------------------------------------------------------
227 // ------------------------------------------------------------------------
230 protected void buildEventList(final ITmfTrace trace
, ITmfTrace parentTrace
, IProgressMonitor monitor
) {
231 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysis
.ID
);
236 List
<ControlFlowEntry
> entryList
= new ArrayList
<>();
237 Map
<Integer
, ControlFlowEntry
> entryMap
= new HashMap
<>();
239 long start
= ssq
.getStartTime();
240 setStartTime(Math
.min(getStartTime(), start
));
242 boolean complete
= false;
244 if (monitor
.isCanceled()) {
247 complete
= ssq
.waitUntilBuilt(BUILD_UPDATE_TIMEOUT
);
248 if (ssq
.isCancelled()) {
251 long end
= ssq
.getCurrentEndTime();
252 if (start
== end
&& !complete
) { // when complete execute one last time regardless of end time
255 setEndTime(Math
.max(getEndTime(), end
+ 1));
256 List
<Integer
> threadQuarks
= ssq
.getQuarks(Attributes
.THREADS
, "*"); //$NON-NLS-1$
257 for (int threadQuark
: threadQuarks
) {
258 if (monitor
.isCanceled()) {
261 String threadName
= ssq
.getAttributeName(threadQuark
);
264 threadId
= Integer
.parseInt(threadName
);
265 } catch (NumberFormatException e1
) {
268 if (threadId
<= 0) { // ignore the 'unknown' (-1) and swapper (0) threads
273 List
<ITmfStateInterval
> execNameIntervals
;
275 execNameQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.EXEC_NAME
);
276 execNameIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, execNameQuark
, start
, end
);
277 } catch (AttributeNotFoundException e
) {
278 /* No information on this thread (yet?), skip it for now */
280 } catch (StateSystemDisposedException e
) {
281 /* State system is closing down, no point continuing */
285 for (ITmfStateInterval execNameInterval
: execNameIntervals
) {
286 if (monitor
.isCanceled()) {
289 ControlFlowEntry entry
= entryMap
.get(threadId
);
290 if (!execNameInterval
.getStateValue().isNull() &&
291 execNameInterval
.getStateValue().getType() == ITmfStateValue
.Type
.STRING
) {
292 String execName
= execNameInterval
.getStateValue().unboxStr();
293 long startTime
= execNameInterval
.getStartTime();
294 long endTime
= execNameInterval
.getEndTime() + 1;
297 ITmfStateInterval ppidInterval
= null;
299 int ppidQuark
= ssq
.getQuarkRelative(threadQuark
, Attributes
.PPID
);
300 ppidInterval
= StateSystemUtils
.queryUntilNonNullValue(ssq
, ppidQuark
, startTime
, endTime
);
301 } catch (AttributeNotFoundException e
) {
302 /* No info, keep PPID at -1 */
306 if (ppidInterval
!= null) {
307 ppid
= ppidInterval
.getStateValue().unboxInt();
309 entry
= new ControlFlowEntry(threadQuark
, trace
, execName
, threadId
, ppid
, startTime
, endTime
);
310 entryList
.add(entry
);
311 entryMap
.put(threadId
, entry
);
313 // update the name of the entry to the latest
315 entry
.setName(execName
);
316 entry
.updateEndTime(endTime
);
319 entryMap
.remove(threadId
);
324 updateTree(entryList
, parentTrace
);
326 if (parentTrace
.equals(getTrace())) {
330 for (ControlFlowEntry entry
: entryList
) {
331 if (monitor
.isCanceled()) {
334 buildStatusEvents(entry
.getTrace(), entry
, monitor
, start
, end
);
341 private void updateTree(List
<ControlFlowEntry
> entryList
, ITmfTrace parentTrace
) {
342 List
<TimeGraphEntry
> rootListToAdd
= new ArrayList
<>();
343 List
<TimeGraphEntry
> rootListToRemove
= new ArrayList
<>();
344 List
<TimeGraphEntry
> rootList
= getEntryList(parentTrace
);
346 for (ControlFlowEntry entry
: entryList
) {
347 boolean root
= (entry
.getParent() == null);
348 if (root
&& entry
.getParentThreadId() > 0) {
349 for (ControlFlowEntry parent
: entryList
) {
351 * Associate the parent entry only if their time overlap. A
352 * child entry may start before its parent, for example at
353 * the beginning of the trace if a parent has not yet
354 * appeared in the state system. We just want to make sure
355 * that the entry didn't start after the parent ended or
356 * ended before the parent started.
358 if (parent
.getThreadId() == entry
.getParentThreadId() &&
359 !(entry
.getStartTime() > parent
.getEndTime() ||
360 entry
.getEndTime() < parent
.getStartTime())) {
361 parent
.addChild(entry
);
363 if (rootList
!= null && rootList
.contains(entry
)) {
364 rootListToRemove
.add(entry
);
370 if (root
&& (rootList
== null || !rootList
.contains(entry
))) {
371 rootListToAdd
.add(entry
);
375 addToEntryList(parentTrace
, rootListToAdd
);
376 removeFromEntryList(parentTrace
, rootListToRemove
);
379 private void buildStatusEvents(ITmfTrace trace
, ControlFlowEntry entry
, @NonNull IProgressMonitor monitor
, long start
, long end
) {
380 if (start
< entry
.getEndTime() && end
> entry
.getStartTime()) {
381 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(entry
.getTrace(), KernelAnalysis
.ID
);
386 long startTime
= Math
.max(start
, entry
.getStartTime());
387 long endTime
= Math
.min(end
+ 1, entry
.getEndTime());
388 long resolution
= Math
.max(1, (end
- ssq
.getStartTime()) / getDisplayWidth());
389 List
<ITimeEvent
> eventList
= getEventList(entry
, startTime
, endTime
, resolution
, monitor
);
390 if (eventList
== null) {
393 for (ITimeEvent event
: eventList
) {
394 entry
.addEvent(event
);
396 if (trace
.equals(getTrace())) {
400 for (ITimeGraphEntry child
: entry
.getChildren()) {
401 if (monitor
.isCanceled()) {
404 buildStatusEvents(trace
, (ControlFlowEntry
) child
, monitor
, start
, end
);
409 protected @Nullable List
<ITimeEvent
> getEventList(TimeGraphEntry tgentry
, long startTime
, long endTime
, long resolution
, IProgressMonitor monitor
) {
410 List
<ITimeEvent
> eventList
= null;
411 if (!(tgentry
instanceof ControlFlowEntry
)) {
414 ControlFlowEntry entry
= (ControlFlowEntry
) tgentry
;
415 final long realStart
= Math
.max(startTime
, entry
.getStartTime());
416 final long realEnd
= Math
.min(endTime
, entry
.getEndTime());
417 if (realEnd
<= realStart
) {
420 KernelAnalysis kernelAnalysis
= TmfTraceUtils
.getAnalysisModuleOfClass(entry
.getTrace(), KernelAnalysis
.class, KernelAnalysis
.ID
);
421 if (kernelAnalysis
== null) {
422 // ITmfStateSystem ssq = TmfStateSystemAnalysisModule.getStateSystem(entry.getTrace(), KernelAnalysis.ID);
423 // if (ssq == null) {
427 List
<ITmfStateInterval
> statusIntervals
= KernelThreadInformationProvider
.getStatusIntervalsForThread(
428 kernelAnalysis
, checkNotNull(entry
.getThreadId()), realStart
, realEnd
, resolution
, monitor
);
429 eventList
= new ArrayList
<>(statusIntervals
.size());
430 long lastEndTime
= -1;
431 for (ITmfStateInterval statusInterval
: statusIntervals
) {
432 if (monitor
.isCanceled()) {
435 long time
= statusInterval
.getStartTime();
436 long duration
= statusInterval
.getEndTime() - time
+ 1;
439 status
= statusInterval
.getStateValue().unboxInt();
440 } catch (StateValueTypeException e
) {
443 if (lastEndTime
!= time
&& lastEndTime
!= -1) {
444 eventList
.add(new TimeEvent(entry
, lastEndTime
, time
- lastEndTime
));
446 eventList
.add(new TimeEvent(entry
, time
, duration
, status
));
447 lastEndTime
= time
+ duration
;
449 } catch (TimeRangeException e
) {
450 Activator
.getDefault().logError(e
.getMessage());
456 * Returns a value corresponding to the selected entry.
458 * Used in conjunction with synchingToTime to change the selected entry. If
459 * one of these methods is overridden in child class, then both should be.
462 * The currently selected time
463 * @return a value identifying the entry
465 private int getSelectionValue(long time
) {
467 ITmfTrace
[] traces
= TmfTraceManager
.getTraceSet(getTrace());
468 if (traces
== null) {
471 for (ITmfTrace trace
: traces
) {
478 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysis
.ID
);
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
) {
535 ITmfStateSystem ssq
= TmfStateSystemAnalysisModule
.getStateSystem(trace
, KernelAnalysis
.ID
);
540 long start
= Math
.max(startTime
, ssq
.getStartTime());
541 long end
= Math
.min(endTime
, ssq
.getCurrentEndTime());
545 List
<Integer
> currentThreadQuarks
= ssq
.getQuarks(Attributes
.CPUS
, "*", Attributes
.CURRENT_THREAD
); //$NON-NLS-1$
546 for (int currentThreadQuark
: currentThreadQuarks
) {
547 // adjust the query range to include the previous and following intervals
548 long qstart
= Math
.max(ssq
.querySingleState(start
, currentThreadQuark
).getStartTime() - 1, ssq
.getStartTime());
549 long qend
= Math
.min(ssq
.querySingleState(end
, currentThreadQuark
).getEndTime() + 1, ssq
.getCurrentEndTime());
550 List
<ITmfStateInterval
> currentThreadIntervals
= StateSystemUtils
.queryHistoryRange(ssq
, currentThreadQuark
, qstart
, qend
, resolution
, monitor
);
554 for (ITmfStateInterval currentThreadInterval
: currentThreadIntervals
) {
555 if (monitor
.isCanceled()) {
558 long time
= currentThreadInterval
.getStartTime();
559 if (time
!= lastEnd
) {
560 // don't create links where there are gaps in intervals due to the resolution
564 int thread
= currentThreadInterval
.getStateValue().unboxInt();
565 if (thread
> 0 && prevThread
> 0) {
566 ITimeGraphEntry prevEntry
= findEntry(entryList
, trace
, prevThread
);
567 ITimeGraphEntry nextEntry
= findEntry(entryList
, trace
, thread
);
568 list
.add(new TimeLinkEvent(prevEntry
, nextEntry
, prevEnd
, time
- prevEnd
, 0));
570 lastEnd
= currentThreadInterval
.getEndTime() + 1;
577 } catch (TimeRangeException
| AttributeNotFoundException
| StateValueTypeException e
) {
579 } catch (StateSystemDisposedException e
) {
586 private ControlFlowEntry
findEntry(List
<?
extends ITimeGraphEntry
> entryList
, ITmfTrace trace
, int threadId
) {
587 for (ITimeGraphEntry entry
: entryList
) {
588 if (entry
instanceof ControlFlowEntry
) {
589 ControlFlowEntry controlFlowEntry
= (ControlFlowEntry
) entry
;
590 if (controlFlowEntry
.getThreadId() == threadId
&& controlFlowEntry
.getTrace() == trace
) {
591 return controlFlowEntry
;
592 } else if (entry
.hasChildren()) {
593 controlFlowEntry
= findEntry(entry
.getChildren(), trace
, threadId
);
594 if (controlFlowEntry
!= null) {
595 return controlFlowEntry
;