Commit | Line | Data |
---|---|---|
cfd22ad0 | 1 | /******************************************************************************* |
c8422608 | 2 | * Copyright (c) 2012, 2013 Ericsson |
cfd22ad0 MD |
3 | * |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Mathieu Denis <mathieu.denis@polymtl.ca> - Initial API and implementation | |
89c06060 | 11 | * Alexandre Montplaisir - Port to ITmfStatistics provider |
0fcf3b09 | 12 | * Patrick Tasse - Support selection range |
cfd22ad0 MD |
13 | *******************************************************************************/ |
14 | ||
15 | package org.eclipse.linuxtools.tmf.ui.viewers.statistics; | |
16 | ||
17 | import java.util.List; | |
89c06060 | 18 | import java.util.Map; |
cfd22ad0 MD |
19 | |
20 | import org.eclipse.jface.viewers.TreeViewer; | |
21 | import org.eclipse.jface.viewers.TreeViewerColumn; | |
22 | import org.eclipse.jface.viewers.Viewer; | |
23 | import org.eclipse.jface.viewers.ViewerComparator; | |
24 | import org.eclipse.linuxtools.tmf.core.component.TmfComponent; | |
fd3f1eff | 25 | import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest; |
05627bda MD |
26 | import org.eclipse.linuxtools.tmf.core.signal.TmfRangeSynchSignal; |
27 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; | |
1c0de632 | 28 | import org.eclipse.linuxtools.tmf.core.signal.TmfStatsUpdatedSignal; |
0fcf3b09 | 29 | import org.eclipse.linuxtools.tmf.core.signal.TmfTimeSynchSignal; |
faa38350 | 30 | import org.eclipse.linuxtools.tmf.core.signal.TmfTraceRangeUpdatedSignal; |
89c06060 | 31 | import org.eclipse.linuxtools.tmf.core.statistics.ITmfStatistics; |
3bd46eef AM |
32 | import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp; |
33 | import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange; | |
05627bda MD |
34 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
35 | import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment; | |
248af329 | 36 | import org.eclipse.linuxtools.tmf.core.trace.TmfTraceManager; |
05627bda | 37 | import org.eclipse.linuxtools.tmf.ui.viewers.TmfViewer; |
cfd22ad0 MD |
38 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.ITmfColumnDataProvider; |
39 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseColumnData; | |
40 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfBaseColumnDataProvider; | |
36033ff0 AM |
41 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTree; |
42 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeManager; | |
cfd22ad0 | 43 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfStatisticsTreeNode; |
cfd22ad0 | 44 | import org.eclipse.linuxtools.tmf.ui.viewers.statistics.model.TmfTreeContentProvider; |
8192f2c6 | 45 | import org.eclipse.linuxtools.tmf.ui.views.statistics.TmfStatisticsModule; |
cfd22ad0 MD |
46 | import org.eclipse.swt.SWT; |
47 | import org.eclipse.swt.events.SelectionAdapter; | |
48 | import org.eclipse.swt.events.SelectionEvent; | |
49 | import org.eclipse.swt.graphics.Color; | |
50 | import org.eclipse.swt.graphics.Cursor; | |
cfd22ad0 MD |
51 | import org.eclipse.swt.widgets.Composite; |
52 | import org.eclipse.swt.widgets.Control; | |
53 | import org.eclipse.swt.widgets.Display; | |
54 | import org.eclipse.swt.widgets.Event; | |
55 | import org.eclipse.swt.widgets.Listener; | |
56 | ||
57 | /** | |
58 | * A basic viewer to display statistics in the statistics view. | |
59 | * | |
8b60cb37 MD |
60 | * It is linked to a single ITmfTrace until its disposal. |
61 | * | |
cfd22ad0 MD |
62 | * @author Mathieu Denis |
63 | * @version 2.0 | |
64 | * @since 2.0 | |
65 | */ | |
05627bda | 66 | public class TmfStatisticsViewer extends TmfViewer { |
cfd22ad0 | 67 | |
05627bda MD |
68 | /** |
69 | * Timestamp scale (nanosecond) | |
70 | */ | |
faa38350 | 71 | public static final byte TIME_SCALE = ITmfTimestamp.NANOSECOND_SCALE; |
05627bda MD |
72 | |
73 | /** | |
74 | * Default PAGE_SIZE for background requests. | |
75 | */ | |
76 | protected static final int PAGE_SIZE = 50000; | |
77 | ||
78 | /** | |
79 | * Refresh frequency. | |
80 | */ | |
81 | protected final Long STATS_INPUT_CHANGED_REFRESH = 5000L; | |
82 | ||
cfd22ad0 MD |
83 | /** |
84 | * The actual tree viewer to display | |
85 | */ | |
86 | protected TreeViewer fTreeViewer; | |
87 | ||
88 | /** | |
05627bda MD |
89 | * The statistics tree linked to this viewer |
90 | */ | |
36033ff0 | 91 | protected TmfStatisticsTree fStatisticsData; |
05627bda MD |
92 | |
93 | /** | |
94 | * Update synchronization parameter (used for streaming): Update busy | |
95 | * indicator. | |
96 | */ | |
97 | protected boolean fStatisticsUpdateBusy = false; | |
98 | ||
99 | /** | |
100 | * Update synchronization parameter (used for streaming): Update pending | |
101 | * indicator. | |
102 | */ | |
103 | protected boolean fStatisticsUpdatePending = false; | |
104 | ||
105 | /** | |
106 | * Update synchronization parameter (used for streaming): Pending Update | |
107 | * time range. | |
108 | */ | |
109 | protected TmfTimeRange fStatisticsUpdateRange = null; | |
110 | ||
111 | /** | |
112 | * Update synchronization object. | |
113 | */ | |
114 | protected final Object fStatisticsUpdateSyncObj = new Object(); | |
115 | ||
3c934968 MD |
116 | /** |
117 | * Update range synchronization object. | |
118 | */ | |
119 | protected final Object fStatisticsRangeUpdateSyncObj = new Object(); | |
120 | ||
89c06060 | 121 | /** |
73fbf6be MD |
122 | * The trace that is displayed by this viewer |
123 | */ | |
124 | protected ITmfTrace fTrace; | |
125 | ||
89c06060 AM |
126 | /** |
127 | * Stores the requested time range. | |
128 | */ | |
129 | protected TmfTimeRange fRequestedTimerange; | |
130 | ||
05627bda MD |
131 | /** |
132 | * Indicates to process all events | |
133 | */ | |
134 | private boolean fProcessAll; | |
135 | ||
136 | /** | |
137 | * View instance counter (for multiple statistics views) | |
cfd22ad0 MD |
138 | */ |
139 | private static int fCountInstance = 0; | |
140 | ||
141 | /** | |
142 | * Number of this instance. Used as an instance ID. | |
143 | */ | |
144 | private int fInstanceNb; | |
145 | ||
146 | /** | |
faa38350 | 147 | * Object to store the cursor while waiting for the trace to load |
cfd22ad0 MD |
148 | */ |
149 | private Cursor fWaitCursor = null; | |
150 | ||
151 | /** | |
05627bda MD |
152 | * Counts the number of times waitCursor() has been called. It avoids |
153 | * removing the waiting cursor, since there may be multiple requests running | |
154 | * at the same time. | |
155 | */ | |
156 | private int fWaitCursorCount = 0; | |
157 | ||
158 | /** | |
faa38350 | 159 | * Tells to send a time range request when the trace gets updated. |
05627bda MD |
160 | */ |
161 | private boolean fSendRangeRequest = true; | |
162 | ||
f0c0d2c2 AM |
163 | /** Reference to the trace manager */ |
164 | private final TmfTraceManager fTraceManager; | |
165 | ||
05627bda MD |
166 | /** |
167 | * Empty constructor. To be used in conjunction with | |
168 | * {@link TmfStatisticsViewer#init(Composite, String, ITmfTrace)} | |
169 | */ | |
170 | public TmfStatisticsViewer() { | |
171 | super(); | |
f0c0d2c2 | 172 | fTraceManager = TmfTraceManager.getInstance(); |
05627bda MD |
173 | } |
174 | ||
175 | /** | |
176 | * Create a basic statistics viewer. To be used in conjunction with | |
177 | * {@link TmfStatisticsViewer#init(Composite, String, ITmfTrace)} | |
178 | * | |
179 | * @param parent | |
180 | * The parent composite that will hold the viewer | |
181 | * @param viewerName | |
182 | * The name that will be assigned to this viewer | |
183 | * @param trace | |
184 | * The trace that is displayed by this viewer | |
185 | * @see TmfComponent | |
186 | */ | |
187 | public TmfStatisticsViewer(Composite parent, String viewerName, ITmfTrace trace) { | |
188 | init(parent, viewerName, trace); | |
f0c0d2c2 | 189 | fTraceManager = TmfTraceManager.getInstance(); |
05627bda MD |
190 | } |
191 | ||
192 | /** | |
193 | * Initialize the statistics viewer. | |
cfd22ad0 MD |
194 | * |
195 | * @param parent | |
05627bda MD |
196 | * The parent component of the viewer. |
197 | * @param viewerName | |
198 | * The name to give to the viewer. | |
199 | * @param trace | |
200 | * The trace that will be displayed by the viewer. | |
cfd22ad0 | 201 | */ |
05627bda MD |
202 | public void init(Composite parent, String viewerName, ITmfTrace trace) { |
203 | super.init(parent, viewerName); | |
cfd22ad0 MD |
204 | // Increment a counter to make sure the tree ID is unique. |
205 | fCountInstance++; | |
206 | fInstanceNb = fCountInstance; | |
05627bda MD |
207 | fTrace = trace; |
208 | ||
faa38350 | 209 | // The viewer will process all events if he is assigned to an experiment |
05627bda MD |
210 | fProcessAll = (trace instanceof TmfExperiment); |
211 | ||
212 | initContent(parent); | |
8b60cb37 | 213 | initInput(); |
05627bda MD |
214 | } |
215 | ||
05627bda MD |
216 | @Override |
217 | public void dispose() { | |
218 | super.dispose(); | |
219 | if (fWaitCursor != null) { | |
220 | fWaitCursor.dispose(); | |
221 | } | |
8b60cb37 | 222 | |
66792052 | 223 | // Clean the model for this viewer |
36033ff0 | 224 | TmfStatisticsTreeManager.removeStatTreeRoot(getTreeID()); |
05627bda MD |
225 | } |
226 | ||
1c0de632 AM |
227 | // ------------------------------------------------------------------------ |
228 | // Signal handlers | |
229 | // ------------------------------------------------------------------------ | |
89c06060 | 230 | |
05627bda | 231 | /** |
faa38350 | 232 | * Handles the signal about new trace range. |
05627bda MD |
233 | * |
234 | * @param signal | |
faa38350 | 235 | * The trace range updated signal |
05627bda MD |
236 | */ |
237 | @TmfSignalHandler | |
faa38350 PT |
238 | public void traceRangeUpdated(TmfTraceRangeUpdatedSignal signal) { |
239 | ITmfTrace trace = signal.getTrace(); | |
05627bda | 240 | // validate |
faa38350 | 241 | if (!isListeningTo(trace)) { |
05627bda MD |
242 | return; |
243 | } | |
244 | ||
3c934968 MD |
245 | synchronized (fStatisticsRangeUpdateSyncObj) { |
246 | // Sends the time range request only once from this method. | |
247 | if (fSendRangeRequest) { | |
248 | fSendRangeRequest = false; | |
0fcf3b09 PT |
249 | ITmfTimestamp begin = fTraceManager.getSelectionBeginTime(); |
250 | ITmfTimestamp end = fTraceManager.getSelectionEndTime(); | |
251 | TmfTimeRange timeRange = new TmfTimeRange(begin, end); | |
252 | requestTimeRangeData(trace, timeRange); | |
3c934968 | 253 | } |
05627bda | 254 | } |
faa38350 | 255 | requestData(trace, signal.getRange()); |
05627bda MD |
256 | } |
257 | ||
258 | /** | |
89c06060 | 259 | * Handles the time range updated signal. It updates the time range |
05627bda MD |
260 | * statistics. |
261 | * | |
262 | * @param signal | |
263 | * Contains the information about the new selected time range. | |
0fcf3b09 PT |
264 | * @deprecated |
265 | * As of 2.1, use {@link #timeSynchUpdated(TmfTimeSynchSignal)} | |
05627bda | 266 | */ |
0fcf3b09 | 267 | @Deprecated |
05627bda MD |
268 | @TmfSignalHandler |
269 | public void timeRangeUpdated(TmfRangeSynchSignal signal) { | |
0fcf3b09 PT |
270 | } |
271 | ||
272 | /** | |
273 | * Handles the time synch updated signal. It updates the time range | |
274 | * statistics. | |
275 | * | |
276 | * @param signal | |
277 | * Contains the information about the new selected time range. | |
4b121c48 | 278 | * @since 2.1 |
0fcf3b09 PT |
279 | */ |
280 | @TmfSignalHandler | |
281 | public void timeSynchUpdated(TmfTimeSynchSignal signal) { | |
faa38350 PT |
282 | if (fTrace == null) { |
283 | return; | |
284 | } | |
0fcf3b09 PT |
285 | ITmfTimestamp begin = signal.getBeginTime(); |
286 | ITmfTimestamp end = signal.getEndTime(); | |
287 | TmfTimeRange timeRange = new TmfTimeRange(begin, end); | |
288 | requestTimeRangeData(fTrace, timeRange); | |
05627bda MD |
289 | } |
290 | ||
1c0de632 AM |
291 | /** |
292 | * Whenever a trace's statistics back-end finishes computing the statistics | |
293 | * for a given interval, it will send the StatsUpdated signal. This method | |
294 | * will receive this signal and update the statistics view accordingly. | |
295 | * | |
296 | * @param sig | |
297 | * The signal that is received | |
298 | */ | |
299 | @TmfSignalHandler | |
300 | public void statsUpdated(TmfStatsUpdatedSignal sig) { | |
301 | /* Only handle this signal if it's about the trace we represent. */ | |
302 | if (!isListeningTo(sig.getTrace())) { | |
303 | return; | |
304 | } | |
305 | ||
306 | final TmfStatisticsTree statsData = TmfStatisticsTreeManager.getStatTree(getTreeID()); | |
307 | Map<String, Long> map = sig.getEventsPerType(); | |
308 | String name = sig.getTrace().getName(); | |
309 | boolean isGlobal = sig.isGlobal(); | |
310 | ||
311 | /* | |
312 | * "Global", "partial", "total", etc., it's all very confusing... | |
313 | * | |
314 | * The base view shows the total count for the trace and for | |
315 | * each even types, organized in columns like this: | |
316 | * | |
317 | * | Global | Time range | | |
318 | * trace name | A | B | | |
319 | * Event Type | | | | |
320 | * <event 1> | C | D | | |
321 | * <event 2> | ... | ... | | |
322 | * ... | | | | |
323 | * | |
324 | * Here, we called the cells like this: | |
325 | * A : GlobalTotal | |
326 | * B : TimeRangeTotal | |
327 | * C : GlobalTypeCount(s) | |
328 | * D : TimeRangeTypeCount(s) | |
329 | */ | |
330 | ||
331 | /* Fill in an the event counts (either cells C or D) */ | |
332 | for (Map.Entry<String, Long> entry : map.entrySet()) { | |
333 | statsData.setTypeCount(name, entry.getKey(), isGlobal, entry.getValue()); | |
334 | } | |
335 | ||
336 | /* | |
337 | * Calculate the totals (cell A or B, depending if isGlobal). We will | |
338 | * use the results of the previous request instead of sending another | |
339 | * one. | |
340 | */ | |
341 | long globalTotal = 0; | |
342 | for (long val : map.values()) { | |
343 | globalTotal += val; | |
344 | } | |
345 | statsData.setTotal(name, isGlobal, globalTotal); | |
346 | ||
347 | modelComplete(isGlobal); | |
348 | } | |
349 | ||
350 | // ------------------------------------------------------------------------ | |
351 | // Class methods | |
352 | // ------------------------------------------------------------------------ | |
353 | ||
05627bda MD |
354 | /* |
355 | * Returns the primary control associated with this viewer. | |
356 | * | |
357 | * @return the SWT control which displays this viewer's content | |
358 | */ | |
359 | @Override | |
360 | public Control getControl() { | |
361 | return fTreeViewer.getControl(); | |
362 | } | |
363 | ||
364 | /** | |
365 | * Get the input of the viewer. | |
366 | * | |
367 | * @return an object representing the input of the statistics viewer. | |
368 | */ | |
369 | public Object getInput() { | |
370 | return fTreeViewer.getInput(); | |
371 | } | |
372 | ||
373 | /** | |
374 | * Return the size of the request when performing background request. | |
375 | * | |
376 | * @return the block size for background request. | |
377 | */ | |
378 | public int getPageSize() { | |
379 | return PAGE_SIZE; | |
380 | } | |
381 | ||
382 | /** | |
383 | * Return the number of events to receive before a refresh of the viewer is | |
384 | * performed. | |
385 | * | |
386 | * @return the input refresh rate | |
387 | */ | |
388 | public long getRefreshRate() { | |
389 | return STATS_INPUT_CHANGED_REFRESH; | |
390 | } | |
391 | ||
392 | /** | |
393 | * This method can be overridden to implement another way of representing | |
394 | * the statistics data and to retrieve the information for display. | |
395 | * | |
396 | * @return a TmfStatisticsData object. | |
397 | */ | |
36033ff0 | 398 | public TmfStatisticsTree getStatisticData() { |
05627bda | 399 | if (fStatisticsData == null) { |
36033ff0 | 400 | fStatisticsData = new TmfStatisticsTree(); |
05627bda MD |
401 | } |
402 | return fStatisticsData; | |
403 | } | |
404 | ||
405 | /** | |
406 | * Returns a unique ID based on name to be associated with the statistics | |
407 | * tree for this viewer. For a same name, it will always return the same ID. | |
408 | * | |
409 | * @return a unique statistics tree ID. | |
410 | */ | |
411 | public String getTreeID() { | |
412 | return getName() + fInstanceNb; | |
413 | } | |
414 | ||
415 | @Override | |
416 | public void refresh() { | |
417 | final Control viewerControl = getControl(); | |
418 | // Ignore update if disposed | |
419 | if (viewerControl.isDisposed()) { | |
420 | return; | |
421 | } | |
422 | ||
423 | viewerControl.getDisplay().asyncExec(new Runnable() { | |
424 | @Override | |
425 | public void run() { | |
426 | if (!viewerControl.isDisposed()) { | |
427 | fTreeViewer.refresh(); | |
428 | } | |
429 | } | |
430 | }); | |
431 | } | |
432 | ||
3c934968 MD |
433 | /** |
434 | * Will force a request on the partial event count if one is needed. | |
435 | */ | |
436 | public void sendPartialRequestOnNextUpdate() { | |
437 | synchronized (fStatisticsRangeUpdateSyncObj) { | |
438 | fSendRangeRequest = true; | |
439 | } | |
440 | } | |
441 | ||
05627bda MD |
442 | /** |
443 | * Focus on the statistics tree of the viewer | |
444 | */ | |
445 | public void setFocus() { | |
446 | fTreeViewer.getTree().setFocus(); | |
447 | } | |
448 | ||
05627bda MD |
449 | /** |
450 | * Cancels the request if it is not already completed | |
451 | * | |
452 | * @param request | |
453 | * The request to be canceled | |
c4767854 | 454 | * @since 3.0 |
05627bda | 455 | */ |
fd3f1eff | 456 | protected void cancelOngoingRequest(ITmfEventRequest request) { |
05627bda MD |
457 | if (request != null && !request.isCompleted()) { |
458 | request.cancel(); | |
459 | } | |
460 | } | |
461 | ||
462 | /** | |
463 | * This method can be overridden to change the representation of the data in | |
464 | * the columns. | |
465 | * | |
466 | * @return an object implementing ITmfBaseColumnDataProvider. | |
467 | */ | |
468 | protected ITmfColumnDataProvider getColumnDataProvider() { | |
469 | return new TmfBaseColumnDataProvider(); | |
470 | } | |
cfd22ad0 | 471 | |
05627bda MD |
472 | /** |
473 | * Initialize the content that will be drawn in this viewer | |
474 | * | |
475 | * @param parent | |
476 | * The parent of the control to create | |
477 | */ | |
478 | protected void initContent(Composite parent) { | |
cfd22ad0 | 479 | final List<TmfBaseColumnData> columnDataList = getColumnDataProvider().getColumnData(); |
cfd22ad0 MD |
480 | |
481 | fTreeViewer = new TreeViewer(parent, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); | |
482 | fTreeViewer.setContentProvider(new TmfTreeContentProvider()); | |
483 | fTreeViewer.getTree().setHeaderVisible(true); | |
484 | fTreeViewer.setUseHashlookup(true); | |
485 | ||
486 | // Creates the columns defined by the column data provider | |
487 | for (final TmfBaseColumnData columnData : columnDataList) { | |
488 | final TreeViewerColumn treeColumn = new TreeViewerColumn(fTreeViewer, columnData.getAlignment()); | |
489 | treeColumn.getColumn().setText(columnData.getHeader()); | |
490 | treeColumn.getColumn().setWidth(columnData.getWidth()); | |
491 | treeColumn.getColumn().setToolTipText(columnData.getTooltip()); | |
492 | ||
493 | if (columnData.getComparator() != null) { // A comparator is defined. | |
494 | // Adds a listener on the columns header for sorting purpose. | |
495 | treeColumn.getColumn().addSelectionListener(new SelectionAdapter() { | |
496 | ||
497 | private ViewerComparator reverseComparator; | |
498 | ||
499 | @Override | |
500 | public void widgetSelected(SelectionEvent e) { | |
501 | // Initializes the reverse comparator once. | |
502 | if (reverseComparator == null) { | |
503 | reverseComparator = new ViewerComparator() { | |
504 | @Override | |
505 | public int compare(Viewer viewer, Object e1, Object e2) { | |
506 | return -1 * columnData.getComparator().compare(viewer, e1, e2); | |
507 | } | |
508 | }; | |
509 | } | |
510 | ||
511 | if (fTreeViewer.getTree().getSortDirection() == SWT.UP | |
512 | || fTreeViewer.getTree().getSortColumn() != treeColumn.getColumn()) { | |
513 | /* | |
514 | * Puts the descendant order if the old order was | |
515 | * up or if the selected column has changed. | |
516 | */ | |
517 | fTreeViewer.setComparator(columnData.getComparator()); | |
518 | fTreeViewer.getTree().setSortDirection(SWT.DOWN); | |
519 | } else { | |
520 | /* | |
521 | * Puts the ascendant ordering if the selected | |
522 | * column hasn't changed. | |
523 | */ | |
524 | fTreeViewer.setComparator(reverseComparator); | |
525 | fTreeViewer.getTree().setSortDirection(SWT.UP); | |
526 | } | |
527 | fTreeViewer.getTree().setSortColumn(treeColumn.getColumn()); | |
528 | } | |
529 | }); | |
530 | } | |
531 | treeColumn.setLabelProvider(columnData.getLabelProvider()); | |
532 | } | |
533 | ||
534 | // Handler that will draw the bar charts. | |
535 | fTreeViewer.getTree().addListener(SWT.EraseItem, new Listener() { | |
536 | @Override | |
537 | public void handleEvent(Event event) { | |
538 | if (columnDataList.get(event.index).getPercentageProvider() != null) { | |
539 | TmfStatisticsTreeNode node = (TmfStatisticsTreeNode) event.item.getData(); | |
540 | ||
541 | double percentage = columnDataList.get(event.index).getPercentageProvider().getPercentage(node); | |
542 | if (percentage == 0) { // No bar to draw | |
543 | return; | |
544 | } | |
545 | ||
546 | if ((event.detail & SWT.SELECTED) > 0) { // The item is selected. | |
547 | // Draws our own background to avoid overwritten the bar. | |
548 | event.gc.fillRectangle(event.x, event.y, event.width, event.height); | |
549 | event.detail &= ~SWT.SELECTED; | |
550 | } | |
551 | ||
552 | int barWidth = (int) ((fTreeViewer.getTree().getColumn(event.index).getWidth() - 8) * percentage); | |
553 | int oldAlpha = event.gc.getAlpha(); | |
554 | Color oldForeground = event.gc.getForeground(); | |
555 | Color oldBackground = event.gc.getBackground(); | |
556 | /* | |
557 | * Draws a transparent gradient rectangle from the color of | |
558 | * foreground and background. | |
559 | */ | |
560 | event.gc.setAlpha(64); | |
561 | event.gc.setForeground(event.item.getDisplay().getSystemColor(SWT.COLOR_BLUE)); | |
562 | event.gc.setBackground(event.item.getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND)); | |
563 | event.gc.fillGradientRectangle(event.x, event.y, barWidth, event.height, true); | |
564 | event.gc.drawRectangle(event.x, event.y, barWidth, event.height); | |
565 | // Restores old values | |
566 | event.gc.setForeground(oldForeground); | |
567 | event.gc.setBackground(oldBackground); | |
568 | event.gc.setAlpha(oldAlpha); | |
569 | event.detail &= ~SWT.BACKGROUND; | |
570 | } | |
571 | } | |
572 | }); | |
573 | ||
574 | // Initializes the comparator parameters | |
575 | fTreeViewer.setComparator(columnDataList.get(0).getComparator()); | |
576 | fTreeViewer.getTree().setSortColumn(fTreeViewer.getTree().getColumn(0)); | |
577 | fTreeViewer.getTree().setSortDirection(SWT.DOWN); | |
578 | } | |
579 | ||
8b60cb37 MD |
580 | /** |
581 | * Initializes the input for the tree viewer. | |
8b60cb37 MD |
582 | */ |
583 | protected void initInput() { | |
584 | String treeID = getTreeID(); | |
faa38350 | 585 | TmfStatisticsTreeNode statisticsTreeNode; |
36033ff0 | 586 | if (TmfStatisticsTreeManager.containsTreeRoot(treeID)) { |
faa38350 PT |
587 | // The statistics root is already present |
588 | statisticsTreeNode = TmfStatisticsTreeManager.getStatTreeRoot(treeID); | |
8b60cb37 MD |
589 | |
590 | // Checks if the trace is already in the statistics tree. | |
faa38350 | 591 | int numNodeTraces = statisticsTreeNode.getNbChildren(); |
8b60cb37 | 592 | |
b9a5bf8f | 593 | ITmfTrace[] traces = TmfTraceManager.getTraceSet(fTrace); |
fe0c44c4 | 594 | int numTraces = traces.length; |
8b60cb37 MD |
595 | |
596 | if (numTraces == numNodeTraces) { | |
597 | boolean same = true; | |
598 | /* | |
599 | * Checks if the experiment contains the same traces as when | |
600 | * previously selected. | |
601 | */ | |
602 | for (int i = 0; i < numTraces; i++) { | |
fe0c44c4 | 603 | String traceName = traces[i].getName(); |
faa38350 | 604 | if (!statisticsTreeNode.containsChild(traceName)) { |
8b60cb37 MD |
605 | same = false; |
606 | break; | |
607 | } | |
608 | } | |
609 | ||
610 | if (same) { | |
611 | // No need to reload data, all traces are already loaded | |
faa38350 | 612 | fTreeViewer.setInput(statisticsTreeNode); |
8b60cb37 MD |
613 | return; |
614 | } | |
615 | // Clears the old content to start over | |
faa38350 | 616 | statisticsTreeNode.reset(); |
8b60cb37 MD |
617 | } |
618 | } else { | |
619 | // Creates a new tree | |
faa38350 | 620 | statisticsTreeNode = TmfStatisticsTreeManager.addStatsTreeRoot(treeID, getStatisticData()); |
8b60cb37 MD |
621 | } |
622 | ||
623 | // Sets the input to a clean data model | |
faa38350 | 624 | fTreeViewer.setInput(statisticsTreeNode); |
8b60cb37 MD |
625 | resetUpdateSynchronization(); |
626 | } | |
627 | ||
cfd22ad0 | 628 | /** |
faa38350 | 629 | * Tells if the viewer is listening to a trace. |
cfd22ad0 | 630 | * |
1c0de632 | 631 | * @param trace |
05627bda MD |
632 | * The trace that the viewer may be listening |
633 | * @return true if the viewer is listening to the trace, false otherwise | |
cfd22ad0 | 634 | */ |
1c0de632 AM |
635 | protected boolean isListeningTo(ITmfTrace trace) { |
636 | if (fProcessAll || trace == fTrace) { | |
05627bda | 637 | return true; |
cfd22ad0 | 638 | } |
05627bda | 639 | return false; |
cfd22ad0 MD |
640 | } |
641 | ||
642 | /** | |
faa38350 | 643 | * Called when an trace request has been completed successfully. |
cfd22ad0 | 644 | * |
05627bda MD |
645 | * @param global |
646 | * Tells if the request is a global or time range (partial) | |
647 | * request. | |
cfd22ad0 | 648 | */ |
05627bda MD |
649 | protected void modelComplete(boolean global) { |
650 | refresh(); | |
651 | waitCursor(false); | |
652 | if (global) { | |
653 | sendPendingUpdate(); | |
654 | } | |
cfd22ad0 MD |
655 | } |
656 | ||
657 | /** | |
faa38350 | 658 | * Called when an trace request has failed or has been cancelled. |
cfd22ad0 | 659 | * |
05627bda MD |
660 | * @param isGlobalRequest |
661 | * Tells if the request is a global or time range (partial) | |
662 | * request. | |
cfd22ad0 | 663 | */ |
05627bda MD |
664 | protected void modelIncomplete(boolean isGlobalRequest) { |
665 | if (isGlobalRequest) { // Clean the global statistics | |
666 | /* | |
763f4972 MD |
667 | * No need to reset the global number of events, since the index of |
668 | * the last requested event is known. | |
05627bda | 669 | */ |
05627bda MD |
670 | resetUpdateSynchronization(); |
671 | sendPendingUpdate(); | |
672 | } else { // Clean the partial statistics | |
673 | resetTimeRangeValue(); | |
674 | } | |
675 | refresh(); | |
676 | waitCursor(false); | |
cfd22ad0 MD |
677 | } |
678 | ||
679 | /** | |
faa38350 | 680 | * Sends the request to the trace for the whole trace |
cfd22ad0 | 681 | * |
faa38350 PT |
682 | * @param trace |
683 | * The trace used to send the request | |
8b260d9f | 684 | * @param timeRange |
faa38350 | 685 | * The range to request to the trace |
cfd22ad0 | 686 | */ |
faa38350 PT |
687 | protected void requestData(final ITmfTrace trace, final TmfTimeRange timeRange) { |
688 | buildStatisticsTree(trace, timeRange, true); | |
cfd22ad0 MD |
689 | } |
690 | ||
691 | /** | |
faa38350 | 692 | * Sends the time range request from the trace |
cfd22ad0 | 693 | * |
faa38350 PT |
694 | * @param trace |
695 | * The trace used to send the request | |
8b260d9f | 696 | * @param timeRange |
faa38350 | 697 | * The range to request to the trace |
cfd22ad0 | 698 | */ |
faa38350 | 699 | protected void requestTimeRangeData(final ITmfTrace trace, final TmfTimeRange timeRange) { |
89c06060 | 700 | fRequestedTimerange = timeRange; |
faa38350 | 701 | buildStatisticsTree(trace, timeRange, false); |
89c06060 AM |
702 | } |
703 | ||
704 | /** | |
faa38350 | 705 | * Requests all the data of the trace to the state system which |
89c06060 AM |
706 | * contains information about the statistics. |
707 | * | |
faa38350 PT |
708 | * Since the viewer may be listening to multiple traces, it may receive |
709 | * an experiment rather than a single trace. The filtering is done with the | |
89c06060 AM |
710 | * method {@link #isListeningTo(String trace)}. |
711 | * | |
faa38350 PT |
712 | * @param trace |
713 | * The trace for which a request must be done | |
89c06060 AM |
714 | * @param timeRange |
715 | * The time range that will be requested to the state system | |
716 | * @param isGlobal | |
717 | * Tells if the request is for the global event count or the | |
718 | * partial one. | |
719 | */ | |
faa38350 | 720 | private void buildStatisticsTree(final ITmfTrace trace, TmfTimeRange timeRange, boolean isGlobal) { |
36033ff0 AM |
721 | final TmfStatisticsTreeNode statTree = TmfStatisticsTreeManager.getStatTreeRoot(getTreeID()); |
722 | final TmfStatisticsTree statsData = TmfStatisticsTreeManager.getStatTree(getTreeID()); | |
89c06060 AM |
723 | if (statsData == null) { |
724 | return; | |
725 | } | |
726 | ||
727 | synchronized (statsData) { | |
728 | if (isGlobal) { | |
729 | statTree.resetGlobalValue(); | |
730 | } else { | |
731 | statTree.resetTimeRangeValue(); | |
732 | } | |
733 | ||
b9a5bf8f | 734 | for (final ITmfTrace aTrace : TmfTraceManager.getTraceSet(trace)) { |
faa38350 | 735 | if (!isListeningTo(aTrace)) { |
89c06060 AM |
736 | continue; |
737 | } | |
738 | ||
8192f2c6 AM |
739 | /* Retrieve the statistics object */ |
740 | final TmfStatisticsModule statsMod = aTrace.getAnalysisModuleOfClass(TmfStatisticsModule.class, TmfStatisticsModule.ID); | |
741 | if (statsMod == null) { | |
742 | /* No statistics module available for this trace */ | |
743 | continue; | |
744 | } | |
745 | final ITmfStatistics stats = statsMod.getStatistics(); | |
89c06060 AM |
746 | if (stats == null) { |
747 | /* | |
1c0de632 AM |
748 | * The statistics provider for this trace is not accessible |
749 | * (yet?). Try the next one. | |
89c06060 AM |
750 | */ |
751 | continue; | |
752 | } | |
753 | ||
1c0de632 AM |
754 | /* The generic statistics are stored in nanoseconds, so we must make |
755 | * sure the time range is scaled correctly. */ | |
8b260d9f AM |
756 | long start = timeRange.getStartTime().normalize(0, TIME_SCALE).getValue(); |
757 | long end = timeRange.getEndTime().normalize(0, TIME_SCALE).getValue(); | |
89c06060 | 758 | |
1c0de632 AM |
759 | /* |
760 | * Send a request to update the statistics view. The result will | |
761 | * be sent through a {@link TmfStatsUpdatedSignal}, and will be | |
762 | * processed by the signal handler. | |
763 | */ | |
8192f2c6 | 764 | stats.updateStats(isGlobal, start, end); |
89c06060 | 765 | } |
89c06060 | 766 | } |
cfd22ad0 MD |
767 | } |
768 | ||
769 | /** | |
05627bda | 770 | * Resets the number of events within the time range |
cfd22ad0 | 771 | */ |
05627bda | 772 | protected void resetTimeRangeValue() { |
36033ff0 | 773 | TmfStatisticsTreeNode treeModelRoot = TmfStatisticsTreeManager.getStatTreeRoot(getTreeID()); |
05627bda MD |
774 | if (treeModelRoot != null && treeModelRoot.hasChildren()) { |
775 | treeModelRoot.resetTimeRangeValue(); | |
776 | } | |
cfd22ad0 MD |
777 | } |
778 | ||
779 | /** | |
faa38350 | 780 | * When the trace is loading the cursor will be different so the user |
05627bda MD |
781 | * knows that the processing is not finished yet. |
782 | * | |
783 | * Calls to this method are stacked. | |
cfd22ad0 | 784 | * |
05627bda | 785 | * @param waitRequested |
cfd22ad0 | 786 | * Indicates if we need to show the waiting cursor, or the |
05627bda | 787 | * default one. |
cfd22ad0 | 788 | */ |
05627bda | 789 | protected void waitCursor(final boolean waitRequested) { |
cfd22ad0 MD |
790 | if ((fTreeViewer == null) || (fTreeViewer.getTree().isDisposed())) { |
791 | return; | |
792 | } | |
793 | ||
05627bda | 794 | boolean needsUpdate = false; |
cfd22ad0 | 795 | Display display = fTreeViewer.getControl().getDisplay(); |
05627bda MD |
796 | if (waitRequested) { |
797 | fWaitCursorCount++; | |
798 | if (fWaitCursor == null) { // The cursor hasn't been initialized yet | |
799 | fWaitCursor = new Cursor(display, SWT.CURSOR_WAIT); | |
800 | } | |
801 | if (fWaitCursorCount == 1) { // The cursor is not in waiting mode | |
802 | needsUpdate = true; | |
803 | } | |
804 | } else { | |
805 | if (fWaitCursorCount > 0) { // The cursor is in waiting mode | |
806 | fWaitCursorCount--; | |
807 | if (fWaitCursorCount == 0) { // No more reason to wait | |
808 | // Put back the default cursor | |
809 | needsUpdate = true; | |
810 | } | |
811 | } | |
cfd22ad0 MD |
812 | } |
813 | ||
05627bda MD |
814 | if (needsUpdate) { |
815 | // Performs the updates on the UI thread | |
816 | display.asyncExec(new Runnable() { | |
817 | @Override | |
818 | public void run() { | |
819 | if ((fTreeViewer != null) | |
820 | && (!fTreeViewer.getTree().isDisposed())) { | |
821 | Cursor cursor = null; // indicates default | |
822 | if (waitRequested) { | |
823 | cursor = fWaitCursor; | |
824 | } | |
825 | fTreeViewer.getControl().setCursor(cursor); | |
cfd22ad0 | 826 | } |
cfd22ad0 | 827 | } |
05627bda MD |
828 | }); |
829 | } | |
cfd22ad0 MD |
830 | } |
831 | ||
05627bda MD |
832 | // ------------------------------------------------------------------------ |
833 | // Methods reserved for the streaming functionality | |
834 | // ------------------------------------------------------------------------ | |
835 | ||
cfd22ad0 | 836 | /** |
05627bda | 837 | * Resets update synchronization information |
cfd22ad0 | 838 | */ |
05627bda MD |
839 | protected void resetUpdateSynchronization() { |
840 | synchronized (fStatisticsUpdateSyncObj) { | |
841 | fStatisticsUpdateBusy = false; | |
842 | fStatisticsUpdatePending = false; | |
843 | fStatisticsUpdateRange = null; | |
844 | } | |
cfd22ad0 MD |
845 | } |
846 | ||
847 | /** | |
05627bda MD |
848 | * Checks if statistics update is ongoing. If it is ongoing, the new time |
849 | * range is stored as pending | |
cfd22ad0 | 850 | * |
05627bda MD |
851 | * @param timeRange |
852 | * - new time range | |
853 | * @return true if statistic update is ongoing else false | |
cfd22ad0 | 854 | */ |
05627bda MD |
855 | protected boolean checkUpdateBusy(TmfTimeRange timeRange) { |
856 | synchronized (fStatisticsUpdateSyncObj) { | |
857 | if (fStatisticsUpdateBusy) { | |
858 | fStatisticsUpdatePending = true; | |
859 | if (fStatisticsUpdateRange == null | |
860 | || timeRange.getEndTime().compareTo(fStatisticsUpdateRange.getEndTime()) > 0) { | |
861 | fStatisticsUpdateRange = timeRange; | |
862 | } | |
863 | return true; | |
864 | } | |
865 | fStatisticsUpdateBusy = true; | |
866 | return false; | |
867 | } | |
868 | } | |
869 | ||
870 | /** | |
871 | * Sends pending request (if any) | |
872 | */ | |
873 | protected void sendPendingUpdate() { | |
874 | synchronized (fStatisticsUpdateSyncObj) { | |
875 | fStatisticsUpdateBusy = false; | |
876 | if (fStatisticsUpdatePending) { | |
877 | fStatisticsUpdatePending = false; | |
faa38350 | 878 | requestData(fTrace, fStatisticsUpdateRange); |
05627bda MD |
879 | fStatisticsUpdateRange = null; |
880 | } | |
881 | } | |
cfd22ad0 MD |
882 | } |
883 | } |