1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.request
;
15 import java
.util
.concurrent
.CountDownLatch
;
17 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Tracer
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 * TmfDataRequests are used to obtain blocks of contiguous data from a data provider. Open ranges can be used,
22 * especially for continuous streaming.
24 * The request is processed asynchronously by a TmfProvider and, as blocks of data become available, handleData() is
25 * invoked synchronously for each block. Upon return, the data instances go out of scope and become eligible for gc. It
26 * is is thus the responsibility of the requester to either clone or keep a reference to the data it wishes to track
29 * This data block approach is used to avoid busting the heap for very large trace files. The block size is
32 * The TmfProvider indicates that the request is completed by calling done(). The request can be canceled at any time
38 * <code><i>TmfTimeWindow range = new TmfTimewindow(...);
39 * TmfDataRequest<DataType[]> request = new TmfDataRequest<DataType[]>(DataType.class, 0, NB_EVENTS, BLOCK_SIZE) {
40 * public void handleData() {
41 * DataType[] data = request.getData();
42 * for (DataType e : data) {
46 * public void handleSuccess() {
50 * public void handleFailure() {
54 * public void handleCancel() {
59 * fProcessor.process(request, true);
63 * TODO: Consider decoupling from "time range", "rank", etc and for the more generic notion of "criteria". This would
64 * allow to extend for "time range", etc instead of providing specialized constructors. This also means removing the
65 * criteria info from the data structure (with the possible exception of fNbRequested). The nice thing about it is that
66 * it would prepare us well for the coming generation of analysis tools.
68 * TODO: Implement request failures (codes, etc...)
70 * @param <T> The request data type
73 * @author Francois Chouinard
75 public abstract class TmfDataRequest
<T
extends ITmfEvent
> implements ITmfDataRequest
<T
> {
77 // ------------------------------------------------------------------------
79 // ------------------------------------------------------------------------
81 /** The default maximum number of events per chunk */
82 public static final int DEFAULT_BLOCK_SIZE
= 1000;
84 /** The request count for all the events */
85 public static final int ALL_DATA
= Integer
.MAX_VALUE
;
87 private static int fRequestNumber
= 0;
89 // ------------------------------------------------------------------------
91 // ------------------------------------------------------------------------
93 private final Class
<T
> fDataType
;
94 private final ExecutionType fExecType
;
95 private final int fRequestId
; // A unique request ID
96 protected long fIndex
; // The index (rank) of the requested event
97 protected int fNbRequested
; // The number of requested events (ALL_DATA for all)
98 private final int fBlockSize
; // The block size (for BG requests)
99 private int fNbRead
; // The number of reads so far
101 private CountDownLatch startedLatch
= new CountDownLatch(1);
102 private CountDownLatch completedLatch
= new CountDownLatch(1);
103 private boolean fRequestRunning
;
104 private boolean fRequestCompleted
;
105 private boolean fRequestFailed
;
106 private boolean fRequestCanceled
;
108 // ------------------------------------------------------------------------
110 // ------------------------------------------------------------------------
113 * Resets the request counter (used for testing)
115 public static void reset() {
120 * Request all the events of a given type (high priority)
121 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
123 * @param dataType the requested data type
125 public TmfDataRequest(Class
<T
> dataType
) {
126 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
130 * Request all the events of a given type (given priority)
131 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
133 * @param dataType the requested data type
134 * @param priority the requested execution priority
136 public TmfDataRequest(Class
<T
> dataType
, ExecutionType priority
) {
137 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, priority
);
141 * Request all the events of a given type from the given index (high priority)
142 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
144 * @param dataType the requested data type
145 * @param index the index of the first event to retrieve
147 public TmfDataRequest(Class
<T
> dataType
, long index
) {
148 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
152 * Request all the events of a given type from the given index (given priority)
153 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
155 * @param dataType the requested data type
156 * @param index the index of the first event to retrieve
157 * @param priority the requested execution priority
159 public TmfDataRequest(Class
<T
> dataType
, long index
, ExecutionType priority
) {
160 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, priority
);
164 * Request 'n' events of a given type from the given index (high priority)
165 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
167 * @param dataType the requested data type
168 * @param index the index of the first event to retrieve
169 * @param nbRequested the number of events requested
171 public TmfDataRequest(Class
<T
> dataType
, long index
, int nbRequested
) {
172 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
176 * Request 'n' events of a given type from the given index (given priority)
177 * Events are returned in blocks of the default size (DEFAULT_BLOCK_SIZE).
179 * @param dataType the requested data type
180 * @param index the index of the first event to retrieve
181 * @param nbRequested the number of events requested
182 * @param priority the requested execution priority
184 public TmfDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, ExecutionType priority
) {
185 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, priority
);
189 * Request 'n' events of a given type from the given index (high priority).
190 * Events are returned in blocks of the given size.
192 * @param dataType the requested data type
193 * @param index the index of the first event to retrieve
194 * @param nbRequested the number of events requested
195 * @param blockSize the number of events per block
197 public TmfDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, int blockSize
) {
198 this(dataType
, index
, nbRequested
, blockSize
, ExecutionType
.FOREGROUND
);
202 * Request 'n' events of a given type from the given index (given priority).
203 * Events are returned in blocks of the given size.
205 * @param dataType the requested data type
206 * @param index the index of the first event to retrieve
207 * @param nbRequested the number of events requested
208 * @param blockSize the number of events per block
209 * @param priority the requested execution priority
211 public TmfDataRequest(Class
<T
> dataType
, long index
, int nbRequested
, int blockSize
, ExecutionType priority
) {
212 fRequestId
= fRequestNumber
++;
213 fDataType
= dataType
;
215 fNbRequested
= nbRequested
;
216 fBlockSize
= blockSize
;
217 fExecType
= priority
;
220 fRequestRunning
= false;
221 fRequestCompleted
= false;
222 fRequestFailed
= false;
223 fRequestCanceled
= false;
225 if (!(this instanceof ITmfEventRequest
) && Tracer
.isRequestTraced()) {
226 String type
= getClass().getName();
227 type
= type
.substring(type
.lastIndexOf('.') + 1);
228 @SuppressWarnings("nls")
229 String message
= "CREATED "
230 + (getExecType() == ITmfDataRequest
.ExecutionType
.BACKGROUND ?
"(BG)" : "(FG)")
231 + " Type=" + type
+ " Index=" + getIndex() + " NbReq=" + getNbRequested()
232 + " DataType=" + getDataType().getSimpleName();
233 Tracer
.traceRequest(this, message
);
240 @SuppressWarnings("unused")
241 private TmfDataRequest(TmfDataRequest
<T
> other
) {
242 this(null, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
);
245 // ------------------------------------------------------------------------
247 // ------------------------------------------------------------------------
250 * @return the request ID
253 public int getRequestId() {
258 * @return the index of the first event requested
261 public long getIndex() {
266 * @return the execution type (priority)
269 public ExecutionType
getExecType() {
274 * @return the number of requested events (ALL_DATA = all)
277 public int getNbRequested() {
282 * @return the block size (for BG requests)
285 public int getBlockSize() {
290 * @return the number of events read so far
293 public synchronized int getNbRead() {
298 * @return indicates if the request is currently running
301 public synchronized boolean isRunning() {
302 return fRequestRunning
;
306 * @return indicates if the request is completed
309 public synchronized boolean isCompleted() {
310 return fRequestCompleted
;
314 * @return indicates if the request has failed
317 public synchronized boolean isFailed() {
318 return fRequestFailed
;
322 * @return indicates if the request is canceled
325 public synchronized boolean isCancelled() {
326 return fRequestCanceled
;
330 * @return the requested data type
333 public Class
<T
> getDataType() {
337 // ------------------------------------------------------------------------
339 // ------------------------------------------------------------------------
342 * this method is called by the event provider to set the index corresponding to the time range start time
345 * the start time index
347 protected void setIndex(int index
) {
351 // ------------------------------------------------------------------------
353 // ------------------------------------------------------------------------
356 * Handle incoming data, one event at a time i.e. this method is invoked
357 * for every data item obtained by the request.
359 * - Data items are received in the order they appear in the stream
360 * - Called by the request processor, in its execution thread, every time
361 * a block of data becomes available.
362 * - Request processor performs a synchronous call to handleData() i.e.
363 * its execution threads holds until handleData() returns.
364 * - Original data items are disposed of on return i.e. keep a reference
365 * (or a copy) if some persistence is needed between invocations.
366 * - When there is no more data, done() is called.
368 * @param data a piece of data
371 public void handleData(T data
) {
378 public void handleStarted() {
379 if (Tracer
.isRequestTraced())
381 Tracer
.traceRequest(this, "STARTED"); //$NON-NLS-1$
386 * Handle the completion of the request. It is called when there is no
387 * more data available either because:
388 * - the request completed normally
389 * - the request failed
390 * - the request was canceled
392 * As a convenience, handleXXXX methods are provided. They are meant to be
393 * overridden by the application if it needs to handle these conditions.
396 public void handleCompleted() {
397 boolean requestFailed
= false;
398 boolean requestCanceled
= false;
399 synchronized (this) {
400 requestFailed
= fRequestFailed
;
401 requestCanceled
= fRequestCanceled
;
406 } else if (requestCanceled
) {
411 if (Tracer
.isRequestTraced())
413 Tracer
.traceRequest(this, "COMPLETED (" + fNbRead
+ " events read)"); //$NON-NLS-1$ //$NON-NLS-2$
418 public void handleSuccess() {
419 if (Tracer
.isRequestTraced())
421 Tracer
.traceRequest(this, "SUCCEEDED"); //$NON-NLS-1$
426 public void handleFailure() {
427 if (Tracer
.isRequestTraced())
429 Tracer
.traceRequest(this, "FAILED"); //$NON-NLS-1$
434 public void handleCancel() {
435 if (Tracer
.isRequestTraced())
437 Tracer
.traceRequest(this, "CANCELLED"); //$NON-NLS-1$
442 * To suspend the client thread until the request starts (or is canceled).
444 * @throws InterruptedException
445 * If the thread was interrupted while waiting
447 public void waitForStart() throws InterruptedException
{
448 while (!fRequestRunning
) {
449 startedLatch
.await();
454 * To suspend the client thread until the request completes (or is
457 * @throws InterruptedException
458 * If the thread was interrupted while waiting
461 public void waitForCompletion() throws InterruptedException
{
462 while (!fRequestCompleted
) {
463 completedLatch
.await();
468 * Called by the request processor upon starting to service the request.
471 public void start() {
472 synchronized (this) {
473 fRequestRunning
= true;
476 startedLatch
.countDown();
480 * Called by the request processor upon completion.
484 synchronized (this) {
485 if (!fRequestCompleted
) {
486 fRequestRunning
= false;
487 fRequestCompleted
= true;
495 completedLatch
.countDown();
500 * Called by the request processor upon failure.
504 synchronized (this) {
505 fRequestFailed
= true;
511 * Called by the request processor upon cancellation.
514 public void cancel() {
515 synchronized (this) {
516 fRequestCanceled
= true;
521 // ------------------------------------------------------------------------
523 // ------------------------------------------------------------------------
526 // All requests have a unique id
527 public int hashCode() {
528 return getRequestId();
532 public boolean equals(Object other
) {
533 if (other
instanceof TmfDataRequest
<?
>) {
534 TmfDataRequest
<?
> request
= (TmfDataRequest
<?
>) other
;
535 return (request
.fDataType
== fDataType
) && (request
.fIndex
== fIndex
)
536 && (request
.fNbRequested
== fNbRequested
);
542 @SuppressWarnings("nls")
543 public String
toString() {
544 return "[TmfDataRequest(" + fRequestId
+ "," + fDataType
.getSimpleName() + "," + fIndex
+ "," + fNbRequested
545 + "," + getBlockSize() + ")]";