Commit | Line | Data |
---|---|---|
8c8bf09f ASL |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010 Ericsson | |
3 | * | |
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 | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
6c13869b | 13 | package org.eclipse.linuxtools.tmf.core.component; |
8c8bf09f | 14 | |
8c8bf09f ASL |
15 | import java.util.Vector; |
16 | import java.util.concurrent.BlockingQueue; | |
17 | import java.util.concurrent.LinkedBlockingQueue; | |
9b635e61 | 18 | import java.util.concurrent.SynchronousQueue; |
8c8bf09f | 19 | |
6c13869b FC |
20 | import org.eclipse.linuxtools.tmf.core.Tracer; |
21 | import org.eclipse.linuxtools.tmf.core.event.TmfData; | |
22 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest; | |
23 | import org.eclipse.linuxtools.tmf.core.request.TmfCoalescedDataRequest; | |
24 | import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest; | |
25 | import org.eclipse.linuxtools.tmf.core.request.TmfRequestExecutor; | |
26 | import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest.ExecutionType; | |
27 | import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal; | |
28 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler; | |
29 | import org.eclipse.linuxtools.tmf.core.signal.TmfStartSynchSignal; | |
30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; | |
8c8bf09f ASL |
31 | |
32 | /** | |
33 | * <b><u>TmfProvider</u></b> | |
34 | * <p> | |
35 | * The TmfProvider<T> is a provider for a data of type <T>. | |
36 | * <p> | |
12c155f5 FC |
37 | * This abstract class implements the housekeeking methods to register/ deregister the event provider and to handle |
38 | * generically the event requests. | |
8c8bf09f | 39 | * <p> |
12c155f5 FC |
40 | * The concrete class can either re-implement processRequest() entirely or just implement the hooks (initializeContext() |
41 | * and getNext()). | |
8c8bf09f ASL |
42 | * <p> |
43 | * TODO: Add support for providing multiple data types. | |
44 | */ | |
951d134a | 45 | public abstract class TmfDataProvider<T extends TmfData> extends TmfComponent implements ITmfDataProvider<T> { |
8c8bf09f | 46 | |
948b0607 FC |
47 | // ------------------------------------------------------------------------ |
48 | // Constants | |
49 | // ------------------------------------------------------------------------ | |
550d787e | 50 | |
00641a97 FC |
51 | public static final int DEFAULT_BLOCK_SIZE = 50000; |
52 | public static final int DEFAULT_QUEUE_SIZE = 1000; | |
53 | ||
948b0607 | 54 | // ------------------------------------------------------------------------ |
12c155f5 | 55 | // Attributes |
948b0607 | 56 | // ------------------------------------------------------------------------ |
8c8bf09f | 57 | |
12c155f5 FC |
58 | protected Class<T> fType; |
59 | protected boolean fLogData; | |
60 | protected boolean fLogError; | |
f6b14ce2 | 61 | |
00641a97 | 62 | protected int fQueueSize = DEFAULT_QUEUE_SIZE; |
12c155f5 FC |
63 | protected BlockingQueue<T> fDataQueue; |
64 | protected TmfRequestExecutor fExecutor; | |
948b0607 FC |
65 | |
66 | private int fSignalDepth = 0; | |
045df77d | 67 | private final Object fLock = new Object(); |
951d134a | 68 | |
c1c69938 FC |
69 | private int fRequestPendingCounter = 0; |
70 | ||
948b0607 FC |
71 | // ------------------------------------------------------------------------ |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
12c155f5 | 75 | public TmfDataProvider() { |
00641a97 | 76 | super(); |
12c155f5 FC |
77 | fQueueSize = DEFAULT_QUEUE_SIZE; |
78 | fDataQueue = new LinkedBlockingQueue<T>(fQueueSize); | |
79 | fExecutor = new TmfRequestExecutor(); | |
80 | } | |
81 | ||
82 | public void init(String name, Class<T> dataType) { | |
83 | super.init(name); | |
84 | fType = dataType; | |
12c155f5 FC |
85 | fDataQueue = (fQueueSize > 1) ? new LinkedBlockingQueue<T>(fQueueSize) : new SynchronousQueue<T>(); |
86 | ||
87 | fExecutor = new TmfRequestExecutor(); | |
88 | fSignalDepth = 0; | |
89 | ||
90 | fLogData = Tracer.isEventTraced(); | |
91 | fLogError = Tracer.isErrorTraced(); | |
92 | ||
93 | TmfProviderManager.register(fType, this); | |
94 | } | |
95 | ||
948b0607 | 96 | protected TmfDataProvider(String name, Class<T> type, int queueSize) { |
00641a97 | 97 | this(); |
948b0607 | 98 | fQueueSize = queueSize; |
00641a97 | 99 | init(name, type); |
948b0607 FC |
100 | } |
101 | ||
102 | public TmfDataProvider(TmfDataProvider<T> other) { | |
00641a97 FC |
103 | this(); |
104 | init(other.getName(), other.fType); | |
105 | } | |
550d787e | 106 | |
00641a97 FC |
107 | public TmfDataProvider(String name, Class<T> type) { |
108 | this(name, type, DEFAULT_QUEUE_SIZE); | |
948b0607 FC |
109 | } |
110 | ||
111 | @Override | |
112 | public void dispose() { | |
113 | TmfProviderManager.deregister(fType, this); | |
114 | fExecutor.stop(); | |
115 | super.dispose(); | |
12c155f5 | 116 | // if (Tracer.isComponentTraced()) Tracer.traceComponent(this, "stopped"); |
948b0607 FC |
117 | } |
118 | ||
00641a97 FC |
119 | // ------------------------------------------------------------------------ |
120 | // Accessors | |
121 | // ------------------------------------------------------------------------ | |
122 | ||
948b0607 FC |
123 | public int getQueueSize() { |
124 | return fQueueSize; | |
125 | } | |
126 | ||
127 | public Class<?> getType() { | |
128 | return fType; | |
129 | } | |
130 | ||
131 | // ------------------------------------------------------------------------ | |
132 | // ITmfRequestHandler | |
133 | // ------------------------------------------------------------------------ | |
134 | ||
135 | @Override | |
136 | public void sendRequest(final ITmfDataRequest<T> request) { | |
137 | synchronized (fLock) { | |
138 | if (fSignalDepth > 0) { | |
139 | coalesceDataRequest(request); | |
140 | } else { | |
141 | dispatchRequest(request); | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | /** | |
147 | * This method queues the coalesced requests. | |
148 | * | |
149 | * @param thread | |
150 | */ | |
151 | @Override | |
c1c69938 | 152 | public void fireRequest() { |
948b0607 FC |
153 | synchronized (fLock) { |
154 | if (fRequestPendingCounter > 0) { | |
155 | return; | |
156 | } | |
157 | if (fPendingCoalescedRequests.size() > 0) { | |
158 | for (TmfDataRequest<T> request : fPendingCoalescedRequests) { | |
159 | dispatchRequest(request); | |
160 | } | |
161 | fPendingCoalescedRequests.clear(); | |
162 | } | |
163 | } | |
164 | } | |
165 | ||
166 | /** | |
12c155f5 FC |
167 | * Increments/decrements the pending requests counters and fires the request if necessary (counter == 0). Used for |
168 | * coalescing requests accross multiple TmfDataProvider. | |
948b0607 FC |
169 | * |
170 | * @param isIncrement | |
171 | */ | |
c1c69938 FC |
172 | @Override |
173 | public void notifyPendingRequest(boolean isIncrement) { | |
948b0607 | 174 | synchronized (fLock) { |
c1c69938 FC |
175 | if (isIncrement) { |
176 | if (fSignalDepth > 0) { | |
177 | fRequestPendingCounter++; | |
178 | } | |
179 | } else { | |
180 | if (fRequestPendingCounter > 0) { | |
181 | fRequestPendingCounter--; | |
182 | } | |
948b0607 | 183 | |
c1c69938 FC |
184 | // fire request if all pending requests are received |
185 | if (fRequestPendingCounter == 0) { | |
186 | fireRequest(); | |
187 | } | |
188 | } | |
189 | } | |
948b0607 FC |
190 | } |
191 | ||
192 | // ------------------------------------------------------------------------ | |
193 | // Coalescing (primitive test...) | |
194 | // ------------------------------------------------------------------------ | |
195 | ||
196 | protected Vector<TmfCoalescedDataRequest<T>> fPendingCoalescedRequests = new Vector<TmfCoalescedDataRequest<T>>(); | |
197 | ||
198 | protected void newCoalescedDataRequest(ITmfDataRequest<T> request) { | |
199 | synchronized (fLock) { | |
1b70b6dc | 200 | TmfCoalescedDataRequest<T> coalescedRequest = new TmfCoalescedDataRequest<T>(request.getDataType(), request.getIndex(), |
12c155f5 | 201 | request.getNbRequested(), request.getBlockSize(), request.getExecType()); |
948b0607 FC |
202 | coalescedRequest.addRequest(request); |
203 | if (Tracer.isRequestTraced()) { | |
204 | Tracer.traceRequest(request, "coalesced with " + coalescedRequest.getRequestId()); //$NON-NLS-1$ | |
205 | } | |
206 | fPendingCoalescedRequests.add(coalescedRequest); | |
207 | } | |
208 | } | |
209 | ||
210 | protected void coalesceDataRequest(ITmfDataRequest<T> request) { | |
211 | synchronized (fLock) { | |
212 | for (TmfCoalescedDataRequest<T> coalescedRequest : fPendingCoalescedRequests) { | |
213 | if (coalescedRequest.isCompatible(request)) { | |
214 | coalescedRequest.addRequest(request); | |
215 | if (Tracer.isRequestTraced()) { | |
216 | Tracer.traceRequest(request, "coalesced with " + coalescedRequest.getRequestId()); //$NON-NLS-1$ | |
217 | } | |
218 | return; | |
219 | } | |
220 | } | |
221 | newCoalescedDataRequest(request); | |
222 | } | |
223 | } | |
224 | ||
225 | // ------------------------------------------------------------------------ | |
226 | // Request processing | |
227 | // ------------------------------------------------------------------------ | |
228 | ||
229 | private void dispatchRequest(final ITmfDataRequest<T> request) { | |
230 | if (request.getExecType() == ExecutionType.FOREGROUND) | |
231 | queueRequest(request); | |
232 | else | |
233 | queueBackgroundRequest(request, request.getBlockSize(), true); | |
234 | } | |
235 | ||
236 | protected void queueRequest(final ITmfDataRequest<T> request) { | |
237 | ||
238 | if (fExecutor.isShutdown()) { | |
239 | request.cancel(); | |
240 | return; | |
241 | } | |
242 | ||
243 | final TmfDataProvider<T> provider = this; | |
244 | ||
245 | // Process the request | |
246 | TmfThread thread = new TmfThread(request.getExecType()) { | |
475743b7 | 247 | |
948b0607 FC |
248 | @Override |
249 | public void run() { | |
250 | ||
251 | if (Tracer.isRequestTraced()) | |
252 | Tracer.trace("Request #" + request.getRequestId() + " is being serviced by " + provider.getName()); //$NON-NLS-1$//$NON-NLS-2$ | |
253 | ||
254 | // Extract the generic information | |
255 | request.start(); | |
256 | int nbRequested = request.getNbRequested(); | |
257 | int nbRead = 0; | |
258 | ||
259 | // Initialize the execution | |
260 | ITmfContext context = armRequest(request); | |
261 | if (context == null) { | |
262 | request.cancel(); | |
263 | return; | |
264 | } | |
265 | ||
266 | try { | |
267 | // Get the ordered events | |
268 | T data = getNext(context); | |
269 | if (Tracer.isRequestTraced()) | |
270 | Tracer.trace("Request #" + request.getRequestId() + " read first event"); //$NON-NLS-1$ //$NON-NLS-2$ | |
271 | while (data != null && !isCompleted(request, data, nbRead)) { | |
272 | if (fLogData) | |
273 | Tracer.traceEvent(provider, request, data); | |
1b70b6dc PT |
274 | if (request.getDataType().isInstance(data)) { |
275 | request.handleData(data); | |
276 | } | |
948b0607 FC |
277 | |
278 | // To avoid an unnecessary read passed the last data | |
279 | // requested | |
280 | if (++nbRead < nbRequested) { | |
281 | data = getNext(context); | |
282 | } | |
283 | } | |
284 | if (Tracer.isRequestTraced()) | |
285 | Tracer.trace("Request #" + request.getRequestId() + " finished"); //$NON-NLS-1$//$NON-NLS-2$ | |
286 | ||
287 | if (request.isCancelled()) { | |
288 | request.cancel(); | |
289 | } else { | |
290 | request.done(); | |
291 | } | |
292 | } catch (Exception e) { | |
293 | request.fail(); | |
294 | } | |
295 | ||
296 | // Cleanup | |
297 | context.dispose(); | |
298 | } | |
475743b7 FC |
299 | |
300 | @Override | |
301 | public void cancel() { | |
8a0edc79 FC |
302 | if (!request.isCompleted()) { |
303 | request.cancel(); | |
475743b7 FC |
304 | } |
305 | } | |
948b0607 FC |
306 | }; |
307 | ||
308 | if (Tracer.isRequestTraced()) | |
309 | Tracer.traceRequest(request, "queued"); //$NON-NLS-1$ | |
310 | fExecutor.execute(thread); | |
311 | ||
312 | } | |
313 | ||
314 | protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) { | |
315 | ||
316 | Thread thread = new Thread() { | |
317 | @Override | |
318 | public void run() { | |
319 | request.start(); | |
320 | ||
321 | final Integer[] CHUNK_SIZE = new Integer[1]; | |
322 | CHUNK_SIZE[0] = Math.min(request.getNbRequested(), blockSize + ((indexing) ? 1 : 0)); | |
323 | ||
324 | final Integer[] nbRead = new Integer[1]; | |
325 | nbRead[0] = 0; | |
326 | ||
327 | final Boolean[] isFinished = new Boolean[1]; | |
328 | isFinished[0] = Boolean.FALSE; | |
329 | ||
330 | while (!isFinished[0]) { | |
331 | ||
12c155f5 FC |
332 | TmfDataRequest<T> subRequest = new TmfDataRequest<T>(request.getDataType(), request.getIndex() |
333 | + nbRead[0], CHUNK_SIZE[0], blockSize, ExecutionType.BACKGROUND) { | |
948b0607 FC |
334 | @Override |
335 | public void handleData(T data) { | |
336 | super.handleData(data); | |
1b70b6dc PT |
337 | if (request.getDataType().isInstance(data)) { |
338 | request.handleData(data); | |
339 | } | |
948b0607 FC |
340 | if (getNbRead() > CHUNK_SIZE[0]) { |
341 | System.out.println("ERROR - Read too many events"); //$NON-NLS-1$ | |
342 | } | |
343 | } | |
344 | ||
345 | @Override | |
346 | public void handleCompleted() { | |
347 | nbRead[0] += getNbRead(); | |
348 | if (nbRead[0] >= request.getNbRequested() || (getNbRead() < CHUNK_SIZE[0])) { | |
90de83da | 349 | if (this.isCancelled()) { |
948b0607 | 350 | request.cancel(); |
90de83da | 351 | } else if (this.isFailed()) { |
12c155f5 | 352 | request.fail(); |
948b0607 FC |
353 | } else { |
354 | request.done(); | |
355 | } | |
356 | isFinished[0] = Boolean.TRUE; | |
357 | } | |
358 | super.handleCompleted(); | |
359 | } | |
360 | }; | |
361 | ||
362 | if (!isFinished[0]) { | |
363 | queueRequest(subRequest); | |
364 | ||
365 | try { | |
366 | subRequest.waitForCompletion(); | |
367 | } catch (InterruptedException e) { | |
368 | e.printStackTrace(); | |
369 | } | |
370 | ||
371 | CHUNK_SIZE[0] = Math.min(request.getNbRequested() - nbRead[0], blockSize); | |
372 | } | |
373 | } | |
374 | } | |
375 | }; | |
376 | ||
377 | thread.start(); | |
378 | } | |
379 | ||
380 | /** | |
12c155f5 | 381 | * Initialize the provider based on the request. The context is provider specific and will be updated by getNext(). |
948b0607 FC |
382 | * |
383 | * @param request | |
12c155f5 | 384 | * @return an application specific context; null if request can't be serviced |
948b0607 FC |
385 | */ |
386 | public abstract ITmfContext armRequest(ITmfDataRequest<T> request); | |
387 | ||
388 | public abstract T getNext(ITmfContext context); | |
389 | ||
390 | /** | |
391 | * Checks if the data meets the request completion criteria. | |
392 | * | |
393 | * @param request | |
394 | * @param data | |
395 | * @return | |
396 | */ | |
397 | public boolean isCompleted(ITmfDataRequest<T> request, T data, int nbRead) { | |
b6be1c3e | 398 | return request.isCompleted() || nbRead >= request.getNbRequested(); |
948b0607 FC |
399 | } |
400 | ||
401 | // ------------------------------------------------------------------------ | |
402 | // Signal handlers | |
403 | // ------------------------------------------------------------------------ | |
404 | ||
405 | @TmfSignalHandler | |
406 | public void startSynch(TmfStartSynchSignal signal) { | |
407 | synchronized (fLock) { | |
408 | fSignalDepth++; | |
409 | } | |
410 | } | |
411 | ||
412 | @TmfSignalHandler | |
413 | public void endSynch(TmfEndSynchSignal signal) { | |
045df77d | 414 | synchronized (fLock) { |
948b0607 FC |
415 | fSignalDepth--; |
416 | if (fSignalDepth == 0) { | |
417 | fireRequest(); | |
418 | } | |
045df77d | 419 | } |
948b0607 | 420 | } |
8c8bf09f ASL |
421 | |
422 | } |