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
extends TmfTrace
implements ITmfEventParser
{
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
[] 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 // ------------------------------------------------------------------------
88 * @param type the event type
89 * @param id the experiment id
90 * @param traces the experiment set of traces
92 public TmfExperiment(final Class
<?
extends ITmfEvent
> type
, final String id
, final ITmfTrace
[] traces
) {
93 this(type
, id
, traces
, DEFAULT_INDEX_PAGE_SIZE
);
97 * @param type the event type
98 * @param path the experiment path
99 * @param traces the experiment set of traces
100 * @param indexPageSize the experiment index page size
102 public TmfExperiment(final Class
<?
extends ITmfEvent
> type
, final String path
, final ITmfTrace
[] traces
, final int indexPageSize
) {
103 setCacheSize(indexPageSize
);
104 setStreamingInterval(0);
105 setIndexer(new TmfCheckpointIndexer(this, indexPageSize
));
108 super.initialize(null, path
, type
);
109 } catch (TmfTraceException e
) {
114 setTimeRange(TmfTimeRange
.NULL_RANGE
);
118 * Clears the experiment
121 public synchronized void dispose() {
123 final TmfExperimentDisposedSignal signal
= new TmfExperimentDisposedSignal(this, this);
126 if (fCurrentExperiment
== this) {
127 fCurrentExperiment
= null;
130 // Clean up the index if applicable
131 if (getIndexer() != null) {
132 getIndexer().dispose();
135 if (fTraces
!= null) {
136 for (final ITmfTrace trace
: fTraces
) {
144 // ------------------------------------------------------------------------
145 // ITmfTrace - Initializers
146 // ------------------------------------------------------------------------
149 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
152 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) {
156 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
159 public boolean validate(final IProject project
, final String path
) {
163 // ------------------------------------------------------------------------
165 // ------------------------------------------------------------------------
168 * Selects the current, framework-wide, experiment
170 * @param experiment das experiment
172 public static void setCurrentExperiment(final TmfExperiment experiment
) {
173 if (fCurrentExperiment
!= null && fCurrentExperiment
!= experiment
) {
174 fCurrentExperiment
.dispose();
176 fCurrentExperiment
= experiment
;
180 * @return das experiment
182 public static TmfExperiment
getCurrentExperiment() {
183 return fCurrentExperiment
;
187 * Get the list of traces. Handle with care...
189 * @return the experiment traces
191 public ITmfTrace
[] getTraces() {
196 * Returns the timestamp of the event at the requested index. If none,
199 * @param index the event index (rank)
200 * @return the corresponding event timestamp
202 public ITmfTimestamp
getTimestamp(final int index
) {
203 final ITmfContext context
= seekEvent(index
);
204 final ITmfEvent event
= getNext(context
);
205 return (event
!= null) ? event
.getTimestamp() : null;
209 * Set the file to be used for bookmarks on this experiment
211 * @param file the bookmarks file
213 public void setBookmarksFile(final IFile file
) {
214 fBookmarksFile
= file
;
218 * Get the file used for bookmarks on this experiment
220 * @return the bookmarks file or null if none is set
222 public IFile
getBookmarksFile() {
223 return fBookmarksFile
;
226 // ------------------------------------------------------------------------
227 // Request management
228 // ------------------------------------------------------------------------
231 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
234 protected synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
236 // Make sure we have something to read from
237 if (fTraces
== null) {
241 if (request
instanceof ITmfEventRequest
242 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
243 && request
.getIndex() == 0)
245 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
246 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
251 // Check if we are already at the right index
252 if ((fExperimentContext
!= null) && fExperimentContext
.getRank() == request
.getIndex()) {
253 return fExperimentContext
;
256 return seekEvent(request
.getIndex());
259 // ------------------------------------------------------------------------
260 // ITmfTrace trace positioning
261 // ------------------------------------------------------------------------
265 * Returns a brand new context based on the location provided and
266 * initializes the event queues
268 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
271 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
272 // Validate the location
273 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
274 return null; // Throw an exception?
276 // Make sure we have something to read from
277 if (fTraces
== null) {
281 // Instantiate the location
282 final TmfExperimentLocation expLocation
= (location
== null)
283 ?
new TmfExperimentLocation(new TmfLocationArray(new ITmfLocation
[fTraces
.length
]))
284 : (TmfExperimentLocation
) location
.clone();
286 // Create and populate the context's traces contexts
287 final TmfExperimentContext context
= new TmfExperimentContext(new ITmfContext
[fTraces
.length
]);
289 for (int i
= 0; i
< fTraces
.length
; i
++) {
290 // Get the relevant trace attributes
291 final ITmfLocation trcLocation
= expLocation
.getLocationInfo().getLocations()[i
];
292 context
.getContexts()[i
] = fTraces
[i
].seekEvent(trcLocation
);
293 expLocation
.getLocationInfo().getLocations()[i
] = context
.getContexts()[i
].getLocation().clone();
294 context
.getEvents()[i
] = fTraces
[i
].getNext(context
.getContexts()[i
]);
298 context
.setLocation(expLocation
);
299 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
300 context
.setRank((location
== null) ?
0 : ITmfContext
.UNKNOWN_RANK
);
302 fExperimentContext
= context
;
306 // ------------------------------------------------------------------------
307 // ITmfTrace - SeekEvent operations (returning a trace context)
308 // ------------------------------------------------------------------------
311 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
314 public ITmfContext
seekEvent(final double ratio
) {
315 final ITmfContext context
= seekEvent((long) (ratio
* getNbEvents()));
320 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
323 public double getLocationRatio(final ITmfLocation location
) {
324 if (location
instanceof TmfExperimentLocation
) {
325 return (double) seekEvent(location
).getRank() / getNbEvents();
331 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
334 public ITmfLocation
getCurrentLocation() {
335 ITmfLocation
[] locations
= new ITmfLocation
[fTraces
.length
];
336 for (int i
= 0; i
< fTraces
.length
; i
++) {
337 locations
[i
] = fTraces
[i
].getCurrentLocation();
339 return new TmfExperimentLocation(new TmfLocationArray(locations
));
342 // ------------------------------------------------------------------------
343 // ITmfTrace trace positioning
344 // ------------------------------------------------------------------------
347 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
350 public synchronized ITmfEvent
parseEvent(final ITmfContext context
) {
351 final ITmfContext savedContext
= context
.clone();
352 final ITmfEvent event
= getNext(savedContext
);
357 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
360 public synchronized ITmfEvent
getNext(ITmfContext context
) {
362 // Validate the context
363 if (!(context
instanceof TmfExperimentContext
)) {
364 return null; // Throw an exception?
367 // Make sure that we have something to read from
368 if (fTraces
== null) {
372 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
374 // If an event was consumed previously, first get the next one from that trace
375 final int lastTrace
= expContext
.getLastTrace();
376 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
377 final ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
378 expContext
.getEvents()[lastTrace
] = fTraces
[lastTrace
].getNext(traceContext
);
379 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
382 // Scan the candidate events and identify the "next" trace to read from
383 int trace
= TmfExperimentContext
.NO_TRACE
;
384 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
385 for (int i
= 0; i
< fTraces
.length
; i
++) {
386 final ITmfEvent event
= expContext
.getEvents()[i
];
387 if (event
!= null && event
.getTimestamp() != null) {
388 final ITmfTimestamp otherTS
= event
.getTimestamp();
389 if (otherTS
.compareTo(timestamp
, true) < 0) {
396 ITmfEvent event
= null;
397 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
398 event
= expContext
.getEvents()[trace
];
400 updateAttributes(expContext
, event
.getTimestamp());
401 expContext
.increaseRank();
402 expContext
.setLastTrace(trace
);
403 final ITmfContext traceContext
= expContext
.getContexts()[trace
];
405 TmfExperimentLocation location
= (TmfExperimentLocation
) expContext
.getLocation();
406 if (location
!= null) {
407 location
.getLocationInfo().getLocations()[trace
] = traceContext
.getLocation().clone();
410 fExperimentContext
= expContext
.clone();
419 * @see java.lang.Object#toString()
422 @SuppressWarnings("nls")
423 public synchronized String
toString() {
424 return "[TmfExperiment (" + getName() + ")]";
427 // ------------------------------------------------------------------------
429 // ------------------------------------------------------------------------
431 private synchronized void initializeStreamingMonitor() {
438 if (getStreamingInterval() == 0) {
439 final ITmfContext context
= seekEvent(0);
440 final ITmfEvent event
= getNext(context
);
444 final TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp().clone(), TmfTimestamp
.BIG_CRUNCH
);
445 final TmfExperimentRangeUpdatedSignal signal
= new TmfExperimentRangeUpdatedSignal(this, this, timeRange
);
447 // Broadcast in separate thread to prevent deadlock
457 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
458 private ITmfTimestamp safeTimestamp
= null;
459 private ITmfTimestamp lastSafeTimestamp
= null;
460 private TmfTimeRange timeRange
= null;
464 while (!executorIsShutdown()) {
465 if (!getIndexer().isIndexing()) {
466 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
467 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
468 for (final ITmfTrace trace
: fTraces
) {
469 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
470 startTimestamp
= trace
.getStartTime();
472 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
473 endTimestamp
= trace
.getEndTime();
476 if (safeTimestamp
!= null && (lastSafeTimestamp
== null || safeTimestamp
.compareTo(lastSafeTimestamp
, false) > 0)) {
477 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
478 lastSafeTimestamp
= safeTimestamp
;
482 safeTimestamp
= endTimestamp
;
483 if (timeRange
!= null) {
484 final TmfExperimentRangeUpdatedSignal signal
=
485 new TmfExperimentRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
490 Thread
.sleep(getStreamingInterval());
491 } catch (final InterruptedException e
) {
501 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
504 public long getStreamingInterval() {
506 for (final ITmfTrace trace
: fTraces
) {
507 interval
= Math
.max(interval
, trace
.getStreamingInterval());
512 // ------------------------------------------------------------------------
514 // ------------------------------------------------------------------------
516 private Integer fEndSynchReference
;
519 * Signal handler for the TmfExperimentSelectedSignal signal
521 * @param signal The incoming signal
524 public void experimentSelected(final TmfExperimentSelectedSignal signal
) {
525 final TmfExperiment experiment
= signal
.getExperiment();
526 if (experiment
== this) {
527 setCurrentExperiment(experiment
);
528 fEndSynchReference
= Integer
.valueOf(signal
.getReference());
533 * Signal handler for the TmfEndSynchSignal signal
535 * @param signal The incoming signal
538 public void endSync(final TmfEndSynchSignal signal
) {
539 if (fEndSynchReference
!= null && fEndSynchReference
.intValue() == signal
.getReference()) {
540 fEndSynchReference
= null;
541 initializeStreamingMonitor();
546 * Signal handler for the TmfTraceUpdatedSignal signal
548 * @param signal The incoming signal
551 public void traceUpdated(final TmfTraceUpdatedSignal signal
) {
552 if (signal
.getTrace() == this) {
553 broadcast(new TmfExperimentUpdatedSignal(this, this));
558 * Signal handler for the TmfExperimentRangeUpdatedSignal signal
560 * @param signal The incoming signal
563 public void experimentRangeUpdated(final TmfExperimentRangeUpdatedSignal signal
) {
564 if (signal
.getExperiment() == this) {
565 getIndexer().buildIndex(getNbEvents(), signal
.getRange(), false);