1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.component
;
15 import java
.util
.Vector
;
16 import java
.util
.concurrent
.BlockingQueue
;
17 import java
.util
.concurrent
.LinkedBlockingQueue
;
18 import java
.util
.concurrent
.SynchronousQueue
;
20 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
21 import org
.eclipse
.linuxtools
.tmf
.event
.TmfData
;
22 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
;
23 import org
.eclipse
.linuxtools
.tmf
.request
.ITmfDataRequest
.ExecutionType
;
24 import org
.eclipse
.linuxtools
.tmf
.request
.TmfCoalescedDataRequest
;
25 import org
.eclipse
.linuxtools
.tmf
.request
.TmfDataRequest
;
26 import org
.eclipse
.linuxtools
.tmf
.request
.TmfRequestExecutor
;
27 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfEndSynchSignal
;
28 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfSignalHandler
;
29 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfStartSynchSignal
;
30 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfContext
;
33 * <b><u>TmfProvider</u></b>
35 * The TmfProvider<T> is a provider for a data of type <T>.
37 * This abstract class implements the housekeeking methods to register/
38 * deregister the event provider and to handle generically the event requests.
40 * The concrete class can either re-implement processRequest() entirely or
41 * just implement the hooks (initializeContext() and getNext()).
43 * TODO: Add support for providing multiple data types.
45 public abstract class TmfDataProvider
<T
extends TmfData
> extends TmfComponent
implements ITmfDataProvider
<T
> {
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 // private static final ITmfDataRequest.ExecutionType SHORT = ITmfDataRequest.ExecutionType.SHORT;
52 // private static final ITmfDataRequest.ExecutionType LONG = ITmfDataRequest.ExecutionType.LONG;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 final protected Class
<T
> fType
;
59 final protected boolean fLogData
;
60 final protected boolean fLogError
;
62 public static final int DEFAULT_BLOCK_SIZE
= 50000;
63 public static final int DEFAULT_QUEUE_SIZE
= 1000;
65 protected final int fQueueSize
;
66 protected final BlockingQueue
<T
> fDataQueue
;
67 protected final TmfRequestExecutor fExecutor
;
69 private int fSignalDepth
= 0;
70 private final Object fLock
= new Object();
72 private int fRequestPendingCounter
= 0;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
78 public TmfDataProvider(String name
, Class
<T
> type
) {
79 this(name
, type
, DEFAULT_QUEUE_SIZE
);
82 protected TmfDataProvider(String name
, Class
<T
> type
, int queueSize
) {
85 fQueueSize
= queueSize
;
86 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
88 fExecutor
= new TmfRequestExecutor();
91 fLogData
= Tracer
.isEventTraced();
92 fLogError
= Tracer
.isErrorTraced();
94 TmfProviderManager
.register(fType
, this);
95 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "started");
98 public TmfDataProvider(TmfDataProvider
<T
> other
) {
101 fQueueSize
= other
.fQueueSize
;
102 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
104 fExecutor
= new TmfRequestExecutor();
107 fLogData
= Tracer
.isEventTraced();
108 fLogError
= Tracer
.isErrorTraced();
112 public void dispose() {
113 TmfProviderManager
.deregister(fType
, this);
116 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped");
119 public int getQueueSize() {
123 public Class
<?
> getType() {
127 // ------------------------------------------------------------------------
128 // ITmfRequestHandler
129 // ------------------------------------------------------------------------
132 public void sendRequest(final ITmfDataRequest
<T
> request
) {
133 synchronized(fLock
) {
134 if (fSignalDepth
> 0) {
135 coalesceDataRequest(request
);
137 dispatchRequest(request
);
143 * This method queues the coalesced requests.
148 public void fireRequest() {
149 synchronized(fLock
) {
150 if (fRequestPendingCounter
> 0) {
153 if (fPendingCoalescedRequests
.size() > 0) {
154 for (TmfDataRequest
<T
> request
: fPendingCoalescedRequests
) {
155 dispatchRequest(request
);
157 fPendingCoalescedRequests
.clear();
163 * Increments/decrements the pending requests counters and fires
164 * the request if necessary (counter == 0). Used for coalescing
165 * requests accross multiple TmfDataProvider.
170 public void notifyPendingRequest(boolean isIncrement
) {
171 synchronized(fLock
) {
173 if (fSignalDepth
> 0) {
174 fRequestPendingCounter
++;
177 if (fRequestPendingCounter
> 0) {
178 fRequestPendingCounter
--;
181 // fire request if all pending requests are received
182 if (fRequestPendingCounter
== 0) {
189 // ------------------------------------------------------------------------
190 // Coalescing (primitive test...)
191 // ------------------------------------------------------------------------
193 protected Vector
<TmfCoalescedDataRequest
<T
>> fPendingCoalescedRequests
= new Vector
<TmfCoalescedDataRequest
<T
>>();
195 protected void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
196 synchronized(fLock
) {
197 TmfCoalescedDataRequest
<T
> coalescedRequest
= new TmfCoalescedDataRequest
<T
>(
198 fType
, request
.getIndex(), request
.getNbRequested(), request
.getBlockSize(), request
.getExecType());
199 coalescedRequest
.addRequest(request
);
200 if (Tracer
.isRequestTraced()) {
201 Tracer
.traceRequest(request
, "coalesced with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
203 fPendingCoalescedRequests
.add(coalescedRequest
);
207 protected void coalesceDataRequest(ITmfDataRequest
<T
> request
) {
208 synchronized(fLock
) {
209 for (TmfCoalescedDataRequest
<T
> coalescedRequest
: fPendingCoalescedRequests
) {
210 if (coalescedRequest
.isCompatible(request
)) {
211 coalescedRequest
.addRequest(request
);
212 if (Tracer
.isRequestTraced()) {
213 Tracer
.traceRequest(request
, "coalesced with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
218 newCoalescedDataRequest(request
);
222 // ------------------------------------------------------------------------
223 // Request processing
224 // ------------------------------------------------------------------------
226 private void dispatchRequest(final ITmfDataRequest
<T
> request
) {
227 if (request
.getExecType() == ExecutionType
.FOREGROUND
)
228 queueRequest(request
);
230 queueBackgroundRequest(request
, request
.getBlockSize(), true);
233 protected void queueRequest(final ITmfDataRequest
<T
> request
) {
235 if (fExecutor
.isShutdown()) {
240 final TmfDataProvider
<T
> provider
= this;
242 // Process the request
243 TmfThread thread
= new TmfThread(request
.getExecType()) {
248 if (Tracer
.isRequestTraced()) Tracer
.trace("Request #" + request
.getRequestId() + " is being serviced by " + provider
.getName()); //$NON-NLS-1$//$NON-NLS-2$
250 // Extract the generic information
252 int nbRequested
= request
.getNbRequested();
255 // Initialize the execution
256 ITmfContext context
= armRequest(request
);
257 if (context
== null) {
263 // Get the ordered events
264 T data
= getNext(context
);
265 if (Tracer
.isRequestTraced()) Tracer
.trace("Request #" + request
.getRequestId() + " read first event"); //$NON-NLS-1$ //$NON-NLS-2$
266 while (data
!= null && !isCompleted(request
, data
, nbRead
))
268 if (fLogData
) Tracer
.traceEvent(provider
, request
, data
);
269 request
.handleData(data
);
271 // To avoid an unnecessary read passed the last data requested
272 if (++nbRead
< nbRequested
) {
273 data
= getNext(context
);
276 if (Tracer
.isRequestTraced()) Tracer
.trace("Request #" + request
.getRequestId() + " finished"); //$NON-NLS-1$//$NON-NLS-2$
278 if (request
.isCancelled()) {
285 catch (Exception e
) {
291 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "queued"); //$NON-NLS-1$
292 fExecutor
.execute(thread
);
296 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
298 Thread thread
= new Thread() {
303 final Integer
[] CHUNK_SIZE
= new Integer
[1];
304 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
306 final Integer
[] nbRead
= new Integer
[1];
309 final Boolean
[] isFinished
= new Boolean
[1];
310 isFinished
[0] = Boolean
.FALSE
;
312 while (!isFinished
[0]) {
314 TmfDataRequest
<T
> subRequest
= new TmfDataRequest
<T
>(request
.getDataType(), request
.getIndex() + nbRead
[0], CHUNK_SIZE
[0], blockSize
, ExecutionType
.BACKGROUND
)
317 public void handleData(T data
) {
318 super.handleData(data
);
319 request
.handleData(data
);
320 if (getNbRead() > CHUNK_SIZE
[0]) {
321 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
326 public void handleCompleted() {
327 nbRead
[0] += getNbRead();
328 if (nbRead
[0] >= request
.getNbRequested() || (getNbRead() < CHUNK_SIZE
[0])) {
335 isFinished
[0] = Boolean
.TRUE
;
337 super.handleCompleted();
341 if (!isFinished
[0]) {
342 queueRequest(subRequest
);
345 subRequest
.waitForCompletion();
346 } catch (InterruptedException e
) {
350 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);
360 * Initialize the provider based on the request. The context is
361 * provider specific and will be updated by getNext().
364 * @return an application specific context; null if request can't be serviced
366 public abstract ITmfContext
armRequest(ITmfDataRequest
<T
> request
);
367 public abstract T
getNext(ITmfContext context
);
370 * Checks if the data meets the request completion criteria.
376 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
377 return request
.isCompleted() || nbRead
>= request
.getNbRequested() || data
.isNullRef();
380 // ------------------------------------------------------------------------
382 // ------------------------------------------------------------------------
385 public void startSynch(TmfStartSynchSignal signal
) {
386 synchronized (fLock
) {
392 public void endSynch(TmfEndSynchSignal signal
) {
393 synchronized (fLock
) {
395 if (fSignalDepth
== 0) {