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