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
.IFile
;
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
.ITmfEvent
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
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
;
49 * <b><u>TmfExperiment</u></b>
51 * TmfExperiment presents a time-ordered, unified view of a set of TmfTraces that are part of a tracing experiment.
54 public class TmfExperiment
<T
extends ITmfEvent
> extends TmfEventProvider
<T
> implements ITmfTrace
<T
> {
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 // The currently selected experiment
61 protected static TmfExperiment
<?
> fCurrentExperiment
= null;
63 // The set of traces that constitute the experiment
64 protected ITmfTrace
<T
>[] fTraces
;
66 // The total number of events
67 protected long fNbEvents
;
69 // The experiment time range
70 protected TmfTimeRange fTimeRange
;
72 // The experiment reference timestamp (default: Zero)
73 protected ITmfTimestamp fEpoch
;
75 // The experiment index
76 protected Vector
<TmfCheckpoint
> fCheckpoints
= new Vector
<TmfCheckpoint
>();
78 // The current experiment context
79 protected TmfExperimentContext fExperimentContext
;
81 // Flag to initialize only once
82 private boolean fInitialized
= false;
84 // The experiment bookmarks file
85 private IFile fBookmarksFile
;
87 // The properties resource
88 private IResource fResource
;
90 // ------------------------------------------------------------------------
92 // ------------------------------------------------------------------------
95 public boolean validate(IProject project
, String path
) {
100 public void initTrace(String name
, String path
, Class
<T
> eventType
) {
104 public void initTrace(String name
, String path
, Class
<T
> eventType
, boolean indexTrace
) {
106 initializeStreamingMonitor();
111 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
) {
115 public void initTrace(String name
, String path
, Class
<T
> eventType
, int cacheSize
, boolean indexTrace
) {
117 initializeStreamingMonitor();
126 * @param indexPageSize
128 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
) {
129 this(type
, id
, traces
, TmfTimestamp
.ZERO
, indexPageSize
, false);
132 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, ITmfTimestamp epoch
, int indexPageSize
, boolean preIndexExperiment
) {
137 fIndexPageSize
= indexPageSize
;
138 fTimeRange
= TmfTimeRange
.NULL_RANGE
;
140 indexExperiment(true);
144 protected TmfExperiment(String id
, Class
<T
> type
) {
153 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
) {
154 this(type
, id
, traces
, TmfTimestamp
.ZERO
, DEFAULT_INDEX_PAGE_SIZE
);
161 * @param indexPageSize
163 public TmfExperiment(Class
<T
> type
, String id
, ITmfTrace
<T
>[] traces
, int indexPageSize
) {
164 this(type
, id
, traces
, TmfTimestamp
.ZERO
, indexPageSize
);
172 @SuppressWarnings("unchecked")
173 public TmfExperiment(TmfExperiment
<T
> other
) {
174 super(other
.getName() + "(copy)", other
.fType
); //$NON-NLS-1$
176 fEpoch
= other
.fEpoch
;
177 fIndexPageSize
= other
.fIndexPageSize
;
179 fTraces
= new ITmfTrace
[other
.fTraces
.length
];
180 for (int trace
= 0; trace
< other
.fTraces
.length
; trace
++) {
181 fTraces
[trace
] = other
.fTraces
[trace
].copy();
184 fNbEvents
= other
.fNbEvents
;
185 fTimeRange
= other
.fTimeRange
;
189 public TmfExperiment
<T
> copy() {
190 TmfExperiment
<T
> experiment
= new TmfExperiment
<T
>(this);
191 TmfSignalManager
.deregister(experiment
);
196 * Clears the experiment
199 @SuppressWarnings("rawtypes")
200 public synchronized void dispose() {
202 TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
204 if (fCurrentExperiment
== this) {
205 fCurrentExperiment
= null;
208 if (fTraces
!= null) {
209 for (ITmfTrace trace
: fTraces
) {
214 if (fCheckpoints
!= null) {
215 fCheckpoints
.clear();
220 // ------------------------------------------------------------------------
222 // ------------------------------------------------------------------------
225 public long getNbEvents() {
230 public int getCacheSize() {
231 return fIndexPageSize
;
235 public TmfTimeRange
getTimeRange() {
240 public ITmfTimestamp
getStartTime() {
241 return fTimeRange
.getStartTime();
245 public ITmfTimestamp
getEndTime() {
246 return fTimeRange
.getEndTime();
249 public Vector
<TmfCheckpoint
> getCheckpoints() {
253 // ------------------------------------------------------------------------
255 // ------------------------------------------------------------------------
257 public static void setCurrentExperiment(TmfExperiment
<?
> experiment
) {
258 if (fCurrentExperiment
!= null && fCurrentExperiment
!= experiment
) {
259 fCurrentExperiment
.dispose();
261 fCurrentExperiment
= experiment
;
264 public static TmfExperiment
<?
> getCurrentExperiment() {
265 return fCurrentExperiment
;
268 public ITmfTimestamp
getEpoch() {
272 public ITmfTrace
<T
>[] getTraces() {
277 * Returns the rank of the first event with the requested timestamp. If none, returns the index of the next event
280 * @param timestamp the event timestamp
281 * @return the corresponding event rank
284 public long getRank(ITmfTimestamp timestamp
) {
285 TmfExperimentContext context
= seekEvent(timestamp
);
286 return context
.getRank();
290 * Returns the timestamp of the event at the requested index. If none, returns null.
292 * @param index the event index (rank)
293 * @return the corresponding event timestamp
295 public ITmfTimestamp
getTimestamp(int index
) {
296 TmfExperimentContext context
= seekEvent(index
);
297 ITmfEvent event
= getNextEvent(context
);
298 return (event
!= null) ? event
.getTimestamp() : null;
301 // ------------------------------------------------------------------------
303 // ------------------------------------------------------------------------
306 * Update the global time range
308 protected void updateTimeRange() {
309 ITmfTimestamp startTime
= fTimeRange
!= TmfTimeRange
.NULL_RANGE ? fTimeRange
.getStartTime() : TmfTimestamp
.BIG_CRUNCH
;
310 ITmfTimestamp endTime
= fTimeRange
!= TmfTimeRange
.NULL_RANGE ? fTimeRange
.getEndTime() : TmfTimestamp
.BIG_BANG
;
312 for (ITmfTrace
<T
> trace
: fTraces
) {
313 ITmfTimestamp traceStartTime
= trace
.getStartTime();
314 if (traceStartTime
.compareTo(startTime
, true) < 0)
315 startTime
= traceStartTime
;
316 ITmfTimestamp traceEndTime
= trace
.getEndTime();
317 if (traceEndTime
.compareTo(endTime
, true) > 0)
318 endTime
= traceEndTime
;
320 fTimeRange
= new TmfTimeRange(startTime
, endTime
);
323 // ------------------------------------------------------------------------
325 // ------------------------------------------------------------------------
327 public ITmfContext
armRequest(ITmfDataRequest
<T
> request
) {
328 // Tracer.trace("Ctx: Arming request - start");
329 ITmfTimestamp timestamp
= (request
instanceof ITmfEventRequest
<?
>) ?
((ITmfEventRequest
<T
>) request
).getRange().getStartTime()
332 if (TmfTimestamp
.BIG_BANG
.equals(timestamp
) || request
.getIndex() > 0) {
333 timestamp
= null; // use request index
336 TmfExperimentContext context
= null;
337 if (timestamp
!= null) {
339 context
= seekEvent(timestamp
);
340 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
343 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
344 // We are already at the right context -> no need to seek
345 context
= fExperimentContext
;
347 context
= seekEvent(request
.getIndex());
350 // Tracer.trace("Ctx: Arming request - done");
354 @SuppressWarnings("unchecked")
356 public T
getNext(ITmfContext context
) {
357 if (context
instanceof TmfExperimentContext
) {
358 return (T
) getNextEvent((TmfExperimentContext
) context
);
363 // ------------------------------------------------------------------------
364 // ITmfTrace trace positioning
365 // ------------------------------------------------------------------------
367 // Returns a brand new context based on the location provided
368 // and initializes the event queues
370 public synchronized TmfExperimentContext
seekLocation(ITmfLocation
<?
> location
) {
371 // Validate the location
372 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
373 return null; // Throw an exception?
376 if (fTraces
== null) { // experiment has been disposed
380 // Instantiate the location
381 TmfExperimentLocation expLocation
= (location
== null) ?
new TmfExperimentLocation(new TmfLocationArray(
382 new ITmfLocation
<?
>[fTraces
.length
]), new long[fTraces
.length
]) : (TmfExperimentLocation
) location
.clone();
384 // Create and populate the context's traces contexts
385 TmfExperimentContext context
= new TmfExperimentContext(fTraces
, new TmfContext
[fTraces
.length
]);
386 // Tracer.trace("Ctx: SeekLocation - start");
389 for (int i
= 0; i
< fTraces
.length
; i
++) {
390 // Get the relevant trace attributes
391 ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().locations
[i
];
392 long traceRank
= expLocation
.getRanks()[i
];
394 // Set the corresponding sub-context
395 context
.getContexts()[i
] = fTraces
[i
].seekLocation(traceLocation
);
396 context
.getContexts()[i
].setRank(traceRank
);
399 // Set the trace location and read the corresponding event
400 /* The (TmfContext) cast should be safe since we created 'context'
401 * ourselves higher up. */
402 expLocation
.getLocation().locations
[i
] = ((TmfContext
) context
.getContexts()[i
]).getLocation().clone();
403 context
.getEvents()[i
] = fTraces
[i
].getNextEvent(context
.getContexts()[i
]);
406 // Tracer.trace("Ctx: SeekLocation - done");
409 context
.setLocation(expLocation
);
410 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
411 context
.setRank(rank
);
413 fExperimentContext
= context
;
421 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools .tmf.event.TmfTimestamp)
424 public synchronized TmfExperimentContext
seekEvent(ITmfTimestamp timestamp
) {
426 // Tracer.trace("Ctx: seekEvent(TS) - start");
428 if (timestamp
== null) {
429 timestamp
= TmfTimestamp
.BIG_BANG
;
432 // First, find the right checkpoint
433 int index
= Collections
.binarySearch(fCheckpoints
, new TmfCheckpoint(timestamp
, null));
435 // In the very likely case that the checkpoint was not found, bsearch
436 // returns its negated would-be location (not an offset...). From that
437 // index, we can then position the stream and get the event.
439 index
= Math
.max(0, -(index
+ 2));
442 // Position the experiment at the checkpoint
443 ITmfLocation
<?
> location
;
444 synchronized (fCheckpoints
) {
445 if (fCheckpoints
.size() > 0) {
446 if (index
>= fCheckpoints
.size()) {
447 index
= fCheckpoints
.size() - 1;
449 location
= fCheckpoints
.elementAt(index
).getLocation();
455 TmfExperimentContext context
= seekLocation(location
);
456 context
.setRank((long) index
* fIndexPageSize
);
458 // And locate the event
459 ITmfEvent event
= parseEvent(context
);
460 while (event
!= null && event
.getTimestamp().compareTo(timestamp
, false) < 0) {
461 getNextEvent(context
);
462 event
= parseEvent(context
);
466 context
.setLocation(null);
467 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
476 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#seekEvent(long)
479 public synchronized TmfExperimentContext
seekEvent(long rank
) {
481 // Tracer.trace("Ctx: seekEvent(rank) - start");
483 // Position the stream at the previous checkpoint
484 int index
= (int) rank
/ fIndexPageSize
;
485 ITmfLocation
<?
> location
;
486 synchronized (fCheckpoints
) {
487 if (fCheckpoints
.size() == 0) {
490 if (index
>= fCheckpoints
.size()) {
491 index
= fCheckpoints
.size() - 1;
493 location
= fCheckpoints
.elementAt(index
).getLocation();
497 TmfExperimentContext context
= seekLocation(location
);
498 context
.setRank((long) index
* fIndexPageSize
);
500 // And locate the event
501 ITmfEvent event
= parseEvent(context
);
502 long pos
= context
.getRank();
503 while (event
!= null && pos
++ < rank
) {
504 getNextEvent(context
);
505 event
= parseEvent(context
);
509 context
.setLocation(null);
510 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
517 public TmfContext
seekLocation(double ratio
) {
518 TmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
523 public double getLocationRatio(ITmfLocation
<?
> location
) {
524 if (location
instanceof TmfExperimentLocation
) {
525 return (double) seekLocation(location
).getRank() / getNbEvents();
531 public ITmfLocation
<?
> getCurrentLocation() {
532 if (fExperimentContext
!= null) {
533 return fExperimentContext
.getLocation();
538 // private void dumpContext(TmfExperimentContext context, boolean isBefore) {
540 // TmfContext context0 = context.getContexts()[0];
541 // TmfEvent event0 = context.getEvents()[0];
542 // TmfExperimentLocation location0 = (TmfExperimentLocation) context.getLocation();
543 // long rank0 = context.getRank();
544 // int trace = context.getLastTrace();
546 // StringBuffer result = new StringBuffer("Ctx: " + (isBefore ? "B " : "A "));
548 // result.append("[Ctx: fLoc= " + context0.getLocation().toString() + ", fRnk= " + context0.getRank() + "] ");
549 // result.append("[Evt: " + event0.getTimestamp().toString() + "] ");
550 // result.append("[Loc: fLoc= " + location0.getLocation()[0].toString() + ", fRnk= " + location0.getRanks()[0] + "] ");
551 // result.append("[Rnk: " + rank0 + "], [Trc: " + trace + "]");
552 // Tracer.trace(result.toString());
556 * Scan the next events from all traces and return the next one in chronological order.
558 * @param context the trace context
559 * @return the next event
561 @SuppressWarnings("unchecked")
563 public synchronized ITmfEvent
getNextEvent(ITmfContext context
) {
565 // Validate the context
566 if (!(context
instanceof TmfExperimentContext
)) {
567 return null; // Throw an exception?
570 if (!context
.equals(fExperimentContext
)) {
571 // Tracer.trace("Ctx: Restoring context");
572 fExperimentContext
= seekLocation(context
.getLocation());
575 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
577 // dumpContext(expContext, true);
579 // If an event was consumed previously, get the next one from that trace
580 int lastTrace
= expContext
.getLastTrace();
581 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
582 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
583 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
584 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
587 // Scan the candidate events and identify the "next" trace to read from
588 ITmfEvent eventArray
[] = expContext
.getEvents();
589 if (eventArray
== null) {
592 int trace
= TmfExperimentContext
.NO_TRACE
;
593 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
594 if (eventArray
.length
== 1) {
595 if (eventArray
[0] != null) {
596 timestamp
= eventArray
[0].getTimestamp();
600 for (int i
= 0; i
< eventArray
.length
; i
++) {
601 ITmfEvent event
= eventArray
[i
];
602 if (event
!= null && event
.getTimestamp() != null) {
603 ITmfTimestamp otherTS
= event
.getTimestamp();
604 if (otherTS
.compareTo(timestamp
, true) < 0) {
611 // Update the experiment context and set the "next" event
612 ITmfEvent event
= null;
613 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
614 updateIndex(expContext
, timestamp
);
616 ITmfContext traceContext
= expContext
.getContexts()[trace
];
617 TmfExperimentLocation expLocation
= (TmfExperimentLocation
) expContext
.getLocation();
618 // expLocation.getLocation()[trace] = traceContext.getLocation().clone();
619 expLocation
.getLocation().locations
[trace
] = (ITmfLocation
<?
extends Comparable
<?
>>) traceContext
.getLocation().clone();
621 // updateIndex(expContext, timestamp);
623 expLocation
.getRanks()[trace
] = traceContext
.getRank();
624 expContext
.setLastTrace(trace
);
625 expContext
.updateRank(1);
626 event
= expContext
.getEvents()[trace
];
627 fExperimentContext
= expContext
;
630 // if (event != null) {
631 // Tracer.trace("Exp: " + (expContext.getRank() - 1) + ": " + event.getTimestamp().toString());
632 // dumpContext(expContext, false);
633 // Tracer.trace("Ctx: Event returned= " + event.getTimestamp().toString());
639 public synchronized void updateIndex(ITmfContext context
, ITmfTimestamp timestamp
) {
640 // Build the index as we go along
641 long rank
= context
.getRank();
642 if (context
.isValidRank() && (rank
% fIndexPageSize
) == 0) {
643 // Determine the table position
644 long position
= rank
/ fIndexPageSize
;
645 // Add new entry at proper location (if empty)
646 if (fCheckpoints
.size() == position
) {
647 ITmfLocation
<?
> location
= context
.getLocation().clone();
648 fCheckpoints
.add(new TmfCheckpoint(timestamp
.clone(), location
));
649 // System.out.println(this + "[" + (fCheckpoints.size() - 1) + "] " + timestamp + ", "
650 // + location.toString());
658 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#parseEvent(org.eclipse.linuxtools .tmf.trace.TmfContext)
661 public ITmfEvent
parseEvent(ITmfContext context
) {
663 // Validate the context
664 if (!(context
instanceof TmfExperimentContext
)) {
665 return null; // Throw an exception?
668 if (!context
.equals(fExperimentContext
)) {
669 // Tracer.trace("Ctx: Restoring context");
670 seekLocation(context
.getLocation());
673 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
675 // If an event was consumed previously, get the next one from that trace
676 int lastTrace
= expContext
.getLastTrace();
677 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
678 ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
679 expContext
.getEvents()[lastTrace
] = expContext
.getTraces()[lastTrace
].getNextEvent(traceContext
);
680 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
681 fExperimentContext
= (TmfExperimentContext
) context
;
684 // Scan the candidate events and identify the "next" trace to read from
685 int trace
= TmfExperimentContext
.NO_TRACE
;
686 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
687 for (int i
= 0; i
< expContext
.getTraces().length
; i
++) {
688 ITmfEvent event
= expContext
.getEvents()[i
];
689 if (event
!= null && event
.getTimestamp() != null) {
690 ITmfTimestamp otherTS
= event
.getTimestamp();
691 if (otherTS
.compareTo(timestamp
, true) < 0) {
698 ITmfEvent event
= null;
699 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
700 event
= expContext
.getEvents()[trace
];
709 * @see java.lang.Object#toString()
712 @SuppressWarnings("nls")
713 public String
toString() {
714 return "[TmfExperiment (" + getName() + ")]";
717 // ------------------------------------------------------------------------
719 // ------------------------------------------------------------------------
721 private synchronized void initializeStreamingMonitor() {
727 if (getStreamingInterval() == 0) {
728 TmfContext context
= seekLocation(null);
729 ITmfEvent event
= getNext(context
);
733 TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
734 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
736 // Broadcast in separate thread to prevent deadlock
746 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
747 ITmfTimestamp safeTimestamp
= null;
748 TmfTimeRange timeRange
= null;
752 while (!fExecutor
.isShutdown()) {
753 if (!isIndexingBusy()) {
754 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
755 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
756 for (ITmfTrace
<T
> trace
: fTraces
) {
757 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
758 startTimestamp
= trace
.getStartTime();
760 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
761 endTimestamp
= trace
.getEndTime();
764 if (safeTimestamp
!= null && safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0) {
765 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
769 safeTimestamp
= endTimestamp
;
770 if (timeRange
!= null) {
771 TmfExperimentRangeUpdatedSignal signal
=
772 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
777 Thread
.sleep(getStreamingInterval());
778 } catch (InterruptedException e
) {
788 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
791 public long getStreamingInterval() {
793 for (ITmfTrace
<T
> trace
: fTraces
) {
794 interval
= Math
.max(interval
, trace
.getStreamingInterval());
800 * The experiment holds the globally ordered events of its set of traces. It is expected to provide access to each
801 * individual event by index i.e. it must be possible to request the Nth event of the experiment.
803 * The purpose of the index is to keep the information needed to rapidly restore the traces contexts at regular
804 * intervals (every INDEX_PAGE_SIZE event).
807 // The index page size
808 private static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
809 protected int fIndexPageSize
;
810 protected boolean fIndexing
= false;
811 protected TmfTimeRange fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
813 private Integer fEndSynchReference
;
815 // private static BufferedWriter fEventLog = null;
816 // private static BufferedWriter openLogFile(String filename) {
817 // BufferedWriter outfile = null;
819 // outfile = new BufferedWriter(new FileWriter(filename));
820 // } catch (IOException e) {
821 // e.printStackTrace();
826 protected boolean isIndexingBusy() {
827 synchronized (fCheckpoints
) {
832 protected void indexExperiment(boolean waitForCompletion
) {
833 indexExperiment(waitForCompletion
, 0, TmfTimeRange
.ETERNITY
);
836 @SuppressWarnings("unchecked")
837 protected void indexExperiment(boolean waitForCompletion
, final int index
, final TmfTimeRange timeRange
) {
839 synchronized (fCheckpoints
) {
846 final Job job
= new Job("Indexing " + getName() + "...") { //$NON-NLS-1$ //$NON-NLS-2$
848 protected IStatus
run(IProgressMonitor monitor
) {
849 while (!monitor
.isCanceled()) {
852 } catch (InterruptedException e
) {
853 return Status
.OK_STATUS
;
857 return Status
.OK_STATUS
;
862 // fEventLog = openLogFile("TraceEvent.log");
863 // System.out.println(System.currentTimeMillis() + ": Experiment indexing started");
865 ITmfEventRequest
<ITmfEvent
> request
= new TmfEventRequest
<ITmfEvent
>(ITmfEvent
.class, timeRange
, index
, TmfDataRequest
.ALL_DATA
,
866 fIndexPageSize
, ITmfDataRequest
.ExecutionType
.BACKGROUND
) { // PATA FOREGROUND
868 // long indexingStart = System.nanoTime();
870 ITmfTimestamp startTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getStartTime();
871 ITmfTimestamp lastTime
= (fTimeRange
== TmfTimeRange
.NULL_RANGE
) ?
null : fTimeRange
.getEndTime();
872 long initialNbEvents
= fNbEvents
;
875 public void handleStarted() {
876 super.handleStarted();
880 public void handleData(ITmfEvent event
) {
881 super.handleData(event
);
883 ITmfTimestamp ts
= event
.getTimestamp();
884 if (startTime
== null)
885 startTime
= ts
.clone();
886 lastTime
= ts
.clone();
887 if ((getNbRead() % fIndexPageSize
) == 1 && getNbRead() != 1) {
894 public void handleSuccess() {
895 // long indexingEnd = System.nanoTime();
897 // if the end time is a real value then it is the streaming safe time stamp
898 // set the last time to the safe time stamp to prevent unnecessary indexing requests
899 if (getRange().getEndTime() != TmfTimestamp
.BIG_CRUNCH
) {
900 lastTime
= getRange().getEndTime();
903 // System.out.println(System.currentTimeMillis() + ": Experiment indexing completed");
905 // long average = (indexingEnd - indexingStart) / fNbEvents;
906 // System.out.println(getName() + ": start=" + startTime + ", end=" + lastTime + ", elapsed="
907 // + (indexingEnd * 1.0 - indexingStart) / 1000000000);
908 // System.out.println(getName() + ": nbEvents=" + fNbEvents + " (" + (average / 1000) + "."
909 // + (average % 1000) + " us/evt)");
910 super.handleSuccess();
914 public void handleCompleted() {
916 super.handleCompleted();
917 synchronized (fCheckpoints
) {
919 if (fIndexingPendingRange
!= TmfTimeRange
.NULL_RANGE
) {
920 indexExperiment(false, (int) fNbEvents
, fIndexingPendingRange
);
921 fIndexingPendingRange
= TmfTimeRange
.NULL_RANGE
;
926 private void updateExperiment() {
927 int nbRead
= getNbRead();
928 if (startTime
!= null) {
929 fTimeRange
= new TmfTimeRange(startTime
, lastTime
.clone());
932 // updateTimeRange();
934 fNbEvents
= initialNbEvents
+ nbRead
;
940 sendRequest((ITmfDataRequest
<T
>) request
);
941 if (waitForCompletion
)
943 request
.waitForCompletion();
944 } catch (InterruptedException e
) {
949 protected void notifyListeners() {
950 broadcast(new TmfExperimentUpdatedSignal(this, this)); // , null));
951 //broadcast(new TmfExperimentRangeUpdatedSignal(this, this, fTimeRange)); // , null));
954 // ------------------------------------------------------------------------
956 // ------------------------------------------------------------------------
959 public void experimentSelected(TmfExperimentSelectedSignal
<T
> signal
) {
960 TmfExperiment
<?
> experiment
= signal
.getExperiment();
961 if (experiment
== this) {
962 setCurrentExperiment(experiment
);
963 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
968 public void endSync(TmfEndSynchSignal signal
) {
969 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
970 fEndSynchReference
= null;
971 initializeStreamingMonitor();
977 public void experimentUpdated(TmfExperimentUpdatedSignal signal
) {
981 public void experimentRangeUpdated(TmfExperimentRangeUpdatedSignal signal
) {
982 if (signal
.getExperiment() == this) {
983 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
988 public void traceUpdated(TmfTraceUpdatedSignal signal
) {
989 for (ITmfTrace
<T
> trace
: fTraces
) {
990 if (trace
== signal
.getTrace()) {
991 synchronized (fCheckpoints
) {
993 if (fIndexingPendingRange
== TmfTimeRange
.NULL_RANGE
) {
994 fIndexingPendingRange
= signal
.getRange();
996 ITmfTimestamp startTime
= fIndexingPendingRange
.getStartTime();
997 ITmfTimestamp endTime
= fIndexingPendingRange
.getEndTime();
998 if (signal
.getRange().getStartTime().compareTo(startTime
) < 0) {
999 startTime
= signal
.getRange().getStartTime();
1001 if (signal
.getRange().getEndTime().compareTo(endTime
) > 0) {
1002 endTime
= signal
.getRange().getEndTime();
1004 fIndexingPendingRange
= new TmfTimeRange(startTime
, endTime
);
1009 indexExperiment(false, (int) fNbEvents
, signal
.getRange());
1016 public String
getPath() {
1017 // TODO Auto-generated method stub
1022 * Set the file to be used for bookmarks on this experiment
1023 * @param file the bookmarks file
1025 public void setBookmarksFile(IFile file
) {
1026 fBookmarksFile
= file
;
1030 * Get the file used for bookmarks on this experiment
1031 * @return the bookmarks file or null if none is set
1033 public IFile
getBookmarksFile() {
1034 return fBookmarksFile
;
1038 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#setResource(org.eclipse.core.resources.IResource)
1041 public void setResource(IResource resource
) {
1042 fResource
= resource
;
1046 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
1049 public IResource
getResource() {