| 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.component; |
| 14 | |
| 15 | import java.lang.reflect.Array; |
| 16 | import java.util.Vector; |
| 17 | import java.util.concurrent.BlockingQueue; |
| 18 | import java.util.concurrent.LinkedBlockingQueue; |
| 19 | import java.util.concurrent.SynchronousQueue; |
| 20 | |
| 21 | import org.eclipse.linuxtools.tmf.Tracer; |
| 22 | import org.eclipse.linuxtools.tmf.event.TmfData; |
| 23 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
| 24 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType; |
| 25 | import org.eclipse.linuxtools.tmf.request.TmfCoalescedDataRequest; |
| 26 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; |
| 27 | import org.eclipse.linuxtools.tmf.request.TmfRequestExecutor; |
| 28 | import org.eclipse.linuxtools.tmf.signal.TmfEndSynchSignal; |
| 29 | import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler; |
| 30 | import org.eclipse.linuxtools.tmf.signal.TmfStartSynchSignal; |
| 31 | import org.eclipse.linuxtools.tmf.trace.ITmfContext; |
| 32 | |
| 33 | /** |
| 34 | * <b><u>TmfProvider</u></b> |
| 35 | * <p> |
| 36 | * The TmfProvider<T> is a provider for a data of type <T>. |
| 37 | * <p> |
| 38 | * This abstract class implements the housekeeking methods to register/ |
| 39 | * deregister the event provider and to handle generically the event requests. |
| 40 | * <p> |
| 41 | * The concrete class can either re-implement processRequest() entirely or |
| 42 | * just implement the hooks (initializeContext() and getNext()). |
| 43 | * <p> |
| 44 | * TODO: Add support for providing multiple data types. |
| 45 | */ |
| 46 | public abstract class TmfDataProvider<T extends TmfData> extends TmfComponent implements ITmfDataProvider<T> { |
| 47 | |
| 48 | // ------------------------------------------------------------------------ |
| 49 | // Constants |
| 50 | // ------------------------------------------------------------------------ |
| 51 | |
| 52 | // private static final ITmfDataRequest.ExecutionType SHORT = ITmfDataRequest.ExecutionType.SHORT; |
| 53 | // private static final ITmfDataRequest.ExecutionType LONG = ITmfDataRequest.ExecutionType.LONG; |
| 54 | |
| 55 | // ------------------------------------------------------------------------ |
| 56 | // |
| 57 | // ------------------------------------------------------------------------ |
| 58 | |
| 59 | final protected Class<T> fType; |
| 60 | final protected boolean fLogData; |
| 61 | final protected boolean fLogError; |
| 62 | |
| 63 | public static final int DEFAULT_QUEUE_SIZE = 1000; |
| 64 | protected final int fQueueSize; |
| 65 | protected final BlockingQueue<T> fDataQueue; |
| 66 | protected final TmfRequestExecutor fExecutor; |
| 67 | |
| 68 | private int fSignalDepth = 0; |
| 69 | |
| 70 | // ------------------------------------------------------------------------ |
| 71 | // Constructors |
| 72 | // ------------------------------------------------------------------------ |
| 73 | |
| 74 | public TmfDataProvider(String name, Class<T> type) { |
| 75 | this(name, type, DEFAULT_QUEUE_SIZE); |
| 76 | } |
| 77 | |
| 78 | protected TmfDataProvider(String name, Class<T> type, int queueSize) { |
| 79 | super(name); |
| 80 | fType = type; |
| 81 | fQueueSize = queueSize; |
| 82 | fDataQueue = (fQueueSize > 1) ? new LinkedBlockingQueue<T>(fQueueSize) : new SynchronousQueue<T>(); |
| 83 | |
| 84 | fExecutor = new TmfRequestExecutor(); |
| 85 | fSignalDepth = 0; |
| 86 | |
| 87 | fLogData = Tracer.isEventTraced(); |
| 88 | fLogError = Tracer.isErrorTraced(); |
| 89 | |
| 90 | TmfProviderManager.register(fType, this); |
| 91 | // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "started"); |
| 92 | } |
| 93 | |
| 94 | public TmfDataProvider(TmfDataProvider<T> other) { |
| 95 | super(other); |
| 96 | fType = other.fType; |
| 97 | fQueueSize = other.fQueueSize; |
| 98 | fDataQueue = (fQueueSize > 1) ? new LinkedBlockingQueue<T>(fQueueSize) : new SynchronousQueue<T>(); |
| 99 | |
| 100 | fExecutor = new TmfRequestExecutor(); |
| 101 | fSignalDepth = 0; |
| 102 | |
| 103 | fLogData = Tracer.isEventTraced(); |
| 104 | fLogError = Tracer.isErrorTraced(); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public void dispose() { |
| 109 | TmfProviderManager.deregister(fType, this); |
| 110 | fExecutor.stop(); |
| 111 | super.dispose(); |
| 112 | // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped"); |
| 113 | } |
| 114 | |
| 115 | public int getQueueSize() { |
| 116 | return fQueueSize; |
| 117 | } |
| 118 | |
| 119 | public Class<?> getType() { |
| 120 | return fType; |
| 121 | } |
| 122 | |
| 123 | // ------------------------------------------------------------------------ |
| 124 | // ITmfRequestHandler |
| 125 | // ------------------------------------------------------------------------ |
| 126 | |
| 127 | public void sendRequest(final ITmfDataRequest<T> request) { |
| 128 | synchronized(this) { |
| 129 | if (fSignalDepth > 0) { |
| 130 | coalesceDataRequest(request); |
| 131 | } else { |
| 132 | dispatchRequest(request); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * This method queues the coalesced requests. |
| 139 | * |
| 140 | * @param thread |
| 141 | */ |
| 142 | public void fireRequests() { |
| 143 | synchronized(this) { |
| 144 | for (TmfDataRequest<T> request : fPendingCoalescedRequests) { |
| 145 | dispatchRequest(request); |
| 146 | } |
| 147 | fPendingCoalescedRequests.clear(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | // ------------------------------------------------------------------------ |
| 152 | // Coalescing (primitive test...) |
| 153 | // ------------------------------------------------------------------------ |
| 154 | |
| 155 | protected Vector<TmfCoalescedDataRequest<T>> fPendingCoalescedRequests = new Vector<TmfCoalescedDataRequest<T>>(); |
| 156 | |
| 157 | protected void newCoalescedDataRequest(ITmfDataRequest<T> request) { |
| 158 | synchronized(this) { |
| 159 | TmfCoalescedDataRequest<T> coalescedRequest = |
| 160 | new TmfCoalescedDataRequest<T>(fType, request.getIndex(), request.getNbRequested(), request.getBlockize(), request.getExecType()); |
| 161 | coalescedRequest.addRequest(request); |
| 162 | if (Tracer.isRequestTraced()) { |
| 163 | Tracer.traceRequest(request, "coalesced with " + coalescedRequest.getRequestId()); |
| 164 | Tracer.traceRequest(coalescedRequest, "added " + request.getRequestId()); |
| 165 | } |
| 166 | fPendingCoalescedRequests.add(coalescedRequest); |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | protected synchronized void coalesceDataRequest(ITmfDataRequest<T> request) { |
| 171 | synchronized(this) { |
| 172 | for (TmfCoalescedDataRequest<T> req : fPendingCoalescedRequests) { |
| 173 | if (req.isCompatible(request)) { |
| 174 | req.addRequest(request); |
| 175 | if (Tracer.isRequestTraced()) { |
| 176 | Tracer.traceRequest(request, "coalesced with " + req.getRequestId()); |
| 177 | Tracer.traceRequest(req, "added " + request.getRequestId()); |
| 178 | } |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | newCoalescedDataRequest(request); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // ------------------------------------------------------------------------ |
| 187 | // Request processing |
| 188 | // ------------------------------------------------------------------------ |
| 189 | |
| 190 | private void dispatchRequest(final ITmfDataRequest<T> request) { |
| 191 | if (request.getExecType() == ExecutionType.SHORT) |
| 192 | queueRequest(request); |
| 193 | else |
| 194 | queueLongRequest(request); |
| 195 | } |
| 196 | |
| 197 | protected void queueRequest(final ITmfDataRequest<T> request) { |
| 198 | |
| 199 | final TmfDataProvider<T> provider = this; |
| 200 | |
| 201 | // Process the request |
| 202 | TmfThread thread = new TmfThread(request.getExecType()) { |
| 203 | |
| 204 | @Override |
| 205 | public void run() { |
| 206 | |
| 207 | if (Tracer.isRequestTraced()) Tracer.traceRequest(request, "started"); |
| 208 | |
| 209 | // Extract the generic information |
| 210 | request.start(); |
| 211 | int blockSize = request.getBlockize(); |
| 212 | int nbRequested = request.getNbRequested(); |
| 213 | |
| 214 | // Create the result buffer |
| 215 | Vector<T> result = new Vector<T>(); |
| 216 | int nbRead = 0; |
| 217 | |
| 218 | // Initialize the execution |
| 219 | ITmfContext context = armRequest(request); |
| 220 | if (context == null) { |
| 221 | request.cancel(); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | try { |
| 226 | // Get the ordered events |
| 227 | if (Tracer.isRequestTraced()) Tracer.trace("Request #" + request.getRequestId() + " is being serviced by " + provider.getName()); |
| 228 | T data = getNext(context); |
| 229 | if (Tracer.isRequestTraced()) Tracer.trace("Request #" + request.getRequestId() + " read first event"); |
| 230 | while (data != null && !isCompleted(request, data, nbRead)) |
| 231 | { |
| 232 | if (fLogData) Tracer.traceEvent(provider, request, data); |
| 233 | result.add(data); |
| 234 | if (++nbRead % blockSize == 0) { |
| 235 | pushData(request, result); |
| 236 | } |
| 237 | // To avoid an unnecessary read passed the last data requested |
| 238 | if (nbRead < nbRequested) { |
| 239 | data = getNext(context); |
| 240 | if (Tracer.isRequestTraced() && (data == null || data.isNullRef())) { |
| 241 | Tracer.trace("Request #" + request.getRequestId() + " end of data"); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | if (result.size() > 0) { |
| 246 | pushData(request, result); |
| 247 | } |
| 248 | request.done(); |
| 249 | |
| 250 | if (Tracer.isRequestTraced()) Tracer.traceRequest(request, "completed"); |
| 251 | } |
| 252 | catch (Exception e) { |
| 253 | if (Tracer.isRequestTraced()) Tracer.traceRequest(request, "exception (failed)"); |
| 254 | request.fail(); |
| 255 | // e.printStackTrace(); |
| 256 | } |
| 257 | } |
| 258 | }; |
| 259 | |
| 260 | fExecutor.execute(thread); |
| 261 | |
| 262 | if (Tracer.isRequestTraced()) Tracer.traceRequest(request, "queued"); |
| 263 | } |
| 264 | |
| 265 | // By default, same behavior as a short request |
| 266 | protected void queueLongRequest(final ITmfDataRequest<T> request) { |
| 267 | queueRequest(request); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Format the result data and forwards it to the requester. |
| 272 | * Note: after handling, the data is *removed*. |
| 273 | * |
| 274 | * @param request |
| 275 | * @param data |
| 276 | */ |
| 277 | @SuppressWarnings("unchecked") |
| 278 | protected void pushData(ITmfDataRequest<T> request, Vector<T> data) { |
| 279 | synchronized(request) { |
| 280 | if (!request.isCompleted()) { |
| 281 | T[] result = (T[]) Array.newInstance(fType, data.size()); |
| 282 | data.toArray(result); |
| 283 | request.setData(result); |
| 284 | request.handleData(); |
| 285 | data.removeAllElements(); |
| 286 | } |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Initialize the provider based on the request. The context is |
| 292 | * provider specific and will be updated by getNext(). |
| 293 | * |
| 294 | * @param request |
| 295 | * @return an application specific context; null if request can't be serviced |
| 296 | */ |
| 297 | public abstract ITmfContext armRequest(ITmfDataRequest<T> request); |
| 298 | public abstract T getNext(ITmfContext context); |
| 299 | |
| 300 | // public abstract void queueResult(T data); |
| 301 | |
| 302 | /** |
| 303 | * Return the next piece of data based on the context supplied. The context |
| 304 | * would typically be updated for the subsequent read. |
| 305 | * |
| 306 | * @param context |
| 307 | * @return |
| 308 | */ |
| 309 | // private static final int TIMEOUT = 10000; |
| 310 | //// public abstract T getNext(ITmfContext context) throws InterruptedException; |
| 311 | //// private int getLevel = 0; |
| 312 | // public T getNext(ITmfContext context) throws InterruptedException { |
| 313 | //// String name = Thread.currentThread().getName(); getLevel++; |
| 314 | //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (getLevel) + " getNext() - entering"); |
| 315 | // T data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS); |
| 316 | // if (data == null) { |
| 317 | //// if (Tracer.isErrorTraced()) Tracer.traceError(getName() + ": Request timeout on read"); |
| 318 | // throw new InterruptedException(); |
| 319 | // } |
| 320 | //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (getLevel) + " getNext() - leaving"); |
| 321 | //// getLevel--; |
| 322 | // return data; |
| 323 | // } |
| 324 | // |
| 325 | // /** |
| 326 | // * Makes the generated result data available for getNext() |
| 327 | // * |
| 328 | // * @param data |
| 329 | // */ |
| 330 | //// public abstract void queueResult(T data) throws InterruptedException; |
| 331 | //// private int putLevel = 0; |
| 332 | // public void queueResult(T data) throws InterruptedException { |
| 333 | //// String name = Thread.currentThread().getName(); putLevel++; |
| 334 | //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (putLevel) + " queueResult() - entering"); |
| 335 | // boolean ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS); |
| 336 | // if (!ok) { |
| 337 | //// if (Tracer.isErrorTraced()) Tracer.traceError(getName() + ": Request timeout on write"); |
| 338 | // throw new InterruptedException(); |
| 339 | // } |
| 340 | //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (putLevel) + " queueResult() - leaving"); |
| 341 | //// putLevel--; |
| 342 | // } |
| 343 | |
| 344 | /** |
| 345 | * Checks if the data meets the request completion criteria. |
| 346 | * |
| 347 | * @param request |
| 348 | * @param data |
| 349 | * @return |
| 350 | */ |
| 351 | public boolean isCompleted(ITmfDataRequest<T> request, T data, int nbRead) { |
| 352 | return request.isCompleted() || nbRead >= request.getNbRequested() || data.isNullRef(); |
| 353 | } |
| 354 | |
| 355 | // ------------------------------------------------------------------------ |
| 356 | // Signal handlers |
| 357 | // ------------------------------------------------------------------------ |
| 358 | |
| 359 | @TmfSignalHandler |
| 360 | public synchronized void startSynch(TmfStartSynchSignal signal) { |
| 361 | fSignalDepth++; |
| 362 | } |
| 363 | |
| 364 | @TmfSignalHandler |
| 365 | public synchronized void endSynch(TmfEndSynchSignal signal) { |
| 366 | fSignalDepth--; |
| 367 | if (fSignalDepth == 0) { |
| 368 | fireRequests(); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | } |