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
.TmfClearExperimentSignal
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalHandler
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceOpenedSignal
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfTraceRangeUpdatedSignal
;
35 * TmfExperiment presents a time-ordered, unified view of a set of ITmfTrace:s
36 * that are part of a tracing experiment.
39 * @author Francois Chouinard
41 public class TmfExperiment
extends TmfTrace
implements ITmfEventParser
{
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
48 * The default index page size
50 public static final int DEFAULT_INDEX_PAGE_SIZE
= 5000;
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
57 * The set of traces that constitute the experiment
59 protected ITmfTrace
[] fTraces
;
62 * The set of traces that constitute the experiment
64 private boolean fInitialized
= false;
67 * The experiment bookmarks file
69 private IFile fBookmarksFile
;
71 // ------------------------------------------------------------------------
73 // ------------------------------------------------------------------------
76 * @param type the event type
77 * @param id the experiment id
78 * @param traces the experiment set of traces
80 public TmfExperiment(final Class
<?
extends ITmfEvent
> type
, final String id
, final ITmfTrace
[] traces
) {
81 this(type
, id
, traces
, DEFAULT_INDEX_PAGE_SIZE
);
85 * @param type the event type
86 * @param path the experiment path
87 * @param traces the experiment set of traces
88 * @param indexPageSize the experiment index page size
90 public TmfExperiment(final Class
<?
extends ITmfEvent
> type
, final String path
, final ITmfTrace
[] traces
, final int indexPageSize
) {
91 setCacheSize(indexPageSize
);
92 setStreamingInterval(0);
93 setIndexer(new TmfCheckpointIndexer(this, indexPageSize
));
96 super.initialize(null, path
, type
);
97 } catch (TmfTraceException e
) {
105 * Clears the experiment
108 public synchronized void dispose() {
110 // Clean up the index if applicable
111 if (getIndexer() != null) {
112 getIndexer().dispose();
115 if (fTraces
!= null) {
116 for (final ITmfTrace trace
: fTraces
) {
125 * @param signal the clear view signal
129 public void handleClearExperimentSignal(TmfClearExperimentSignal signal
) {
133 // ------------------------------------------------------------------------
134 // ITmfTrace - Initializers
135 // ------------------------------------------------------------------------
138 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
141 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> type
) {
145 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(org.eclipse.core.resources.IProject, java.lang.String)
148 public boolean validate(final IProject project
, final String path
) {
152 // ------------------------------------------------------------------------
154 // ------------------------------------------------------------------------
157 * Get the list of traces. Handle with care...
159 * @return the experiment traces
161 public ITmfTrace
[] getTraces() {
166 * Returns the timestamp of the event at the requested index. If none,
169 * @param index the event index (rank)
170 * @return the corresponding event timestamp
172 public ITmfTimestamp
getTimestamp(final int index
) {
173 final ITmfContext context
= seekEvent(index
);
174 final ITmfEvent event
= getNext(context
);
176 return (event
!= null) ? event
.getTimestamp() : null;
180 * Set the file to be used for bookmarks on this experiment
182 * @param file the bookmarks file
184 public void setBookmarksFile(final IFile file
) {
185 fBookmarksFile
= file
;
189 * Get the file used for bookmarks on this experiment
191 * @return the bookmarks file or null if none is set
193 public IFile
getBookmarksFile() {
194 return fBookmarksFile
;
197 // ------------------------------------------------------------------------
198 // Request management
199 // ------------------------------------------------------------------------
202 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
205 protected synchronized ITmfContext
armRequest(final ITmfDataRequest request
) {
207 // Make sure we have something to read from
208 if (fTraces
== null) {
212 if (request
instanceof ITmfEventRequest
213 && !TmfTimestamp
.BIG_BANG
.equals(((ITmfEventRequest
) request
).getRange().getStartTime())
214 && request
.getIndex() == 0)
216 final ITmfContext context
= seekEvent(((ITmfEventRequest
) request
).getRange().getStartTime());
217 ((ITmfEventRequest
) request
).setStartIndex((int) context
.getRank());
222 return seekEvent(request
.getIndex());
225 // ------------------------------------------------------------------------
226 // ITmfTrace trace positioning
227 // ------------------------------------------------------------------------
231 * Returns a brand new context based on the location provided and
232 * initializes the event queues
234 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
237 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
238 // Validate the location
239 if (location
!= null && !(location
instanceof TmfExperimentLocation
)) {
240 return null; // Throw an exception?
242 // Make sure we have something to read from
243 if (fTraces
== null) {
247 // Create and populate the context's traces contexts
248 final TmfExperimentContext context
= new TmfExperimentContext(new ITmfContext
[fTraces
.length
]);
249 ITmfLocation
[] expLocations
= new ITmfLocation
[fTraces
.length
];
250 if (location
!= null) {
251 TmfExperimentLocation locations
= (TmfExperimentLocation
) location
;
253 ITmfLocation l
= locations
.getLocationInfo().getLocation(index
);
254 while (index
< expLocations
.length
&& l
!= null) {
255 expLocations
[index
] = l
;
256 l
= locations
.getLocationInfo().getLocation(++index
);
260 // Position the traces
261 for (int i
= 0; i
< fTraces
.length
; i
++) {
262 // Get the relevant trace attributes
263 final ITmfLocation trcLocation
= expLocations
[i
];
264 context
.getContexts()[i
] = fTraces
[i
].seekEvent(trcLocation
);
265 expLocations
[i
] = context
.getContexts()[i
].getLocation();
266 context
.getEvents()[i
] = fTraces
[i
].getNext(context
.getContexts()[i
]);
270 context
.setLocation(new TmfExperimentLocation(new TmfLocationArray(expLocations
)));
271 context
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
272 context
.setRank((location
== null) ?
0 : ITmfContext
.UNKNOWN_RANK
);
277 // ------------------------------------------------------------------------
278 // ITmfTrace - SeekEvent operations (returning a trace context)
279 // ------------------------------------------------------------------------
282 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
285 public ITmfContext
seekEvent(final double ratio
) {
286 final ITmfContext context
= seekEvent(Math
.round(ratio
* getNbEvents()));
291 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(org.eclipse.linuxtools.tmf.core.trace.ITmfLocation)
294 public double getLocationRatio(final ITmfLocation location
) {
295 if (location
instanceof TmfExperimentLocation
) {
296 return (double) seekEvent(location
).getRank() / getNbEvents();
302 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
305 public ITmfLocation
getCurrentLocation() {
306 ITmfLocation
[] locations
= new ITmfLocation
[fTraces
.length
];
307 for (int i
= 0; i
< fTraces
.length
; i
++) {
308 locations
[i
] = fTraces
[i
].getCurrentLocation();
310 return new TmfExperimentLocation(new TmfLocationArray(locations
));
313 // ------------------------------------------------------------------------
314 // ITmfTrace trace positioning
315 // ------------------------------------------------------------------------
318 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser#parseEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
321 public synchronized ITmfEvent
parseEvent(final ITmfContext context
) {
322 final ITmfContext savedContext
= context
.clone();
323 final ITmfEvent event
= getNext(savedContext
);
328 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
331 public synchronized ITmfEvent
getNext(ITmfContext context
) {
333 // Validate the context
334 if (!(context
instanceof TmfExperimentContext
)) {
335 return null; // Throw an exception?
338 // Make sure that we have something to read from
339 if (fTraces
== null) {
343 TmfExperimentContext expContext
= (TmfExperimentContext
) context
;
345 // If an event was consumed previously, first get the next one from that trace
346 final int lastTrace
= expContext
.getLastTrace();
347 if (lastTrace
!= TmfExperimentContext
.NO_TRACE
) {
348 final ITmfContext traceContext
= expContext
.getContexts()[lastTrace
];
349 expContext
.getEvents()[lastTrace
] = fTraces
[lastTrace
].getNext(traceContext
);
350 expContext
.setLastTrace(TmfExperimentContext
.NO_TRACE
);
353 // Scan the candidate events and identify the "next" trace to read from
354 int trace
= TmfExperimentContext
.NO_TRACE
;
355 ITmfTimestamp timestamp
= TmfTimestamp
.BIG_CRUNCH
;
356 for (int i
= 0; i
< fTraces
.length
; i
++) {
357 final ITmfEvent event
= expContext
.getEvents()[i
];
358 if (event
!= null && event
.getTimestamp() != null) {
359 final ITmfTimestamp otherTS
= event
.getTimestamp();
360 if (otherTS
.compareTo(timestamp
, true) < 0) {
367 ITmfEvent event
= null;
368 if (trace
!= TmfExperimentContext
.NO_TRACE
) {
369 event
= expContext
.getEvents()[trace
];
371 updateAttributes(expContext
, event
.getTimestamp());
372 expContext
.increaseRank();
373 expContext
.setLastTrace(trace
);
374 final ITmfContext traceContext
= expContext
.getContexts()[trace
];
376 expContext
.setLocation(new TmfExperimentLocation(
377 (TmfExperimentLocation
) expContext
.getLocation(),
378 trace
, traceContext
.getLocation()));
388 * @see java.lang.Object#toString()
391 @SuppressWarnings("nls")
392 public synchronized String
toString() {
393 return "[TmfExperiment (" + getName() + ")]";
396 // ------------------------------------------------------------------------
398 // ------------------------------------------------------------------------
400 private synchronized void initializeStreamingMonitor() {
407 if (getStreamingInterval() == 0) {
408 final ITmfContext context
= seekEvent(0);
409 final ITmfEvent event
= getNext(context
);
414 final TmfTimeRange timeRange
= new TmfTimeRange(event
.getTimestamp(), TmfTimestamp
.BIG_CRUNCH
);
415 final TmfTraceRangeUpdatedSignal signal
= new TmfTraceRangeUpdatedSignal(this, this, timeRange
);
417 // Broadcast in separate thread to prevent deadlock
427 final Thread thread
= new Thread("Streaming Monitor for experiment " + getName()) { //$NON-NLS-1$
428 private ITmfTimestamp safeTimestamp
= null;
429 private ITmfTimestamp lastSafeTimestamp
= null;
430 private TmfTimeRange timeRange
= null;
434 while (!executorIsShutdown()) {
435 if (!getIndexer().isIndexing()) {
436 ITmfTimestamp startTimestamp
= TmfTimestamp
.BIG_CRUNCH
;
437 ITmfTimestamp endTimestamp
= TmfTimestamp
.BIG_BANG
;
438 for (final ITmfTrace trace
: fTraces
) {
439 if (trace
.getStartTime().compareTo(startTimestamp
) < 0) {
440 startTimestamp
= trace
.getStartTime();
442 if (trace
.getStreamingInterval() != 0 && trace
.getEndTime().compareTo(endTimestamp
) > 0) {
443 endTimestamp
= trace
.getEndTime();
446 if (safeTimestamp
!= null && (lastSafeTimestamp
== null || safeTimestamp
.compareTo(lastSafeTimestamp
, false) > 0)) {
447 timeRange
= new TmfTimeRange(startTimestamp
, safeTimestamp
);
448 lastSafeTimestamp
= safeTimestamp
;
452 safeTimestamp
= endTimestamp
;
453 if (timeRange
!= null) {
454 final TmfTraceRangeUpdatedSignal signal
=
455 new TmfTraceRangeUpdatedSignal(TmfExperiment
.this, TmfExperiment
.this, timeRange
);
460 Thread
.sleep(getStreamingInterval());
461 } catch (final InterruptedException e
) {
471 * @see org.eclipse.linuxtools.tmf.trace.ITmfTrace#getStreamingInterval()
474 public long getStreamingInterval() {
476 for (final ITmfTrace trace
: fTraces
) {
477 interval
= Math
.max(interval
, trace
.getStreamingInterval());
482 // ------------------------------------------------------------------------
484 // ------------------------------------------------------------------------
487 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#traceOpened(org.eclipse.linuxtools.tmf.core.signal.TmfTraceOpenedSignal)
491 public void traceOpened(TmfTraceOpenedSignal signal
) {
492 if (signal
.getTrace() == this) {
493 initializeStreamingMonitor();