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
.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
.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
.ITmfTraceProperties
;
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
.CtfTmfEventType
;
71 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfChannelAspect
;
72 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfCpuAspect
;
73 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.lookup
.CtfTmfCallsite
;
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 ITmfTraceProperties
, ITmfPersistentlyIndexable
,
86 ITmfTraceWithPreDefinedEvents
, AutoCloseable
{
88 // -------------------------------------------
90 // -------------------------------------------
93 * Default cache size for CTF traces
95 protected static final int DEFAULT_CACHE_SIZE
= 50000;
98 * Event aspects available for all CTF traces
101 protected static final @NonNull Collection
<ITmfEventAspect
> CTF_ASPECTS
=
102 checkNotNull(ImmutableList
.of(
103 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
104 new CtfChannelAspect(),
106 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
107 ITmfEventAspect
.BaseAspects
.CONTENTS
111 * The Ctf clock unique identifier field
113 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
114 private static final int CONFIDENCE
= 10;
115 private static final int MIN_CONFIDENCE
= 1;
117 // -------------------------------------------
119 // -------------------------------------------
121 private final Map
<String
, CtfTmfEventType
> fContainedEventTypes
=
122 Collections
.synchronizedMap(new HashMap
<String
, CtfTmfEventType
>());
124 private final CtfIteratorManager fIteratorManager
=
125 new CtfIteratorManager(this);
127 /* Reference to the CTF Trace */
128 private CTFTrace fTrace
;
130 // -------------------------------------------
131 // TmfTrace Overrides
132 // -------------------------------------------
137 * The resource associated with this trace
139 * The path to the trace file
141 * The type of events that will be read from this trace
142 * @throws TmfTraceException
143 * If something went wrong while reading the trace
146 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
147 throws TmfTraceException
{
149 * Set the cache size. This has to be done before the call to super()
150 * because the super needs to know the cache size.
154 super.initTrace(resource
, path
, eventType
);
157 this.fTrace
= new CTFTrace(path
);
159 /* Set the start and (current) end times for this trace */
160 ctx
= (CtfTmfContext
) seekEvent(0L);
161 CtfTmfEvent event
= getNext(ctx
);
162 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
163 /* Handle the case where the trace is empty */
164 this.setStartTime(TmfTimestamp
.BIG_BANG
);
166 final ITmfTimestamp curTime
= event
.getTimestamp();
167 this.setStartTime(curTime
);
168 this.setEndTime(curTime
);
171 * Register every event type. When you call getType, it will
172 * register a trace to that type in the TmfEventTypeManager
174 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
175 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
176 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
177 if (ctfTmfEventType
== null) {
178 List
<ITmfEventField
> content
= new ArrayList
<>();
179 /* Should only return null the first time */
180 for (String fieldName
: ied
.getFields().getFieldsList()) {
181 content
.add(new TmfEventField(fieldName
, null, null));
183 ITmfEventField contentTree
= new TmfEventField(
184 ITmfEventField
.ROOT_FIELD_ID
,
186 content
.toArray(new ITmfEventField
[content
.size()])
189 ctfTmfEventType
= new CtfTmfEventType(ied
.getName(), contentTree
);
190 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
194 } catch (final CTFException e
) {
196 * If it failed at the init(), we can assume it's because the file
197 * was not found or was not recognized as a CTF trace. Throw into
198 * the new type of exception expected by the rest of TMF.
200 throw new TmfTraceException(e
.getMessage(), e
);
205 public void close() {
210 public synchronized void dispose() {
211 fIteratorManager
.dispose();
212 if (fTrace
!= null) {
221 * The default implementation of a CTF trace.
223 * Firstly a weak validation of the metadata is done to determine if the
224 * path is actually for a CTF trace. After that a full validation is done.
226 * If the weak and full validation are successful the confidence is set
229 * If the weak validation was successful, but the full validation fails
230 * a TraceValidationStatus with severity warning and confidence of 1 is
233 * If both weak and full validation fails an error status is returned.
236 public IStatus
validate(final IProject project
, final String path
) {
237 boolean isMetadataFile
= false;
239 isMetadataFile
= Metadata
.preValidate(path
);
240 } catch (final CTFException e
) {
241 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
244 if (isMetadataFile
) {
245 // Trace is pre-validated, continue will full validation
247 final CTFTrace trace
= new CTFTrace(path
);
248 if (!trace
.majorIsSet()) {
249 if (isMetadataFile
) {
250 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
, null);
252 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
255 // Validate using reader initialization
256 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {}
258 // Trace is validated, return with confidence
259 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
261 } catch (final CTFException
| BufferOverflowException e
) {
262 // return warning since it's a CTF trace but with errors in it
263 return new TraceValidationStatus(MIN_CONFIDENCE
, IStatus
.WARNING
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString(), e
); //$NON-NLS-1$
266 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
);
270 public Iterable
<ITmfEventAspect
> getEventAspects() {
275 * Method getCurrentLocation. This is not applicable in CTF
277 * @return null, since the trace has no knowledge of the current location
278 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
281 public ITmfLocation
getCurrentLocation() {
286 public double getLocationRatio(ITmfLocation location
) {
287 final CtfLocation curLocation
= (CtfLocation
) location
;
288 final long startTime
= getStartTime().getValue();
289 final double diff
= curLocation
.getLocationInfo().getTimestamp() - startTime
;
290 final double total
= getEndTime().getValue() - startTime
;
291 return Math
.max(0.0, Math
.min(1.0, diff
/ total
));
299 * @return ITmfContext
302 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
303 CtfLocation currentLocation
= (CtfLocation
) location
;
304 CtfTmfContext context
= new CtfTmfContext(this);
305 if (fTrace
== null) {
306 context
.setLocation(null);
307 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
311 * The rank is set to 0 if the iterator seeks the beginning. If not, it
312 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
315 if (currentLocation
== null) {
316 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
319 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
320 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
321 currentLocation
= new CtfLocation(fTrace
.getCurrentEndTime() + 1, 0L);
324 /* This will seek and update the location after the seek */
325 context
.setLocation(currentLocation
);
330 public synchronized ITmfContext
seekEvent(double ratio
) {
331 CtfTmfContext context
= new CtfTmfContext(this);
332 if (fTrace
== null) {
333 context
.setLocation(null);
334 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
337 final long end
= getEndTime().getValue();
338 final long start
= getStartTime().getValue();
339 final long diff
= end
- start
;
340 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
341 context
.seek(ratioTs
);
342 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
347 * Method readNextEvent.
351 * @return CtfTmfEvent
352 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
355 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
356 if (fTrace
== null) {
359 CtfTmfEvent event
= null;
360 if (context
instanceof CtfTmfContext
) {
361 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
364 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
365 event
= ctfContext
.getCurrentEvent();
368 updateAttributes(context
, event
);
369 ctfContext
.advance();
370 ctfContext
.increaseRank();
378 * Ctf traces have a clock with a unique uuid that will be used to identify
379 * the host. Traces with the same clock uuid will be known to have been made
380 * on the same machine.
382 * Note: uuid is an optional field, it may not be there for a clock.
385 public String
getHostId() {
386 CTFClock clock
= fTrace
.getClock();
388 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
389 if (clockHost
!= null) {
393 return super.getHostId();
397 * Get the first callsite that matches the event name
399 * @param eventName The event name to look for
400 * @return The best callsite candidate
402 public @Nullable CtfTmfCallsite
getCallsite(String eventName
) {
403 CTFCallsite callsite
= fTrace
.getCallsite(eventName
);
404 if (callsite
!= null) {
405 return new CtfTmfCallsite(callsite
);
411 * Get the closest matching callsite for given event name and instruction
417 * The instruction pointer
418 * @return The closest matching callsite
420 public @Nullable CtfTmfCallsite
getCallsite(String eventName
, long ip
) {
421 CTFCallsite calliste
= fTrace
.getCallsite(eventName
, ip
);
422 if (calliste
!= null) {
423 return new CtfTmfCallsite(calliste
);
429 * Get the CTF environment variables defined in this CTF trace, in <name,
430 * value> form. This comes from the trace's CTF metadata.
432 * @return The CTF environment
434 public Map
<String
, String
> getEnvironment() {
435 return fTrace
.getEnvironment();
438 // -------------------------------------------
439 // ITmfTraceProperties
440 // -------------------------------------------
443 public Map
<String
, String
> getTraceProperties() {
444 Map
<String
, String
> properties
= new HashMap
<>();
445 properties
.putAll(fTrace
.getEnvironment());
446 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
450 // -------------------------------------------
452 // -------------------------------------------
455 * gets the clock offset
457 * @return the clock offset in ns
459 public long getOffset() {
460 if (fTrace
!= null) {
461 return fTrace
.getOffset();
467 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
471 * The timestamp in cycles
472 * @return The timestamp in nanoseconds
474 public long timestampCyclesToNanos(long cycles
) {
475 return fTrace
.timestampCyclesToNanos(cycles
);
479 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
483 * The timestamp in nanoseconds
484 * @return The timestamp in cycles
486 public long timestampNanoToCycles(long nanos
) {
487 return fTrace
.timestampNanoToCycles(nanos
);
491 * Gets the list of declared events
494 public Set
<CtfTmfEventType
> getContainedEventTypes() {
495 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
499 * Register an event type to this trace.
501 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
503 * FIXME This could probably be made cleaner?
506 * The event type to register
508 public void registerEventType(CtfTmfEventType eventType
) {
509 fContainedEventTypes
.put(eventType
.getName(), eventType
);
512 // -------------------------------------------
514 // -------------------------------------------
517 public CtfTmfEvent
parseEvent(ITmfContext context
) {
518 CtfTmfEvent event
= null;
519 if (context
instanceof CtfTmfContext
) {
520 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
521 event
= getNext(tmpContext
);
527 * Sets the cache size for a CtfTmfTrace.
529 protected void setCacheSize() {
530 setCacheSize(DEFAULT_CACHE_SIZE
);
533 // -------------------------------------------
534 // CtfIterator factory methods
535 // -------------------------------------------
538 * Get an iterator to the trace
540 * @return an iterator to the trace
542 public ITmfContext
createIterator() {
544 return new CtfIterator(fTrace
, this);
545 } catch (CTFException e
) {
546 Activator
.getDefault().logError(e
.getMessage(), e
);
552 * Get an iterator to the trace, , which will initially point to the given
555 * @param ctfLocationData
556 * The initial timestamp the iterator will be pointing to
559 * @return The new iterator
561 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
563 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
564 } catch (CTFException e
) {
565 Activator
.getDefault().logError(e
.getMessage(), e
);
571 * Create the 'CtfIterator' object from a CtfTmfContext.
574 * The iterator will initially be pointing to this context
575 * @return A new CtfIterator object
578 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
579 return fIteratorManager
.getIterator(context
);
583 * Dispose an iterator that was create with
584 * {@link #createIteratorFromContext}
587 * The last context that was pointed to by the iterator (this is
588 * the 'key' to find the correct iterator to dispose).
591 public void disposeContext(CtfTmfContext context
) {
592 fIteratorManager
.removeIterator(context
);
595 // ------------------------------------------------------------------------
596 // Timestamp transformation functions
597 // ------------------------------------------------------------------------
603 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
604 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
607 private static int fCheckpointSize
= -1;
610 public synchronized int getCheckpointSize() {
611 if (fCheckpointSize
== -1) {
612 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
613 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
616 fCheckpointSize
= b
.position();
619 return fCheckpointSize
;
623 protected ITmfTraceIndexer
createIndexer(int interval
) {
624 return new TmfBTreeTraceIndexer(this, interval
);
628 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
629 return new CtfLocation(bufferIn
);
633 public boolean isComplete() {
634 if (getResource() == null) {
640 String sessionName
= null;
642 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
643 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
644 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
645 } catch (CoreException e
) {
646 Activator
.getDefault().logError(e
.getMessage(), e
);
647 // Something happened to the resource, assume we won't get any more
651 return host
== null || port
== null || sessionName
== null;
655 public void setComplete(final boolean isComplete
) {
656 super.setComplete(isComplete
);
659 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
660 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
661 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
663 } catch (CoreException e
) {
664 Activator
.getDefault().logError(e
.getMessage(), e
);