Add support for older Eclipse version (e.g. 3.6) than 3.8
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / statistics / TmfEventsStatistics.java
CommitLineData
1c0de632
AM
1/*******************************************************************************
2 * Copyright (c) 2012 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 * Alexandre Montplaisir - Initial API and implementation
11 ******************************************************************************/
12
13package org.eclipse.linuxtools.tmf.core.statistics;
14
15import java.util.HashMap;
16import java.util.Map;
17
18import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
19import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
20import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
8b260d9f 21import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
1c0de632
AM
22import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
23import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
24import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
25import org.eclipse.linuxtools.tmf.core.signal.TmfSignal;
26import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
27import org.eclipse.linuxtools.tmf.core.signal.TmfStatsUpdatedSignal;
28import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29
30/**
31 * Implementation of ITmfStatistics which uses event requests to the trace to
32 * retrieve its information.
33 *
34 * There is almost no setup time, but queries themselves are longer than with a
35 * TmfStateStatistics. Queries are O(n * m), where n is the size of the trace,
36 * and m is the portion of the trace covered by the selected interval.
37 *
38 * @author Alexandre Montplaisir
39 * @since 2.0
40 */
41public class TmfEventsStatistics implements ITmfStatistics {
42
8b260d9f
AM
43 /* All timestamps should be stored in nanoseconds in the statistics backend */
44 private static final int SCALE = ITmfTimestamp.NANOSECOND_SCALE;
45
1c0de632
AM
46 private final ITmfTrace trace;
47
48 /* Event request objects for the time-range request. */
49 private StatsTotalRequest totalRequest = null;
50 private StatsPerTypeRequest perTypeRequest = null;
51
52 /**
53 * Constructor
54 *
55 * @param trace
56 * The trace for which we are building the statistics
57 */
58 public TmfEventsStatistics(ITmfTrace trace) {
59 this.trace = trace;
60 }
61
1a4205d9
AM
62 @Override
63 public void dispose() {
64 cancelOngoingRequests();
65 }
66
1c0de632 67 @Override
8b260d9f 68 public void updateStats(final boolean isGlobal, long start, long end) {
1c0de632
AM
69 cancelOngoingRequests();
70
71 /*
72 * Prepare and send the event requests. This needs to be done in the
73 * same thread, since it will be run by TmfStatisticsViewer's signal
74 * handlers, to ensure they get correctly coalesced.
75 */
8b260d9f
AM
76 ITmfTimestamp startTS = new TmfTimestamp(start, SCALE);
77 ITmfTimestamp endTS = new TmfTimestamp(end, SCALE);
78 TmfTimeRange range = isGlobal ? TmfTimeRange.ETERNITY : new TmfTimeRange(startTS, endTS);
1c0de632
AM
79 final StatsTotalRequest totalReq = new StatsTotalRequest(trace, range);
80 final StatsPerTypeRequest perTypeReq = new StatsPerTypeRequest(trace, range);
81
82 /*
83 * Only allow one time-range request at a time (there should be only one
84 * global request at the beginning anyway, no need to track those).
85 */
86 if (!isGlobal) {
87 this.totalRequest = totalReq;
88 this.perTypeRequest = perTypeReq;
89 }
90
91 trace.sendRequest(totalReq);
92 trace.sendRequest(perTypeReq);
93
94 /*
95 * This thread can now return. Start a new thread that will wait until
96 * the request are done and will then send the results.
97 */
98 Thread statsThread = new Thread("Statistics update") { //$NON-NLS-1$
99 @Override
100 public void run() {
101 /* Wait for both requests to complete */
102 try {
103 totalReq.waitForCompletion();
104 perTypeReq.waitForCompletion();
105 } catch (InterruptedException e) {
106 e.printStackTrace();
107 }
108
109 /*
110 * If the request was cancelled, this means a newer one was
111 * sent, discard the current one and return without sending
112 * the signal.
113 */
114 if (totalReq.isCancelled() || perTypeReq.isCancelled()) {
115 return;
116 }
117
118 /* If it completed successfully, retrieve the results. */
119 long total = totalReq.getResult();
120 Map<String, Long> map = perTypeReq.getResults();
121
122 /* Send the signal to notify the stats viewer to update its display. */
123 TmfSignal sig = new TmfStatsUpdatedSignal(this, trace, isGlobal, total, map);
124 TmfSignalManager.dispatchSignal(sig);
125 }
126 };
127 statsThread.start();
128 return;
129 }
130
131 private synchronized void cancelOngoingRequests() {
132 if (totalRequest != null && totalRequest.isRunning()) {
133 totalRequest.cancel();
134 }
135 if (perTypeRequest != null && perTypeRequest.isRunning()) {
136 perTypeRequest.cancel();
137 }
138 }
139
140 @Override
141 public long getEventsTotal() {
142 StatsTotalRequest request = new StatsTotalRequest(trace, TmfTimeRange.ETERNITY);
143 sendAndWait(request);
144
145 long total = request.getResult();
146 return total;
147 }
148
149 @Override
150 public Map<String, Long> getEventTypesTotal() {
151 StatsPerTypeRequest request = new StatsPerTypeRequest(trace, TmfTimeRange.ETERNITY);
152 sendAndWait(request);
153
154 Map<String, Long> stats = request.getResults();
155 return stats;
156 }
157
158 @Override
8b260d9f
AM
159 public long getEventsInRange(long start, long end) {
160 ITmfTimestamp startTS = new TmfTimestamp(start, SCALE);
161 ITmfTimestamp endTS = new TmfTimestamp(end, SCALE);
162 TmfTimeRange range = new TmfTimeRange(startTS, endTS);
163
1c0de632
AM
164 StatsTotalRequest request = new StatsTotalRequest(trace, range);
165 sendAndWait(request);
166
167 long total = request.getResult();
168 return total;
169 }
170
171 @Override
8b260d9f
AM
172 public Map<String, Long> getEventTypesInRange(long start, long end) {
173 ITmfTimestamp startTS = new TmfTimestamp(start, SCALE);
174 ITmfTimestamp endTS = new TmfTimestamp(end, SCALE);
175 TmfTimeRange range = new TmfTimeRange(startTS, endTS);
176
1c0de632
AM
177 StatsPerTypeRequest request = new StatsPerTypeRequest(trace, range);
178 sendAndWait(request);
179
180 Map<String, Long> stats = request.getResults();
181 return stats;
182 }
183
184 private void sendAndWait(TmfEventRequest request) {
185 trace.sendRequest(request);
186 try {
187 request.waitForCompletion();
188 } catch (InterruptedException e) {
189 e.printStackTrace();
190 }
191 }
192
193
194 /**
195 * Event request to get the total number of events
196 */
197 private class StatsTotalRequest extends TmfEventRequest {
198
199 /* Total number of events the request has found */
200 private long total;
201
202 public StatsTotalRequest(ITmfTrace trace, TmfTimeRange range) {
203 super(trace.getEventType(), range, TmfDataRequest.ALL_DATA,
204 trace.getCacheSize(), ITmfDataRequest.ExecutionType.BACKGROUND);
205 total = 0;
206 }
207
208 public long getResult() {
209 return total;
210 }
211
212 @Override
213 public void handleData(final ITmfEvent event) {
214 super.handleData(event);
215 if (event != null) {
216 if (event.getTrace() == trace) {
217 total += 1;
218 }
219 }
220 }
221 }
222
223
224 /**
225 * Event request to get the counts per event type
226 */
227 private class StatsPerTypeRequest extends TmfEventRequest {
228
229 /* Map in which the results are saved */
230 private final Map<String, Long> stats;
231
232 public StatsPerTypeRequest(ITmfTrace trace, TmfTimeRange range) {
233 super(trace.getEventType(), range, TmfDataRequest.ALL_DATA,
234 trace.getCacheSize(), ITmfDataRequest.ExecutionType.BACKGROUND);
235 this.stats = new HashMap<String, Long>();
236 }
237
238 public Map<String, Long> getResults() {
239 return stats;
240 }
241
242 @Override
243 public void handleData(final ITmfEvent event) {
244 super.handleData(event);
245 if (event != null) {
246 if (event.getTrace() == trace) {
247 processEvent(event);
248 }
249 }
250 }
251
252 private void processEvent(ITmfEvent event) {
253 String eventType = event.getType().getName();
254 if (stats.containsKey(eventType)) {
255 long curValue = stats.get(eventType);
256 stats.put(eventType, curValue + 1L);
257 } else {
258 stats.put(eventType, 1L);
259 }
260 }
261 }
262
263}
This page took 0.047399 seconds and 5 git commands to generate.