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
.HashSet
;
26 import java
.util
.List
;
30 import org
.eclipse
.core
.resources
.IProject
;
31 import org
.eclipse
.core
.resources
.IResource
;
32 import org
.eclipse
.core
.runtime
.CoreException
;
33 import org
.eclipse
.core
.runtime
.IStatus
;
34 import org
.eclipse
.core
.runtime
.Status
;
35 import org
.eclipse
.jdt
.annotation
.NonNull
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.CTFException
;
37 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFClock
;
38 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
39 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
40 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
41 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
42 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.Metadata
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.Activator
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
45 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIteratorManager
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.project
.model
.ITmfPropertiesProvider
;
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
.ITmfTraceWithPreDefinedEvents
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
63 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
64 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
65 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
66 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocation
;
67 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
68 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfTmfContext
;
69 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
70 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventFactory
;
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
;
75 import com
.google
.common
.collect
.ImmutableList
;
76 import com
.google
.common
.collect
.ImmutableSet
;
79 * The CTf trace handler
82 * @author Matthew khouzam
84 public class CtfTmfTrace
extends TmfTrace
85 implements ITmfPropertiesProvider
, ITmfPersistentlyIndexable
,
86 ITmfTraceWithPreDefinedEvents
{
88 // -------------------------------------------
90 // -------------------------------------------
93 * Clock offset property
96 public static final String CLOCK_OFFSET
= "clock_offset"; //$NON-NLS-1$
99 * Default cache size for CTF traces
101 protected static final int DEFAULT_CACHE_SIZE
= 50000;
104 * Event aspects available for all CTF traces
107 protected static final @NonNull Collection
<@NonNull ITmfEventAspect
<?
>> CTF_ASPECTS
=
109 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
110 new CtfChannelAspect(),
112 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
113 ITmfEventAspect
.BaseAspects
.CONTENTS
117 * The Ctf clock unique identifier field
119 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
120 private static final int CONFIDENCE
= 10;
121 private static final int MIN_CONFIDENCE
= 1;
123 // -------------------------------------------
125 // -------------------------------------------
127 private final Map
<@NonNull String
, @NonNull CtfTmfEventType
> fContainedEventTypes
=
128 Collections
.synchronizedMap(new HashMap
<>());
130 private final CtfIteratorManager fIteratorManager
= new CtfIteratorManager(this);
132 private final @NonNull CtfTmfEventFactory fEventFactory
;
134 /** Reference to the CTF Trace */
135 private CTFTrace fTrace
;
137 // -------------------------------------------
139 // -------------------------------------------
142 * Default constructor
144 public CtfTmfTrace() {
147 /* Use default event factory */
148 fEventFactory
= CtfTmfEventFactory
.instance();
152 * Constructor for sub-classes to specify their own event factory.
154 * @param eventFactory
155 * The event factory to use to generate trace events
158 protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory
) {
160 fEventFactory
= eventFactory
;
163 // -------------------------------------------
164 // TmfTrace Overrides
165 // -------------------------------------------
170 * The resource associated with this trace
172 * The path to the trace file
174 * The type of events that will be read from this trace
175 * @throws TmfTraceException
176 * If something went wrong while reading the trace
179 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
180 throws TmfTraceException
{
182 * Set the cache size. This has to be done before the call to super()
183 * because the super needs to know the cache size.
187 super.initTrace(resource
, path
, eventType
);
190 this.fTrace
= new CTFTrace(path
);
192 /* Set the start and (current) end times for this trace */
193 ctx
= (CtfTmfContext
) seekEvent(0L);
194 CtfTmfEvent event
= getNext(ctx
);
195 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
196 /* Handle the case where the trace is empty */
197 this.setStartTime(TmfTimestamp
.BIG_BANG
);
199 final ITmfTimestamp curTime
= event
.getTimestamp();
200 this.setStartTime(curTime
);
201 this.setEndTime(curTime
);
204 * Register every event type. When you call getType, it will
205 * register a trace to that type in the TmfEventTypeManager
207 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
208 Set
<@NonNull ITmfEventField
> streamContextNames
= new HashSet
<>();
209 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
210 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
211 if (ctfTmfEventType
== null) {
212 List
<ITmfEventField
> content
= new ArrayList
<>();
214 /* Should only return null the first time */
215 final StructDeclaration fields
= ied
.getFields();
216 if (fields
!= null) {
217 for (String fieldName
: fields
.getFieldsList()) {
218 content
.add(new TmfEventField(checkNotNull(fieldName
), null, null));
222 /* Add stream contexts */
223 final StructDeclaration streamContexts
= ied
.getStream().getEventContextDecl();
224 if (streamContextNames
.isEmpty()) {
225 if (streamContexts
!= null) {
226 for (String fieldName
: streamContexts
.getFieldsList()) {
227 streamContextNames
.add(new TmfEventField(checkNotNull(CtfConstants
.CONTEXT_FIELD_PREFIX
+ fieldName
), null, null));
231 content
.addAll(streamContextNames
);
233 if (!content
.isEmpty()) {
234 ITmfEventField contentTree
= new TmfEventField(
235 ITmfEventField
.ROOT_FIELD_ID
,
237 content
.toArray(new ITmfEventField
[content
.size()]));
239 ctfTmfEventType
= new CtfTmfEventType(checkNotNull(ied
.getName()), contentTree
);
240 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
246 } catch (final CTFException e
) {
248 * If it failed at the init(), we can assume it's because the file
249 * was not found or was not recognized as a CTF trace. Throw into
250 * the new type of exception expected by the rest of TMF.
252 throw new TmfTraceException(e
.getMessage(), e
);
257 public synchronized void dispose() {
258 fIteratorManager
.dispose();
259 if (fTrace
!= null) {
268 * The default implementation of a CTF trace.
270 * Firstly a weak validation of the metadata is done to determine if the
271 * path is actually for a CTF trace. After that a full validation is done.
273 * If the weak and full validation are successful the confidence is set
276 * If the weak validation was successful, but the full validation fails
277 * a TraceValidationStatus with severity warning and confidence of 1 is
280 * If both weak and full validation fails an error status is returned.
283 public IStatus
validate(final IProject project
, final String path
) {
284 boolean isMetadataFile
= false;
286 isMetadataFile
= Metadata
.preValidate(path
);
287 } catch (final CTFException e
) {
288 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
291 if (isMetadataFile
) {
292 // Trace is pre-validated, continue will full validation
294 final CTFTrace trace
= new CTFTrace(path
);
295 if (!trace
.majorIsSet()) {
296 if (isMetadataFile
) {
297 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
299 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
302 // Validate using reader initialization
303 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
305 // Trace is validated, return with confidence
306 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
308 } catch (final CTFException
| BufferOverflowException e
) {
309 // return warning since it's a CTF trace but with errors in it
310 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
313 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
317 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
322 * Method getCurrentLocation. This is not applicable in CTF
324 * @return null, since the trace has no knowledge of the current location
325 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
328 public ITmfLocation
getCurrentLocation() {
333 public double getLocationRatio(ITmfLocation location
) {
334 final CtfLocation curLocation
= (CtfLocation
) location
;
335 final long startTime
= getStartTime().getValue();
336 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
337 final double total
= getEndTime().getValue() - startTime
;
338 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
346 * @return ITmfContext
349 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
350 CtfLocation currentLocation
= (CtfLocation
) location
;
351 CtfTmfContext context
= new CtfTmfContext(this);
352 if (fTrace
== null) {
353 context
.setLocation(null);
354 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
358 * The rank is set to 0 if the iterator seeks the beginning. If not, it
359 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
362 if (currentLocation
== null) {
363 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
366 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
368 /* This will seek and update the location after the seek */
369 context
.setLocation(currentLocation
);
374 public synchronized ITmfContext
seekEvent(double ratio
) {
375 CtfTmfContext context
= new CtfTmfContext(this);
376 if (fTrace
== null) {
377 context
.setLocation(null);
378 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
381 final long end
= getEndTime().getValue();
382 final long start
= getStartTime().getValue();
383 final long diff
= end
- start
;
384 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
385 context
.seek(ratioTs
);
386 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
391 * Method readNextEvent.
395 * @return CtfTmfEvent
396 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
399 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
400 if (fTrace
== null) {
403 CtfTmfEvent event
= null;
404 if (context
instanceof CtfTmfContext
) {
405 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
408 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
409 event
= ctfContext
.getCurrentEvent();
412 updateAttributes(context
, event
);
413 ctfContext
.advance();
414 ctfContext
.increaseRank();
422 * Ctf traces have a clock with a unique uuid that will be used to identify
423 * the host. Traces with the same clock uuid will be known to have been made
424 * on the same machine.
426 * Note: uuid is an optional field, it may not be there for a clock.
429 public String
getHostId() {
430 CTFClock clock
= fTrace
.getClock();
432 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
433 if (clockHost
!= null) {
437 return super.getHostId();
441 * Get the CTF environment variables defined in this CTF trace, in <name,
442 * value> form. This comes from the trace's CTF metadata.
444 * @return The CTF environment
446 public Map
<String
, String
> getEnvironment() {
447 return fTrace
.getEnvironment();
450 // -------------------------------------------
451 // ITmfPropertiesProvider
452 // -------------------------------------------
458 public Map
<String
, String
> getProperties() {
459 Map
<String
, String
> properties
= new HashMap
<>();
460 properties
.putAll(fTrace
.getEnvironment());
461 properties
.put(CLOCK_OFFSET
, Long
.toUnsignedString(fTrace
.getOffset()));
462 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
466 // -------------------------------------------
468 // -------------------------------------------
471 * gets the clock offset
473 * @return the clock offset in ns
475 public long getOffset() {
476 if (fTrace
!= null) {
477 return fTrace
.getOffset();
483 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
487 * The timestamp in cycles
488 * @return The timestamp in nanoseconds
490 public long timestampCyclesToNanos(long cycles
) {
491 return fTrace
.timestampCyclesToNanos(cycles
);
495 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
499 * The timestamp in nanoseconds
500 * @return The timestamp in cycles
502 public long timestampNanoToCycles(long nanos
) {
503 return fTrace
.timestampNanoToCycles(nanos
);
507 * Gets the list of declared events
510 public Set
<@NonNull CtfTmfEventType
> getContainedEventTypes() {
511 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
515 * Register an event type to this trace.
517 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
519 * FIXME This could probably be made cleaner?
522 * The event type to register
524 public void registerEventType(CtfTmfEventType eventType
) {
525 fContainedEventTypes
.put(eventType
.getName(), eventType
);
528 // -------------------------------------------
530 // -------------------------------------------
533 public CtfTmfEvent
parseEvent(ITmfContext context
) {
534 CtfTmfEvent event
= null;
535 if (context
instanceof CtfTmfContext
) {
536 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
537 event
= getNext(tmpContext
);
543 * Sets the cache size for a CtfTmfTrace.
545 protected void setCacheSize() {
546 setCacheSize(DEFAULT_CACHE_SIZE
);
549 // -------------------------------------------
550 // CtfIterator factory methods
551 // -------------------------------------------
554 * Get the event factory for this trace to generate new events for it.
556 * @return The event factory
559 public @NonNull CtfTmfEventFactory
getEventFactory() {
560 return fEventFactory
;
564 * Get an iterator to the trace
566 * @return an iterator to the trace
568 public ITmfContext
createIterator() {
570 return new CtfIterator(fTrace
, this);
571 } catch (CTFException e
) {
572 Activator
.getDefault().logError(e
.getMessage(), e
);
578 * Get an iterator to the trace, , which will initially point to the given
581 * @param ctfLocationData
582 * The initial timestamp the iterator will be pointing to
585 * @return The new iterator
587 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
589 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
590 } catch (CTFException e
) {
591 Activator
.getDefault().logError(e
.getMessage(), e
);
597 * Create the 'CtfIterator' object from a CtfTmfContext.
600 * The iterator will initially be pointing to this context
601 * @return A new CtfIterator object
604 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
605 return fIteratorManager
.getIterator(context
);
609 * Dispose an iterator that was create with
610 * {@link #createIteratorFromContext}
613 * The last context that was pointed to by the iterator (this is
614 * the 'key' to find the correct iterator to dispose).
617 public void disposeContext(CtfTmfContext context
) {
618 fIteratorManager
.removeIterator(context
);
621 // ------------------------------------------------------------------------
622 // Timestamp transformation functions
623 // ------------------------------------------------------------------------
629 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
630 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
633 private static int fCheckpointSize
= -1;
636 public synchronized int getCheckpointSize() {
637 if (fCheckpointSize
== -1) {
638 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
639 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
642 fCheckpointSize
= b
.position();
645 return fCheckpointSize
;
649 protected ITmfTraceIndexer
createIndexer(int interval
) {
650 return new TmfBTreeTraceIndexer(this, interval
);
654 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
655 return new CtfLocation(bufferIn
);
659 public boolean isComplete() {
660 if (getResource() == null) {
666 String sessionName
= null;
668 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
669 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
670 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
671 } catch (CoreException e
) {
672 Activator
.getDefault().logError(e
.getMessage(), e
);
673 // Something happened to the resource, assume we won't get any more
677 return host
== null || port
== null || sessionName
== null;
681 public void setComplete(final boolean isComplete
) {
682 super.setComplete(isComplete
);
685 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
686 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
687 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
689 } catch (CoreException e
) {
690 Activator
.getDefault().logError(e
.getMessage(), e
);