| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2009, 2010 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 | * Francois Chouinard - Initial API and implementation |
| 11 | *******************************************************************************/ |
| 12 | |
| 13 | package org.eclipse.linuxtools.tmf.core.component; |
| 14 | |
| 15 | import java.util.Vector; |
| 16 | import java.util.concurrent.BlockingQueue; |
| 17 | import java.util.concurrent.LinkedBlockingQueue; |
| 18 | import java.util.concurrent.SynchronousQueue; |
| 19 | |
| 20 | import org.eclipse.linuxtools.tmf.core.Tracer; |
| 21 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
| 22 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; |
| 23 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType; |
| 24 | import org.eclipse.linuxtools.tmf.core.request.TmfCoalescedDataRequest; |
| 25 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; |
| 26 | import org.eclipse.linuxtools.tmf.core.request.TmfRequestExecutor; |
| 27 | import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal; |
| 28 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; |
| 29 | import org.eclipse.linuxtools.tmf.core.signal.TmfStartSynchSignal; |
| 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
| 31 | |
| 32 | /** |
| 33 | * <b><u>TmfProvider</u></b> |
| 34 | * <p> |
| 35 | * The TmfProvider<T> is a provider for a data of type <T>. |
| 36 | * <p> |
| 37 | * This abstract class implements the housekeeking methods to register/ deregister the event provider and to handle |
| 38 | * generically the event requests. |
| 39 | * <p> |
| 40 | * The concrete class can either re-implement processRequest() entirely or just implement the hooks (initializeContext() |
| 41 | * and getNext()). |
| 42 | * <p> |
| 43 | * TODO: Add support for providing multiple data types. |
| 44 | */ |
| 45 | public abstract class TmfDataProvider<T extends TmfEvent> extends TmfComponent implements ITmfDataProvider<T> { |
| 46 | |
| 47 | // ------------------------------------------------------------------------ |
| 48 | // Constants |
| 49 | // ------------------------------------------------------------------------ |
| 50 | |
| 51 | public static final int DEFAULT_BLOCK_SIZE = 50000; |
| 52 | public static final int DEFAULT_QUEUE_SIZE = 1000; |
| 53 | |
| 54 | // ------------------------------------------------------------------------ |
| 55 | // Attributes |
| 56 | // ------------------------------------------------------------------------ |
| 57 | |
| 58 | protected Class<T> fType; |
| 59 | protected boolean fLogData; |
| 60 | protected boolean fLogError; |
| 61 | |
| 62 | protected int fQueueSize = DEFAULT_QUEUE_SIZE; |
| 63 | protected BlockingQueue<T> fDataQueue; |
| 64 | protected TmfRequestExecutor fExecutor; |
| 65 | |
| 66 | private int fSignalDepth = 0; |
| 67 | private final Object fLock = new Object(); |
| 68 | |
| 69 | private int fRequestPendingCounter = 0; |
| 70 | |
| 71 | // ------------------------------------------------------------------------ |
| 72 | // Constructors |
| 73 | // ------------------------------------------------------------------------ |
| 74 | |
| 75 | public TmfDataProvider() { |
| 76 | super(); |
| 77 | fQueueSize = DEFAULT_QUEUE_SIZE; |
| 78 | fDataQueue = new LinkedBlockingQueue<T>(fQueueSize); |
| 79 | fExecutor = new TmfRequestExecutor(); |
| 80 | } |
| 81 | |
| 82 | public void init(String name, Class<T> dataType) { |
| 83 | super.init(name); |
| 84 | fType = dataType; |
| 85 | fDataQueue = (fQueueSize > 1) ? new LinkedBlockingQueue<T>(fQueueSize) : new SynchronousQueue<T>(); |
| 86 | |
| 87 | fExecutor = new TmfRequestExecutor(); |
| 88 | fSignalDepth = 0; |
| 89 | |
| 90 | fLogData = Tracer.isEventTraced(); |
| 91 | fLogError = Tracer.isErrorTraced(); |
| 92 | |
| 93 | TmfProviderManager.register(fType, this); |
| 94 | } |
| 95 | |
| 96 | protected TmfDataProvider(String name, Class<T> type, int queueSize) { |
| 97 | this(); |
| 98 | fQueueSize = queueSize; |
| 99 | init(name, type); |
| 100 | } |
| 101 | |
| 102 | public TmfDataProvider(TmfDataProvider<T> other) { |
| 103 | this(); |
| 104 | init(other.getName(), other.fType); |
| 105 | } |
| 106 | |
| 107 | public TmfDataProvider(String name, Class<T> type) { |
| 108 | this(name, type, DEFAULT_QUEUE_SIZE); |
| 109 | } |
| 110 | |
| 111 | @Override |
| 112 | public void dispose() { |
| 113 | TmfProviderManager.deregister(fType, this); |
| 114 | fExecutor.stop(); |
| 115 | super.dispose(); |
| 116 | // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped"); |
| 117 | } |
| 118 | |
| 119 | // ------------------------------------------------------------------------ |
| 120 | // Accessors |
| 121 | // ------------------------------------------------------------------------ |
| 122 | |
| 123 | public int getQueueSize() { |
| 124 | return fQueueSize; |
| 125 | } |
| 126 | |
| 127 | public Class<?> getType() { |
| 128 | return fType; |
| 129 | } |
| 130 | |
| 131 | // ------------------------------------------------------------------------ |
| 132 | // ITmfRequestHandler |
| 133 | // ------------------------------------------------------------------------ |
| 134 | |
| 135 | @Override |
| 136 | public void sendRequest(final ITmfDataRequest<T> request) { |
| 137 | synchronized (fLock) { |
| 138 | if (fSignalDepth > 0) { |
| 139 | coalesceDataRequest(request); |
| 140 | } else { |
| 141 | dispatchRequest(request); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * This method queues the coalesced requests. |
| 148 | * |
| 149 | * @param thread |
| 150 | */ |
| 151 | @Override |
| 152 | public void fireRequest() { |
| 153 | synchronized (fLock) { |
| 154 | if (fRequestPendingCounter > 0) { |
| 155 | return; |
| 156 | } |
| 157 | if (fPendingCoalescedRequests.size() > 0) { |
| 158 | for (TmfDataRequest<T> request : fPendingCoalescedRequests) { |
| 159 | dispatchRequest(request); |
| 160 | } |
| 161 | fPendingCoalescedRequests.clear(); |
| 162 | } |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Increments/decrements the pending requests counters and fires the request if necessary (counter == 0). Used for |
| 168 | * coalescing requests accross multiple TmfDataProvider. |
| 169 | * |
| 170 | * @param isIncrement |
| 171 | */ |
| 172 | @Override |
| 173 | public void notifyPendingRequest(boolean isIncrement) { |
| 174 | synchronized (fLock) { |
| 175 | if (isIncrement) { |
| 176 | if (fSignalDepth > 0) { |
| 177 | fRequestPendingCounter++; |
| 178 | } |
| 179 | } else { |
| 180 | if (fRequestPendingCounter > 0) { |
| 181 | fRequestPendingCounter--; |
| 182 | } |
| 183 | |
| 184 | // fire request if all pending requests are received |
| 185 | if (fRequestPendingCounter == 0) { |
| 186 | fireRequest(); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | // ------------------------------------------------------------------------ |
| 193 | // Coalescing (primitive test...) |
| 194 | // ------------------------------------------------------------------------ |
| 195 | |
| 196 | protected Vector<TmfCoalescedDataRequest<T>> fPendingCoalescedRequests = new Vector<TmfCoalescedDataRequest<T>>(); |
| 197 | |
| 198 | protected void newCoalescedDataRequest(ITmfDataRequest<T> request) { |
| 199 | synchronized (fLock) { |
| 200 | TmfCoalescedDataRequest<T> coalescedRequest = new TmfCoalescedDataRequest<T>(request.getDataType(), request.getIndex(), |
| 201 | request.getNbRequested(), request.getBlockSize(), request.getExecType()); |
| 202 | coalescedRequest.addRequest(request); |
| 203 | if (Tracer.isRequestTraced()) { |
| 204 | Tracer.traceRequest(request, "coalesced with " + coalescedRequest.getRequestId()); //$NON-NLS-1$ |
| 205 | } |
| 206 | fPendingCoalescedRequests.add(coalescedRequest); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | protected void coalesceDataRequest(ITmfDataRequest<T> request) { |
| 211 | synchronized (fLock) { |
| 212 | for (TmfCoalescedDataRequest<T> coalescedRequest : fPendingCoalescedRequests) { |
| 213 | if (coalescedRequest.isCompatible(request)) { |
| 214 | coalescedRequest.addRequest(request); |
| 215 | if (Tracer.isRequestTraced()) { |
| 216 | Tracer.traceRequest(request, "coalesced with " + coalescedRequest.getRequestId()); //$NON-NLS-1$ |
| 217 | } |
| 218 | return; |
| 219 | } |
| 220 | } |
| 221 | newCoalescedDataRequest(request); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | // ------------------------------------------------------------------------ |
| 226 | // Request processing |
| 227 | // ------------------------------------------------------------------------ |
| 228 | |
| 229 | private void dispatchRequest(final ITmfDataRequest<T> request) { |
| 230 | if (request.getExecType() == ExecutionType.FOREGROUND) |
| 231 | queueRequest(request); |
| 232 | else |
| 233 | queueBackgroundRequest(request, request.getBlockSize(), true); |
| 234 | } |
| 235 | |
| 236 | protected void queueRequest(final ITmfDataRequest<T> request) { |
| 237 | |
| 238 | if (fExecutor.isShutdown()) { |
| 239 | request.cancel(); |
| 240 | return; |
| 241 | } |
| 242 | |
| 243 | final TmfDataProvider<T> provider = this; |
| 244 | |
| 245 | // Process the request |
| 246 | TmfThread thread = new TmfThread(request.getExecType()) { |
| 247 | |
| 248 | @Override |
| 249 | public void run() { |
| 250 | |
| 251 | if (Tracer.isRequestTraced()) |
| 252 | Tracer.trace("Request #" + request.getRequestId() + " is being serviced by " + provider.getName()); //$NON-NLS-1$//$NON-NLS-2$ |
| 253 | |
| 254 | // Extract the generic information |
| 255 | request.start(); |
| 256 | int nbRequested = request.getNbRequested(); |
| 257 | int nbRead = 0; |
| 258 | |
| 259 | // Initialize the execution |
| 260 | ITmfContext context = armRequest(request); |
| 261 | if (context == null) { |
| 262 | request.cancel(); |
| 263 | return; |
| 264 | } |
| 265 | |
| 266 | try { |
| 267 | // Get the ordered events |
| 268 | T data = getNext(context); |
| 269 | if (Tracer.isRequestTraced()) |
| 270 | Tracer.trace("Request #" + request.getRequestId() + " read first event"); //$NON-NLS-1$ //$NON-NLS-2$ |
| 271 | while (data != null && !isCompleted(request, data, nbRead)) { |
| 272 | if (fLogData) |
| 273 | Tracer.traceEvent(provider, request, data); |
| 274 | if (request.getDataType().isInstance(data)) { |
| 275 | request.handleData(data); |
| 276 | } |
| 277 | |
| 278 | // To avoid an unnecessary read passed the last data |
| 279 | // requested |
| 280 | if (++nbRead < nbRequested) { |
| 281 | data = getNext(context); |
| 282 | } |
| 283 | } |
| 284 | if (Tracer.isRequestTraced()) |
| 285 | Tracer.trace("Request #" + request.getRequestId() + " finished"); //$NON-NLS-1$//$NON-NLS-2$ |
| 286 | |
| 287 | if (request.isCancelled()) { |
| 288 | request.cancel(); |
| 289 | } else { |
| 290 | request.done(); |
| 291 | } |
| 292 | } catch (Exception e) { |
| 293 | request.fail(); |
| 294 | } |
| 295 | |
| 296 | // Cleanup |
| 297 | context.dispose(); |
| 298 | } |
| 299 | |
| 300 | @Override |
| 301 | public void cancel() { |
| 302 | if (!request.isCompleted()) { |
| 303 | request.cancel(); |
| 304 | } |
| 305 | } |
| 306 | }; |
| 307 | |
| 308 | if (Tracer.isRequestTraced()) |
| 309 | Tracer.traceRequest(request, "queued"); //$NON-NLS-1$ |
| 310 | fExecutor.execute(thread); |
| 311 | |
| 312 | } |
| 313 | |
| 314 | protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) { |
| 315 | |
| 316 | Thread thread = new Thread() { |
| 317 | @Override |
| 318 | public void run() { |
| 319 | request.start(); |
| 320 | |
| 321 | final Integer[] CHUNK_SIZE = new Integer[1]; |
| 322 | CHUNK_SIZE[0] = Math.min(request.getNbRequested(), blockSize + ((indexing) ? 1 : 0)); |
| 323 | |
| 324 | final Integer[] nbRead = new Integer[1]; |
| 325 | nbRead[0] = 0; |
| 326 | |
| 327 | final Boolean[] isFinished = new Boolean[1]; |
| 328 | isFinished[0] = Boolean.FALSE; |
| 329 | |
| 330 | while (!isFinished[0]) { |
| 331 | |
| 332 | TmfDataRequest<T> subRequest = new TmfDataRequest<T>(request.getDataType(), request.getIndex() |
| 333 | + nbRead[0], CHUNK_SIZE[0], blockSize, ExecutionType.BACKGROUND) { |
| 334 | @Override |
| 335 | public void handleData(T data) { |
| 336 | super.handleData(data); |
| 337 | if (request.getDataType().isInstance(data)) { |
| 338 | request.handleData(data); |
| 339 | } |
| 340 | if (getNbRead() > CHUNK_SIZE[0]) { |
| 341 | System.out.println("ERROR - Read too many events"); //$NON-NLS-1$ |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | @Override |
| 346 | public void handleCompleted() { |
| 347 | nbRead[0] += getNbRead(); |
| 348 | if (nbRead[0] >= request.getNbRequested() || (getNbRead() < CHUNK_SIZE[0])) { |
| 349 | if (this.isCancelled()) { |
| 350 | request.cancel(); |
| 351 | } else if (this.isFailed()) { |
| 352 | request.fail(); |
| 353 | } else { |
| 354 | request.done(); |
| 355 | } |
| 356 | isFinished[0] = Boolean.TRUE; |
| 357 | } |
| 358 | super.handleCompleted(); |
| 359 | } |
| 360 | }; |
| 361 | |
| 362 | if (!isFinished[0]) { |
| 363 | queueRequest(subRequest); |
| 364 | |
| 365 | try { |
| 366 | subRequest.waitForCompletion(); |
| 367 | } catch (InterruptedException e) { |
| 368 | e.printStackTrace(); |
| 369 | } |
| 370 | |
| 371 | CHUNK_SIZE[0] = Math.min(request.getNbRequested() - nbRead[0], blockSize); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | }; |
| 376 | |
| 377 | thread.start(); |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Initialize the provider based on the request. The context is provider specific and will be updated by getNext(). |
| 382 | * |
| 383 | * @param request |
| 384 | * @return an application specific context; null if request can't be serviced |
| 385 | */ |
| 386 | public abstract ITmfContext armRequest(ITmfDataRequest<T> request); |
| 387 | |
| 388 | public abstract T getNext(ITmfContext context); |
| 389 | |
| 390 | /** |
| 391 | * Checks if the data meets the request completion criteria. |
| 392 | * |
| 393 | * @param request |
| 394 | * @param data |
| 395 | * @return |
| 396 | */ |
| 397 | public boolean isCompleted(ITmfDataRequest<T> request, T data, int nbRead) { |
| 398 | return request.isCompleted() || nbRead >= request.getNbRequested(); |
| 399 | } |
| 400 | |
| 401 | // ------------------------------------------------------------------------ |
| 402 | // Signal handlers |
| 403 | // ------------------------------------------------------------------------ |
| 404 | |
| 405 | @TmfSignalHandler |
| 406 | public void startSynch(TmfStartSynchSignal signal) { |
| 407 | synchronized (fLock) { |
| 408 | fSignalDepth++; |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | @TmfSignalHandler |
| 413 | public void endSynch(TmfEndSynchSignal signal) { |
| 414 | synchronized (fLock) { |
| 415 | fSignalDepth--; |
| 416 | if (fSignalDepth == 0) { |
| 417 | fireRequest(); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | } |