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