1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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
53 public abstract class TmfEventProvider
extends TmfComponent
implements ITmfEventProvider
, ITmfFilter
{
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 /** 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 // ------------------------------------------------------------------------
174 * Get the event type this provider handles
176 * @return The type of ITmfEvent
178 public Class
<?
extends ITmfEvent
> getType() {
182 // ------------------------------------------------------------------------
183 // ITmfRequestHandler
184 // ------------------------------------------------------------------------
190 public void sendRequest(final ITmfEventRequest request
) {
191 synchronized (fLock
) {
193 if (TmfCoreTracer
.isRequestTraced()) {
194 TmfCoreTracer
.traceRequest(request
.getRequestId(), "SENT to provider " + getName()); //$NON-NLS-1$
197 if (request
.getProviderFilter() == null) {
198 request
.setProviderFilter(this);
201 if (sendWithParent(request
)) {
205 if (request
.getExecType() == ExecutionType
.FOREGROUND
) {
206 if ((fSignalDepth
> 0) || (fRequestPendingCounter
> 0)) {
207 coalesceEventRequest(request
);
209 queueRequest(request
);
215 * Dispatch request in case timer is not running.
217 if (fTimer
== null) {
218 queueRequest(request
);
222 coalesceEventRequest(request
);
224 if (fIsTimerEnabled
) {
225 fCurrentTask
.cancel();
226 fCurrentTask
= new TimerTask() {
229 synchronized (fLock
) {
234 fTimer
.schedule(fCurrentTask
, DELAY
);
239 private void fireRequest(boolean isTimeout
) {
240 synchronized (fLock
) {
241 if (fRequestPendingCounter
> 0) {
245 if (fPendingCoalescedRequests
.size() > 0) {
246 Iterator
<TmfCoalescedEventRequest
> iter
= fPendingCoalescedRequests
.iterator();
247 while (iter
.hasNext()) {
248 ExecutionType type
= (isTimeout ? ExecutionType
.BACKGROUND
: ExecutionType
.FOREGROUND
);
249 ITmfEventRequest request
= iter
.next();
250 if (type
== request
.getExecType()) {
251 queueRequest(request
);
260 * Increments/decrements the pending requests counters and fires the request
261 * if necessary (counter == 0). Used for coalescing requests across multiple
265 * Should we increment (true) or decrement (false) the pending
269 public void notifyPendingRequest(boolean isIncrement
) {
270 synchronized (fLock
) {
272 fRequestPendingCounter
++;
274 if (fRequestPendingCounter
> 0) {
275 fRequestPendingCounter
--;
278 // fire request if all pending requests are received
279 if (fRequestPendingCounter
== 0) {
287 // ------------------------------------------------------------------------
289 // ------------------------------------------------------------------------
292 * Create a new request from an existing one, and add it to the coalesced
296 * The request to copy
299 protected void newCoalescedEventRequest(ITmfEventRequest request
) {
300 synchronized (fLock
) {
301 TmfCoalescedEventRequest coalescedRequest
= new TmfCoalescedEventRequest(
302 request
.getDataType(),
305 request
.getNbRequested(),
306 request
.getExecType());
307 coalescedRequest
.addRequest(request
);
308 coalescedRequest
.setProviderFilter(this);
309 if (TmfCoreTracer
.isRequestTraced()) {
310 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
311 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
313 coalesceChildrenRequests(coalescedRequest
);
314 fPendingCoalescedRequests
.add(coalescedRequest
);
319 * Add an existing requests to the list of coalesced ones
322 * The request to add to the list
325 protected void coalesceEventRequest(ITmfEventRequest request
) {
326 synchronized (fLock
) {
327 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
328 if (coalescedRequest
.isCompatible(request
)) {
329 coalescedRequest
.addRequest(request
);
330 if (TmfCoreTracer
.isRequestTraced()) {
331 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
332 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
334 coalesceChildrenRequests(coalescedRequest
);
338 newCoalescedEventRequest(request
);
343 * Sends a request with the parent if compatible.
345 private boolean sendWithParent(final ITmfEventRequest request
) {
346 ITmfEventProvider parent
= getParent();
347 if (parent
instanceof TmfEventProvider
) {
348 return ((TmfEventProvider
) parent
).sendIfCompatible(request
);
354 * Sends a request if compatible with a pending coalesced request.
356 private boolean sendIfCompatible(ITmfEventRequest request
) {
357 synchronized (fLock
) {
358 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
359 if (coalescedRequest
.isCompatible(request
)) {
360 // Send so it can be coalesced with the parent(s)
361 sendRequest(request
);
366 return sendWithParent(request
);
370 * Coalesces children requests with given request if compatible.
372 private void coalesceChildrenRequests(final TmfCoalescedEventRequest request
) {
373 synchronized (fChildren
) {
374 for (TmfEventProvider child
: fChildren
) {
375 child
.coalesceCompatibleRequests(request
);
382 * Coalesces all pending requests that are compatible with coalesced request.
384 private void coalesceCompatibleRequests(TmfCoalescedEventRequest request
) {
385 Iterator
<TmfCoalescedEventRequest
> iter
= getPendingRequests().iterator();
386 while (iter
.hasNext()) {
387 TmfCoalescedEventRequest pendingRequest
= iter
.next();
388 if (request
.isCompatible(pendingRequest
)) {
389 request
.addRequest(pendingRequest
);
390 if (TmfCoreTracer
.isRequestTraced()) {
391 TmfCoreTracer
.traceRequest(pendingRequest
.getRequestId(), "COALESCED with " + request
.getRequestId()); //$NON-NLS-1$
392 TmfCoreTracer
.traceRequest(request
.getRequestId(), "now contains " + request
.getSubRequestIds()); //$NON-NLS-1$
399 // ------------------------------------------------------------------------
400 // Request processing
401 // ------------------------------------------------------------------------
410 protected void queueRequest(final ITmfEventRequest request
) {
412 if (fExecutor
.isShutdown()) {
417 TmfEventThread thread
= new TmfEventThread(this, request
);
419 if (TmfCoreTracer
.isRequestTraced()) {
420 TmfCoreTracer
.traceRequest(request
.getRequestId(), "QUEUED"); //$NON-NLS-1$
423 fExecutor
.execute(thread
);
427 * Initialize the provider based on the request. The context is provider
428 * specific and will be updated by getNext().
432 * @return An application specific context; null if request can't be
436 public abstract ITmfContext
armRequest(ITmfEventRequest request
);
439 * Checks if the data meets the request completion criteria.
446 * The number of events read so far
447 * @return true if completion criteria is met
450 public boolean isCompleted(ITmfEventRequest request
, ITmfEvent event
, int nbRead
) {
451 boolean requestCompleted
= isCompleted2(request
, nbRead
);
452 if (!requestCompleted
) {
453 ITmfTimestamp endTime
= request
.getRange().getEndTime();
454 return event
.getTimestamp().compareTo(endTime
) > 0;
456 return requestCompleted
;
459 private static boolean isCompleted2(ITmfEventRequest request
,int nbRead
) {
460 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
463 // ------------------------------------------------------------------------
464 // Pass-through's to the request executor
465 // ------------------------------------------------------------------------
468 * @return the shutdown state (i.e. if it is accepting new requests)
471 protected boolean executorIsShutdown() {
472 return fExecutor
.isShutdown();
476 * @return the termination state
479 protected boolean executorIsTerminated() {
480 return fExecutor
.isTerminated();
483 // ------------------------------------------------------------------------
485 // ------------------------------------------------------------------------
488 * Handler for the start synch signal
494 public void startSynch(TmfStartSynchSignal signal
) {
495 synchronized (fLock
) {
501 * Handler for the end synch signal
507 public void endSynch(TmfEndSynchSignal signal
) {
508 synchronized (fLock
) {
510 if (fSignalDepth
== 0) {
517 public ITmfEventProvider
getParent() {
518 synchronized (fLock
) {
524 public void setParent(ITmfEventProvider parent
) {
525 if (!(parent
instanceof TmfEventProvider
)) {
526 throw new IllegalArgumentException();
529 synchronized (fLock
) {
530 fParent
= (TmfEventProvider
) parent
;
535 public List
<ITmfEventProvider
> getChildren() {
536 synchronized (fChildren
) {
537 List
<ITmfEventProvider
> list
= new ArrayList
<>();
538 list
.addAll(fChildren
);
544 public <T
extends ITmfEventProvider
> List
<T
> getChildren(Class
<T
> clazz
) {
545 List
<T
> list
= new ArrayList
<>();
546 synchronized (fChildren
) {
547 for (TmfEventProvider child
: fChildren
) {
548 if (clazz
.isAssignableFrom(child
.getClass())) {
549 list
.add(clazz
.cast(child
));
557 public ITmfEventProvider
getChild(String name
) {
558 synchronized (fChildren
) {
559 for (TmfEventProvider child
: fChildren
) {
560 if (child
.getName().equals(name
)) {
569 public ITmfEventProvider
getChild(int index
) {
570 return NonNullUtils
.checkNotNull(fChildren
.get(index
));
574 public void addChild(ITmfEventProvider child
) {
575 if (!(child
instanceof TmfEventProvider
)) {
576 throw new IllegalArgumentException();
578 child
.setParent(this);
579 fChildren
.add((TmfEventProvider
)child
);
583 public int getNbChildren() {
584 return fChildren
.size();
588 * Returns true if an event was provided by this event provider or one of
589 * its children event providers else false.
593 * @return <code>true</code> if event was provided by this provider or one
594 * of its children else <code>false</code>
597 public boolean matches(ITmfEvent event
) {
598 if ((event
.getTrace() == this)) {
601 if (fChildren
.size() > 0) {
602 synchronized (fLock
) {
603 List
<TmfEventProvider
> children
= getChildren(TmfEventProvider
.class);
604 for (TmfEventProvider child
: children
) {
605 if (child
.matches(event
)) {
614 // ------------------------------------------------------------------------
615 // Debug code (will also used in tests using reflection)
616 // ------------------------------------------------------------------------
619 * Gets a list of all pending requests. Debug code.
621 * @return a list of all pending requests
623 private List
<TmfCoalescedEventRequest
> getPendingRequests() {
624 return fPendingCoalescedRequests
;
628 * Clears all pending requests. Debug code.
630 private void clearPendingRequests() {
631 fPendingCoalescedRequests
.clear();
635 * Enables/disables the timer. Debug code.
638 * the enable flag to set
640 private void setTimerEnabled(Boolean enabled
) {
641 fIsTimerEnabled
= enabled
;