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
.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
;
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
;
34 * <b><u>TmfProvider</u></b>
36 * The TmfProvider<T> is a provider for a data of type <T>.
38 * This abstract class implements the housekeeking methods to register/
39 * deregister the event provider and to handle generically the event requests.
41 * The concrete class can either re-implement processRequest() entirely or
42 * just implement the hooks (initializeContext() and getNext()).
44 * TODO: Add support for providing multiple data types.
46 public abstract class TmfDataProvider
<T
extends TmfData
> extends TmfComponent
implements ITmfDataProvider
<T
> {
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 // private static final ITmfDataRequest.ExecutionType SHORT = ITmfDataRequest.ExecutionType.SHORT;
53 // private static final ITmfDataRequest.ExecutionType LONG = ITmfDataRequest.ExecutionType.LONG;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 final protected Class
<T
> fType
;
60 final protected boolean fLogData
;
61 final protected boolean fLogError
;
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
;
68 private int fSignalDepth
= 0;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
74 public TmfDataProvider(String name
, Class
<T
> type
) {
75 this(name
, type
, DEFAULT_QUEUE_SIZE
);
78 protected TmfDataProvider(String name
, Class
<T
> type
, int queueSize
) {
81 fQueueSize
= queueSize
;
82 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
84 fExecutor
= new TmfRequestExecutor();
87 fLogData
= Tracer
.isEventTraced();
88 fLogError
= Tracer
.isErrorTraced();
90 TmfProviderManager
.register(fType
, this);
91 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "started");
94 public TmfDataProvider(TmfDataProvider
<T
> other
) {
97 fQueueSize
= other
.fQueueSize
;
98 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
100 fExecutor
= new TmfRequestExecutor();
103 fLogData
= Tracer
.isEventTraced();
104 fLogError
= Tracer
.isErrorTraced();
108 public void dispose() {
109 TmfProviderManager
.deregister(fType
, this);
112 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped");
115 public int getQueueSize() {
119 public Class
<?
> getType() {
123 // ------------------------------------------------------------------------
124 // ITmfRequestHandler
125 // ------------------------------------------------------------------------
127 public void sendRequest(final ITmfDataRequest
<T
> request
) {
129 if (fSignalDepth
> 0) {
130 coalesceDataRequest(request
);
132 dispatchRequest(request
);
138 * This method queues the coalesced requests.
142 public void fireRequests() {
144 for (TmfDataRequest
<T
> request
: fPendingCoalescedRequests
) {
145 dispatchRequest(request
);
147 fPendingCoalescedRequests
.clear();
151 // ------------------------------------------------------------------------
152 // Coalescing (primitive test...)
153 // ------------------------------------------------------------------------
155 protected Vector
<TmfCoalescedDataRequest
<T
>> fPendingCoalescedRequests
= new Vector
<TmfCoalescedDataRequest
<T
>>();
157 protected void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
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());
166 fPendingCoalescedRequests
.add(coalescedRequest
);
170 protected synchronized void coalesceDataRequest(ITmfDataRequest
<T
> request
) {
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());
182 newCoalescedDataRequest(request
);
186 // ------------------------------------------------------------------------
187 // Request processing
188 // ------------------------------------------------------------------------
190 private void dispatchRequest(final ITmfDataRequest
<T
> request
) {
191 if (request
.getExecType() == ExecutionType
.SHORT
)
192 queueRequest(request
);
194 queueLongRequest(request
);
197 protected void queueRequest(final ITmfDataRequest
<T
> request
) {
199 final TmfDataProvider
<T
> provider
= this;
201 // Process the request
202 TmfThread thread
= new TmfThread(request
.getExecType()) {
207 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "started");
209 // Extract the generic information
211 int blockSize
= request
.getBlockize();
212 int nbRequested
= request
.getNbRequested();
214 // Create the result buffer
215 Vector
<T
> result
= new Vector
<T
>();
218 // Initialize the execution
219 ITmfContext context
= armRequest(request
);
220 if (context
== null) {
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
))
232 if (fLogData
) Tracer
.traceEvent(provider
, request
, data
);
234 if (++nbRead
% blockSize
== 0) {
235 pushData(request
, result
);
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");
245 if (result
.size() > 0) {
246 pushData(request
, result
);
250 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "completed");
252 catch (Exception e
) {
253 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "exception (failed)");
255 // e.printStackTrace();
260 fExecutor
.execute(thread
);
262 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "queued");
265 // By default, same behavior as a short request
266 protected void queueLongRequest(final ITmfDataRequest
<T
> request
) {
267 queueRequest(request
);
271 * Format the result data and forwards it to the requester.
272 * Note: after handling, the data is *removed*.
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();
291 * Initialize the provider based on the request. The context is
292 * provider specific and will be updated by getNext().
295 * @return an application specific context; null if request can't be serviced
297 public abstract ITmfContext
armRequest(ITmfDataRequest
<T
> request
);
298 public abstract T
getNext(ITmfContext context
);
300 // public abstract void queueResult(T data);
303 * Return the next piece of data based on the context supplied. The context
304 * would typically be updated for the subsequent read.
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();
320 //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (getLevel) + " getNext() - leaving");
326 // * Makes the generated result data available for getNext()
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);
337 //// if (Tracer.isErrorTraced()) Tracer.traceError(getName() + ": Request timeout on write");
338 // throw new InterruptedException();
340 //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (putLevel) + " queueResult() - leaving");
345 * Checks if the data meets the request completion criteria.
351 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
352 return request
.isCompleted() || nbRead
>= request
.getNbRequested() || data
.isNullRef();
355 // ------------------------------------------------------------------------
357 // ------------------------------------------------------------------------
360 public synchronized void startSynch(TmfStartSynchSignal signal
) {
365 public synchronized void endSynch(TmfEndSynchSignal signal
) {
367 if (fSignalDepth
== 0) {