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 java
.util
.concurrent
.Semaphore
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.IStatus
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.eclipse
.core
.runtime
.jobs
.Job
;
20 import org
.eclipse
.jdt
.annotation
.NonNull
;
21 import org
.eclipse
.jface
.action
.Action
;
22 import org
.eclipse
.jface
.action
.GroupMarker
;
23 import org
.eclipse
.jface
.action
.IAction
;
24 import org
.eclipse
.jface
.action
.IMenuListener
;
25 import org
.eclipse
.jface
.action
.IMenuManager
;
26 import org
.eclipse
.jface
.action
.IToolBarManager
;
27 import org
.eclipse
.jface
.action
.MenuManager
;
28 import org
.eclipse
.jface
.action
.Separator
;
29 import org
.eclipse
.jface
.dialogs
.IDialogSettings
;
30 import org
.eclipse
.jface
.resource
.ImageDescriptor
;
31 import org
.eclipse
.jface
.viewers
.ISelection
;
32 import org
.eclipse
.jface
.viewers
.IStructuredSelection
;
33 import org
.eclipse
.swt
.SWT
;
34 import org
.eclipse
.swt
.events
.MenuDetectEvent
;
35 import org
.eclipse
.swt
.events
.MenuDetectListener
;
36 import org
.eclipse
.swt
.events
.MouseAdapter
;
37 import org
.eclipse
.swt
.events
.MouseEvent
;
38 import org
.eclipse
.swt
.widgets
.Composite
;
39 import org
.eclipse
.swt
.widgets
.Display
;
40 import org
.eclipse
.swt
.widgets
.Menu
;
41 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.core
.callgraph
.CallGraphAnalysis
;
42 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.Activator
;
43 import org
.eclipse
.tracecompass
.internal
.analysis
.timing
.ui
.callgraph
.CallGraphAnalysisUI
;
44 import org
.eclipse
.tracecompass
.segmentstore
.core
.ISegment
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSelectionRangeUpdatedSignal
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceClosedSignal
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfTraceSelectedSignal
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTraceUtils
;
52 import org
.eclipse
.tracecompass
.tmf
.ui
.editors
.ITmfTraceEditor
;
53 import org
.eclipse
.tracecompass
.tmf
.ui
.views
.TmfView
;
54 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphPresentationProvider
;
55 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.TimeGraphViewer
;
56 import org
.eclipse
.tracecompass
.tmf
.ui
.widgets
.timegraph
.widgets
.TimeGraphControl
;
57 import org
.eclipse
.ui
.IActionBars
;
58 import org
.eclipse
.ui
.IEditorPart
;
59 import org
.eclipse
.ui
.IWorkbenchActionConstants
;
61 import com
.google
.common
.annotations
.VisibleForTesting
;
64 * View to display the flame graph .This uses the flameGraphNode tree generated
65 * by CallGraphAnalysisUI.
67 * @author Sonia Farrah
69 public class FlameGraphView
extends TmfView
{
74 public static final String ID
= FlameGraphView
.class.getPackage().getName() + ".flamegraphView"; //$NON-NLS-1$
76 private static final String SORT_OPTION_KEY
= "sort.option"; //$NON-NLS-1$
77 private static final ImageDescriptor SORT_BY_NAME_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_alpha.gif"); //$NON-NLS-1$
78 private static final ImageDescriptor SORT_BY_NAME_REV_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_alpha_rev.gif"); //$NON-NLS-1$
79 private static final ImageDescriptor SORT_BY_ID_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_num.gif"); //$NON-NLS-1$
80 private static final ImageDescriptor SORT_BY_ID_REV_ICON
= Activator
.getDefault().getImageDescripterFromPath("icons/etool16/sort_num_rev.gif"); //$NON-NLS-1$
82 private TimeGraphViewer fTimeGraphViewer
;
84 private FlameGraphContentProvider fTimeGraphContentProvider
;
86 private TimeGraphPresentationProvider fPresentationProvider
;
88 private ITmfTrace fTrace
;
90 private final @NonNull MenuManager fEventMenuManager
= new MenuManager();
91 private Action fSortByNameAction
;
92 private Action fSortByIdAction
;
94 * A plain old semaphore is used since different threads will be competing
95 * for the same resource.
97 private final Semaphore fLock
= new Semaphore(1);
102 public FlameGraphView() {
107 public void createPartControl(Composite parent
) {
108 super.createPartControl(parent
);
109 fTimeGraphViewer
= new TimeGraphViewer(parent
, SWT
.NONE
);
110 fTimeGraphContentProvider
= new FlameGraphContentProvider();
111 fPresentationProvider
= new FlameGraphPresentationProvider();
112 fTimeGraphViewer
.setTimeGraphContentProvider(fTimeGraphContentProvider
);
113 fTimeGraphViewer
.setTimeGraphProvider(fPresentationProvider
);
114 IEditorPart editor
= getSite().getPage().getActiveEditor();
115 if (editor
instanceof ITmfTraceEditor
) {
116 ITmfTrace trace
= ((ITmfTraceEditor
) editor
).getTrace();
118 traceSelected(new TmfTraceSelectedSignal(this, trace
));
121 contributeToActionBars();
124 getSite().setSelectionProvider(fTimeGraphViewer
.getSelectionProvider());
125 createTimeEventContextMenu();
126 fTimeGraphViewer
.getTimeGraphControl().addMouseListener(new MouseAdapter() {
128 public void mouseDoubleClick(MouseEvent e
) {
129 TimeGraphControl timeGraphControl
= getTimeGraphViewer().getTimeGraphControl();
130 ISelection selection
= timeGraphControl
.getSelection();
131 if (selection
instanceof IStructuredSelection
) {
132 for (Object object
: ((IStructuredSelection
) selection
).toList()) {
133 if (object
instanceof FlamegraphEvent
) {
134 FlamegraphEvent event
= (FlamegraphEvent
) object
;
135 long startTime
= event
.getTime();
136 long endTime
= startTime
+ event
.getDuration();
137 getTimeGraphViewer().setStartFinishTime(startTime
, endTime
);
147 * Get the time graph viewer
149 * @return the time graph viewer
152 public TimeGraphViewer
getTimeGraphViewer() {
153 return fTimeGraphViewer
;
157 * Handler for the trace selected signal
160 * The incoming signal
163 public void traceSelected(final TmfTraceSelectedSignal signal
) {
164 fTrace
= signal
.getTrace();
165 if (fTrace
!= null) {
166 CallGraphAnalysis flamegraphModule
= TmfTraceUtils
.getAnalysisModuleOfClass(fTrace
, CallGraphAnalysis
.class, CallGraphAnalysisUI
.ID
);
167 buildFlameGraph(flamegraphModule
);
172 * Get the necessary data for the flame graph and display it
174 * @param callGraphAnalysis
175 * the callGraphAnalysis
178 public void buildFlameGraph(CallGraphAnalysis callGraphAnalysis
) {
180 * Note for synchronization:
182 * Acquire the lock at entry. then we have 4 places to release it
184 * 1- if the lock failed
186 * 2- if the data is null and we have no UI to update
188 * 3- if the request is cancelled before it gets to the display
190 * 4- on a clean execution
194 } catch (InterruptedException e
) {
195 Activator
.getDefault().logError(e
.getMessage(), e
);
198 if (callGraphAnalysis
== null) {
199 fTimeGraphViewer
.setInput(null);
203 fTimeGraphViewer
.setInput(callGraphAnalysis
.getSegmentStore());
204 callGraphAnalysis
.schedule();
205 Job j
= new Job(Messages
.CallGraphAnalysis_Execution
) {
208 protected IStatus
run(IProgressMonitor monitor
) {
209 if (monitor
.isCanceled()) {
211 return Status
.CANCEL_STATUS
;
213 callGraphAnalysis
.waitForCompletion(monitor
);
214 Display
.getDefault().asyncExec(() -> {
215 fTimeGraphViewer
.setInput(callGraphAnalysis
.getThreadNodes());
216 fTimeGraphViewer
.resetStartFinishTime();
219 return Status
.OK_STATUS
;
226 * Await the next refresh
228 * @throws InterruptedException
229 * something took too long
232 public void waitForUpdate() throws InterruptedException
{
234 * wait for the semaphore to be available, then release it immediately
241 * Trace is closed: clear the data structures and the view
244 * the signal received
247 public void traceClosed(final TmfTraceClosedSignal signal
) {
248 if (signal
.getTrace() == fTrace
) {
249 fTimeGraphViewer
.setInput(null);
254 public void setFocus() {
255 fTimeGraphViewer
.setFocus();
258 // ------------------------------------------------------------------------
260 // ------------------------------------------------------------------------
262 private void createTimeEventContextMenu() {
263 fEventMenuManager
.setRemoveAllWhenShown(true);
264 TimeGraphControl timeGraphControl
= fTimeGraphViewer
.getTimeGraphControl();
265 final Menu timeEventMenu
= fEventMenuManager
.createContextMenu(timeGraphControl
);
267 timeGraphControl
.addTimeGraphEntryMenuListener(new MenuDetectListener() {
269 public void menuDetected(MenuDetectEvent event
) {
271 * The TimeGraphControl will call the TimeGraphEntryMenuListener
272 * before the TimeEventMenuListener. We need to clear the menu
273 * for the case the selection was done on the namespace where
274 * the time event listener below won't be called afterwards.
276 timeGraphControl
.setMenu(null);
280 timeGraphControl
.addTimeEventMenuListener(new MenuDetectListener() {
282 public void menuDetected(MenuDetectEvent event
) {
283 Menu menu
= timeEventMenu
;
284 if (event
.data
instanceof FlamegraphEvent
) {
285 timeGraphControl
.setMenu(menu
);
288 timeGraphControl
.setMenu(null);
293 fEventMenuManager
.addMenuListener(new IMenuListener() {
295 public void menuAboutToShow(IMenuManager manager
) {
296 fillTimeEventContextMenu(fEventMenuManager
);
297 fEventMenuManager
.add(new GroupMarker(IWorkbenchActionConstants
.MB_ADDITIONS
));
300 getSite().registerContextMenu(fEventMenuManager
, fTimeGraphViewer
.getSelectionProvider());
307 * a menuManager to fill
309 protected void fillTimeEventContextMenu(@NonNull IMenuManager menuManager
) {
310 ISelection selection
= getSite().getSelectionProvider().getSelection();
311 if (selection
instanceof IStructuredSelection
) {
312 for (Object object
: ((IStructuredSelection
) selection
).toList()) {
313 if (object
instanceof FlamegraphEvent
) {
314 final FlamegraphEvent flamegraphEvent
= (FlamegraphEvent
) object
;
315 menuManager
.add(new Action(Messages
.FlameGraphView_GotoMaxDuration
) {
318 ISegment maxSeg
= flamegraphEvent
.getStatistics().getMaxSegment();
319 TmfSelectionRangeUpdatedSignal sig
= new TmfSelectionRangeUpdatedSignal(this, TmfTimestamp
.fromNanos(maxSeg
.getStart()), TmfTimestamp
.fromNanos(maxSeg
.getEnd()));
324 menuManager
.add(new Action(Messages
.FlameGraphView_GotoMinDuration
) {
327 ISegment minSeg
= flamegraphEvent
.getStatistics().getMinSegment();
328 TmfSelectionRangeUpdatedSignal sig
= new TmfSelectionRangeUpdatedSignal(this, TmfTimestamp
.fromNanos(minSeg
.getStart()), TmfTimestamp
.fromNanos(minSeg
.getEnd()));
337 private void contributeToActionBars() {
338 IActionBars bars
= getViewSite().getActionBars();
339 fillLocalToolBar(bars
.getToolBarManager());
342 private void fillLocalToolBar(IToolBarManager manager
) {
343 manager
.add(getSortByNameAction());
344 manager
.add(getSortByIdAction());
345 manager
.add(new Separator());
348 private Action
getSortByNameAction() {
349 if (fSortByNameAction
== null) {
350 fSortByNameAction
= new Action(Messages
.FlameGraph_SortByThreadName
, IAction
.AS_CHECK_BOX
) {
353 SortOption sortOption
= fTimeGraphContentProvider
.getSortOption();
354 if (sortOption
== SortOption
.BY_NAME
) {
355 setSortOption(SortOption
.BY_NAME_REV
);
357 setSortOption(SortOption
.BY_NAME
);
361 fSortByNameAction
.setToolTipText(Messages
.FlameGraph_SortByThreadName
);
362 fSortByNameAction
.setImageDescriptor(SORT_BY_NAME_ICON
);
364 return fSortByNameAction
;
367 private Action
getSortByIdAction() {
368 if (fSortByIdAction
== null) {
369 fSortByIdAction
= new Action(Messages
.FlameGraph_SortByThreadId
, IAction
.AS_CHECK_BOX
) {
372 SortOption sortOption
= fTimeGraphContentProvider
.getSortOption();
373 if (sortOption
== SortOption
.BY_ID
) {
374 setSortOption(SortOption
.BY_ID_REV
);
376 setSortOption(SortOption
.BY_ID
);
380 fSortByIdAction
.setToolTipText(Messages
.FlameGraph_SortByThreadId
);
381 fSortByIdAction
.setImageDescriptor(SORT_BY_ID_ICON
);
383 return fSortByIdAction
;
386 private void setSortOption(SortOption sortOption
) {
388 getSortByNameAction().setChecked(false);
389 getSortByNameAction().setImageDescriptor(SORT_BY_NAME_ICON
);
390 getSortByIdAction().setChecked(false);
391 getSortByIdAction().setImageDescriptor(SORT_BY_ID_ICON
);
393 if (sortOption
.equals(SortOption
.BY_NAME
)) {
394 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_NAME
);
395 getSortByNameAction().setChecked(true);
396 } else if (sortOption
.equals(SortOption
.BY_NAME_REV
)) {
397 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_NAME_REV
);
398 getSortByNameAction().setChecked(true);
399 getSortByNameAction().setImageDescriptor(SORT_BY_NAME_REV_ICON
);
400 } else if (sortOption
.equals(SortOption
.BY_ID
)) {
401 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_ID
);
402 getSortByIdAction().setChecked(true);
403 } else if (sortOption
.equals(SortOption
.BY_ID_REV
)) {
404 fTimeGraphContentProvider
.setSortOption(SortOption
.BY_ID_REV
);
405 getSortByIdAction().setChecked(true);
406 getSortByIdAction().setImageDescriptor(SORT_BY_ID_REV_ICON
);
409 fTimeGraphViewer
.refresh();
412 private void saveSortOption() {
413 SortOption sortOption
= fTimeGraphContentProvider
.getSortOption();
414 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
415 IDialogSettings section
= settings
.getSection(getClass().getName());
416 if (section
== null) {
417 section
= settings
.addNewSection(getClass().getName());
419 section
.put(SORT_OPTION_KEY
, sortOption
.name());
422 private void loadSortOption() {
423 IDialogSettings settings
= Activator
.getDefault().getDialogSettings();
424 IDialogSettings section
= settings
.getSection(getClass().getName());
425 if (section
== null) {
428 String sortOption
= section
.get(SORT_OPTION_KEY
);
429 if (sortOption
== null) {
432 setSortOption(SortOption
.fromName(sortOption
));