tmf: Mark ITmfEventRequest.handleData's parameter as NonNull
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsCache.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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 * Patrick Tasse - Initial API and implementation
11 ******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.viewers.events;
14
15 import java.util.ArrayList;
16 import java.util.Arrays;
17 import java.util.List;
18
19 import org.eclipse.core.runtime.IProgressMonitor;
20 import org.eclipse.core.runtime.IStatus;
21 import org.eclipse.core.runtime.Status;
22 import org.eclipse.core.runtime.jobs.Job;
23 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
24 import org.eclipse.linuxtools.tmf.core.component.ITmfEventProvider;
25 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
26 import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter;
27 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
28 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
29 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimeRange;
30 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
31
32 /**
33 * The generic TMF Events table events cache
34 *
35 * This can help avoid re-reading the trace when the user scrolls a window,
36 * for example.
37 *
38 * @version 1.0
39 * @author Patrick Tasse
40 */
41 public class TmfEventsCache {
42
43 /**
44 * The generic TMF Events table cached event
45 *
46 * @version 1.0
47 * @author Patrick Tasse
48 */
49 public static class CachedEvent {
50 ITmfEvent event;
51 long rank;
52
53 /**
54 * Constructor for new cached events.
55 *
56 * @param iTmfEvent
57 * The original trace event
58 * @param rank
59 * The rank of this event in the trace
60 */
61 public CachedEvent (ITmfEvent iTmfEvent, long rank) {
62 this.event = iTmfEvent;
63 this.rank = rank;
64 }
65 }
66
67 private final CachedEvent[] fCache;
68 private final int fCacheSize;
69 private int fCacheStartIndex = 0;
70 private int fCacheEndIndex = 0;
71
72 private ITmfTrace fTrace;
73 private final TmfEventsTable fTable;
74 private ITmfFilter fFilter;
75 private final List<Integer> fFilterIndex = new ArrayList<>(); // contains the event rank at each 'cache size' filtered events
76
77 /**
78 * Constructor for the event cache
79 *
80 * @param cacheSize
81 * The size of the cache, in number of events
82 * @param table
83 * The Events table this cache will cover
84 */
85 public TmfEventsCache(int cacheSize, TmfEventsTable table) {
86 fCacheSize = cacheSize;
87 fCache = new CachedEvent[cacheSize * 2]; // the cache holds two blocks of cache size
88 fTable = table;
89 }
90
91 /**
92 * Assign a new trace to this events cache. This clears the current
93 * contents.
94 *
95 * @param trace
96 * The trace to assign.
97 */
98 public void setTrace(ITmfTrace trace) {
99 fTrace = trace;
100 clear();
101 }
102
103 /**
104 * Clear the current contents of this cache.
105 */
106 public synchronized void clear() {
107 if (job != null && job.getState() != Job.NONE) {
108 job.cancel();
109 }
110 Arrays.fill(fCache, null);
111 fCacheStartIndex = 0;
112 fCacheEndIndex = 0;
113 fFilterIndex.clear();
114 }
115
116 /**
117 * Apply a filter on this event cache. This clears the current cache
118 * contents.
119 *
120 * @param filter
121 * The ITmfFilter to apply.
122 */
123 public void applyFilter(ITmfFilter filter) {
124 fFilter = filter;
125 clear();
126 }
127
128 /**
129 * Clear the current filter on this cache. This also clears the current
130 * cache contents.
131 */
132 public void clearFilter() {
133 fFilter = null;
134 clear();
135 }
136
137 /**
138 * Get an event from the cache. If the cache does not contain the event,
139 * a cache population request is triggered.
140 *
141 * @param index
142 * The index of this event in the cache
143 * @return The cached event, or 'null' if the event is not in the cache
144 */
145 public synchronized CachedEvent getEvent(int index) {
146 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
147 int i = index - fCacheStartIndex;
148 return fCache[i];
149 }
150 populateCache(index);
151 return null;
152 }
153
154 /**
155 * Peek an event in the cache. Does not trigger cache population.
156 *
157 * @param index
158 * Index of the event to peek
159 * @return The cached event, or 'null' if the event is not in the cache
160 */
161 public synchronized CachedEvent peekEvent(int index) {
162 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
163 int i = index - fCacheStartIndex;
164 return fCache[i];
165 }
166 return null;
167 }
168
169 /**
170 * Add a trace event to the cache.
171 *
172 * @param event
173 * The original trace event to be cached
174 * @param rank
175 * The rank of this event in the trace
176 * @param index
177 * The index this event will occupy in the cache
178 */
179 public synchronized void storeEvent(ITmfEvent event, long rank, int index) {
180 if (index == fCacheEndIndex) {
181 int i = index - fCacheStartIndex;
182 if (i < fCache.length) {
183 fCache[i] = new CachedEvent(event, rank);
184 fCacheEndIndex++;
185 }
186 }
187 if ((fFilter != null) && ((index % fCacheSize) == 0)) {
188 int i = index / fCacheSize;
189 fFilterIndex.add(i, Integer.valueOf((int) rank));
190 }
191 }
192
193 /**
194 * Get the cache index of an event from his rank in the trace. This will
195 * take in consideration any filter that might be applied.
196 *
197 * @param rank
198 * The rank of the event in the trace
199 * @return The position (index) this event should use once cached
200 */
201 public int getFilteredEventIndex(final long rank) {
202 int current;
203 int startRank;
204 TmfEventRequest request;
205 final ITmfFilter filter = fFilter;
206 synchronized (this) {
207 int start = 0;
208 int end = fFilterIndex.size();
209
210 if ((fCacheEndIndex - fCacheStartIndex) > 1) {
211 if (rank < fCache[0].rank) {
212 end = (fCacheStartIndex / fCacheSize) + 1;
213 } else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) {
214 start = fCacheEndIndex / fCacheSize;
215 } else {
216 for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {
217 if (fCache[i].rank >= rank) {
218 return fCacheStartIndex + i;
219 }
220 }
221 return fCacheEndIndex;
222 }
223 }
224
225 current = (start + end) / 2;
226 while (current != start) {
227 if (rank < fFilterIndex.get(current)) {
228 end = current;
229 current = (start + end) / 2;
230 } else {
231 start = current;
232 current = (start + end) / 2;
233 }
234 }
235 startRank = fFilterIndex.size() > 0 ? fFilterIndex.get(current) : 0;
236 }
237
238 final int index = current * fCacheSize;
239
240 class DataRequest extends TmfEventRequest {
241 ITmfFilter requestFilter;
242 int requestRank;
243 int requestIndex;
244
245 DataRequest(Class<? extends ITmfEvent> dataType, ITmfFilter reqFilter, int start, int nbRequested) {
246 super(dataType, TmfTimeRange.ETERNITY, start, nbRequested,
247 TmfEventRequest.ExecutionType.FOREGROUND);
248 requestFilter = reqFilter;
249 requestRank = start;
250 requestIndex = index;
251 }
252
253 @Override
254 public void handleData(ITmfEvent event) {
255 super.handleData(event);
256 if (isCancelled()) {
257 return;
258 }
259 if (requestRank >= rank) {
260 cancel();
261 return;
262 }
263 requestRank++;
264 if (requestFilter.matches(event)) {
265 requestIndex++;
266 }
267 }
268
269 public int getFilteredIndex() {
270 return requestIndex;
271 }
272 }
273
274 request = new DataRequest(ITmfEvent.class, filter, startRank, ITmfEventRequest.ALL_DATA);
275 ((ITmfEventProvider) fTrace).sendRequest(request);
276 try {
277 request.waitForCompletion();
278 return ((DataRequest) request).getFilteredIndex();
279 } catch (InterruptedException e) {
280 Activator.getDefault().logError("Filter request interrupted!", e); //$NON-NLS-1$
281 }
282 return 0;
283 }
284
285 // ------------------------------------------------------------------------
286 // Event cache population
287 // ------------------------------------------------------------------------
288
289 // The event fetching job
290 private Job job;
291 private synchronized void populateCache(final int index) {
292
293 /* Check if the current job will fetch the requested event:
294 * 1. The job must exist
295 * 2. It must be running (i.e. not completed)
296 * 3. The requested index must be within the cache range
297 *
298 * If the job meets these conditions, we simply exit.
299 * Otherwise, we create a new job but we might have to cancel
300 * an existing job for an obsolete range.
301 */
302 if (job != null) {
303 if (job.getState() != Job.NONE) {
304 if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {
305 return;
306 }
307 // The new index is out of the requested range
308 // Kill the job and start a new one
309 job.cancel();
310 }
311 }
312
313 // Populate the cache starting at the index that is one block less
314 // of cache size than the requested index. The cache will hold two
315 // consecutive blocks of cache size, centered on the requested index.
316 fCacheStartIndex = Math.max(0, index - fCacheSize);
317 fCacheEndIndex = fCacheStartIndex;
318
319 job = new Job("Fetching Events") { //$NON-NLS-1$
320 private int startIndex = fCacheStartIndex;
321 private int skipCount = 0;
322 @Override
323 protected IStatus run(final IProgressMonitor monitor) {
324
325 int nbRequested;
326 if (fFilter == null) {
327 nbRequested = fCache.length;
328 } else {
329 nbRequested = ITmfEventRequest.ALL_DATA;
330 int i = startIndex / fCacheSize;
331 if (i < fFilterIndex.size()) {
332 skipCount = startIndex - (i * fCacheSize);
333 startIndex = fFilterIndex.get(i);
334 }
335 }
336
337 TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
338 TmfTimeRange.ETERNITY,
339 startIndex,
340 nbRequested,
341 TmfEventRequest.ExecutionType.FOREGROUND) {
342 private int count = 0;
343 private long rank = startIndex;
344 @Override
345 public void handleData(ITmfEvent event) {
346 // If the job is canceled, cancel the request so waitForCompletion() will unlock
347 if (monitor.isCanceled()) {
348 cancel();
349 return;
350 }
351 super.handleData(event);
352 if (((fFilter == null) || fFilter.matches(event)) && (skipCount-- <= 0)) {
353 synchronized (TmfEventsCache.this) {
354 if (monitor.isCanceled()) {
355 return;
356 }
357 fCache[count] = new CachedEvent(event, rank);
358 count++;
359 fCacheEndIndex++;
360 }
361 if (fFilter != null) {
362 fTable.cacheUpdated(false);
363 }
364 }
365 if (count >= fCache.length) {
366 cancel();
367 } else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
368 cancel();
369 }
370 rank++;
371 }
372 };
373
374 ((ITmfEventProvider) fTrace).sendRequest(request);
375 try {
376 request.waitForCompletion();
377 } catch (InterruptedException e) {
378 Activator.getDefault().logError("Wait for completion interrupted for populateCache ", e); //$NON-NLS-1$
379 }
380
381 fTable.cacheUpdated(true);
382
383 // Flag the UI thread that the cache is ready
384 if (monitor.isCanceled()) {
385 return Status.CANCEL_STATUS;
386 }
387 return Status.OK_STATUS;
388 }
389 };
390 //job.setSystem(true);
391 job.setPriority(Job.SHORT);
392 job.schedule();
393 }
394
395 }
This page took 0.042463 seconds and 5 git commands to generate.