Commit | Line | Data |
---|---|---|
8c8bf09f | 1 | /******************************************************************************* |
e31e01e8 | 2 | * Copyright (c) 2009, 2010 Ericsson |
8c8bf09f ASL |
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 | ||
13 | package org.eclipse.linuxtools.tmf.trace; | |
14 | ||
62d1696a | 15 | import java.io.FileNotFoundException; |
62d1696a | 16 | import java.util.Collections; |
8c8bf09f ASL |
17 | import java.util.Vector; |
18 | ||
05bd3318 FC |
19 | import org.eclipse.core.runtime.IProgressMonitor; |
20 | import org.eclipse.core.runtime.IStatus; | |
21 | import org.eclipse.core.runtime.Status; | |
22 | import org.eclipse.core.runtime.jobs.Job; | |
fc6ccf6f | 23 | import org.eclipse.linuxtools.tmf.component.TmfEventProvider; |
8c8bf09f ASL |
24 | import org.eclipse.linuxtools.tmf.event.TmfEvent; |
25 | import org.eclipse.linuxtools.tmf.event.TmfTimeRange; | |
26 | import org.eclipse.linuxtools.tmf.event.TmfTimestamp; | |
2fb2eb37 | 27 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest; |
64267c9d | 28 | import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType; |
2fb2eb37 | 29 | import org.eclipse.linuxtools.tmf.request.ITmfEventRequest; |
83e13355 FC |
30 | import org.eclipse.linuxtools.tmf.request.TmfDataRequest; |
31 | import org.eclipse.linuxtools.tmf.request.TmfEventRequest; | |
83e13355 | 32 | import org.eclipse.linuxtools.tmf.signal.TmfTraceUpdatedSignal; |
8c8bf09f ASL |
33 | |
34 | /** | |
146a887c | 35 | * <b><u>TmfTrace</u></b> |
8c8bf09f | 36 | * <p> |
146a887c FC |
37 | * Abstract implementation of ITmfTrace. It should be sufficient to extend this |
38 | * class and provide implementation for <code>getCurrentLocation()</code> and | |
39 | * <code>seekLocation()</code>, as well as a proper parser, to have a working | |
4e3aa37d | 40 | * concrete implementation. |
ff4ed569 | 41 | * <p> |
54d55ced | 42 | * Note: The notion of event rank is still under heavy discussion. Although |
ff4ed569 | 43 | * used by the Events View and probably useful in the general case, there |
54d55ced | 44 | * is no easy way to implement it for LTTng (actually a strong case is being |
ff4ed569 FC |
45 | * made that this is useless). |
46 | * <p> | |
47 | * That it is not supported by LTTng does by no mean indicate that it is not | |
48 | * useful for (just about) every other tracing tool. Therefore, this class | |
49 | * provides a minimal (and partial) implementation of rank. However, the current | |
50 | * implementation should not be relied on in the general case. | |
54d55ced | 51 | * |
4e3aa37d | 52 | * TODO: Add support for live streaming (notifications, incremental indexing, ...) |
8c8bf09f | 53 | */ |
ff4ed569 | 54 | public abstract class TmfTrace<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace, Cloneable { |
62d1696a | 55 | |
e31e01e8 | 56 | // ------------------------------------------------------------------------ |
62d1696a | 57 | // Constants |
e31e01e8 | 58 | // ------------------------------------------------------------------------ |
62d1696a FC |
59 | |
60 | // The default number of events to cache | |
e31e01e8 | 61 | // TODO: Make the DEFAULT_CACHE_SIZE a preference |
b12f4544 | 62 | public static final int DEFAULT_INDEX_PAGE_SIZE = 50000; |
8c8bf09f | 63 | |
e31e01e8 | 64 | // ------------------------------------------------------------------------ |
8c8bf09f | 65 | // Attributes |
e31e01e8 | 66 | // ------------------------------------------------------------------------ |
8c8bf09f | 67 | |
b0a282fb FC |
68 | // The trace path |
69 | private final String fPath; | |
70 | ||
8d2e2848 | 71 | // The cache page size AND checkpoints interval |
9f584e4c | 72 | protected int fIndexPageSize; |
62d1696a FC |
73 | |
74 | // The set of event stream checkpoints (for random access) | |
9f584e4c | 75 | protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>(); |
62d1696a FC |
76 | |
77 | // The number of events collected | |
a3fe52fc | 78 | protected long fNbEvents = 0; |
62d1696a FC |
79 | |
80 | // The time span of the event stream | |
cb866e08 FC |
81 | private TmfTimestamp fStartTime = TmfTimestamp.BigCrunch; |
82 | private TmfTimestamp fEndTime = TmfTimestamp.BigBang; | |
62d1696a | 83 | |
e31e01e8 | 84 | // ------------------------------------------------------------------------ |
50adc88e | 85 | // Constructors |
e31e01e8 | 86 | // ------------------------------------------------------------------------ |
8c8bf09f | 87 | |
ff4ed569 FC |
88 | /** |
89 | * @param path | |
90 | * @throws FileNotFoundException | |
91 | */ | |
ce785d7d | 92 | protected TmfTrace(String name, Class<T> type, String path) throws FileNotFoundException { |
05bd3318 | 93 | this(name, type, path, DEFAULT_INDEX_PAGE_SIZE, true); |
ff4ed569 FC |
94 | } |
95 | ||
62d1696a | 96 | /** |
e31e01e8 FC |
97 | * @param path |
98 | * @param cacheSize | |
62d1696a FC |
99 | * @throws FileNotFoundException |
100 | */ | |
ce785d7d | 101 | protected TmfTrace(String name, Class<T> type, String path, int cacheSize) throws FileNotFoundException { |
05bd3318 FC |
102 | this(name, type, path, cacheSize, true); |
103 | } | |
104 | ||
105 | /** | |
106 | * @param path | |
107 | * @param indexTrace | |
108 | * @throws FileNotFoundException | |
109 | */ | |
110 | protected TmfTrace(String name, Class<T> type, String path, boolean indexTrace) throws FileNotFoundException { | |
111 | this(name, type, path, DEFAULT_INDEX_PAGE_SIZE, indexTrace); | |
112 | } | |
113 | ||
114 | /** | |
115 | * @param path | |
116 | * @param cacheSize | |
117 | * @param indexTrace | |
118 | * @throws FileNotFoundException | |
119 | */ | |
120 | protected TmfTrace(String name, Class<T> type, String path, int cacheSize, boolean indexTrace) throws FileNotFoundException { | |
ce785d7d | 121 | super(name, type); |
8e31f2d2 FC |
122 | // if (path != null) { |
123 | // int sep = path.lastIndexOf(File.separator); | |
124 | // String simpleName = (sep >= 0) ? path.substring(sep + 1) : path; | |
125 | // setName(simpleName); | |
126 | // } | |
b0a282fb | 127 | fPath = path; |
664902f7 | 128 | fIndexPageSize = (cacheSize > 0) ? cacheSize : DEFAULT_INDEX_PAGE_SIZE; |
163d6b7e FC |
129 | if (indexTrace) { |
130 | indexTrace(false); | |
131 | } | |
8c8bf09f ASL |
132 | } |
133 | ||
ff4ed569 FC |
134 | /* (non-Javadoc) |
135 | * @see java.lang.Object#clone() | |
62d1696a | 136 | */ |
ff4ed569 FC |
137 | @SuppressWarnings("unchecked") |
138 | @Override | |
139 | public TmfTrace<T> clone() throws CloneNotSupportedException { | |
140 | TmfTrace<T> clone = (TmfTrace<T>) super.clone(); | |
cb866e08 FC |
141 | clone.fCheckpoints = (Vector<TmfCheckpoint>) fCheckpoints; |
142 | clone.fStartTime = new TmfTimestamp(fStartTime); | |
143 | clone.fEndTime = new TmfTimestamp(fEndTime); | |
ff4ed569 | 144 | return clone; |
8c8bf09f ASL |
145 | } |
146 | ||
e31e01e8 | 147 | // ------------------------------------------------------------------------ |
8c8bf09f | 148 | // Accessors |
e31e01e8 | 149 | // ------------------------------------------------------------------------ |
8c8bf09f | 150 | |
62d1696a | 151 | /** |
b0a282fb | 152 | * @return the trace path |
62d1696a | 153 | */ |
d4011df2 FC |
154 | @Override |
155 | public String getPath() { | |
b0a282fb | 156 | return fPath; |
8c8bf09f ASL |
157 | } |
158 | ||
62d1696a FC |
159 | /* (non-Javadoc) |
160 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getNbEvents() | |
161 | */ | |
d4011df2 FC |
162 | @Override |
163 | public long getNbEvents() { | |
62d1696a | 164 | return fNbEvents; |
8c8bf09f ASL |
165 | } |
166 | ||
b0a282fb FC |
167 | /** |
168 | * @return the size of the cache | |
169 | */ | |
d4011df2 FC |
170 | @Override |
171 | public int getCacheSize() { | |
9f584e4c | 172 | return fIndexPageSize; |
b0a282fb FC |
173 | } |
174 | ||
62d1696a FC |
175 | /* (non-Javadoc) |
176 | * @see org.eclipse.linuxtools.tmf.stream.ITmfEventStream#getTimeRange() | |
177 | */ | |
d4011df2 FC |
178 | @Override |
179 | public TmfTimeRange getTimeRange() { | |
cb866e08 | 180 | return new TmfTimeRange(fStartTime, fEndTime); |
8c8bf09f ASL |
181 | } |
182 | ||
e31e01e8 FC |
183 | /* (non-Javadoc) |
184 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStartTime() | |
185 | */ | |
d4011df2 FC |
186 | @Override |
187 | public TmfTimestamp getStartTime() { | |
cb866e08 | 188 | return fStartTime; |
146a887c FC |
189 | } |
190 | ||
e31e01e8 FC |
191 | /* (non-Javadoc) |
192 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getEndTime() | |
193 | */ | |
d4011df2 FC |
194 | @Override |
195 | public TmfTimestamp getEndTime() { | |
cb866e08 | 196 | return fEndTime; |
146a887c FC |
197 | } |
198 | ||
ff4ed569 FC |
199 | @SuppressWarnings("unchecked") |
200 | public Vector<TmfCheckpoint> getCheckpoints() { | |
201 | return (Vector<TmfCheckpoint>) fCheckpoints.clone(); | |
54d55ced FC |
202 | } |
203 | ||
abfad0aa FC |
204 | /** |
205 | * Returns the rank of the first event with the requested timestamp. | |
206 | * If none, returns the index of the next event (if any). | |
207 | * | |
208 | * @param timestamp | |
209 | * @return | |
210 | */ | |
d4011df2 FC |
211 | @Override |
212 | public long getRank(TmfTimestamp timestamp) { | |
abfad0aa FC |
213 | TmfContext context = seekEvent(timestamp); |
214 | return context.getRank(); | |
215 | } | |
216 | ||
e31e01e8 | 217 | // ------------------------------------------------------------------------ |
8c8bf09f | 218 | // Operators |
e31e01e8 | 219 | // ------------------------------------------------------------------------ |
8c8bf09f | 220 | |
4e3aa37d | 221 | protected void setTimeRange(TmfTimeRange range) { |
cb866e08 FC |
222 | fStartTime = range.getStartTime(); |
223 | fEndTime = range.getEndTime(); | |
4e3aa37d FC |
224 | } |
225 | ||
226 | protected void setStartTime(TmfTimestamp startTime) { | |
cb866e08 | 227 | fStartTime = startTime; |
4e3aa37d FC |
228 | } |
229 | ||
230 | protected void setEndTime(TmfTimestamp endTime) { | |
cb866e08 | 231 | fEndTime = endTime; |
4e3aa37d FC |
232 | } |
233 | ||
e31e01e8 FC |
234 | // ------------------------------------------------------------------------ |
235 | // TmfProvider | |
236 | // ------------------------------------------------------------------------ | |
237 | ||
238 | @Override | |
2fb2eb37 FC |
239 | public ITmfContext armRequest(ITmfDataRequest<T> request) { |
240 | if (request instanceof ITmfEventRequest<?>) { | |
241 | return seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime()); | |
e31e01e8 | 242 | } |
ff4ed569 | 243 | return seekEvent(request.getIndex()); |
e31e01e8 FC |
244 | } |
245 | ||
246 | /** | |
247 | * Return the next piece of data based on the context supplied. The context | |
248 | * would typically be updated for the subsequent read. | |
249 | * | |
250 | * @param context | |
251 | * @return | |
252 | */ | |
253 | @SuppressWarnings("unchecked") | |
254 | @Override | |
255 | public T getNext(ITmfContext context) { | |
9f584e4c FC |
256 | if (context instanceof TmfContext) { |
257 | return (T) getNextEvent((TmfContext) context); | |
e31e01e8 FC |
258 | } |
259 | return null; | |
260 | } | |
261 | ||
e31e01e8 FC |
262 | // ------------------------------------------------------------------------ |
263 | // ITmfTrace | |
264 | // ------------------------------------------------------------------------ | |
265 | ||
146a887c FC |
266 | /* (non-Javadoc) |
267 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp) | |
268 | */ | |
d4011df2 FC |
269 | @Override |
270 | public TmfContext seekEvent(TmfTimestamp timestamp) { | |
62d1696a | 271 | |
4e3aa37d FC |
272 | if (timestamp == null) { |
273 | timestamp = TmfTimestamp.BigBang; | |
274 | } | |
275 | ||
276 | // First, find the right checkpoint | |
9f584e4c | 277 | int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null)); |
62d1696a | 278 | |
8d2e2848 | 279 | // In the very likely case that the checkpoint was not found, bsearch |
62d1696a FC |
280 | // returns its negated would-be location (not an offset...). From that |
281 | // index, we can then position the stream and get the event. | |
282 | if (index < 0) { | |
283 | index = Math.max(0, -(index + 2)); | |
284 | } | |
285 | ||
286 | // Position the stream at the checkpoint | |
452ad365 | 287 | ITmfLocation<?> location; |
e31e01e8 FC |
288 | synchronized (fCheckpoints) { |
289 | if (fCheckpoints.size() > 0) { | |
290 | if (index >= fCheckpoints.size()) { | |
291 | index = fCheckpoints.size() - 1; | |
292 | } | |
293 | location = fCheckpoints.elementAt(index).getLocation(); | |
294 | } | |
295 | else { | |
296 | location = null; | |
297 | } | |
8d2e2848 | 298 | } |
54d55ced FC |
299 | TmfContext context = seekLocation(location); |
300 | context.setRank(index * fIndexPageSize); | |
62d1696a | 301 | |
54d55ced | 302 | // And locate the event |
ff4ed569 | 303 | TmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype... |
62d1696a FC |
304 | TmfEvent event = getNextEvent(nextEventContext); |
305 | while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) { | |
54d55ced FC |
306 | context.setLocation(nextEventContext.getLocation().clone()); |
307 | context.updateRank(1); | |
62d1696a FC |
308 | event = getNextEvent(nextEventContext); |
309 | } | |
310 | ||
54d55ced | 311 | return context; |
62d1696a FC |
312 | } |
313 | ||
146a887c FC |
314 | /* (non-Javadoc) |
315 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(int) | |
316 | */ | |
d4011df2 FC |
317 | @Override |
318 | public TmfContext seekEvent(long rank) { | |
62d1696a FC |
319 | |
320 | // Position the stream at the previous checkpoint | |
9f584e4c | 321 | int index = (int) rank / fIndexPageSize; |
452ad365 | 322 | ITmfLocation<?> location; |
e31e01e8 | 323 | synchronized (fCheckpoints) { |
54d55ced FC |
324 | if (fCheckpoints.size() == 0) { |
325 | location = null; | |
326 | } | |
327 | else { | |
e31e01e8 | 328 | if (index >= fCheckpoints.size()) { |
54d55ced | 329 | index = fCheckpoints.size() - 1; |
e31e01e8 FC |
330 | } |
331 | location = fCheckpoints.elementAt(index).getLocation(); | |
332 | } | |
8d2e2848 | 333 | } |
54d55ced | 334 | |
9f584e4c FC |
335 | TmfContext context = seekLocation(location); |
336 | long pos = index * fIndexPageSize; | |
337 | context.setRank(pos); | |
e31e01e8 | 338 | |
9f584e4c | 339 | if (pos < rank) { |
e31e01e8 | 340 | TmfEvent event = getNextEvent(context); |
9f584e4c | 341 | while (event != null && ++pos < rank) { |
e31e01e8 FC |
342 | event = getNextEvent(context); |
343 | } | |
165c977c | 344 | } |
62d1696a | 345 | |
8f50c396 | 346 | return context; |
8c8bf09f ASL |
347 | } |
348 | ||
146a887c FC |
349 | /* (non-Javadoc) |
350 | * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getNextEvent(org.eclipse.linuxtools.tmf.trace.ITmfTrace.TraceContext) | |
351 | */ | |
d4011df2 | 352 | @Override |
9f584e4c | 353 | public synchronized TmfEvent getNextEvent(TmfContext context) { |
e31e01e8 | 354 | // parseEvent() does not update the context |
cc6eec3e | 355 | TmfEvent event = parseEvent(context); |
4e3aa37d | 356 | if (event != null) { |
550d787e | 357 | updateIndex(context, context.getRank(), event.getTimestamp()); |
cb866e08 | 358 | context.setLocation(getCurrentLocation()); |
54d55ced | 359 | context.updateRank(1); |
4e3aa37d FC |
360 | processEvent(event); |
361 | } | |
146a887c FC |
362 | return event; |
363 | } | |
8c8bf09f | 364 | |
cb866e08 FC |
365 | protected synchronized void updateIndex(ITmfContext context, long rank, TmfTimestamp timestamp) { |
366 | if (fStartTime.compareTo(timestamp, false) > 0) fStartTime = timestamp; | |
367 | if (fEndTime.compareTo(timestamp, false) < 0) fEndTime = timestamp; | |
368 | if (context.isValidRank()) { | |
369 | if (fNbEvents <= rank) | |
370 | fNbEvents = rank + 1; | |
371 | // Build the index as we go along | |
372 | if ((rank % fIndexPageSize) == 0) { | |
373 | // Determine the table position | |
374 | long position = rank / fIndexPageSize; | |
375 | // Add new entry at proper location (if empty) | |
376 | if (fCheckpoints.size() == position) { | |
377 | ITmfLocation<?> location = context.getLocation().clone(); | |
1a971e96 | 378 | fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location)); |
cb866e08 FC |
379 | // System.out.println(getName() + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString()); |
380 | } | |
550d787e FC |
381 | } |
382 | } | |
383 | } | |
384 | ||
4e3aa37d | 385 | /** |
e31e01e8 FC |
386 | * Hook for "special" processing by the concrete class |
387 | * (called by getNextEvent()) | |
388 | * | |
146a887c FC |
389 | * @param event |
390 | */ | |
ff4ed569 | 391 | protected void processEvent(TmfEvent event) { |
146a887c | 392 | // Do nothing by default |
62d1696a | 393 | } |
4e3aa37d | 394 | |
e31e01e8 FC |
395 | /** |
396 | * To be implemented by the concrete class | |
4e3aa37d | 397 | */ |
d4011df2 FC |
398 | @Override |
399 | public abstract TmfContext seekLocation(ITmfLocation<?> location); | |
c76c54bb | 400 | @Override |
452ad365 | 401 | public abstract ITmfLocation<?> getCurrentLocation(); |
d4011df2 FC |
402 | @Override |
403 | public abstract TmfEvent parseEvent(TmfContext context); | |
c76c54bb FC |
404 | @Override |
405 | public abstract TmfContext seekLocation(double ratio); | |
406 | @Override | |
407 | public abstract double getLocationRatio(ITmfLocation<?> location); | |
4e3aa37d | 408 | |
e31e01e8 FC |
409 | // ------------------------------------------------------------------------ |
410 | // toString | |
411 | // ------------------------------------------------------------------------ | |
8d2e2848 FC |
412 | |
413 | /* (non-Javadoc) | |
414 | * @see java.lang.Object#toString() | |
415 | */ | |
416 | @Override | |
3b38ea61 | 417 | @SuppressWarnings("nls") |
8d2e2848 | 418 | public String toString() { |
ce785d7d | 419 | return "[TmfTrace (" + getName() + ")]"; |
8d2e2848 | 420 | } |
146a887c | 421 | |
664902f7 FC |
422 | // ------------------------------------------------------------------------ |
423 | // Indexing | |
424 | // ------------------------------------------------------------------------ | |
425 | ||
83e13355 FC |
426 | /* |
427 | * The purpose of the index is to keep the information needed to rapidly | |
428 | * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE | |
429 | * event). | |
430 | */ | |
431 | ||
8e31f2d2 | 432 | @SuppressWarnings({ "unchecked" }) |
83e13355 FC |
433 | private void indexTrace(boolean waitForCompletion) { |
434 | ||
05bd3318 FC |
435 | final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$ |
436 | @Override | |
437 | protected IStatus run(IProgressMonitor monitor) { | |
438 | while (!monitor.isCanceled()) { | |
439 | try { | |
440 | Thread.sleep(100); | |
441 | } catch (InterruptedException e) { | |
442 | return Status.OK_STATUS; | |
443 | } | |
444 | } | |
445 | monitor.done(); | |
446 | return Status.OK_STATUS; | |
447 | } | |
448 | }; | |
449 | job.schedule(); | |
450 | ||
451 | fCheckpoints.clear(); | |
83e13355 FC |
452 | ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity, |
453 | TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.BACKGROUND) { | |
454 | ||
455 | TmfTimestamp startTime = null; | |
456 | TmfTimestamp lastTime = null; | |
457 | ||
458 | @Override | |
459 | public void handleData(TmfEvent event) { | |
460 | super.handleData(event); | |
461 | if (event != null) { | |
462 | TmfTimestamp ts = event.getTimestamp(); | |
463 | if (startTime == null) | |
464 | startTime = new TmfTimestamp(ts); | |
465 | lastTime = new TmfTimestamp(ts); | |
466 | ||
64267c9d | 467 | if ((getNbRead() % fIndexPageSize) == 0) { |
83e13355 FC |
468 | updateTrace(); |
469 | } | |
470 | } | |
471 | } | |
472 | ||
473 | @Override | |
474 | public void handleSuccess() { | |
475 | updateTrace(); | |
476 | } | |
477 | ||
05bd3318 FC |
478 | @Override |
479 | public void handleCompleted() { | |
480 | job.cancel(); | |
481 | super.handleCompleted(); | |
482 | } | |
483 | ||
83e13355 FC |
484 | private void updateTrace() { |
485 | int nbRead = getNbRead(); | |
486 | if (nbRead != 0) { | |
487 | fStartTime = startTime; | |
488 | fEndTime = lastTime; | |
489 | fNbEvents = nbRead; | |
490 | notifyListeners(); | |
491 | } | |
492 | } | |
493 | }; | |
494 | ||
495 | sendRequest((ITmfDataRequest<T>) request); | |
496 | if (waitForCompletion) | |
497 | try { | |
498 | request.waitForCompletion(); | |
499 | } catch (InterruptedException e) { | |
500 | e.printStackTrace(); | |
501 | } | |
502 | } | |
503 | ||
83e13355 FC |
504 | protected void notifyListeners() { |
505 | broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime))); | |
506 | } | |
507 | ||
abfad0aa FC |
508 | // ------------------------------------------------------------------------ |
509 | // TmfDataProvider | |
510 | // ------------------------------------------------------------------------ | |
511 | ||
83e13355 | 512 | @Override |
64267c9d FC |
513 | protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) { |
514 | ||
515 | // TODO: Handle the data requests also... | |
516 | if (!(request instanceof ITmfEventRequest<?>)) { | |
517 | super.queueRequest(request); | |
518 | return; | |
519 | } | |
520 | final ITmfEventRequest<T> eventRequest = (ITmfEventRequest<T>) request; | |
521 | ||
522 | Thread thread = new Thread() { | |
523 | @Override | |
524 | public void run() { | |
525 | ||
526 | // final long requestStart = System.nanoTime(); | |
527 | ||
528 | final Integer[] CHUNK_SIZE = new Integer[1]; | |
529 | CHUNK_SIZE[0] = blockSize + ((indexing) ? 1 : 0); | |
530 | ||
531 | final Integer[] nbRead = new Integer[1]; | |
532 | nbRead[0] = 0; | |
533 | ||
534 | // final TmfTimestamp[] timestamp = new TmfTimestamp[1]; | |
535 | // timestamp[0] = new TmfTimestamp(eventRequest.getRange().getStartTime()); | |
536 | // final TmfTimestamp endTS = eventRequest.getRange().getEndTime(); | |
537 | ||
538 | final Boolean[] isFinished = new Boolean[1]; | |
539 | isFinished[0] = Boolean.FALSE; | |
540 | ||
541 | while (!isFinished[0]) { | |
542 | ||
543 | // TmfEventRequest<T> subRequest = new TmfEventRequest<T>(eventRequest.getDataType(), new TmfTimeRange(timestamp[0], endTS), CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND) | |
544 | // TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND) | |
545 | TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], ExecutionType.BACKGROUND) | |
546 | { | |
547 | @Override | |
548 | public void handleData(T data) { | |
549 | super.handleData(data); | |
550 | eventRequest.handleData(data); | |
551 | if (getNbRead() == CHUNK_SIZE[0]) { | |
552 | nbRead[0] += getNbRead(); | |
553 | } | |
554 | if (getNbRead() > CHUNK_SIZE[0]) { | |
555 | System.out.println("ERROR - Read too many events"); //$NON-NLS-1$ | |
556 | } | |
557 | } | |
558 | ||
559 | @Override | |
560 | public void handleCompleted() { | |
561 | // System.out.println("Request completed at: " + timestamp[0]); | |
562 | if (getNbRead() < CHUNK_SIZE[0]) { | |
563 | if (isCancelled()) { | |
564 | eventRequest.cancel(); | |
565 | } | |
566 | else { | |
567 | eventRequest.done(); | |
568 | } | |
569 | isFinished[0] = Boolean.TRUE; | |
570 | nbRead[0] += getNbRead(); | |
571 | // System.out.println("fNbRead=" + getNbRead() + " total=" + nbRead[0]); | |
572 | } | |
573 | super.handleCompleted(); | |
574 | } | |
575 | }; | |
576 | ||
577 | if (!isFinished[0]) { | |
578 | queueRequest(subRequest); | |
579 | ||
580 | try { | |
581 | subRequest.waitForCompletion(); | |
582 | // System.out.println("Finished at " + timestamp[0]); | |
583 | } catch (InterruptedException e) { | |
584 | e.printStackTrace(); | |
585 | } | |
586 | ||
587 | // TmfTimestamp newTS = new TmfTimestamp(timestamp[0].getValue() + 1, timestamp[0].getScale(), timestamp[0].getPrecision()); | |
588 | // timestamp[0] = newTS; | |
589 | CHUNK_SIZE[0] = blockSize; | |
590 | // System.out.println("New timestamp: " + timestamp[0]); | |
591 | } | |
592 | } | |
593 | // final long requestEnded = System.nanoTime(); | |
594 | // System.out.println("Background request completed. Elapsed= " + (requestEnded * 1.0 - requestStart) / 1000000000); | |
595 | } | |
596 | }; | |
597 | ||
598 | thread.start(); | |
599 | } | |
600 | ||
601 | // @Override | |
602 | // protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean adjust) { | |
603 | // super.queueBackgroundRequest(request, fIndexPageSize, true); | |
604 | // } | |
f6b14ce2 | 605 | |
8c8bf09f | 606 | } |