1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Bernd Hufmann - Add support for event collapsing
12 ******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.events
;
16 import java
.util
.ArrayList
;
17 import java
.util
.Arrays
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.core
.runtime
.jobs
.Job
;
24 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.filter
.TmfCollapseFilter
;
25 import org
.eclipse
.tracecompass
.internal
.tmf
.ui
.Activator
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.component
.ITmfEventProvider
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.request
.TmfEventRequest
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimeRange
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTrace
;
38 * The generic TMF Events table events cache
40 * This can help avoid re-reading the trace when the user scrolls a window,
43 * @author Patrick Tasse
45 public class TmfEventsCache
{
48 * The generic TMF Events table cached event.
50 * @author Patrick Tasse
52 public static class CachedEvent
implements ITmfEvent
{
56 * When {@link TmfCollapseFilter} is active then it's event reference
57 * of the first event of repeated events.
63 * When {@link TmfCollapseFilter} is active then it's event rank of the
64 * first event of repeated events.
68 * Number times event is repeated. Updated by using {@link TmfCollapseFilter}
73 * Constructor for new cached events.
76 * The original trace event
78 * The rank of this event in the trace
80 public CachedEvent (ITmfEvent iTmfEvent
, long rank
) {
81 this.event
= iTmfEvent
;
86 public <T
> T
getAdapter(Class
<T
> adapterType
) {
87 return event
.getAdapter(adapterType
);
91 public ITmfTrace
getTrace() {
92 return event
.getTrace();
96 public long getRank() {
97 return event
.getRank();
104 public String
getName() {
105 return event
.getName();
109 public ITmfTimestamp
getTimestamp() {
110 return event
.getTimestamp();
114 public ITmfEventType
getType() {
115 return event
.getType();
119 public ITmfEventField
getContent() {
120 return event
.getContent();
124 private final CachedEvent
[] fCache
;
125 private final int fCacheSize
;
126 private int fCacheStartIndex
= 0;
127 private int fCacheEndIndex
= 0;
129 private ITmfTrace fTrace
;
130 private final TmfEventsTable fTable
;
131 private ITmfFilter fFilter
;
132 private final List
<Integer
> fFilterIndex
= new ArrayList
<>(); // contains the event rank at each 'cache size' filtered events
133 private boolean fCollapseFilterEnabled
= false;
136 * Constructor for the event cache
139 * The size of the cache, in number of events
141 * The Events table this cache will cover
143 public TmfEventsCache(int cacheSize
, TmfEventsTable table
) {
144 fCacheSize
= cacheSize
;
145 fCache
= new CachedEvent
[cacheSize
* 2]; // the cache holds two blocks of cache size
150 * Assign a new trace to this events cache. This clears the current
154 * The trace to assign.
156 public void setTrace(ITmfTrace trace
) {
162 * Clear the current contents of this cache.
164 public synchronized void clear() {
165 if (job
!= null && job
.getState() != Job
.NONE
) {
168 Arrays
.fill(fCache
, null);
169 fCacheStartIndex
= 0;
171 fFilterIndex
.clear();
175 * Apply a filter on this event cache. This clears the current cache
179 * The ITmfFilter to apply.
180 * @param collapseFilterEnabled
181 * true if the collapse filter is enabled
184 public void applyFilter(ITmfFilter filter
, boolean collapseFilterEnabled
) {
186 fCollapseFilterEnabled
= collapseFilterEnabled
;
191 * Clear the current filter on this cache. This also clears the current
194 public void clearFilter() {
196 fCollapseFilterEnabled
= false;
201 * Get an event from the cache. If the cache does not contain the event,
202 * a cache population request is triggered.
205 * The index of this event in the cache
206 * @return The cached event, or 'null' if the event is not in the cache
208 public synchronized CachedEvent
getEvent(int index
) {
209 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
210 int i
= index
- fCacheStartIndex
;
213 populateCache(index
);
218 * Peek an event in the cache. Does not trigger cache population.
221 * Index of the event to peek
222 * @return The cached event, or 'null' if the event is not in the cache
224 public synchronized CachedEvent
peekEvent(int index
) {
225 if ((index
>= fCacheStartIndex
) && (index
< fCacheEndIndex
)) {
226 int i
= index
- fCacheStartIndex
;
233 * Add a trace event to the cache.
236 * The original trace event to be cached
238 * The rank of this event in the trace
240 * The index this event will occupy in the cache
242 public synchronized void storeEvent(ITmfEvent event
, long rank
, int index
) {
243 if (index
== fCacheEndIndex
) {
244 int i
= index
- fCacheStartIndex
;
245 if (i
< fCache
.length
) {
246 fCache
[i
] = new CachedEvent(event
, rank
);
250 if ((fFilter
!= null) && ((index
% fCacheSize
) == 0)) {
251 int i
= index
/ fCacheSize
;
252 fFilterIndex
.add(i
, Integer
.valueOf((int) rank
));
257 * Update event repeat count at index
260 * The index this event occupies in the cache
262 public synchronized void updateCollapsedEvent(int index
) {
263 int i
= index
- fCacheStartIndex
;
264 if (i
< fCache
.length
) {
265 fCache
[i
].repeatCount
++;
270 * Get the cache index of an event from his rank in the trace. This will
271 * take in consideration any filter that might be applied.
274 * The rank of the event in the trace
275 * @return The position (index) this event should use once cached
277 public int getFilteredEventIndex(final long rank
) {
280 TmfEventRequest request
;
281 final ITmfFilter filter
= fFilter
;
282 synchronized (this) {
284 int end
= fFilterIndex
.size();
286 if ((fCacheEndIndex
- fCacheStartIndex
) > 1) {
287 if (rank
< fCache
[0].rank
) {
288 end
= (fCacheStartIndex
/ fCacheSize
) + 1;
289 } else if (rank
> fCache
[fCacheEndIndex
- fCacheStartIndex
- 1].rank
) {
290 start
= fCacheEndIndex
/ fCacheSize
;
292 for (int i
= 0; i
< (fCacheEndIndex
- fCacheStartIndex
); i
++) {
293 if (fCache
[i
].rank
>= rank
) {
294 return fCacheStartIndex
+ i
;
297 return fCacheEndIndex
;
301 current
= (start
+ end
) / 2;
302 while (current
!= start
) {
303 if (rank
< fFilterIndex
.get(current
)) {
305 current
= (start
+ end
) / 2;
308 current
= (start
+ end
) / 2;
311 startRank
= fFilterIndex
.size() > 0 ? fFilterIndex
.get(current
) : 0;
314 final int index
= current
* fCacheSize
;
316 class DataRequest
extends TmfEventRequest
{
317 ITmfFilter requestFilter
;
318 TmfCollapseFilter requestCollapsedFilter
;
322 DataRequest(Class
<?
extends ITmfEvent
> dataType
, ITmfFilter reqFilter
, int start
, int nbRequested
) {
323 super(dataType
, TmfTimeRange
.ETERNITY
, start
, nbRequested
,
324 TmfEventRequest
.ExecutionType
.FOREGROUND
);
325 requestFilter
= reqFilter
;
327 requestIndex
= index
;
328 requestCollapsedFilter
= fCollapseFilterEnabled ?
new TmfCollapseFilter() : null;
332 public void handleData(ITmfEvent event
) {
333 super.handleData(event
);
337 if (requestRank
>= rank
) {
342 if (requestFilter
.matches(event
)) {
343 if (requestCollapsedFilter
== null || requestCollapsedFilter
.matches(event
)) {
349 public int getFilteredIndex() {
354 request
= new DataRequest(ITmfEvent
.class, filter
, startRank
, ITmfEventRequest
.ALL_DATA
);
355 ((ITmfEventProvider
) fTrace
).sendRequest(request
);
357 request
.waitForCompletion();
358 return ((DataRequest
) request
).getFilteredIndex();
359 } catch (InterruptedException e
) {
360 Activator
.getDefault().logError("Filter request interrupted!", e
); //$NON-NLS-1$
365 // ------------------------------------------------------------------------
366 // Event cache population
367 // ------------------------------------------------------------------------
369 // The event fetching job
371 private synchronized void populateCache(final int index
) {
373 /* Check if the current job will fetch the requested event:
374 * 1. The job must exist
375 * 2. It must be running (i.e. not completed)
376 * 3. The requested index must be within the cache range
378 * If the job meets these conditions, we simply exit.
379 * Otherwise, we create a new job but we might have to cancel
380 * an existing job for an obsolete range.
383 if (job
.getState() != Job
.NONE
) {
384 if ((index
>= fCacheStartIndex
) && (index
< (fCacheStartIndex
+ fCache
.length
))) {
387 // The new index is out of the requested range
388 // Kill the job and start a new one
393 // Populate the cache starting at the index that is one block less
394 // of cache size than the requested index. The cache will hold two
395 // consecutive blocks of cache size, centered on the requested index.
396 fCacheStartIndex
= Math
.max(0, index
- fCacheSize
);
397 fCacheEndIndex
= fCacheStartIndex
;
399 job
= new Job("Fetching Events") { //$NON-NLS-1$
400 private int startIndex
= fCacheStartIndex
;
401 private int skipCount
= 0;
403 protected IStatus
run(final IProgressMonitor monitor
) {
406 if (fFilter
== null) {
407 nbRequested
= fCache
.length
;
409 nbRequested
= ITmfEventRequest
.ALL_DATA
;
410 int i
= startIndex
/ fCacheSize
;
411 if (i
< fFilterIndex
.size()) {
412 skipCount
= startIndex
- (i
* fCacheSize
);
413 startIndex
= fFilterIndex
.get(i
);
417 TmfEventRequest request
= new TmfEventRequest(ITmfEvent
.class,
418 TmfTimeRange
.ETERNITY
,
421 TmfEventRequest
.ExecutionType
.FOREGROUND
) {
422 private int count
= 0;
423 private long rank
= startIndex
;
424 private TmfCollapseFilter collapseFilter
= fCollapseFilterEnabled ?
new TmfCollapseFilter() : null;
426 public void handleData(ITmfEvent event
) {
427 // If the job is canceled, cancel the request so waitForCompletion() will unlock
428 if (monitor
.isCanceled()) {
432 super.handleData(event
);
433 if ((fFilter
== null) || fFilter
.matches(event
)) {
434 if (collapseFilter
== null || collapseFilter
.matches(event
)) {
435 if (skipCount
-- <= 0) {
436 synchronized (TmfEventsCache
.this) {
437 if (monitor
.isCanceled()) {
440 fCache
[count
] = new CachedEvent(event
, rank
);
444 if (fFilter
!= null) {
445 fTable
.cacheUpdated(false);
448 } else if ((count
> 0) && (skipCount
<= 0)) {
449 fCache
[count
- 1].repeatCount
++;
452 if (count
>= fCache
.length
) {
454 } else if ((fFilter
!= null) && (count
>= (fTable
.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
461 ((ITmfEventProvider
) fTrace
).sendRequest(request
);
463 request
.waitForCompletion();
464 } catch (InterruptedException e
) {
465 Activator
.getDefault().logError("Wait for completion interrupted for populateCache ", e
); //$NON-NLS-1$
468 fTable
.cacheUpdated(true);
470 // Flag the UI thread that the cache is ready
471 if (monitor
.isCanceled()) {
472 return Status
.CANCEL_STATUS
;
474 return Status
.OK_STATUS
;
477 //job.setSystem(true);
478 job
.setPriority(Job
.SHORT
);