1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
4 * All rights reserved. This program and the accompanying materials are made
5 * 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
11 *******************************************************************************/
12 package org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.flamegraph
;
14 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
15 import org
.eclipse
.core
.runtime
.IStatus
;
16 import org
.eclipse
.core
.runtime
.Status
;
17 import org
.eclipse
.core
.runtime
.jobs
.Job
;
18 import org
.eclipse
.jdt
.annotation
.NonNull
;
19 import org
.eclipse
.jface
.action
.Action
;
20 import org
.eclipse
.jface
.action
.GroupMarker
;
21 import org
.eclipse
.jface
.action
.IAction
;
22 import org
.eclipse
.jface
.action
.IMenuListener
;
23 import org
.eclipse
.jface
.action
.IMenuManager
;
24 import org
.eclipse
.jface
.action
.IToolBarManager
;
25 import org
.eclipse
.jface
.action
.MenuManager
;
26 import org
.eclipse
.jface
.action
.Separator
;
27 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
28 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
29 import org
.eclipse
.jface
.viewers
.ISelection
;
30 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
31 import org
.eclipse
.swt
.SWT
;
32 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
33 import org
.eclipse
.swt
.events
.MenuDetectListener
;
34 import org
.eclipse
.swt
.widgets
.Composite
;
35 import org
.eclipse
.swt
.widgets
.Display
;
36 import org
.eclipse
.swt
.widgets
.Menu
;
37 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.callgraph
.CallGraphAnalysis
;
38 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.Activator
;
39 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.callgraph
.CallGraphAnalysisUI
;
40 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
49 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.ITmfTraceEditor
;
50 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TmfView
;
51 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphPresentationProvider
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphViewer
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphControl
;
54 import org
.eclipse
.ui
.IActionBars
;
55 import org
.eclipse
.ui
.IEditorPart
;
56 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
59 * View to display the flame graph .This uses the flameGraphNode tree generated
60 * by CallGraphAnalysisUI.
62 * @author Sonia Farrah
64 public class FlameGraphView
extends TmfView
{
69 public static final String ID
= FlameGraphView
.class.getPackage().getName() + ".flamegraphView"; //$NON-NLS-1$
71 private static final String SORT_OPTION_KEY
= "sort.option"; //$NON-NLS-1$
72 private static final ImageDescriptor SORT_BY_NAME_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_alpha.gif"); //$NON-NLS-1$
73 private static final ImageDescriptor SORT_BY_NAME_REV_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_alpha_rev.gif"); //$NON-NLS-1$
74 private static final ImageDescriptor SORT_BY_ID_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_num.gif"); //$NON-NLS-1$
75 private static final ImageDescriptor SORT_BY_ID_REV_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_num_rev.gif"); //$NON-NLS-1$
78 private TimeGraphViewer fTimeGraphViewer
;
80 private FlameGraphContentProvider fTimeGraphContentProvider
;
82 private TimeGraphPresentationProvider fPresentationProvider
;
84 private ITmfTrace fTrace
;
86 private final @NonNull MenuManager fEventMenuManager
= new MenuManager();
87 private Action fSortByNameAction
;
88 private Action fSortByIdAction
;
93 public FlameGraphView() {
98 public void createPartControl(Composite parent
) {
99 super.createPartControl(parent
);
100 fTimeGraphViewer
= new TimeGraphViewer(parent
, SWT
.NONE
);
101 fTimeGraphContentProvider
= new FlameGraphContentProvider();
102 fPresentationProvider
= new FlameGraphPresentationProvider();
103 fTimeGraphViewer
.setTimeGraphContentProvider(fTimeGraphContentProvider
);
104 fTimeGraphViewer
.setTimeGraphProvider(fPresentationProvider
);
105 IEditorPart editor
= getSite().getPage().getActiveEditor();
106 if (editor
instanceof ITmfTraceEditor
) {
107 ITmfTrace trace
= ((ITmfTraceEditor
) editor
).getTrace();
109 traceSelected(new TmfTraceSelectedSignal(this, trace
));
112 contributeToActionBars();
115 getSite().setSelectionProvider(fTimeGraphViewer
.getSelectionProvider());
116 createTimeEventContextMenu();
121 * Handler for the trace opened signal
124 * The incoming signal
127 public void TraceOpened(TmfTraceOpenedSignal signal
) {
128 fTrace
= signal
.getTrace();
129 if (fTrace
!= null) {
130 CallGraphAnalysis flamegraphModule
= TmfTraceUtils
.getAnalysisModuleOfClass(fTrace
, CallGraphAnalysis
.class, CallGraphAnalysisUI
.ID
);
131 buildFlameGraph(flamegraphModule
);
136 * Handler for the trace selected signal
139 * The incoming signal
142 public void traceSelected(final TmfTraceSelectedSignal signal
) {
143 fTrace
= signal
.getTrace();
144 if (fTrace
!= null) {
145 CallGraphAnalysis flamegraphModule
= TmfTraceUtils
.getAnalysisModuleOfClass(fTrace
, CallGraphAnalysis
.class, CallGraphAnalysisUI
.ID
);
146 buildFlameGraph(flamegraphModule
);
151 * Get the necessary data for the flame graph and display it
153 * @param flamegraphModule
154 * the callGraphAnalysis
156 private void buildFlameGraph(CallGraphAnalysis callGraphAnalysis
) {
157 fTimeGraphViewer
.setInput(null);
158 callGraphAnalysis
.schedule();
159 Job j
= new Job(Messages
.CallGraphAnalysis_Execution
) {
162 protected IStatus
run(IProgressMonitor monitor
) {
163 if (monitor
.isCanceled()) {
164 return Status
.CANCEL_STATUS
;
166 callGraphAnalysis
.waitForCompletion(monitor
);
167 Display
.getDefault().asyncExec(() -> {
168 fTimeGraphViewer
.setInput(callGraphAnalysis
.getThreadNodes());
169 fTimeGraphViewer
.resetStartFinishTime();
171 return Status
.OK_STATUS
;
178 * Trace is closed: clear the data structures and the view
181 * the signal received
184 public void traceClosed(final TmfTraceClosedSignal signal
) {
185 if (signal
.getTrace() == fTrace
) {
186 fTimeGraphViewer
.setInput(null);
191 public void setFocus() {
192 fTimeGraphViewer
.setFocus();
195 // ------------------------------------------------------------------------
197 // ------------------------------------------------------------------------
199 private void createTimeEventContextMenu() {
200 fEventMenuManager
.setRemoveAllWhenShown(true);
201 TimeGraphControl timeGraphControl
= fTimeGraphViewer
.getTimeGraphControl();
202 final Menu timeEventMenu
= fEventMenuManager
.createContextMenu(timeGraphControl
);
204 timeGraphControl
.addTimeGraphEntryMenuListener(new MenuDetectListener() {
206 public void menuDetected(MenuDetectEvent event
) {
208 * The TimeGraphControl will call the TimeGraphEntryMenuListener
209 * before the TimeEventMenuListener. We need to clear the menu
210 * for the case the selection was done on the namespace where
211 * the time event listener below won't be called afterwards.
213 timeGraphControl
.setMenu(null);
217 timeGraphControl
.addTimeEventMenuListener(new MenuDetectListener() {
219 public void menuDetected(MenuDetectEvent event
) {
220 Menu menu
= timeEventMenu
;
221 if (event
.data
instanceof FlamegraphEvent
) {
222 timeGraphControl
.setMenu(menu
);
225 timeGraphControl
.setMenu(null);
230 fEventMenuManager
.addMenuListener(new IMenuListener() {
232 public void menuAboutToShow(IMenuManager manager
) {
233 fillTimeEventContextMenu(fEventMenuManager
);
234 fEventMenuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
237 getSite().registerContextMenu(fEventMenuManager
, fTimeGraphViewer
.getSelectionProvider());
244 * a menuManager to fill
246 protected void fillTimeEventContextMenu(@NonNull IMenuManager menuManager
) {
247 ISelection selection
= getSite().getSelectionProvider().getSelection();
248 if (selection
instanceof IStructuredSelection
) {
249 for (Object object
: ((IStructuredSelection
) selection
).toList()) {
250 if (object
instanceof FlamegraphEvent
) {
251 final FlamegraphEvent flamegraphEvent
= (FlamegraphEvent
) object
;
252 menuManager
.add(new Action(Messages
.FlameGraphView_GotoMaxDuration
) {
255 ISegment maxSeg
= flamegraphEvent
.getStatistics().getMaxSegment();
256 TmfSelectionRangeUpdatedSignal sig
= new TmfSelectionRangeUpdatedSignal(this, TmfTimestamp
.fromNanos(maxSeg
.getStart()), TmfTimestamp
.fromNanos(maxSeg
.getEnd()));
261 menuManager
.add(new Action(Messages
.FlameGraphView_GotoMinDuration
) {
264 ISegment minSeg
= flamegraphEvent
.getStatistics().getMinSegment();
265 TmfSelectionRangeUpdatedSignal sig
= new TmfSelectionRangeUpdatedSignal(this, TmfTimestamp
.fromNanos(minSeg
.getStart()), TmfTimestamp
.fromNanos(minSeg
.getEnd()));
274 private void contributeToActionBars() {
275 IActionBars bars
= getViewSite().getActionBars();
276 fillLocalToolBar(bars
.getToolBarManager());
279 private void fillLocalToolBar(IToolBarManager manager
) {
280 manager
.add(getSortByNameAction());
281 manager
.add(getSortByIdAction());
282 manager
.add(new Separator());
285 private Action
getSortByNameAction() {
286 if (fSortByNameAction
== null) {
287 fSortByNameAction
= new Action(Messages
.FlameGraph_SortByThreadName
, IAction
.AS_CHECK_BOX
) {
290 SortOption sortOption
= fTimeGraphContentProvider
.getSortOption();
291 if (sortOption
== SortOption
.BY_NAME
) {
292 setSortOption(SortOption
.BY_NAME_REV
);
294 setSortOption(SortOption
.BY_NAME
);
298 fSortByNameAction
.setToolTipText(Messages
.FlameGraph_SortByThreadName
);
299 fSortByNameAction
.setImageDescriptor(SORT_BY_NAME_ICON
);
301 return fSortByNameAction
;
304 private Action
getSortByIdAction() {
305 if (fSortByIdAction
== null) {
306 fSortByIdAction
= new Action(Messages
.FlameGraph_SortByThreadId
, IAction
.AS_CHECK_BOX
) {
309 SortOption sortOption
= fTimeGraphContentProvider
.getSortOption();
310 if (sortOption
== SortOption
.BY_ID
) {
311 setSortOption(SortOption
.BY_ID_REV
);
313 setSortOption(SortOption
.BY_ID
);
317 fSortByIdAction
.setToolTipText(Messages
.FlameGraph_SortByThreadId
);
318 fSortByIdAction
.setImageDescriptor(SORT_BY_ID_ICON
);
320 return fSortByIdAction
;
323 private void setSortOption(SortOption sortOption
) {
325 getSortByNameAction().setChecked(false);
326 getSortByNameAction().setImageDescriptor(SORT_BY_NAME_ICON
);
327 getSortByIdAction().setChecked(false);
328 getSortByIdAction().setImageDescriptor(SORT_BY_ID_ICON
);
330 if (sortOption
.equals(SortOption
.BY_NAME
)) {
331 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_NAME
);
332 getSortByNameAction().setChecked(true);
333 } else if (sortOption
.equals(SortOption
.BY_NAME_REV
)) {
334 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_NAME_REV
);
335 getSortByNameAction().setChecked(true);
336 getSortByNameAction().setImageDescriptor(SORT_BY_NAME_REV_ICON
);
337 } else if (sortOption
.equals(SortOption
.BY_ID
)) {
338 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_ID
);
339 getSortByIdAction().setChecked(true);
340 } else if (sortOption
.equals(SortOption
.BY_ID_REV
)) {
341 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_ID_REV
);
342 getSortByIdAction().setChecked(true);
343 getSortByIdAction().setImageDescriptor(SORT_BY_ID_REV_ICON
);
346 fTimeGraphViewer
.refresh();
349 private void saveSortOption() {
350 SortOption sortOption
= fTimeGraphContentProvider
.getSortOption();
351 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
352 IDialogSettings section
= settings
.getSection(getClass().getName());
353 if (section
== null) {
354 section
= settings
.addNewSection(getClass().getName());
356 section
.put(SORT_OPTION_KEY
, sortOption
.name());
359 private void loadSortOption() {
360 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
361 IDialogSettings section
= settings
.getSection(getClass().getName());
362 if (section
== null) {
365 String sortOption
= section
.get(SORT_OPTION_KEY
);
366 if (sortOption
== null) {
369 setSortOption(SortOption
.fromName(sortOption
));