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
.tracecompass
.ctf
.core
.CTFException
;
36 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFClock
;
37 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
38 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
39 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
40 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
41 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.Metadata
;
42 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.Activator
;
43 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
44 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.trace
.iterator
.CtfIteratorManager
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.event
.aspect
.ITmfEventAspect
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceProperties
;
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 ITmfTraceProperties
, ITmfPersistentlyIndexable
,
85 ITmfTraceWithPreDefinedEvents
{
87 // -------------------------------------------
89 // -------------------------------------------
92 * Default cache size for CTF traces
94 protected static final int DEFAULT_CACHE_SIZE
= 50000;
97 * Event aspects available for all CTF traces
100 protected static final @NonNull Collection
<@NonNull ITmfEventAspect
> CTF_ASPECTS
=
101 checkNotNull(ImmutableList
.of(
102 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
103 new CtfChannelAspect(),
105 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
106 ITmfEventAspect
.BaseAspects
.CONTENTS
110 * The Ctf clock unique identifier field
112 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
113 private static final int CONFIDENCE
= 10;
114 private static final int MIN_CONFIDENCE
= 1;
116 // -------------------------------------------
118 // -------------------------------------------
120 private final Map
<@NonNull String
, @NonNull CtfTmfEventType
> fContainedEventTypes
=
121 Collections
.synchronizedMap(new HashMap
<>());
123 private final CtfIteratorManager fIteratorManager
= new CtfIteratorManager(this);
125 private final @NonNull CtfTmfEventFactory fEventFactory
;
127 /** Reference to the CTF Trace */
128 private CTFTrace fTrace
;
130 // -------------------------------------------
132 // -------------------------------------------
135 * Default constructor
137 public CtfTmfTrace() {
140 /* Use default event factory */
141 fEventFactory
= CtfTmfEventFactory
.instance();
145 * Constructor for sub-classes to specify their own event factory.
147 * @param eventFactory
148 * The event factory to use to generate trace events
151 protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory
) {
153 fEventFactory
= eventFactory
;
156 // -------------------------------------------
157 // TmfTrace Overrides
158 // -------------------------------------------
163 * The resource associated with this trace
165 * The path to the trace file
167 * The type of events that will be read from this trace
168 * @throws TmfTraceException
169 * If something went wrong while reading the trace
172 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
173 throws TmfTraceException
{
175 * Set the cache size. This has to be done before the call to super()
176 * because the super needs to know the cache size.
180 super.initTrace(resource
, path
, eventType
);
183 this.fTrace
= new CTFTrace(path
);
185 /* Set the start and (current) end times for this trace */
186 ctx
= (CtfTmfContext
) seekEvent(0L);
187 CtfTmfEvent event
= getNext(ctx
);
188 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
189 /* Handle the case where the trace is empty */
190 this.setStartTime(TmfTimestamp
.BIG_BANG
);
192 final ITmfTimestamp curTime
= event
.getTimestamp();
193 this.setStartTime(curTime
);
194 this.setEndTime(curTime
);
197 * Register every event type. When you call getType, it will
198 * register a trace to that type in the TmfEventTypeManager
200 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
201 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
202 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
203 if (ctfTmfEventType
== null) {
204 List
<ITmfEventField
> content
= new ArrayList
<>();
205 /* Should only return null the first time */
206 final StructDeclaration fields
= ied
.getFields();
207 if (fields
!= null) {
208 for (String fieldName
: fields
.getFieldsList()) {
209 content
.add(new TmfEventField(checkNotNull(fieldName
), null, null));
211 ITmfEventField contentTree
= new TmfEventField(
212 ITmfEventField
.ROOT_FIELD_ID
,
214 content
.toArray(new ITmfEventField
[content
.size()]));
216 ctfTmfEventType
= new CtfTmfEventType(checkNotNull(ied
.getName()), contentTree
);
217 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
223 } catch (final CTFException e
) {
225 * If it failed at the init(), we can assume it's because the file
226 * was not found or was not recognized as a CTF trace. Throw into
227 * the new type of exception expected by the rest of TMF.
229 throw new TmfTraceException(e
.getMessage(), e
);
234 public synchronized void dispose() {
235 fIteratorManager
.dispose();
236 if (fTrace
!= null) {
245 * The default implementation of a CTF trace.
247 * Firstly a weak validation of the metadata is done to determine if the
248 * path is actually for a CTF trace. After that a full validation is done.
250 * If the weak and full validation are successful the confidence is set
253 * If the weak validation was successful, but the full validation fails
254 * a TraceValidationStatus with severity warning and confidence of 1 is
257 * If both weak and full validation fails an error status is returned.
260 public IStatus
validate(final IProject project
, final String path
) {
261 boolean isMetadataFile
= false;
263 isMetadataFile
= Metadata
.preValidate(path
);
264 } catch (final CTFException e
) {
265 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
268 if (isMetadataFile
) {
269 // Trace is pre-validated, continue will full validation
271 final CTFTrace trace
= new CTFTrace(path
);
272 if (!trace
.majorIsSet()) {
273 if (isMetadataFile
) {
274 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
276 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
279 // Validate using reader initialization
280 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
282 // Trace is validated, return with confidence
283 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
285 } catch (final CTFException
| BufferOverflowException e
) {
286 // return warning since it's a CTF trace but with errors in it
287 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
290 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
294 public Iterable
<ITmfEventAspect
> getEventAspects() {
299 * Method getCurrentLocation. This is not applicable in CTF
301 * @return null, since the trace has no knowledge of the current location
302 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
305 public ITmfLocation
getCurrentLocation() {
310 public double getLocationRatio(ITmfLocation location
) {
311 final CtfLocation curLocation
= (CtfLocation
) location
;
312 final long startTime
= getStartTime().getValue();
313 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
314 final double total
= getEndTime().getValue() - startTime
;
315 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
323 * @return ITmfContext
326 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
327 CtfLocation currentLocation
= (CtfLocation
) location
;
328 CtfTmfContext context
= new CtfTmfContext(this);
329 if (fTrace
== null) {
330 context
.setLocation(null);
331 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
335 * The rank is set to 0 if the iterator seeks the beginning. If not, it
336 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
339 if (currentLocation
== null) {
340 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
343 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
345 /* This will seek and update the location after the seek */
346 context
.setLocation(currentLocation
);
351 public synchronized ITmfContext
seekEvent(double ratio
) {
352 CtfTmfContext context
= new CtfTmfContext(this);
353 if (fTrace
== null) {
354 context
.setLocation(null);
355 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
358 final long end
= getEndTime().getValue();
359 final long start
= getStartTime().getValue();
360 final long diff
= end
- start
;
361 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
362 context
.seek(ratioTs
);
363 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
368 * Method readNextEvent.
372 * @return CtfTmfEvent
373 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
376 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
377 if (fTrace
== null) {
380 CtfTmfEvent event
= null;
381 if (context
instanceof CtfTmfContext
) {
382 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
385 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
386 event
= ctfContext
.getCurrentEvent();
389 updateAttributes(context
, event
);
390 ctfContext
.advance();
391 ctfContext
.increaseRank();
399 * Ctf traces have a clock with a unique uuid that will be used to identify
400 * the host. Traces with the same clock uuid will be known to have been made
401 * on the same machine.
403 * Note: uuid is an optional field, it may not be there for a clock.
406 public String
getHostId() {
407 CTFClock clock
= fTrace
.getClock();
409 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
410 if (clockHost
!= null) {
414 return super.getHostId();
418 * Get the CTF environment variables defined in this CTF trace, in <name,
419 * value> form. This comes from the trace's CTF metadata.
421 * @return The CTF environment
423 public Map
<String
, String
> getEnvironment() {
424 return fTrace
.getEnvironment();
427 // -------------------------------------------
428 // ITmfTraceProperties
429 // -------------------------------------------
432 public Map
<String
, String
> getTraceProperties() {
433 Map
<String
, String
> properties
= new HashMap
<>();
434 properties
.putAll(fTrace
.getEnvironment());
435 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
439 // -------------------------------------------
441 // -------------------------------------------
444 * gets the clock offset
446 * @return the clock offset in ns
448 public long getOffset() {
449 if (fTrace
!= null) {
450 return fTrace
.getOffset();
456 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
460 * The timestamp in cycles
461 * @return The timestamp in nanoseconds
463 public long timestampCyclesToNanos(long cycles
) {
464 return fTrace
.timestampCyclesToNanos(cycles
);
468 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
472 * The timestamp in nanoseconds
473 * @return The timestamp in cycles
475 public long timestampNanoToCycles(long nanos
) {
476 return fTrace
.timestampNanoToCycles(nanos
);
480 * Gets the list of declared events
483 public Set
<@NonNull CtfTmfEventType
> getContainedEventTypes() {
484 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
488 * Register an event type to this trace.
490 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
492 * FIXME This could probably be made cleaner?
495 * The event type to register
497 public void registerEventType(CtfTmfEventType eventType
) {
498 fContainedEventTypes
.put(eventType
.getName(), eventType
);
501 // -------------------------------------------
503 // -------------------------------------------
506 public CtfTmfEvent
parseEvent(ITmfContext context
) {
507 CtfTmfEvent event
= null;
508 if (context
instanceof CtfTmfContext
) {
509 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
510 event
= getNext(tmpContext
);
516 * Sets the cache size for a CtfTmfTrace.
518 protected void setCacheSize() {
519 setCacheSize(DEFAULT_CACHE_SIZE
);
522 // -------------------------------------------
523 // CtfIterator factory methods
524 // -------------------------------------------
527 * Get the event factory for this trace to generate new events for it.
529 * @return The event factory
532 public @NonNull CtfTmfEventFactory
getEventFactory() {
533 return fEventFactory
;
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
);