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