timing: ss statistics use the common statistics
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / analysis / timing / core / segmentstore / statistics / AbstractSegmentStatisticsAnalysis.java
1 /*******************************************************************************
2 * Copyright (c) 2016 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 package org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics;
10
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Iterator;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.function.Function;
17
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.jdt.annotation.NonNull;
20 import org.eclipse.jdt.annotation.Nullable;
21 import org.eclipse.tracecompass.analysis.timing.core.segmentstore.ISegmentStoreProvider;
22 import org.eclipse.tracecompass.analysis.timing.core.statistics.IStatistics;
23 import org.eclipse.tracecompass.analysis.timing.core.statistics.Statistics;
24 import org.eclipse.tracecompass.segmentstore.core.ISegment;
25 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
26 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
27 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
28 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
29 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
30 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
31
32 import com.google.common.collect.ImmutableList;
33
34 /**
35 * Abstract analysis to build statistics data for a segment store
36 *
37 * @author Jean-Christian Kouame
38 */
39 public abstract class AbstractSegmentStatisticsAnalysis extends TmfAbstractAnalysisModule {
40
41 private static Function<ISegment, Long> FCT_LENGTH = s -> s.getLength();
42
43 private @Nullable ISegmentStoreProvider fSegmentStoreProviderModule;
44
45 private @Nullable IStatistics<ISegment> fTotalStats;
46
47 private Map<String, IStatistics<ISegment>> fPerSegmentTypeStats = new HashMap<>();
48
49 @Override
50 protected Iterable<IAnalysisModule> getDependentAnalyses() {
51 ITmfTrace trace = getTrace();
52 if (trace != null) {
53 ISegmentStoreProvider provider = getSegmentProviderAnalysis(trace);
54 fSegmentStoreProviderModule = provider;
55 if (provider instanceof IAnalysisModule) {
56 return ImmutableList.of((IAnalysisModule) provider);
57 }
58 }
59 return super.getDependentAnalyses();
60 }
61
62 @Override
63 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
64 if (monitor.isCanceled()) {
65 return false;
66 }
67
68 IStatistics<ISegment> totalStats = getTotalStats(TmfTimeRange.ETERNITY.getStartTime().toNanos(), TmfTimeRange.ETERNITY.getEndTime().toNanos(), monitor);
69 if (totalStats == null) {
70 return false;
71 }
72
73 Map<String, IStatistics<ISegment>> perTypeStats = getPerTypeStats(TmfTimeRange.ETERNITY.getStartTime().toNanos(), TmfTimeRange.ETERNITY.getEndTime().toNanos(), monitor);
74 fTotalStats = totalStats;
75 fPerSegmentTypeStats = perTypeStats;
76
77 return true;
78 }
79
80 private @Nullable IStatistics<ISegment> getTotalStats(long start, long end, IProgressMonitor monitor) {
81 Iterable<@NonNull ISegment> store = getSegmentStore(start, end);
82 if (store == null) {
83 return null;
84 }
85 if (monitor.isCanceled()) {
86 return null;
87 }
88 return calculateTotalManual(store, monitor);
89 }
90
91 /**
92 * Get the total statistics for a specific range. If the range start is
93 * TmfTimeRange.ETERNITY.getStartTime().toNanos() and the range end is
94 * TmfTimeRange.ETERNITY.getEndTime().toNanos(), it will return the
95 * statistics for the whole trace.
96 *
97 * @param start
98 * The start time of the range
99 * @param end
100 * The end time of the range
101 * @param monitor
102 * The progress monitor
103 * @return The total statistics, or null if segment store is not valid or if
104 * the request is canceled
105 * @since 1.2
106 * @deprecated use {@link #getStatsForRange(long, long, IProgressMonitor)} instead
107 */
108 @Deprecated
109 public @Nullable SegmentStoreStatistics getTotalStatsForRange(long start, long end, IProgressMonitor monitor) {
110 IStatistics<@NonNull ISegment> stats = getStatsForRange(start, end, monitor);
111 return (stats == null) ? null : new SegmentStoreStatistics(stats);
112 }
113
114 /**
115 * Get the total statistics for a specific range. If the range start is
116 * TmfTimeRange.ETERNITY.getStartTime().toNanos() and the range end is
117 * TmfTimeRange.ETERNITY.getEndTime().toNanos(), it will return the
118 * statistics for the whole trace.
119 *
120 * @param start
121 * The start time of the range
122 * @param end
123 * The end time of the range
124 * @param monitor
125 * The progress monitor
126 * @return The total statistics, or null if segment store is not valid or if
127 * the request is canceled
128 * @since 1.3
129 */
130 public @Nullable IStatistics<ISegment> getStatsForRange(long start, long end, IProgressMonitor monitor) {
131 ITmfTrace trace = getTrace();
132 if (trace != null && (start == TmfTimeRange.ETERNITY.getStartTime().toNanos() && end == TmfTimeRange.ETERNITY.getEndTime().toNanos())) {
133 waitForCompletion();
134 return getStatsTotal();
135 }
136 return getTotalStats(start, end, monitor);
137 }
138
139 private Map<@NonNull String, org.eclipse.tracecompass.analysis.timing.core.statistics.IStatistics<ISegment>> getPerTypeStats(long start, long end, IProgressMonitor monitor) {
140 Iterable<@NonNull ISegment> store = getSegmentStore(start, end);
141 if (monitor.isCanceled() || store == null) {
142 return Collections.EMPTY_MAP;
143 }
144 return calculateTotalPerType(store, monitor);
145 }
146
147 /**
148 * Get the per segment type statistics for a specific range. If the range
149 * start is TmfTimeRange.ETERNITY.getStartTime().toNanos() and the range end
150 * is TmfTimeRange.ETERNITY.getEndTime().toNanos(), it will return the
151 * statistics for the whole trace.
152 *
153 * @param start
154 * The start time of the range
155 * @param end
156 * The end time of the range
157 * @param monitor
158 * The progress monitor
159 * @return The per segment type statistics, or null if segment store is not
160 * valid or if the request is canceled
161 * @since 1.2
162 * @deprecated use {@link #getStatsPerTypeForRange(long, long, IProgressMonitor)} instead
163 */
164 @Deprecated
165 public @Nullable Map<String, SegmentStoreStatistics> getPerSegmentTypeStatsForRange(long start, long end, IProgressMonitor monitor) {
166 Map<String, IStatistics<ISegment>> stats = getStatsPerTypeForRange(start, end, monitor);
167 Map<String, SegmentStoreStatistics> map = new HashMap<>();
168 for (Entry<String, IStatistics<ISegment>> entry : stats.entrySet()) {
169 map.put(entry.getKey(), new SegmentStoreStatistics(entry.getValue()));
170 }
171 return map;
172 }
173
174 /**
175 * Get the per segment type statistics for a specific range. If the range
176 * start is TmfTimeRange.ETERNITY.getStartTime().toNanos() and the range end
177 * is TmfTimeRange.ETERNITY.getEndTime().toNanos(), it will return the
178 * statistics for the whole trace.
179 *
180 * @param start
181 * The start time of the range
182 * @param end
183 * The end time of the range
184 * @param monitor
185 * The progress monitor
186 * @return The per segment type statistics, or null if segment store is not
187 * valid or if the request is canceled
188 * @since 1.3
189 */
190 public Map<@NonNull String, org.eclipse.tracecompass.analysis.timing.core.statistics.IStatistics<ISegment>> getStatsPerTypeForRange(long start, long end, IProgressMonitor monitor) {
191 ITmfTrace trace = getTrace();
192 if (trace != null && (start == TmfTimeRange.ETERNITY.getStartTime().toNanos() && end == TmfTimeRange.ETERNITY.getEndTime().toNanos())) {
193 waitForCompletion();
194 return getStatsPerType();
195 }
196 return getPerTypeStats(start, end, monitor);
197 }
198
199 /**
200 * Get the segment store from which we want the statistics
201 *
202 * @return The segment store
203 */
204 private @Nullable Iterable<@NonNull ISegment> getSegmentStore(long start, long end) {
205 ISegmentStoreProvider segmentStoreProviderModule = fSegmentStoreProviderModule;
206 if (segmentStoreProviderModule == null) {
207 return null;
208 }
209 if (segmentStoreProviderModule instanceof IAnalysisModule) {
210 ((IAnalysisModule) segmentStoreProviderModule).waitForCompletion();
211 }
212
213 ISegmentStore<@NonNull ISegment> segmentStore = segmentStoreProviderModule.getSegmentStore();
214 return segmentStore != null ? start != TmfTimeRange.ETERNITY.getStartTime().toNanos() || end != TmfTimeRange.ETERNITY.getEndTime().toNanos() ? (Iterable<@NonNull ISegment>) segmentStore.getIntersectingElements(start, end) : segmentStore
215 : Collections.EMPTY_LIST;
216 }
217
218 private static @Nullable IStatistics<ISegment> calculateTotalManual(Iterable<@NonNull ISegment> segments, IProgressMonitor monitor) {
219 IStatistics<ISegment> total = new Statistics<>(FCT_LENGTH);
220 Iterator<@NonNull ISegment> iter = segments.iterator();
221 while (iter.hasNext()) {
222 if (monitor.isCanceled()) {
223 return null;
224 }
225 ISegment segment = iter.next();
226 total.update(segment);
227 }
228 return total;
229 }
230
231 private Map<@NonNull String, org.eclipse.tracecompass.analysis.timing.core.statistics.IStatistics<ISegment>> calculateTotalPerType(Iterable<ISegment> segments, IProgressMonitor monitor) {
232 Map<String, IStatistics<ISegment>> perSegmentTypeStats = new HashMap<>();
233
234 Iterator<ISegment> iter = segments.iterator();
235 while (iter.hasNext()) {
236 if (monitor.isCanceled()) {
237 return Collections.EMPTY_MAP;
238 }
239 ISegment segment = iter.next();
240 String segmentType = getSegmentType(segment);
241 if (segmentType != null) {
242 IStatistics<ISegment> values = perSegmentTypeStats.get(segmentType);
243 if (values == null) {
244 values = new Statistics<>(FCT_LENGTH);
245 }
246 values.update(segment);
247 perSegmentTypeStats.put(segmentType, values);
248 }
249 }
250 return perSegmentTypeStats;
251 }
252
253 /**
254 * Get the type of a segment. Statistics per type will use this type as a
255 * key
256 *
257 * @param segment
258 * the segment for which to get the type
259 * @return The type of the segment
260 */
261 protected abstract @Nullable String getSegmentType(ISegment segment);
262
263 /**
264 * Find the segment store provider used for this analysis
265 *
266 * @param trace
267 * The active trace
268 *
269 * @return The segment store provider
270 */
271 protected abstract @Nullable ISegmentStoreProvider getSegmentProviderAnalysis(ITmfTrace trace);
272
273 @Override
274 protected void canceling() {
275 }
276
277 /**
278 * The total statistics
279 *
280 * @return the total statistics
281 * @deprecated use {@link #getStatsTotal()} instead
282 */
283 @Deprecated
284 public @Nullable SegmentStoreStatistics getTotalStats() {
285 IStatistics<@NonNull ISegment> totalStats = fTotalStats;
286 return totalStats == null ? null : new SegmentStoreStatistics(totalStats);
287 }
288
289 /**
290 * Get the statistics for the full segment store
291 *
292 * @return The complete statistics
293 * @since 1.3
294 */
295 public @Nullable IStatistics<ISegment> getStatsTotal() {
296 return fTotalStats;
297 }
298
299 /**
300 * The per syscall statistics
301 *
302 * @return the per syscall statistics
303 * @deprecated use {@link #getStatsPerType()} instead
304 */
305 @Deprecated
306 public @Nullable Map<String, SegmentStoreStatistics> getPerSegmentTypeStats() {
307 Map<String, SegmentStoreStatistics> map = new HashMap<>();
308 for (Entry<String, IStatistics<ISegment>> entry : fPerSegmentTypeStats.entrySet()) {
309 map.put(entry.getKey(), new SegmentStoreStatistics(entry.getValue()));
310 }
311 return map;
312 }
313
314 /**
315 * Get the statistics for each type of segment in this segment store
316 *
317 * @return the map of statistics per type
318 * @since 1.3
319 */
320 public Map<String, IStatistics<ISegment>> getStatsPerType() {
321 return fPerSegmentTypeStats;
322 }
323
324 }
This page took 0.041748 seconds and 6 git commands to generate.