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