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