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.List;
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;
30
* The generic TMF Events table events cache
32
* This can help avoid re-reading the trace when the user scrolls a window,
36
* @author Patrick Tasse
38
public class TmfEventsCache {
41
* The generic TMF Events table cached event
44
* @author Patrick Tasse
46
public static class CachedEvent {
51
* Constructor for new cached events.
54
* The original trace event
56
* The rank of this event in the trace
58
public CachedEvent (ITmfEvent iTmfEvent, long rank) {
59
this.event = iTmfEvent;
64
private final CachedEvent[] fCache;
65
private int fCacheStartIndex = 0;
66
private int fCacheEndIndex = 0;
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
74
* Constructor for the event cache
77
* The size of the cache, in number of events
79
* The Events table this cache will cover
81
public TmfEventsCache(int cacheSize, TmfEventsTable table) {
82
fCache = new CachedEvent[cacheSize];
87
* Assign a new trace to this events cache. This clears the current
91
* The trace to assign.
93
public void setTrace(ITmfTrace<?> trace) {
99
* Clear the current contents of this cache.
101
public synchronized void clear() {
102
fCacheStartIndex = 0;
104
fFilterIndex.clear();
108
* Apply a filter on this event cache. This clears the current cache
112
* The ITmfFilter to apply.
114
public void applyFilter(ITmfFilter filter) {
120
* Clear the current filter on this cache. This also clears the current
123
public void clearFilter() {
129
* Get an event from the cache. This will remove the event from the cache.
131
* FIXME this does not currently remove the event!
134
* The index of this event in the cache
135
* @return The cached event, or 'null' if there is no event at that index
137
public synchronized CachedEvent getEvent(int index) {
138
if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
139
int i = index - fCacheStartIndex;
142
populateCache(index);
147
* Read an event, but without removing it from the cache.
150
* Index of the event to peek
151
* @return A reference to the event, or 'null' if there is no event at this
154
public synchronized CachedEvent peekEvent(int index) {
155
if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
156
int i = index - fCacheStartIndex;
163
* Add a trace event to the cache.
166
* The original trace event to be cached
168
* The rank of this event in the trace
170
* The index this event will occupy in the cache
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);
180
if ((fFilter != null) && ((index % fCache.length) == 0)) {
181
int i = index / fCache.length;
182
fFilterIndex.add(i, Integer.valueOf((int) rank));
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.
191
* The rank of the event in the trace
192
* @return The position (index) this event should use once cached
194
@SuppressWarnings("unchecked")
195
public int getFilteredEventIndex(final long rank) {
198
TmfDataRequest<ITmfEvent> request;
199
final ITmfFilter filter = fFilter;
200
synchronized (this) {
202
int end = fFilterIndex.size();
204
if ((fCacheEndIndex - fCacheStartIndex) > 1) {
205
if (rank < fCache[0].rank) {
206
end = (fCacheStartIndex / fCache.length) + 1;
207
} else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) {
208
start = fCacheEndIndex / fCache.length;
210
for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {
211
if (fCache[i].rank >= rank) {
212
return fCacheStartIndex + i;
215
return fCacheEndIndex;
219
current = (start + end) / 2;
220
while (current != start) {
221
if (rank < fFilterIndex.get(current)) {
223
current = (start + end) / 2;
226
current = (start + end) / 2;
229
startRank = fFilterIndex.size() > 0 ? fFilterIndex.get(current) : 0;
232
final int index = current * fCache.length;
234
class DataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
239
DataRequest(Class<T> dataType, ITmfFilter filter, int start, int nbRequested) {
240
super(dataType, start, nbRequested);
247
public void handleData(T event) {
248
super.handleData(event);
257
if (fFilter.matches(event)) {
262
public int getFilteredIndex() {
267
request = new DataRequest<ITmfEvent>(ITmfEvent.class, filter, startRank, TmfDataRequest.ALL_DATA);
268
((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);
270
request.waitForCompletion();
271
return ((DataRequest<ITmfEvent>) request).getFilteredIndex();
272
} catch (InterruptedException e) {
273
Activator.getDefault().logError("Filter request interrupted!", e); //$NON-NLS-1$
278
// ------------------------------------------------------------------------
279
// Event cache population
280
// ------------------------------------------------------------------------
282
// The event fetching job
284
private synchronized void populateCache(final int index) {
286
/* Check if the current job will fetch the requested event:
287
* 1. The job must exist
288
* 2. It must be running (i.e. not completed)
289
* 3. The requested index must be within the cache range
291
* If the job meets these conditions, we simply exit.
292
* Otherwise, we create a new job but we might have to cancel
293
* an existing job for an obsolete range.
296
if (job.getState() != Job.NONE) {
297
if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {
300
// The new index is out of the requested range
301
// Kill the job and start a new one
306
fCacheStartIndex = index;
307
fCacheEndIndex = index;
309
job = new Job("Fetching Events") { //$NON-NLS-1$
310
private int startIndex = index;
311
private int skipCount = 0;
313
@SuppressWarnings("unchecked")
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<ITmfEvent> request = new TmfDataRequest<ITmfEvent>(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
fCache[count] = new CachedEvent(event.clone(), rank);
346
if (fFilter != null) {
347
fTable.cacheUpdated(false);
351
if (count >= fCache.length) {
353
} else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
360
((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);
362
request.waitForCompletion();
363
} catch (InterruptedException e) {
364
Activator.getDefault().logError("Wait for completion interrupted for populateCache ", e); //$NON-NLS-1$
367
fTable.cacheUpdated(true);
369
// Flag the UI thread that the cache is ready
370
if (monitor.isCanceled()) {
371
return Status.CANCEL_STATUS;
373
return Status.OK_STATUS;
376
//job.setSystem(true);
377
job.setPriority(Job.SHORT);
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.List;
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;
30
* The generic TMF Events table events cache
32
* This can help avoid re-reading the trace when the user scrolls a window,
36
* @author Patrick Tasse
38
public class TmfEventsCache {
41
* The generic TMF Events table cached event
44
* @author Patrick Tasse
46
public static class CachedEvent {
51
* Constructor for new cached events.
54
* The original trace event
56
* The rank of this event in the trace
58
public CachedEvent (ITmfEvent iTmfEvent, long rank) {
59
this.event = iTmfEvent;
64
private final CachedEvent[] fCache;
65
private int fCacheStartIndex = 0;
66
private int fCacheEndIndex = 0;
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
74
* Constructor for the event cache
77
* The size of the cache, in number of events
79
* The Events table this cache will cover
81
public TmfEventsCache(int cacheSize, TmfEventsTable table) {
82
fCache = new CachedEvent[cacheSize];
87
* Assign a new trace to this events cache. This clears the current
91
* The trace to assign.
93
public void setTrace(ITmfTrace<?> trace) {
99
* Clear the current contents of this cache.
101
public synchronized void clear() {
102
fCacheStartIndex = 0;
104
fFilterIndex.clear();
108
* Apply a filter on this event cache. This clears the current cache
112
* The ITmfFilter to apply.
114
public void applyFilter(ITmfFilter filter) {
120
* Clear the current filter on this cache. This also clears the current
123
public void clearFilter() {
129
* Get an event from the cache. This will remove the event from the cache.
131
* FIXME this does not currently remove the event!
134
* The index of this event in the cache
135
* @return The cached event, or 'null' if there is no event at that index
137
public synchronized CachedEvent getEvent(int index) {
138
if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
139
int i = index - fCacheStartIndex;
142
populateCache(index);
147
* Read an event, but without removing it from the cache.
150
* Index of the event to peek
151
* @return A reference to the event, or 'null' if there is no event at this
154
public synchronized CachedEvent peekEvent(int index) {
155
if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
156
int i = index - fCacheStartIndex;
163
* Add a trace event to the cache.
166
* The original trace event to be cached
168
* The rank of this event in the trace
170
* The index this event will occupy in the cache
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);
180
if ((fFilter != null) && ((index % fCache.length) == 0)) {
181
int i = index / fCache.length;
182
fFilterIndex.add(i, Integer.valueOf((int) rank));
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.
191
* The rank of the event in the trace
192
* @return The position (index) this event should use once cached
194
@SuppressWarnings("unchecked")
195
public int getFilteredEventIndex(final long rank) {
198
TmfDataRequest<ITmfEvent> request;
199
final ITmfFilter filter = fFilter;
200
synchronized (this) {
202
int end = fFilterIndex.size();
204
if ((fCacheEndIndex - fCacheStartIndex) > 1) {
205
if (rank < fCache[0].rank) {
206
end = (fCacheStartIndex / fCache.length) + 1;
207
} else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) {
208
start = fCacheEndIndex / fCache.length;
210
for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {
211
if (fCache[i].rank >= rank) {
212
return fCacheStartIndex + i;
215
return fCacheEndIndex;
219
current = (start + end) / 2;
220
while (current != start) {
221
if (rank < fFilterIndex.get(current)) {
223
current = (start + end) / 2;
226
current = (start + end) / 2;
229
startRank = fFilterIndex.size() > 0 ? fFilterIndex.get(current) : 0;
232
final int index = current * fCache.length;
234
class DataRequest<T extends ITmfEvent> extends TmfDataRequest<T> {
239
DataRequest(Class<T> dataType, ITmfFilter filter, int start, int nbRequested) {
240
super(dataType, start, nbRequested);
247
public void handleData(T event) {
248
super.handleData(event);
257
if (fFilter.matches(event)) {
262
public int getFilteredIndex() {
267
request = new DataRequest<ITmfEvent>(ITmfEvent.class, filter, startRank, TmfDataRequest.ALL_DATA);
268
((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);
270
request.waitForCompletion();
271
return ((DataRequest<ITmfEvent>) request).getFilteredIndex();
272
} catch (InterruptedException e) {
273
Activator.getDefault().logError("Filter request interrupted!", e); //$NON-NLS-1$
278
// ------------------------------------------------------------------------
279
// Event cache population
280
// ------------------------------------------------------------------------
282
// The event fetching job
284
private synchronized void populateCache(final int index) {
286
/* Check if the current job will fetch the requested event:
287
* 1. The job must exist
288
* 2. It must be running (i.e. not completed)
289
* 3. The requested index must be within the cache range
291
* If the job meets these conditions, we simply exit.
292
* Otherwise, we create a new job but we might have to cancel
293
* an existing job for an obsolete range.
296
if (job.getState() != Job.NONE) {
297
if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {
300
// The new index is out of the requested range
301
// Kill the job and start a new one
306
fCacheStartIndex = index;
307
fCacheEndIndex = index;
309
job = new Job("Fetching Events") { //$NON-NLS-1$
310
private int startIndex = index;
311
private int skipCount = 0;
313
@SuppressWarnings("unchecked")
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<ITmfEvent> request = new TmfDataRequest<ITmfEvent>(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<ITmfEvent>) 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);