1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2012 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 * Francois Chouinard - Updated as per TMF Trace Model 1.0
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.trace
;
16 import org
.eclipse
.core
.resources
.IFile
;
17 import org
.eclipse
.core
.resources
.IProject
;
18 import org
.eclipse
.core
.resources
.IResource
;
19 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentContext
;
20 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfExperimentLocation
;
21 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.trace
.TmfLocationArray
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfEndSynchSignal
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentDisposedSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentRangeUpdatedSignal
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentUpdatedSignal
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceUpdatedSignal
;
38 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
39 * that are part of a tracing experiment.
42 * @author Francois Chouinard
44 public class TmfExperiment
<T
extends ITmfEvent
> extends TmfTrace
<T
> implements ITmfEventParser
<T
> {
46 // ------------------------------------------------------------------------
48 // ------------------------------------------------------------------------
51 * The default index page size
53 public static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
55 // ------------------------------------------------------------------------
57 // ------------------------------------------------------------------------
60 * The currently selected experiment (null if none)
62 protected static TmfExperiment
<?
> fCurrentExperiment
= null;
65 * The set of traces that constitute the experiment
67 protected ITmfTrace
<T
>[] fTraces
;
70 * The set of traces that constitute the experiment
72 private boolean fInitialized
= false;
75 * The experiment bookmarks file
77 private IFile fBookmarksFile
;
80 // Saved experiment context (optimization)
81 private TmfExperimentContext fExperimentContext
;
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
91 * @throws TmfTraceException
93 public TmfExperiment(final Class
<T
> type
, final String id
, final ITmfTrace
<T
>[] traces
) {
94 this(type
, id
, traces
, DEFAULT_INDEX_PAGE_SIZE
);
101 * @param indexPageSize
102 * @throws TmfTraceException
104 @SuppressWarnings({ "unchecked", "rawtypes" })
105 public TmfExperiment(final Class
<T
> type
, final String path
, final ITmfTrace
<T
>[] traces
, final int indexPageSize
) {
106 setCacheSize(indexPageSize
);
107 setStreamingInterval(0);
108 setIndexer(new TmfCheckpointIndexer(this, indexPageSize
));
111 super.initialize(null, path
, type
);
112 } catch (TmfTraceException e
) {
117 setTimeRange(TmfTimeRange
.NULL_RANGE
);
121 * Clears the experiment
124 @SuppressWarnings("rawtypes")
125 public synchronized void dispose() {
127 final TmfExperimentDisposedSignal
<T
> signal
= new TmfExperimentDisposedSignal
<T
>(this, this);
130 if (fCurrentExperiment
== this) {
131 fCurrentExperiment
= null;
134 // Clean up the index if applicable
135 if (getIndexer() != null) {
136 getIndexer().dispose();
139 if (fTraces
!= null) {
140 for (final ITmfTrace trace
: fTraces
) {
148 // ------------------------------------------------------------------------
149 // ITmfTrace - Initializers
150 // ------------------------------------------------------------------------
153 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
156 public void initTrace(final IResource resource
, final String path
, final Class
<T
> type
) {
160 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
163 public boolean validate(final IProject project
, final String path
) {
167 // ------------------------------------------------------------------------
169 // ------------------------------------------------------------------------
172 * Selects the current, framework-wide, experiment
174 * @param experiment das experiment
176 public static void setCurrentExperiment(final TmfExperiment
<?
> experiment
) {
177 if (fCurrentExperiment
!= null && fCurrentExperiment
!= experiment
) {
178 fCurrentExperiment
.dispose();
180 fCurrentExperiment
= experiment
;
184 * @return das experiment
186 public static TmfExperiment
<?
> getCurrentExperiment() {
187 return fCurrentExperiment
;
191 * Get the list of traces. Handle with care...
193 * @return the experiment traces
195 public ITmfTrace
<T
>[] getTraces() {
200 * Returns the timestamp of the event at the requested index. If none,
203 * @param index the event index (rank)
204 * @return the corresponding event timestamp
206 public ITmfTimestamp
getTimestamp(final int index
) {
207 final ITmfContext context
= seekEvent(index
);
208 final ITmfEvent event
= getNext(context
);
209 return (event
!= null) ? event
.getTimestamp() : null;
213 * Set the file to be used for bookmarks on this experiment
215 * @param file the bookmarks file
217 public void setBookmarksFile(final IFile file
) {
218 fBookmarksFile
= file
;
222 * Get the file used for bookmarks on this experiment
224 * @return the bookmarks file or null if none is set
226 public IFile
getBookmarksFile() {
227 return fBookmarksFile
;
230 // ------------------------------------------------------------------------
231 // Request management
232 // ------------------------------------------------------------------------
235 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
238 protected synchronized ITmfContext
armRequest(final ITmfDataRequest
<T
> request
) {
240 // Make sure we have something to read from
241 if (fTraces
== null) {
245 if (request
instanceof ITmfEventRequest
<?
>
246 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime())
247 && request
.getIndex() == 0)
249 final ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
250 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
255 // Check if we are already at the right index
256 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
257 return fExperimentContext
;
260 return seekEvent(request
.getIndex());
263 // ------------------------------------------------------------------------
264 // ITmfTrace trace positioning
265 // ------------------------------------------------------------------------
269 * Returns a brand new context based on the location provided and
270 * initializes the event queues
272 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
275 public synchronized ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
276 // Validate the location
277 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
278 return null; // Throw an exception?
280 // Make sure we have something to read from
281 if (fTraces
== null) {
285 // Instantiate the location
286 final TmfExperimentLocation expLocation
= (location
== null)
287 ?
new TmfExperimentLocation(new TmfLocationArray(new ITmfLocation
<?
>[fTraces
.length
]))
288 : (TmfExperimentLocation
) location
.clone();
290 // Create and populate the context's traces contexts
291 final TmfExperimentContext context
= new TmfExperimentContext(new ITmfContext
[fTraces
.length
]);
293 for (int i
= 0; i
< fTraces
.length
; i
++) {
294 // Get the relevant trace attributes
295 final ITmfLocation
<?
> trcLocation
= expLocation
.getLocation().getLocations()[i
];
296 context
.getContexts()[i
] = fTraces
[i
].seekEvent(trcLocation
);
297 expLocation
.getLocation().getLocations()[i
] = context
.getContexts()[i
].getLocation().clone();
298 context
.getEvents()[i
] = fTraces
[i
].getNext(context
.getContexts()[i
]);
302 context
.setLocation(expLocation
);
303 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
304 context
.setRank((location
== null) ?
0 : ITmfContext
.UNKNOWN_RANK
);
306 fExperimentContext
= context
;
310 // ------------------------------------------------------------------------
311 // ITmfTrace - SeekEvent operations (returning a trace context)
312 // ------------------------------------------------------------------------
315 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
318 public ITmfContext
seekEvent(final double ratio
) {
319 final ITmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
324 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
327 public double getLocationRatio(final ITmfLocation
<?
> location
) {
328 if (location
instanceof TmfExperimentLocation
) {
329 return (double) seekEvent(location
).getRank() / getNbEvents();
335 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
338 public ITmfLocation
<?
> getCurrentLocation() {
339 ITmfLocation
<?
>[] locations
= new ITmfLocation
<?
>[fTraces
.length
];
340 for (int i
= 0; i
< fTraces
.length
; i
++) {
341 locations
[i
] = fTraces
[i
].getCurrentLocation();
343 return new TmfExperimentLocation(new TmfLocationArray(locations
));
346 // ------------------------------------------------------------------------
347 // ITmfTrace trace positioning
348 // ------------------------------------------------------------------------
351 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
354 public synchronized T
parseEvent(final ITmfContext context
) {
355 final ITmfContext savedContext
= context
.clone();
356 final T event
= getNext(savedContext
);
361 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
364 @SuppressWarnings("unchecked")
365 public synchronized T
getNext(ITmfContext context
) {
367 // Validate the context
368 if (!(context
instanceof TmfExperimentContext
)) {
369 return null; // Throw an exception?
372 // Make sure that we have something to read from
373 if (fTraces
== null) {
377 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
379 // If an event was consumed previously, first get the next one from that trace
380 final int lastTrace
= expContext
.getLastTrace();
381 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
382 final ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
383 expContext
.getEvents()[lastTrace
] = fTraces
[lastTrace
].getNext(traceContext
);
384 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
387 // Scan the candidate events and identify the "next" trace to read from
388 int trace
= TmfExperimentContext
.NO_TRACE
;
389 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
390 for (int i
= 0; i
< fTraces
.length
; i
++) {
391 final ITmfEvent event
= expContext
.getEvents()[i
];
392 if (event
!= null && event
.getTimestamp() != null) {
393 final ITmfTimestamp otherTS
= event
.getTimestamp();
394 if (otherTS
.compareTo(timestamp
, true) < 0) {
402 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
403 event
= (T
) expContext
.getEvents()[trace
];
405 updateAttributes(expContext
, event
.getTimestamp());
406 expContext
.increaseRank();
407 expContext
.setLastTrace(trace
);
408 final ITmfContext traceContext
= expContext
.getContexts()[trace
];
410 TmfExperimentLocation location
= (TmfExperimentLocation
) expContext
.getLocation();
411 if (location
!= null) {
412 location
.getLocation().getLocations()[trace
] = traceContext
.getLocation().clone();
415 fExperimentContext
= expContext
.clone();
424 * @see java.lang.Object#toString()
427 @SuppressWarnings("nls")
428 public synchronized String
toString() {
429 return "[TmfExperiment (" + getName() + ")]";
432 // ------------------------------------------------------------------------
434 // ------------------------------------------------------------------------
436 private synchronized void initializeStreamingMonitor() {
443 if (getStreamingInterval() == 0) {
444 final ITmfContext context
= seekEvent(0);
445 final ITmfEvent event
= getNext(context
);
449 final TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
450 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
452 // Broadcast in separate thread to prevent deadlock
462 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
463 private ITmfTimestamp safeTimestamp
= null;
464 private ITmfTimestamp lastSafeTimestamp
= null;
465 private TmfTimeRange timeRange
= null;
469 while (!fExecutor
.isShutdown()) {
470 if (!getIndexer().isIndexing()) {
471 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
472 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
473 for (final ITmfTrace
<T
> trace
: fTraces
) {
474 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
475 startTimestamp
= trace
.getStartTime();
477 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
478 endTimestamp
= trace
.getEndTime();
481 if (safeTimestamp
!= null && (lastSafeTimestamp
== null || safeTimestamp
.compareTo(lastSafeTimestamp
, false) > 0)) {
482 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
483 lastSafeTimestamp
= safeTimestamp
;
487 safeTimestamp
= endTimestamp
;
488 if (timeRange
!= null) {
489 final TmfExperimentRangeUpdatedSignal signal
=
490 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
495 Thread
.sleep(getStreamingInterval());
496 } catch (final InterruptedException e
) {
506 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
509 public long getStreamingInterval() {
511 for (final ITmfTrace
<T
> trace
: fTraces
) {
512 interval
= Math
.max(interval
, trace
.getStreamingInterval());
517 // ------------------------------------------------------------------------
519 // ------------------------------------------------------------------------
521 private Integer fEndSynchReference
;
524 * Signal handler for the TmfExperimentSelectedSignal signal
526 * @param signal The incoming signal
529 public void experimentSelected(final TmfExperimentSelectedSignal
<T
> signal
) {
530 final TmfExperiment
<?
> experiment
= signal
.getExperiment();
531 if (experiment
== this) {
532 setCurrentExperiment(experiment
);
533 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
538 * Signal handler for the TmfEndSynchSignal signal
540 * @param signal The incoming signal
543 public void endSync(final TmfEndSynchSignal signal
) {
544 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
545 fEndSynchReference
= null;
546 initializeStreamingMonitor();
551 * Signal handler for the TmfTraceUpdatedSignal signal
553 * @param signal The incoming signal
556 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
557 if (signal
.getTrace() == this) {
558 broadcast(new TmfExperimentUpdatedSignal(this, this));
563 * Signal handler for the TmfExperimentRangeUpdatedSignal signal
565 * @param signal The incoming signal
568 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal
) {
569 if (signal
.getExperiment() == this) {
570 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);