1 /*******************************************************************************
2 * Copyright (c) 2009 Ericsson
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 * Yann N. Dauphin (dhaemon@gmail.com) - Implementation
11 * Francois Chouinard (fchouinard@gmail.com) - Initial API
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.statistics
;
16 import java
.text
.DecimalFormat
;
17 import java
.util
.Arrays
;
18 import java
.util
.HashSet
;
21 import org
.eclipse
.jface
.viewers
.ColumnLabelProvider
;
22 import org
.eclipse
.jface
.viewers
.ITreeContentProvider
;
23 import org
.eclipse
.jface
.viewers
.TreeViewer
;
24 import org
.eclipse
.jface
.viewers
.TreeViewerColumn
;
25 import org
.eclipse
.jface
.viewers
.Viewer
;
26 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
27 import org
.eclipse
.linuxtools
.lttng
.control
.LttngCoreProviderFactory
;
28 import org
.eclipse
.linuxtools
.lttng
.request
.ILttngSyntEventRequest
;
29 import org
.eclipse
.linuxtools
.lttng
.state
.evProcessor
.AbsEventToHandlerResolver
;
30 import org
.eclipse
.linuxtools
.lttng
.ui
.TraceDebug
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.model
.trange
.ItemContainer
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.common
.AbsTimeUpdateView
;
33 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.common
.ParamsUpdater
;
34 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.statistics
.evProcessor
.StatsTimeCountHandlerFactory
;
35 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.statistics
.model
.StatisticsTreeNode
;
36 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.statistics
.model
.StatisticsTreeRootFactory
;
37 import org
.eclipse
.linuxtools
.tmf
.event
.TmfEvent
;
38 import org
.eclipse
.linuxtools
.tmf
.event
.TmfTimeRange
;
39 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
40 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
.ExecutionType
;
41 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfExperimentSelectedSignal
;
42 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
43 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
44 import org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.timeAnalysis
.model
.ITmfTimeAnalysisEntry
;
45 import org
.eclipse
.swt
.SWT
;
46 import org
.eclipse
.swt
.events
.SelectionAdapter
;
47 import org
.eclipse
.swt
.events
.SelectionEvent
;
48 import org
.eclipse
.swt
.graphics
.Color
;
49 import org
.eclipse
.swt
.graphics
.Cursor
;
50 import org
.eclipse
.swt
.graphics
.Image
;
51 import org
.eclipse
.swt
.layout
.FillLayout
;
52 import org
.eclipse
.swt
.widgets
.Composite
;
53 import org
.eclipse
.swt
.widgets
.Display
;
54 import org
.eclipse
.swt
.widgets
.Event
;
55 import org
.eclipse
.swt
.widgets
.Listener
;
56 import org
.eclipse
.ui
.ISharedImages
;
57 import org
.eclipse
.ui
.PlatformUI
;
60 * <b><u>StatisticsView</u></b>
62 * The Statistics View displays statistics for traces.
64 * It is implemented according to the MVC pattern. - The model is a
65 * StatisticsTreeNode built by the State Manager. - The view is built with a
66 * TreeViewer. - The controller that keeps model and view synchronised is an
67 * observer of the model.
69 public class StatisticsView
extends AbsTimeUpdateView
{
70 public static final String ID
= "org.eclipse.linuxtools.lttng.ui.views.statistics"; //$NON-NLS-1$
71 private TreeViewer treeViewer
;
74 private final String LEVEL_COLUMN
= Messages
.StatisticsView_LevelColumn
;
75 private final String EVENTS_COUNT_COLUMN
= Messages
.StatisticsView_NbEventsColumn
;
76 private final String CPU_TIME_COLUMN
= Messages
.StatisticsView_CPUTimeColumn
;
77 private final String CUMULATIVE_CPU_TIME_COLUMN
= Messages
.StatisticsView_CumCPUTimeColumn
;
78 private final String ELAPSED_TIME_COLUMN
= Messages
.StatisticsView_ElapsedTimeColumn
;
80 // Table column tooltips
81 private final String LEVEL_COLUMN_TIP
= Messages
.StatisticsView_LevelColumnTip
;
82 private final String EVENTS_COUNT_COLUMN_TIP
= Messages
.StatisticsView_NbEventsTip
;
83 private final String CPU_TIME_COLUMN_TIP
= Messages
.StatisticsView_CPUTimeTip
;
84 private final String CUMULATIVE_CPU_TIME_COLUMN_TIP
= Messages
.StatisticsView_CumCPUTimeTip
;
85 private final String ELAPSED_TIME_COLUMN_TIP
= Messages
.StatisticsView_ElapsedTimeTip
;
87 // Level for which statistics should not be displayed.
88 private Set
<String
> folderLevels
= new HashSet
<String
>(Arrays
.asList(
89 new String
[] { "Event Types", "Modes", "Submodes", "CPUs", "Processes", "Functions" })); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$
91 // Levels for which sub-levels should not contain time-related statistics.
92 private Set
<String
> levelsWithEmptyTime
= new HashSet
<String
>(Arrays
.asList(
93 new String
[] { "Event Types" })); //$NON-NLS-1$
95 private DecimalFormat decimalFormat
= new DecimalFormat("0.#########"); //$NON-NLS-1$
96 private Cursor fwaitCursor
= null;
98 private static final Long STATS_INPUT_CHANGED_REFRESH
= 5000L;
100 // Used to draw bar charts in columns.
101 private interface ColumnPercentageProvider
{
102 public double getPercentage(StatisticsTreeNode node
);
106 * Contains all the information necessary to build a column of the table.
108 private class ColumnData
{
109 // Name of the column.
110 public final String header
;
111 // Width of the column.
112 public final int width
;
113 // Alignment of the column.
114 public final int alignment
;
115 // Tooltip of the column.
116 public final String tooltip
;
117 // Adapts a StatisticsTreeNode into the content of it's corresponding
118 // cell for that column.
119 public final ColumnLabelProvider labelProvider
;
120 // Used to sort elements of this column. Can be null.
121 public final ViewerComparator comparator
;
122 // Used to draw bar charts in this column. Can be null.
123 public final ColumnPercentageProvider percentageProvider
;
125 public ColumnData(String h
, int w
, int a
, String t
,
126 ColumnLabelProvider l
, ViewerComparator c
,
127 ColumnPercentageProvider p
) {
134 percentageProvider
= p
;
138 // List that will be used to create the table.
139 private ColumnData
[] columnDataList
= new ColumnData
[] {
140 new ColumnData(LEVEL_COLUMN
, 200, SWT
.LEFT
, LEVEL_COLUMN_TIP
,
141 new ColumnLabelProvider() {
143 public String
getText(Object element
) {
144 return ((StatisticsTreeNode
) element
).getKey();
148 public Image
getImage(Object element
) {
149 StatisticsTreeNode node
= (StatisticsTreeNode
) element
;
150 if (folderLevels
.contains(node
.getKey())) {
151 return PlatformUI
.getWorkbench()
152 .getSharedImages().getImage(
153 ISharedImages
.IMG_OBJ_FOLDER
);
155 return PlatformUI
.getWorkbench()
156 .getSharedImages().getImage(
157 ISharedImages
.IMG_OBJ_ELEMENT
);
160 }, new ViewerComparator() {
162 public int compare(Viewer viewer
, Object e1
, Object e2
) {
163 StatisticsTreeNode n1
= (StatisticsTreeNode
) e1
;
164 StatisticsTreeNode n2
= (StatisticsTreeNode
) e2
;
166 return n1
.getKey().compareTo(n2
.getKey());
169 new ColumnData(EVENTS_COUNT_COLUMN
, 125, SWT
.LEFT
,
170 EVENTS_COUNT_COLUMN_TIP
, new ColumnLabelProvider() {
172 public String
getText(Object element
) {
173 StatisticsTreeNode node
= (StatisticsTreeNode
) element
;
174 if (!folderLevels
.contains(node
.getKey())) {
175 return Long
.toString(node
.getValue().nbEvents
);
177 return ""; //$NON-NLS-1$
180 }, new ViewerComparator() {
182 public int compare(Viewer viewer
, Object e1
, Object e2
) {
183 StatisticsTreeNode n1
= (StatisticsTreeNode
) e1
;
184 StatisticsTreeNode n2
= (StatisticsTreeNode
) e2
;
186 return (int) (n1
.getValue().nbEvents
- n2
187 .getValue().nbEvents
);
189 }, new ColumnPercentageProvider() {
191 public double getPercentage(StatisticsTreeNode node
) {
192 StatisticsTreeNode parent
= node
;
194 parent
= parent
.getParent();
195 } while (parent
!= null
196 && parent
.getValue().nbEvents
== 0);
198 if (parent
== null) {
201 return (double) node
.getValue().nbEvents
202 / parent
.getValue().nbEvents
;
206 new ColumnData(CPU_TIME_COLUMN
, 125, SWT
.LEFT
, CPU_TIME_COLUMN_TIP
,
207 new ColumnLabelProvider() {
209 public String
getText(Object element
) {
210 StatisticsTreeNode node
= (StatisticsTreeNode
) element
;
212 if (folderLevels
.contains(node
.getKey())) {
213 return ""; //$NON-NLS-1$
214 } else if (node
.getParent() != null
215 && levelsWithEmptyTime
.contains(node
216 .getParent().getKey())) {
217 return ""; //$NON-NLS-1$
220 .format(node
.getValue().cpuTime
225 new ColumnData(CUMULATIVE_CPU_TIME_COLUMN
, 155, SWT
.LEFT
,
226 CUMULATIVE_CPU_TIME_COLUMN_TIP
, new ColumnLabelProvider() {
228 public String
getText(Object element
) {
229 StatisticsTreeNode node
= (StatisticsTreeNode
) element
;
230 if (folderLevels
.contains(node
.getKey())) {
231 return ""; //$NON-NLS-1$
232 } else if (node
.getParent() != null
233 && levelsWithEmptyTime
.contains(node
234 .getParent().getKey())) {
235 return ""; //$NON-NLS-1$
238 .format(node
.getValue().cumulativeCpuTime
243 new ColumnData(ELAPSED_TIME_COLUMN
, 100, SWT
.LEFT
,
244 ELAPSED_TIME_COLUMN_TIP
, new ColumnLabelProvider() {
246 public String
getText(Object element
) {
247 StatisticsTreeNode node
= (StatisticsTreeNode
) element
;
248 if (folderLevels
.contains(node
.getKey())) {
249 return ""; //$NON-NLS-1$
250 } else if (node
.getParent() != null
251 && levelsWithEmptyTime
.contains(node
252 .getParent().getKey())) {
253 return ""; //$NON-NLS-1$
256 .format(node
.getValue().elapsedTime
263 * Adapter TreeViewers can use to interact with StatisticsTreeNode objects.
265 * @see org.eclipse.jface.viewers.ITreeContentProvider
267 class TreeContentProvider
implements ITreeContentProvider
{
272 * org.eclipse.jface.viewers.ITreeContentProvider#getChildren(java.lang
276 public Object
[] getChildren(Object parentElement
) {
277 return ((StatisticsTreeNode
) parentElement
).getChildren().toArray();
284 * org.eclipse.jface.viewers.ITreeContentProvider#getParent(java.lang
288 public Object
getParent(Object element
) {
289 return ((StatisticsTreeNode
) element
).getParent();
296 * org.eclipse.jface.viewers.ITreeContentProvider#hasChildren(java.lang
300 public boolean hasChildren(Object element
) {
301 return ((StatisticsTreeNode
) element
).hasChildren();
308 * org.eclipse.jface.viewers.IStructuredContentProvider#getElements(
312 public Object
[] getElements(Object inputElement
) {
313 return getChildren(inputElement
);
319 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
322 public void dispose() {
329 * org.eclipse.jface.viewers.IContentProvider#inputChanged(org.eclipse
330 * .jface.viewers.Viewer, java.lang.Object, java.lang.Object)
334 public void inputChanged(Viewer viewer
, Object oldInput
, Object newInput
) {
338 public StatisticsView(String viewName
) {
342 private static final String STATISTICS_VIEW
= "StatisticsView"; //$NON-NLS-1$
343 public StatisticsView() {
344 this(STATISTICS_VIEW
);
351 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
355 public void createPartControl(Composite parent
) {
356 parent
.setLayout(new FillLayout());
358 treeViewer
= new TreeViewer(parent
, SWT
.BORDER
| SWT
.H_SCROLL
360 treeViewer
.setContentProvider(new TreeContentProvider());
361 treeViewer
.getTree().setHeaderVisible(true);
362 treeViewer
.setUseHashlookup(true);
364 for (final ColumnData columnData
: columnDataList
) {
365 final TreeViewerColumn treeColumn
= new TreeViewerColumn(
366 treeViewer
, columnData
.alignment
);
367 treeColumn
.getColumn().setText(columnData
.header
);
368 treeColumn
.getColumn().setWidth(columnData
.width
);
369 treeColumn
.getColumn().setToolTipText(columnData
.tooltip
);
370 if (columnData
.comparator
!= null) {
371 treeColumn
.getColumn().addSelectionListener(
372 new SelectionAdapter() {
374 public void widgetSelected(SelectionEvent e
) {
375 if (treeViewer
.getTree().getSortDirection() == SWT
.UP
376 || treeViewer
.getTree().getSortColumn() != treeColumn
379 .setComparator(columnData
.comparator
);
380 treeViewer
.getTree().setSortDirection(
384 .setComparator(new ViewerComparator() {
388 Object e1
, Object e2
) {
390 * columnData
.comparator
397 treeViewer
.getTree().setSortDirection(
400 treeViewer
.getTree().setSortColumn(
401 treeColumn
.getColumn());
405 treeColumn
.setLabelProvider(columnData
.labelProvider
);
408 // Handler that will draw the bar charts.
409 treeViewer
.getTree().addListener(SWT
.EraseItem
, new Listener() {
412 public void handleEvent(Event event
) {
413 if (columnDataList
[event
.index
].percentageProvider
!= null) {
414 StatisticsTreeNode node
= (StatisticsTreeNode
) event
.item
417 double percentage
= columnDataList
[event
.index
].percentageProvider
418 .getPercentage(node
);
419 if (percentage
== 0) {
423 if ((event
.detail
& SWT
.SELECTED
) > 0) {
424 Color oldForeground
= event
.gc
.getForeground();
425 event
.gc
.setForeground(event
.item
.getDisplay()
426 .getSystemColor(SWT
.COLOR_LIST_SELECTION
));
427 event
.gc
.fillRectangle(event
.x
, event
.y
, event
.width
,
429 event
.gc
.setForeground(oldForeground
);
430 event
.detail
&= ~SWT
.SELECTED
;
433 int barWidth
= (int) ((treeViewer
.getTree().getColumn(1)
434 .getWidth() - 8) * percentage
);
435 int oldAlpha
= event
.gc
.getAlpha();
436 Color oldForeground
= event
.gc
.getForeground();
437 Color oldBackground
= event
.gc
.getBackground();
438 event
.gc
.setAlpha(64);
439 event
.gc
.setForeground(event
.item
.getDisplay()
440 .getSystemColor(SWT
.COLOR_BLUE
));
441 event
.gc
.setBackground(event
.item
.getDisplay()
442 .getSystemColor(SWT
.COLOR_LIST_BACKGROUND
));
443 event
.gc
.fillGradientRectangle(event
.x
, event
.y
, barWidth
,
445 event
.gc
.drawRectangle(event
.x
, event
.y
, barWidth
,
447 event
.gc
.setForeground(oldForeground
);
448 event
.gc
.setBackground(oldBackground
);
449 event
.gc
.setAlpha(oldAlpha
);
450 event
.detail
&= ~SWT
.BACKGROUND
;
455 treeViewer
.setComparator(columnDataList
[0].comparator
);
456 treeViewer
.getTree().setSortColumn(treeViewer
.getTree().getColumn(0));
457 treeViewer
.getTree().setSortDirection(SWT
.DOWN
);
459 // Read current data if any available
460 TmfExperiment
<?
> experiment
= TmfExperiment
.getCurrentExperiment();
461 if (experiment
!= null) {
462 requestData(experiment
);
464 TraceDebug
.debug("No selected experiment information available"); //$NON-NLS-1$
469 public void dispose() {
471 if (fwaitCursor
!= null) {
472 fwaitCursor
.dispose();
476 StatisticsTreeRootFactory
.removeAll();
482 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
485 public void setFocus() {
486 treeViewer
.getTree().setFocus();
492 * @see org.eclipse.linuxtools.lttng.ui.views.common.AbsTimeUpdateView#getInputChangedRefresh()
495 protected Long
getInputChangedRefresh() {
496 return STATS_INPUT_CHANGED_REFRESH
;
503 public AbsEventToHandlerResolver
getEventProcessor() {
504 return StatsTimeCountHandlerFactory
.getInstance();
511 * org.eclipse.linuxtools.lttng.ui.views.common.AbsTimeUpdateView#waitCursor
515 protected void waitCursor(final boolean waitInd
) {
516 if ((treeViewer
== null) || (treeViewer
.getTree().isDisposed())) {
520 Display display
= treeViewer
.getControl().getDisplay();
521 if (fwaitCursor
== null) {
522 fwaitCursor
= new Cursor(display
, SWT
.CURSOR_WAIT
);
525 // Perform the updates on the UI thread
526 display
.asyncExec(new Runnable() {
529 if ((treeViewer
!= null) && (!treeViewer
.getTree().isDisposed())) {
530 Cursor cursor
= null; /* indicates default */
532 cursor
= fwaitCursor
;
534 treeViewer
.getControl().setCursor(cursor
);
541 public void ModelUpdatePrep(TmfTimeRange timeRange
, boolean clearAllData
) {
542 Object input
= treeViewer
.getInput();
543 if ((input
!= null) && (input
instanceof StatisticsTreeNode
) && (!treeViewer
.getTree().isDisposed())) {
544 ((StatisticsTreeNode
) input
).reset();
545 treeViewer
.getTree().getDisplay().asyncExec(new Runnable() {
549 if (!treeViewer
.getTree().isDisposed())
550 treeViewer
.refresh();
557 public void modelInputChanged(ILttngSyntEventRequest request
, boolean complete
) {
558 // Ignore update if disposed
559 if (treeViewer
.getTree().isDisposed()) return;
561 treeViewer
.getTree().getDisplay().asyncExec(new Runnable() {
565 if (!treeViewer
.getTree().isDisposed())
566 treeViewer
.refresh();
574 * @see org.eclipse.linuxtools.lttng.ui.views.common.AbsTimeUpdateView#
576 * (org.eclipse.linuxtools.lttng.request.ILttngSyntEventRequest)
579 public void modelIncomplete(ILttngSyntEventRequest request
) {
580 Object input
= treeViewer
.getInput();
581 if (input
!= null && input
instanceof StatisticsTreeNode
) {
582 // The data from this experiment is invalid and shall be removed to
583 // refresh upon next selection
584 String name
= request
.getExperimentName();
585 StatisticsTreeRootFactory
.removeStatTreeRoot(name
);
593 public void experimentSelected(TmfExperimentSelectedSignal
<?
extends TmfEvent
> signal
) {
594 if (signal
!= null) {
595 TmfExperiment
<?
> experiment
= signal
.getExperiment();
596 String experimentName
= experiment
.getName();
598 if (StatisticsTreeRootFactory
.containsTreeRoot(experimentName
)) {
599 // The experiment root is already present
600 StatisticsTreeNode experimentTreeNode
= StatisticsTreeRootFactory
.getStatTreeRoot(experimentName
);
602 ITmfTrace
[] traces
= experiment
.getTraces();
604 // check if there is partial data loaded in the experiment
605 int numTraces
= experiment
.getTraces().length
;
606 int numNodeTraces
= experimentTreeNode
.getNbChildren();
608 if (numTraces
== numNodeTraces
) {
610 // Detect if the experiment contains the same traces as when
611 // previously selected
612 for (int i
= 0; i
< numTraces
; i
++) {
613 String traceName
= traces
[i
].getName();
614 if (!experimentTreeNode
.containsChild(traceName
)) {
621 // no need to reload data, all traces are already loaded
622 treeViewer
.setInput(experimentTreeNode
);
628 // if the data is not available or has changed, reload it
629 requestData(experiment
);
636 private void requestData(TmfExperiment
<?
> experiment
) {
637 if (experiment
!= null) {
638 StatisticsTreeNode treeModelRoot
= StatisticsTreeRootFactory
.getStatTreeRoot(experiment
.getName());
640 // if the model has contents, clear to start over
641 if (treeModelRoot
.hasChildren()) {
642 treeModelRoot
.reset();
645 // set input to a clean data model
646 treeViewer
.setInput(treeModelRoot
);
647 TmfTimeRange experimentTRange
= experiment
.getTimeRange();
649 // send the initial request, to start filling up model
650 dataRequest(experimentTRange
, experimentTRange
, true, ExecutionType
.BACKGROUND
);
652 TraceDebug
.debug("No selected experiment information available"); //$NON-NLS-1$
660 * org.eclipse.linuxtools.lttng.ui.views.common.AbsTimeUpdateView#displayModel
661 * (org.eclipse.linuxtools.tmf.ui.viewers.timeAnalysis.model.
662 * ITmfTimeAnalysisEntry[], long, long, boolean, long, long,
666 protected void displayModel(ITmfTimeAnalysisEntry
[] items
, long startBoundTime
, long endBoundTime
,
667 boolean updateTimeBounds
, long startVisibleWindow
, long endVisibleWindow
, Object source
) {
668 // No applicable to statistics view
674 * @see org.eclipse.linuxtools.lttng.ui.views.common.AbsTimeUpdateView#
678 protected ParamsUpdater
getParamsUpdater() {
679 // Not applicable to statistics view
684 protected ItemContainer
<?
> getItemContainer() {
685 // Not applicable to statistics view
691 * @see org.eclipse.linuxtools.lttng.ui.views.common.AbsTimeUpdateView#getProviderId()
694 protected int getProviderId() {
695 return LttngCoreProviderFactory
.STATISTICS_LTTNG_SYTH_EVENT_PROVIDER
;