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;
69 private final Object fLock
= new Object();
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
75 public TmfDataProvider(String name
, Class
<T
> type
) {
76 this(name
, type
, DEFAULT_QUEUE_SIZE
);
79 protected TmfDataProvider(String name
, Class
<T
> type
, int queueSize
) {
82 fQueueSize
= queueSize
;
83 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
85 fExecutor
= new TmfRequestExecutor();
88 fLogData
= Tracer
.isEventTraced();
89 fLogError
= Tracer
.isErrorTraced();
91 TmfProviderManager
.register(fType
, this);
92 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "started");
95 public TmfDataProvider(TmfDataProvider
<T
> other
) {
98 fQueueSize
= other
.fQueueSize
;
99 fDataQueue
= (fQueueSize
> 1) ?
new LinkedBlockingQueue
<T
>(fQueueSize
) : new SynchronousQueue
<T
>();
101 fExecutor
= new TmfRequestExecutor();
104 fLogData
= Tracer
.isEventTraced();
105 fLogError
= Tracer
.isErrorTraced();
109 public void dispose() {
110 TmfProviderManager
.deregister(fType
, this);
113 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped");
116 public int getQueueSize() {
120 public Class
<?
> getType() {
124 // ------------------------------------------------------------------------
125 // ITmfRequestHandler
126 // ------------------------------------------------------------------------
128 public void sendRequest(final ITmfDataRequest
<T
> request
) {
129 synchronized(fLock
) {
130 if (fSignalDepth
> 0) {
131 coalesceDataRequest(request
);
133 dispatchRequest(request
);
139 * This method queues the coalesced requests.
143 public void fireRequests() {
144 synchronized(fLock
) {
145 for (TmfDataRequest
<T
> request
: fPendingCoalescedRequests
) {
146 dispatchRequest(request
);
148 fPendingCoalescedRequests
.clear();
152 // ------------------------------------------------------------------------
153 // Coalescing (primitive test...)
154 // ------------------------------------------------------------------------
156 protected Vector
<TmfCoalescedDataRequest
<T
>> fPendingCoalescedRequests
= new Vector
<TmfCoalescedDataRequest
<T
>>();
158 protected void newCoalescedDataRequest(ITmfDataRequest
<T
> request
) {
159 synchronized(fLock
) {
160 TmfCoalescedDataRequest
<T
> coalescedRequest
=
161 new TmfCoalescedDataRequest
<T
>(fType
, request
.getIndex(), request
.getNbRequested(), request
.getBlockize(), request
.getExecType());
162 coalescedRequest
.addRequest(request
);
163 if (Tracer
.isRequestTraced()) {
164 Tracer
.traceRequest(request
, "coalesced with " + coalescedRequest
.getRequestId());
165 Tracer
.traceRequest(coalescedRequest
, "added " + request
.getRequestId());
167 fPendingCoalescedRequests
.add(coalescedRequest
);
171 protected void coalesceDataRequest(ITmfDataRequest
<T
> request
) {
172 synchronized(fLock
) {
173 for (TmfCoalescedDataRequest
<T
> req
: fPendingCoalescedRequests
) {
174 if (req
.isCompatible(request
)) {
175 req
.addRequest(request
);
176 if (Tracer
.isRequestTraced()) {
177 Tracer
.traceRequest(request
, "coalesced with " + req
.getRequestId());
178 Tracer
.traceRequest(req
, "added " + request
.getRequestId());
183 newCoalescedDataRequest(request
);
187 // ------------------------------------------------------------------------
188 // Request processing
189 // ------------------------------------------------------------------------
191 private void dispatchRequest(final ITmfDataRequest
<T
> request
) {
192 if (request
.getExecType() == ExecutionType
.SHORT
)
193 queueRequest(request
);
195 queueLongRequest(request
);
198 protected void queueRequest(final ITmfDataRequest
<T
> request
) {
200 if (fExecutor
.isShutdown()) {
205 final TmfDataProvider
<T
> provider
= this;
207 // Process the request
208 TmfThread thread
= new TmfThread(request
.getExecType()) {
213 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "started");
215 // Extract the generic information
217 int blockSize
= request
.getBlockize();
218 int nbRequested
= request
.getNbRequested();
220 // Create the result buffer
221 Vector
<T
> result
= new Vector
<T
>();
224 // Initialize the execution
225 ITmfContext context
= armRequest(request
);
226 if (context
== null) {
232 // Get the ordered events
233 if (Tracer
.isRequestTraced()) Tracer
.trace("Request #" + request
.getRequestId() + " is being serviced by " + provider
.getName());
234 T data
= getNext(context
);
235 if (Tracer
.isRequestTraced()) Tracer
.trace("Request #" + request
.getRequestId() + " read first event");
236 while (data
!= null && !isCompleted(request
, data
, nbRead
))
238 if (fLogData
) Tracer
.traceEvent(provider
, request
, data
);
240 if (++nbRead
% blockSize
== 0) {
241 pushData(request
, result
);
243 // To avoid an unnecessary read passed the last data requested
244 if (nbRead
< nbRequested
) {
245 data
= getNext(context
);
246 if (Tracer
.isRequestTraced() && (data
== null || data
.isNullRef())) {
247 Tracer
.trace("Request #" + request
.getRequestId() + " end of data");
251 if (result
.size() > 0) {
252 pushData(request
, result
);
256 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "completed");
258 catch (Exception e
) {
259 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "exception (failed)");
261 // e.printStackTrace();
266 fExecutor
.execute(thread
);
268 if (Tracer
.isRequestTraced()) Tracer
.traceRequest(request
, "queued");
271 // By default, same behavior as a short request
272 protected void queueLongRequest(final ITmfDataRequest
<T
> request
) {
273 queueRequest(request
);
277 * Format the result data and forwards it to the requester.
278 * Note: after handling, the data is *removed*.
283 @SuppressWarnings("unchecked")
284 protected void pushData(ITmfDataRequest
<T
> request
, Vector
<T
> data
) {
285 synchronized(request
) {
286 if (!request
.isCompleted()) {
287 T
[] result
= (T
[]) Array
.newInstance(fType
, data
.size());
288 data
.toArray(result
);
289 request
.setData(result
);
290 request
.handleData();
291 data
.removeAllElements();
297 * Initialize the provider based on the request. The context is
298 * provider specific and will be updated by getNext().
301 * @return an application specific context; null if request can't be serviced
303 public abstract ITmfContext
armRequest(ITmfDataRequest
<T
> request
);
304 public abstract T
getNext(ITmfContext context
);
306 // public abstract void queueResult(T data);
309 * Return the next piece of data based on the context supplied. The context
310 * would typically be updated for the subsequent read.
315 // private static final int TIMEOUT = 10000;
316 //// public abstract T getNext(ITmfContext context) throws InterruptedException;
317 //// private int getLevel = 0;
318 // public T getNext(ITmfContext context) throws InterruptedException {
319 //// String name = Thread.currentThread().getName(); getLevel++;
320 //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (getLevel) + " getNext() - entering");
321 // T data = fDataQueue.poll(TIMEOUT, TimeUnit.MILLISECONDS);
322 // if (data == null) {
323 //// if (Tracer.isErrorTraced()) Tracer.traceError(getName() + ": Request timeout on read");
324 // throw new InterruptedException();
326 //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (getLevel) + " getNext() - leaving");
332 // * Makes the generated result data available for getNext()
336 //// public abstract void queueResult(T data) throws InterruptedException;
337 //// private int putLevel = 0;
338 // public void queueResult(T data) throws InterruptedException {
339 //// String name = Thread.currentThread().getName(); putLevel++;
340 //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (putLevel) + " queueResult() - entering");
341 // boolean ok = fDataQueue.offer(data, TIMEOUT, TimeUnit.MILLISECONDS);
343 //// if (Tracer.isErrorTraced()) Tracer.traceError(getName() + ": Request timeout on write");
344 // throw new InterruptedException();
346 //// System.out.println("[" + System.currentTimeMillis() + "] " + name + " " + (putLevel) + " queueResult() - leaving");
351 * Checks if the data meets the request completion criteria.
357 public boolean isCompleted(ITmfDataRequest
<T
> request
, T data
, int nbRead
) {
358 return request
.isCompleted() || nbRead
>= request
.getNbRequested() || data
.isNullRef();
361 // ------------------------------------------------------------------------
363 // ------------------------------------------------------------------------
366 public void startSynch(TmfStartSynchSignal signal
) {
367 synchronized (fLock
) {
373 public void endSynch(TmfEndSynchSignal signal
) {
374 synchronized (fLock
) {
376 if (fSignalDepth
== 0) {