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
.internal
.tmf
.core
.TmfCoreTracer
;
28 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.component
.TmfEventThread
;
29 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.component
.TmfProviderManager
;
30 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.request
.TmfCoalescedEventRequest
;
31 import org
.eclipse
.tracecompass
.internal
.tmf
.core
.request
.TmfRequestExecutor
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.request
.ITmfEventRequest
.ExecutionType
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfEndSynchSignal
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfSignalHandler
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.signal
.TmfStartSynchSignal
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
42 * An abstract base class that implements ITmfEventProvider.
44 * This abstract class implements the housekeeping methods to register/
45 * de-register the event provider and to handle generically the event requests.
48 * @author Francois Chouinard
51 public abstract class TmfEventProvider
extends TmfComponent
implements ITmfEventProvider
{
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 /** 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 // ------------------------------------------------------------------------
188 public void sendRequest(final ITmfEventRequest request
) {
189 synchronized (fLock
) {
191 if (TmfCoreTracer
.isRequestTraced()) {
192 TmfCoreTracer
.traceRequest(request
.getRequestId(), "SENT to provider " + getName()); //$NON-NLS-1$
195 if (request
.getEventProvider() == null) {
196 request
.setEventProvider(this);
199 if (sendWithParent(request
)) {
203 if (request
.getExecType() == ExecutionType
.FOREGROUND
) {
204 if ((fSignalDepth
> 0) || (fRequestPendingCounter
> 0)) {
205 coalesceEventRequest(request
);
207 queueRequest(request
);
213 * Dispatch request in case timer is not running.
215 if (fTimer
== null) {
216 queueRequest(request
);
220 coalesceEventRequest(request
);
222 if (fIsTimerEnabled
) {
223 fCurrentTask
.cancel();
224 fCurrentTask
= new TimerTask() {
227 synchronized (fLock
) {
232 fTimer
.schedule(fCurrentTask
, DELAY
);
237 private void fireRequest(boolean isTimeout
) {
238 synchronized (fLock
) {
239 if (fRequestPendingCounter
> 0) {
243 if (fPendingCoalescedRequests
.size() > 0) {
244 Iterator
<TmfCoalescedEventRequest
> iter
= fPendingCoalescedRequests
.iterator();
245 while (iter
.hasNext()) {
246 ExecutionType type
= (isTimeout ? ExecutionType
.BACKGROUND
: ExecutionType
.FOREGROUND
);
247 ITmfEventRequest request
= iter
.next();
248 if (type
== request
.getExecType()) {
249 queueRequest(request
);
258 * Increments/decrements the pending requests counters and fires the request
259 * if necessary (counter == 0). Used for coalescing requests across multiple
263 * Should we increment (true) or decrement (false) the pending
267 public void notifyPendingRequest(boolean isIncrement
) {
268 synchronized (fLock
) {
270 fRequestPendingCounter
++;
272 if (fRequestPendingCounter
> 0) {
273 fRequestPendingCounter
--;
276 // fire request if all pending requests are received
277 if (fRequestPendingCounter
== 0) {
285 // ------------------------------------------------------------------------
287 // ------------------------------------------------------------------------
290 * Create a new request from an existing one, and add it to the coalesced
294 * The request to copy
297 protected void newCoalescedEventRequest(ITmfEventRequest request
) {
298 synchronized (fLock
) {
299 TmfCoalescedEventRequest coalescedRequest
= new TmfCoalescedEventRequest(
300 request
.getDataType(),
303 request
.getNbRequested(),
304 request
.getExecType());
305 coalescedRequest
.addRequest(request
);
306 coalescedRequest
.setEventProvider(this);
307 if (TmfCoreTracer
.isRequestTraced()) {
308 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
309 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
311 coalesceChildrenRequests(coalescedRequest
);
312 fPendingCoalescedRequests
.add(coalescedRequest
);
317 * Add an existing requests to the list of coalesced ones
320 * The request to add to the list
323 protected void coalesceEventRequest(ITmfEventRequest request
) {
324 synchronized (fLock
) {
325 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
326 if (coalescedRequest
.isCompatible(request
)) {
327 coalescedRequest
.addRequest(request
);
328 if (TmfCoreTracer
.isRequestTraced()) {
329 TmfCoreTracer
.traceRequest(request
.getRequestId(), "COALESCED with " + coalescedRequest
.getRequestId()); //$NON-NLS-1$
330 TmfCoreTracer
.traceRequest(coalescedRequest
.getRequestId(), "now contains " + coalescedRequest
.getSubRequestIds()); //$NON-NLS-1$
332 coalesceChildrenRequests(coalescedRequest
);
336 newCoalescedEventRequest(request
);
341 * Sends a request with the parent if compatible.
343 private boolean sendWithParent(final ITmfEventRequest request
) {
344 ITmfEventProvider parent
= getParent();
345 if (parent
instanceof TmfEventProvider
) {
346 return ((TmfEventProvider
) parent
).sendIfCompatible(request
);
352 * Sends a request if compatible with a pending coalesced request.
354 private boolean sendIfCompatible(ITmfEventRequest request
) {
355 synchronized (fLock
) {
356 for (TmfCoalescedEventRequest coalescedRequest
: getPendingRequests()) {
357 if (coalescedRequest
.isCompatible(request
)) {
358 // Send so it can be coalesced with the parent(s)
359 sendRequest(request
);
364 return sendWithParent(request
);
368 * Coalesces children requests with given request if compatible.
370 private void coalesceChildrenRequests(final TmfCoalescedEventRequest request
) {
371 synchronized (fChildren
) {
372 for (TmfEventProvider child
: fChildren
) {
373 child
.coalesceCompatibleRequests(request
);
380 * Coalesces all pending requests that are compatible with coalesced request.
382 private void coalesceCompatibleRequests(TmfCoalescedEventRequest request
) {
383 Iterator
<TmfCoalescedEventRequest
> iter
= getPendingRequests().iterator();
384 while (iter
.hasNext()) {
385 TmfCoalescedEventRequest pendingRequest
= iter
.next();
386 if (request
.isCompatible(pendingRequest
)) {
387 request
.addRequest(pendingRequest
);
388 if (TmfCoreTracer
.isRequestTraced()) {
389 TmfCoreTracer
.traceRequest(pendingRequest
.getRequestId(), "COALESCED with " + request
.getRequestId()); //$NON-NLS-1$
390 TmfCoreTracer
.traceRequest(request
.getRequestId(), "now contains " + request
.getSubRequestIds()); //$NON-NLS-1$
397 // ------------------------------------------------------------------------
398 // Request processing
399 // ------------------------------------------------------------------------
408 protected void queueRequest(final ITmfEventRequest request
) {
410 if (fExecutor
.isShutdown()) {
415 TmfEventThread thread
= new TmfEventThread(this, request
);
417 if (TmfCoreTracer
.isRequestTraced()) {
418 TmfCoreTracer
.traceRequest(request
.getRequestId(), "QUEUED"); //$NON-NLS-1$
421 fExecutor
.execute(thread
);
425 * Initialize the provider based on the request. The context is provider
426 * specific and will be updated by getNext().
430 * @return An application specific context; null if request can't be
434 public abstract ITmfContext
armRequest(ITmfEventRequest request
);
437 * Checks if the data meets the request completion criteria.
444 * The number of events read so far
445 * @return true if completion criteria is met
448 public boolean isCompleted(ITmfEventRequest request
, ITmfEvent event
, int nbRead
) {
449 boolean requestCompleted
= isCompleted2(request
, nbRead
);
450 if (!requestCompleted
) {
451 ITmfTimestamp endTime
= request
.getRange().getEndTime();
452 return event
.getTimestamp().compareTo(endTime
) > 0;
454 return requestCompleted
;
457 private static boolean isCompleted2(ITmfEventRequest request
,int nbRead
) {
458 return request
.isCompleted() || nbRead
>= request
.getNbRequested();
461 // ------------------------------------------------------------------------
462 // Pass-through's to the request executor
463 // ------------------------------------------------------------------------
466 * @return the shutdown state (i.e. if it is accepting new requests)
469 protected boolean executorIsShutdown() {
470 return fExecutor
.isShutdown();
474 * @return the termination state
477 protected boolean executorIsTerminated() {
478 return fExecutor
.isTerminated();
481 // ------------------------------------------------------------------------
483 // ------------------------------------------------------------------------
486 * Handler for the start synch signal
492 public void startSynch(TmfStartSynchSignal signal
) {
493 synchronized (fLock
) {
499 * Handler for the end synch signal
505 public void endSynch(TmfEndSynchSignal signal
) {
506 synchronized (fLock
) {
508 if (fSignalDepth
== 0) {
515 public ITmfEventProvider
getParent() {
516 synchronized (fLock
) {
522 public void setParent(ITmfEventProvider parent
) {
523 if (!(parent
instanceof TmfEventProvider
)) {
524 throw new IllegalArgumentException();
527 synchronized (fLock
) {
528 fParent
= (TmfEventProvider
) parent
;
533 public List
<ITmfEventProvider
> getChildren() {
534 synchronized (fChildren
) {
535 List
<ITmfEventProvider
> list
= new ArrayList
<>();
536 list
.addAll(fChildren
);
542 public <T
extends ITmfEventProvider
> List
<T
> getChildren(Class
<T
> clazz
) {
543 List
<T
> list
= new ArrayList
<>();
544 synchronized (fChildren
) {
545 for (TmfEventProvider child
: fChildren
) {
546 if (clazz
.isAssignableFrom(child
.getClass())) {
547 list
.add(clazz
.cast(child
));
555 public ITmfEventProvider
getChild(String name
) {
556 synchronized (fChildren
) {
557 for (TmfEventProvider child
: fChildren
) {
558 if (child
.getName().equals(name
)) {
566 @SuppressWarnings("null")
568 public ITmfEventProvider
getChild(int index
) {
569 return fChildren
.get(index
);
573 public void addChild(ITmfEventProvider child
) {
574 if (!(child
instanceof TmfEventProvider
)) {
575 throw new IllegalArgumentException();
577 child
.setParent(this);
578 fChildren
.add((TmfEventProvider
)child
);
582 public int getNbChildren() {
583 return fChildren
.size();
587 public boolean providesEvent(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
.providesEvent(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
;