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
);
198 } catch (final CTFException e
) {
200 * If it failed at the init(), we can assume it's because the file
201 * was not found or was not recognized as a CTF trace. Throw into
202 * the new type of exception expected by the rest of TMF.
204 throw new TmfTraceException(e
.getMessage(), e
);
209 public void close() {
214 public synchronized void dispose() {
215 fIteratorManager
.dispose();
216 if (fTrace
!= null) {
225 * The default implementation of a CTF trace.
227 * Firstly a weak validation of the metadata is done to determine if the
228 * path is actually for a CTF trace. After that a full validation is done.
230 * If the weak and full validation are successful the confidence is set
233 * If the weak validation was successful, but the full validation fails
234 * a TraceValidationStatus with severity warning and confidence of 1 is
237 * If both weak and full validation fails an error status is returned.
240 public IStatus
validate(final IProject project
, final String path
) {
241 boolean isMetadataFile
= false;
243 isMetadataFile
= Metadata
.preValidate(path
);
244 } catch (final CTFException e
) {
245 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
248 if (isMetadataFile
) {
249 // Trace is pre-validated, continue will full validation
251 final CTFTrace trace
= new CTFTrace(path
);
252 if (!trace
.majorIsSet()) {
253 if (isMetadataFile
) {
254 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
256 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
259 // Validate using reader initialization
260 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
262 // Trace is validated, return with confidence
263 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
265 } catch (final CTFException
| BufferOverflowException e
) {
266 // return warning since it's a CTF trace but with errors in it
267 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
270 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
274 public Iterable
<ITmfEventAspect
> getEventAspects() {
279 * Method getCurrentLocation. This is not applicable in CTF
281 * @return null, since the trace has no knowledge of the current location
282 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
285 public ITmfLocation
getCurrentLocation() {
290 public double getLocationRatio(ITmfLocation location
) {
291 final CtfLocation curLocation
= (CtfLocation
) location
;
292 final long startTime
= getStartTime().getValue();
293 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
294 final double total
= getEndTime().getValue() - startTime
;
295 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
303 * @return ITmfContext
306 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
307 CtfLocation currentLocation
= (CtfLocation
) location
;
308 CtfTmfContext context
= new CtfTmfContext(this);
309 if (fTrace
== null) {
310 context
.setLocation(null);
311 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
315 * The rank is set to 0 if the iterator seeks the beginning. If not, it
316 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
319 if (currentLocation
== null) {
320 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
323 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
324 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
325 currentLocation
= new CtfLocation(fTrace
.getCurrentEndTime() + 1, 0L);
328 /* This will seek and update the location after the seek */
329 context
.setLocation(currentLocation
);
334 public synchronized ITmfContext
seekEvent(double ratio
) {
335 CtfTmfContext context
= new CtfTmfContext(this);
336 if (fTrace
== null) {
337 context
.setLocation(null);
338 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
341 final long end
= getEndTime().getValue();
342 final long start
= getStartTime().getValue();
343 final long diff
= end
- start
;
344 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
345 context
.seek(ratioTs
);
346 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
351 * Method readNextEvent.
355 * @return CtfTmfEvent
356 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
359 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
360 if (fTrace
== null) {
363 CtfTmfEvent event
= null;
364 if (context
instanceof CtfTmfContext
) {
365 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
368 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
369 event
= ctfContext
.getCurrentEvent();
372 updateAttributes(context
, event
);
373 ctfContext
.advance();
374 ctfContext
.increaseRank();
382 * Ctf traces have a clock with a unique uuid that will be used to identify
383 * the host. Traces with the same clock uuid will be known to have been made
384 * on the same machine.
386 * Note: uuid is an optional field, it may not be there for a clock.
389 public String
getHostId() {
390 CTFClock clock
= fTrace
.getClock();
392 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
393 if (clockHost
!= null) {
397 return super.getHostId();
401 * Get the first callsite that matches the event name
403 * @param eventName The event name to look for
404 * @return The best callsite candidate
406 public @Nullable CtfTmfCallsite
getCallsite(String eventName
) {
407 CTFCallsite callsite
= fTrace
.getCallsite(eventName
);
408 if (callsite
!= null) {
409 return new CtfTmfCallsite(callsite
);
415 * Get the closest matching callsite for given event name and instruction
421 * The instruction pointer
422 * @return The closest matching callsite
424 public @Nullable CtfTmfCallsite
getCallsite(String eventName
, long ip
) {
425 CTFCallsite calliste
= fTrace
.getCallsite(eventName
, ip
);
426 if (calliste
!= null) {
427 return new CtfTmfCallsite(calliste
);
433 * Get the CTF environment variables defined in this CTF trace, in <name,
434 * value> form. This comes from the trace's CTF metadata.
436 * @return The CTF environment
438 public Map
<String
, String
> getEnvironment() {
439 return fTrace
.getEnvironment();
442 // -------------------------------------------
443 // ITmfTraceProperties
444 // -------------------------------------------
447 public Map
<String
, String
> getTraceProperties() {
448 Map
<String
, String
> properties
= new HashMap
<>();
449 properties
.putAll(fTrace
.getEnvironment());
450 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
454 // -------------------------------------------
456 // -------------------------------------------
459 * gets the clock offset
461 * @return the clock offset in ns
463 public long getOffset() {
464 if (fTrace
!= null) {
465 return fTrace
.getOffset();
471 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
475 * The timestamp in cycles
476 * @return The timestamp in nanoseconds
478 public long timestampCyclesToNanos(long cycles
) {
479 return fTrace
.timestampCyclesToNanos(cycles
);
483 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
487 * The timestamp in nanoseconds
488 * @return The timestamp in cycles
490 public long timestampNanoToCycles(long nanos
) {
491 return fTrace
.timestampNanoToCycles(nanos
);
495 * Gets the list of declared events
498 public Set
<CtfTmfEventType
> getContainedEventTypes() {
499 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
503 * Register an event type to this trace.
505 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
507 * FIXME This could probably be made cleaner?
510 * The event type to register
512 public void registerEventType(CtfTmfEventType eventType
) {
513 fContainedEventTypes
.put(eventType
.getName(), eventType
);
516 // -------------------------------------------
518 // -------------------------------------------
521 public CtfTmfEvent
parseEvent(ITmfContext context
) {
522 CtfTmfEvent event
= null;
523 if (context
instanceof CtfTmfContext
) {
524 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
525 event
= getNext(tmpContext
);
531 * Sets the cache size for a CtfTmfTrace.
533 protected void setCacheSize() {
534 setCacheSize(DEFAULT_CACHE_SIZE
);
537 // -------------------------------------------
538 // CtfIterator factory methods
539 // -------------------------------------------
542 * Get an iterator to the trace
544 * @return an iterator to the trace
546 public ITmfContext
createIterator() {
548 return new CtfIterator(fTrace
, this);
549 } catch (CTFException e
) {
550 Activator
.getDefault().logError(e
.getMessage(), e
);
556 * Get an iterator to the trace, , which will initially point to the given
559 * @param ctfLocationData
560 * The initial timestamp the iterator will be pointing to
563 * @return The new iterator
565 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
567 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
568 } catch (CTFException e
) {
569 Activator
.getDefault().logError(e
.getMessage(), e
);
575 * Create the 'CtfIterator' object from a CtfTmfContext.
578 * The iterator will initially be pointing to this context
579 * @return A new CtfIterator object
582 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
583 return fIteratorManager
.getIterator(context
);
587 * Dispose an iterator that was create with
588 * {@link #createIteratorFromContext}
591 * The last context that was pointed to by the iterator (this is
592 * the 'key' to find the correct iterator to dispose).
595 public void disposeContext(CtfTmfContext context
) {
596 fIteratorManager
.removeIterator(context
);
599 // ------------------------------------------------------------------------
600 // Timestamp transformation functions
601 // ------------------------------------------------------------------------
607 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
608 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
611 private static int fCheckpointSize
= -1;
614 public synchronized int getCheckpointSize() {
615 if (fCheckpointSize
== -1) {
616 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
617 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
620 fCheckpointSize
= b
.position();
623 return fCheckpointSize
;
627 protected ITmfTraceIndexer
createIndexer(int interval
) {
628 return new TmfBTreeTraceIndexer(this, interval
);
632 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
633 return new CtfLocation(bufferIn
);
637 public boolean isComplete() {
638 if (getResource() == null) {
644 String sessionName
= null;
646 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
647 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
648 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
649 } catch (CoreException e
) {
650 Activator
.getDefault().logError(e
.getMessage(), e
);
651 // Something happened to the resource, assume we won't get any more
655 return host
== null || port
== null || sessionName
== null;
659 public void setComplete(final boolean isComplete
) {
660 super.setComplete(isComplete
);
663 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
664 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
665 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
667 } catch (CoreException e
) {
668 Activator
.getDefault().logError(e
.getMessage(), e
);