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 static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Collections
;
22 import java
.util
.Iterator
;
23 import java
.util
.LinkedList
;
24 import java
.util
.List
;
25 import java
.util
.Timer
;
26 import java
.util
.TimerTask
;
28 import org
.eclipse
.jdt
.annotation
.NonNull
;
29 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.TmfCoreTracer
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.component
.TmfEventThread
;
32 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.component
.TmfProviderManager
;
33 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.request
.TmfCoalescedEventRequest
;
34 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.request
.TmfRequestExecutor
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.filter
.ITmfFilter
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEndSynchSignal
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfStartSynchSignal
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
46 * An abstract base class that implements ITmfEventProvider.
48 * This abstract class implements the housekeeping methods to register/
49 * de-register the event provider and to handle generically the event requests.
52 * @author Francois Chouinard
54 public abstract class TmfEventProvider
extends TmfComponent
implements ITmfEventProvider
, ITmfFilter
{
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 /** Default amount of events per request "chunk" */
61 public static final int DEFAULT_BLOCK_SIZE
= 50000;
63 /** Delay for coalescing background requests (in milli-seconds) */
64 private static final long DELAY
= 1000;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 /** List of coalesced requests */
71 private final List
<TmfCoalescedEventRequest
> fPendingCoalescedRequests
= new LinkedList
<>();
73 /** The type of event handled by this provider */
74 private Class
<?
extends ITmfEvent
> fType
;
76 private final TmfRequestExecutor fExecutor
;
78 private final Object fLock
= new Object();
80 private int fSignalDepth
= 0;
82 private int fRequestPendingCounter
= 0;
86 /** Current timer task */
87 @NonNull private TimerTask fCurrentTask
= new TimerTask() { @Override public void run() {} };
89 private boolean fIsTimerEnabled
;
92 * The parent event provider.
94 private TmfEventProvider fParent
= null;
96 * The list if children event provider.
98 private final List
<TmfEventProvider
> fChildren
= Collections
.synchronizedList(new ArrayList
<TmfEventProvider
>());
100 // ------------------------------------------------------------------------
102 // ------------------------------------------------------------------------
105 * Default constructor
107 public TmfEventProvider() {
109 setTimerEnabled(true);
110 fExecutor
= new TmfRequestExecutor();
114 * Standard constructor. Instantiate and initialize at the same time.
117 * Name of the provider
119 * The type of events that will be handled
121 public TmfEventProvider(String name
, Class
<?
extends ITmfEvent
> type
) {
127 * Initialize this data provider
130 * Name of the provider
132 * The type of events that will be handled
134 public void init(String name
, Class
<?
extends ITmfEvent
> type
) {
141 synchronized (fLock
) {
142 fTimer
= new Timer();
145 TmfProviderManager
.register(fType
, this);
149 public void dispose() {
150 TmfProviderManager
.deregister(fType
, this);
152 synchronized (fLock
) {
153 if (fTimer
!= null) {
159 synchronized (fChildren
) {
160 for (TmfEventProvider child
: fChildren
) {
165 clearPendingRequests();
169 // ------------------------------------------------------------------------
171 // ------------------------------------------------------------------------
177 public Class
<?
extends ITmfEvent
> getEventType() {
181 // ------------------------------------------------------------------------
182 // ITmfRequestHandler
183 // ------------------------------------------------------------------------
186 public void sendRequest(final ITmfEventRequest request
) {
187 synchronized (fLock
) {
189 if (TmfCoreTracer
.isRequestTraced()) {
190 TmfCoreTracer
.traceRequest(request
.getRequestId(), "SENT to provider " + getName()); //$NON-NLS-1$
193 if (request
.getProviderFilter() == null) {
194 request
.setProviderFilter(this);
197 if (sendWithParent(request
)) {
201 if (request
.getExecType() == ExecutionType
.FOREGROUND
) {
202 if ((fSignalDepth
> 0) || (fRequestPendingCounter
> 0)) {
203 coalesceEventRequest(request
);
205 queueRequest(request
);
211 * Dispatch request in case timer is not running.
213 if (fTimer
== null) {
214 queueRequest(request
);
218 coalesceEventRequest(request
);
220 if (fIsTimerEnabled
) {
221 fCurrentTask
.cancel();
222 fCurrentTask
= new TimerTask() {
225 synchronized (fLock
) {
230 fTimer
.schedule(fCurrentTask
, DELAY
);
235 private void fireRequest(boolean isTimeout
) {
236 synchronized (fLock
) {
237 if (fRequestPendingCounter
> 0) {
241 if (fPendingCoalescedRequests
.size() > 0) {
242 Iterator
<TmfCoalescedEventRequest
> iter
= fPendingCoalescedRequests
.iterator();
243 while (iter
.hasNext()) {
244 ExecutionType type
= (isTimeout ? ExecutionType
.BACKGROUND
: ExecutionType
.FOREGROUND
);
245 ITmfEventRequest request
= iter
.next();
246 if (type
== request
.getExecType()) {
247 queueRequest(request
);
256 * Increments/decrements the pending requests counters and fires the request
257 * if necessary (counter == 0). Used for coalescing requests across multiple
261 * Should we increment (true) or decrement (false) the pending
265 public void notifyPendingRequest(boolean isIncrement
) {
266 synchronized (fLock
) {
268 fRequestPendingCounter
++;
270 if (fRequestPendingCounter
> 0) {
271 fRequestPendingCounter
--;
274 // fire request if all pending requests are received
275 if (fRequestPendingCounter
== 0) {
283 // ------------------------------------------------------------------------
285 // ------------------------------------------------------------------------
288 * Create a new request from an existing one, and add it to the coalesced
292 * The request to copy
294 protected void newCoalescedEventRequest(ITmfEventRequest request
) {
295 synchronized (fLock
) {
296 TmfCoalescedEventRequest coalescedRequest
= new TmfCoalescedEventRequest(
297 request
.getDataType(),
300 request
.getNbRequested(),
301 request
.getExecType(),
302 request
.getDependencyLevel());
303 coalescedRequest
.addRequest(request
);
304 coalescedRequest
.setProviderFilter(this);
305 if (TmfCoreTracer
.isRequestTraced()) {
306 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
307 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
309 coalesceChildrenRequests(coalescedRequest
);
310 fPendingCoalescedRequests
.add(coalescedRequest
);
315 * Add an existing requests to the list of coalesced ones
318 * The request to add to the list
320 protected void coalesceEventRequest(ITmfEventRequest request
) {
321 synchronized (fLock
) {
322 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
323 if (coalescedRequest
.isCompatible(request
)) {
324 coalescedRequest
.addRequest(request
);
325 if (TmfCoreTracer
.isRequestTraced()) {
326 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
327 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
329 coalesceChildrenRequests(coalescedRequest
);
333 newCoalescedEventRequest(request
);
338 * Sends a request with the parent if compatible.
340 private boolean sendWithParent(final ITmfEventRequest request
) {
341 ITmfEventProvider parent
= getParent();
342 if (parent
instanceof TmfEventProvider
) {
343 return ((TmfEventProvider
) parent
).sendIfCompatible(request
);
349 * Sends a request if compatible with a pending coalesced request.
351 private boolean sendIfCompatible(ITmfEventRequest request
) {
352 synchronized (fLock
) {
353 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
354 if (coalescedRequest
.isCompatible(request
)) {
355 // Send so it can be coalesced with the parent(s)
356 sendRequest(request
);
361 return sendWithParent(request
);
365 * Coalesces children requests with given request if compatible.
367 private void coalesceChildrenRequests(final TmfCoalescedEventRequest request
) {
368 synchronized (fChildren
) {
369 for (TmfEventProvider child
: fChildren
) {
370 child
.coalesceCompatibleRequests(request
);
377 * Coalesces all pending requests that are compatible with coalesced request.
379 private void coalesceCompatibleRequests(TmfCoalescedEventRequest request
) {
380 Iterator
<TmfCoalescedEventRequest
> iter
= getPendingRequests().iterator();
381 while (iter
.hasNext()) {
382 TmfCoalescedEventRequest pendingRequest
= iter
.next();
383 if (request
.isCompatible(pendingRequest
)) {
384 request
.addRequest(pendingRequest
);
385 if (TmfCoreTracer
.isRequestTraced()) {
386 TmfCoreTracer
.traceRequest(pendingRequest
.getRequestId(), "COALESCED with " + request
.getRequestId()); //$NON-NLS-1$
387 TmfCoreTracer
.traceRequest(request
.getRequestId(), "now contains " + request
.getSubRequestIds()); //$NON-NLS-1$
394 // ------------------------------------------------------------------------
395 // Request processing
396 // ------------------------------------------------------------------------
404 protected void queueRequest(final ITmfEventRequest request
) {
406 if (fExecutor
.isShutdown()) {
411 TmfEventThread thread
= new TmfEventThread(this, request
);
413 if (TmfCoreTracer
.isRequestTraced()) {
414 TmfCoreTracer
.traceRequest(request
.getRequestId(), "QUEUED"); //$NON-NLS-1$
417 fExecutor
.execute(thread
);
421 * Initialize the provider based on the request. The context is provider
422 * specific and will be updated by getNext().
426 * @return An application specific context; null if request can't be
429 public abstract ITmfContext
armRequest(ITmfEventRequest request
);
432 * Checks if the data meets the request completion criteria.
439 * The number of events read so far
440 * @return true if completion criteria is met
442 public boolean isCompleted(ITmfEventRequest request
, ITmfEvent event
, int nbRead
) {
443 boolean requestCompleted
= isCompleted2(request
, nbRead
);
444 if (!requestCompleted
) {
445 ITmfTimestamp endTime
= request
.getRange().getEndTime();
446 return event
.getTimestamp().compareTo(endTime
) > 0;
448 return requestCompleted
;
451 private static boolean isCompleted2(ITmfEventRequest request
,int nbRead
) {
452 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
455 // ------------------------------------------------------------------------
456 // Pass-through's to the request executor
457 // ------------------------------------------------------------------------
460 * @return the shutdown state (i.e. if it is accepting new requests)
462 protected boolean executorIsShutdown() {
463 return fExecutor
.isShutdown();
467 * @return the termination state
469 protected boolean executorIsTerminated() {
470 return fExecutor
.isTerminated();
473 // ------------------------------------------------------------------------
475 // ------------------------------------------------------------------------
478 * Handler for the start synch signal
484 public void startSynch(TmfStartSynchSignal signal
) {
485 synchronized (fLock
) {
491 * Handler for the end synch signal
497 public void endSynch(TmfEndSynchSignal signal
) {
498 synchronized (fLock
) {
500 if (fSignalDepth
== 0) {
507 public ITmfEventProvider
getParent() {
508 synchronized (fLock
) {
514 public void setParent(ITmfEventProvider parent
) {
515 if (!(parent
instanceof TmfEventProvider
)) {
516 throw new IllegalArgumentException();
519 synchronized (fLock
) {
520 fParent
= (TmfEventProvider
) parent
;
525 public List
<ITmfEventProvider
> getChildren() {
526 synchronized (fChildren
) {
527 List
<ITmfEventProvider
> list
= new ArrayList
<>();
528 list
.addAll(fChildren
);
534 public <T
extends ITmfEventProvider
> List
<T
> getChildren(Class
<T
> clazz
) {
535 List
<@NonNull T
> list
= new ArrayList
<>();
536 synchronized (fChildren
) {
537 for (TmfEventProvider child
: fChildren
) {
538 if (clazz
.isAssignableFrom(child
.getClass())) {
539 list
.add(checkNotNull(clazz
.cast(child
)));
547 public ITmfEventProvider
getChild(String name
) {
548 synchronized (fChildren
) {
549 for (TmfEventProvider child
: fChildren
) {
550 if (child
.getName().equals(name
)) {
559 public ITmfEventProvider
getChild(int index
) {
560 return NonNullUtils
.checkNotNull(fChildren
.get(index
));
564 public void addChild(ITmfEventProvider child
) {
565 if (!(child
instanceof TmfEventProvider
)) {
566 throw new IllegalArgumentException();
568 child
.setParent(this);
569 fChildren
.add((TmfEventProvider
)child
);
573 public int getNbChildren() {
574 return fChildren
.size();
578 * Returns true if an event was provided by this event provider or one of
579 * its children event providers else false.
583 * @return <code>true</code> if event was provided by this provider or one
584 * of its children else <code>false</code>
587 public boolean matches(ITmfEvent event
) {
588 if ((event
.getTrace() == this)) {
591 if (fChildren
.size() > 0) {
592 synchronized (fLock
) {
593 List
<TmfEventProvider
> children
= getChildren(TmfEventProvider
.class);
594 for (TmfEventProvider child
: children
) {
595 if (child
.matches(event
)) {
604 // ------------------------------------------------------------------------
605 // Debug code (will also used in tests using reflection)
606 // ------------------------------------------------------------------------
609 * Gets a list of all pending requests. Debug code.
611 * @return a list of all pending requests
613 private List
<TmfCoalescedEventRequest
> getPendingRequests() {
614 return fPendingCoalescedRequests
;
618 * Clears all pending requests. Debug code.
620 private void clearPendingRequests() {
621 fPendingCoalescedRequests
.clear();
625 * Enables/disables the timer. Debug code.
628 * the enable flag to set
630 private void setTimerEnabled(Boolean enabled
) {
631 fIsTimerEnabled
= enabled
;