tmf: Fix regression in event requests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / component / TmfDataProvider.java
CommitLineData
8c8bf09f 1/*******************************************************************************
5419a136 2 * Copyright (c) 2009, 2010 Ericsson
0283f7ff 3 *
8c8bf09f
ASL
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
0283f7ff 8 *
8c8bf09f
ASL
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
5419a136 11 * Francois Chouinard - Replace background requests by pre-emptable requests
8c8bf09f
ASL
12 *******************************************************************************/
13
6c13869b 14package org.eclipse.linuxtools.tmf.core.component;
8c8bf09f 15
8c8bf09f
ASL
16import java.util.Vector;
17import java.util.concurrent.BlockingQueue;
18import java.util.concurrent.LinkedBlockingQueue;
9b635e61 19import java.util.concurrent.SynchronousQueue;
8c8bf09f 20
5500a7f0 21import org.eclipse.linuxtools.internal.tmf.core.TmfCoreTracer;
96b353c5 22import org.eclipse.linuxtools.internal.tmf.core.component.TmfEventThread;
8fd82db5 23import org.eclipse.linuxtools.internal.tmf.core.component.TmfProviderManager;
5419a136 24import org.eclipse.linuxtools.internal.tmf.core.request.TmfCoalescedDataRequest;
8fd82db5 25import org.eclipse.linuxtools.internal.tmf.core.request.TmfRequestExecutor;
72f1e62a 26import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
5419a136
AM
27import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
28import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType;
29import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
6c13869b
FC
30import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
31import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
32import org.eclipse.linuxtools.tmf.core.signal.TmfStartSynchSignal;
33import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
8c8bf09f
ASL
34
35/**
5419a136 36 * An abstract base class that implements ITmfDataProvider.
8c8bf09f 37 * <p>
8fd82db5
FC
38 * This abstract class implements the housekeeping methods to register/
39 * de-register the event provider and to handle generically the event requests.
8c8bf09f 40 * <p>
8fd82db5
FC
41 * The concrete class can either re-implement processRequest() entirely or just
42 * implement the hooks (initializeContext() and getNext()).
8c8bf09f 43 * <p>
0283f7ff 44 *
8fd82db5 45 * @author Francois Chouinard
96b353c5 46 * @version 1.1
8c8bf09f 47 */
5419a136 48public abstract class TmfDataProvider extends TmfComponent implements ITmfDataProvider {
8c8bf09f 49
948b0607
FC
50 // ------------------------------------------------------------------------
51 // Constants
52 // ------------------------------------------------------------------------
550d787e 53
063f0d27 54 /** Default amount of events per request "chunk" */
00641a97 55 public static final int DEFAULT_BLOCK_SIZE = 50000;
063f0d27
AM
56
57 /** Default size of the queue */
00641a97
FC
58 public static final int DEFAULT_QUEUE_SIZE = 1000;
59
948b0607 60 // ------------------------------------------------------------------------
12c155f5 61 // Attributes
948b0607 62 // ------------------------------------------------------------------------
8c8bf09f 63
6f4e8ec0 64 /** The type of event handled by this provider */
6256d8ad 65 protected Class<? extends ITmfEvent> fType;
6f4e8ec0
AM
66
67 /** Is there some data being logged? */
12c155f5 68 protected boolean fLogData;
6f4e8ec0
AM
69
70 /** Are errors being logged? */
12c155f5 71 protected boolean fLogError;
f6b14ce2 72
6f4e8ec0 73 /** Queue of events */
6256d8ad 74 protected BlockingQueue<ITmfEvent> fDataQueue;
6f4e8ec0
AM
75
76 /** Size of the fDataQueue */
77 protected int fQueueSize = DEFAULT_QUEUE_SIZE;
78
fc7cd0be 79 private TmfRequestExecutor fExecutor;
948b0607
FC
80
81 private int fSignalDepth = 0;
045df77d 82 private final Object fLock = new Object();
951d134a 83
c1c69938
FC
84 private int fRequestPendingCounter = 0;
85
948b0607
FC
86 // ------------------------------------------------------------------------
87 // Constructors
88 // ------------------------------------------------------------------------
89
063f0d27
AM
90 /**
91 * Default constructor
92 */
12c155f5 93 public TmfDataProvider() {
00641a97 94 super();
12c155f5 95 fQueueSize = DEFAULT_QUEUE_SIZE;
6256d8ad 96 fDataQueue = new LinkedBlockingQueue<ITmfEvent>(fQueueSize);
12c155f5
FC
97 fExecutor = new TmfRequestExecutor();
98 }
99
063f0d27
AM
100 /**
101 * Initialize this data provider
102 *
103 * @param name
104 * Name of the provider
105 * @param type
106 * The type of events that will be handled
107 */
6256d8ad 108 public void init(String name, Class<? extends ITmfEvent> type) {
12c155f5 109 super.init(name);
3791b5df 110 fType = type;
6256d8ad 111 fDataQueue = (fQueueSize > 1) ? new LinkedBlockingQueue<ITmfEvent>(fQueueSize) : new SynchronousQueue<ITmfEvent>();
12c155f5
FC
112
113 fExecutor = new TmfRequestExecutor();
114 fSignalDepth = 0;
115
5500a7f0 116 fLogData = TmfCoreTracer.isEventTraced();
5419a136 117// fLogError = TmfCoreTracer.isErrorTraced();
12c155f5
FC
118
119 TmfProviderManager.register(fType, this);
120 }
121
6f4e8ec0
AM
122 /**
123 * Constructor specifying the event type and the queue size.
124 *
125 * @param name
126 * Name of the provider
127 * @param type
128 * Type of event that will be handled
129 * @param queueSize
130 * Size of the event queue
131 */
6256d8ad 132 protected TmfDataProvider(String name, Class<? extends ITmfEvent> type, int queueSize) {
00641a97 133 this();
948b0607 134 fQueueSize = queueSize;
00641a97 135 init(name, type);
948b0607
FC
136 }
137
063f0d27
AM
138 /**
139 * Copy constructor
140 *
141 * @param other
142 * The other object to copy
143 */
6256d8ad 144 public TmfDataProvider(TmfDataProvider other) {
00641a97
FC
145 this();
146 init(other.getName(), other.fType);
147 }
550d787e 148
063f0d27
AM
149 /**
150 * Standard constructor. Instantiate and initialize at the same time.
151 *
152 * @param name
153 * Name of the provider
154 * @param type
155 * The type of events that will be handled
156 */
6256d8ad 157 public TmfDataProvider(String name, Class<? extends ITmfEvent> type) {
00641a97 158 this(name, type, DEFAULT_QUEUE_SIZE);
948b0607
FC
159 }
160
161 @Override
162 public void dispose() {
163 TmfProviderManager.deregister(fType, this);
164 fExecutor.stop();
165 super.dispose();
5419a136 166 // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped");
948b0607
FC
167 }
168
00641a97
FC
169 // ------------------------------------------------------------------------
170 // Accessors
171 // ------------------------------------------------------------------------
172
063f0d27
AM
173 /**
174 * Get the queue size of this provider
175 *
176 * @return The size of the queue
177 */
948b0607
FC
178 public int getQueueSize() {
179 return fQueueSize;
180 }
181
063f0d27
AM
182 /**
183 * Get the event type this provider handles
184 *
185 * @return The type of ITmfEvent
186 */
948b0607
FC
187 public Class<?> getType() {
188 return fType;
189 }
190
191 // ------------------------------------------------------------------------
192 // ITmfRequestHandler
193 // ------------------------------------------------------------------------
194
195 @Override
5419a136 196 public void sendRequest(final ITmfDataRequest request) {
948b0607
FC
197 synchronized (fLock) {
198 if (fSignalDepth > 0) {
199 coalesceDataRequest(request);
200 } else {
201 dispatchRequest(request);
202 }
203 }
204 }
205
948b0607 206 @Override
c1c69938 207 public void fireRequest() {
948b0607
FC
208 synchronized (fLock) {
209 if (fRequestPendingCounter > 0) {
210 return;
211 }
212 if (fPendingCoalescedRequests.size() > 0) {
5419a136 213 for (TmfDataRequest request : fPendingCoalescedRequests) {
948b0607
FC
214 dispatchRequest(request);
215 }
216 fPendingCoalescedRequests.clear();
217 }
218 }
219 }
220
221 /**
063f0d27 222 * Increments/decrements the pending requests counters and fires the request
0283f7ff
FC
223 * if necessary (counter == 0). Used for coalescing requests across multiple
224 * TmfDataProvider's.
063f0d27 225 *
948b0607 226 * @param isIncrement
0283f7ff
FC
227 * Should we increment (true) or decrement (false) the pending
228 * counter
948b0607 229 */
c1c69938
FC
230 @Override
231 public void notifyPendingRequest(boolean isIncrement) {
948b0607 232 synchronized (fLock) {
c1c69938
FC
233 if (isIncrement) {
234 if (fSignalDepth > 0) {
235 fRequestPendingCounter++;
236 }
237 } else {
238 if (fRequestPendingCounter > 0) {
239 fRequestPendingCounter--;
240 }
948b0607 241
c1c69938
FC
242 // fire request if all pending requests are received
243 if (fRequestPendingCounter == 0) {
244 fireRequest();
245 }
246 }
247 }
948b0607
FC
248 }
249
250 // ------------------------------------------------------------------------
251 // Coalescing (primitive test...)
252 // ------------------------------------------------------------------------
253
5419a136
AM
254 /** List of coalesced requests */
255 protected Vector<TmfCoalescedDataRequest> fPendingCoalescedRequests = new Vector<TmfCoalescedDataRequest>();
256
6f4e8ec0
AM
257 /**
258 * Create a new request from an existing one, and add it to the coalesced
259 * requests
260 *
5419a136
AM
261 * @param request
262 * The request to copy
6f4e8ec0 263 */
5419a136 264 protected void newCoalescedDataRequest(ITmfDataRequest request) {
948b0607 265 synchronized (fLock) {
5419a136
AM
266 TmfCoalescedDataRequest coalescedRequest = new TmfCoalescedDataRequest(request.getDataType(), request.getIndex(),
267 request.getNbRequested(), request.getBlockSize(), request.getExecType());
268 coalescedRequest.addRequest(request);
5500a7f0
FC
269 if (TmfCoreTracer.isRequestTraced()) {
270 TmfCoreTracer.traceRequest(request, "COALESCED with " + coalescedRequest.getRequestId()); //$NON-NLS-1$
271 TmfCoreTracer.traceRequest(coalescedRequest, "now contains " + coalescedRequest.getSubRequestIds()); //$NON-NLS-1$
948b0607
FC
272 }
273 fPendingCoalescedRequests.add(coalescedRequest);
274 }
275 }
276
6f4e8ec0
AM
277 /**
278 * Add an existing requests to the list of coalesced ones
279 *
5419a136
AM
280 * @param request
281 * The request to add to the list
6f4e8ec0 282 */
5419a136 283 protected void coalesceDataRequest(ITmfDataRequest request) {
948b0607 284 synchronized (fLock) {
5419a136 285 for (TmfCoalescedDataRequest coalescedRequest : fPendingCoalescedRequests) {
948b0607
FC
286 if (coalescedRequest.isCompatible(request)) {
287 coalescedRequest.addRequest(request);
5500a7f0
FC
288 if (TmfCoreTracer.isRequestTraced()) {
289 TmfCoreTracer.traceRequest(request, "COALESCED with " + coalescedRequest.getRequestId()); //$NON-NLS-1$
290 TmfCoreTracer.traceRequest(coalescedRequest, "now contains " + coalescedRequest.getSubRequestIds()); //$NON-NLS-1$
948b0607
FC
291 }
292 return;
293 }
294 }
295 newCoalescedDataRequest(request);
296 }
297 }
298
299 // ------------------------------------------------------------------------
300 // Request processing
301 // ------------------------------------------------------------------------
302
5419a136
AM
303 private void dispatchRequest(final ITmfDataRequest request) {
304 if (request.getExecType() == ExecutionType.FOREGROUND) {
948b0607 305 queueRequest(request);
0283f7ff 306 } else {
5419a136 307 queueBackgroundRequest(request, request.getBlockSize(), true);
0283f7ff 308 }
948b0607
FC
309 }
310
6f4e8ec0
AM
311 /**
312 * Queue a request.
313 *
5419a136
AM
314 * @param request
315 * The data request
6f4e8ec0 316 */
5419a136 317 protected void queueRequest(final ITmfDataRequest request) {
948b0607
FC
318
319 if (fExecutor.isShutdown()) {
320 request.cancel();
321 return;
322 }
323
96b353c5 324 TmfEventThread thread = new TmfEventThread(this, request);
948b0607 325
5500a7f0
FC
326 if (TmfCoreTracer.isRequestTraced()) {
327 TmfCoreTracer.traceRequest(request, "QUEUED"); //$NON-NLS-1$
0283f7ff 328 }
96b353c5 329
948b0607 330 fExecutor.execute(thread);
948b0607
FC
331 }
332
6f4e8ec0
AM
333 /**
334 * Queue a background request
335 *
5419a136
AM
336 * @param request
337 * The request
338 * @param blockSize
339 * The request should be split in chunks of this size
340 * @param indexing
341 * Should we index the chunks
6f4e8ec0 342 */
5419a136 343 protected void queueBackgroundRequest(final ITmfDataRequest request, final int blockSize, final boolean indexing) {
96b353c5 344 queueRequest(request);
948b0607
FC
345 }
346
347 /**
d337369a
FC
348 * Initialize the provider based on the request. The context is provider
349 * specific and will be updated by getNext().
0283f7ff 350 *
948b0607 351 * @param request
6f4e8ec0 352 * The request
80b237e6 353 * @return An application specific context; null if request can't be
6f4e8ec0 354 * serviced
80b237e6 355 * @since 2.0
948b0607 356 */
5419a136 357 public abstract ITmfContext armRequest(ITmfDataRequest request);
948b0607 358
c32744d6
FC
359// /**
360// * Return the next event based on the context supplied. The context
361// * will be updated for the subsequent read.
0283f7ff 362// *
c32744d6
FC
363// * @param context the trace read context (updated)
364// * @return the event referred to by context
365// */
366// public abstract T getNext(ITmfContext context);
948b0607
FC
367
368 /**
369 * Checks if the data meets the request completion criteria.
0283f7ff 370 *
0d9a6d76 371 * @param request the request
5419a136 372 * @param data the data to verify
0d9a6d76
FC
373 * @param nbRead the number of events read so far
374 * @return true if completion criteria is met
948b0607 375 */
5419a136
AM
376 public boolean isCompleted(ITmfDataRequest request, ITmfEvent data, int nbRead) {
377 return request.isCompleted() || nbRead >= request.getNbRequested();
948b0607
FC
378 }
379
fc7cd0be
AM
380 // ------------------------------------------------------------------------
381 // Pass-through's to the request executor
382 // ------------------------------------------------------------------------
383
384 /**
385 * @return the shutdown state (i.e. if it is accepting new requests)
386 * @since 2.0
387 */
388 protected boolean executorIsShutdown() {
389 return fExecutor.isShutdown();
390 }
391
392 /**
393 * @return the termination state
394 * @since 2.0
395 */
396 protected boolean executorIsTerminated() {
397 return fExecutor.isTerminated();
398 }
399
948b0607
FC
400 // ------------------------------------------------------------------------
401 // Signal handlers
402 // ------------------------------------------------------------------------
403
063f0d27
AM
404 /**
405 * Handler for the start synch signal
406 *
407 * @param signal
408 * Incoming signal
409 */
948b0607
FC
410 @TmfSignalHandler
411 public void startSynch(TmfStartSynchSignal signal) {
412 synchronized (fLock) {
413 fSignalDepth++;
414 }
415 }
416
063f0d27
AM
417 /**
418 * Handler for the end synch signal
419 *
420 * @param signal
421 * Incoming signal
422 */
948b0607
FC
423 @TmfSignalHandler
424 public void endSynch(TmfEndSynchSignal signal) {
045df77d 425 synchronized (fLock) {
948b0607
FC
426 fSignalDepth--;
427 if (fSignalDepth == 0) {
428 fireRequest();
429 }
045df77d 430 }
948b0607 431 }
8c8bf09f
ASL
432
433}
This page took 0.08063 seconds and 5 git commands to generate.