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
.TmfTimestamp
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceWithPreDefinedEvents
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
63 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
64 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
65 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocation
;
66 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
67 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfTmfContext
;
68 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
69 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventFactory
;
70 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventType
;
71 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfChannelAspect
;
72 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfCpuAspect
;
74 import com
.google
.common
.collect
.ImmutableList
;
75 import com
.google
.common
.collect
.ImmutableSet
;
78 * The CTf trace handler
81 * @author Matthew khouzam
83 public class CtfTmfTrace
extends TmfTrace
84 implements ITmfPropertiesProvider
, ITmfPersistentlyIndexable
,
85 ITmfTraceWithPreDefinedEvents
{
87 // -------------------------------------------
89 // -------------------------------------------
92 * Clock offset property
95 public static final String CLOCK_OFFSET
= "clock_offset"; //$NON-NLS-1$
98 * Default cache size for CTF traces
100 protected static final int DEFAULT_CACHE_SIZE
= 50000;
103 * Event aspects available for all CTF traces
106 protected static final @NonNull Collection
<@NonNull ITmfEventAspect
<?
>> CTF_ASPECTS
=
108 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
109 new CtfChannelAspect(),
111 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
112 ITmfEventAspect
.BaseAspects
.CONTENTS
116 * The Ctf clock unique identifier field
118 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
119 private static final int CONFIDENCE
= 10;
120 private static final int MIN_CONFIDENCE
= 1;
122 // -------------------------------------------
124 // -------------------------------------------
126 private final Map
<@NonNull String
, @NonNull CtfTmfEventType
> fContainedEventTypes
=
127 Collections
.synchronizedMap(new HashMap
<>());
129 private final CtfIteratorManager fIteratorManager
= new CtfIteratorManager(this);
131 private final @NonNull CtfTmfEventFactory fEventFactory
;
133 /** Reference to the CTF Trace */
134 private CTFTrace fTrace
;
136 // -------------------------------------------
138 // -------------------------------------------
141 * Default constructor
143 public CtfTmfTrace() {
146 /* Use default event factory */
147 fEventFactory
= CtfTmfEventFactory
.instance();
151 * Constructor for sub-classes to specify their own event factory.
153 * @param eventFactory
154 * The event factory to use to generate trace events
157 protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory
) {
159 fEventFactory
= eventFactory
;
162 // -------------------------------------------
163 // TmfTrace Overrides
164 // -------------------------------------------
169 * The resource associated with this trace
171 * The path to the trace file
173 * The type of events that will be read from this trace
174 * @throws TmfTraceException
175 * If something went wrong while reading the trace
178 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
179 throws TmfTraceException
{
181 * Set the cache size. This has to be done before the call to super()
182 * because the super needs to know the cache size.
186 super.initTrace(resource
, path
, eventType
);
189 this.fTrace
= new CTFTrace(path
);
191 /* Set the start and (current) end times for this trace */
192 ctx
= (CtfTmfContext
) seekEvent(0L);
193 CtfTmfEvent event
= getNext(ctx
);
194 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
195 /* Handle the case where the trace is empty */
196 this.setStartTime(TmfTimestamp
.BIG_BANG
);
198 final ITmfTimestamp curTime
= event
.getTimestamp();
199 this.setStartTime(curTime
);
200 this.setEndTime(curTime
);
203 * Register every event type. When you call getType, it will
204 * register a trace to that type in the TmfEventTypeManager
206 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
207 Set
<@NonNull ITmfEventField
> streamContextNames
= new HashSet
<>();
208 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
209 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
210 if (ctfTmfEventType
== null) {
211 List
<ITmfEventField
> content
= new ArrayList
<>();
213 /* Should only return null the first time */
214 final StructDeclaration fields
= ied
.getFields();
215 if (fields
!= null) {
216 for (String fieldName
: fields
.getFieldsList()) {
217 content
.add(new TmfEventField(checkNotNull(fieldName
), null, null));
221 /* Add stream contexts */
222 final StructDeclaration streamContexts
= ied
.getStream().getEventContextDecl();
223 if (streamContextNames
.isEmpty()) {
224 if (streamContexts
!= null) {
225 for (String fieldName
: streamContexts
.getFieldsList()) {
226 streamContextNames
.add(new TmfEventField(checkNotNull(CtfConstants
.CONTEXT_FIELD_PREFIX
+ fieldName
), null, null));
230 content
.addAll(streamContextNames
);
232 if (!content
.isEmpty()) {
233 ITmfEventField contentTree
= new TmfEventField(
234 ITmfEventField
.ROOT_FIELD_ID
,
236 content
.toArray(new ITmfEventField
[content
.size()]));
238 ctfTmfEventType
= new CtfTmfEventType(checkNotNull(ied
.getName()), contentTree
);
239 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
245 } catch (final CTFException e
) {
247 * If it failed at the init(), we can assume it's because the file
248 * was not found or was not recognized as a CTF trace. Throw into
249 * the new type of exception expected by the rest of TMF.
251 throw new TmfTraceException(e
.getMessage(), e
);
256 public synchronized void dispose() {
257 fIteratorManager
.dispose();
258 if (fTrace
!= null) {
267 * The default implementation of a CTF trace.
269 * Firstly a weak validation of the metadata is done to determine if the
270 * path is actually for a CTF trace. After that a full validation is done.
272 * If the weak and full validation are successful the confidence is set
275 * If the weak validation was successful, but the full validation fails
276 * a TraceValidationStatus with severity warning and confidence of 1 is
279 * If both weak and full validation fails an error status is returned.
282 public IStatus
validate(final IProject project
, final String path
) {
283 boolean isMetadataFile
= false;
285 isMetadataFile
= Metadata
.preValidate(path
);
286 } catch (final CTFException e
) {
287 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
290 if (isMetadataFile
) {
291 // Trace is pre-validated, continue will full validation
293 final CTFTrace trace
= new CTFTrace(path
);
294 if (!trace
.majorIsSet()) {
295 if (isMetadataFile
) {
296 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
298 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
301 // Validate using reader initialization
302 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
304 // Trace is validated, return with confidence
305 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
307 } catch (final CTFException
| BufferOverflowException e
) {
308 // return warning since it's a CTF trace but with errors in it
309 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
312 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
316 public Iterable
<ITmfEventAspect
<?
>> getEventAspects() {
321 * Method getCurrentLocation. This is not applicable in CTF
323 * @return null, since the trace has no knowledge of the current location
324 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
327 public ITmfLocation
getCurrentLocation() {
332 public double getLocationRatio(ITmfLocation location
) {
333 final CtfLocation curLocation
= (CtfLocation
) location
;
334 final long startTime
= getStartTime().getValue();
335 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
336 final double total
= getEndTime().getValue() - startTime
;
337 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
345 * @return ITmfContext
348 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
349 CtfLocation currentLocation
= (CtfLocation
) location
;
350 CtfTmfContext context
= new CtfTmfContext(this);
351 if (fTrace
== null) {
352 context
.setLocation(null);
353 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
357 * The rank is set to 0 if the iterator seeks the beginning. If not, it
358 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
361 if (currentLocation
== null) {
362 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
365 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
367 /* This will seek and update the location after the seek */
368 context
.setLocation(currentLocation
);
373 public synchronized ITmfContext
seekEvent(double ratio
) {
374 CtfTmfContext context
= new CtfTmfContext(this);
375 if (fTrace
== null) {
376 context
.setLocation(null);
377 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
380 final long end
= getEndTime().getValue();
381 final long start
= getStartTime().getValue();
382 final long diff
= end
- start
;
383 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
384 context
.seek(ratioTs
);
385 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
390 * Method readNextEvent.
394 * @return CtfTmfEvent
395 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
398 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
399 if (fTrace
== null) {
402 CtfTmfEvent event
= null;
403 if (context
instanceof CtfTmfContext
) {
404 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
407 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
408 event
= ctfContext
.getCurrentEvent();
411 updateAttributes(context
, event
);
412 ctfContext
.advance();
413 ctfContext
.increaseRank();
421 * Ctf traces have a clock with a unique uuid that will be used to identify
422 * the host. Traces with the same clock uuid will be known to have been made
423 * on the same machine.
425 * Note: uuid is an optional field, it may not be there for a clock.
428 public String
getHostId() {
429 CTFClock clock
= fTrace
.getClock();
431 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
432 if (clockHost
!= null) {
436 return super.getHostId();
440 * Get the CTF environment variables defined in this CTF trace, in <name,
441 * value> form. This comes from the trace's CTF metadata.
443 * @return The CTF environment
445 public Map
<String
, String
> getEnvironment() {
446 return fTrace
.getEnvironment();
449 // -------------------------------------------
450 // ITmfPropertiesProvider
451 // -------------------------------------------
457 public Map
<String
, String
> getProperties() {
458 Map
<String
, String
> properties
= new HashMap
<>();
459 properties
.putAll(fTrace
.getEnvironment());
460 properties
.put(CLOCK_OFFSET
, Long
.toUnsignedString(fTrace
.getOffset()));
461 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
465 // -------------------------------------------
467 // -------------------------------------------
470 * gets the clock offset
472 * @return the clock offset in ns
474 public long getOffset() {
475 if (fTrace
!= null) {
476 return fTrace
.getOffset();
482 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
486 * The timestamp in cycles
487 * @return The timestamp in nanoseconds
489 public long timestampCyclesToNanos(long cycles
) {
490 return fTrace
.timestampCyclesToNanos(cycles
);
494 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
498 * The timestamp in nanoseconds
499 * @return The timestamp in cycles
501 public long timestampNanoToCycles(long nanos
) {
502 return fTrace
.timestampNanoToCycles(nanos
);
506 * Gets the list of declared events
509 public Set
<@NonNull CtfTmfEventType
> getContainedEventTypes() {
510 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
514 * Register an event type to this trace.
516 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
518 * FIXME This could probably be made cleaner?
521 * The event type to register
523 public void registerEventType(CtfTmfEventType eventType
) {
524 fContainedEventTypes
.put(eventType
.getName(), eventType
);
527 // -------------------------------------------
529 // -------------------------------------------
532 public CtfTmfEvent
parseEvent(ITmfContext context
) {
533 CtfTmfEvent event
= null;
534 if (context
instanceof CtfTmfContext
) {
535 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
536 event
= getNext(tmpContext
);
542 * Sets the cache size for a CtfTmfTrace.
544 protected void setCacheSize() {
545 setCacheSize(DEFAULT_CACHE_SIZE
);
548 // -------------------------------------------
549 // CtfIterator factory methods
550 // -------------------------------------------
553 * Get the event factory for this trace to generate new events for it.
555 * @return The event factory
558 public @NonNull CtfTmfEventFactory
getEventFactory() {
559 return fEventFactory
;
563 * Get an iterator to the trace
565 * @return an iterator to the trace
567 public ITmfContext
createIterator() {
569 return new CtfIterator(fTrace
, this);
570 } catch (CTFException e
) {
571 Activator
.getDefault().logError(e
.getMessage(), e
);
577 * Get an iterator to the trace, , which will initially point to the given
580 * @param ctfLocationData
581 * The initial timestamp the iterator will be pointing to
584 * @return The new iterator
586 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
588 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
589 } catch (CTFException e
) {
590 Activator
.getDefault().logError(e
.getMessage(), e
);
596 * Create the 'CtfIterator' object from a CtfTmfContext.
599 * The iterator will initially be pointing to this context
600 * @return A new CtfIterator object
603 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
604 return fIteratorManager
.getIterator(context
);
608 * Dispose an iterator that was create with
609 * {@link #createIteratorFromContext}
612 * The last context that was pointed to by the iterator (this is
613 * the 'key' to find the correct iterator to dispose).
616 public void disposeContext(CtfTmfContext context
) {
617 fIteratorManager
.removeIterator(context
);
620 // ------------------------------------------------------------------------
621 // Timestamp transformation functions
622 // ------------------------------------------------------------------------
628 public @NonNull ITmfTimestamp
createTimestamp(long ts
) {
629 return TmfTimestamp
.fromNanos(getTimestampTransform().transform(ts
));
632 private static int fCheckpointSize
= -1;
635 public synchronized int getCheckpointSize() {
636 if (fCheckpointSize
== -1) {
637 TmfCheckpoint c
= new TmfCheckpoint(TmfTimestamp
.fromNanos(0), new CtfLocation(0, 0), 0);
638 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
641 fCheckpointSize
= b
.position();
644 return fCheckpointSize
;
648 protected ITmfTraceIndexer
createIndexer(int interval
) {
649 return new TmfBTreeTraceIndexer(this, interval
);
653 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
654 return new CtfLocation(bufferIn
);
658 public boolean isComplete() {
659 if (getResource() == null) {
665 String sessionName
= null;
667 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
668 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
669 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
670 } catch (CoreException e
) {
671 Activator
.getDefault().logError(e
.getMessage(), e
);
672 // Something happened to the resource, assume we won't get any more
676 return host
== null || port
== null || sessionName
== null;
680 public void setComplete(final boolean isComplete
) {
681 super.setComplete(isComplete
);
684 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
685 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
686 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
688 } catch (CoreException e
) {
689 Activator
.getDefault().logError(e
.getMessage(), e
);