2ecb6b3ff240f55c36d07849ac61fd96bfb5d610
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 Ericsson
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
10 * Patrick Tasse - Initial API and implementation
11 ******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.ui
.viewers
.events
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.List
;
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
;
33 * The generic TMF Events table events cache
35 * This can help avoid re-reading the trace when the user scrolls a window,
39 * @author Patrick Tasse
41 public class TmfEventsCache
{
44 * The generic TMF Events table cached event
47 * @author Patrick Tasse
49 public static class CachedEvent
{
54 * Constructor for new cached events.
57 * The original trace event
59 * The rank of this event in the trace
61 public CachedEvent (ITmfEvent iTmfEvent
, long rank
) {
62 this.event
= iTmfEvent
;
67 private final CachedEvent
[] fCache
;
68 private final int fCacheSize
;
69 private int fCacheStartIndex
= 0;
70 private int fCacheEndIndex
= 0;
72 private ITmfTrace fTrace
;
73 private final TmfEventsTable fTable
;
74 private ITmfFilter fFilter
;
75 private final List
<Integer
> fFilterIndex
= new ArrayList
<Integer
>(); // contains the event rank at each 'cache size' filtered events
78 * Constructor for the event cache
81 * The size of the cache, in number of events
83 * The Events table this cache will cover
85 public TmfEventsCache(int cacheSize
, TmfEventsTable table
) {
86 fCacheSize
= cacheSize
;
87 fCache
= new CachedEvent
[cacheSize
* 2]; // the cache holds two blocks of cache size
92 * Assign a new trace to this events cache. This clears the current
96 * The trace to assign.
98 public void setTrace(ITmfTrace trace
) {
104 * Clear the current contents of this cache.
106 public synchronized void clear() {
107 if (job
!= null && job
.getState() != Job
.NONE
) {
110 Arrays
.fill(fCache
, null);
111 fCacheStartIndex
= 0;
113 fFilterIndex
.clear();
117 * Apply a filter on this event cache. This clears the current cache
121 * The ITmfFilter to apply.
123 public void applyFilter(ITmfFilter filter
) {
129 * Clear the current filter on this cache. This also clears the current
132 public void clearFilter() {
138 * Get an event from the cache. If the cache does not contain the event,
139 * a cache population request is triggered.
142 * The index of this event in the cache
143 * @return The cached event, or 'null' if the event is not in the cache
145 public synchronized CachedEvent
getEvent(int index
) {
146 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
147 int i
= index
- fCacheStartIndex
;
150 populateCache(index
);
155 * Peek an event in the cache. Does not trigger cache population.
158 * Index of the event to peek
159 * @return The cached event, or 'null' if the event is not in the cache
161 public synchronized CachedEvent
peekEvent(int index
) {
162 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
163 int i
= index
- fCacheStartIndex
;
170 * Add a trace event to the cache.
173 * The original trace event to be cached
175 * The rank of this event in the trace
177 * The index this event will occupy in the cache
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
);
187 if ((fFilter
!= null) && ((index
% fCacheSize
) == 0)) {
188 int i
= index
/ fCacheSize
;
189 fFilterIndex
.add(i
, Integer
.valueOf((int) rank
));
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.
198 * The rank of the event in the trace
199 * @return The position (index) this event should use once cached
201 public int getFilteredEventIndex(final long rank
) {
204 TmfEventRequest request
;
205 final ITmfFilter filter
= fFilter
;
206 synchronized (this) {
208 int end
= fFilterIndex
.size();
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
;
216 for (int i
= 0; i
< (fCacheEndIndex
- fCacheStartIndex
); i
++) {
217 if (fCache
[i
].rank
>= rank
) {
218 return fCacheStartIndex
+ i
;
221 return fCacheEndIndex
;
225 current
= (start
+ end
) / 2;
226 while (current
!= start
) {
227 if (rank
< fFilterIndex
.get(current
)) {
229 current
= (start
+ end
) / 2;
232 current
= (start
+ end
) / 2;
235 startRank
= fFilterIndex
.size() > 0 ? fFilterIndex
.get(current
) : 0;
238 final int index
= current
* fCacheSize
;
240 class DataRequest
extends TmfEventRequest
{
241 ITmfFilter requestFilter
;
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
;
250 requestIndex
= index
;
254 public void handleData(ITmfEvent event
) {
255 super.handleData(event
);
259 if (requestRank
>= rank
) {
264 if (requestFilter
.matches(event
)) {
269 public int getFilteredIndex() {
274 request
= new DataRequest(ITmfEvent
.class, filter
, startRank
, ITmfEventRequest
.ALL_DATA
);
275 ((ITmfEventProvider
) fTrace
).sendRequest(request
);
277 request
.waitForCompletion();
278 return ((DataRequest
) request
).getFilteredIndex();
279 } catch (InterruptedException e
) {
280 Activator
.getDefault().logError("Filter request interrupted!", e
); //$NON-NLS-1$
285 // ------------------------------------------------------------------------
286 // Event cache population
287 // ------------------------------------------------------------------------
289 // The event fetching job
291 private synchronized void populateCache(final int index
) {
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
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.
303 if (job
.getState() != Job
.NONE
) {
304 if ((index
>= fCacheStartIndex
) && (index
< (fCacheStartIndex
+ fCache
.length
))) {
307 // The new index is out of the requested range
308 // Kill the job and start a new one
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
;
319 job
= new Job("Fetching Events") { //$NON-NLS-1$
320 private int startIndex
= fCacheStartIndex
;
321 private int skipCount
= 0;
323 protected IStatus
run(final IProgressMonitor monitor
) {
326 if (fFilter
== null) {
327 nbRequested
= fCache
.length
;
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
);
337 TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
338 TmfTimeRange
.ETERNITY
,
341 TmfEventRequest
.ExecutionType
.FOREGROUND
) {
342 private int count
= 0;
343 private long rank
= startIndex
;
345 public void handleData(ITmfEvent event
) {
346 // If the job is canceled, cancel the request so waitForCompletion() will unlock
347 if (monitor
.isCanceled()) {
351 super.handleData(event
);
353 if (((fFilter
== null) || fFilter
.matches(event
)) && (skipCount
-- <= 0)) {
354 synchronized (TmfEventsCache
.this) {
355 if (monitor
.isCanceled()) {
358 fCache
[count
] = new CachedEvent(event
, rank
);
362 if (fFilter
!= null) {
363 fTable
.cacheUpdated(false);
367 if (count
>= fCache
.length
) {
369 } else if ((fFilter
!= null) && (count
>= (fTable
.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
376 ((ITmfEventProvider
) fTrace
).sendRequest(request
);
378 request
.waitForCompletion();
379 } catch (InterruptedException e
) {
380 Activator
.getDefault().logError("Wait for completion interrupted for populateCache ", e
); //$NON-NLS-1$
383 fTable
.cacheUpdated(true);
385 // Flag the UI thread that the cache is ready
386 if (monitor
.isCanceled()) {
387 return Status
.CANCEL_STATUS
;
389 return Status
.OK_STATUS
;
392 //job.setSystem(true);
393 job
.setPriority(Job
.SHORT
);
This page took 0.039481 seconds and 4 git commands to generate.