Merge branch 'master' into TmfEventModel
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / 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.core.experiment;
14
15 import java.io.FileNotFoundException;
16 import java.util.Collections;
17 import java.util.Vector;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.IProgressMonitor;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Status;
24 import org.eclipse.core.runtime.jobs.Job;
25 import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
26 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
28 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
29 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
30 import org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest;
31 import org.eclipse.linuxtools.tmf.core.request.ITmfEventRequest;
32 import org.eclipse.linuxtools.tmf.core.request.TmfDataRequest;
33 import org.eclipse.linuxtools.tmf.core.request.TmfEventRequest;
34 import org.eclipse.linuxtools.tmf.core.signal.TmfEndSynchSignal;
35 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentDisposedSignal;
36 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentRangeUpdatedSignal;
37 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
38 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentUpdatedSignal;
39 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalHandler;
40 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
41 import org.eclipse.linuxtools.tmf.core.signal.TmfTraceUpdatedSignal;
42 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
43 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
44 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
45 import org.eclipse.linuxtools.tmf.core.trace.TmfCheckpoint;
46 import org.eclipse.linuxtools.tmf.core.trace.TmfContext;
47
48 /**
49 * <b><u>TmfExperiment</u></b>
50 * <p>
51 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces that are part of a tracing experiment.
52 * <p>
53 */
54 public class TmfExperiment<T extends TmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> {
55
56 // ------------------------------------------------------------------------
57 // Attributes
58 // ------------------------------------------------------------------------
59
60 // The currently selected experiment
61 protected static TmfExperiment<?> fCurrentExperiment = null;
62
63 // The set of traces that constitute the experiment
64 protected ITmfTrace<T>[] fTraces;
65
66 // The total number of events
67 protected long fNbEvents;
68
69 // The experiment time range
70 protected TmfTimeRange fTimeRange;
71
72 // The experiment reference timestamp (default: Zero)
73 protected ITmfTimestamp fEpoch;
74
75 // The experiment index
76 protected Vector<TmfCheckpoint> fCheckpoints = new Vector<TmfCheckpoint>();
77
78 // The current experiment context
79 protected TmfExperimentContext fExperimentContext;
80
81 // Flag to initialize only once
82 private boolean fInitialized = false;
83
84 // The experiment resource
85 private IResource fResource;
86
87 // ------------------------------------------------------------------------
88 // Constructors
89 // ------------------------------------------------------------------------
90
91 @Override
92 public boolean validate(IProject project, String path) {
93 return true;
94 }
95
96 @Override
97 public void initTrace(String path, Class<T> eventType) throws FileNotFoundException {
98 }
99
100 @Override
101 public void initTrace(String path, Class<T> eventType, boolean indexTrace) throws FileNotFoundException {
102 }
103
104 @Override
105 public void initTrace(String path, Class<T> eventType, int cacheSize) throws FileNotFoundException {
106 }
107
108 @Override
109 public void initTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace) throws FileNotFoundException {
110 }
111
112 @Override
113 public void initTrace(String path, Class<T> eventType, int cacheSize, boolean indexTrace, String name) throws FileNotFoundException {
114 }
115
116 /**
117 * @param type
118 * @param id
119 * @param traces
120 * @param epoch
121 * @param indexPageSize
122 */
123 public TmfExperiment(Class<T> type, String id, ITmfTrace<T>[] traces, ITmfTimestamp epoch, int indexPageSize) {
124 this(type, id, traces, TmfTimestamp.Zero, indexPageSize, false);
125 }
126
127 public TmfExperiment(Class<T> type, String id, ITmfTrace<T>[] traces, ITmfTimestamp epoch, int indexPageSize, boolean preIndexExperiment) {
128 super(id, type);
129
130 fTraces = traces;
131 fEpoch = epoch;
132 fIndexPageSize = indexPageSize;
133 fTimeRange = TmfTimeRange.Null;
134
135 if (preIndexExperiment) {
136 indexExperiment(true);
137 updateTimeRange();
138 }
139
140 }
141
142 protected TmfExperiment(String id, Class<T> type) {
143 super(id, type);
144 }
145
146 /**
147 * @param type
148 * @param id
149 * @param traces
150 */
151 public TmfExperiment(Class<T> type, String id, ITmfTrace<T>[] traces) {
152 this(type, id, traces, TmfTimestamp.Zero, DEFAULT_INDEX_PAGE_SIZE);
153 }
154
155 /**
156 * @param type
157 * @param id
158 * @param traces
159 * @param indexPageSize
160 */
161 public TmfExperiment(Class<T> type, String id, ITmfTrace<T>[] traces, int indexPageSize) {
162 this(type, id, traces, TmfTimestamp.Zero, indexPageSize);
163 }
164
165 /**
166 * Copy constructor
167 *
168 * @param other
169 */
170 @SuppressWarnings("unchecked")
171 public TmfExperiment(TmfExperiment<T> other) {
172 super(other.getName() + "(clone)", other.fType); //$NON-NLS-1$
173
174 fEpoch = other.fEpoch;
175 fIndexPageSize = other.fIndexPageSize;
176
177 fTraces = new ITmfTrace[other.fTraces.length];
178 for (int trace = 0; trace < other.fTraces.length; trace++) {
179 fTraces[trace] = other.fTraces[trace].copy();
180 }
181
182 fNbEvents = other.fNbEvents;
183 fTimeRange = other.fTimeRange;
184 }
185
186 @Override
187 public TmfExperiment<T> copy() {
188 TmfExperiment<T> experiment = new TmfExperiment<T>(this);
189 TmfSignalManager.deregister(experiment);
190 return experiment;
191 }
192
193 /**
194 * Clears the experiment
195 */
196 @Override
197 @SuppressWarnings("rawtypes")
198 public synchronized void dispose() {
199
200 TmfExperimentDisposedSignal<T> signal = new TmfExperimentDisposedSignal<T>(this, this);
201 broadcast(signal);
202
203 if (fTraces != null) {
204 for (ITmfTrace trace : fTraces) {
205 trace.dispose();
206 }
207 fTraces = null;
208 }
209 if (fCheckpoints != null) {
210 fCheckpoints.clear();
211 }
212 super.dispose();
213 }
214
215 // ------------------------------------------------------------------------
216 // ITmfTrace
217 // ------------------------------------------------------------------------
218
219 @Override
220 public long getNbEvents() {
221 return fNbEvents;
222 }
223
224 @Override
225 public int getCacheSize() {
226 return fIndexPageSize;
227 }
228
229 @Override
230 public TmfTimeRange getTimeRange() {
231 return fTimeRange;
232 }
233
234 @Override
235 public ITmfTimestamp getStartTime() {
236 return fTimeRange.getStartTime();
237 }
238
239 @Override
240 public ITmfTimestamp getEndTime() {
241 return fTimeRange.getEndTime();
242 }
243
244 public Vector<TmfCheckpoint> getCheckpoints() {
245 return fCheckpoints;
246 }
247
248 // ------------------------------------------------------------------------
249 // Accessors
250 // ------------------------------------------------------------------------
251
252 public static void setCurrentExperiment(TmfExperiment<?> experiment) {
253 fCurrentExperiment = experiment;
254 }
255
256 public static TmfExperiment<?> getCurrentExperiment() {
257 return fCurrentExperiment;
258 }
259
260 public ITmfTimestamp getEpoch() {
261 return fEpoch;
262 }
263
264 public ITmfTrace<T>[] getTraces() {
265 return fTraces;
266 }
267
268 /**
269 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
270 * (if any).
271 *
272 * @param timestamp
273 * @return
274 */
275 @Override
276 public long getRank(ITmfTimestamp timestamp) {
277 TmfExperimentContext context = seekEvent(timestamp);
278 return context.getRank();
279 }
280
281 /**
282 * Returns the timestamp of the event at the requested index. If none, returns null.
283 *
284 * @param index
285 * @return
286 */
287 public ITmfTimestamp getTimestamp(int index) {
288 TmfExperimentContext context = seekEvent(index);
289 TmfEvent event = getNextEvent(context);
290 return (event != null) ? event.getTimestamp() : null;
291 }
292
293 // ------------------------------------------------------------------------
294 // Operators
295 // ------------------------------------------------------------------------
296
297 /**
298 * Update the global time range
299 */
300 protected void updateTimeRange() {
301 ITmfTimestamp startTime = fTimeRange != TmfTimeRange.Null ? fTimeRange.getStartTime() : TmfTimestamp.BigCrunch;
302 ITmfTimestamp endTime = fTimeRange != TmfTimeRange.Null ? fTimeRange.getEndTime() : TmfTimestamp.BigBang;
303
304 for (ITmfTrace<T> trace : fTraces) {
305 ITmfTimestamp traceStartTime = trace.getStartTime();
306 if (traceStartTime.compareTo(startTime, true) < 0)
307 startTime = traceStartTime;
308 ITmfTimestamp traceEndTime = trace.getEndTime();
309 if (traceEndTime.compareTo(endTime, true) > 0)
310 endTime = traceEndTime;
311 }
312 fTimeRange = new TmfTimeRange(startTime, endTime);
313 }
314
315 // ------------------------------------------------------------------------
316 // TmfProvider
317 // ------------------------------------------------------------------------
318 @Override
319 public ITmfContext armRequest(ITmfDataRequest<T> request) {
320 // Tracer.trace("Ctx: Arming request - start");
321 ITmfTimestamp timestamp = (request instanceof ITmfEventRequest<?>) ? ((ITmfEventRequest<T>) request).getRange().getStartTime()
322 : null;
323
324 if (TmfTimestamp.BigBang.equals(timestamp) || request.getIndex() > 0) {
325 timestamp = null; // use request index
326 }
327
328 TmfExperimentContext context = null;
329 if (timestamp != null) {
330 // seek by timestamp
331 context = seekEvent(timestamp);
332 ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
333 } else {
334 // Seek by rank
335 if ((fExperimentContext != null) && fExperimentContext.getRank() == request.getIndex()) {
336 // We are already at the right context -> no need to seek
337 context = fExperimentContext;
338 } else {
339 context = seekEvent(request.getIndex());
340 }
341 }
342 // Tracer.trace("Ctx: Arming request - done");
343 return context;
344 }
345
346 @SuppressWarnings("unchecked")
347 @Override
348 public T getNext(ITmfContext context) {
349 if (context instanceof TmfExperimentContext) {
350 return (T) getNextEvent((TmfExperimentContext) context);
351 }
352 return null;
353 }
354
355 // ------------------------------------------------------------------------
356 // ITmfTrace trace positioning
357 // ------------------------------------------------------------------------
358
359 // Returns a brand new context based on the location provided
360 // and initializes the event queues
361 @Override
362 public synchronized TmfExperimentContext seekLocation(ITmfLocation<?> location) {
363 // Validate the location
364 if (location != null && !(location instanceof TmfExperimentLocation)) {
365 return null; // Throw an exception?
366 }
367
368 if (fTraces == null) { // experiment has been disposed
369 return null;
370 }
371
372 // Instantiate the location
373 TmfExperimentLocation expLocation = (location == null) ? new TmfExperimentLocation(new TmfLocationArray(
374 new ITmfLocation<?>[fTraces.length]), new long[fTraces.length]) : (TmfExperimentLocation) location.clone();
375
376 // Create and populate the context's traces contexts
377 TmfExperimentContext context = new TmfExperimentContext(fTraces, new TmfContext[fTraces.length]);
378 // Tracer.trace("Ctx: SeekLocation - start");
379
380 long rank = 0;
381 for (int i = 0; i < fTraces.length; i++) {
382 // Get the relevant trace attributes
383 ITmfLocation<?> traceLocation = expLocation.getLocation().locations[i];
384 long traceRank = expLocation.getRanks()[i];
385
386 // Set the corresponding sub-context
387 context.getContexts()[i] = fTraces[i].seekLocation(traceLocation);
388 context.getContexts()[i].setRank(traceRank);
389 rank += traceRank;
390
391 // Set the trace location and read the corresponding event
392 expLocation.getLocation().locations[i] = context.getContexts()[i].getLocation().clone();
393 context.getEvents()[i] = fTraces[i].getNextEvent(context.getContexts()[i]);
394 }
395
396 // Tracer.trace("Ctx: SeekLocation - done");
397
398 // Finalize context
399 context.setLocation(expLocation);
400 context.setLastTrace(TmfExperimentContext.NO_TRACE);
401 context.setRank(rank);
402
403 fExperimentContext = context;
404
405 return context;
406 }
407
408 /*
409 * (non-Javadoc)
410 *
411 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
412 */
413 @Override
414 public synchronized TmfExperimentContext seekEvent(ITmfTimestamp timestamp) {
415
416 // Tracer.trace("Ctx: seekEvent(TS) - start");
417
418 if (timestamp == null) {
419 timestamp = TmfTimestamp.BigBang;
420 }
421
422 // First, find the right checkpoint
423 int index = Collections.binarySearch(fCheckpoints, new TmfCheckpoint(timestamp, null));
424
425 // In the very likely case that the checkpoint was not found, bsearch
426 // returns its negated would-be location (not an offset...). From that
427 // index, we can then position the stream and get the event.
428 if (index < 0) {
429 index = Math.max(0, -(index + 2));
430 }
431
432 // Position the experiment at the checkpoint
433 ITmfLocation<?> location;
434 synchronized (fCheckpoints) {
435 if (fCheckpoints.size() > 0) {
436 if (index >= fCheckpoints.size()) {
437 index = fCheckpoints.size() - 1;
438 }
439 location = fCheckpoints.elementAt(index).getLocation();
440 } else {
441 location = null;
442 }
443 }
444
445 TmfExperimentContext context = seekLocation(location);
446 context.setRank((long) index * fIndexPageSize);
447
448 // And locate the event
449 TmfEvent event = parseEvent(context);
450 while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
451 getNextEvent(context);
452 event = parseEvent(context);
453 }
454
455 if (event == null) {
456 context.setLocation(null);
457 context.setRank(ITmfContext.UNKNOWN_RANK);
458 }
459
460 return context;
461 }
462
463 /*
464 * (non-Javadoc)
465 *
466 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
467 */
468 @Override
469 public synchronized TmfExperimentContext seekEvent(long rank) {
470
471 // Tracer.trace("Ctx: seekEvent(rank) - start");
472
473 // Position the stream at the previous checkpoint
474 int index = (int) rank / fIndexPageSize;
475 ITmfLocation<?> location;
476 synchronized (fCheckpoints) {
477 if (fCheckpoints.size() == 0) {
478 location = null;
479 } else {
480 if (index >= fCheckpoints.size()) {
481 index = fCheckpoints.size() - 1;
482 }
483 location = fCheckpoints.elementAt(index).getLocation();
484 }
485 }
486
487 TmfExperimentContext context = seekLocation(location);
488 context.setRank((long) index * fIndexPageSize);
489
490 // And locate the event
491 TmfEvent event = parseEvent(context);
492 long pos = context.getRank();
493 while (event != null && pos++ < rank) {
494 getNextEvent(context);
495 event = parseEvent(context);
496 }
497
498 if (event == null) {
499 context.setLocation(null);
500 context.setRank(ITmfContext.UNKNOWN_RANK);
501 }
502
503 return context;
504 }
505
506 @Override
507 public TmfContext seekLocation(double ratio) {
508 TmfContext context = seekEvent((long) (ratio * getNbEvents()));
509 return context;
510 }
511
512 @Override
513 public double getLocationRatio(ITmfLocation<?> location) {
514 if (location instanceof TmfExperimentLocation) {
515 return (double) seekLocation(location).getRank() / getNbEvents();
516 }
517 return 0;
518 }
519
520 @Override
521 public ITmfLocation<?> getCurrentLocation() {
522 if (fExperimentContext != null) {
523 return fExperimentContext.getLocation();
524 }
525 return null;
526 }
527
528 /**
529 * Scan the next events from all traces and return the next one in chronological order.
530 *
531 * @param context
532 * @return
533 */
534
535 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
536
537 // TmfContext context0 = context.getContexts()[0];
538 // TmfEvent event0 = context.getEvents()[0];
539 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
540 // long rank0 = context.getRank();
541 // int trace = context.getLastTrace();
542 //
543 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
544 //
545 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
546 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
547 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
548 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
549 // Tracer.trace(result.toString());
550 // }
551
552 @Override
553 public synchronized TmfEvent getNextEvent(TmfContext context) {
554
555 // Validate the context
556 if (!(context instanceof TmfExperimentContext)) {
557 return null; // Throw an exception?
558 }
559
560 if (!context.equals(fExperimentContext)) {
561 // Tracer.trace("Ctx: Restoring context");
562 fExperimentContext = seekLocation(context.getLocation());
563 }
564
565 TmfExperimentContext expContext = (TmfExperimentContext) context;
566
567 // dumpContext(expContext, true);
568
569 // If an event was consumed previously, get the next one from that trace
570 int lastTrace = expContext.getLastTrace();
571 if (lastTrace != TmfExperimentContext.NO_TRACE) {
572 TmfContext traceContext = expContext.getContexts()[lastTrace];
573 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
574 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
575 }
576
577 // Scan the candidate events and identify the "next" trace to read from
578 TmfEvent eventArray[] = expContext.getEvents();
579 if (eventArray == null) {
580 return null;
581 }
582 int trace = TmfExperimentContext.NO_TRACE;
583 ITmfTimestamp timestamp = TmfTimestamp.BigCrunch;
584 if (eventArray.length == 1) {
585 if (eventArray[0] != null) {
586 timestamp = eventArray[0].getTimestamp();
587 trace = 0;
588 }
589 } else {
590 for (int i = 0; i < eventArray.length; i++) {
591 TmfEvent event = eventArray[i];
592 if (event != null && event.getTimestamp() != null) {
593 ITmfTimestamp otherTS = event.getTimestamp();
594 if (otherTS.compareTo(timestamp, true) < 0) {
595 trace = i;
596 timestamp = otherTS;
597 }
598 }
599 }
600 }
601 // Update the experiment context and set the "next" event
602 TmfEvent event = null;
603 if (trace != TmfExperimentContext.NO_TRACE) {
604 updateIndex(expContext, timestamp);
605
606 TmfContext traceContext = expContext.getContexts()[trace];
607 TmfExperimentLocation expLocation = (TmfExperimentLocation) expContext.getLocation();
608 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
609 expLocation.getLocation().locations[trace] = traceContext.getLocation().clone();
610
611 // updateIndex(expContext, timestamp);
612
613 expLocation.getRanks()[trace] = traceContext.getRank();
614 expContext.setLastTrace(trace);
615 expContext.updateRank(1);
616 event = expContext.getEvents()[trace];
617 fExperimentContext = expContext;
618 }
619
620 // if (event != null) {
621 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
622 // dumpContext(expContext, false);
623 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
624 // }
625
626 return event;
627 }
628
629 public synchronized void updateIndex(ITmfContext context, ITmfTimestamp timestamp) {
630 // Build the index as we go along
631 long rank = context.getRank();
632 if (context.isValidRank() && (rank % fIndexPageSize) == 0) {
633 // Determine the table position
634 long position = rank / fIndexPageSize;
635 // Add new entry at proper location (if empty)
636 if (fCheckpoints.size() == position) {
637 ITmfLocation<?> location = context.getLocation().clone();
638 fCheckpoints.add(new TmfCheckpoint(timestamp.clone(), location));
639 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
640 // + location.toString());
641 }
642 }
643 }
644
645 /*
646 * (non-Javadoc)
647 *
648 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
649 */
650 @Override
651 public TmfEvent parseEvent(TmfContext context) {
652
653 // Validate the context
654 if (!(context instanceof TmfExperimentContext)) {
655 return null; // Throw an exception?
656 }
657
658 if (!context.equals(fExperimentContext)) {
659 // Tracer.trace("Ctx: Restoring context");
660 seekLocation(context.getLocation());
661 }
662
663 TmfExperimentContext expContext = (TmfExperimentContext) context;
664
665 // If an event was consumed previously, get the next one from that trace
666 int lastTrace = expContext.getLastTrace();
667 if (lastTrace != TmfExperimentContext.NO_TRACE) {
668 TmfContext traceContext = expContext.getContexts()[lastTrace];
669 expContext.getEvents()[lastTrace] = expContext.getTraces()[lastTrace].getNextEvent(traceContext);
670 expContext.setLastTrace(TmfExperimentContext.NO_TRACE);
671 fExperimentContext = (TmfExperimentContext) context;
672 }
673
674 // Scan the candidate events and identify the "next" trace to read from
675 int trace = TmfExperimentContext.NO_TRACE;
676 ITmfTimestamp timestamp = TmfTimestamp.BigCrunch;
677 for (int i = 0; i < expContext.getTraces().length; i++) {
678 TmfEvent event = expContext.getEvents()[i];
679 if (event != null && event.getTimestamp() != null) {
680 ITmfTimestamp otherTS = event.getTimestamp();
681 if (otherTS.compareTo(timestamp, true) < 0) {
682 trace = i;
683 timestamp = otherTS;
684 }
685 }
686 }
687
688 TmfEvent event = null;
689 if (trace != TmfExperimentContext.NO_TRACE) {
690 event = expContext.getEvents()[trace];
691 }
692
693 return event;
694 }
695
696 /*
697 * (non-Javadoc)
698 *
699 * @see java.lang.Object#toString()
700 */
701 @Override
702 @SuppressWarnings("nls")
703 public String toString() {
704 return "[TmfExperiment (" + getName() + ")]";
705 }
706
707 // ------------------------------------------------------------------------
708 // Indexing
709 // ------------------------------------------------------------------------
710
711 private synchronized void initializeStreamingMonitor() {
712 if (fInitialized) {
713 return;
714 }
715 fInitialized = true;
716
717 if (getStreamingInterval() == 0) {
718 TmfContext context = seekLocation(null);
719 TmfEvent event = getNext(context);
720 if (event == null) {
721 return;
722 }
723 TmfTimeRange timeRange = new TmfTimeRange(event.getTimestamp(), TmfTimestamp.BigCrunch);
724 final TmfExperimentRangeUpdatedSignal signal = new TmfExperimentRangeUpdatedSignal(this, this, timeRange);
725
726 // Broadcast in separate thread to prevent deadlock
727 new Thread() {
728 @Override
729 public void run() {
730 broadcast(signal);
731 }
732 }.start();
733 return;
734 }
735
736 final Thread thread = new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
737 ITmfTimestamp safeTimestamp = null;
738 TmfTimeRange timeRange = null;
739
740 @Override
741 public void run() {
742 while (!fExecutor.isShutdown()) {
743 if (!isIndexingBusy()) {
744 ITmfTimestamp startTimestamp = TmfTimestamp.BigCrunch;
745 ITmfTimestamp endTimestamp = TmfTimestamp.BigBang;
746 for (ITmfTrace<T> trace : fTraces) {
747 if (trace.getStartTime().compareTo(startTimestamp) < 0) {
748 startTimestamp = trace.getStartTime();
749 }
750 if (trace.getStreamingInterval() != 0 && trace.getEndTime().compareTo(endTimestamp) > 0) {
751 endTimestamp = trace.getEndTime();
752 }
753 }
754 if (safeTimestamp != null && safeTimestamp.compareTo(getTimeRange().getEndTime(), false) > 0) {
755 timeRange = new TmfTimeRange(startTimestamp, safeTimestamp);
756 } else {
757 timeRange = null;
758 }
759 safeTimestamp = endTimestamp;
760 if (timeRange != null) {
761 TmfExperimentRangeUpdatedSignal signal =
762 new TmfExperimentRangeUpdatedSignal(TmfExperiment.this, TmfExperiment.this, timeRange);
763 broadcast(signal);
764 }
765 }
766 try {
767 Thread.sleep(getStreamingInterval());
768 } catch (InterruptedException e) {
769 e.printStackTrace();
770 }
771 }
772 }
773 };
774 thread.start();
775 }
776
777 /* (non-Javadoc)
778 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
779 */
780 @Override
781 public long getStreamingInterval() {
782 long interval = 0;
783 for (ITmfTrace<T> trace : fTraces) {
784 interval = Math.max(interval, trace.getStreamingInterval());
785 }
786 return interval;
787 }
788
789 /*
790 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
791 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
792 *
793 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
794 * intervals (every INDEX_PAGE_SIZE event).
795 */
796
797 // The index page size
798 private static final int DEFAULT_INDEX_PAGE_SIZE = 5000;
799 protected int fIndexPageSize;
800 protected boolean fIndexing = false;
801 protected TmfTimeRange fIndexingPendingRange = TmfTimeRange.Null;
802
803 private Integer fEndSynchReference;
804
805 // private static BufferedWriter fEventLog = null;
806 // private static BufferedWriter openLogFile(String filename) {
807 // BufferedWriter outfile = null;
808 // try {
809 // outfile = new BufferedWriter(new FileWriter(filename));
810 // } catch (IOException e) {
811 // e.printStackTrace();
812 // }
813 // return outfile;
814 // }
815
816 protected boolean isIndexingBusy() {
817 synchronized (fCheckpoints) {
818 return fIndexing;
819 }
820 }
821
822 protected void indexExperiment(boolean waitForCompletion) {
823 indexExperiment(waitForCompletion, 0, TmfTimeRange.Eternity);
824 }
825
826 @SuppressWarnings("unchecked")
827 protected void indexExperiment(boolean waitForCompletion, final int index, final TmfTimeRange timeRange) {
828
829 synchronized (fCheckpoints) {
830 if (fIndexing) {
831 return;
832 }
833 fIndexing = true;
834 }
835
836 final Job job = new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
837 @Override
838 protected IStatus run(IProgressMonitor monitor) {
839 while (!monitor.isCanceled()) {
840 try {
841 Thread.sleep(100);
842 } catch (InterruptedException e) {
843 return Status.OK_STATUS;
844 }
845 }
846 monitor.done();
847 return Status.OK_STATUS;
848 }
849 };
850 job.schedule();
851
852 // fEventLog = openLogFile("TraceEvent.log");
853 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
854
855 ITmfEventRequest<TmfEvent> request = new TmfEventRequest<TmfEvent>(TmfEvent.class, timeRange, index, TmfDataRequest.ALL_DATA,
856 fIndexPageSize, ITmfDataRequest.ExecutionType.BACKGROUND) { // PATA FOREGROUND
857
858 // long indexingStart = System.nanoTime();
859
860 ITmfTimestamp startTime = (fTimeRange == TmfTimeRange.Null) ? null : fTimeRange.getStartTime();
861 ITmfTimestamp lastTime = (fTimeRange == TmfTimeRange.Null) ? null : fTimeRange.getEndTime();
862 long initialNbEvents = fNbEvents;
863
864 @Override
865 public void handleStarted() {
866 super.handleStarted();
867 }
868
869 @Override
870 public void handleData(TmfEvent event) {
871 super.handleData(event);
872 if (event != null) {
873 ITmfTimestamp ts = event.getTimestamp();
874 if (startTime == null)
875 startTime = ts.clone();
876 lastTime = ts.clone();
877 if ((getNbRead() % fIndexPageSize) == 1 && getNbRead() != 1) {
878 updateExperiment();
879 }
880 }
881 }
882
883 @Override
884 public void handleSuccess() {
885 // long indexingEnd = System.nanoTime();
886
887 if (getRange() != TmfTimeRange.Eternity) {
888 lastTime = getRange().getEndTime();
889 }
890 updateExperiment();
891 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
892
893 // long average = (indexingEnd - indexingStart) / fNbEvents;
894 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
895 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
896 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
897 // + (average % 1000) + " us/evt)");
898 super.handleSuccess();
899 }
900
901 @Override
902 public void handleCompleted() {
903 job.cancel();
904 super.handleCompleted();
905 synchronized (fCheckpoints) {
906 fIndexing = false;
907 if (fIndexingPendingRange != TmfTimeRange.Null) {
908 indexExperiment(false, (int) fNbEvents, fIndexingPendingRange);
909 fIndexingPendingRange = TmfTimeRange.Null;
910 }
911 }
912 }
913
914 private void updateExperiment() {
915 int nbRead = getNbRead();
916 if (startTime != null) {
917 fTimeRange = new TmfTimeRange(startTime, lastTime.clone());
918 }
919 if (nbRead != 0) {
920 // updateTimeRange();
921 // updateNbEvents();
922 fNbEvents = initialNbEvents + nbRead;
923 notifyListeners();
924 }
925 }
926 };
927
928 sendRequest((ITmfDataRequest<T>) request);
929 if (waitForCompletion)
930 try {
931 request.waitForCompletion();
932 } catch (InterruptedException e) {
933 e.printStackTrace();
934 }
935 }
936
937 protected void notifyListeners() {
938 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
939 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
940 }
941
942 // ------------------------------------------------------------------------
943 // Signal handlers
944 // ------------------------------------------------------------------------
945
946 @TmfSignalHandler
947 public void experimentSelected(TmfExperimentSelectedSignal<T> signal) {
948 TmfExperiment<?> experiment = signal.getExperiment();
949 if (experiment == this) {
950 setCurrentExperiment(experiment);
951 fEndSynchReference = new Integer(signal.getReference());
952 }
953 }
954
955 @TmfSignalHandler
956 public void endSync(TmfEndSynchSignal signal) {
957 if (fEndSynchReference != null && fEndSynchReference.intValue() == signal.getReference()) {
958 fEndSynchReference = null;
959 initializeStreamingMonitor();
960 }
961
962 }
963
964 @TmfSignalHandler
965 public void experimentUpdated(TmfExperimentUpdatedSignal signal) {
966 }
967
968 @TmfSignalHandler
969 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal) {
970 indexExperiment(false, (int) fNbEvents, signal.getRange());
971 }
972
973 @TmfSignalHandler
974 public void traceUpdated(TmfTraceUpdatedSignal signal) {
975 for (ITmfTrace<T> trace : fTraces) {
976 if (trace == signal.getTrace()) {
977 synchronized (fCheckpoints) {
978 if (fIndexing) {
979 if (fIndexingPendingRange == TmfTimeRange.Null) {
980 fIndexingPendingRange = signal.getRange();
981 } else {
982 ITmfTimestamp startTime = fIndexingPendingRange.getStartTime();
983 ITmfTimestamp endTime = fIndexingPendingRange.getEndTime();
984 if (signal.getRange().getStartTime().compareTo(startTime) < 0) {
985 startTime = signal.getRange().getStartTime();
986 }
987 if (signal.getRange().getEndTime().compareTo(endTime) > 0) {
988 endTime = signal.getRange().getEndTime();
989 }
990 fIndexingPendingRange = new TmfTimeRange(startTime, endTime);
991 }
992 return;
993 }
994 }
995 indexExperiment(false, (int) fNbEvents, signal.getRange());
996 return;
997 }
998 }
999 }
1000
1001 @Override
1002 public String getPath() {
1003 // TODO Auto-generated method stub
1004 return null;
1005 }
1006
1007 /**
1008 * Set the resource to be used for bookmarks on this experiment
1009 * @param resource the bookmarks resource
1010 */
1011 public void setResource(IResource resource) {
1012 fResource = resource;
1013 }
1014
1015 /**
1016 * Get the resource used for bookmarks on this experiment
1017 * @return the bookmarks resource or null if none is set
1018 */
1019 public IResource getResource() {
1020 return fResource;
1021 }
1022 }
This page took 0.058233 seconds and 6 git commands to generate.