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
.request
;
15 import java
.util
.concurrent
.CountDownLatch
;
17 import org
.eclipse
.linuxtools
.tmf
.Tracer
;
18 import org
.eclipse
.linuxtools
.tmf
.event
.TmfData
;
21 * <b><u>TmfDataRequest</u></b>
23 * TmfDataRequests are used to obtain blocks of contiguous data from a data provider. Open ranges can be used,
24 * especially for continuous streaming.
26 * The request is processed asynchronously by a TmfProvider and, as blocks of data become available, handleData() is
27 * invoked synchronously for each block. Upon return, the data instances go out of scope and become eligible for gc. It
28 * is is thus the responsibility of the requester to either clone or keep a reference to the data it wishes to track
31 * This data block approach is used to avoid busting the heap for very large trace files. The block size is
34 * The TmfProvider indicates that the request is completed by calling done(). The request can be canceled at any time
40 * <code><i>TmfTimeWindow range = new TmfTimewindow(...);
41 * TmfDataRequest<DataType[]> request = new TmfDataRequest<DataType[]>(DataType.class, 0, NB_EVENTS, BLOCK_SIZE) {
42 * public void handleData() {
43 * DataType[] data = request.getData();
44 * for (DataType e : data) {
48 * public void handleSuccess() {
52 * public void handleFailure() {
56 * public void handleCancel() {
61 * fProcessor.process(request, true);
65 * TODO: Consider decoupling from "time range", "rank", etc and for the more generic notion of "criteria". This would
66 * allow to extend for "time range", etc instead of providing specialized constructors. This also means removing the
67 * criteria info from the data structure (with the possible exception of fNbRequested). The nice thing about it is that
68 * it would prepare us well for the coming generation of analysis tools.
70 * TODO: Implement request failures (codes, etc...)
72 public abstract class TmfDataRequest
<T
extends TmfData
> implements ITmfDataRequest
<T
> {
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
78 // The default maximum number of events per chunk
79 public static final int DEFAULT_BLOCK_SIZE
= 1000;
81 // The request count for all the events
82 public static final int ALL_DATA
= Integer
.MAX_VALUE
;
84 private static int fRequestNumber
= 0;
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
90 private final Class
<T
> fDataType
;
91 private final ExecutionType fExecType
;
92 private final int fRequestId
; // A unique request ID
93 private int fIndex
; // The index (rank) of the requested event
94 private final int fNbRequested
; // The number of requested events (ALL_DATA for all)
95 private final int fBlockSize
; // The block size (for BG requests)
96 private int fNbRead
; // The number of reads so far
98 private CountDownLatch startedLatch
= new CountDownLatch(1);
99 private CountDownLatch completedLatch
= new CountDownLatch(1);
100 private boolean fRequestRunning
= false;
101 private boolean fRequestCompleted
= false;
102 private boolean fRequestFailed
= false;
103 private boolean fRequestCanceled
= false;
105 // ------------------------------------------------------------------------
107 // ------------------------------------------------------------------------
110 * Resets the request counter (used for testing)
112 public static void reset() {
117 * Default constructor
120 * the requested data type
122 public TmfDataRequest(Class
<T
> dataType
) {
123 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
126 public TmfDataRequest(Class
<T
> dataType
, ExecutionType execType
) {
127 this(dataType
, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
, execType
);
132 * the requested data type
134 * the number of data items requested
136 public TmfDataRequest(Class
<T
> dataType
, int index
) {
137 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
140 public TmfDataRequest(Class
<T
> dataType
, int index
, ExecutionType execType
) {
141 this(dataType
, index
, ALL_DATA
, DEFAULT_BLOCK_SIZE
, execType
);
146 * the requested data type
148 * the index (rank) of the first event requested
150 * the number of data items per block
152 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
) {
153 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, ExecutionType
.FOREGROUND
);
156 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, ExecutionType execType
) {
157 this(dataType
, index
, nbRequested
, DEFAULT_BLOCK_SIZE
, execType
);
162 * the requested data type
164 * the index (rank) of the first event requested
166 * the number of data items requested
168 * the number of data items per block
170 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
) {
171 this(dataType
, index
, nbRequested
, blockSize
, ExecutionType
.FOREGROUND
);
174 public TmfDataRequest(Class
<T
> dataType
, int index
, int nbRequested
, int blockSize
, ExecutionType execType
) {
175 fRequestId
= fRequestNumber
++;
176 fDataType
= dataType
;
178 fNbRequested
= nbRequested
;
179 fBlockSize
= blockSize
;
180 fExecType
= execType
;
182 if (Tracer
.isRequestTraced())
183 Tracer
.traceRequest(this, "created"); //$NON-NLS-1$
189 @SuppressWarnings("unused")
190 private TmfDataRequest(TmfDataRequest
<T
> other
) {
191 this(null, 0, ALL_DATA
, DEFAULT_BLOCK_SIZE
);
194 // ------------------------------------------------------------------------
196 // ------------------------------------------------------------------------
199 * @return the request ID
202 public int getRequestId() {
207 * @return the index of the first event requested
210 public int getIndex() {
215 * @return the index of the first event requested
218 public ExecutionType
getExecType() {
223 * @return the number of requested events (ALL_DATA = all)
226 public int getNbRequested() {
231 * @return the block size (for BG requests)
234 public int getBlockSize() {
239 * @return the number of events read so far
242 public synchronized int getNbRead() {
247 * @return indicates if the request is completed
250 public synchronized boolean isRunning() {
251 return fRequestRunning
;
255 * @return indicates if the request is completed
258 public synchronized boolean isCompleted() {
259 return fRequestCompleted
;
263 * @return indicates if the request is canceled
266 public synchronized boolean isFailed() {
267 return fRequestFailed
;
271 * @return indicates if the request is canceled
274 public synchronized boolean isCancelled() {
275 return fRequestCanceled
;
279 * @return the requested data type
282 public Class
<T
> getDataType() {
286 // ------------------------------------------------------------------------
288 // ------------------------------------------------------------------------
291 * this method is called by the event provider to set the index corresponding to the time range start time
294 * the start time index
296 protected void setIndex(int index
) {
300 // ------------------------------------------------------------------------
302 // ------------------------------------------------------------------------
305 * Sets the data object to specified value. To be called by the asynchronous method implementor.
312 * Handle a block of incoming data. This method is called every time a block of data becomes available.
314 * - Data items are received in the order they appear in the stream. - Called by the request processor, in its
315 * execution thread, every time a block of data becomes available. - Request processor performs a synchronous call
316 * to handlePartialResult() i.e. its execution threads holds until handlePartialData() returns. - Original data
317 * items are disposed of on return i.e. keep a reference (or a copy) if some persistence is needed between
318 * invocations. - When there is no more data, done() is called.
324 public void handleData(T data
) {
331 public void handleStarted() {
332 if (Tracer
.isRequestTraced())
333 Tracer
.traceRequest(this, "started"); //$NON-NLS-1$
337 * Handle the completion of the request. It is called when there is no more data available either because: - the
338 * request completed normally - the request failed - the request was canceled
340 * As a convenience, handleXXXX methods are provided. They are meant to be overridden by the application if it needs
341 * to handle these conditions.
344 public void handleCompleted() {
345 if (fRequestFailed
) {
347 } else if (fRequestCanceled
) {
352 if (Tracer
.isRequestTraced())
353 Tracer
.traceRequest(this, "completed (" + fNbRead
+ " events read)"); //$NON-NLS-1$
357 public void handleSuccess() {
358 if (Tracer
.isRequestTraced())
359 Tracer
.traceRequest(this, "succeeded"); //$NON-NLS-1$
363 public void handleFailure() {
364 if (Tracer
.isRequestTraced())
365 Tracer
.traceRequest(this, "failed"); //$NON-NLS-1$
369 public void handleCancel() {
370 if (Tracer
.isRequestTraced())
371 Tracer
.traceRequest(this, "cancelled"); //$NON-NLS-1$
375 * To suspend the client thread until the request starts (or is canceled).
377 * @throws InterruptedException
379 public void waitForStart() throws InterruptedException
{
380 while (!fRequestRunning
) {
381 startedLatch
.await();
386 * To suspend the client thread until the request completes (or is canceled).
388 * @throws InterruptedException
391 public void waitForCompletion() throws InterruptedException
{
392 while (!fRequestCompleted
) {
393 completedLatch
.await();
398 * Called by the request processor upon starting to service the request.
401 public void start() {
402 synchronized (this) {
403 fRequestRunning
= true;
406 startedLatch
.countDown();
410 * Called by the request processor upon completion.
414 synchronized (this) {
415 if (!fRequestCompleted
) {
416 fRequestRunning
= false;
417 fRequestCompleted
= true;
425 completedLatch
.countDown();
430 * Called by the request processor upon failure.
434 synchronized (this) {
435 fRequestFailed
= true;
441 * Called by the request processor upon cancellation.
444 public void cancel() {
445 synchronized (this) {
446 fRequestCanceled
= true;
451 // ------------------------------------------------------------------------
453 // ------------------------------------------------------------------------
456 // All requests have a unique id
457 public int hashCode() {
458 return getRequestId();
462 public boolean equals(Object other
) {
463 if (other
instanceof TmfDataRequest
<?
>) {
464 TmfDataRequest
<?
> request
= (TmfDataRequest
<?
>) other
;
465 return (request
.fDataType
== fDataType
) && (request
.fIndex
== fIndex
)
466 && (request
.fNbRequested
== fNbRequested
);
472 @SuppressWarnings("nls")
473 public String
toString() {
474 return "[TmfDataRequest(" + fRequestId
+ "," + fDataType
.getSimpleName() + "," + fIndex
+ "," + fNbRequested
475 + "," + getBlockSize() + ")]";