295f041e6535894533abf81be36a14439bc1f75b
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / tmf / ui / viewers / events / TmfEventsCache.java
1 /*******************************************************************************
2 * Copyright (c) 2011 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 ******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.ui.viewers.events;
14
15 import java.util.ArrayList;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.core.runtime.jobs.Job;
21 import org.eclipse.linuxtools.tmf.component.ITmfDataProvider;
22 import org.eclipse.linuxtools.tmf.event.TmfEvent;
23 import org.eclipse.linuxtools.tmf.filter.ITmfFilter;
24 import org.eclipse.linuxtools.tmf.request.TmfDataRequest;
25 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
26
27 public class TmfEventsCache {
28
29 public class CachedEvent {
30 TmfEvent event;
31 long rank;
32
33 public CachedEvent (TmfEvent event, long rank) {
34 this.event = event;
35 this.rank = rank;
36 }
37 }
38
39 private CachedEvent[] fCache;
40 private int fCacheStartIndex = 0;
41 private int fCacheEndIndex = 0;
42
43 private ITmfTrace fTrace;
44 private TmfEventsTable fTable;
45 private ITmfFilter fFilter;
46 private ArrayList<Integer> fFilterIndex = new ArrayList<Integer>(); // contains the event rank at each 'cache size' filtered events
47
48 public TmfEventsCache(int cacheSize, TmfEventsTable table) {
49 fCache = new CachedEvent[cacheSize];
50 fTable = table;
51 }
52
53 public void setTrace(ITmfTrace trace) {
54 fTrace = trace;
55 clear();
56 }
57
58 public void clear() {
59 fCacheStartIndex = 0;
60 fCacheEndIndex = 0;
61 fFilterIndex.clear();
62 }
63
64 public void applyFilter(ITmfFilter filter) {
65 fFilter = filter;
66 clear();
67 }
68
69 public void clearFilter() {
70 fFilter = null;
71 clear();
72 }
73
74 public CachedEvent getEvent(int index) {
75 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
76 int i = index - fCacheStartIndex;
77 return fCache[i];
78 }
79 populateCache(index);
80 return null;
81 }
82
83 public CachedEvent peekEvent(int index) {
84 if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) {
85 int i = index - fCacheStartIndex;
86 return fCache[i];
87 }
88 return null;
89 }
90
91 public synchronized void storeEvent(TmfEvent event, long rank, int index) {
92 if (fCacheStartIndex == fCacheEndIndex) {
93 fCacheStartIndex = index;
94 fCacheEndIndex = index;
95 }
96 if (index == fCacheEndIndex) {
97 int i = index - fCacheStartIndex;
98 if (i < fCache.length) {
99 fCache[i] = new CachedEvent(event.clone(), rank);
100 fCacheEndIndex++;
101 }
102 }
103 if (fFilter != null && index % fCache.length == 0) {
104 int i = index / fCache.length;
105 fFilterIndex.add(i, new Integer((int) rank));
106 }
107 }
108
109 @SuppressWarnings("unchecked")
110 public int getFilteredEventIndex(final long rank) {
111 int current;
112 int startRank;
113 TmfDataRequest<TmfEvent> request;
114 synchronized (this) {
115 int start = 0;
116 int end = fFilterIndex.size();
117
118 if (fCacheEndIndex - fCacheStartIndex > 1) {
119 if (rank < fCache[0].rank) {
120 end = fCacheStartIndex / fCache.length + 1;
121 } else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) {
122 start = fCacheEndIndex / fCache.length;
123 } else {
124 for (int i = 0; i < fCacheEndIndex - fCacheStartIndex; i++) {
125 if (fCache[i].rank >= rank) {
126 return fCacheStartIndex + i;
127 }
128 }
129 return fCacheEndIndex;
130 }
131 }
132
133 current = (start + end) / 2;
134 while (current != start) {
135 if (rank < fFilterIndex.get(current)) {
136 end = current;
137 current = (start + end) / 2;
138 } else {
139 start = current;
140 current = (start + end) / 2;
141 }
142 }
143 startRank = fFilterIndex.get(current);
144 }
145
146 final int index = current * fCache.length;
147
148 class DataRequest<T extends TmfEvent> extends TmfDataRequest<T> {
149 int fRank;
150 int fIndex;
151
152 DataRequest(Class<T> dataType, int start, int nbRequested) {
153 super(dataType, start, nbRequested);
154 fRank = start;
155 fIndex = index;
156 }
157
158 @Override
159 public void handleData(T event) {
160 super.handleData(event);
161 if (isCancelled()) return;
162 if (fRank >= rank) {
163 cancel();
164 return;
165 }
166 fRank++;
167 if (fFilter.matches(event)) {
168 fIndex++;
169 }
170 }
171
172 public int getFilteredIndex() {
173 return fIndex;
174 }
175 }
176
177 request = new DataRequest<TmfEvent>(TmfEvent.class, startRank, TmfDataRequest.ALL_DATA);
178 ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);
179 try {
180 request.waitForCompletion();
181 return ((DataRequest<TmfEvent>) request).getFilteredIndex();
182 } catch (InterruptedException e) {
183 }
184 return 0;
185 }
186
187 // ------------------------------------------------------------------------
188 // Event cache population
189 // ------------------------------------------------------------------------
190
191 // The event fetching job
192 private Job job;
193 private synchronized void populateCache(final int index) {
194
195 /* Check if the current job will fetch the requested event:
196 * 1. The job must exist
197 * 2. It must be running (i.e. not completed)
198 * 3. The requested index must be within the cache range
199 *
200 * If the job meets these conditions, we simply exit.
201 * Otherwise, we create a new job but we might have to cancel
202 * an existing job for an obsolete range.
203 */
204 if (job != null) {
205 if (job.getState() != Job.NONE) {
206 if (index >= fCacheStartIndex && index < (fCacheStartIndex + fCache.length)) {
207 return;
208 }
209 // The new index is out of the requested range
210 // Kill the job and start a new one
211 job.cancel();
212 }
213 }
214
215 fCacheStartIndex = index;
216 fCacheEndIndex = index;
217
218 job = new Job("Fetching Events") { //$NON-NLS-1$
219 private int startIndex = index;
220 private int skipCount = 0;
221 @Override
222 @SuppressWarnings("unchecked")
223 protected IStatus run(final IProgressMonitor monitor) {
224
225 int nbRequested;
226 if (fFilter == null) {
227 nbRequested = fCache.length;
228 } else {
229 nbRequested = TmfDataRequest.ALL_DATA;
230 int i = index / fCache.length;
231 if (i < fFilterIndex.size()) {
232 startIndex = fFilterIndex.get(i);
233 skipCount = index - (i * fCache.length);
234 }
235 }
236
237 TmfDataRequest<TmfEvent> request = new TmfDataRequest<TmfEvent>(TmfEvent.class, startIndex, nbRequested) {
238 private int count = 0;
239 private long rank = startIndex;
240 @Override
241 public void handleData(TmfEvent event) {
242 // If the job is canceled, cancel the request so waitForCompletion() will unlock
243 if (monitor.isCanceled()) {
244 cancel();
245 return;
246 }
247 super.handleData(event);
248 if (event != null) {
249 if ((fFilter == null || fFilter.matches(event)) && skipCount-- <= 0) {
250 synchronized (TmfEventsCache.this) {
251 fCache[count] = new CachedEvent(event.clone(), rank);
252 count++;
253 fCacheEndIndex++;
254 }
255 if (fFilter != null) {
256 fTable.cacheUpdated(false);
257 }
258 }
259 }
260 if (count >= fCache.length) {
261 cancel();
262 } else if (fFilter != null && count >= fTable.getTable().getItemCount() - 3) { // -1 for header row, -2 for top and bottom filter status rows
263 cancel();
264 }
265 rank++;
266 }
267 };
268
269 ((ITmfDataProvider<TmfEvent>) fTrace).sendRequest(request);
270 try {
271 request.waitForCompletion();
272 } catch (InterruptedException e) {
273 e.printStackTrace();
274 }
275
276 fTable.cacheUpdated(true);
277
278 // Flag the UI thread that the cache is ready
279 if (monitor.isCanceled()) {
280 return Status.CANCEL_STATUS;
281 } else {
282 return Status.OK_STATUS;
283 }
284 }
285 };
286 //job.setSystem(true);
287 job.setPriority(Job.SHORT);
288 job.schedule();
289 }
290
291 }
This page took 0.036417 seconds and 5 git commands to generate.