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; |
05bd3318 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); | |
452ad365 | 399 | public abstract ITmfLocation<?> getCurrentLocation(); |
d4011df2 FC |
400 | @Override |
401 | public abstract TmfEvent parseEvent(TmfContext context); | |
4e3aa37d | 402 | |
e31e01e8 FC |
403 | // ------------------------------------------------------------------------ |
404 | // toString | |
405 | // ------------------------------------------------------------------------ | |
8d2e2848 FC |
406 | |
407 | /* (non-Javadoc) | |
408 | * @see java.lang.Object#toString() | |
409 | */ | |
410 | @Override | |
3b38ea61 | 411 | @SuppressWarnings("nls") |
8d2e2848 | 412 | public String toString() { |
ce785d7d | 413 | return "[TmfTrace (" + getName() + ")]"; |
8d2e2848 | 414 | } |
146a887c | 415 | |
664902f7 FC |
416 | // ------------------------------------------------------------------------ |
417 | // Indexing | |
418 | // ------------------------------------------------------------------------ | |
419 | ||
83e13355 FC |
420 | /* |
421 | * The purpose of the index is to keep the information needed to rapidly | |
422 | * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE | |
423 | * event). | |
424 | */ | |
425 | ||
426 | @SuppressWarnings({ "unchecked" }) | |
427 | private void indexTrace(boolean waitForCompletion) { | |
428 | ||
05bd3318 FC |
429 | final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$ |
430 | @Override | |
431 | protected IStatus run(IProgressMonitor monitor) { | |
432 | while (!monitor.isCanceled()) { | |
433 | try { | |
434 | Thread.sleep(100); | |
435 | } catch (InterruptedException e) { | |
436 | return Status.OK_STATUS; | |
437 | } | |
438 | } | |
439 | monitor.done(); | |
440 | return Status.OK_STATUS; | |
441 | } | |
442 | }; | |
443 | job.schedule(); | |
444 | ||
445 | fCheckpoints.clear(); | |
83e13355 FC |
446 | ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity, |
447 | TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.BACKGROUND) { | |
448 | ||
449 | TmfTimestamp startTime = null; | |
450 | TmfTimestamp lastTime = null; | |
451 | ||
452 | @Override | |
453 | public void handleData(TmfEvent event) { | |
454 | super.handleData(event); | |
455 | if (event != null) { | |
456 | TmfTimestamp ts = event.getTimestamp(); | |
457 | if (startTime == null) | |
458 | startTime = new TmfTimestamp(ts); | |
459 | lastTime = new TmfTimestamp(ts); | |
460 | ||
64267c9d | 461 | if ((getNbRead() % fIndexPageSize) == 0) { |
83e13355 FC |
462 | updateTrace(); |
463 | } | |
464 | } | |
465 | } | |
466 | ||
467 | @Override | |
468 | public void handleSuccess() { | |
469 | updateTrace(); | |
470 | } | |
471 | ||
05bd3318 FC |
472 | @Override |
473 | public void handleCompleted() { | |
474 | job.cancel(); | |
475 | super.handleCompleted(); | |
476 | } | |
477 | ||
83e13355 FC |
478 | private void updateTrace() { |
479 | int nbRead = getNbRead(); | |
480 | if (nbRead != 0) { | |
481 | fStartTime = startTime; | |
482 | fEndTime = lastTime; | |
483 | fNbEvents = nbRead; | |
484 | notifyListeners(); | |
485 | } | |
486 | } | |
487 | }; | |
488 | ||
489 | sendRequest((ITmfDataRequest<T>) request); | |
490 | if (waitForCompletion) | |
491 | try { | |
492 | request.waitForCompletion(); | |
493 | } catch (InterruptedException e) { | |
494 | e.printStackTrace(); | |
495 | } | |
496 | } | |
497 | ||
83e13355 FC |
498 | protected void notifyListeners() { |
499 | broadcast(new TmfTraceUpdatedSignal(this, this, new TmfTimeRange(fStartTime, fEndTime))); | |
500 | } | |
501 | ||
abfad0aa FC |
502 | // ------------------------------------------------------------------------ |
503 | // TmfDataProvider | |
504 | // ------------------------------------------------------------------------ | |
505 | ||
83e13355 | 506 | @Override |
64267c9d FC |
507 | protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) { |
508 | ||
509 | // TODO: Handle the data requests also... | |
510 | if (!(request instanceof ITmfEventRequest<?>)) { | |
511 | super.queueRequest(request); | |
512 | return; | |
513 | } | |
514 | final ITmfEventRequest<T> eventRequest = (ITmfEventRequest<T>) request; | |
515 | ||
516 | Thread thread = new Thread() { | |
517 | @Override | |
518 | public void run() { | |
519 | ||
520 | // final long requestStart = System.nanoTime(); | |
521 | ||
522 | final Integer[] CHUNK_SIZE = new Integer[1]; | |
523 | CHUNK_SIZE[0] = blockSize + ((indexing) ? 1 : 0); | |
524 | ||
525 | final Integer[] nbRead = new Integer[1]; | |
526 | nbRead[0] = 0; | |
527 | ||
528 | // final TmfTimestamp[] timestamp = new TmfTimestamp[1]; | |
529 | // timestamp[0] = new TmfTimestamp(eventRequest.getRange().getStartTime()); | |
530 | // final TmfTimestamp endTS = eventRequest.getRange().getEndTime(); | |
531 | ||
532 | final Boolean[] isFinished = new Boolean[1]; | |
533 | isFinished[0] = Boolean.FALSE; | |
534 | ||
535 | while (!isFinished[0]) { | |
536 | ||
537 | // TmfEventRequest<T> subRequest = new TmfEventRequest<T>(eventRequest.getDataType(), new TmfTimeRange(timestamp[0], endTS), CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND) | |
538 | // TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND) | |
539 | TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], ExecutionType.BACKGROUND) | |
540 | { | |
541 | @Override | |
542 | public void handleData(T data) { | |
543 | super.handleData(data); | |
544 | eventRequest.handleData(data); | |
545 | if (getNbRead() == CHUNK_SIZE[0]) { | |
546 | nbRead[0] += getNbRead(); | |
547 | } | |
548 | if (getNbRead() > CHUNK_SIZE[0]) { | |
549 | System.out.println("ERROR - Read too many events"); //$NON-NLS-1$ | |
550 | } | |
551 | } | |
552 | ||
553 | @Override | |
554 | public void handleCompleted() { | |
555 | // System.out.println("Request completed at: " + timestamp[0]); | |
556 | if (getNbRead() < CHUNK_SIZE[0]) { | |
557 | if (isCancelled()) { | |
558 | eventRequest.cancel(); | |
559 | } | |
560 | else { | |
561 | eventRequest.done(); | |
562 | } | |
563 | isFinished[0] = Boolean.TRUE; | |
564 | nbRead[0] += getNbRead(); | |
565 | // System.out.println("fNbRead=" + getNbRead() + " total=" + nbRead[0]); | |
566 | } | |
567 | super.handleCompleted(); | |
568 | } | |
569 | }; | |
570 | ||
571 | if (!isFinished[0]) { | |
572 | queueRequest(subRequest); | |
573 | ||
574 | try { | |
575 | subRequest.waitForCompletion(); | |
576 | // System.out.println("Finished at " + timestamp[0]); | |
577 | } catch (InterruptedException e) { | |
578 | e.printStackTrace(); | |
579 | } | |
580 | ||
581 | // TmfTimestamp newTS = new TmfTimestamp(timestamp[0].getValue() + 1, timestamp[0].getScale(), timestamp[0].getPrecision()); | |
582 | // timestamp[0] = newTS; | |
583 | CHUNK_SIZE[0] = blockSize; | |
584 | // System.out.println("New timestamp: " + timestamp[0]); | |
585 | } | |
586 | } | |
587 | // final long requestEnded = System.nanoTime(); | |
588 | // System.out.println("Background request completed. Elapsed= " + (requestEnded * 1.0 - requestStart) / 1000000000); | |
589 | } | |
590 | }; | |
591 | ||
592 | thread.start(); | |
593 | } | |
594 | ||
595 | // @Override | |
596 | // protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean adjust) { | |
597 | // super.queueBackgroundRequest(request, fIndexPageSize, true); | |
598 | // } | |
f6b14ce2 | 599 | |
8c8bf09f | 600 | } |