1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 Ericsson, École Polytechnique de Montréal
4 * All rights reserved. This program and the accompanying materials are made
5 * 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 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
12 * Geneviève Bastien - Added the createTimestamp function
13 *******************************************************************************/
15 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
;
17 import java
.nio
.BufferOverflowException
;
18 import java
.nio
.ByteBuffer
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
26 import org
.eclipse
.core
.resources
.IProject
;
27 import org
.eclipse
.core
.resources
.IResource
;
28 import org
.eclipse
.core
.runtime
.CoreException
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.Status
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFClock
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.Activator
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfEventParser
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceProperties
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceWithPreDefinedEvents
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
56 import com
.google
.common
.collect
.ImmutableSet
;
59 * The CTf trace handler
62 * @author Matthew khouzam
64 public class CtfTmfTrace
extends TmfTrace
65 implements ITmfEventParser
, ITmfTraceProperties
, ITmfPersistentlyIndexable
,
66 ITmfTraceWithPreDefinedEvents
, AutoCloseable
{
68 // -------------------------------------------
70 // -------------------------------------------
72 * Default cache size for CTF traces
74 protected static final int DEFAULT_CACHE_SIZE
= 50000;
77 * The Ctf clock unique identifier field
79 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
80 private static final int CONFIDENCE
= 10;
82 // -------------------------------------------
84 // -------------------------------------------
86 private final Map
<String
, CtfTmfEventType
> fContainedEventTypes
=
87 Collections
.synchronizedMap(new HashMap
<String
, CtfTmfEventType
>());
89 private final CtfIteratorManager fIteratorManager
=
90 new CtfIteratorManager(this);
92 /* Reference to the CTF Trace */
93 private CTFTrace fTrace
;
95 // -------------------------------------------
97 // -------------------------------------------
102 * The resource associated with this trace
104 * The path to the trace file
106 * The type of events that will be read from this trace
107 * @throws TmfTraceException
108 * If something went wrong while reading the trace
111 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
112 throws TmfTraceException
{
114 * Set the cache size. This has to be done before the call to super()
115 * because the super needs to know the cache size.
119 super.initTrace(resource
, path
, eventType
);
122 this.fTrace
= new CTFTrace(path
);
124 /* Set the start and (current) end times for this trace */
125 ctx
= (CtfTmfContext
) seekEvent(0L);
126 CtfTmfEvent event
= getNext(ctx
);
127 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
128 /* Handle the case where the trace is empty */
129 this.setStartTime(TmfTimestamp
.BIG_BANG
);
131 final ITmfTimestamp curTime
= event
.getTimestamp();
132 this.setStartTime(curTime
);
133 this.setEndTime(curTime
);
136 * Register every event type. When you call getType, it will
137 * register a trace to that type in the TmfEventTypeManager
139 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
140 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
141 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
142 if (ctfTmfEventType
== null) {
143 List
<ITmfEventField
> content
= new ArrayList
<>();
144 /* Should only return null the first time */
145 for (String fieldName
: ied
.getFields().getFieldsList()) {
146 content
.add(new TmfEventField(fieldName
, null, null));
148 ITmfEventField contentTree
= new TmfEventField(
149 ITmfEventField
.ROOT_FIELD_ID
,
151 content
.toArray(new ITmfEventField
[content
.size()])
154 ctfTmfEventType
= new CtfTmfEventType(ied
.getName(), contentTree
);
155 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
159 } catch (final CTFReaderException e
) {
161 * If it failed at the init(), we can assume it's because the file
162 * was not found or was not recognized as a CTF trace. Throw into
163 * the new type of exception expected by the rest of TMF.
165 throw new TmfTraceException(e
.getMessage(), e
);
170 * Return the iterator manager of this trace
172 * @return The iterator manager
174 public CtfIteratorManager
getIteratorManager() {
175 return fIteratorManager
;
179 public void close() {
184 public synchronized void dispose() {
185 fIteratorManager
.dispose();
186 if (fTrace
!= null) {
196 * The default implementation sets the confidence to 10 if the trace is a
200 public IStatus
validate(final IProject project
, final String path
) {
201 IStatus status
= new TraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
);
202 try (final CTFTrace temp
= new CTFTrace(path
);) {
203 if (!temp
.majorIsSet()) {
204 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
206 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);) {
207 if (!ctfTraceReader
.hasMoreEvents()) {
208 // TODO: This will need an additional check when we
209 // support live traces
210 // because having no event is valid for a live trace
211 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
215 } catch (final CTFReaderException e
) {
216 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString()); //$NON-NLS-1$
217 } catch (final BufferOverflowException e
) {
218 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + Messages
.CtfTmfTrace_BufferOverflowErrorMessage
); //$NON-NLS-1$
225 * Method getCurrentLocation. This is not applicable in CTF
227 * @return null, since the trace has no knowledge of the current location
228 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
232 public ITmfLocation
getCurrentLocation() {
240 public double getLocationRatio(ITmfLocation location
) {
241 final CtfLocation curLocation
= (CtfLocation
) location
;
242 final CtfTmfContext context
= new CtfTmfContext(this);
243 context
.setLocation(curLocation
);
244 context
.seek(curLocation
.getLocationInfo());
245 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
246 final long startTime
= fIteratorManager
.getIterator(context
).getStartTime();
247 final long endTime
= fIteratorManager
.getIterator(context
).getEndTime();
248 return ((double) currentTime
.getTimestamp() - startTime
)
249 / (endTime
- startTime
);
257 * @return ITmfContext
261 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
262 CtfLocation currentLocation
= (CtfLocation
) location
;
263 CtfTmfContext context
= new CtfTmfContext(this);
264 if (fTrace
== null) {
265 context
.setLocation(null);
266 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
270 * The rank is set to 0 if the iterator seeks the beginning. If not, it
271 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
274 if (currentLocation
== null) {
275 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
278 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
279 currentLocation
= new CtfLocation(getCTFTrace().getCurrentEndTime() + 1, 0L);
281 context
.setLocation(currentLocation
);
282 if (location
== null) {
283 long timestamp
= fIteratorManager
.getIterator(context
).getCurrentTimestamp();
284 currentLocation
= new CtfLocation(timestamp
, 0);
286 if (context
.getRank() != 0) {
287 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
293 public synchronized ITmfContext
seekEvent(double ratio
) {
294 CtfTmfContext context
= new CtfTmfContext(this);
295 if (fTrace
== null) {
296 context
.setLocation(null);
297 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
300 final long end
= getCTFTrace().getCurrentEndTime();
301 final long start
= getCTFTrace().getCurrentStartTime();
302 final long diff
= end
- start
;
303 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
304 context
.seek(ratioTs
);
305 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
310 * Method readNextEvent.
314 * @return CtfTmfEvent
315 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
318 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
319 if (fTrace
== null) {
322 CtfTmfEvent event
= null;
323 if (context
instanceof CtfTmfContext
) {
324 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
327 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
328 event
= ctfContext
.getCurrentEvent();
331 updateAttributes(context
, event
.getTimestamp());
332 ctfContext
.advance();
333 ctfContext
.increaseRank();
341 * gets the CTFtrace that this is wrapping
343 * @return the CTF trace
345 public CTFTrace
getCTFTrace() {
350 * Ctf traces have a clock with a unique uuid that will be used to identify
351 * the host. Traces with the same clock uuid will be known to have been made
352 * on the same machine.
354 * Note: uuid is an optional field, it may not be there for a clock.
357 public String
getHostId() {
358 CTFClock clock
= getCTFTrace().getClock();
360 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
361 if (clockHost
!= null) {
365 return super.getHostId();
368 // -------------------------------------------
369 // ITmfTraceProperties
370 // -------------------------------------------
376 public Map
<String
, String
> getTraceProperties() {
377 Map
<String
, String
> properties
= new HashMap
<>();
378 properties
.putAll(fTrace
.getEnvironment());
379 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
383 // -------------------------------------------
385 // -------------------------------------------
388 * gets the clock offset
390 * @return the clock offset in ns
392 public long getOffset() {
393 if (fTrace
!= null) {
394 return fTrace
.getOffset();
400 * Gets the list of declared events
405 public Set
<CtfTmfEventType
> getContainedEventTypes() {
406 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
410 * Register an event type to this trace.
412 * Package-visible so that {@link CtfTmfEvent#getType} can call it.
414 * FIXME This could probably be made cleaner?
416 void registerEventType(CtfTmfEventType eventType
) {
417 fContainedEventTypes
.put(eventType
.getName(), eventType
);
420 // -------------------------------------------
422 // -------------------------------------------
425 public CtfTmfEvent
parseEvent(ITmfContext context
) {
426 CtfTmfEvent event
= null;
427 if (context
instanceof CtfTmfContext
) {
428 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
429 event
= getNext(tmpContext
);
435 * Sets the cache size for a CtfTmfTrace.
437 protected void setCacheSize() {
438 setCacheSize(DEFAULT_CACHE_SIZE
);
441 // -------------------------------------------
443 // -------------------------------------------
446 * Get an iterator to the trace
448 * @return an iterator to the trace
451 public CtfIterator
createIterator() {
453 return new CtfIterator(this);
454 } catch (CTFReaderException e
) {
455 Activator
.getDefault().logError(e
.getMessage(), e
);
460 // ------------------------------------------------------------------------
461 // Timestamp transformation functions
462 // ------------------------------------------------------------------------
468 public CtfTmfTimestamp
createTimestamp(long ts
) {
469 return new CtfTmfTimestamp(getTimestampTransform().transform(ts
));
472 private static int fCheckpointSize
= -1;
478 public synchronized int getCheckpointSize() {
479 if (fCheckpointSize
== -1) {
480 TmfCheckpoint c
= new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
481 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
484 fCheckpointSize
= b
.position();
487 return fCheckpointSize
;
491 protected ITmfTraceIndexer
createIndexer(int interval
) {
492 return new TmfBTreeTraceIndexer(this, interval
);
499 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
500 return new CtfLocation(bufferIn
);
504 public boolean isComplete() {
505 if (getResource() == null) {
511 String sessionName
= null;
513 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
514 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
515 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
516 } catch (CoreException e
) {
517 Activator
.getDefault().logError(e
.getMessage(), e
);
518 // Something happened to the resource, assume we won't get any more data from it
521 return host
== null || port
== null || sessionName
== null;
525 public void setComplete(final boolean isComplete
) {
526 super.setComplete(isComplete
);
529 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
530 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
531 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
533 } catch (CoreException e
) {
534 Activator
.getDefault().logError(e
.getMessage(), e
);