timing.core: Add local statistics to the latency statistics
[deliverable/tracecompass.git] / analysis / org.eclipse.tracecompass.analysis.timing.core / src / org / eclipse / tracecompass / analysis / timing / core / segmentstore / statistics / AbstractSegmentStatisticsAnalysis.java
CommitLineData
abda4cb0
JCK
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 *******************************************************************************/
5b901f94 9package org.eclipse.tracecompass.analysis.timing.core.segmentstore.statistics;
abda4cb0 10
b405ad64
JCK
11import java.util.Collection;
12import java.util.Collections;
abda4cb0
JCK
13import java.util.HashMap;
14import java.util.Iterator;
15import java.util.Map;
16
17import org.eclipse.core.runtime.IProgressMonitor;
b405ad64 18import org.eclipse.jdt.annotation.NonNull;
abda4cb0
JCK
19import org.eclipse.jdt.annotation.Nullable;
20import org.eclipse.tracecompass.analysis.timing.core.segmentstore.ISegmentStoreProvider;
21import org.eclipse.tracecompass.segmentstore.core.ISegment;
22import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
23import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
24import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
25import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
b405ad64 26import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
abda4cb0
JCK
27import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
28
29import com.google.common.collect.ImmutableList;
30
31/**
32 * Abstract analysis to build statistics data for a segment store
33 *
34 * @author Jean-Christian Kouame
35 */
36public abstract class AbstractSegmentStatisticsAnalysis extends TmfAbstractAnalysisModule {
37
38 private @Nullable IAnalysisModule fSegmentStoreProviderModule;
39
40 private @Nullable SegmentStoreStatistics fTotalStats;
41
b405ad64 42 private Map<String, SegmentStoreStatistics> fPerSegmentTypeStats = new HashMap<>();
abda4cb0
JCK
43
44 @Override
45 protected Iterable<IAnalysisModule> getDependentAnalyses() {
46 ITmfTrace trace = getTrace();
47 if (trace != null) {
48 ISegmentStoreProvider provider = getSegmentProviderAnalysis(trace);
49 if (provider instanceof IAnalysisModule) {
50 fSegmentStoreProviderModule = (IAnalysisModule) provider;
51 return ImmutableList.of((IAnalysisModule) provider);
52 }
53 }
54 return super.getDependentAnalyses();
55 }
56
57 @Override
58 protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
b405ad64 59 if (monitor.isCanceled()) {
abda4cb0
JCK
60 return false;
61 }
b405ad64
JCK
62 @Nullable SegmentStoreStatistics totalStats = getTotalStats(TmfTimeRange.ETERNITY.getStartTime().toNanos(), TmfTimeRange.ETERNITY.getEndTime().toNanos(), monitor);
63 if (totalStats == null) {
64 return false;
65 }
66
67 @Nullable Map<@NonNull String, @NonNull SegmentStoreStatistics> perTypeStats = getPerTypeStats(TmfTimeRange.ETERNITY.getStartTime().toNanos(), TmfTimeRange.ETERNITY.getEndTime().toNanos(), monitor);
68 if (perTypeStats == null) {
69 return false;
70 }
71 fTotalStats = totalStats;
72 fPerSegmentTypeStats = perTypeStats;
73 return true;
74 }
abda4cb0 75
b405ad64
JCK
76 private @Nullable SegmentStoreStatistics getTotalStats(long start, long end , IProgressMonitor monitor) {
77 Collection<@NonNull ISegment> store = getSegmentStore(start, end);
78 if (store == null) {
79 return null;
80 }
81 if (monitor.isCanceled()) {
82 return null;
83 }
84 return calculateTotalManual(store, monitor);
85 }
abda4cb0 86
b405ad64
JCK
87 /**
88 * Get the total statistics for a specific range. If the range start is
89 * TmfTimeRange.ETERNITY.getStartTime().toNanos() and the range end is
90 * TmfTimeRange.ETERNITY.getEndTime().toNanos(), it will return the
91 * statistics for the whole trace.
92 *
93 * @param start
94 * The start time of the range
95 * @param end
96 * The end time of the range
97 * @param monitor
98 * The progress monitor
99 * @return The total statistics, or null if segment store is not valid or if
100 * the request is canceled
101 * @since 1.2
102 */
103 public @Nullable SegmentStoreStatistics getTotalStatsForRange(long start, long end, IProgressMonitor monitor) {
104 @Nullable ITmfTrace trace = getTrace();
105 if (trace != null && (start == TmfTimeRange.ETERNITY.getStartTime().toNanos() && end == TmfTimeRange.ETERNITY.getEndTime().toNanos())) {
106 waitForCompletion();
107 return getTotalStats();
108 }
109 return getTotalStats(start, end, monitor);
110 }
abda4cb0 111
b405ad64
JCK
112 private @Nullable Map<@NonNull String, @NonNull SegmentStoreStatistics> getPerTypeStats(long start, long end , IProgressMonitor monitor) {
113 Collection<@NonNull ISegment> store = getSegmentStore(start, end);
114 if (monitor.isCanceled()) {
115 return Collections.EMPTY_MAP;
116 }
117 return calculateTotalPerType(store, monitor);
118 }
abda4cb0 119
b405ad64
JCK
120 /**
121 * Get the per segment type statistics for a specific range. If the range
122 * start is TmfTimeRange.ETERNITY.getStartTime().toNanos() and the range end
123 * is TmfTimeRange.ETERNITY.getEndTime().toNanos(), it will return the
124 * statistics for the whole trace.
125 *
126 * @param start
127 * The start time of the range
128 * @param end
129 * The end time of the range
130 * @param monitor
131 * The progress monitor
132 * @return The per segment type statistics, or null if segment store is not
133 * valid or if the request is canceled
134 * @since 1.2
135 */
136 public @Nullable Map<@NonNull String, @NonNull SegmentStoreStatistics> getPerSegmentTypeStatsForRange(long start, long end, IProgressMonitor monitor) {
137 @Nullable ITmfTrace trace = getTrace();
138 if (trace != null && (start == TmfTimeRange.ETERNITY.getStartTime().toNanos() && end == TmfTimeRange.ETERNITY.getEndTime().toNanos())) {
139 waitForCompletion();
140 return getPerSegmentTypeStats();
141 }
142 return getPerTypeStats(start, end, monitor);
143 }
abda4cb0 144
b405ad64
JCK
145 /**
146 * Get the segment store from which we want the statistics
147 *
148 * @return The segment store
149 */
150 private @Nullable Collection<@NonNull ISegment> getSegmentStore(long start, long end) {
151 IAnalysisModule segmentStoreProviderModule = fSegmentStoreProviderModule;
152 if (!(segmentStoreProviderModule instanceof ISegmentStoreProvider)) {
153 return null;
abda4cb0 154 }
b405ad64
JCK
155 segmentStoreProviderModule.waitForCompletion();
156
157 @Nullable ISegmentStore<@NonNull ISegment> segmentStore = ((ISegmentStoreProvider) segmentStoreProviderModule).getSegmentStore();
158 return segmentStore != null ?
159 start != TmfTimeRange.ETERNITY.getStartTime().toNanos() || end != TmfTimeRange.ETERNITY.getEndTime().toNanos() ? (Collection<@NonNull ISegment>) segmentStore.getIntersectingElements(start, end) : segmentStore
160 : Collections.EMPTY_LIST;
abda4cb0
JCK
161 }
162
b405ad64 163 private static @Nullable SegmentStoreStatistics calculateTotalManual(Collection<@NonNull ISegment> segments, IProgressMonitor monitor) {
abda4cb0 164 SegmentStoreStatistics total = new SegmentStoreStatistics();
b405ad64 165 Iterator<@NonNull ISegment> iter = segments.iterator();
abda4cb0
JCK
166 while (iter.hasNext()) {
167 if (monitor.isCanceled()) {
b405ad64 168 return null;
abda4cb0 169 }
b405ad64
JCK
170 @NonNull ISegment segment = iter.next();
171 total.update(segment);
abda4cb0 172 }
b405ad64 173 return total;
abda4cb0
JCK
174 }
175
b405ad64 176 private Map<@NonNull String, @NonNull SegmentStoreStatistics> calculateTotalPerType(Collection<ISegment> segments, IProgressMonitor monitor) {
abda4cb0
JCK
177 Map<String, SegmentStoreStatistics> perSegmentTypeStats = new HashMap<>();
178
b405ad64 179 Iterator<ISegment> iter = segments.iterator();
abda4cb0
JCK
180 while (iter.hasNext()) {
181 if (monitor.isCanceled()) {
b405ad64 182 return Collections.EMPTY_MAP;
abda4cb0
JCK
183 }
184 ISegment segment = iter.next();
185 String segmentType = getSegmentType(segment);
186 if (segmentType != null) {
187 SegmentStoreStatistics values = perSegmentTypeStats.get(segmentType);
188 if (values == null) {
189 values = new SegmentStoreStatistics();
190 }
191 values.update(segment);
192 perSegmentTypeStats.put(segmentType, values);
193 }
194 }
b405ad64 195 return perSegmentTypeStats;
abda4cb0
JCK
196 }
197
198 /**
199 * Get the type of a segment. Statistics per type will use this type as a
200 * key
201 *
202 * @param segment
203 * the segment for which to get the type
204 * @return The type of the segment
205 */
206 protected abstract @Nullable String getSegmentType(ISegment segment);
207
208 /**
209 * Find the segment store provider used for this analysis
210 *
211 * @param trace
212 * The active trace
213 *
214 * @return The segment store provider
215 */
216 protected abstract @Nullable ISegmentStoreProvider getSegmentProviderAnalysis(ITmfTrace trace);
217
218 @Override
219 protected void canceling() {
220 }
221
222 /**
223 * The total statistics
224 *
225 * @return the total statistics
226 */
227 public @Nullable SegmentStoreStatistics getTotalStats() {
228 return fTotalStats;
229 }
230
231 /**
232 * The per syscall statistics
233 *
234 * @return the per syscall statistics
235 */
236 public @Nullable Map<String, SegmentStoreStatistics> getPerSegmentTypeStats() {
237 return fPerSegmentTypeStats;
238 }
239
240}
This page took 0.042162 seconds and 5 git commands to generate.