tmf: Update event properties to use aspects
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / events / TmfEventsCache.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Patrick Tasse - Initial API and implementation
11 * Bernd Hufmann - Add support for event collapsing
12 ******************************************************************************/
13
14 package org.eclipse.tracecompass.tmf.ui.viewers.events;
15
16 import java.util.ArrayList;
17 import java.util.Arrays;
18 import java.util.List;
19
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;
36
37 /**
38 * The generic TMF Events table events cache
39 *
40 * This can help avoid re-reading the trace when the user scrolls a window,
41 * for example.
42 *
43 * @author Patrick Tasse
44 */
45 public class TmfEventsCache {
46
47 /**
48 * The generic TMF Events table cached event.
49 *
50 * @author Patrick Tasse
51 */
52 public static class CachedEvent implements ITmfEvent {
53 /**
54 * Event reference.
55 *
56 * When {@link TmfCollapseFilter} is active then it's event reference
57 * of the first event of repeated events.
58 */
59 ITmfEvent event;
60 /**
61 * Events rank.
62 *
63 * When {@link TmfCollapseFilter} is active then it's event rank of the
64 * first event of repeated events.
65 */
66 long rank;
67 /**
68 * Number times event is repeated. Updated by using {@link TmfCollapseFilter}
69 */
70 long repeatCount;
71
72 /**
73 * Constructor for new cached events.
74 *
75 * @param iTmfEvent
76 * The original trace event
77 * @param rank
78 * The rank of this event in the trace
79 */
80 public CachedEvent (ITmfEvent iTmfEvent, long rank) {
81 this.event = iTmfEvent;
82 this.rank = rank;
83 }
84
85 @Override
86 public <T> T getAdapter(Class<T> adapterType) {
87 return event.getAdapter(adapterType);
88 }
89
90 @Override
91 public ITmfTrace getTrace() {
92 return event.getTrace();
93 }
94
95 @Override
96 public long getRank() {
97 return event.getRank();
98 }
99
100 /**
101 * @since 1.0
102 */
103 @Override
104 public String getName() {
105 return event.getName();
106 }
107
108 @Override
109 public ITmfTimestamp getTimestamp() {
110 return event.getTimestamp();
111 }
112
113 @Override
114 public ITmfEventType getType() {
115 return event.getType();
116 }
117
118 @Override
119 public ITmfEventField getContent() {
120 return event.getContent();
121 }
122 }
123
124 private final CachedEvent[] fCache;
125 private final int fCacheSize;
126 private int fCacheStartIndex = 0;
127 private int fCacheEndIndex = 0;
128
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;
134
135 /**
136 * Constructor for the event cache
137 *
138 * @param cacheSize
139 * The size of the cache, in number of events
140 * @param table
141 * The Events table this cache will cover
142 */
143 public TmfEventsCache(int cacheSize, TmfEventsTable table) {
144 fCacheSize = cacheSize;
145 fCache = new CachedEvent[cacheSize * 2]; // the cache holds two blocks of cache size
146 fTable = table;
147 }
148
149 /**
150 * Assign a new trace to this events cache. This clears the current
151 * contents.
152 *
153 * @param trace
154 * The trace to assign.
155 */
156 public void setTrace(ITmfTrace trace) {
157 fTrace = trace;
158 clear();
159 }
160
161 /**
162 * Clear the current contents of this cache.
163 */
164 public synchronized void clear() {
165 if (job != null && job.getState() != Job.NONE) {
166 job.cancel();
167 }
168 Arrays.fill(fCache, null);
169 fCacheStartIndex = 0;
170 fCacheEndIndex = 0;
171 fFilterIndex.clear();
172 }
173
174 /**
175 * Apply a filter on this event cache. This clears the current cache
176 * contents.
177 *
178 * @param filter
179 * The ITmfFilter to apply.
180 * @param collapseFilterEnabled
181 * true if the collapse filter is enabled
182 * @since 2.0
183 */
184 public void applyFilter(ITmfFilter filter, boolean collapseFilterEnabled) {
185 fFilter = filter;
186 fCollapseFilterEnabled = collapseFilterEnabled;
187 clear();
188 }
189
190 /**
191 * Clear the current filter on this cache. This also clears the current
192 * cache contents.
193 */
194 public void clearFilter() {
195 fFilter = null;
196 fCollapseFilterEnabled = false;
197 clear();
198 }
199
200 /**
201 * Get an event from the cache. If the cache does not contain the event,
202 * a cache population request is triggered.
203 *
204 * @param index
205 * The index of this event in the cache
206 * @return The cached event, or 'null' if the event is not in the cache
207 */
208 public synchronized CachedEvent getEvent(int index) {
209 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
210 int i = index - fCacheStartIndex;
211 return fCache[i];
212 }
213 populateCache(index);
214 return null;
215 }
216
217 /**
218 * Peek an event in the cache. Does not trigger cache population.
219 *
220 * @param index
221 * Index of the event to peek
222 * @return The cached event, or 'null' if the event is not in the cache
223 */
224 public synchronized CachedEvent peekEvent(int index) {
225 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
226 int i = index - fCacheStartIndex;
227 return fCache[i];
228 }
229 return null;
230 }
231
232 /**
233 * Add a trace event to the cache.
234 *
235 * @param event
236 * The original trace event to be cached
237 * @param rank
238 * The rank of this event in the trace
239 * @param index
240 * The index this event will occupy in the cache
241 */
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);
247 fCacheEndIndex++;
248 }
249 }
250 if ((fFilter != null) && ((index % fCacheSize) == 0)) {
251 int i = index / fCacheSize;
252 fFilterIndex.add(i, Integer.valueOf((int) rank));
253 }
254 }
255
256 /**
257 * Update event repeat count at index
258 *
259 * @param index
260 * The index this event occupies in the cache
261 */
262 public synchronized void updateCollapsedEvent(int index) {
263 int i = index - fCacheStartIndex;
264 if (i < fCache.length) {
265 fCache[i].repeatCount++;
266 }
267 }
268
269 /**
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.
272 *
273 * @param rank
274 * The rank of the event in the trace
275 * @return The position (index) this event should use once cached
276 */
277 public int getFilteredEventIndex(final long rank) {
278 int current;
279 int startRank;
280 TmfEventRequest request;
281 final ITmfFilter filter = fFilter;
282 synchronized (this) {
283 int start = 0;
284 int end = fFilterIndex.size();
285
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;
291 } else {
292 for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) {
293 if (fCache[i].rank >= rank) {
294 return fCacheStartIndex + i;
295 }
296 }
297 return fCacheEndIndex;
298 }
299 }
300
301 current = (start + end) / 2;
302 while (current != start) {
303 if (rank < fFilterIndex.get(current)) {
304 end = current;
305 current = (start + end) / 2;
306 } else {
307 start = current;
308 current = (start + end) / 2;
309 }
310 }
311 startRank = fFilterIndex.size() > 0 ? fFilterIndex.get(current) : 0;
312 }
313
314 final int index = current * fCacheSize;
315
316 class DataRequest extends TmfEventRequest {
317 ITmfFilter requestFilter;
318 TmfCollapseFilter requestCollapsedFilter;
319 int requestRank;
320 int requestIndex;
321
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;
326 requestRank = start;
327 requestIndex = index;
328 requestCollapsedFilter = fCollapseFilterEnabled ? new TmfCollapseFilter() : null;
329 }
330
331 @Override
332 public void handleData(ITmfEvent event) {
333 super.handleData(event);
334 if (isCancelled()) {
335 return;
336 }
337 if (requestRank >= rank) {
338 cancel();
339 return;
340 }
341 requestRank++;
342 if (requestFilter.matches(event)) {
343 if (requestCollapsedFilter == null || requestCollapsedFilter.matches(event)) {
344 requestIndex++;
345 }
346 }
347 }
348
349 public int getFilteredIndex() {
350 return requestIndex;
351 }
352 }
353
354 request = new DataRequest(ITmfEvent.class, filter, startRank, ITmfEventRequest.ALL_DATA);
355 ((ITmfEventProvider) fTrace).sendRequest(request);
356 try {
357 request.waitForCompletion();
358 return ((DataRequest) request).getFilteredIndex();
359 } catch (InterruptedException e) {
360 Activator.getDefault().logError("Filter request interrupted!", e); //$NON-NLS-1$
361 }
362 return 0;
363 }
364
365 // ------------------------------------------------------------------------
366 // Event cache population
367 // ------------------------------------------------------------------------
368
369 // The event fetching job
370 private Job job;
371 private synchronized void populateCache(final int index) {
372
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
377 *
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.
381 */
382 if (job != null) {
383 if (job.getState() != Job.NONE) {
384 if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {
385 return;
386 }
387 // The new index is out of the requested range
388 // Kill the job and start a new one
389 job.cancel();
390 }
391 }
392
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;
398
399 job = new Job("Fetching Events") { //$NON-NLS-1$
400 private int startIndex = fCacheStartIndex;
401 private int skipCount = 0;
402 @Override
403 protected IStatus run(final IProgressMonitor monitor) {
404
405 int nbRequested;
406 if (fFilter == null) {
407 nbRequested = fCache.length;
408 } else {
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);
414 }
415 }
416
417 TmfEventRequest request = new TmfEventRequest(ITmfEvent.class,
418 TmfTimeRange.ETERNITY,
419 startIndex,
420 nbRequested,
421 TmfEventRequest.ExecutionType.FOREGROUND) {
422 private int count = 0;
423 private long rank = startIndex;
424 private TmfCollapseFilter collapseFilter = fCollapseFilterEnabled ? new TmfCollapseFilter() : null;
425 @Override
426 public void handleData(ITmfEvent event) {
427 // If the job is canceled, cancel the request so waitForCompletion() will unlock
428 if (monitor.isCanceled()) {
429 cancel();
430 return;
431 }
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()) {
438 return;
439 }
440 fCache[count] = new CachedEvent(event, rank);
441 count++;
442 fCacheEndIndex++;
443 }
444 if (fFilter != null) {
445 fTable.cacheUpdated(false);
446 }
447 }
448 } else if ((count > 0) && (skipCount <= 0)) {
449 fCache[count - 1].repeatCount++;
450 }
451 }
452 if (count >= fCache.length) {
453 cancel();
454 } else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows
455 cancel();
456 }
457 rank++;
458 }
459 };
460
461 ((ITmfEventProvider) fTrace).sendRequest(request);
462 try {
463 request.waitForCompletion();
464 } catch (InterruptedException e) {
465 Activator.getDefault().logError("Wait for completion interrupted for populateCache ", e); //$NON-NLS-1$
466 }
467
468 fTable.cacheUpdated(true);
469
470 // Flag the UI thread that the cache is ready
471 if (monitor.isCanceled()) {
472 return Status.CANCEL_STATUS;
473 }
474 return Status.OK_STATUS;
475 }
476 };
477 //job.setSystem(true);
478 job.setPriority(Job.SHORT);
479 job.schedule();
480 }
481
482 }
This page took 0.04206 seconds and 5 git commands to generate.