| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2011, 2013 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 | import java.util.Arrays; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.core.runtime.IProgressMonitor; |
| 20 | import org.eclipse.core.runtime.IStatus; |
| 21 | import org.eclipse.core.runtime.Status; |
| 22 | import org.eclipse.core.runtime.jobs.Job; |
| 23 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
| 24 | import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider; |
| 25 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
| 26 | import org.eclipse.linuxtools.tmf.core.filter.ITmfFilter; |
| 27 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; |
| 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
| 29 | |
| 30 | /** |
| 31 | * The generic TMF Events table events cache |
| 32 | * |
| 33 | * This can help avoid re-reading the trace when the user scrolls a window, |
| 34 | * for example. |
| 35 | * |
| 36 | * @version 1.0 |
| 37 | * @author Patrick Tasse |
| 38 | */ |
| 39 | public class TmfEventsCache { |
| 40 | |
| 41 | /** |
| 42 | * The generic TMF Events table cached event |
| 43 | * |
| 44 | * @version 1.0 |
| 45 | * @author Patrick Tasse |
| 46 | */ |
| 47 | public static class CachedEvent { |
| 48 | ITmfEvent event; |
| 49 | long rank; |
| 50 | |
| 51 | /** |
| 52 | * Constructor for new cached events. |
| 53 | * |
| 54 | * @param iTmfEvent |
| 55 | * The original trace event |
| 56 | * @param rank |
| 57 | * The rank of this event in the trace |
| 58 | */ |
| 59 | public CachedEvent (ITmfEvent iTmfEvent, long rank) { |
| 60 | this.event = iTmfEvent; |
| 61 | this.rank = rank; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | private final CachedEvent[] fCache; |
| 66 | private final int fCacheSize; |
| 67 | private int fCacheStartIndex = 0; |
| 68 | private int fCacheEndIndex = 0; |
| 69 | |
| 70 | private ITmfTrace fTrace; |
| 71 | private final TmfEventsTable fTable; |
| 72 | private ITmfFilter fFilter; |
| 73 | private final List<Integer> fFilterIndex = new ArrayList<Integer>(); // contains the event rank at each 'cache size' filtered events |
| 74 | |
| 75 | /** |
| 76 | * Constructor for the event cache |
| 77 | * |
| 78 | * @param cacheSize |
| 79 | * The size of the cache, in number of events |
| 80 | * @param table |
| 81 | * The Events table this cache will cover |
| 82 | */ |
| 83 | public TmfEventsCache(int cacheSize, TmfEventsTable table) { |
| 84 | fCacheSize = cacheSize; |
| 85 | fCache = new CachedEvent[cacheSize * 2]; // the cache holds two blocks of cache size |
| 86 | fTable = table; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Assign a new trace to this events cache. This clears the current |
| 91 | * contents. |
| 92 | * |
| 93 | * @param trace |
| 94 | * The trace to assign. |
| 95 | */ |
| 96 | public void setTrace(ITmfTrace trace) { |
| 97 | fTrace = trace; |
| 98 | clear(); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Clear the current contents of this cache. |
| 103 | */ |
| 104 | public synchronized void clear() { |
| 105 | if (job != null && job.getState() != Job.NONE) { |
| 106 | job.cancel(); |
| 107 | } |
| 108 | Arrays.fill(fCache, null); |
| 109 | fCacheStartIndex = 0; |
| 110 | fCacheEndIndex = 0; |
| 111 | fFilterIndex.clear(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Apply a filter on this event cache. This clears the current cache |
| 116 | * contents. |
| 117 | * |
| 118 | * @param filter |
| 119 | * The ITmfFilter to apply. |
| 120 | */ |
| 121 | public void applyFilter(ITmfFilter filter) { |
| 122 | fFilter = filter; |
| 123 | clear(); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Clear the current filter on this cache. This also clears the current |
| 128 | * cache contents. |
| 129 | */ |
| 130 | public void clearFilter() { |
| 131 | fFilter = null; |
| 132 | clear(); |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Get an event from the cache. If the cache does not contain the event, |
| 137 | * a cache population request is triggered. |
| 138 | * |
| 139 | * @param index |
| 140 | * The index of this event in the cache |
| 141 | * @return The cached event, or 'null' if the event is not in the cache |
| 142 | */ |
| 143 | public synchronized CachedEvent getEvent(int index) { |
| 144 | if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) { |
| 145 | int i = index - fCacheStartIndex; |
| 146 | return fCache[i]; |
| 147 | } |
| 148 | populateCache(index); |
| 149 | return null; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Peek an event in the cache. Does not trigger cache population. |
| 154 | * |
| 155 | * @param index |
| 156 | * Index of the event to peek |
| 157 | * @return The cached event, or 'null' if the event is not in the cache |
| 158 | */ |
| 159 | public synchronized CachedEvent peekEvent(int index) { |
| 160 | if ((index >= fCacheStartIndex) && (index < fCacheEndIndex)) { |
| 161 | int i = index - fCacheStartIndex; |
| 162 | return fCache[i]; |
| 163 | } |
| 164 | return null; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Add a trace event to the cache. |
| 169 | * |
| 170 | * @param event |
| 171 | * The original trace event to be cached |
| 172 | * @param rank |
| 173 | * The rank of this event in the trace |
| 174 | * @param index |
| 175 | * The index this event will occupy in the cache |
| 176 | */ |
| 177 | public synchronized void storeEvent(ITmfEvent event, long rank, int index) { |
| 178 | if (index == fCacheEndIndex) { |
| 179 | int i = index - fCacheStartIndex; |
| 180 | if (i < fCache.length) { |
| 181 | fCache[i] = new CachedEvent(event, rank); |
| 182 | fCacheEndIndex++; |
| 183 | } |
| 184 | } |
| 185 | if ((fFilter != null) && ((index % fCacheSize) == 0)) { |
| 186 | int i = index / fCacheSize; |
| 187 | fFilterIndex.add(i, Integer.valueOf((int) rank)); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Get the cache index of an event from his rank in the trace. This will |
| 193 | * take in consideration any filter that might be applied. |
| 194 | * |
| 195 | * @param rank |
| 196 | * The rank of the event in the trace |
| 197 | * @return The position (index) this event should use once cached |
| 198 | */ |
| 199 | public int getFilteredEventIndex(final long rank) { |
| 200 | int current; |
| 201 | int startRank; |
| 202 | TmfDataRequest request; |
| 203 | final ITmfFilter filter = fFilter; |
| 204 | synchronized (this) { |
| 205 | int start = 0; |
| 206 | int end = fFilterIndex.size(); |
| 207 | |
| 208 | if ((fCacheEndIndex - fCacheStartIndex) > 1) { |
| 209 | if (rank < fCache[0].rank) { |
| 210 | end = (fCacheStartIndex / fCacheSize) + 1; |
| 211 | } else if (rank > fCache[fCacheEndIndex - fCacheStartIndex - 1].rank) { |
| 212 | start = fCacheEndIndex / fCacheSize; |
| 213 | } else { |
| 214 | for (int i = 0; i < (fCacheEndIndex - fCacheStartIndex); i++) { |
| 215 | if (fCache[i].rank >= rank) { |
| 216 | return fCacheStartIndex + i; |
| 217 | } |
| 218 | } |
| 219 | return fCacheEndIndex; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | current = (start + end) / 2; |
| 224 | while (current != start) { |
| 225 | if (rank < fFilterIndex.get(current)) { |
| 226 | end = current; |
| 227 | current = (start + end) / 2; |
| 228 | } else { |
| 229 | start = current; |
| 230 | current = (start + end) / 2; |
| 231 | } |
| 232 | } |
| 233 | startRank = fFilterIndex.size() > 0 ? fFilterIndex.get(current) : 0; |
| 234 | } |
| 235 | |
| 236 | final int index = current * fCacheSize; |
| 237 | |
| 238 | class DataRequest extends TmfDataRequest { |
| 239 | ITmfFilter requestFilter; |
| 240 | int requestRank; |
| 241 | int requestIndex; |
| 242 | |
| 243 | DataRequest(Class<? extends ITmfEvent> dataType, ITmfFilter reqFilter, int start, int nbRequested) { |
| 244 | super(dataType, start, nbRequested); |
| 245 | requestFilter = reqFilter; |
| 246 | requestRank = start; |
| 247 | requestIndex = index; |
| 248 | } |
| 249 | |
| 250 | @Override |
| 251 | public void handleData(ITmfEvent event) { |
| 252 | super.handleData(event); |
| 253 | if (isCancelled()) { |
| 254 | return; |
| 255 | } |
| 256 | if (requestRank >= rank) { |
| 257 | cancel(); |
| 258 | return; |
| 259 | } |
| 260 | requestRank++; |
| 261 | if (requestFilter.matches(event)) { |
| 262 | requestIndex++; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | public int getFilteredIndex() { |
| 267 | return requestIndex; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | request = new DataRequest(ITmfEvent.class, filter, startRank, TmfDataRequest.ALL_DATA); |
| 272 | ((ITmfDataProvider) fTrace).sendRequest(request); |
| 273 | try { |
| 274 | request.waitForCompletion(); |
| 275 | return ((DataRequest) request).getFilteredIndex(); |
| 276 | } catch (InterruptedException e) { |
| 277 | Activator.getDefault().logError("Filter request interrupted!", e); //$NON-NLS-1$ |
| 278 | } |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | // ------------------------------------------------------------------------ |
| 283 | // Event cache population |
| 284 | // ------------------------------------------------------------------------ |
| 285 | |
| 286 | // The event fetching job |
| 287 | private Job job; |
| 288 | private synchronized void populateCache(final int index) { |
| 289 | |
| 290 | /* Check if the current job will fetch the requested event: |
| 291 | * 1. The job must exist |
| 292 | * 2. It must be running (i.e. not completed) |
| 293 | * 3. The requested index must be within the cache range |
| 294 | * |
| 295 | * If the job meets these conditions, we simply exit. |
| 296 | * Otherwise, we create a new job but we might have to cancel |
| 297 | * an existing job for an obsolete range. |
| 298 | */ |
| 299 | if (job != null) { |
| 300 | if (job.getState() != Job.NONE) { |
| 301 | if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) { |
| 302 | return; |
| 303 | } |
| 304 | // The new index is out of the requested range |
| 305 | // Kill the job and start a new one |
| 306 | job.cancel(); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | // Populate the cache starting at the index that is one block less |
| 311 | // of cache size than the requested index. The cache will hold two |
| 312 | // consecutive blocks of cache size, centered on the requested index. |
| 313 | fCacheStartIndex = Math.max(0, index - fCacheSize); |
| 314 | fCacheEndIndex = fCacheStartIndex; |
| 315 | |
| 316 | job = new Job("Fetching Events") { //$NON-NLS-1$ |
| 317 | private int startIndex = fCacheStartIndex; |
| 318 | private int skipCount = 0; |
| 319 | @Override |
| 320 | protected IStatus run(final IProgressMonitor monitor) { |
| 321 | |
| 322 | int nbRequested; |
| 323 | if (fFilter == null) { |
| 324 | nbRequested = fCache.length; |
| 325 | } else { |
| 326 | nbRequested = TmfDataRequest.ALL_DATA; |
| 327 | int i = startIndex / fCacheSize; |
| 328 | if (i < fFilterIndex.size()) { |
| 329 | skipCount = startIndex - (i * fCacheSize); |
| 330 | startIndex = fFilterIndex.get(i); |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | TmfDataRequest request = new TmfDataRequest(ITmfEvent.class, startIndex, nbRequested) { |
| 335 | private int count = 0; |
| 336 | private long rank = startIndex; |
| 337 | @Override |
| 338 | public void handleData(ITmfEvent event) { |
| 339 | // If the job is canceled, cancel the request so waitForCompletion() will unlock |
| 340 | if (monitor.isCanceled()) { |
| 341 | cancel(); |
| 342 | return; |
| 343 | } |
| 344 | super.handleData(event); |
| 345 | if (event != null) { |
| 346 | if (((fFilter == null) || fFilter.matches(event)) && (skipCount-- <= 0)) { |
| 347 | synchronized (TmfEventsCache.this) { |
| 348 | if (monitor.isCanceled()) { |
| 349 | return; |
| 350 | } |
| 351 | fCache[count] = new CachedEvent(event, rank); |
| 352 | count++; |
| 353 | fCacheEndIndex++; |
| 354 | } |
| 355 | if (fFilter != null) { |
| 356 | fTable.cacheUpdated(false); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | if (count >= fCache.length) { |
| 361 | cancel(); |
| 362 | } else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows |
| 363 | cancel(); |
| 364 | } |
| 365 | rank++; |
| 366 | } |
| 367 | }; |
| 368 | |
| 369 | ((ITmfDataProvider) fTrace).sendRequest(request); |
| 370 | try { |
| 371 | request.waitForCompletion(); |
| 372 | } catch (InterruptedException e) { |
| 373 | Activator.getDefault().logError("Wait for completion interrupted for populateCache ", e); //$NON-NLS-1$ |
| 374 | } |
| 375 | |
| 376 | fTable.cacheUpdated(true); |
| 377 | |
| 378 | // Flag the UI thread that the cache is ready |
| 379 | if (monitor.isCanceled()) { |
| 380 | return Status.CANCEL_STATUS; |
| 381 | } |
| 382 | return Status.OK_STATUS; |
| 383 | } |
| 384 | }; |
| 385 | //job.setSystem(true); |
| 386 | job.setPriority(Job.SHORT); |
| 387 | job.schedule(); |
| 388 | } |
| 389 | |
| 390 | } |