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 // ------------------------------------------------------------------------
172 * Get the event type this provider handles
174 * @return The type of ITmfEvent
176 public Class
<?
extends ITmfEvent
> getType() {
180 // ------------------------------------------------------------------------
181 // ITmfRequestHandler
182 // ------------------------------------------------------------------------
185 public void sendRequest(final ITmfEventRequest request
) {
186 synchronized (fLock
) {
188 if (TmfCoreTracer
.isRequestTraced()) {
189 TmfCoreTracer
.traceRequest(request
.getRequestId(), "SENT to provider " + getName()); //$NON-NLS-1$
192 if (request
.getProviderFilter() == null) {
193 request
.setProviderFilter(this);
196 if (sendWithParent(request
)) {
200 if (request
.getExecType() == ExecutionType
.FOREGROUND
) {
201 if ((fSignalDepth
> 0) || (fRequestPendingCounter
> 0)) {
202 coalesceEventRequest(request
);
204 queueRequest(request
);
210 * Dispatch request in case timer is not running.
212 if (fTimer
== null) {
213 queueRequest(request
);
217 coalesceEventRequest(request
);
219 if (fIsTimerEnabled
) {
220 fCurrentTask
.cancel();
221 fCurrentTask
= new TimerTask() {
224 synchronized (fLock
) {
229 fTimer
.schedule(fCurrentTask
, DELAY
);
234 private void fireRequest(boolean isTimeout
) {
235 synchronized (fLock
) {
236 if (fRequestPendingCounter
> 0) {
240 if (fPendingCoalescedRequests
.size() > 0) {
241 Iterator
<TmfCoalescedEventRequest
> iter
= fPendingCoalescedRequests
.iterator();
242 while (iter
.hasNext()) {
243 ExecutionType type
= (isTimeout ? ExecutionType
.BACKGROUND
: ExecutionType
.FOREGROUND
);
244 ITmfEventRequest request
= iter
.next();
245 if (type
== request
.getExecType()) {
246 queueRequest(request
);
255 * Increments/decrements the pending requests counters and fires the request
256 * if necessary (counter == 0). Used for coalescing requests across multiple
260 * Should we increment (true) or decrement (false) the pending
264 public void notifyPendingRequest(boolean isIncrement
) {
265 synchronized (fLock
) {
267 fRequestPendingCounter
++;
269 if (fRequestPendingCounter
> 0) {
270 fRequestPendingCounter
--;
273 // fire request if all pending requests are received
274 if (fRequestPendingCounter
== 0) {
282 // ------------------------------------------------------------------------
284 // ------------------------------------------------------------------------
287 * Create a new request from an existing one, and add it to the coalesced
291 * The request to copy
293 protected void newCoalescedEventRequest(ITmfEventRequest request
) {
294 synchronized (fLock
) {
295 TmfCoalescedEventRequest coalescedRequest
= new TmfCoalescedEventRequest(
296 request
.getDataType(),
299 request
.getNbRequested(),
300 request
.getExecType());
301 coalescedRequest
.addRequest(request
);
302 coalescedRequest
.setProviderFilter(this);
303 if (TmfCoreTracer
.isRequestTraced()) {
304 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
305 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
307 coalesceChildrenRequests(coalescedRequest
);
308 fPendingCoalescedRequests
.add(coalescedRequest
);
313 * Add an existing requests to the list of coalesced ones
316 * The request to add to the list
318 protected void coalesceEventRequest(ITmfEventRequest request
) {
319 synchronized (fLock
) {
320 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
321 if (coalescedRequest
.isCompatible(request
)) {
322 coalescedRequest
.addRequest(request
);
323 if (TmfCoreTracer
.isRequestTraced()) {
324 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
325 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
327 coalesceChildrenRequests(coalescedRequest
);
331 newCoalescedEventRequest(request
);
336 * Sends a request with the parent if compatible.
338 private boolean sendWithParent(final ITmfEventRequest request
) {
339 ITmfEventProvider parent
= getParent();
340 if (parent
instanceof TmfEventProvider
) {
341 return ((TmfEventProvider
) parent
).sendIfCompatible(request
);
347 * Sends a request if compatible with a pending coalesced request.
349 private boolean sendIfCompatible(ITmfEventRequest request
) {
350 synchronized (fLock
) {
351 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
352 if (coalescedRequest
.isCompatible(request
)) {
353 // Send so it can be coalesced with the parent(s)
354 sendRequest(request
);
359 return sendWithParent(request
);
363 * Coalesces children requests with given request if compatible.
365 private void coalesceChildrenRequests(final TmfCoalescedEventRequest request
) {
366 synchronized (fChildren
) {
367 for (TmfEventProvider child
: fChildren
) {
368 child
.coalesceCompatibleRequests(request
);
375 * Coalesces all pending requests that are compatible with coalesced request.
377 private void coalesceCompatibleRequests(TmfCoalescedEventRequest request
) {
378 Iterator
<TmfCoalescedEventRequest
> iter
= getPendingRequests().iterator();
379 while (iter
.hasNext()) {
380 TmfCoalescedEventRequest pendingRequest
= iter
.next();
381 if (request
.isCompatible(pendingRequest
)) {
382 request
.addRequest(pendingRequest
);
383 if (TmfCoreTracer
.isRequestTraced()) {
384 TmfCoreTracer
.traceRequest(pendingRequest
.getRequestId(), "COALESCED with " + request
.getRequestId()); //$NON-NLS-1$
385 TmfCoreTracer
.traceRequest(request
.getRequestId(), "now contains " + request
.getSubRequestIds()); //$NON-NLS-1$
392 // ------------------------------------------------------------------------
393 // Request processing
394 // ------------------------------------------------------------------------
402 protected void queueRequest(final ITmfEventRequest request
) {
404 if (fExecutor
.isShutdown()) {
409 TmfEventThread thread
= new TmfEventThread(this, request
);
411 if (TmfCoreTracer
.isRequestTraced()) {
412 TmfCoreTracer
.traceRequest(request
.getRequestId(), "QUEUED"); //$NON-NLS-1$
415 fExecutor
.execute(thread
);
419 * Initialize the provider based on the request. The context is provider
420 * specific and will be updated by getNext().
424 * @return An application specific context; null if request can't be
427 public abstract ITmfContext
armRequest(ITmfEventRequest request
);
430 * Checks if the data meets the request completion criteria.
437 * The number of events read so far
438 * @return true if completion criteria is met
440 public boolean isCompleted(ITmfEventRequest request
, ITmfEvent event
, int nbRead
) {
441 boolean requestCompleted
= isCompleted2(request
, nbRead
);
442 if (!requestCompleted
) {
443 ITmfTimestamp endTime
= request
.getRange().getEndTime();
444 return event
.getTimestamp().compareTo(endTime
) > 0;
446 return requestCompleted
;
449 private static boolean isCompleted2(ITmfEventRequest request
,int nbRead
) {
450 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
453 // ------------------------------------------------------------------------
454 // Pass-through's to the request executor
455 // ------------------------------------------------------------------------
458 * @return the shutdown state (i.e. if it is accepting new requests)
460 protected boolean executorIsShutdown() {
461 return fExecutor
.isShutdown();
465 * @return the termination state
467 protected boolean executorIsTerminated() {
468 return fExecutor
.isTerminated();
471 // ------------------------------------------------------------------------
473 // ------------------------------------------------------------------------
476 * Handler for the start synch signal
482 public void startSynch(TmfStartSynchSignal signal
) {
483 synchronized (fLock
) {
489 * Handler for the end synch signal
495 public void endSynch(TmfEndSynchSignal signal
) {
496 synchronized (fLock
) {
498 if (fSignalDepth
== 0) {
505 public ITmfEventProvider
getParent() {
506 synchronized (fLock
) {
512 public void setParent(ITmfEventProvider parent
) {
513 if (!(parent
instanceof TmfEventProvider
)) {
514 throw new IllegalArgumentException();
517 synchronized (fLock
) {
518 fParent
= (TmfEventProvider
) parent
;
523 public List
<ITmfEventProvider
> getChildren() {
524 synchronized (fChildren
) {
525 List
<ITmfEventProvider
> list
= new ArrayList
<>();
526 list
.addAll(fChildren
);
532 public <T
extends ITmfEventProvider
> List
<T
> getChildren(Class
<T
> clazz
) {
533 List
<T
> list
= new ArrayList
<>();
534 synchronized (fChildren
) {
535 for (TmfEventProvider child
: fChildren
) {
536 if (clazz
.isAssignableFrom(child
.getClass())) {
537 list
.add(clazz
.cast(child
));
545 public ITmfEventProvider
getChild(String name
) {
546 synchronized (fChildren
) {
547 for (TmfEventProvider child
: fChildren
) {
548 if (child
.getName().equals(name
)) {
557 public ITmfEventProvider
getChild(int index
) {
558 return NonNullUtils
.checkNotNull(fChildren
.get(index
));
562 public void addChild(ITmfEventProvider child
) {
563 if (!(child
instanceof TmfEventProvider
)) {
564 throw new IllegalArgumentException();
566 child
.setParent(this);
567 fChildren
.add((TmfEventProvider
)child
);
571 public int getNbChildren() {
572 return fChildren
.size();
576 * Returns true if an event was provided by this event provider or one of
577 * its children event providers else false.
581 * @return <code>true</code> if event was provided by this provider or one
582 * of its children else <code>false</code>
585 public boolean matches(ITmfEvent event
) {
586 if ((event
.getTrace() == this)) {
589 if (fChildren
.size() > 0) {
590 synchronized (fLock
) {
591 List
<TmfEventProvider
> children
= getChildren(TmfEventProvider
.class);
592 for (TmfEventProvider child
: children
) {
593 if (child
.matches(event
)) {
602 // ------------------------------------------------------------------------
603 // Debug code (will also used in tests using reflection)
604 // ------------------------------------------------------------------------
607 * Gets a list of all pending requests. Debug code.
609 * @return a list of all pending requests
611 private List
<TmfCoalescedEventRequest
> getPendingRequests() {
612 return fPendingCoalescedRequests
;
616 * Clears all pending requests. Debug code.
618 private void clearPendingRequests() {
619 fPendingCoalescedRequests
.clear();
623 * Enables/disables the timer. Debug code.
626 * the enable flag to set
628 private void setTimerEnabled(Boolean enabled
) {
629 fIsTimerEnabled
= enabled
;