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
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.Collection;
12 import java.util.Collections;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.Map;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.analysis.timing.core.segmentstore.ISegmentStoreProvider;
21 import org.eclipse.tracecompass.segmentstore.core.ISegment;
22 import org.eclipse.tracecompass.segmentstore.core.ISegmentStore;
23 import org.eclipse.tracecompass.tmf.core.analysis.IAnalysisModule;
24 import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
25 import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
26 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
27 import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
28
29 import 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 */
36 public abstract class AbstractSegmentStatisticsAnalysis extends TmfAbstractAnalysisModule {
37
38 private @Nullable IAnalysisModule fSegmentStoreProviderModule;
39
40 private @Nullable SegmentStoreStatistics fTotalStats;
41
42 private Map<String, SegmentStoreStatistics> fPerSegmentTypeStats = new HashMap<>();
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 {
59 if (monitor.isCanceled()) {
60 return false;
61 }
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 }
75
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 }
86
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 }
111
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 }
119
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 }
144
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;
154 }
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;
161 }
162
163 private static @Nullable SegmentStoreStatistics calculateTotalManual(Collection<@NonNull ISegment> segments, IProgressMonitor monitor) {
164 SegmentStoreStatistics total = new SegmentStoreStatistics();
165 Iterator<@NonNull ISegment> iter = segments.iterator();
166 while (iter.hasNext()) {
167 if (monitor.isCanceled()) {
168 return null;
169 }
170 @NonNull ISegment segment = iter.next();
171 total.update(segment);
172 }
173 return total;
174 }
175
176 private Map<@NonNull String, @NonNull SegmentStoreStatistics> calculateTotalPerType(Collection<ISegment> segments, IProgressMonitor monitor) {
177 Map<String, SegmentStoreStatistics> perSegmentTypeStats = new HashMap<>();
178
179 Iterator<ISegment> iter = segments.iterator();
180 while (iter.hasNext()) {
181 if (monitor.isCanceled()) {
182 return Collections.EMPTY_MAP;
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 }
195 return perSegmentTypeStats;
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.038234 seconds and 5 git commands to generate.