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