1 /*******************************************************************************
2 * Copyright (c) 2009, 2015 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, replace background
11 * requests by preemptable requests
12 * Alexandre Montplaisir - Merge with TmfDataProvider
13 * Bernd Hufmann - Add timer based coalescing for background requests
14 *******************************************************************************/
16 package org
.eclipse
.tracecompass
.tmf
.core
.component
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Collections
;
20 import java
.util
.Iterator
;
21 import java
.util
.LinkedList
;
22 import java
.util
.List
;
23 import java
.util
.Timer
;
24 import java
.util
.TimerTask
;
26 import org
.eclipse
.jdt
.annotation
.NonNull
;
27 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.TmfCoreTracer
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.component
.TmfEventThread
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.component
.TmfProviderManager
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.request
.TmfCoalescedEventRequest
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.request
.TmfRequestExecutor
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEndSynchSignal
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfStartSynchSignal
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
44 * An abstract base class that implements ITmfEventProvider.
46 * This abstract class implements the housekeeping methods to register/
47 * de-register the event provider and to handle generically the event requests.
50 * @author Francois Chouinard
52 public abstract class TmfEventProvider
extends TmfComponent
implements ITmfEventProvider
, ITmfFilter
{
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 /** Default amount of events per request "chunk" */
59 public static final int DEFAULT_BLOCK_SIZE
= 50000;
61 /** Delay for coalescing background requests (in milli-seconds) */
62 private static final long DELAY
= 1000;
64 // ------------------------------------------------------------------------
66 // ------------------------------------------------------------------------
68 /** List of coalesced requests */
69 private final List
<TmfCoalescedEventRequest
> fPendingCoalescedRequests
= new LinkedList
<>();
71 /** The type of event handled by this provider */
72 private Class
<?
extends ITmfEvent
> fType
;
74 private final TmfRequestExecutor fExecutor
;
76 private final Object fLock
= new Object();
78 private int fSignalDepth
= 0;
80 private int fRequestPendingCounter
= 0;
84 /** Current timer task */
85 @NonNull private TimerTask fCurrentTask
= new TimerTask() { @Override public void run() {} };
87 private boolean fIsTimerEnabled
;
90 * The parent event provider.
92 private TmfEventProvider fParent
= null;
94 * The list if children event provider.
96 private final List
<TmfEventProvider
> fChildren
= Collections
.synchronizedList(new ArrayList
<TmfEventProvider
>());
98 // ------------------------------------------------------------------------
100 // ------------------------------------------------------------------------
103 * Default constructor
105 public TmfEventProvider() {
107 setTimerEnabled(true);
108 fExecutor
= new TmfRequestExecutor();
112 * Standard constructor. Instantiate and initialize at the same time.
115 * Name of the provider
117 * The type of events that will be handled
119 public TmfEventProvider(String name
, Class
<?
extends ITmfEvent
> type
) {
125 * Initialize this data provider
128 * Name of the provider
130 * The type of events that will be handled
132 public void init(String name
, Class
<?
extends ITmfEvent
> type
) {
139 synchronized (fLock
) {
140 fTimer
= new Timer();
143 TmfProviderManager
.register(fType
, this);
147 public void dispose() {
148 TmfProviderManager
.deregister(fType
, this);
150 synchronized (fLock
) {
151 if (fTimer
!= null) {
157 synchronized (fChildren
) {
158 for (TmfEventProvider child
: fChildren
) {
163 clearPendingRequests();
167 // ------------------------------------------------------------------------
169 // ------------------------------------------------------------------------
175 public Class
<?
extends ITmfEvent
> getEventType() {
179 // ------------------------------------------------------------------------
180 // ITmfRequestHandler
181 // ------------------------------------------------------------------------
184 public void sendRequest(final ITmfEventRequest request
) {
185 synchronized (fLock
) {
187 if (TmfCoreTracer
.isRequestTraced()) {
188 TmfCoreTracer
.traceRequest(request
.getRequestId(), "SENT to provider " + getName()); //$NON-NLS-1$
191 if (request
.getProviderFilter() == null) {
192 request
.setProviderFilter(this);
195 if (sendWithParent(request
)) {
199 if (request
.getExecType() == ExecutionType
.FOREGROUND
) {
200 if ((fSignalDepth
> 0) || (fRequestPendingCounter
> 0)) {
201 coalesceEventRequest(request
);
203 queueRequest(request
);
209 * Dispatch request in case timer is not running.
211 if (fTimer
== null) {
212 queueRequest(request
);
216 coalesceEventRequest(request
);
218 if (fIsTimerEnabled
) {
219 fCurrentTask
.cancel();
220 fCurrentTask
= new TimerTask() {
223 synchronized (fLock
) {
228 fTimer
.schedule(fCurrentTask
, DELAY
);
233 private void fireRequest(boolean isTimeout
) {
234 synchronized (fLock
) {
235 if (fRequestPendingCounter
> 0) {
239 if (fPendingCoalescedRequests
.size() > 0) {
240 Iterator
<TmfCoalescedEventRequest
> iter
= fPendingCoalescedRequests
.iterator();
241 while (iter
.hasNext()) {
242 ExecutionType type
= (isTimeout ? ExecutionType
.BACKGROUND
: ExecutionType
.FOREGROUND
);
243 ITmfEventRequest request
= iter
.next();
244 if (type
== request
.getExecType()) {
245 queueRequest(request
);
254 * Increments/decrements the pending requests counters and fires the request
255 * if necessary (counter == 0). Used for coalescing requests across multiple
259 * Should we increment (true) or decrement (false) the pending
263 public void notifyPendingRequest(boolean isIncrement
) {
264 synchronized (fLock
) {
266 fRequestPendingCounter
++;
268 if (fRequestPendingCounter
> 0) {
269 fRequestPendingCounter
--;
272 // fire request if all pending requests are received
273 if (fRequestPendingCounter
== 0) {
281 // ------------------------------------------------------------------------
283 // ------------------------------------------------------------------------
286 * Create a new request from an existing one, and add it to the coalesced
290 * The request to copy
292 protected void newCoalescedEventRequest(ITmfEventRequest request
) {
293 synchronized (fLock
) {
294 TmfCoalescedEventRequest coalescedRequest
= new TmfCoalescedEventRequest(
295 request
.getDataType(),
298 request
.getNbRequested(),
299 request
.getExecType());
300 coalescedRequest
.addRequest(request
);
301 coalescedRequest
.setProviderFilter(this);
302 if (TmfCoreTracer
.isRequestTraced()) {
303 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
304 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
306 coalesceChildrenRequests(coalescedRequest
);
307 fPendingCoalescedRequests
.add(coalescedRequest
);
312 * Add an existing requests to the list of coalesced ones
315 * The request to add to the list
317 protected void coalesceEventRequest(ITmfEventRequest request
) {
318 synchronized (fLock
) {
319 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
320 if (coalescedRequest
.isCompatible(request
)) {
321 coalescedRequest
.addRequest(request
);
322 if (TmfCoreTracer
.isRequestTraced()) {
323 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
324 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
326 coalesceChildrenRequests(coalescedRequest
);
330 newCoalescedEventRequest(request
);
335 * Sends a request with the parent if compatible.
337 private boolean sendWithParent(final ITmfEventRequest request
) {
338 ITmfEventProvider parent
= getParent();
339 if (parent
instanceof TmfEventProvider
) {
340 return ((TmfEventProvider
) parent
).sendIfCompatible(request
);
346 * Sends a request if compatible with a pending coalesced request.
348 private boolean sendIfCompatible(ITmfEventRequest request
) {
349 synchronized (fLock
) {
350 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
351 if (coalescedRequest
.isCompatible(request
)) {
352 // Send so it can be coalesced with the parent(s)
353 sendRequest(request
);
358 return sendWithParent(request
);
362 * Coalesces children requests with given request if compatible.
364 private void coalesceChildrenRequests(final TmfCoalescedEventRequest request
) {
365 synchronized (fChildren
) {
366 for (TmfEventProvider child
: fChildren
) {
367 child
.coalesceCompatibleRequests(request
);
374 * Coalesces all pending requests that are compatible with coalesced request.
376 private void coalesceCompatibleRequests(TmfCoalescedEventRequest request
) {
377 Iterator
<TmfCoalescedEventRequest
> iter
= getPendingRequests().iterator();
378 while (iter
.hasNext()) {
379 TmfCoalescedEventRequest pendingRequest
= iter
.next();
380 if (request
.isCompatible(pendingRequest
)) {
381 request
.addRequest(pendingRequest
);
382 if (TmfCoreTracer
.isRequestTraced()) {
383 TmfCoreTracer
.traceRequest(pendingRequest
.getRequestId(), "COALESCED with " + request
.getRequestId()); //$NON-NLS-1$
384 TmfCoreTracer
.traceRequest(request
.getRequestId(), "now contains " + request
.getSubRequestIds()); //$NON-NLS-1$
391 // ------------------------------------------------------------------------
392 // Request processing
393 // ------------------------------------------------------------------------
401 protected void queueRequest(final ITmfEventRequest request
) {
403 if (fExecutor
.isShutdown()) {
408 TmfEventThread thread
= new TmfEventThread(this, request
);
410 if (TmfCoreTracer
.isRequestTraced()) {
411 TmfCoreTracer
.traceRequest(request
.getRequestId(), "QUEUED"); //$NON-NLS-1$
414 fExecutor
.execute(thread
);
418 * Initialize the provider based on the request. The context is provider
419 * specific and will be updated by getNext().
423 * @return An application specific context; null if request can't be
426 public abstract ITmfContext
armRequest(ITmfEventRequest request
);
429 * Checks if the data meets the request completion criteria.
436 * The number of events read so far
437 * @return true if completion criteria is met
439 public boolean isCompleted(ITmfEventRequest request
, ITmfEvent event
, int nbRead
) {
440 boolean requestCompleted
= isCompleted2(request
, nbRead
);
441 if (!requestCompleted
) {
442 ITmfTimestamp endTime
= request
.getRange().getEndTime();
443 return event
.getTimestamp().compareTo(endTime
) > 0;
445 return requestCompleted
;
448 private static boolean isCompleted2(ITmfEventRequest request
,int nbRead
) {
449 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
452 // ------------------------------------------------------------------------
453 // Pass-through's to the request executor
454 // ------------------------------------------------------------------------
457 * @return the shutdown state (i.e. if it is accepting new requests)
459 protected boolean executorIsShutdown() {
460 return fExecutor
.isShutdown();
464 * @return the termination state
466 protected boolean executorIsTerminated() {
467 return fExecutor
.isTerminated();
470 // ------------------------------------------------------------------------
472 // ------------------------------------------------------------------------
475 * Handler for the start synch signal
481 public void startSynch(TmfStartSynchSignal signal
) {
482 synchronized (fLock
) {
488 * Handler for the end synch signal
494 public void endSynch(TmfEndSynchSignal signal
) {
495 synchronized (fLock
) {
497 if (fSignalDepth
== 0) {
504 public ITmfEventProvider
getParent() {
505 synchronized (fLock
) {
511 public void setParent(ITmfEventProvider parent
) {
512 if (!(parent
instanceof TmfEventProvider
)) {
513 throw new IllegalArgumentException();
516 synchronized (fLock
) {
517 fParent
= (TmfEventProvider
) parent
;
522 public List
<ITmfEventProvider
> getChildren() {
523 synchronized (fChildren
) {
524 List
<ITmfEventProvider
> list
= new ArrayList
<>();
525 list
.addAll(fChildren
);
531 public <T
extends ITmfEventProvider
> List
<T
> getChildren(Class
<T
> clazz
) {
532 List
<T
> list
= new ArrayList
<>();
533 synchronized (fChildren
) {
534 for (TmfEventProvider child
: fChildren
) {
535 if (clazz
.isAssignableFrom(child
.getClass())) {
536 list
.add(clazz
.cast(child
));
544 public ITmfEventProvider
getChild(String name
) {
545 synchronized (fChildren
) {
546 for (TmfEventProvider child
: fChildren
) {
547 if (child
.getName().equals(name
)) {
556 public ITmfEventProvider
getChild(int index
) {
557 return NonNullUtils
.checkNotNull(fChildren
.get(index
));
561 public void addChild(ITmfEventProvider child
) {
562 if (!(child
instanceof TmfEventProvider
)) {
563 throw new IllegalArgumentException();
565 child
.setParent(this);
566 fChildren
.add((TmfEventProvider
)child
);
570 public int getNbChildren() {
571 return fChildren
.size();
575 * Returns true if an event was provided by this event provider or one of
576 * its children event providers else false.
580 * @return <code>true</code> if event was provided by this provider or one
581 * of its children else <code>false</code>
584 public boolean matches(ITmfEvent event
) {
585 if ((event
.getTrace() == this)) {
588 if (fChildren
.size() > 0) {
589 synchronized (fLock
) {
590 List
<TmfEventProvider
> children
= getChildren(TmfEventProvider
.class);
591 for (TmfEventProvider child
: children
) {
592 if (child
.matches(event
)) {
601 // ------------------------------------------------------------------------
602 // Debug code (will also used in tests using reflection)
603 // ------------------------------------------------------------------------
606 * Gets a list of all pending requests. Debug code.
608 * @return a list of all pending requests
610 private List
<TmfCoalescedEventRequest
> getPendingRequests() {
611 return fPendingCoalescedRequests
;
615 * Clears all pending requests. Debug code.
617 private void clearPendingRequests() {
618 fPendingCoalescedRequests
.clear();
622 * Enables/disables the timer. Debug code.
625 * the enable flag to set
627 private void setTimerEnabled(Boolean enabled
) {
628 fIsTimerEnabled
= enabled
;