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
135 getIndexer().dispose();
137 if (fTraces
!= null) {
138 for (final ITmfTrace trace
: fTraces
)
145 // ------------------------------------------------------------------------
146 // ITmfTrace - Initializers
147 // ------------------------------------------------------------------------
150 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
153 public boolean validate(final IProject project
, final String path
) {
158 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
161 public void initTrace(final IResource resource
, final String path
, final Class
<T
> type
) {
164 // ------------------------------------------------------------------------
166 // ------------------------------------------------------------------------
169 * Selects the current, framework-wide, experiment
171 * @param experiment das experiment
173 public static void setCurrentExperiment(final TmfExperiment
<?
> experiment
) {
174 if (fCurrentExperiment
!= null && fCurrentExperiment
!= experiment
) {
175 fCurrentExperiment
.dispose();
177 fCurrentExperiment
= experiment
;
181 * @return das experiment
183 public static TmfExperiment
<?
> getCurrentExperiment() {
184 return fCurrentExperiment
;
188 * Get the list of traces. Handle with care...
190 * @return the experiment traces
192 public ITmfTrace
<T
>[] getTraces() {
197 * Returns the timestamp of the event at the requested index. If none,
200 * @param index the event index (rank)
201 * @return the corresponding event timestamp
203 public ITmfTimestamp
getTimestamp(final int index
) {
204 final ITmfContext context
= seekEvent(index
);
205 final ITmfEvent event
= getNext(context
);
206 return (event
!= null) ? event
.getTimestamp() : null;
210 * Set the file to be used for bookmarks on this experiment
212 * @param file the bookmarks file
214 public void setBookmarksFile(final IFile file
) {
215 fBookmarksFile
= file
;
219 * Get the file used for bookmarks on this experiment
221 * @return the bookmarks file or null if none is set
223 public IFile
getBookmarksFile() {
224 return fBookmarksFile
;
227 // ------------------------------------------------------------------------
228 // Request management
229 // ------------------------------------------------------------------------
232 protected ITmfContext
armRequest(final ITmfDataRequest
<T
> request
) {
233 if (request
instanceof ITmfEventRequest
<?
>
234 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
<T
>) request
).getRange().getStartTime())
235 && request
.getIndex() == 0)
237 final ITmfContext context
= seekEvent(((ITmfEventRequest
<T
>) request
).getRange().getStartTime());
238 ((ITmfEventRequest
<T
>) request
).setStartIndex((int) context
.getRank());
243 // Check if we are already at the right index
244 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
245 return fExperimentContext
;
248 return seekEvent(request
.getIndex());
251 // ------------------------------------------------------------------------
252 // ITmfTrace trace positioning
253 // ------------------------------------------------------------------------
257 * Returns a brand new context based on the location provided and
258 * initializes the event queues
260 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
263 public synchronized ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
264 // Validate the location
265 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
266 return null; // Throw an exception?
268 // Make sure we have something to read from
269 if (fTraces
== null) {
273 // Instantiate the location
274 final TmfExperimentLocation expLocation
= (location
== null)
275 ?
new TmfExperimentLocation(new TmfLocationArray(new ITmfLocation
<?
>[fTraces
.length
]))
276 : (TmfExperimentLocation
) location
.clone();
278 // Create and populate the context's traces contexts
279 final TmfExperimentContext context
= new TmfExperimentContext(new ITmfContext
[fTraces
.length
]);
281 for (int i
= 0; i
< fTraces
.length
; i
++) {
282 // Get the relevant trace attributes
283 final ITmfLocation
<?
> traceLocation
= expLocation
.getLocation().getLocations()[i
];
284 context
.getContexts()[i
] = fTraces
[i
].seekEvent(traceLocation
);
285 expLocation
.getLocation().getLocations()[i
] = context
.getContexts()[i
].getLocation().clone();
286 context
.getEvents()[i
] = fTraces
[i
].getNext(context
.getContexts()[i
]);
290 context
.setLocation(expLocation
);
291 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
292 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
294 fExperimentContext
= context
;
296 return (ITmfContext
) context
;
300 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
303 public ITmfContext
seekEvent(final double ratio
) {
304 final ITmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
309 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
312 public double getLocationRatio(final ITmfLocation
<?
> location
) {
313 if (location
instanceof TmfExperimentLocation
) {
314 return (double) seekEvent(location
).getRank() / getNbEvents();
320 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
323 public ITmfLocation
<?
> getCurrentLocation() {
324 ITmfLocation
<?
>[] locations
= new ITmfLocation
<?
>[fTraces
.length
];
325 for (int i
= 0; i
< fTraces
.length
; i
++) {
326 locations
[i
] = fTraces
[i
].getCurrentLocation();
328 return new TmfExperimentLocation(new TmfLocationArray(locations
));
331 // ------------------------------------------------------------------------
332 // ITmfTrace trace positioning
333 // ------------------------------------------------------------------------
336 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
339 public synchronized T
getNext(final ITmfContext context
) {
340 final ITmfContext previousContext
= (TmfExperimentContext
) context
.clone();
341 final T event
= parseEvent(context
);
343 updateAttributes(previousContext
, event
.getTimestamp());
345 fExperimentContext
= (TmfExperimentContext
) context
;
346 int trace
= fExperimentContext
.getLastTrace();
347 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
348 TmfExperimentLocation location
= (TmfExperimentLocation
) fExperimentContext
.getLocation();
349 location
.getLocation().getLocations()[trace
] = fExperimentContext
.getContexts()[trace
].getLocation();
352 context
.increaseRank();
359 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
361 @SuppressWarnings("unchecked")
363 public T
parseEvent(ITmfContext context
) {
365 // Validate the context
366 if (!(context
instanceof TmfExperimentContext
)) {
367 return null; // Throw an exception?
370 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
372 // If an event was consumed previously, first get the next one from that trace
373 final int lastTrace
= expContext
.getLastTrace();
374 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
375 final ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
376 expContext
.getEvents()[lastTrace
] = fTraces
[lastTrace
].getNext(traceContext
);
377 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
380 // Scan the candidate events and identify the "next" trace to read from
381 int trace
= TmfExperimentContext
.NO_TRACE
;
382 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
383 for (int i
= 0; i
< fTraces
.length
; i
++) {
384 final ITmfEvent event
= expContext
.getEvents()[i
];
385 if (event
!= null && event
.getTimestamp() != null) {
386 final ITmfTimestamp otherTS
= event
.getTimestamp();
387 if (otherTS
.compareTo(timestamp
, true) < 0) {
395 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
396 event
= (T
) expContext
.getEvents()[trace
];
399 expContext
.setLastTrace(trace
);
404 * @see java.lang.Object#toString()
407 @SuppressWarnings("nls")
408 public String
toString() {
409 return "[TmfExperiment (" + getName() + ")]";
412 // ------------------------------------------------------------------------
414 // ------------------------------------------------------------------------
416 private synchronized void initializeStreamingMonitor() {
423 if (getStreamingInterval() == 0) {
424 final ITmfContext context
= seekEvent(0);
425 final ITmfEvent event
= getNext(context
);
428 final TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
429 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
431 // Broadcast in separate thread to prevent deadlock
441 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
442 private ITmfTimestamp safeTimestamp
= null;
443 private TmfTimeRange timeRange
= null;
447 while (!fExecutor
.isShutdown()) {
448 if (!getIndexer().isIndexing()) {
449 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
450 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
451 for (final ITmfTrace
<T
> trace
: fTraces
) {
452 if (trace
.getStartTime().compareTo(startTimestamp
) < 0)
453 startTimestamp
= trace
.getStartTime();
454 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0)
455 endTimestamp
= trace
.getEndTime();
457 if (safeTimestamp
!= null && safeTimestamp
.compareTo(getTimeRange().getEndTime(), false) > 0)
458 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
461 safeTimestamp
= endTimestamp
;
462 if (timeRange
!= null) {
463 final TmfExperimentRangeUpdatedSignal signal
=
464 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
469 Thread
.sleep(getStreamingInterval());
470 } catch (final InterruptedException e
) {
480 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
483 public long getStreamingInterval() {
485 for (final ITmfTrace
<T
> trace
: fTraces
)
486 interval
= Math
.max(interval
, trace
.getStreamingInterval());
490 // ------------------------------------------------------------------------
492 // ------------------------------------------------------------------------
494 private Integer fEndSynchReference
;
497 * Signal handler for the TmfExperimentSelectedSignal signal
502 public void experimentSelected(final TmfExperimentSelectedSignal
<T
> signal
) {
503 final TmfExperiment
<?
> experiment
= signal
.getExperiment();
504 if (experiment
== this) {
505 setCurrentExperiment(experiment
);
506 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
511 * Signal handler for the TmfEndSynchSignal signal
516 public void endSync(final TmfEndSynchSignal signal
) {
517 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
518 fEndSynchReference
= null;
519 initializeStreamingMonitor();
524 * Signal handler for the TmfTraceUpdatedSignal signal
529 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
530 if (signal
.getTrace() == this) {
531 broadcast(new TmfExperimentUpdatedSignal(this, this));
536 * Signal handler for the TmfExperimentRangeUpdatedSignal signal
541 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal
) {
542 if (signal
.getExperiment() == this) {
543 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);