1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 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
.trace
;
17 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
19 import java
.nio
.BufferOverflowException
;
20 import java
.nio
.ByteBuffer
;
21 import java
.util
.ArrayList
;
22 import java
.util
.Collection
;
23 import java
.util
.Collections
;
24 import java
.util
.HashMap
;
25 import java
.util
.List
;
29 import org
.eclipse
.core
.resources
.IProject
;
30 import org
.eclipse
.core
.resources
.IResource
;
31 import org
.eclipse
.core
.runtime
.CoreException
;
32 import org
.eclipse
.core
.runtime
.IStatus
;
33 import org
.eclipse
.core
.runtime
.Status
;
34 import org
.eclipse
.jdt
.annotation
.NonNull
;
35 import org
.eclipse
.jdt
.annotation
.Nullable
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
37 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFCallsite
;
38 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFClock
;
39 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
40 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
41 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
42 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
43 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.Metadata
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.Activator
;
45 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
46 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIteratorManager
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceProperties
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceWithPreDefinedEvents
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
63 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
64 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
65 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
66 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
67 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocation
;
68 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
69 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfTmfContext
;
70 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
71 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventType
;
72 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfChannelAspect
;
73 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfCpuAspect
;
74 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.lookup
.CtfTmfCallsite
;
76 import com
.google
.common
.collect
.ImmutableList
;
77 import com
.google
.common
.collect
.ImmutableSet
;
80 * The CTf trace handler
83 * @author Matthew khouzam
85 public class CtfTmfTrace
extends TmfTrace
86 implements ITmfTraceProperties
, ITmfPersistentlyIndexable
,
87 ITmfTraceWithPreDefinedEvents
, AutoCloseable
{
89 // -------------------------------------------
91 // -------------------------------------------
94 * Default cache size for CTF traces
96 protected static final int DEFAULT_CACHE_SIZE
= 50000;
99 * Event aspects available for all CTF traces
102 protected static final @NonNull Collection
<ITmfEventAspect
> CTF_ASPECTS
=
103 checkNotNull(ImmutableList
.of(
104 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
105 new CtfChannelAspect(),
107 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
108 ITmfEventAspect
.BaseAspects
.CONTENTS
112 * The Ctf clock unique identifier field
114 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
115 private static final int CONFIDENCE
= 10;
116 private static final int MIN_CONFIDENCE
= 1;
118 // -------------------------------------------
120 // -------------------------------------------
122 private final Map
<String
, CtfTmfEventType
> fContainedEventTypes
=
123 Collections
.synchronizedMap(new HashMap
<String
, CtfTmfEventType
>());
125 private final CtfIteratorManager fIteratorManager
=
126 new CtfIteratorManager(this);
128 /* Reference to the CTF Trace */
129 private CTFTrace fTrace
;
131 // -------------------------------------------
132 // TmfTrace Overrides
133 // -------------------------------------------
138 * The resource associated with this trace
140 * The path to the trace file
142 * The type of events that will be read from this trace
143 * @throws TmfTraceException
144 * If something went wrong while reading the trace
147 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
148 throws TmfTraceException
{
150 * Set the cache size. This has to be done before the call to super()
151 * because the super needs to know the cache size.
155 super.initTrace(resource
, path
, eventType
);
158 this.fTrace
= new CTFTrace(path
);
160 /* Set the start and (current) end times for this trace */
161 ctx
= (CtfTmfContext
) seekEvent(0L);
162 CtfTmfEvent event
= getNext(ctx
);
163 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
164 /* Handle the case where the trace is empty */
165 this.setStartTime(TmfTimestamp
.BIG_BANG
);
167 final ITmfTimestamp curTime
= event
.getTimestamp();
168 this.setStartTime(curTime
);
169 this.setEndTime(curTime
);
172 * Register every event type. When you call getType, it will
173 * register a trace to that type in the TmfEventTypeManager
175 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
176 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
177 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
178 if (ctfTmfEventType
== null) {
179 List
<ITmfEventField
> content
= new ArrayList
<>();
180 /* Should only return null the first time */
181 final StructDeclaration fields
= ied
.getFields();
182 if (fields
!= null) {
183 for (String fieldName
: fields
.getFieldsList()) {
184 content
.add(new TmfEventField(checkNotNull(fieldName
), null, null));
186 ITmfEventField contentTree
= new TmfEventField(
187 ITmfEventField
.ROOT_FIELD_ID
,
189 content
.toArray(new ITmfEventField
[content
.size()]));
191 ctfTmfEventType
= new CtfTmfEventType(ied
.getName(), contentTree
);
192 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
197 } catch (final CTFException e
) {
199 * If it failed at the init(), we can assume it's because the file
200 * was not found or was not recognized as a CTF trace. Throw into
201 * the new type of exception expected by the rest of TMF.
203 throw new TmfTraceException(e
.getMessage(), e
);
208 public void close() {
213 public synchronized void dispose() {
214 fIteratorManager
.dispose();
215 if (fTrace
!= null) {
224 * The default implementation of a CTF trace.
226 * Firstly a weak validation of the metadata is done to determine if the
227 * path is actually for a CTF trace. After that a full validation is done.
229 * If the weak and full validation are successful the confidence is set
232 * If the weak validation was successful, but the full validation fails
233 * a TraceValidationStatus with severity warning and confidence of 1 is
236 * If both weak and full validation fails an error status is returned.
239 public IStatus
validate(final IProject project
, final String path
) {
240 boolean isMetadataFile
= false;
242 isMetadataFile
= Metadata
.preValidate(path
);
243 } catch (final CTFException e
) {
244 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
247 if (isMetadataFile
) {
248 // Trace is pre-validated, continue will full validation
250 final CTFTrace trace
= new CTFTrace(path
);
251 if (!trace
.majorIsSet()) {
252 if (isMetadataFile
) {
253 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
255 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
258 // Validate using reader initialization
259 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
261 // Trace is validated, return with confidence
262 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
264 } catch (final CTFException
| BufferOverflowException e
) {
265 // return warning since it's a CTF trace but with errors in it
266 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
269 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
273 public Iterable
<ITmfEventAspect
> getEventAspects() {
278 * Method getCurrentLocation. This is not applicable in CTF
280 * @return null, since the trace has no knowledge of the current location
281 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
284 public ITmfLocation
getCurrentLocation() {
289 public double getLocationRatio(ITmfLocation location
) {
290 final CtfLocation curLocation
= (CtfLocation
) location
;
291 final long startTime
= getStartTime().getValue();
292 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
293 final double total
= getEndTime().getValue() - startTime
;
294 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
302 * @return ITmfContext
305 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
306 CtfLocation currentLocation
= (CtfLocation
) location
;
307 CtfTmfContext context
= new CtfTmfContext(this);
308 if (fTrace
== null) {
309 context
.setLocation(null);
310 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
314 * The rank is set to 0 if the iterator seeks the beginning. If not, it
315 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
318 if (currentLocation
== null) {
319 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
322 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
323 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
324 currentLocation
= new CtfLocation(fTrace
.getCurrentEndTime() + 1, 0L);
327 /* This will seek and update the location after the seek */
328 context
.setLocation(currentLocation
);
333 public synchronized ITmfContext
seekEvent(double ratio
) {
334 CtfTmfContext context
= new CtfTmfContext(this);
335 if (fTrace
== null) {
336 context
.setLocation(null);
337 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
340 final long end
= getEndTime().getValue();
341 final long start
= getStartTime().getValue();
342 final long diff
= end
- start
;
343 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
344 context
.seek(ratioTs
);
345 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
350 * Method readNextEvent.
354 * @return CtfTmfEvent
355 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
358 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
359 if (fTrace
== null) {
362 CtfTmfEvent event
= null;
363 if (context
instanceof CtfTmfContext
) {
364 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
367 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
368 event
= ctfContext
.getCurrentEvent();
371 updateAttributes(context
, event
);
372 ctfContext
.advance();
373 ctfContext
.increaseRank();
381 * Ctf traces have a clock with a unique uuid that will be used to identify
382 * the host. Traces with the same clock uuid will be known to have been made
383 * on the same machine.
385 * Note: uuid is an optional field, it may not be there for a clock.
388 public String
getHostId() {
389 CTFClock clock
= fTrace
.getClock();
391 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
392 if (clockHost
!= null) {
396 return super.getHostId();
400 * Get the first callsite that matches the event name
402 * @param eventName The event name to look for
403 * @return The best callsite candidate
405 public @Nullable CtfTmfCallsite
getCallsite(String eventName
) {
406 CTFCallsite callsite
= fTrace
.getCallsite(eventName
);
407 if (callsite
!= null) {
408 return new CtfTmfCallsite(callsite
);
414 * Get the closest matching callsite for given event name and instruction
420 * The instruction pointer
421 * @return The closest matching callsite
423 public @Nullable CtfTmfCallsite
getCallsite(String eventName
, long ip
) {
424 CTFCallsite calliste
= fTrace
.getCallsite(eventName
, ip
);
425 if (calliste
!= null) {
426 return new CtfTmfCallsite(calliste
);
432 * Get the CTF environment variables defined in this CTF trace, in <name,
433 * value> form. This comes from the trace's CTF metadata.
435 * @return The CTF environment
437 public Map
<String
, String
> getEnvironment() {
438 return fTrace
.getEnvironment();
441 // -------------------------------------------
442 // ITmfTraceProperties
443 // -------------------------------------------
446 public Map
<String
, String
> getTraceProperties() {
447 Map
<String
, String
> properties
= new HashMap
<>();
448 properties
.putAll(fTrace
.getEnvironment());
449 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
453 // -------------------------------------------
455 // -------------------------------------------
458 * gets the clock offset
460 * @return the clock offset in ns
462 public long getOffset() {
463 if (fTrace
!= null) {
464 return fTrace
.getOffset();
470 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
474 * The timestamp in cycles
475 * @return The timestamp in nanoseconds
477 public long timestampCyclesToNanos(long cycles
) {
478 return fTrace
.timestampCyclesToNanos(cycles
);
482 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
486 * The timestamp in nanoseconds
487 * @return The timestamp in cycles
489 public long timestampNanoToCycles(long nanos
) {
490 return fTrace
.timestampNanoToCycles(nanos
);
494 * Gets the list of declared events
497 public Set
<CtfTmfEventType
> getContainedEventTypes() {
498 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
502 * Register an event type to this trace.
504 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
506 * FIXME This could probably be made cleaner?
509 * The event type to register
511 public void registerEventType(CtfTmfEventType eventType
) {
512 fContainedEventTypes
.put(eventType
.getName(), eventType
);
515 // -------------------------------------------
517 // -------------------------------------------
520 public CtfTmfEvent
parseEvent(ITmfContext context
) {
521 CtfTmfEvent event
= null;
522 if (context
instanceof CtfTmfContext
) {
523 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
524 event
= getNext(tmpContext
);
530 * Sets the cache size for a CtfTmfTrace.
532 protected void setCacheSize() {
533 setCacheSize(DEFAULT_CACHE_SIZE
);
536 // -------------------------------------------
537 // CtfIterator factory methods
538 // -------------------------------------------
541 * Get an iterator to the trace
543 * @return an iterator to the trace
545 public ITmfContext
createIterator() {
547 return new CtfIterator(fTrace
, this);
548 } catch (CTFException e
) {
549 Activator
.getDefault().logError(e
.getMessage(), e
);
555 * Get an iterator to the trace, , which will initially point to the given
558 * @param ctfLocationData
559 * The initial timestamp the iterator will be pointing to
562 * @return The new iterator
564 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
566 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
567 } catch (CTFException e
) {
568 Activator
.getDefault().logError(e
.getMessage(), e
);
574 * Create the 'CtfIterator' object from a CtfTmfContext.
577 * The iterator will initially be pointing to this context
578 * @return A new CtfIterator object
581 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
582 return fIteratorManager
.getIterator(context
);
586 * Dispose an iterator that was create with
587 * {@link #createIteratorFromContext}
590 * The last context that was pointed to by the iterator (this is
591 * the 'key' to find the correct iterator to dispose).
594 public void disposeContext(CtfTmfContext context
) {
595 fIteratorManager
.removeIterator(context
);
598 // ------------------------------------------------------------------------
599 // Timestamp transformation functions
600 // ------------------------------------------------------------------------
606 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
607 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
610 private static int fCheckpointSize
= -1;
613 public synchronized int getCheckpointSize() {
614 if (fCheckpointSize
== -1) {
615 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
616 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
619 fCheckpointSize
= b
.position();
622 return fCheckpointSize
;
626 protected ITmfTraceIndexer
createIndexer(int interval
) {
627 return new TmfBTreeTraceIndexer(this, interval
);
631 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
632 return new CtfLocation(bufferIn
);
636 public boolean isComplete() {
637 if (getResource() == null) {
643 String sessionName
= null;
645 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
646 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
647 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
648 } catch (CoreException e
) {
649 Activator
.getDefault().logError(e
.getMessage(), e
);
650 // Something happened to the resource, assume we won't get any more
654 return host
== null || port
== null || sessionName
== null;
658 public void setComplete(final boolean isComplete
) {
659 super.setComplete(isComplete
);
662 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
663 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
664 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
666 } catch (CoreException e
) {
667 Activator
.getDefault().logError(e
.getMessage(), e
);