1 /*******************************************************************************
2 * Copyright (c) 2011 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
.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
;
31 * The generic TMF Events table events cache
33 * This can help avoid re-reading the trace when the user scrolls a window,
37 * @author Patrick Tasse
39 public class TmfEventsCache
{
42 * The generic TMF Events table cached event
45 * @author Patrick Tasse
47 public static class CachedEvent
{
52 * Constructor for new cached events.
55 * The original trace event
57 * The rank of this event in the trace
59 public CachedEvent (ITmfEvent iTmfEvent
, long rank
) {
60 this.event
= iTmfEvent
;
65 private final CachedEvent
[] fCache
;
66 private int fCacheStartIndex
= 0;
67 private int fCacheEndIndex
= 0;
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
75 * Constructor for the event cache
78 * The size of the cache, in number of events
80 * The Events table this cache will cover
82 public TmfEventsCache(int cacheSize
, TmfEventsTable table
) {
83 fCache
= new CachedEvent
[cacheSize
];
88 * Assign a new trace to this events cache. This clears the current
92 * The trace to assign.
94 public void setTrace(ITmfTrace trace
) {
100 * Clear the current contents of this cache.
102 public synchronized void clear() {
103 if (job
!= null && job
.getState() != Job
.NONE
) {
106 Arrays
.fill(fCache
, null);
107 fCacheStartIndex
= 0;
109 fFilterIndex
.clear();
113 * Apply a filter on this event cache. This clears the current cache
117 * The ITmfFilter to apply.
119 public void applyFilter(ITmfFilter filter
) {
125 * Clear the current filter on this cache. This also clears the current
128 public void clearFilter() {
134 * Get an event from the cache. This will remove the event from the cache.
136 * FIXME this does not currently remove the event!
139 * The index of this event in the cache
140 * @return The cached event, or 'null' if there is no event at that index
142 public synchronized CachedEvent
getEvent(int index
) {
143 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
144 int i
= index
- fCacheStartIndex
;
147 populateCache(index
);
152 * Read an event, but without removing it from the cache.
155 * Index of the event to peek
156 * @return A reference to the event, or 'null' if there is no event at this
159 public synchronized CachedEvent
peekEvent(int index
) {
160 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
161 int i
= index
- fCacheStartIndex
;
168 * Add a trace event to the cache.
171 * The original trace event to be cached
173 * The rank of this event in the trace
175 * The index this event will occupy in the cache
177 public synchronized void storeEvent(ITmfEvent event
, long rank
, int index
) {
178 if (index
== fCacheEndIndex
) {
179 int i
= index
- fCacheStartIndex
;
180 if (i
< fCache
.length
) {
181 fCache
[i
] = new CachedEvent(event
.clone(), rank
);
185 if ((fFilter
!= null) && ((index
% fCache
.length
) == 0)) {
186 int i
= index
/ fCache
.length
;
187 fFilterIndex
.add(i
, Integer
.valueOf((int) rank
));
192 * Get the cache index of an event from his rank in the trace. This will
193 * take in consideration any filter that might be applied.
196 * The rank of the event in the trace
197 * @return The position (index) this event should use once cached
199 public int getFilteredEventIndex(final long rank
) {
202 TmfDataRequest request
;
203 final ITmfFilter filter
= fFilter
;
204 synchronized (this) {
206 int end
= fFilterIndex
.size();
208 if ((fCacheEndIndex
- fCacheStartIndex
) > 1) {
209 if (rank
< fCache
[0].rank
) {
210 end
= (fCacheStartIndex
/ fCache
.length
) + 1;
211 } else if (rank
> fCache
[fCacheEndIndex
- fCacheStartIndex
- 1].rank
) {
212 start
= fCacheEndIndex
/ fCache
.length
;
214 for (int i
= 0; i
< (fCacheEndIndex
- fCacheStartIndex
); i
++) {
215 if (fCache
[i
].rank
>= rank
) {
216 return fCacheStartIndex
+ i
;
219 return fCacheEndIndex
;
223 current
= (start
+ end
) / 2;
224 while (current
!= start
) {
225 if (rank
< fFilterIndex
.get(current
)) {
227 current
= (start
+ end
) / 2;
230 current
= (start
+ end
) / 2;
233 startRank
= fFilterIndex
.size() > 0 ? fFilterIndex
.get(current
) : 0;
236 final int index
= current
* fCache
.length
;
238 class DataRequest
extends TmfDataRequest
{
239 ITmfFilter requestFilter
;
243 DataRequest(Class
<?
extends ITmfEvent
> dataType
, ITmfFilter reqFilter
, int start
, int nbRequested
) {
244 super(dataType
, start
, nbRequested
);
245 requestFilter
= reqFilter
;
247 requestIndex
= index
;
251 public void handleData(ITmfEvent event
) {
252 super.handleData(event
);
256 if (requestRank
>= rank
) {
261 if (requestFilter
.matches(event
)) {
266 public int getFilteredIndex() {
271 request
= new DataRequest(ITmfEvent
.class, filter
, startRank
, TmfDataRequest
.ALL_DATA
);
272 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
274 request
.waitForCompletion();
275 return ((DataRequest
) request
).getFilteredIndex();
276 } catch (InterruptedException e
) {
277 Activator
.getDefault().logError("Filter request interrupted!", e
); //$NON-NLS-1$
282 // ------------------------------------------------------------------------
283 // Event cache population
284 // ------------------------------------------------------------------------
286 // The event fetching job
288 private synchronized void populateCache(final int index
) {
290 /* Check if the current job will fetch the requested event:
291 * 1. The job must exist
292 * 2. It must be running (i.e. not completed)
293 * 3. The requested index must be within the cache range
295 * If the job meets these conditions, we simply exit.
296 * Otherwise, we create a new job but we might have to cancel
297 * an existing job for an obsolete range.
300 if (job
.getState() != Job
.NONE
) {
301 if ((index
>= fCacheStartIndex
) && (index
< (fCacheStartIndex
+ fCache
.length
))) {
304 // The new index is out of the requested range
305 // Kill the job and start a new one
310 fCacheStartIndex
= index
;
311 fCacheEndIndex
= index
;
313 job
= new Job("Fetching Events") { //$NON-NLS-1$
314 private int startIndex
= index
;
315 private int skipCount
= 0;
317 protected IStatus
run(final IProgressMonitor monitor
) {
320 if (fFilter
== null) {
321 nbRequested
= fCache
.length
;
323 nbRequested
= TmfDataRequest
.ALL_DATA
;
324 int i
= index
/ fCache
.length
;
325 if (i
< fFilterIndex
.size()) {
326 startIndex
= fFilterIndex
.get(i
);
327 skipCount
= index
- (i
* fCache
.length
);
331 TmfDataRequest request
= new TmfDataRequest(ITmfEvent
.class, startIndex
, nbRequested
) {
332 private int count
= 0;
333 private long rank
= startIndex
;
335 public void handleData(ITmfEvent event
) {
336 // If the job is canceled, cancel the request so waitForCompletion() will unlock
337 if (monitor
.isCanceled()) {
341 super.handleData(event
);
343 if (((fFilter
== null) || fFilter
.matches(event
)) && (skipCount
-- <= 0)) {
344 synchronized (TmfEventsCache
.this) {
345 if (monitor
.isCanceled()) {
348 fCache
[count
] = new CachedEvent(event
.clone(), rank
);
352 if (fFilter
!= null) {
353 fTable
.cacheUpdated(false);
357 if (count
>= fCache
.length
) {
359 } else if ((fFilter
!= null) && (count
>= (fTable
.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
366 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
368 request
.waitForCompletion();
369 } catch (InterruptedException e
) {
370 Activator
.getDefault().logError("Wait for completion interrupted for populateCache ", e
); //$NON-NLS-1$
373 fTable
.cacheUpdated(true);
375 // Flag the UI thread that the cache is ready
376 if (monitor
.isCanceled()) {
377 return Status
.CANCEL_STATUS
;
379 return Status
.OK_STATUS
;
382 //job.setSystem(true);
383 job
.setPriority(Job
.SHORT
);
This page took 0.039961 seconds and 5 git commands to generate.