2010-10-15 Francois Chouinard <fchouinard@gmail.com> Fix for Bug327910
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf / src / org / eclipse / linuxtools / tmf / experiment / TmfExperiment.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.experiment;
14
15 import java.util.Collections;
16 import java.util.Vector;
17
18 import org.eclipse.linuxtools.tmf.component.TmfEventProvider;
19 import org.eclipse.linuxtools.tmf.event.TmfEvent;
20 import org.eclipse.linuxtools.tmf.event.TmfTimeRange;
21 import org.eclipse.linuxtools.tmf.event.TmfTimestamp;
22 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest;
23 import org.eclipse.linuxtools.tmf.request.ITmfDataRequest.ExecutionType;
24 import org.eclipse.linuxtools.tmf.request.ITmfEventRequest;
25 import org.eclipse.linuxtools.tmf.request.TmfDataRequest;
26 import org.eclipse.linuxtools.tmf.request.TmfEventRequest;
27 import org.eclipse.linuxtools.tmf.signal.TmfExperimentSelectedSignal;
28 import org.eclipse.linuxtools.tmf.signal.TmfExperimentUpdatedSignal;
29 import org.eclipse.linuxtools.tmf.signal.TmfSignalHandler;
30 import org.eclipse.linuxtools.tmf.signal.TmfSignalManager;
31 import org.eclipse.linuxtools.tmf.signal.TmfTraceUpdatedSignal;
32 import org.eclipse.linuxtools.tmf.trace.ITmfContext;
33 import org.eclipse.linuxtools.tmf.trace.ITmfLocation;
34 import org.eclipse.linuxtools.tmf.trace.ITmfTrace;
35 import org.eclipse.linuxtools.tmf.trace.TmfCheckpoint;
36 import org.eclipse.linuxtools.tmf.trace.TmfContext;
37
38 /**
39 * <b><u>TmfExperiment</u></b>
40 * <p>
41 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces
42 * that are part of a tracing experiment.
43 * <p>
44 */
45 public class TmfExperiment<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace {
46
47 // ------------------------------------------------------------------------
48 // Attributes
49 // ------------------------------------------------------------------------
50
51 // The currently selected experiment
52 protected static TmfExperiment<?> fCurrentExperiment = null;
53
54 // The set of traces that constitute the experiment
55 protected ITmfTrace[] fTraces;
56
57 // The total number of events
58 protected long fNbEvents;
59
60 // The experiment time range
61 protected TmfTimeRange fTimeRange;
62
63 // The experiment reference timestamp (default: Zero)
64 protected TmfTimestamp fEpoch;
65
66 // The experiment index
67 protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
68
69 // The current experiment context
70 protected TmfExperimentContext fExperimentContext;
71
72 // ------------------------------------------------------------------------
73 // Constructors
74 // ------------------------------------------------------------------------
75
76 /**
77 * @param type
78 * @param id
79 * @param traces
80 * @param epoch
81 * @param indexPageSize
82 */
83 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize) {
84 this(type, id, traces, TmfTimestamp.Zero, indexPageSize, false);
85 }
86
87 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, TmfTimestamp epoch, int indexPageSize, boolean preIndexExperiment) {
88 super(id, type);
89
90 fTraces = traces;
91 fEpoch = epoch;
92 fIndexPageSize = indexPageSize;
93
94 if (preIndexExperiment) indexExperiment(true);
95
96 updateTimeRange();
97 }
98
99 protected TmfExperiment(String id, Class<T> type) {
100 super(id, type);
101 }
102
103 /**
104 * @param type
105 * @param id
106 * @param traces
107 */
108 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces) {
109 this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE);
110 }
111
112 /**
113 * @param type
114 * @param id
115 * @param traces
116 * @param indexPageSize
117 */
118 public TmfExperiment(Class<T> type, String id, ITmfTrace[] traces, int indexPageSize) {
119 this(type, id, traces, TmfTimestamp.Zero, indexPageSize);
120 }
121
122 /**
123 * Copy constructor
124 * @param other
125 */
126 public TmfExperiment(TmfExperiment<T> other) {
127 super(other.getName() + "(clone)", other.fType);
128
129 fEpoch = other.fEpoch;
130 fIndexPageSize = other.fIndexPageSize;
131
132 fTraces = new ITmfTrace[other.fTraces.length];
133 for (int trace = 0; trace < other.fTraces.length; trace++) {
134 fTraces[trace] = other.fTraces[trace].createTraceCopy();
135 }
136
137 fNbEvents = other.fNbEvents;
138 fTimeRange = other.fTimeRange;
139 }
140
141 public TmfExperiment<T> createTraceCopy() {
142 TmfExperiment<T> experiment = new TmfExperiment<T>(this);
143 TmfSignalManager.deregister(experiment);
144 return experiment;
145 }
146
147 /**
148 * Clears the experiment
149 */
150 @Override
151 public synchronized void dispose() {
152 if (fTraces != null) {
153 for (ITmfTrace trace : fTraces) {
154 trace.dispose();
155 }
156 fTraces = null;
157 }
158 if (fCheckpoints != null) {
159 fCheckpoints.clear();
160 }
161 super.dispose();
162 }
163
164 // ------------------------------------------------------------------------
165 // ITmfTrace
166 // ------------------------------------------------------------------------
167
168 public String getPath() {
169 return null;
170 }
171
172 public long getNbEvents() {
173 return fNbEvents;
174 }
175
176 public int getCacheSize() {
177 return fIndexPageSize;
178 }
179
180 public TmfTimeRange getTimeRange() {
181 return fTimeRange;
182 }
183
184 public TmfTimestamp getStartTime() {
185 return fTimeRange.getStartTime();
186 }
187
188 public TmfTimestamp getEndTime() {
189 return fTimeRange.getEndTime();
190 }
191
192 public Vector<TmfCheckpoint> getCheckpoints() {
193 return fCheckpoints;
194 }
195
196 // ------------------------------------------------------------------------
197 // Accessors
198 // ------------------------------------------------------------------------
199
200 private static void setCurrentExperiment(TmfExperiment<?> experiment) {
201 fCurrentExperiment = experiment;
202 }
203
204 public static TmfExperiment<?> getCurrentExperiment() {
205 return fCurrentExperiment;
206 }
207
208 public TmfTimestamp getEpoch() {
209 return fEpoch;
210 }
211
212 public ITmfTrace[] getTraces() {
213 return fTraces;
214 }
215
216 /**
217 * Returns the rank of the first event with the requested timestamp.
218 * If none, returns the index of the next event (if any).
219 *
220 * @param timestamp
221 * @return
222 */
223 public long getRank(TmfTimestamp timestamp) {
224 TmfExperimentContext context = seekEvent(timestamp);
225 return context.getRank();
226 }
227
228 /**
229 * Returns the timestamp of the event at the requested index.
230 * If none, returns null.
231 *
232 * @param index
233 * @return
234 */
235 public TmfTimestamp getTimestamp(int index) {
236 TmfExperimentContext context = seekEvent(index);
237 TmfEvent event = getNextEvent(context);
238 return (event != null) ? event.getTimestamp() : null;
239 }
240
241 // ------------------------------------------------------------------------
242 // Operators
243 // ------------------------------------------------------------------------
244
245 /**
246 * Update the total number of events
247 */
248 private void updateNbEvents() {
249 int nbEvents = 0;
250 for (ITmfTrace trace : fTraces) {
251 nbEvents += trace.getNbEvents();
252 }
253 fNbEvents = nbEvents;
254 }
255
256 /**
257 * Update the global time range
258 */
259 private void updateTimeRange() {
260 TmfTimestamp startTime = fTimeRange != null ? fTimeRange.getStartTime() : TmfTimestamp.BigCrunch;
261 TmfTimestamp endTime = fTimeRange != null ? fTimeRange.getEndTime() : TmfTimestamp.BigBang;
262
263 for (ITmfTrace trace : fTraces) {
264 TmfTimestamp traceStartTime = trace.getStartTime();
265 if (traceStartTime.compareTo(startTime, true) < 0)
266 startTime = traceStartTime;
267 TmfTimestamp traceEndTime = trace.getEndTime();
268 if (traceEndTime.compareTo(endTime, true) > 0)
269 endTime = traceEndTime;
270 }
271 fTimeRange = new TmfTimeRange(startTime, endTime);
272 }
273
274 // ------------------------------------------------------------------------
275 // TmfProvider
276 // ------------------------------------------------------------------------
277
278 @Override
279 public ITmfContext armRequest(ITmfDataRequest<T> request) {
280 // Tracer.trace("Ctx: Arming request - start");
281 TmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ?
282 ((ITmfEventRequest<T>) request).getRange().getStartTime() : null;
283 TmfExperimentContext context = (timestamp != null) ?
284 seekEvent(timestamp) : seekEvent(request.getIndex());
285 // Tracer.trace("Ctx: Arming request - done");
286 return context;
287 }
288
289 @SuppressWarnings("unchecked")
290 @Override
291 public T getNext(ITmfContext context) {
292 if (context instanceof TmfExperimentContext) {
293 return (T) getNextEvent((TmfExperimentContext) context);
294 }
295 return null;
296 }
297
298 // ------------------------------------------------------------------------
299 // ITmfTrace trace positioning
300 // ------------------------------------------------------------------------
301
302 // Returns a brand new context based on the location provided
303 // and initializes the event queues
304 public synchronized TmfExperimentContext seekLocation(ITmfLocation<?> location) {
305
306 // Validate the location
307 if (location != null && !(location instanceof TmfExperimentLocation)) {
308 return null; // Throw an exception?
309 }
310
311 // Instantiate the location
312 TmfExperimentLocation expLocation = (location == null)
313 ? new TmfExperimentLocation(new ITmfLocation<?>[fTraces.length], new long[fTraces.length])
314 : (TmfExperimentLocation) location.clone();
315
316 // Create and populate the context's traces contexts
317 TmfExperimentContext context = new TmfExperimentContext(fTraces, new TmfContext[fTraces.length]);
318 // Tracer.trace("Ctx: SeekLocation - start");
319
320 long rank = 0;
321 for (int i = 0; i < fTraces.length; i++) {
322 // Get the relevant trace attributes
323 ITmfLocation<?> traceLocation = expLocation.getLocation()[i];
324 long traceRank = expLocation.getRanks()[i];
325
326 // Set the corresponding sub-context
327 context.getContexts()[i] = fTraces[i].seekLocation(traceLocation);
328 context.getContexts()[i].setRank(traceRank);
329 rank += traceRank;
330
331 // Set the trace location and read the corresponding event
332 expLocation.getLocation()[i] = context.getContexts()[i].getLocation();
333 context.getEvents()[i] = fTraces[i].getNextEvent(context.getContexts()[i]);
334 }
335
336 // Tracer.trace("Ctx: SeekLocation - done");
337
338 // Finalize context
339 context.setLocation(expLocation);
340 context.setLastTrace(TmfExperimentContext.NO_TRACE);
341 context.setRank(rank);
342
343 fExperimentContext = context;
344
345 return context;
346 }
347
348 /* (non-Javadoc)
349 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.event.TmfTimestamp)
350 */
351 public synchronized TmfExperimentContext seekEvent(TmfTimestamp timestamp) {
352
353 // Tracer.trace("Ctx: seekEvent(TS) - start");
354
355 if (timestamp == null) {
356 timestamp = TmfTimestamp.BigBang;
357 }
358
359 // First, find the right checkpoint
360 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
361
362 // In the very likely case that the checkpoint was not found, bsearch
363 // returns its negated would-be location (not an offset...). From that
364 // index, we can then position the stream and get the event.
365 if (index < 0) {
366 index = Math.max(0, -(index + 2));
367 }
368
369 // Position the experiment at the checkpoint
370 ITmfLocation<?> location;
371 synchronized (fCheckpoints) {
372 if (fCheckpoints.size() > 0) {
373 if (index >= fCheckpoints.size()) {
374 index = fCheckpoints.size() - 1;
375 }
376 location = fCheckpoints.elementAt(index).getLocation();
377 }
378 else {
379 location = null;
380 }
381 }
382
383 TmfExperimentContext context = seekLocation(location);
384 context.setRank((long) index * fIndexPageSize);
385
386 // And locate the event
387 TmfEvent event = parseEvent(context);
388 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
389 getNextEvent(context);
390 event = parseEvent(context);
391 }
392
393 if (event == null) {
394 context.setLocation(null);
395 context.setRank(ITmfContext.UNKNOWN_RANK);
396 }
397
398 return context;
399 }
400
401 /* (non-Javadoc)
402 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
403 */
404 public synchronized TmfExperimentContext seekEvent(long rank) {
405
406 // Tracer.trace("Ctx: seekEvent(rank) - start");
407
408 // Position the stream at the previous checkpoint
409 int index = (int) rank / fIndexPageSize;
410 ITmfLocation<?> location;
411 synchronized (fCheckpoints) {
412 if (fCheckpoints.size() == 0) {
413 location = null;
414 }
415 else {
416 if (index >= fCheckpoints.size()) {
417 index = fCheckpoints.size() - 1;
418 }
419 location = fCheckpoints.elementAt(index).getLocation();
420 }
421 }
422
423 TmfExperimentContext context = seekLocation(location);
424 context.setRank((long) index * fIndexPageSize);
425
426 // And locate the event
427 TmfEvent event = parseEvent(context);
428 long pos = context.getRank();
429 while (event != null && pos++ < rank) {
430 getNextEvent(context);
431 event = parseEvent(context);
432 }
433
434 if (event == null) {
435 context.setLocation(null);
436 context.setRank(ITmfContext.UNKNOWN_RANK);
437 }
438
439 return context;
440 }
441
442 /**
443 * Scan the next events from all traces and return the next one
444 * in chronological order.
445 *
446 * @param context
447 * @return
448 */
449
450 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
451
452 // TmfContext context0 = context.getContexts()[0];
453 // TmfEvent event0 = context.getEvents()[0];
454 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
455 // long rank0 = context.getRank();
456 // int trace = context.getLastTrace();
457 //
458 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
459 //
460 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
461 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
462 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
463 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
464 // Tracer.trace(result.toString());
465 // }
466
467 public synchronized TmfEvent getNextEvent(TmfContext context) {
468
469 // Validate the context
470 if (!(context instanceof TmfExperimentContext)) {
471 return null; // Throw an exception?
472 }
473
474 if (!context.equals(fExperimentContext)) {
475 // Tracer.trace("Ctx: Restoring context");
476 seekLocation(context.getLocation());
477 }
478
479 TmfExperimentContext expContext = (TmfExperimentContext) context;
480
481 // dumpContext(expContext, true);
482
483 // If an event was consumed previously, get the next one from that trace
484 int lastTrace = expContext.getLastTrace();
485 if (lastTrace != TmfExperimentContext.NO_TRACE) {
486 TmfContext traceContext = expContext.getContexts()[lastTrace];
487 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
488 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
489 }
490
491 // Scan the candidate events and identify the "next" trace to read from
492 int trace = TmfExperimentContext.NO_TRACE;
493 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
494 for (int i = 0; i < expContext.getTraces().length; i++) {
495 TmfEvent event = expContext.getEvents()[i];
496 if (event != null && event.getTimestamp() != null) {
497 TmfTimestamp otherTS = event.getTimestamp();
498 if (otherTS.compareTo(timestamp, true) < 0) {
499 trace = i;
500 timestamp = otherTS;
501 }
502 }
503 }
504
505 // Update the experiment context and set the "next" event
506 TmfEvent event = null;
507 if (trace != TmfExperimentContext.NO_TRACE) {
508 updateIndex(expContext, timestamp);
509
510 TmfContext traceContext = expContext.getContexts()[trace];
511 TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
512 expLocation.getLocation()[trace] = traceContext.getLocation().clone();
513
514 // updateIndex(expContext, timestamp);
515
516 expLocation.getRanks()[trace] = traceContext.getRank();
517 expContext.setLastTrace(trace);
518 expContext.updateRank(1);
519 event = expContext.getEvents()[trace];
520 }
521
522 // if (event != null) {
523 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
524 // dumpContext(expContext, false);
525 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
526 // }
527
528 return event;
529 }
530
531 public synchronized void updateIndex(ITmfContext context, TmfTimestamp timestamp) {
532 // Build the index as we go along
533 long rank = context.getRank();
534 if (context.isValidRank() && (rank % fIndexPageSize) == 0) {
535 // Determine the table position
536 long position = rank / fIndexPageSize;
537 // Add new entry at proper location (if empty)
538 if (fCheckpoints.size() == position) {
539 ITmfLocation<?> location = context.getLocation().clone();
540 fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location));
541 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", " + location.toString());
542 }
543 }
544 }
545
546 /* (non-Javadoc)
547 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools.tmf.trace.TmfContext)
548 */
549 public TmfEvent parseEvent(TmfContext context) {
550
551 // Validate the context
552 if (!(context instanceof TmfExperimentContext)) {
553 return null; // Throw an exception?
554 }
555
556 if (!context.equals(fExperimentContext)) {
557 // Tracer.trace("Ctx: Restoring context");
558 seekLocation(context.getLocation());
559 }
560
561 TmfExperimentContext expContext = (TmfExperimentContext) context;
562
563 // If an event was consumed previously, get the next one from that trace
564 int lastTrace = expContext.getLastTrace();
565 if (lastTrace != TmfExperimentContext.NO_TRACE) {
566 TmfContext traceContext = expContext.getContexts()[lastTrace];
567 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
568 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
569 fExperimentContext = (TmfExperimentContext) context;
570 }
571
572 // Scan the candidate events and identify the "next" trace to read from
573 int trace = TmfExperimentContext.NO_TRACE;
574 TmfTimestamp timestamp = TmfTimestamp.BigCrunch;
575 for (int i = 0; i < expContext.getTraces().length; i++) {
576 TmfEvent event = expContext.getEvents()[i];
577 if (event != null && event.getTimestamp() != null) {
578 TmfTimestamp otherTS = event.getTimestamp();
579 if (otherTS.compareTo(timestamp, true) < 0) {
580 trace = i;
581 timestamp = otherTS;
582 }
583 }
584 }
585
586 TmfEvent event = null;
587 if (trace != TmfExperimentContext.NO_TRACE) {
588 event = expContext.getEvents()[trace];
589 }
590
591 return event;
592 }
593
594 /* (non-Javadoc)
595 * @see java.lang.Object#toString()
596 */
597 @Override
598 public String toString() {
599 return "[TmfExperiment (" + getName() + ")]";
600 }
601
602 // ------------------------------------------------------------------------
603 // Indexing
604 // ------------------------------------------------------------------------
605
606 /*
607 * The experiment holds the globally ordered events of its set of traces.
608 * It is expected to provide access to each individual event by index i.e.
609 * it must be possible to request the Nth event of the experiment.
610 *
611 * The purpose of the index is to keep the information needed to rapidly
612 * restore the traces contexts at regular intervals (every INDEX_PAGE_SIZE
613 * event).
614 */
615
616 // The index page size
617 private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
618 protected int fIndexPageSize;
619
620 // private static BufferedWriter fEventLog = null;
621 // private static BufferedWriter openLogFile(String filename) {
622 // BufferedWriter outfile = null;
623 // try {
624 // outfile = new BufferedWriter(new FileWriter(filename));
625 // } catch (IOException e) {
626 // e.printStackTrace();
627 // }
628 // return outfile;
629 // }
630
631 @SuppressWarnings("unchecked")
632 private void indexExperiment(boolean waitForCompletion) {
633
634 fCheckpoints.clear();
635
636 // fEventLog = openLogFile("TraceEvent.log");
637 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
638
639 ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, TmfTimeRange.Eternity,
640 TmfDataRequest.ALL_DATA, 1, ITmfDataRequest.ExecutionType.BACKGROUND) {
641
642 // long indexingStart = System.nanoTime();
643
644 TmfTimestamp startTime = null;
645 TmfTimestamp lastTime = null;
646
647 @Override
648 public void handleData(TmfEvent event) {
649 super.handleData(event);
650 if (event != null) {
651 TmfTimestamp ts = event.getTimestamp();
652 if (startTime == null)
653 startTime = new TmfTimestamp(ts);
654 lastTime = new TmfTimestamp(ts);
655
656 if ((getNbRead() % DEFAULT_INDEX_PAGE_SIZE) == 0) {
657 updateExperiment();
658 }
659 }
660 }
661
662 @Override
663 public void handleSuccess() {
664 // long indexingEnd = System.nanoTime();
665
666 updateExperiment();
667 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
668
669 // long average = (indexingEnd - indexingStart) / fNbEvents;
670 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed=" + (indexingEnd * 1.0 - indexingStart) / 1000000000);
671 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "." + (average % 1000) + " us/evt)");
672 }
673
674 private void updateExperiment() {
675 int nbRead = getNbRead();
676 if (nbRead != 0) {
677 // updateTimeRange();
678 // updateNbEvents();
679 fTimeRange = new TmfTimeRange(startTime, new TmfTimestamp(lastTime));
680 fNbEvents = nbRead;
681 notifyListeners();
682 }
683 }
684 };
685
686 sendRequest((ITmfDataRequest<T>) request);
687 if (waitForCompletion)
688 try {
689 request.waitForCompletion();
690 } catch (InterruptedException e) {
691 e.printStackTrace();
692 }
693 }
694
695 protected void notifyListeners() {
696 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
697 }
698
699 // ------------------------------------------------------------------------
700 // Signal handlers
701 // ------------------------------------------------------------------------
702
703 @TmfSignalHandler
704 public void experimentSelected(TmfExperimentSelectedSignal<T> signal) {
705 TmfExperiment<?> experiment = signal.getExperiment();
706 if (experiment == this) {
707 setCurrentExperiment(experiment);
708 indexExperiment(false);
709 }
710 else {
711 dispose();
712 }
713 }
714
715 @TmfSignalHandler
716 public void experimentUpdated(TmfExperimentUpdatedSignal signal) {
717 }
718
719 @TmfSignalHandler
720 public void traceUpdated(TmfTraceUpdatedSignal signal) {
721 // TODO: Incremental index update
722 synchronized(this) {
723 updateNbEvents();
724 updateTimeRange();
725 }
726 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , signal.getTrace()));
727 }
728
729 // ------------------------------------------------------------------------
730 // TmfDataProvider
731 // ------------------------------------------------------------------------
732
733 @Override
734 protected void queueBackgroundRequest(final ITmfDataRequest<T> request, final int blockSize, final boolean indexing) {
735
736 // TODO: Handle the data requests also...
737 if (!(request instanceof ITmfEventRequest<?>)) {
738 super.queueRequest(request);
739 return;
740 }
741 final ITmfEventRequest<T> eventRequest = (ITmfEventRequest<T>) request;
742
743 Thread thread = new Thread() {
744 @Override
745 public void run() {
746
747 // final long requestStart = System.nanoTime();
748
749 final Integer[] CHUNK_SIZE = new Integer[1];
750 CHUNK_SIZE[0] = blockSize + ((indexing) ? 1 : 0);
751
752 final Integer[] nbRead = new Integer[1];
753 nbRead[0] = 0;
754
755 // final TmfTimestamp[] timestamp = new TmfTimestamp[1];
756 // timestamp[0] = new TmfTimestamp(eventRequest.getRange().getStartTime());
757 // final TmfTimestamp endTS = eventRequest.getRange().getEndTime();
758
759 final Boolean[] isFinished = new Boolean[1];
760 isFinished[0] = Boolean.FALSE;
761
762 while (!isFinished[0]) {
763
764 // TmfEventRequest<T> subRequest = new TmfEventRequest<T>(eventRequest.getDataType(), new TmfTimeRange(timestamp[0], endTS), CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND)
765 // TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], eventRequest.getBlockize(), ExecutionType.BACKGROUND)
766 TmfDataRequest<T> subRequest = new TmfDataRequest<T>(eventRequest.getDataType(), nbRead[0], CHUNK_SIZE[0], ExecutionType.BACKGROUND)
767 {
768 @Override
769 public void handleData(T data) {
770 super.handleData(data);
771 eventRequest.handleData(data);
772 if (getNbRead() == CHUNK_SIZE[0]) {
773 nbRead[0] += getNbRead();
774 }
775 if (getNbRead() > CHUNK_SIZE[0]) {
776 System.out.println("ERROR - Read too many events");
777 }
778 }
779
780 @Override
781 public void handleCompleted() {
782 // System.out.println("Request completed at: " + timestamp[0]);
783 if (getNbRead() < CHUNK_SIZE[0]) {
784 eventRequest.done();
785 isFinished[0] = Boolean.TRUE;
786 nbRead[0] += getNbRead();
787 // System.out.println("fNbRead=" + fNbRead + ", count=" + count +", total=" + nbRead[0]);
788 }
789 super.handleCompleted();
790 }
791 };
792
793 if (!isFinished[0]) {
794 queueRequest(subRequest);
795
796 try {
797 subRequest.waitForCompletion();
798 // System.out.println("Finished at " + timestamp[0]);
799 } catch (InterruptedException e) {
800 e.printStackTrace();
801 }
802
803 // TmfTimestamp newTS = new TmfTimestamp(timestamp[0].getValue() + 1, timestamp[0].getScale(), timestamp[0].getPrecision());
804 // timestamp[0] = newTS;
805 CHUNK_SIZE[0] = blockSize;
806 // System.out.println("New timestamp: " + timestamp[0]);
807 }
808 }
809 // final long requestEnded = System.nanoTime();
810 // System.out.println("Background request completed. Elapsed= " + (requestEnded * 1.0 - requestStart) / 1000000000);
811 }
812 };
813
814 thread.start();
815 }
816
817 }
This page took 0.051031 seconds and 6 git commands to generate.