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 just
41 * 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 the request
164 * if necessary (counter == 0). Used for coalescing requests accross
165 * 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
>(fType
, request
.getIndex(), request
.getNbRequested(),
198 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())
249 Tracer
.trace("Request #" + request
.getRequestId() + " is being serviced by " + provider
.getName()); //$NON-NLS-1$//$NON-NLS-2$
251 // Extract the generic information
253 int nbRequested
= request
.getNbRequested();
256 // Initialize the execution
257 ITmfContext context
= armRequest(request
);
258 if (context
== null) {
264 // Get the ordered events
265 T data
= getNext(context
);
266 if (Tracer
.isRequestTraced())
267 Tracer
.trace("Request #" + request
.getRequestId() + " read first event"); //$NON-NLS-1$ //$NON-NLS-2$
268 while (data
!= null && !isCompleted(request
, data
, nbRead
)) {
270 Tracer
.traceEvent(provider
, request
, data
);
271 request
.handleData(data
);
273 // To avoid an unnecessary read passed the last data
275 if (++nbRead
< nbRequested
) {
276 data
= getNext(context
);
279 if (Tracer
.isRequestTraced())
280 Tracer
.trace("Request #" + request
.getRequestId() + " finished"); //$NON-NLS-1$//$NON-NLS-2$
282 if (request
.isCancelled()) {
287 } catch (Exception e
) {
296 if (Tracer
.isRequestTraced())
297 Tracer
.traceRequest(request
, "queued"); //$NON-NLS-1$
298 fExecutor
.execute(thread
);
302 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
304 Thread thread
= new Thread() {
309 final Integer
[] CHUNK_SIZE
= new Integer
[1];
310 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
312 final Integer
[] nbRead
= new Integer
[1];
315 final Boolean
[] isFinished
= new Boolean
[1];
316 isFinished
[0] = Boolean
.FALSE
;
318 while (!isFinished
[0]) {
320 TmfDataRequest
<T
> subRequest
= new TmfDataRequest
<T
>(request
.getDataType(), request
.getIndex() + nbRead
[0], CHUNK_SIZE
[0],
321 blockSize
, ExecutionType
.BACKGROUND
) {
323 public void handleData(T data
) {
324 super.handleData(data
);
325 request
.handleData(data
);
326 if (getNbRead() > CHUNK_SIZE
[0]) {
327 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
332 public void handleCompleted() {
333 nbRead
[0] += getNbRead();
334 if (nbRead
[0] >= request
.getNbRequested() || (getNbRead() < CHUNK_SIZE
[0])) {
335 if (this.isCancelled()) {
337 } else if (this.isFailed()) {
342 isFinished
[0] = Boolean
.TRUE
;
344 super.handleCompleted();
348 if (!isFinished
[0]) {
349 queueRequest(subRequest
);
352 subRequest
.waitForCompletion();
353 } catch (InterruptedException e
) {
357 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);
367 * Initialize the provider based on the request. The context is provider
368 * specific and will be updated by getNext().
371 * @return an application specific context; null if request can't be
374 public abstract ITmfContext
armRequest(ITmfDataRequest
<T
> request
);
376 public abstract T
getNext(ITmfContext context
);
379 * Checks if the data meets the request completion criteria.
385 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
386 return request
.isCompleted() || nbRead
>= request
.getNbRequested() || data
.isNullRef();
389 // ------------------------------------------------------------------------
391 // ------------------------------------------------------------------------
394 public void startSynch(TmfStartSynchSignal signal
) {
395 synchronized (fLock
) {
401 public void endSynch(TmfEndSynchSignal signal
) {
402 synchronized (fLock
) {
404 if (fSignalDepth
== 0) {