1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.experiment
;
15 import java
.util
.Collections
;
16 import java
.util
.Vector
;
18 import org
.eclipse
.core
.resources
.IProject
;
19 import org
.eclipse
.core
.resources
.IResource
;
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
.ITmfEvent
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfDataRequest
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.request
.TmfEventRequest
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEndSynchSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentDisposedSignal
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentRangeUpdatedSignal
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentUpdatedSignal
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
39 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
40 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
41 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
42 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
43 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
44 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfCheckpoint
;
45 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfContext
;
48 * <b><u>TmfExperiment</u></b>
50 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces that are part of a tracing experiment.
53 public class TmfExperiment
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
59 // The currently selected experiment
60 protected static TmfExperiment
<?
> fCurrentExperiment
= null;
62 // The set of traces that constitute the experiment
63 protected ITmfTrace
<T
>[] fTraces
;
65 // The total number of events
66 protected long fNbEvents
;
68 // The experiment time range
69 protected TmfTimeRange fTimeRange
;
71 // The experiment reference timestamp (default: Zero)
72 protected ITmfTimestamp fEpoch
;
74 // The experiment index
75 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
77 // The current experiment context
78 protected TmfExperimentContext fExperimentContext
;
80 // Flag to initialize only once
81 private boolean fInitialized
= false;
83 // The experiment resource
84 private IResource fResource
;
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
91 public boolean validate(IProject project
, String path
) {
96 public void initTrace(String name
, String path
, Class
<T
> eventType
) {
100 public void initTrace(String name
, String path
, Class
<T
> eventType
, boolean indexTrace
) {
102 initializeStreamingMonitor();
107 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
) {
111 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) {
113 initializeStreamingMonitor();
122 * @param indexPageSize
124 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
) {
125 this(type
, id
, traces
, TmfTimestamp
.Zero
, indexPageSize
, false);
128 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
, boolean preIndexExperiment
) {
133 fIndexPageSize
= indexPageSize
;
134 fTimeRange
= TmfTimeRange
.Null
;
136 if (preIndexExperiment
) {
137 indexExperiment(true);
143 protected TmfExperiment(String id
, Class
<T
> type
) {
152 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
) {
153 this(type
, id
, traces
, TmfTimestamp
.Zero
, DEFAULT_INDEX_PAGE_SIZE
);
160 * @param indexPageSize
162 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, int indexPageSize
) {
163 this(type
, id
, traces
, TmfTimestamp
.Zero
, indexPageSize
);
171 @SuppressWarnings("unchecked")
172 public TmfExperiment(TmfExperiment
<T
> other
) {
173 super(other
.getName() + "(clone)", other
.fType
); //$NON-NLS-1$
175 fEpoch
= other
.fEpoch
;
176 fIndexPageSize
= other
.fIndexPageSize
;
178 fTraces
= new ITmfTrace
[other
.fTraces
.length
];
179 for (int trace
= 0; trace
< other
.fTraces
.length
; trace
++) {
180 fTraces
[trace
] = other
.fTraces
[trace
].copy();
183 fNbEvents
= other
.fNbEvents
;
184 fTimeRange
= other
.fTimeRange
;
188 public TmfExperiment
<T
> copy() {
189 TmfExperiment
<T
> experiment
= new TmfExperiment
<T
>(this);
190 TmfSignalManager
.deregister(experiment
);
195 * Clears the experiment
198 @SuppressWarnings("rawtypes")
199 public synchronized void dispose() {
201 TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
203 if (fCurrentExperiment
== this) {
204 fCurrentExperiment
= null;
207 if (fTraces
!= null) {
208 for (ITmfTrace trace
: fTraces
) {
213 if (fCheckpoints
!= null) {
214 fCheckpoints
.clear();
219 // ------------------------------------------------------------------------
221 // ------------------------------------------------------------------------
224 public long getNbEvents() {
229 public int getCacheSize() {
230 return fIndexPageSize
;
234 public TmfTimeRange
getTimeRange() {
239 public ITmfTimestamp
getStartTime() {
240 return fTimeRange
.getStartTime();
244 public ITmfTimestamp
getEndTime() {
245 return fTimeRange
.getEndTime();
248 public Vector
<TmfCheckpoint
> getCheckpoints() {
252 // ------------------------------------------------------------------------
254 // ------------------------------------------------------------------------
256 public static void setCurrentExperiment(TmfExperiment
<?
> experiment
) {
257 if (fCurrentExperiment
!= null && fCurrentExperiment
!= experiment
) {
258 fCurrentExperiment
.dispose();
260 fCurrentExperiment
= experiment
;
263 public static TmfExperiment
<?
> getCurrentExperiment() {
264 return fCurrentExperiment
;
267 public ITmfTimestamp
getEpoch() {
271 public ITmfTrace
<T
>[] getTraces() {
276 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
283 public long getRank(ITmfTimestamp timestamp
) {
284 TmfExperimentContext context
= seekEvent(timestamp
);
285 return context
.getRank();
289 * Returns the timestamp of the event at the requested index. If none, returns null.
294 public ITmfTimestamp
getTimestamp(int index
) {
295 TmfExperimentContext context
= seekEvent(index
);
296 ITmfEvent event
= getNextEvent(context
);
297 return (event
!= null) ? event
.getTimestamp() : null;
300 // ------------------------------------------------------------------------
302 // ------------------------------------------------------------------------
305 * Update the global time range
307 protected void updateTimeRange() {
308 ITmfTimestamp startTime
= fTimeRange
!= TmfTimeRange
.Null ? fTimeRange
.getStartTime() : TmfTimestamp
.BigCrunch
;
309 ITmfTimestamp endTime
= fTimeRange
!= TmfTimeRange
.Null ? fTimeRange
.getEndTime() : TmfTimestamp
.BigBang
;
311 for (ITmfTrace
<T
> trace
: fTraces
) {
312 ITmfTimestamp traceStartTime
= trace
.getStartTime();
313 if (traceStartTime
.compareTo(startTime
, true) < 0)
314 startTime
= traceStartTime
;
315 ITmfTimestamp traceEndTime
= trace
.getEndTime();
316 if (traceEndTime
.compareTo(endTime
, true) > 0)
317 endTime
= traceEndTime
;
319 fTimeRange
= new TmfTimeRange(startTime
, endTime
);
322 // ------------------------------------------------------------------------
324 // ------------------------------------------------------------------------
326 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
327 // Tracer.trace("Ctx: Arming request - start");
328 ITmfTimestamp timestamp
= (request
instanceof ITmfEventRequest
<?
>) ?
((ITmfEventRequest
<T
>) request
).getRange().getStartTime()
331 if (TmfTimestamp
.BigBang
.equals(timestamp
) || request
.getIndex() > 0) {
332 timestamp
= null; // use request index
335 TmfExperimentContext context
= null;
336 if (timestamp
!= null) {
338 context
= seekEvent(timestamp
);
339 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
342 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
343 // We are already at the right context -> no need to seek
344 context
= fExperimentContext
;
346 context
= seekEvent(request
.getIndex());
349 // Tracer.trace("Ctx: Arming request - done");
353 @SuppressWarnings("unchecked")
355 public T
getNext(ITmfContext context
) {
356 if (context
instanceof TmfExperimentContext
) {
357 return (T
) getNextEvent((TmfExperimentContext
) context
);
362 // ------------------------------------------------------------------------
363 // ITmfTrace trace positioning
364 // ------------------------------------------------------------------------
366 // Returns a brand new context based on the location provided
367 // and initializes the event queues
369 public synchronized TmfExperimentContext
seekLocation(ITmfLocation
<?
> location
) {
370 // Validate the location
371 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
372 return null; // Throw an exception?
375 if (fTraces
== null) { // experiment has been disposed
379 // Instantiate the location
380 TmfExperimentLocation expLocation
= (location
== null) ?
new TmfExperimentLocation(new TmfLocationArray(
381 new ITmfLocation
<?
>[fTraces
.length
]), new long[fTraces
.length
]) : (TmfExperimentLocation
) location
.clone();
383 // Create and populate the context's traces contexts
384 TmfExperimentContext context
= new TmfExperimentContext(fTraces
, new TmfContext
[fTraces
.length
]);
385 // Tracer.trace("Ctx: SeekLocation - start");
388 for (int i
= 0; i
< fTraces
.length
; i
++) {
389 // Get the relevant trace attributes
390 ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().locations
[i
];
391 long traceRank
= expLocation
.getRanks()[i
];
393 // Set the corresponding sub-context
394 context
.getContexts()[i
] = fTraces
[i
].seekLocation(traceLocation
);
395 context
.getContexts()[i
].setRank(traceRank
);
398 // Set the trace location and read the corresponding event
399 expLocation
.getLocation().locations
[i
] = context
.getContexts()[i
].getLocation().clone();
400 context
.getEvents()[i
] = fTraces
[i
].getNextEvent(context
.getContexts()[i
]);
403 // Tracer.trace("Ctx: SeekLocation - done");
406 context
.setLocation(expLocation
);
407 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
408 context
.setRank(rank
);
410 fExperimentContext
= context
;
418 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
421 public synchronized TmfExperimentContext
seekEvent(ITmfTimestamp timestamp
) {
423 // Tracer.trace("Ctx: seekEvent(TS) - start");
425 if (timestamp
== null) {
426 timestamp
= TmfTimestamp
.BigBang
;
429 // First, find the right checkpoint
430 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
432 // In the very likely case that the checkpoint was not found, bsearch
433 // returns its negated would-be location (not an offset...). From that
434 // index, we can then position the stream and get the event.
436 index
= Math
.max(0, -(index
+ 2));
439 // Position the experiment at the checkpoint
440 ITmfLocation
<?
> location
;
441 synchronized (fCheckpoints
) {
442 if (fCheckpoints
.size() > 0) {
443 if (index
>= fCheckpoints
.size()) {
444 index
= fCheckpoints
.size() - 1;
446 location
= fCheckpoints
.elementAt(index
).getLocation();
452 TmfExperimentContext context
= seekLocation(location
);
453 context
.setRank((long) index
* fIndexPageSize
);
455 // And locate the event
456 ITmfEvent event
= parseEvent(context
);
457 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
458 getNextEvent(context
);
459 event
= parseEvent(context
);
463 context
.setLocation(null);
464 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
473 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
476 public synchronized TmfExperimentContext
seekEvent(long rank
) {
478 // Tracer.trace("Ctx: seekEvent(rank) - start");
480 // Position the stream at the previous checkpoint
481 int index
= (int) rank
/ fIndexPageSize
;
482 ITmfLocation
<?
> location
;
483 synchronized (fCheckpoints
) {
484 if (fCheckpoints
.size() == 0) {
487 if (index
>= fCheckpoints
.size()) {
488 index
= fCheckpoints
.size() - 1;
490 location
= fCheckpoints
.elementAt(index
).getLocation();
494 TmfExperimentContext context
= seekLocation(location
);
495 context
.setRank((long) index
* fIndexPageSize
);
497 // And locate the event
498 ITmfEvent event
= parseEvent(context
);
499 long pos
= context
.getRank();
500 while (event
!= null && pos
++ < rank
) {
501 getNextEvent(context
);
502 event
= parseEvent(context
);
506 context
.setLocation(null);
507 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
514 public TmfContext
seekLocation(double ratio
) {
515 TmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
520 public double getLocationRatio(ITmfLocation
<?
> location
) {
521 if (location
instanceof TmfExperimentLocation
) {
522 return (double) seekLocation(location
).getRank() / getNbEvents();
528 public ITmfLocation
<?
> getCurrentLocation() {
529 if (fExperimentContext
!= null) {
530 return fExperimentContext
.getLocation();
536 * Scan the next events from all traces and return the next one in chronological order.
542 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
544 // TmfContext context0 = context.getContexts()[0];
545 // TmfEvent event0 = context.getEvents()[0];
546 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
547 // long rank0 = context.getRank();
548 // int trace = context.getLastTrace();
550 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
552 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
553 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
554 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
555 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
556 // Tracer.trace(result.toString());
560 public synchronized ITmfEvent
getNextEvent(TmfContext context
) {
562 // Validate the context
563 if (!(context
instanceof TmfExperimentContext
)) {
564 return null; // Throw an exception?
567 if (!context
.equals(fExperimentContext
)) {
568 // Tracer.trace("Ctx: Restoring context");
569 fExperimentContext
= seekLocation(context
.getLocation());
572 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
574 // dumpContext(expContext, true);
576 // If an event was consumed previously, get the next one from that trace
577 int lastTrace
= expContext
.getLastTrace();
578 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
579 TmfContext traceContext
= expContext
.getContexts()[lastTrace
];
580 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
581 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
584 // Scan the candidate events and identify the "next" trace to read from
585 ITmfEvent eventArray
[] = expContext
.getEvents();
586 if (eventArray
== null) {
589 int trace
= TmfExperimentContext
.NO_TRACE
;
590 ITmfTimestamp timestamp
= TmfTimestamp
.BigCrunch
;
591 if (eventArray
.length
== 1) {
592 if (eventArray
[0] != null) {
593 timestamp
= eventArray
[0].getTimestamp();
597 for (int i
= 0; i
< eventArray
.length
; i
++) {
598 ITmfEvent event
= eventArray
[i
];
599 if (event
!= null && event
.getTimestamp() != null) {
600 ITmfTimestamp otherTS
= event
.getTimestamp();
601 if (otherTS
.compareTo(timestamp
, true) < 0) {
608 // Update the experiment context and set the "next" event
609 ITmfEvent event
= null;
610 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
611 updateIndex(expContext
, timestamp
);
613 TmfContext traceContext
= expContext
.getContexts()[trace
];
614 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
615 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
616 expLocation
.getLocation().locations
[trace
] = traceContext
.getLocation().clone();
618 // updateIndex(expContext, timestamp);
620 expLocation
.getRanks()[trace
] = traceContext
.getRank();
621 expContext
.setLastTrace(trace
);
622 expContext
.updateRank(1);
623 event
= expContext
.getEvents()[trace
];
624 fExperimentContext
= expContext
;
627 // if (event != null) {
628 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
629 // dumpContext(expContext, false);
630 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
636 public synchronized void updateIndex(ITmfContext context
, ITmfTimestamp timestamp
) {
637 // Build the index as we go along
638 long rank
= context
.getRank();
639 if (context
.isValidRank() && (rank
% fIndexPageSize
) == 0) {
640 // Determine the table position
641 long position
= rank
/ fIndexPageSize
;
642 // Add new entry at proper location (if empty)
643 if (fCheckpoints
.size() == position
) {
644 ITmfLocation
<?
> location
= context
.getLocation().clone();
645 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
646 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
647 // + location.toString());
655 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
658 public ITmfEvent
parseEvent(TmfContext context
) {
660 // Validate the context
661 if (!(context
instanceof TmfExperimentContext
)) {
662 return null; // Throw an exception?
665 if (!context
.equals(fExperimentContext
)) {
666 // Tracer.trace("Ctx: Restoring context");
667 seekLocation(context
.getLocation());
670 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
672 // If an event was consumed previously, get the next one from that trace
673 int lastTrace
= expContext
.getLastTrace();
674 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
675 TmfContext traceContext
= expContext
.getContexts()[lastTrace
];
676 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
677 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
678 fExperimentContext
= (TmfExperimentContext
) context
;
681 // Scan the candidate events and identify the "next" trace to read from
682 int trace
= TmfExperimentContext
.NO_TRACE
;
683 ITmfTimestamp timestamp
= TmfTimestamp
.BigCrunch
;
684 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
685 ITmfEvent event
= expContext
.getEvents()[i
];
686 if (event
!= null && event
.getTimestamp() != null) {
687 ITmfTimestamp otherTS
= event
.getTimestamp();
688 if (otherTS
.compareTo(timestamp
, true) < 0) {
695 ITmfEvent event
= null;
696 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
697 event
= expContext
.getEvents()[trace
];
706 * @see java.lang.Object#toString()
709 @SuppressWarnings("nls")
710 public String
toString() {
711 return "[TmfExperiment (" + getName() + ")]";
714 // ------------------------------------------------------------------------
716 // ------------------------------------------------------------------------
718 private synchronized void initializeStreamingMonitor() {
724 if (getStreamingInterval() == 0) {
725 TmfContext context
= seekLocation(null);
726 ITmfEvent event
= getNext(context
);
730 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp(), TmfTimestamp
.BigCrunch
);
731 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
733 // Broadcast in separate thread to prevent deadlock
743 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
744 ITmfTimestamp safeTimestamp
= null;
745 TmfTimeRange timeRange
= null;
749 while (!fExecutor
.isShutdown()) {
750 if (!isIndexingBusy()) {
751 ITmfTimestamp startTimestamp
= TmfTimestamp
.BigCrunch
;
752 ITmfTimestamp endTimestamp
= TmfTimestamp
.BigBang
;
753 for (ITmfTrace
<T
> trace
: fTraces
) {
754 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
755 startTimestamp
= trace
.getStartTime();
757 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
758 endTimestamp
= trace
.getEndTime();
761 if (safeTimestamp
!= null && safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0) {
762 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
766 safeTimestamp
= endTimestamp
;
767 if (timeRange
!= null) {
768 TmfExperimentRangeUpdatedSignal signal
=
769 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
774 Thread
.sleep(getStreamingInterval());
775 } catch (InterruptedException e
) {
785 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
788 public long getStreamingInterval() {
790 for (ITmfTrace
<T
> trace
: fTraces
) {
791 interval
= Math
.max(interval
, trace
.getStreamingInterval());
797 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
798 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
800 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
801 * intervals (every INDEX_PAGE_SIZE event).
804 // The index page size
805 private static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
806 protected int fIndexPageSize
;
807 protected boolean fIndexing
= false;
808 protected TmfTimeRange fIndexingPendingRange
= TmfTimeRange
.Null
;
810 private Integer fEndSynchReference
;
812 // private static BufferedWriter fEventLog = null;
813 // private static BufferedWriter openLogFile(String filename) {
814 // BufferedWriter outfile = null;
816 // outfile = new BufferedWriter(new FileWriter(filename));
817 // } catch (IOException e) {
818 // e.printStackTrace();
823 protected boolean isIndexingBusy() {
824 synchronized (fCheckpoints
) {
829 protected void indexExperiment(boolean waitForCompletion
) {
830 indexExperiment(waitForCompletion
, 0, TmfTimeRange
.Eternity
);
833 @SuppressWarnings("unchecked")
834 protected void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
836 synchronized (fCheckpoints
) {
843 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
845 protected IStatus
run(IProgressMonitor monitor
) {
846 while (!monitor
.isCanceled()) {
849 } catch (InterruptedException e
) {
850 return Status
.OK_STATUS
;
854 return Status
.OK_STATUS
;
859 // fEventLog = openLogFile("TraceEvent.log");
860 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
862 ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
863 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
865 // long indexingStart = System.nanoTime();
867 ITmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.Null
) ?
null : fTimeRange
.getStartTime();
868 ITmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.Null
) ?
null : fTimeRange
.getEndTime();
869 long initialNbEvents
= fNbEvents
;
872 public void handleStarted() {
873 super.handleStarted();
877 public void handleData(ITmfEvent event
) {
878 super.handleData(event
);
880 ITmfTimestamp ts
= event
.getTimestamp();
881 if (startTime
== null)
882 startTime
= ts
.clone();
883 lastTime
= ts
.clone();
884 if ((getNbRead() % fIndexPageSize
) == 1 && getNbRead() != 1) {
891 public void handleSuccess() {
892 // long indexingEnd = System.nanoTime();
894 if (getRange() != TmfTimeRange
.Eternity
) {
895 lastTime
= getRange().getEndTime();
898 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
900 // long average = (indexingEnd - indexingStart) / fNbEvents;
901 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
902 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
903 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
904 // + (average % 1000) + " us/evt)");
905 super.handleSuccess();
909 public void handleCompleted() {
911 super.handleCompleted();
912 synchronized (fCheckpoints
) {
914 if (fIndexingPendingRange
!= TmfTimeRange
.Null
) {
915 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
916 fIndexingPendingRange
= TmfTimeRange
.Null
;
921 private void updateExperiment() {
922 int nbRead
= getNbRead();
923 if (startTime
!= null) {
924 fTimeRange
= new TmfTimeRange(startTime
, lastTime
.clone());
927 // updateTimeRange();
929 fNbEvents
= initialNbEvents
+ nbRead
;
935 sendRequest((ITmfDataRequest
<T
>) request
);
936 if (waitForCompletion
)
938 request
.waitForCompletion();
939 } catch (InterruptedException e
) {
944 protected void notifyListeners() {
945 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
946 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
949 // ------------------------------------------------------------------------
951 // ------------------------------------------------------------------------
954 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
955 TmfExperiment
<?
> experiment
= signal
.getExperiment();
956 if (experiment
== this) {
957 setCurrentExperiment(experiment
);
958 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
963 public void endSync(TmfEndSynchSignal signal
) {
964 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
965 fEndSynchReference
= null;
966 initializeStreamingMonitor();
972 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
976 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
977 if (signal
.getExperiment() == this) {
978 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
983 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
984 for (ITmfTrace
<T
> trace
: fTraces
) {
985 if (trace
== signal
.getTrace()) {
986 synchronized (fCheckpoints
) {
988 if (fIndexingPendingRange
== TmfTimeRange
.Null
) {
989 fIndexingPendingRange
= signal
.getRange();
991 ITmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
992 ITmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
993 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
994 startTime
= signal
.getRange().getStartTime();
996 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
997 endTime
= signal
.getRange().getEndTime();
999 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
1004 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
1011 public String
getPath() {
1012 // TODO Auto-generated method stub
1017 * Set the resource to be used for bookmarks on this experiment
1018 * @param resource the bookmarks resource
1020 public void setResource(IResource resource
) {
1021 fResource
= resource
;
1025 * Get the resource used for bookmarks on this experiment
1026 * @return the bookmarks resource or null if none is set
1028 public IResource
getResource() {