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 Arrays
.fill(fCache
, null);
104 fCacheStartIndex
= 0;
106 fFilterIndex
.clear();
110 * Apply a filter on this event cache. This clears the current cache
114 * The ITmfFilter to apply.
116 public void applyFilter(ITmfFilter filter
) {
122 * Clear the current filter on this cache. This also clears the current
125 public void clearFilter() {
131 * Get an event from the cache. This will remove the event from the cache.
133 * FIXME this does not currently remove the event!
136 * The index of this event in the cache
137 * @return The cached event, or 'null' if there is no event at that index
139 public synchronized CachedEvent
getEvent(int index
) {
140 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
141 int i
= index
- fCacheStartIndex
;
144 populateCache(index
);
149 * Read an event, but without removing it from the cache.
152 * Index of the event to peek
153 * @return A reference to the event, or 'null' if there is no event at this
156 public synchronized CachedEvent
peekEvent(int index
) {
157 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
158 int i
= index
- fCacheStartIndex
;
165 * Add a trace event to the cache.
168 * The original trace event to be cached
170 * The rank of this event in the trace
172 * The index this event will occupy in the cache
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
);
182 if ((fFilter
!= null) && ((index
% fCache
.length
) == 0)) {
183 int i
= index
/ fCache
.length
;
184 fFilterIndex
.add(i
, Integer
.valueOf((int) rank
));
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.
193 * The rank of the event in the trace
194 * @return The position (index) this event should use once cached
196 public int getFilteredEventIndex(final long rank
) {
199 TmfDataRequest request
;
200 final ITmfFilter filter
= fFilter
;
201 synchronized (this) {
203 int end
= fFilterIndex
.size();
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
;
211 for (int i
= 0; i
< (fCacheEndIndex
- fCacheStartIndex
); i
++) {
212 if (fCache
[i
].rank
>= rank
) {
213 return fCacheStartIndex
+ i
;
216 return fCacheEndIndex
;
220 current
= (start
+ end
) / 2;
221 while (current
!= start
) {
222 if (rank
< fFilterIndex
.get(current
)) {
224 current
= (start
+ end
) / 2;
227 current
= (start
+ end
) / 2;
230 startRank
= fFilterIndex
.size() > 0 ? fFilterIndex
.get(current
) : 0;
233 final int index
= current
* fCache
.length
;
235 class DataRequest
extends TmfDataRequest
{
240 DataRequest(Class
<?
extends ITmfEvent
> dataType
, ITmfFilter filter
, int start
, int nbRequested
) {
241 super(dataType
, start
, nbRequested
);
248 public void handleData(ITmfEvent event
) {
249 super.handleData(event
);
258 if (fFilter
.matches(event
)) {
263 public int getFilteredIndex() {
268 request
= new DataRequest(ITmfEvent
.class, filter
, startRank
, TmfDataRequest
.ALL_DATA
);
269 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
271 request
.waitForCompletion();
272 return ((DataRequest
) request
).getFilteredIndex();
273 } catch (InterruptedException e
) {
274 Activator
.getDefault().logError("Filter request interrupted!", e
); //$NON-NLS-1$
279 // ------------------------------------------------------------------------
280 // Event cache population
281 // ------------------------------------------------------------------------
283 // The event fetching job
285 private synchronized void populateCache(final int index
) {
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
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.
297 if (job
.getState() != Job
.NONE
) {
298 if ((index
>= fCacheStartIndex
) && (index
< (fCacheStartIndex
+ fCache
.length
))) {
301 // The new index is out of the requested range
302 // Kill the job and start a new one
307 fCacheStartIndex
= index
;
308 fCacheEndIndex
= index
;
310 job
= new Job("Fetching Events") { //$NON-NLS-1$
311 private int startIndex
= index
;
312 private int skipCount
= 0;
314 protected IStatus
run(final IProgressMonitor monitor
) {
317 if (fFilter
== null) {
318 nbRequested
= fCache
.length
;
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
);
328 TmfDataRequest request
= new TmfDataRequest(ITmfEvent
.class, startIndex
, nbRequested
) {
329 private int count
= 0;
330 private long rank
= startIndex
;
332 public void handleData(ITmfEvent event
) {
333 // If the job is canceled, cancel the request so waitForCompletion() will unlock
334 if (monitor
.isCanceled()) {
338 super.handleData(event
);
340 if (((fFilter
== null) || fFilter
.matches(event
)) && (skipCount
-- <= 0)) {
341 synchronized (TmfEventsCache
.this) {
342 if (monitor
.isCanceled()) {
345 fCache
[count
] = new CachedEvent(event
.clone(), rank
);
349 if (fFilter
!= null) {
350 fTable
.cacheUpdated(false);
354 if (count
>= fCache
.length
) {
356 } else if ((fFilter
!= null) && (count
>= (fTable
.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
363 ((ITmfDataProvider
) fTrace
).sendRequest(request
);
365 request
.waitForCompletion();
366 } catch (InterruptedException e
) {
367 Activator
.getDefault().logError("Wait for completion interrupted for populateCache ", e
); //$NON-NLS-1$
370 fTable
.cacheUpdated(true);
372 // Flag the UI thread that the cache is ready
373 if (monitor
.isCanceled()) {
374 return Status
.CANCEL_STATUS
;
376 return Status
.OK_STATUS
;
379 //job.setSystem(true);
380 job
.setPriority(Job
.SHORT
);
This page took 0.058818 seconds and 6 git commands to generate.