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
.core
.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
.internal
.tmf
.core
.Tracer
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
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
;
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/ deregister the event provider and to handle
38 * generically the event requests.
40 * The concrete class can either re-implement processRequest() entirely or just implement the hooks (initializeContext()
43 * TODO: Add support for providing multiple data types.
45 public abstract class TmfDataProvider
<T
extends ITmfEvent
> extends TmfComponent
implements ITmfDataProvider
<T
> {
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 public static final int DEFAULT_BLOCK_SIZE
= 50000;
52 public static final int DEFAULT_QUEUE_SIZE
= 1000;
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 protected Class
<T
> fType
;
59 protected boolean fLogData
;
60 protected boolean fLogError
;
62 protected int fQueueSize
= DEFAULT_QUEUE_SIZE
;
63 protected BlockingQueue
<T
> fDataQueue
;
64 protected TmfRequestExecutor fExecutor
;
66 private int fSignalDepth
= 0;
67 private final Object fLock
= new Object();
69 private int fRequestPendingCounter
= 0;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 public TmfDataProvider() {
77 fQueueSize
= DEFAULT_QUEUE_SIZE
;
78 fDataQueue
= new LinkedBlockingQueue
<T
>(fQueueSize
);
79 fExecutor
= new TmfRequestExecutor();
82 public void init(String name
, Class
<T
> type
) {
85 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
87 fExecutor
= new TmfRequestExecutor();
90 fLogData
= Tracer
.isEventTraced();
91 fLogError
= Tracer
.isErrorTraced();
93 TmfProviderManager
.register(fType
, this);
96 protected TmfDataProvider(String name
, Class
<T
> type
, int queueSize
) {
98 fQueueSize
= queueSize
;
102 public TmfDataProvider(TmfDataProvider
<T
> other
) {
104 init(other
.getName(), other
.fType
);
107 public TmfDataProvider(String name
, Class
<T
> type
) {
108 this(name
, type
, DEFAULT_QUEUE_SIZE
);
112 public void dispose() {
113 TmfProviderManager
.deregister(fType
, this);
116 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped");
119 // ------------------------------------------------------------------------
121 // ------------------------------------------------------------------------
123 public int getQueueSize() {
127 public Class
<?
> getType() {
131 // ------------------------------------------------------------------------
132 // ITmfRequestHandler
133 // ------------------------------------------------------------------------
136 public void sendRequest(final ITmfDataRequest
<T
> request
) {
137 synchronized (fLock
) {
138 if (fSignalDepth
> 0) {
139 coalesceDataRequest(request
);
141 dispatchRequest(request
);
147 * This method queues the coalesced requests.
150 public void fireRequest() {
151 synchronized (fLock
) {
152 if (fRequestPendingCounter
> 0) {
155 if (fPendingCoalescedRequests
.size() > 0) {
156 for (TmfDataRequest
<T
> request
: fPendingCoalescedRequests
) {
157 dispatchRequest(request
);
159 fPendingCoalescedRequests
.clear();
165 * Increments/decrements the pending requests counters and fires the request if necessary (counter == 0). Used for
166 * coalescing requests accross multiple TmfDataProvider.
171 public void notifyPendingRequest(boolean isIncrement
) {
172 synchronized (fLock
) {
174 if (fSignalDepth
> 0) {
175 fRequestPendingCounter
++;
178 if (fRequestPendingCounter
> 0) {
179 fRequestPendingCounter
--;
182 // fire request if all pending requests are received
183 if (fRequestPendingCounter
== 0) {
190 // ------------------------------------------------------------------------
191 // Coalescing (primitive test...)
192 // ------------------------------------------------------------------------
194 protected Vector
<TmfCoalescedDataRequest
<T
>> fPendingCoalescedRequests
= new Vector
<TmfCoalescedDataRequest
<T
>>();
196 protected void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
197 synchronized (fLock
) {
198 TmfCoalescedDataRequest
<T
> coalescedRequest
= new TmfCoalescedDataRequest
<T
>(request
.getDataType(), request
.getIndex(),
199 request
.getNbRequested(), request
.getBlockSize(), request
.getExecType());
200 coalescedRequest
.addRequest(request
);
201 if (Tracer
.isRequestTraced()) {
202 Tracer
.traceRequest(request
, "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
203 Tracer
.traceRequest(coalescedRequest
, "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
205 fPendingCoalescedRequests
.add(coalescedRequest
);
209 protected void coalesceDataRequest(ITmfDataRequest
<T
> request
) {
210 synchronized (fLock
) {
211 for (TmfCoalescedDataRequest
<T
> coalescedRequest
: fPendingCoalescedRequests
) {
212 if (coalescedRequest
.isCompatible(request
)) {
213 coalescedRequest
.addRequest(request
);
214 if (Tracer
.isRequestTraced()) {
215 Tracer
.traceRequest(request
, "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
216 Tracer
.traceRequest(coalescedRequest
, "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
221 newCoalescedDataRequest(request
);
225 // ------------------------------------------------------------------------
226 // Request processing
227 // ------------------------------------------------------------------------
229 private void dispatchRequest(final ITmfDataRequest
<T
> request
) {
230 if (request
.getExecType() == ExecutionType
.FOREGROUND
)
231 queueRequest(request
);
233 queueBackgroundRequest(request
, request
.getBlockSize(), true);
236 protected void queueRequest(final ITmfDataRequest
<T
> request
) {
238 if (fExecutor
.isShutdown()) {
243 final TmfDataProvider
<T
> provider
= this;
245 // Process the request
246 TmfThread thread
= new TmfThread(request
.getExecType()) {
251 if (Tracer
.isRequestTraced()) {
252 Tracer
.traceRequest(request
, "is being serviced by " + provider
.getName()); //$NON-NLS-1$
255 // Extract the generic information
257 int nbRequested
= request
.getNbRequested();
260 // Initialize the execution
261 ITmfContext context
= armRequest(request
);
262 if (context
== null) {
268 // Get the ordered events
269 T data
= getNext(context
);
270 if (Tracer
.isRequestTraced())
271 Tracer
.traceRequest(request
, "read first event"); //$NON-NLS-1$
272 while (data
!= null && !isCompleted(request
, data
, nbRead
)) {
274 // Tracer.traceEvent(provider, request, data);
275 if (request
.getDataType().isInstance(data
)) {
276 request
.handleData(data
);
279 // To avoid an unnecessary read passed the last data
281 if (++nbRead
< nbRequested
) {
282 data
= getNext(context
);
285 if (Tracer
.isRequestTraced())
286 Tracer
.traceRequest(request
, "COMPLETED"); //$NON-NLS-1$
288 if (request
.isCancelled()) {
293 } catch (Exception e
) {
302 public void cancel() {
303 if (!request
.isCompleted()) {
309 if (Tracer
.isRequestTraced())
310 Tracer
.traceRequest(request
, "QUEUED"); //$NON-NLS-1$
311 fExecutor
.execute(thread
);
315 protected void queueBackgroundRequest(final ITmfDataRequest
<T
> request
, final int blockSize
, final boolean indexing
) {
317 final TmfDataProvider
<T
> provider
= this;
319 Thread thread
= new Thread() {
323 if (Tracer
.isRequestTraced()) {
324 Tracer
.traceRequest(request
, "is being serviced by " + provider
.getName()); //$NON-NLS-1$
329 final Integer
[] CHUNK_SIZE
= new Integer
[1];
330 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested(), blockSize
+ ((indexing
) ?
1 : 0));
332 final Integer
[] nbRead
= new Integer
[1];
335 final Boolean
[] isFinished
= new Boolean
[1];
336 isFinished
[0] = Boolean
.FALSE
;
338 while (!isFinished
[0]) {
340 TmfDataRequest
<T
> subRequest
= new TmfDataRequest
<T
>(request
.getDataType(), request
.getIndex()
341 + nbRead
[0], CHUNK_SIZE
[0], blockSize
, ExecutionType
.BACKGROUND
) {
343 public void handleData(T data
) {
344 super.handleData(data
);
345 if (request
.getDataType().isInstance(data
)) {
346 request
.handleData(data
);
348 if (getNbRead() > CHUNK_SIZE
[0]) {
349 System
.out
.println("ERROR - Read too many events"); //$NON-NLS-1$
354 public void handleCompleted() {
355 nbRead
[0] += getNbRead();
356 if (nbRead
[0] >= request
.getNbRequested() || (getNbRead() < CHUNK_SIZE
[0])) {
357 if (this.isCancelled()) {
359 } else if (this.isFailed()) {
364 isFinished
[0] = Boolean
.TRUE
;
366 super.handleCompleted();
370 if (!isFinished
[0]) {
371 queueRequest(subRequest
);
374 subRequest
.waitForCompletion();
375 } catch (InterruptedException e
) {
379 CHUNK_SIZE
[0] = Math
.min(request
.getNbRequested() - nbRead
[0], blockSize
);
389 * Initialize the provider based on the request. The context is provider
390 * specific and will be updated by getNext().
393 * @return an application specific context; null if request can't be serviced
395 public abstract ITmfContext
armRequest(ITmfDataRequest
<T
> request
);
398 // * Return the next event based on the context supplied. The context
399 // * will be updated for the subsequent read.
401 // * @param context the trace read context (updated)
402 // * @return the event referred to by context
404 // public abstract T getNext(ITmfContext context);
407 * Checks if the data meets the request completion criteria.
409 * @param request the request
410 * @param data the data to verify
411 * @param nbRead the number of events read so far
412 * @return true if completion criteria is met
414 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
415 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
418 // ------------------------------------------------------------------------
420 // ------------------------------------------------------------------------
423 public void startSynch(TmfStartSynchSignal signal
) {
424 synchronized (fLock
) {
430 public void endSynch(TmfEndSynchSignal signal
) {
431 synchronized (fLock
) {
433 if (fSignalDepth
== 0) {