-
Notifications
You must be signed in to change notification settings - Fork 10
/
TmfStateStatistics.java
355 lines (313 loc) · 13.2 KB
/
TmfStateStatistics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*******************************************************************************
* Copyright (c) 2012, 2018 Ericsson
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License 2.0 which
* accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Alexandre Montplaisir - Initial API and implementation
* Patrick Tasse - Fix TimeRangeException
******************************************************************************/
package org.eclipse.tracecompass.tmf.core.statistics;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.tracecompass.common.core.log.TraceCompassLog;
import org.eclipse.tracecompass.common.core.log.TraceCompassLogUtils.FlowScopeLog;
import org.eclipse.tracecompass.common.core.log.TraceCompassLogUtils.FlowScopeLogBuilder;
import org.eclipse.tracecompass.statesystem.core.ITmfStateSystem;
import org.eclipse.tracecompass.statesystem.core.exceptions.StateSystemDisposedException;
import org.eclipse.tracecompass.statesystem.core.interval.ITmfStateInterval;
import com.google.common.collect.Lists;
/**
* Implementation of ITmfStatistics which uses a state history for storing its
* information. In reality, it uses two state histories, one for "event totals"
* information (which should ideally use a fast backend), and another one for
* the rest (per event type, per CPU, etc.).
*
* Compared to the event-request-based statistics calculations, it adds the
* building the history first, but gives much faster response times once built :
* Queries are O(log n) wrt the size of the trace, and O(1) wrt to the size of
* the time interval selected.
*
* @author Alexandre Montplaisir
*/
public class TmfStateStatistics implements ITmfStatistics {
// ------------------------------------------------------------------------
// Fields
// ------------------------------------------------------------------------
/** The event totals state system */
private final ITmfStateSystem fTotalsStats;
/** The state system for event types */
private final ITmfStateSystem fTypesStats;
private static final @NonNull Logger LOGGER = TraceCompassLog.getLogger(TmfStateStatistics.class);
// ------------------------------------------------------------------------
// Constructors
// ------------------------------------------------------------------------
/**
* Constructor
*
* @param totals
* The state system containing the "totals" information
* @param eventTypes
* The state system containing the "event types" information
*/
public TmfStateStatistics(@NonNull ITmfStateSystem totals, @NonNull ITmfStateSystem eventTypes) {
fTotalsStats = totals;
fTypesStats = eventTypes;
}
/**
* Return the state system containing the "totals" values
*
* @return The "totals" state system
*/
public ITmfStateSystem getTotalsSS() {
return fTotalsStats;
}
/**
* Return the state system containing the "event types" values
*
* @return The "event types" state system
*/
public ITmfStateSystem getEventTypesSS() {
return fTypesStats;
}
// ------------------------------------------------------------------------
// ITmfStatistics
// ------------------------------------------------------------------------
@Override
public void dispose() {
fTotalsStats.dispose();
fTypesStats.dispose();
}
@Override
public List<@NonNull Long> histogramQuery(long[] timeRequested) {
final List<@NonNull Long> list = new ArrayList<>();
if (fTotalsStats.isCancelled()) {
return list;
}
final int quark = fTotalsStats.optQuarkAbsolute(Attributes.TOTAL);
if (quark == ITmfStateSystem.INVALID_ATTRIBUTE) {
return Collections.emptyList();
}
List<@NonNull Long> times = new ArrayList<>();
for (int i = 0; i < timeRequested.length; i++) {
/* create padding for every window of time before the start time */
if (timeRequested[i] < fTotalsStats.getStartTime()) {
list.add(0L);
} else {
if (!list.isEmpty()) {
/* replace this bucket with the [start time, next time] */
times.add(fTotalsStats.getStartTime());
list.remove(list.size() - 1);
}
times.add(timeRequested[i]);
}
if (timeRequested[i] > fTotalsStats.getCurrentEndTime()) {
times.add(fTotalsStats.getCurrentEndTime());
break;
}
}
try (FlowScopeLog log = new FlowScopeLogBuilder(LOGGER, Level.FINE, "StateStatistics:histogramQuery").build()) { //$NON-NLS-1$
addHistogramValuesFromQuery2d(quark, times, list);
/* Padding for after trace ends */
for (int i = list.size(); i < timeRequested.length; i++) {
list.add(0L);
}
return list;
}
}
private List<@NonNull Long> addHistogramValuesFromQuery2d(int quark, List<@NonNull Long> times, List<@NonNull Long> results) {
try {
Iterable<@NonNull ITmfStateInterval> intervals = fTotalsStats.query2D(Collections.singletonList(quark), times);
List<@NonNull ITmfStateInterval> sortedIntervals = Lists.newArrayList(intervals);
if (sortedIntervals.isEmpty()) {
return results;
}
sortedIntervals.sort(Comparator.comparingLong(ITmfStateInterval::getStartTime));
long previousTotal;
if (fTotalsStats.getStartTime() != sortedIntervals.get(0).getStartTime()) {
previousTotal = sortedIntervals.get(0).getValueLong();
} else {
previousTotal = 0;
}
int j = 0;
for (int i = 0; i < times.size(); i++) {
while (j < sortedIntervals.size() - 1 && sortedIntervals.get(j).getEndTime() < times.get(i)) {
j++;
}
long count = sortedIntervals.get(j).getValueLong() - previousTotal;
results.add(count);
previousTotal = sortedIntervals.get(j).getValueLong();
}
return results;
} catch (StateSystemDisposedException e) {
/*
* Assume there is no events, nothing will be put in the histogram.
*/
return results;
}
}
@Override
public long getEventsTotal() {
long endTime = fTotalsStats.getCurrentEndTime();
final int quark = fTotalsStats.optQuarkAbsolute(Attributes.TOTAL);
if (quark == ITmfStateSystem.INVALID_ATTRIBUTE) {
return 0;
}
try {
return extractCount(fTotalsStats.querySingleState(endTime, quark).getValue());
} catch (StateSystemDisposedException e) {
/* Assume there is no events for that range */
return 0;
}
}
@Override
public Map<@NonNull String, @NonNull Long> getEventTypesTotal() {
int quark = fTypesStats.optQuarkAbsolute(Attributes.EVENT_TYPES);
if (quark == ITmfStateSystem.INVALID_ATTRIBUTE) {
return Collections.emptyMap();
}
/* Get the list of quarks, one for each even type in the database */
List<Integer> quarks = fTypesStats.getSubAttributes(quark, false);
long endTime = fTypesStats.getCurrentEndTime();
final Map<@NonNull String, @NonNull Long> map = new HashMap<>();
try {
/* Since we want the total we can look only at the end */
List<ITmfStateInterval> endState = fTypesStats.queryFullState(endTime);
for (int typeQuark : quarks) {
String curEventName = fTypesStats.getAttributeName(typeQuark);
long eventCount = extractCount(endState.get(typeQuark).getValue());
map.put(curEventName, eventCount);
}
return map;
} catch (StateSystemDisposedException e) {
/* Assume there is no events, nothing will be put in the map. */
return Collections.emptyMap();
}
}
@Override
public long getEventsInRange(long start, long end) {
long startCount;
if (start == fTotalsStats.getStartTime()) {
startCount = 0;
} else {
/*
* We want the events happening at "start" to be included, so we'll
* need to query one unit before that point.
*/
startCount = getEventCountAt(start - 1);
}
long endCount = getEventCountAt(end);
return endCount - startCount;
}
@Override
public Map<String, Long> getEventTypesInRange(long start, long end) {
/*
* Make sure the start/end times are within the state history, so we
* don't get TimeRange exceptions.
*/
long startTime = Long.max(start, fTypesStats.getStartTime());
long endTime = Long.min(end, fTypesStats.getCurrentEndTime());
if (endTime < startTime) {
/*
* The start/end times do not intersect this state system range.
* Return the empty map.
*/
return Collections.emptyMap();
}
/* Get the list of quarks, one for each even type in the database */
int quark = fTypesStats.optQuarkAbsolute(Attributes.EVENT_TYPES);
if (quark == ITmfStateSystem.INVALID_ATTRIBUTE) {
/*
* The state system does not (yet?) have the needed attributes, it
* probably means there are no events counted yet. Return the empty
* map.
*/
return Collections.emptyMap();
}
List<Integer> quarks = fTypesStats.getSubAttributes(quark, false);
final Map<String, Long> map = new HashMap<>();
try {
List<ITmfStateInterval> endState = fTypesStats.queryFullState(endTime);
if (startTime == fTypesStats.getStartTime()) {
/* Only use the values picked up at the end time */
for (int typeQuark : quarks) {
String curEventName = fTypesStats.getAttributeName(typeQuark);
Object eventCount = endState.get(typeQuark).getValue();
map.put(curEventName, extractCount(eventCount));
}
} else {
/*
* Query the start time at -1, so the beginning of the interval
* is inclusive.
*/
List<ITmfStateInterval> startState = fTypesStats.queryFullState(startTime - 1);
for (int typeQuark : quarks) {
String curEventName = fTypesStats.getAttributeName(typeQuark);
Object countAtStart = startState.get(typeQuark).getValue();
Object countAtEnd = endState.get(typeQuark).getValue();
long eventCount = extractCount(countAtEnd) - extractCount(countAtStart);
map.put(curEventName, eventCount);
}
}
} catch (StateSystemDisposedException e) {
/*
* Assume there is no (more) events, nothing will be put in the map.
*/
}
return map;
}
// ------------------------------------------------------------------------
// Helper methods
// ------------------------------------------------------------------------
private long getEventCountAt(long timestamp) {
/* Make sure the target time is within the range of the history */
long ts = Long.max(fTotalsStats.getStartTime(), timestamp);
ts = Long.min(ts, fTotalsStats.getCurrentEndTime());
final int quark = fTotalsStats.optQuarkAbsolute(Attributes.TOTAL);
if (quark == ITmfStateSystem.INVALID_ATTRIBUTE) {
return 0l;
}
try {
return extractCount(fTotalsStats.querySingleState(ts, quark).getValue());
} catch (StateSystemDisposedException e) {
/*
* Assume there is no (more) events, nothing will be put in the map.
*/
return 0;
}
}
private static long extractCount(@Nullable Object state) {
if (state instanceof Number) {
return ((Number) state).longValue();
}
return 0l;
}
/**
* The attribute names that are used in the state provider
*/
public static class Attributes {
/** Total nb of events */
public static final String TOTAL = "total"; //$NON-NLS-1$
/** event_types */
public static final String EVENT_TYPES = "event_types"; //$NON-NLS-1$
/**
* lost_events
*
* @since 2.0
*/
public static final String LOST_EVENTS = "lost_events"; //$NON-NLS-1$
}
}