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