1 /*******************************************************************************
2 * Copyright (c) 2015 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 * Mathieu Denis - Initial API and implementation
11 * Alexis Cabana-Loriaux - Extract the class in a compilation unit
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
;
18 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
19 import org
.eclipse
.core
.runtime
.IStatus
;
20 import org
.eclipse
.core
.runtime
.Status
;
21 import org
.eclipse
.core
.runtime
.jobs
.Job
;
22 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.piecharts
.model
.TmfPieChartStatisticsModel
;
23 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTree
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.viewers
.statistics
.model
.TmfStatisticsTreeManager
;
25 import org
.eclipse
.tracecompass
.statesystem
.core
.ITmfStateSystem
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.statistics
.ITmfStatistics
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.statistics
.TmfStatisticsEventTypesModule
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.statistics
.TmfStatisticsModule
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
33 * Class used to update the Statistics view. Normally, it should only be used by
36 * @author Mathieu Denis
38 class StatisticsUpdateJob
extends Job
{
40 private final ITmfTrace fJobTrace
;
41 private final boolean fIsGlobal
;
42 private final TmfStatisticsModule fStatsMod
;
43 private final TmfStatisticsViewer fViewer
;
46 * The delay (in ms) between each update in live-reading mode
48 private static final long LIVE_UPDATE_DELAY
= 1000;
50 private TmfTimeRange fTimerange
;
54 * The name of the working job
58 * If the query is for the global time-range or a selection
63 * The statistics module of the trace
65 * The viewer to update
67 public StatisticsUpdateJob(String name
, ITmfTrace trace
, boolean isGlobal
, TmfTimeRange timerange
, TmfStatisticsModule statsMod
, TmfStatisticsViewer viewer
) {
71 fTimerange
= timerange
;
77 protected IStatus
run(IProgressMonitor monitor
) {
79 /* Wait until the analysis is ready to be queried */
80 fStatsMod
.waitForInitialization();
81 ITmfStatistics stats
= fStatsMod
.getStatistics();
83 /* It should have worked, but didn't */
84 throw new IllegalStateException();
88 * TODO Eventually this could be exposed through the
89 * TmfStateSystemAnalysisModule directly.
91 ITmfStateSystem ss
= fStatsMod
.getStateSystem(TmfStatisticsEventTypesModule
.ID
);
94 * It should be instantiated after the
95 * statsMod.waitForInitialization() above.
97 throw new IllegalStateException();
101 * Periodically update the statistics while they are being built (or, if
102 * the back-end is already completely built, it will skip over the
103 * while() immediately.
107 boolean finished
= false;
109 /* This model update is done every second */
110 if (monitor
.isCanceled()) {
111 fViewer
.removeFromJobs(fIsGlobal
, fJobTrace
);
112 return Status
.CANCEL_STATUS
;
114 finished
= ss
.waitUntilBuilt(LIVE_UPDATE_DELAY
);
115 TmfTimeRange localtimeRange
= fTimerange
;
117 * The generic statistics are stored in nanoseconds, so we must make
118 * sure the time range is scaled correctly.
120 start
= localtimeRange
.getStartTime().toNanos();
121 end
= localtimeRange
.getEndTime().toNanos();
123 Map
<String
, Long
> map
= stats
.getEventTypesInRange(start
, end
);
128 /* Query one last time for the final values */
129 Map
<String
, Long
> map
= stats
.getEventTypesInRange(start
, end
);
131 fViewer
.refreshPieCharts(fIsGlobal
, !fIsGlobal
);
133 * Remove job from map so that new range selection updates can be
136 fViewer
.removeFromJobs(fIsGlobal
, fJobTrace
);
137 return Status
.OK_STATUS
;
141 * Update the tree for a given trace
143 private void updateStats(Map
<String
, Long
> eventsPerType
) {
145 final TmfStatisticsTree statsData
= TmfStatisticsTreeManager
.getStatTree(fViewer
.getTreeID());
146 if (statsData
== null) {
147 /* The stat tree has been disposed, abort mission. */
151 Map
<String
, Long
> map
= eventsPerType
;
152 String name
= fJobTrace
.getName();
156 * "Global", "partial", "total", etc., it's all very confusing...
158 * The base view shows the total count for the trace and for
159 * each even types, organized in columns like this:
161 * | Global | Time range |
162 * trace name | A | B |
164 * <event 1> | C | D |
165 * <event 2> | ... | ... |
168 * Here, we called the cells like this:
171 * C : GlobalTypeCount(s)
172 * D : TimeRangeTypeCount(s)
176 /* Fill in an the event counts (either cells C or D) */
177 for (Map
.Entry
<String
, Long
> entry
: map
.entrySet()) {
178 statsData
.setTypeCount(name
, entry
.getKey(), fIsGlobal
, entry
.getValue());
182 * Calculate the totals (cell A or B, depending if isGlobal). We will
183 * use the results of the previous request instead of sending another
186 long globalTotal
= 0;
187 for (long val
: map
.values()) {
190 /* Update both the tree model and the piechart model */
191 statsData
.setTotal(name
, fIsGlobal
, globalTotal
);
192 TmfPieChartStatisticsModel model
= fViewer
.getPieChartModel();
194 model
.setPieChartTypeCount(fIsGlobal
, fJobTrace
, eventsPerType
);
196 /* notify that the viewer needs to be refreshed */
197 fViewer
.modelComplete(fIsGlobal
);