| 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 |
| 21 | import org.eclipse.linuxtools.tmf.core.component.ITmfDataProvider;\r |
| 22 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;\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 |
| 26 | \r |
| 27 | public class TmfEventsCache {\r |
| 28 | \r |
| 29 | public static class CachedEvent {\r |
| 30 | ITmfEvent event;\r |
| 31 | long rank;\r |
| 32 | \r |
| 33 | public CachedEvent (ITmfEvent iTmfEvent, long rank) {\r |
| 34 | this.event = iTmfEvent;\r |
| 35 | this.rank = rank;\r |
| 36 | }\r |
| 37 | }\r |
| 38 | \r |
| 39 | private final CachedEvent[] fCache;\r |
| 40 | private int fCacheStartIndex = 0;\r |
| 41 | private int fCacheEndIndex = 0;\r |
| 42 | \r |
| 43 | private ITmfTrace<?> fTrace;\r |
| 44 | private final TmfEventsTable fTable;\r |
| 45 | private ITmfFilter fFilter;\r |
| 46 | private final 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 synchronized 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 synchronized 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 synchronized 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(ITmfEvent 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, Integer.valueOf((int) rank));\r |
| 106 | }\r |
| 107 | }\r |
| 108 | \r |
| 109 | @SuppressWarnings("unchecked")\r |
| 110 | public synchronized int getFilteredEventIndex(final long rank) {\r |
| 111 | int current;\r |
| 112 | int startRank;\r |
| 113 | TmfDataRequest<ITmfEvent> 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 ITmfEvent> 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()) {\r |
| 162 | return;\r |
| 163 | }\r |
| 164 | if (fRank >= rank) {\r |
| 165 | cancel();\r |
| 166 | return;\r |
| 167 | }\r |
| 168 | fRank++;\r |
| 169 | if (fFilter.matches(event)) {\r |
| 170 | fIndex++;\r |
| 171 | }\r |
| 172 | }\r |
| 173 | \r |
| 174 | public int getFilteredIndex() {\r |
| 175 | return fIndex;\r |
| 176 | }\r |
| 177 | }\r |
| 178 | \r |
| 179 | request = new DataRequest<ITmfEvent>(ITmfEvent.class, startRank, TmfDataRequest.ALL_DATA);\r |
| 180 | ((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);\r |
| 181 | try {\r |
| 182 | request.waitForCompletion();\r |
| 183 | return ((DataRequest<ITmfEvent>) request).getFilteredIndex();\r |
| 184 | } catch (InterruptedException e) {\r |
| 185 | }\r |
| 186 | return 0;\r |
| 187 | }\r |
| 188 | \r |
| 189 | // ------------------------------------------------------------------------\r |
| 190 | // Event cache population\r |
| 191 | // ------------------------------------------------------------------------\r |
| 192 | \r |
| 193 | // The event fetching job\r |
| 194 | private Job job;\r |
| 195 | private synchronized void populateCache(final int index) {\r |
| 196 | \r |
| 197 | /* Check if the current job will fetch the requested event:\r |
| 198 | * 1. The job must exist\r |
| 199 | * 2. It must be running (i.e. not completed)\r |
| 200 | * 3. The requested index must be within the cache range\r |
| 201 | *\r |
| 202 | * If the job meets these conditions, we simply exit.\r |
| 203 | * Otherwise, we create a new job but we might have to cancel\r |
| 204 | * an existing job for an obsolete range.\r |
| 205 | */\r |
| 206 | if (job != null) {\r |
| 207 | if (job.getState() != Job.NONE) {\r |
| 208 | if ((index >= fCacheStartIndex) && (index < (fCacheStartIndex + fCache.length))) {\r |
| 209 | return;\r |
| 210 | }\r |
| 211 | // The new index is out of the requested range\r |
| 212 | // Kill the job and start a new one\r |
| 213 | job.cancel();\r |
| 214 | }\r |
| 215 | }\r |
| 216 | \r |
| 217 | fCacheStartIndex = index;\r |
| 218 | fCacheEndIndex = index;\r |
| 219 | \r |
| 220 | job = new Job("Fetching Events") { //$NON-NLS-1$\r |
| 221 | private int startIndex = index;\r |
| 222 | private int skipCount = 0;\r |
| 223 | @Override\r |
| 224 | @SuppressWarnings("unchecked")\r |
| 225 | protected IStatus run(final IProgressMonitor monitor) {\r |
| 226 | \r |
| 227 | int nbRequested;\r |
| 228 | if (fFilter == null) {\r |
| 229 | nbRequested = fCache.length;\r |
| 230 | } else {\r |
| 231 | nbRequested = TmfDataRequest.ALL_DATA;\r |
| 232 | int i = index / fCache.length;\r |
| 233 | if (i < fFilterIndex.size()) {\r |
| 234 | startIndex = fFilterIndex.get(i);\r |
| 235 | skipCount = index - (i * fCache.length);\r |
| 236 | }\r |
| 237 | }\r |
| 238 | \r |
| 239 | TmfDataRequest<ITmfEvent> request = new TmfDataRequest<ITmfEvent>(ITmfEvent.class, startIndex, nbRequested) {\r |
| 240 | private int count = 0;\r |
| 241 | private long rank = startIndex;\r |
| 242 | @Override\r |
| 243 | public void handleData(ITmfEvent event) {\r |
| 244 | // If the job is canceled, cancel the request so waitForCompletion() will unlock\r |
| 245 | if (monitor.isCanceled()) {\r |
| 246 | cancel();\r |
| 247 | return;\r |
| 248 | }\r |
| 249 | super.handleData(event);\r |
| 250 | if (event != null) {\r |
| 251 | if (((fFilter == null) || fFilter.matches(event)) && (skipCount-- <= 0)) {\r |
| 252 | synchronized (TmfEventsCache.this) {\r |
| 253 | fCache[count] = new CachedEvent(event.clone(), rank);\r |
| 254 | count++;\r |
| 255 | fCacheEndIndex++;\r |
| 256 | }\r |
| 257 | if (fFilter != null) {\r |
| 258 | fTable.cacheUpdated(false);\r |
| 259 | }\r |
| 260 | }\r |
| 261 | }\r |
| 262 | if (count >= fCache.length) {\r |
| 263 | cancel();\r |
| 264 | } else if ((fFilter != null) && (count >= (fTable.getTable().getItemCount() - 3))) { // -1 for header row, -2 for top and bottom filter status rows\r |
| 265 | cancel();\r |
| 266 | }\r |
| 267 | rank++;\r |
| 268 | }\r |
| 269 | };\r |
| 270 | \r |
| 271 | ((ITmfDataProvider<ITmfEvent>) fTrace).sendRequest(request);\r |
| 272 | try {\r |
| 273 | request.waitForCompletion();\r |
| 274 | } catch (InterruptedException e) {\r |
| 275 | e.printStackTrace();\r |
| 276 | }\r |
| 277 | \r |
| 278 | fTable.cacheUpdated(true);\r |
| 279 | \r |
| 280 | // Flag the UI thread that the cache is ready\r |
| 281 | if (monitor.isCanceled()) {\r |
| 282 | return Status.CANCEL_STATUS;\r |
| 283 | } else {\r |
| 284 | return Status.OK_STATUS;\r |
| 285 | }\r |
| 286 | }\r |
| 287 | };\r |
| 288 | //job.setSystem(true);\r |
| 289 | job.setPriority(Job.SHORT);\r |
| 290 | job.schedule();\r |
| 291 | }\r |
| 292 | \r |
| 293 | }\r |