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
{
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 synchronized void dispose() {
210 fIteratorManager
.dispose();
211 if (fTrace
!= null) {
220 * The default implementation of a CTF trace.
222 * Firstly a weak validation of the metadata is done to determine if the
223 * path is actually for a CTF trace. After that a full validation is done.
225 * If the weak and full validation are successful the confidence is set
228 * If the weak validation was successful, but the full validation fails
229 * a TraceValidationStatus with severity warning and confidence of 1 is
232 * If both weak and full validation fails an error status is returned.
235 public IStatus
validate(final IProject project
, final String path
) {
236 boolean isMetadataFile
= false;
238 isMetadataFile
= Metadata
.preValidate(path
);
239 } catch (final CTFException e
) {
240 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
243 if (isMetadataFile
) {
244 // Trace is pre-validated, continue will full validation
246 final CTFTrace trace
= new CTFTrace(path
);
247 if (!trace
.majorIsSet()) {
248 if (isMetadataFile
) {
249 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
251 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
254 // Validate using reader initialization
255 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
257 // Trace is validated, return with confidence
258 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
260 } catch (final CTFException
| BufferOverflowException e
) {
261 // return warning since it's a CTF trace but with errors in it
262 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
265 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
269 public Iterable
<ITmfEventAspect
> getEventAspects() {
274 * Method getCurrentLocation. This is not applicable in CTF
276 * @return null, since the trace has no knowledge of the current location
277 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
280 public ITmfLocation
getCurrentLocation() {
285 public double getLocationRatio(ITmfLocation location
) {
286 final CtfLocation curLocation
= (CtfLocation
) location
;
287 final long startTime
= getStartTime().getValue();
288 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
289 final double total
= getEndTime().getValue() - startTime
;
290 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
298 * @return ITmfContext
301 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
302 CtfLocation currentLocation
= (CtfLocation
) location
;
303 CtfTmfContext context
= new CtfTmfContext(this);
304 if (fTrace
== null) {
305 context
.setLocation(null);
306 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
310 * The rank is set to 0 if the iterator seeks the beginning. If not, it
311 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
314 if (currentLocation
== null) {
315 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
318 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
319 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
320 currentLocation
= new CtfLocation(fTrace
.getCurrentEndTime() + 1, 0L);
323 /* This will seek and update the location after the seek */
324 context
.setLocation(currentLocation
);
329 public synchronized ITmfContext
seekEvent(double ratio
) {
330 CtfTmfContext context
= new CtfTmfContext(this);
331 if (fTrace
== null) {
332 context
.setLocation(null);
333 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
336 final long end
= getEndTime().getValue();
337 final long start
= getStartTime().getValue();
338 final long diff
= end
- start
;
339 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
340 context
.seek(ratioTs
);
341 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
346 * Method readNextEvent.
350 * @return CtfTmfEvent
351 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
354 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
355 if (fTrace
== null) {
358 CtfTmfEvent event
= null;
359 if (context
instanceof CtfTmfContext
) {
360 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
363 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
364 event
= ctfContext
.getCurrentEvent();
367 updateAttributes(context
, event
);
368 ctfContext
.advance();
369 ctfContext
.increaseRank();
377 * Ctf traces have a clock with a unique uuid that will be used to identify
378 * the host. Traces with the same clock uuid will be known to have been made
379 * on the same machine.
381 * Note: uuid is an optional field, it may not be there for a clock.
384 public String
getHostId() {
385 CTFClock clock
= fTrace
.getClock();
387 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
388 if (clockHost
!= null) {
392 return super.getHostId();
396 * Get the first callsite that matches the event name
398 * @param eventName The event name to look for
399 * @return The best callsite candidate
401 public @Nullable CtfTmfCallsite
getCallsite(String eventName
) {
402 CTFCallsite callsite
= fTrace
.getCallsite(eventName
);
403 if (callsite
!= null) {
404 return new CtfTmfCallsite(callsite
);
410 * Get the closest matching callsite for given event name and instruction
416 * The instruction pointer
417 * @return The closest matching callsite
419 public @Nullable CtfTmfCallsite
getCallsite(String eventName
, long ip
) {
420 CTFCallsite calliste
= fTrace
.getCallsite(eventName
, ip
);
421 if (calliste
!= null) {
422 return new CtfTmfCallsite(calliste
);
428 * Get the CTF environment variables defined in this CTF trace, in <name,
429 * value> form. This comes from the trace's CTF metadata.
431 * @return The CTF environment
433 public Map
<String
, String
> getEnvironment() {
434 return fTrace
.getEnvironment();
437 // -------------------------------------------
438 // ITmfTraceProperties
439 // -------------------------------------------
442 public Map
<String
, String
> getTraceProperties() {
443 Map
<String
, String
> properties
= new HashMap
<>();
444 properties
.putAll(fTrace
.getEnvironment());
445 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
449 // -------------------------------------------
451 // -------------------------------------------
454 * gets the clock offset
456 * @return the clock offset in ns
458 public long getOffset() {
459 if (fTrace
!= null) {
460 return fTrace
.getOffset();
466 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
470 * The timestamp in cycles
471 * @return The timestamp in nanoseconds
473 public long timestampCyclesToNanos(long cycles
) {
474 return fTrace
.timestampCyclesToNanos(cycles
);
478 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
482 * The timestamp in nanoseconds
483 * @return The timestamp in cycles
485 public long timestampNanoToCycles(long nanos
) {
486 return fTrace
.timestampNanoToCycles(nanos
);
490 * Gets the list of declared events
493 public Set
<CtfTmfEventType
> getContainedEventTypes() {
494 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
498 * Register an event type to this trace.
500 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
502 * FIXME This could probably be made cleaner?
505 * The event type to register
507 public void registerEventType(CtfTmfEventType eventType
) {
508 fContainedEventTypes
.put(eventType
.getName(), eventType
);
511 // -------------------------------------------
513 // -------------------------------------------
516 public CtfTmfEvent
parseEvent(ITmfContext context
) {
517 CtfTmfEvent event
= null;
518 if (context
instanceof CtfTmfContext
) {
519 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
520 event
= getNext(tmpContext
);
526 * Sets the cache size for a CtfTmfTrace.
528 protected void setCacheSize() {
529 setCacheSize(DEFAULT_CACHE_SIZE
);
532 // -------------------------------------------
533 // CtfIterator factory methods
534 // -------------------------------------------
537 * Get an iterator to the trace
539 * @return an iterator to the trace
541 public ITmfContext
createIterator() {
543 return new CtfIterator(fTrace
, this);
544 } catch (CTFException e
) {
545 Activator
.getDefault().logError(e
.getMessage(), e
);
551 * Get an iterator to the trace, , which will initially point to the given
554 * @param ctfLocationData
555 * The initial timestamp the iterator will be pointing to
558 * @return The new iterator
560 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
562 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
563 } catch (CTFException e
) {
564 Activator
.getDefault().logError(e
.getMessage(), e
);
570 * Create the 'CtfIterator' object from a CtfTmfContext.
573 * The iterator will initially be pointing to this context
574 * @return A new CtfIterator object
577 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
578 return fIteratorManager
.getIterator(context
);
582 * Dispose an iterator that was create with
583 * {@link #createIteratorFromContext}
586 * The last context that was pointed to by the iterator (this is
587 * the 'key' to find the correct iterator to dispose).
590 public void disposeContext(CtfTmfContext context
) {
591 fIteratorManager
.removeIterator(context
);
594 // ------------------------------------------------------------------------
595 // Timestamp transformation functions
596 // ------------------------------------------------------------------------
602 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
603 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
606 private static int fCheckpointSize
= -1;
609 public synchronized int getCheckpointSize() {
610 if (fCheckpointSize
== -1) {
611 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
612 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
615 fCheckpointSize
= b
.position();
618 return fCheckpointSize
;
622 protected ITmfTraceIndexer
createIndexer(int interval
) {
623 return new TmfBTreeTraceIndexer(this, interval
);
627 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
628 return new CtfLocation(bufferIn
);
632 public boolean isComplete() {
633 if (getResource() == null) {
639 String sessionName
= null;
641 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
642 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
643 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
644 } catch (CoreException e
) {
645 Activator
.getDefault().logError(e
.getMessage(), e
);
646 // Something happened to the resource, assume we won't get any more
650 return host
== null || port
== null || sessionName
== null;
654 public void setComplete(final boolean isComplete
) {
655 super.setComplete(isComplete
);
658 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
659 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
660 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
662 } catch (CoreException e
) {
663 Activator
.getDefault().logError(e
.getMessage(), e
);