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
.project
.model
.ITmfPropertiesProvider
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
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 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
208 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
209 if (ctfTmfEventType
== null) {
210 List
<ITmfEventField
> content
= new ArrayList
<>();
211 /* Should only return null the first time */
212 final StructDeclaration fields
= ied
.getFields();
213 if (fields
!= null) {
214 for (String fieldName
: fields
.getFieldsList()) {
215 content
.add(new TmfEventField(checkNotNull(fieldName
), null, null));
217 ITmfEventField contentTree
= new TmfEventField(
218 ITmfEventField
.ROOT_FIELD_ID
,
220 content
.toArray(new ITmfEventField
[content
.size()]));
222 ctfTmfEventType
= new CtfTmfEventType(checkNotNull(ied
.getName()), contentTree
);
223 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
229 } catch (final CTFException e
) {
231 * If it failed at the init(), we can assume it's because the file
232 * was not found or was not recognized as a CTF trace. Throw into
233 * the new type of exception expected by the rest of TMF.
235 throw new TmfTraceException(e
.getMessage(), e
);
240 public synchronized void dispose() {
241 fIteratorManager
.dispose();
242 if (fTrace
!= null) {
251 * The default implementation of a CTF trace.
253 * Firstly a weak validation of the metadata is done to determine if the
254 * path is actually for a CTF trace. After that a full validation is done.
256 * If the weak and full validation are successful the confidence is set
259 * If the weak validation was successful, but the full validation fails
260 * a TraceValidationStatus with severity warning and confidence of 1 is
263 * If both weak and full validation fails an error status is returned.
266 public IStatus
validate(final IProject project
, final String path
) {
267 boolean isMetadataFile
= false;
269 isMetadataFile
= Metadata
.preValidate(path
);
270 } catch (final CTFException e
) {
271 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
274 if (isMetadataFile
) {
275 // Trace is pre-validated, continue will full validation
277 final CTFTrace trace
= new CTFTrace(path
);
278 if (!trace
.majorIsSet()) {
279 if (isMetadataFile
) {
280 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
282 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
285 // Validate using reader initialization
286 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
288 // Trace is validated, return with confidence
289 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
291 } catch (final CTFException
| BufferOverflowException e
) {
292 // return warning since it's a CTF trace but with errors in it
293 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
296 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
300 public Iterable
<ITmfEventAspect
> getEventAspects() {
305 * Method getCurrentLocation. This is not applicable in CTF
307 * @return null, since the trace has no knowledge of the current location
308 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
311 public ITmfLocation
getCurrentLocation() {
316 public double getLocationRatio(ITmfLocation location
) {
317 final CtfLocation curLocation
= (CtfLocation
) location
;
318 final long startTime
= getStartTime().getValue();
319 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
320 final double total
= getEndTime().getValue() - startTime
;
321 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
329 * @return ITmfContext
332 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
333 CtfLocation currentLocation
= (CtfLocation
) location
;
334 CtfTmfContext context
= new CtfTmfContext(this);
335 if (fTrace
== null) {
336 context
.setLocation(null);
337 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
341 * The rank is set to 0 if the iterator seeks the beginning. If not, it
342 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
345 if (currentLocation
== null) {
346 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
349 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
351 /* This will seek and update the location after the seek */
352 context
.setLocation(currentLocation
);
357 public synchronized ITmfContext
seekEvent(double ratio
) {
358 CtfTmfContext context
= new CtfTmfContext(this);
359 if (fTrace
== null) {
360 context
.setLocation(null);
361 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
364 final long end
= getEndTime().getValue();
365 final long start
= getStartTime().getValue();
366 final long diff
= end
- start
;
367 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
368 context
.seek(ratioTs
);
369 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
374 * Method readNextEvent.
378 * @return CtfTmfEvent
379 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
382 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
383 if (fTrace
== null) {
386 CtfTmfEvent event
= null;
387 if (context
instanceof CtfTmfContext
) {
388 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
391 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
392 event
= ctfContext
.getCurrentEvent();
395 updateAttributes(context
, event
);
396 ctfContext
.advance();
397 ctfContext
.increaseRank();
405 * Ctf traces have a clock with a unique uuid that will be used to identify
406 * the host. Traces with the same clock uuid will be known to have been made
407 * on the same machine.
409 * Note: uuid is an optional field, it may not be there for a clock.
412 public String
getHostId() {
413 CTFClock clock
= fTrace
.getClock();
415 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
416 if (clockHost
!= null) {
420 return super.getHostId();
424 * Get the CTF environment variables defined in this CTF trace, in <name,
425 * value> form. This comes from the trace's CTF metadata.
427 * @return The CTF environment
429 public Map
<String
, String
> getEnvironment() {
430 return fTrace
.getEnvironment();
433 // -------------------------------------------
434 // ITmfPropertiesProvider
435 // -------------------------------------------
441 public Map
<String
, String
> getProperties() {
442 Map
<String
, String
> properties
= new HashMap
<>();
443 properties
.putAll(fTrace
.getEnvironment());
444 properties
.put(CLOCK_OFFSET
, Long
.toUnsignedString(fTrace
.getOffset()));
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
<@NonNull 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 the event factory for this trace to generate new events for it.
539 * @return The event factory
542 public @NonNull CtfTmfEventFactory
getEventFactory() {
543 return fEventFactory
;
547 * Get an iterator to the trace
549 * @return an iterator to the trace
551 public ITmfContext
createIterator() {
553 return new CtfIterator(fTrace
, this);
554 } catch (CTFException e
) {
555 Activator
.getDefault().logError(e
.getMessage(), e
);
561 * Get an iterator to the trace, , which will initially point to the given
564 * @param ctfLocationData
565 * The initial timestamp the iterator will be pointing to
568 * @return The new iterator
570 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
572 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
573 } catch (CTFException e
) {
574 Activator
.getDefault().logError(e
.getMessage(), e
);
580 * Create the 'CtfIterator' object from a CtfTmfContext.
583 * The iterator will initially be pointing to this context
584 * @return A new CtfIterator object
587 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
588 return fIteratorManager
.getIterator(context
);
592 * Dispose an iterator that was create with
593 * {@link #createIteratorFromContext}
596 * The last context that was pointed to by the iterator (this is
597 * the 'key' to find the correct iterator to dispose).
600 public void disposeContext(CtfTmfContext context
) {
601 fIteratorManager
.removeIterator(context
);
604 // ------------------------------------------------------------------------
605 // Timestamp transformation functions
606 // ------------------------------------------------------------------------
612 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
613 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
616 private static int fCheckpointSize
= -1;
619 public synchronized int getCheckpointSize() {
620 if (fCheckpointSize
== -1) {
621 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
622 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
625 fCheckpointSize
= b
.position();
628 return fCheckpointSize
;
632 protected ITmfTraceIndexer
createIndexer(int interval
) {
633 return new TmfBTreeTraceIndexer(this, interval
);
637 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
638 return new CtfLocation(bufferIn
);
642 public boolean isComplete() {
643 if (getResource() == null) {
649 String sessionName
= null;
651 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
652 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
653 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
654 } catch (CoreException e
) {
655 Activator
.getDefault().logError(e
.getMessage(), e
);
656 // Something happened to the resource, assume we won't get any more
660 return host
== null || port
== null || sessionName
== null;
664 public void setComplete(final boolean isComplete
) {
665 super.setComplete(isComplete
);
668 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
669 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
670 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
672 } catch (CoreException e
) {
673 Activator
.getDefault().logError(e
.getMessage(), e
);