tmf.all: use ITmfTimestamp#toNanos when possible
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / internal / tmf / ui / viewers / statistics / StatisticsUpdateJob.java
CommitLineData
cdf994ef
ACL
1/*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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 - Initial API and implementation
11 * Alexis Cabana-Loriaux - Extract the class in a compilation unit
12 *******************************************************************************/
13
1743f395 14package org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics;
cdf994ef
ACL
15
16import java.util.Map;
17
18import org.eclipse.core.runtime.IProgressMonitor;
19import org.eclipse.core.runtime.IStatus;
20import org.eclipse.core.runtime.Status;
21import org.eclipse.core.runtime.jobs.Job;
1743f395
BH
22import org.eclipse.tracecompass.internal.tmf.ui.viewers.piecharts.model.TmfPieChartStatisticsModel;
23import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfStatisticsTree;
24import org.eclipse.tracecompass.internal.tmf.ui.viewers.statistics.model.TmfStatisticsTreeManager;
cdf994ef
ACL
25import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
26import org.eclipse.tracecompass.tmf.core.statistics.ITmfStatistics;
27import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsEventTypesModule;
28import org.eclipse.tracecompass.tmf.core.statistics.TmfStatisticsModule;
cdf994ef
ACL
29import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
30import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
cdf994ef
ACL
31
32/**
33 * Class used to update the Statistics view. Normally, it should only be used by
34 * this class
35 *
36 * @author Mathieu Denis
37 */
38class StatisticsUpdateJob extends Job {
39
40 private final ITmfTrace fJobTrace;
41 private final boolean fIsGlobal;
42 private final TmfStatisticsModule fStatsMod;
43 private final TmfStatisticsViewer fViewer;
44
45 /**
46 * The delay (in ms) between each update in live-reading mode
47 */
48 private static final long LIVE_UPDATE_DELAY = 1000;
49
cdf994ef
ACL
50 private TmfTimeRange fTimerange;
51
52 /**
53 * @param name
54 * The name of the working job
55 * @param trace
56 * The trace to query
57 * @param isGlobal
58 * If the query is for the global time-range or a selection
59 * time-range
60 * @param timerange
61 * The timerange of
62 * @param statsMod
63 * The statistics module of the trace
64 * @param viewer
65 * The viewer to update
66 */
67 public StatisticsUpdateJob(String name, ITmfTrace trace, boolean isGlobal, TmfTimeRange timerange, TmfStatisticsModule statsMod, TmfStatisticsViewer viewer) {
68 super(name);
69 fJobTrace = trace;
70 fIsGlobal = isGlobal;
71 fTimerange = timerange;
72 fStatsMod = statsMod;
73 fViewer = viewer;
74 }
75
76 @Override
77 protected IStatus run(IProgressMonitor monitor) {
78
79 /* Wait until the analysis is ready to be queried */
80 fStatsMod.waitForInitialization();
81 ITmfStatistics stats = fStatsMod.getStatistics();
82 if (stats == null) {
83 /* It should have worked, but didn't */
84 throw new IllegalStateException();
85 }
86
87 /*
88 * TODO Eventually this could be exposed through the
89 * TmfStateSystemAnalysisModule directly.
90 */
91 ITmfStateSystem ss = fStatsMod.getStateSystem(TmfStatisticsEventTypesModule.ID);
92 if (ss == null) {
93 /*
94 * It should be instantiated after the
95 * statsMod.waitForInitialization() above.
96 */
97 throw new IllegalStateException();
98 }
99
100 /*
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.
104 */
105 long start = 0;
106 long end = 0;
107 boolean finished = false;
108 do {
109 /* This model update is done every second */
110 if (monitor.isCanceled()) {
111 fViewer.removeFromJobs(fIsGlobal, fJobTrace);
112 return Status.CANCEL_STATUS;
113 }
114 finished = ss.waitUntilBuilt(LIVE_UPDATE_DELAY);
115 TmfTimeRange localtimeRange = fTimerange;
116 /*
117 * The generic statistics are stored in nanoseconds, so we must make
118 * sure the time range is scaled correctly.
119 */
16801c72
MK
120 start = localtimeRange.getStartTime().toNanos();
121 end = localtimeRange.getEndTime().toNanos();
cdf994ef
ACL
122
123 Map<String, Long> map = stats.getEventTypesInRange(start, end);
124 updateStats(map);
125 } while (!finished);
126
1743f395 127
cdf994ef
ACL
128 /* Query one last time for the final values */
129 Map<String, Long> map = stats.getEventTypesInRange(start, end);
130 updateStats(map);
131 fViewer.refreshPieCharts(fIsGlobal, !fIsGlobal);
132 /*
133 * Remove job from map so that new range selection updates can be
134 * processed.
135 */
136 fViewer.removeFromJobs(fIsGlobal, fJobTrace);
137 return Status.OK_STATUS;
138 }
139
140 /*
141 * Update the tree for a given trace
142 */
143 private void updateStats(Map<String, Long> eventsPerType) {
144
145 final TmfStatisticsTree statsData = TmfStatisticsTreeManager.getStatTree(fViewer.getTreeID());
146 if (statsData == null) {
147 /* The stat tree has been disposed, abort mission. */
148 return;
149 }
150
151 Map<String, Long> map = eventsPerType;
152 String name = fJobTrace.getName();
153
154 /**
155 * <pre>
156 * "Global", "partial", "total", etc., it's all very confusing...
157 *
158 * The base view shows the total count for the trace and for
159 * each even types, organized in columns like this:
160 *
161 * | Global | Time range |
162 * trace name | A | B |
163 * Event Type | | |
164 * <event 1> | C | D |
165 * <event 2> | ... | ... |
166 * ... | | |
167 *
168 * Here, we called the cells like this:
169 * A : GlobalTotal
170 * B : TimeRangeTotal
171 * C : GlobalTypeCount(s)
172 * D : TimeRangeTypeCount(s)
173 * </pre>
174 */
175
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());
179 }
180
181 /*
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
184 * one.
185 */
186 long globalTotal = 0;
187 for (long val : map.values()) {
188 globalTotal += val;
189 }
190 /* Update both the tree model and the piechart model */
191 statsData.setTotal(name, fIsGlobal, globalTotal);
192 TmfPieChartStatisticsModel model = fViewer.getPieChartModel();
193 if (model != null) {
194 model.setPieChartTypeCount(fIsGlobal, fJobTrace, eventsPerType);
195 }
196 /* notify that the viewer needs to be refreshed */
197 fViewer.modelComplete(fIsGlobal);
198 }
199}
This page took 0.056701 seconds and 5 git commands to generate.