/*******************************************************************************
* Copyright (c) 2009, 2010, 2012 Ericsson
- *
+ *
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
- *
+ *
* Contributors:
* Francois Chouinard - Initial API and implementation
* Francois Chouinard - Updated as per TMF Trace Model 1.0
package org.eclipse.linuxtools.tmf.core.trace;
import java.io.File;
-import java.io.FileNotFoundException;
import org.eclipse.core.resources.IResource;
-import org.eclipse.core.runtime.Path;
+import org.eclipse.core.runtime.IPath;
import org.eclipse.linuxtools.tmf.core.component.TmfEventProvider;
import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
* The concrete class can either specify its own indexer or use the provided
* TmfCheckpointIndexer (default). In this case, the trace cache size will be
* used as checkpoint interval.
- *
- * @since 1.0
+ *
* @version 1.0
* @author Francois Chouinard
*
* @see ITmfTraceIndexer
* @see ITmfEventParser
*/
-public abstract class TmfTrace<T extends ITmfEvent> extends TmfEventProvider<T> implements ITmfTrace<T> {
-
- // ------------------------------------------------------------------------
- // Constants
- // ------------------------------------------------------------------------
-
- /**
- * The default trace cache size
- */
- public static final int DEFAULT_TRACE_CACHE_SIZE = 10000;
+public abstract class TmfTrace extends TmfEventProvider implements ITmfTrace {
// ------------------------------------------------------------------------
// Attributes
// The trace path
private String fPath;
- /**
- * The trace cache page size
- */
- protected int fCacheSize = DEFAULT_TRACE_CACHE_SIZE;
+ // The trace cache page size
+ private int fCacheSize = ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
- /**
- * The number of events collected (so far)
- */
- protected long fNbEvents = 0;
+ // The number of events collected (so far)
+ private long fNbEvents = 0;
// The time span of the event stream
private ITmfTimestamp fStartTime = TmfTimestamp.BIG_CRUNCH;
private ITmfTimestamp fEndTime = TmfTimestamp.BIG_BANG;
- /**
- * The trace streaming interval (0 = no streaming)
- */
- protected long fStreamingInterval = 0;
+ // The trace streaming interval (0 = no streaming)
+ private long fStreamingInterval = 0;
- /**
- * The trace indexer
- */
- protected ITmfTraceIndexer<ITmfTrace<ITmfEvent>> fIndexer;
+ // The trace indexer
+ private ITmfTraceIndexer fIndexer;
- /**
- * The trace parser
- */
- protected ITmfEventParser<T> fParser;
+ // The trace parser
+ private ITmfEventParser fParser;
// ------------------------------------------------------------------------
// Construction
/**
* The default, parameterless, constructor
*/
- @SuppressWarnings({ "unchecked", "rawtypes" })
public TmfTrace() {
super();
- fIndexer = new TmfCheckpointIndexer(this);
}
/**
* The standard constructor (non-live trace). Applicable when the trace
- * implements its own parser and if at checkpoint-based index is OK.
- *
+ * implements its own parser and if at checkpoint-based index is OK.
+ *
* @param resource the resource associated to the trace
* @param type the trace event type
* @param path the trace path
* @param cacheSize the trace cache size
* @throws TmfTraceException
*/
- protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize) throws TmfTraceException {
- this(resource, type, path, cacheSize, 0, null);
+ protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize) throws TmfTraceException {
+ this(resource, type, path, cacheSize, 0);
}
/**
* The standard constructor (live trace). Applicable when the trace
* implements its own parser and if at checkpoint-based index is OK.
- *
+ *
* @param resource the resource associated to the trace
* @param type the trace event type
* @param path the trace path
* @param interval the trace streaming interval
* @throws TmfTraceException
*/
- protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize, final long interval) throws TmfTraceException {
+ protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize, final long interval) throws TmfTraceException {
this(resource, type, path, cacheSize, interval, null);
}
/**
* The 'non-default indexer' constructor. Allows to provide a trace
* specific indexer.
- *
+ *
* @param resource the resource associated to the trace
* @param type the trace event type
* @param path the trace path
* @param indexer the trace indexer
* @throws TmfTraceException
*/
- protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize,
- final long interval, final ITmfTraceIndexer<?> indexer) throws TmfTraceException {
- this(resource, type, path, cacheSize, interval, null, null);
+ protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize,
+ final long interval, final ITmfTraceIndexer indexer) throws TmfTraceException {
+ this(resource, type, path, cacheSize, interval, indexer, null);
}
/**
- * The full constructor where trace specific indexer/parser are provided.
- *
+ * The full constructor where trace specific indexer/parser are provided.
+ *
* @param resource the resource associated to the trace
* @param type the trace event type
* @param path the trace path
* @param parser the trace event parser
* @throws TmfTraceException
*/
- @SuppressWarnings({ "unchecked", "rawtypes" })
- protected TmfTrace(final IResource resource, final Class<T> type, final String path, final int cacheSize,
- final long interval, final ITmfTraceIndexer<?> indexer, final ITmfEventParser<T> parser) throws TmfTraceException {
+ protected TmfTrace(final IResource resource, final Class<? extends ITmfEvent> type, final String path, final int cacheSize,
+ final long interval, final ITmfTraceIndexer indexer, final ITmfEventParser parser) throws TmfTraceException {
super();
- fCacheSize = (cacheSize > 0) ? cacheSize : DEFAULT_TRACE_CACHE_SIZE;
+ fCacheSize = (cacheSize > 0) ? cacheSize : ITmfTrace.DEFAULT_TRACE_CACHE_SIZE;
fStreamingInterval = interval;
fIndexer = (indexer != null) ? indexer : new TmfCheckpointIndexer(this, fCacheSize);
fParser = parser;
/**
* Copy constructor
- *
+ *
* @param trace the original trace
+ * @throws TmfTraceException Should not happen usually
*/
- @SuppressWarnings({ "unchecked", "rawtypes" })
- public TmfTrace(final TmfTrace<T> trace) throws TmfTraceException {
+ public TmfTrace(final TmfTrace trace) throws TmfTraceException {
super();
- if (trace == null)
+ if (trace == null) {
throw new IllegalArgumentException();
+ }
fCacheSize = trace.getCacheSize();
fStreamingInterval = trace.getStreamingInterval();
fIndexer = new TmfCheckpointIndexer(this);
* @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(org.eclipse.core.resources.IResource, java.lang.String, java.lang.Class)
*/
@Override
- public void initTrace(final IResource resource, final String path, final Class<T> type) throws TmfTraceException {
+ public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
+ fIndexer = new TmfCheckpointIndexer(this, fCacheSize);
initialize(resource, path, type);
- fIndexer.buildIndex(false);
}
/**
* Initialize the trace common attributes and the base component.
- *
- * @param resource the Eclipse resource (trace)
+ *
+ * @param resource the Eclipse resource (trace)
* @param path the trace path
* @param type the trace event type
- *
- * @throws FileNotFoundException
+ *
+ * @throws TmfTraceException
*/
- @SuppressWarnings("unchecked")
- protected void initialize(final IResource resource, final String path, final Class<T> type) throws TmfTraceException {
- if (path == null)
+ protected void initialize(final IResource resource, final String path, final Class<? extends ITmfEvent> type) throws TmfTraceException {
+ if (path == null) {
throw new TmfTraceException("Invalid trace path"); //$NON-NLS-1$
+ }
fPath = path;
fResource = resource;
String traceName = (resource != null) ? resource.getName() : null;
// If no resource was provided, extract the display name the trace path
if (traceName == null) {
- final int sep = path.lastIndexOf(Path.SEPARATOR);
+ final int sep = path.lastIndexOf(IPath.SEPARATOR);
traceName = (sep >= 0) ? path.substring(sep + 1) : path;
}
if (fParser == null) {
if (this instanceof ITmfEventParser) {
- fParser = (ITmfEventParser<T>) this;
+ fParser = (ITmfEventParser) this;
} else {
throw new TmfTraceException("Invalid trace parser"); //$NON-NLS-1$
}
/**
* Indicates if the path points to an existing file/directory
- *
+ *
* @param path the path to test
* @return true if the file/directory exists
*/
return file.exists();
}
+ /**
+ * Index the trace
+ *
+ * @param waitForCompletion index synchronously (true) or not (false)
+ */
+ protected void indexTrace(boolean waitForCompletion) {
+ getIndexer().buildIndex(0, TmfTimeRange.ETERNITY, waitForCompletion);
+ }
+
+ /**
+ * Clears the trace
+ */
+ @Override
+ public synchronized void dispose() {
+ // Clean up the index if applicable
+ if (getIndexer() != null) {
+ getIndexer().dispose();
+ }
+ super.dispose();
+ }
+
// ------------------------------------------------------------------------
// ITmfTrace - Basic getters
// ------------------------------------------------------------------------
* @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
*/
@Override
- @SuppressWarnings("unchecked")
- public Class<T> getEventType() {
- return (Class<T>) super.getType();
+ public Class<ITmfEvent> getEventType() {
+ return (Class<ITmfEvent>) super.getType();
}
/* (non-Javadoc)
return fStreamingInterval;
}
+ /**
+ * @return the trace indexer
+ */
+ protected ITmfTraceIndexer getIndexer() {
+ return fIndexer;
+ }
+
+ /**
+ * @return the trace parser
+ */
+ protected ITmfEventParser getParser() {
+ return fParser;
+ }
+
// ------------------------------------------------------------------------
// ITmfTrace - Trace characteristics getters
// ------------------------------------------------------------------------
}
// ------------------------------------------------------------------------
- // Convenience setters
+ // Convenience setters/getters
// ------------------------------------------------------------------------
+ /**
+ * Set the trace cache size. Must be done at initialization time.
+ *
+ * @param cacheSize The trace cache size
+ */
+ protected void setCacheSize(final int cacheSize) {
+ fCacheSize = cacheSize;
+ }
+
+ /**
+ * Set the trace known number of events. This can be quite dynamic
+ * during indexing or for live traces.
+ *
+ * @param nbEvents The number of events
+ */
+ protected synchronized void setNbEvents(final long nbEvents) {
+ fNbEvents = (nbEvents > 0) ? nbEvents : 0;
+ }
+
/**
* Update the trace events time range
- *
+ *
* @param range the new time range
*/
protected void setTimeRange(final TmfTimeRange range) {
/**
* Update the trace chronologically first event timestamp
- *
+ *
* @param startTime the new first event timestamp
*/
protected void setStartTime(final ITmfTimestamp startTime) {
/**
* Update the trace chronologically last event timestamp
- *
+ *
* @param endTime the new last event timestamp
*/
protected void setEndTime(final ITmfTimestamp endTime) {
}
/**
- * Update the trace streaming interval
- *
+ * Set the polling interval for live traces (default = 0 = no streaming).
+ *
* @param interval the new trace streaming interval
*/
protected void setStreamingInterval(final long interval) {
fStreamingInterval = (interval > 0) ? interval : 0;
}
+ /**
+ * Set the trace indexer. Must be done at initialization time.
+ *
+ * @param indexer the trace indexer
+ */
+ protected void setIndexer(final ITmfTraceIndexer indexer) {
+ fIndexer = indexer;
+ }
+
+ /**
+ * Set the trace parser. Must be done at initialization time.
+ *
+ * @param parser the new trace parser
+ */
+ protected void setParser(final ITmfEventParser parser) {
+ fParser = parser;
+ }
+
// ------------------------------------------------------------------------
// ITmfTrace - SeekEvent operations (returning a trace context)
// ------------------------------------------------------------------------
// A rank <= 0 indicates to seek the first event
if (rank <= 0) {
- ITmfContext context = seekEvent((ITmfLocation<?>) null);
+ ITmfContext context = seekEvent((ITmfLocation) null);
context.setRank(0);
return context;
}
// And locate the requested event context
long pos = context.getRank();
if (pos < rank) {
- ITmfEvent event = readNextEvent(context);
- while (event != null && ++pos < rank) {
- event = readNextEvent(context);
+ ITmfEvent event = getNext(context);
+ while ((event != null) && (++pos < rank)) {
+ event = getNext(context);
}
}
return context;
// A null timestamp indicates to seek the first event
if (timestamp == null) {
- ITmfContext context = seekEvent((ITmfLocation<?>) null);
+ ITmfContext context = seekEvent((ITmfLocation) null);
context.setRank(0);
return context;
}
// Position the trace at the checkpoint
- final ITmfContext context = fIndexer.seekIndex(timestamp);
+ ITmfContext context = fIndexer.seekIndex(timestamp);
// And locate the requested event context
final ITmfContext nextEventContext = context.clone(); // Must use clone() to get the right subtype...
- ITmfEvent event = readNextEvent(nextEventContext);
+ ITmfEvent event = getNext(nextEventContext);
while (event != null && event.getTimestamp().compareTo(timestamp, false) < 0) {
- context.setLocation(nextEventContext.getLocation().clone());
- context.increaseRank();
- event = readNextEvent(nextEventContext);
+ context = nextEventContext.clone();
+ event = getNext(nextEventContext);
+ }
+ if (event == null) {
+ context.setLocation(null);
+ context.setRank(ITmfContext.UNKNOWN_RANK);
}
return context;
}
* @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
*/
@Override
- public synchronized ITmfEvent readNextEvent(final ITmfContext context) {
+ public synchronized ITmfEvent getNext(final ITmfContext context) {
// parseEvent() does not update the context
final ITmfEvent event = fParser.parseEvent(context);
if (event != null) {
/**
* Hook for special event processing by the concrete class
* (called by TmfTrace.getEvent())
- *
+ *
* @param event the event
*/
protected void processEvent(final ITmfEvent event) {
/**
* Update the trace attributes
- *
+ *
* @param context the current trace context
* @param timestamp the corresponding timestamp
*/
protected synchronized void updateAttributes(final ITmfContext context, final ITmfTimestamp timestamp) {
- if (fStartTime.compareTo(timestamp, false) > 0) {
- fStartTime = timestamp;
+ if (fStartTime.equals(TmfTimestamp.BIG_BANG) || (fStartTime.compareTo(timestamp, false) > 0)) {
+ fStartTime = timestamp.clone();
}
- if (fEndTime.compareTo(timestamp, false) < 0) {
- fEndTime = timestamp;
+ if (fEndTime.equals(TmfTimestamp.BIG_CRUNCH) || (fEndTime.compareTo(timestamp, false) < 0)) {
+ fEndTime = timestamp.clone();
}
if (context.hasValidRank()) {
long rank = context.getRank();
* @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#armRequest(org.eclipse.linuxtools.tmf.core.request.ITmfDataRequest)
*/
@Override
- public ITmfContext armRequest(final ITmfDataRequest<T> request) {
- if (request instanceof ITmfEventRequest<?>
- && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest<T>) request).getRange().getStartTime())
- && request.getIndex() == 0) {
- final ITmfContext context = seekEvent(((ITmfEventRequest<T>) request).getRange().getStartTime());
- ((ITmfEventRequest<T>) request).setStartIndex((int) context.getRank());
+ protected ITmfContext armRequest(final ITmfDataRequest request) {
+ if ((request instanceof ITmfEventRequest)
+ && !TmfTimestamp.BIG_BANG.equals(((ITmfEventRequest) request).getRange().getStartTime())
+ && (request.getIndex() == 0))
+ {
+ final ITmfContext context = seekEvent(((ITmfEventRequest) request).getRange().getStartTime());
+ ((ITmfEventRequest) request).setStartIndex((int) context.getRank());
return context;
}
return seekEvent(request.getIndex());
}
- /* (non-Javadoc)
- * @see org.eclipse.linuxtools.tmf.core.component.TmfDataProvider#getNext(org.eclipse.linuxtools.tmf.core.trace.ITmfContext)
- */
- @Override
- @SuppressWarnings("unchecked")
- public T getNext(final ITmfContext context) {
- if (context instanceof TmfContext)
- return (T) readNextEvent(context);
- return null;
- }
-
-
// ------------------------------------------------------------------------
// toString
// ------------------------------------------------------------------------