1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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
.event
.CTFCallsite
;
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
.trace
.CTFReaderException
;
40 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
41 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
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
.TmfTimestamp
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceProperties
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceWithPreDefinedEvents
;
55 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
56 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
57 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
58 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
59 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
60 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
61 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
62 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
63 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
64 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocation
;
65 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
66 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfTmfContext
;
67 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
68 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventType
;
69 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfChannelAspect
;
70 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.aspect
.CtfCpuAspect
;
71 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.lookup
.CtfTmfCallsite
;
72 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.timestamp
.CtfTmfTimestamp
;
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
, AutoCloseable
{
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
99 protected static final @NonNull Collection
<ITmfEventAspect
> CTF_ASPECTS
=
100 checkNotNull(ImmutableList
.of(
101 ITmfEventAspect
.BaseAspects
.TIMESTAMP
,
102 new CtfChannelAspect(),
104 ITmfEventAspect
.BaseAspects
.EVENT_TYPE
,
105 ITmfEventAspect
.BaseAspects
.CONTENTS
109 * The Ctf clock unique identifier field
111 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
112 private static final int CONFIDENCE
= 10;
114 // -------------------------------------------
116 // -------------------------------------------
118 private final Map
<String
, CtfTmfEventType
> fContainedEventTypes
=
119 Collections
.synchronizedMap(new HashMap
<String
, CtfTmfEventType
>());
121 private final CtfIteratorManager fIteratorManager
=
122 new CtfIteratorManager(this);
124 /* Reference to the CTF Trace */
125 private CTFTrace fTrace
;
127 // -------------------------------------------
128 // TmfTrace Overrides
129 // -------------------------------------------
134 * The resource associated with this trace
136 * The path to the trace file
138 * The type of events that will be read from this trace
139 * @throws TmfTraceException
140 * If something went wrong while reading the trace
143 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
144 throws TmfTraceException
{
146 * Set the cache size. This has to be done before the call to super()
147 * because the super needs to know the cache size.
151 super.initTrace(resource
, path
, eventType
);
154 this.fTrace
= new CTFTrace(path
);
156 /* Set the start and (current) end times for this trace */
157 ctx
= (CtfTmfContext
) seekEvent(0L);
158 CtfTmfEvent event
= getNext(ctx
);
159 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
160 /* Handle the case where the trace is empty */
161 this.setStartTime(TmfTimestamp
.BIG_BANG
);
163 final ITmfTimestamp curTime
= event
.getTimestamp();
164 this.setStartTime(curTime
);
165 this.setEndTime(curTime
);
168 * Register every event type. When you call getType, it will
169 * register a trace to that type in the TmfEventTypeManager
171 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
172 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
173 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
174 if (ctfTmfEventType
== null) {
175 List
<ITmfEventField
> content
= new ArrayList
<>();
176 /* Should only return null the first time */
177 for (String fieldName
: ied
.getFields().getFieldsList()) {
178 content
.add(new TmfEventField(fieldName
, null, null));
180 ITmfEventField contentTree
= new TmfEventField(
181 ITmfEventField
.ROOT_FIELD_ID
,
183 content
.toArray(new ITmfEventField
[content
.size()])
186 ctfTmfEventType
= new CtfTmfEventType(ied
.getName(), contentTree
);
187 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
191 } catch (final CTFReaderException e
) {
193 * If it failed at the init(), we can assume it's because the file
194 * was not found or was not recognized as a CTF trace. Throw into
195 * the new type of exception expected by the rest of TMF.
197 throw new TmfTraceException(e
.getMessage(), e
);
202 * Return the iterator manager of this trace
204 * @return The iterator manager
206 public CtfIteratorManager
getIteratorManager() {
207 return fIteratorManager
;
211 public void close() {
216 public synchronized void dispose() {
217 fIteratorManager
.dispose();
218 if (fTrace
!= null) {
227 * The default implementation sets the confidence to 10 if the trace is a
231 public IStatus
validate(final IProject project
, final String path
) {
232 IStatus status
= new TraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
);
234 final CTFTrace temp
= new CTFTrace(path
);
235 if (!temp
.majorIsSet()) {
236 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
238 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);) {
239 if (!ctfTraceReader
.hasMoreEvents()) {
240 // TODO: This will need an additional check when we
241 // support live traces
242 // because having no event is valid for a live trace
243 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
247 } catch (final CTFReaderException e
) {
248 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString()); //$NON-NLS-1$
249 } catch (final BufferOverflowException e
) {
250 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + Messages
.CtfTmfTrace_BufferOverflowErrorMessage
); //$NON-NLS-1$
257 public Iterable
<ITmfEventAspect
> getEventAspects() {
262 * Method getCurrentLocation. This is not applicable in CTF
264 * @return null, since the trace has no knowledge of the current location
265 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
269 public ITmfLocation
getCurrentLocation() {
277 public double getLocationRatio(ITmfLocation location
) {
278 final CtfLocation curLocation
= (CtfLocation
) location
;
279 final CtfTmfContext context
= new CtfTmfContext(this);
280 context
.setLocation(curLocation
);
281 context
.seek(curLocation
.getLocationInfo());
282 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
283 final long startTime
= fIteratorManager
.getIterator(context
).getStartTime();
284 final long endTime
= fIteratorManager
.getIterator(context
).getEndTime();
285 return ((double) currentTime
.getTimestamp() - startTime
)
286 / (endTime
- startTime
);
294 * @return ITmfContext
298 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
299 CtfLocation currentLocation
= (CtfLocation
) location
;
300 CtfTmfContext context
= new CtfTmfContext(this);
301 if (fTrace
== null) {
302 context
.setLocation(null);
303 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
307 * The rank is set to 0 if the iterator seeks the beginning. If not, it
308 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
311 if (currentLocation
== null) {
312 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
315 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
316 currentLocation
= new CtfLocation(fTrace
.getCurrentEndTime() + 1, 0L);
318 context
.setLocation(currentLocation
);
319 if (location
== null) {
320 long timestamp
= fIteratorManager
.getIterator(context
).getCurrentTimestamp();
321 currentLocation
= new CtfLocation(timestamp
, 0);
323 if (context
.getRank() != 0) {
324 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
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
= fTrace
.getCurrentEndTime();
338 final long start
= fTrace
.getCurrentStartTime();
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
.getTimestamp());
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 // -------------------------------------------
446 public Map
<String
, String
> getTraceProperties() {
447 Map
<String
, String
> properties
= new HashMap
<>();
448 properties
.putAll(fTrace
.getEnvironment());
449 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
453 // -------------------------------------------
455 // -------------------------------------------
458 * gets the clock offset
460 * @return the clock offset in ns
462 public long getOffset() {
463 if (fTrace
!= null) {
464 return fTrace
.getOffset();
470 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
474 * The timestamp in cycles
475 * @return The timestamp in nanoseconds
477 public long timestampCyclesToNanos(long cycles
) {
478 return fTrace
.timestampCyclesToNanos(cycles
);
482 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
486 * The timestamp in nanoseconds
487 * @return The timestamp in cycles
489 public long timestampNanoToCycles(long nanos
) {
490 return fTrace
.timestampNanoToCycles(nanos
);
494 * Gets the list of declared events
499 public Set
<CtfTmfEventType
> getContainedEventTypes() {
500 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
504 * Register an event type to this trace.
506 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
508 * FIXME This could probably be made cleaner?
511 * The event type to register
513 public void registerEventType(CtfTmfEventType eventType
) {
514 fContainedEventTypes
.put(eventType
.getName(), eventType
);
517 // -------------------------------------------
519 // -------------------------------------------
522 public CtfTmfEvent
parseEvent(ITmfContext context
) {
523 CtfTmfEvent event
= null;
524 if (context
instanceof CtfTmfContext
) {
525 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
526 event
= getNext(tmpContext
);
532 * Sets the cache size for a CtfTmfTrace.
534 protected void setCacheSize() {
535 setCacheSize(DEFAULT_CACHE_SIZE
);
538 // -------------------------------------------
539 // CtfIterator factory methods
540 // -------------------------------------------
543 * Get an iterator to the trace
545 * @return an iterator to the trace
548 public ITmfContext
createIterator() {
550 return new CtfIterator(fTrace
, this);
551 } catch (CTFReaderException e
) {
552 Activator
.getDefault().logError(e
.getMessage(), e
);
558 * Get an iterator to the trace, , which will initially point to the given
561 * @param ctfLocationData
562 * The initial timestamp the iterator will be pointing to
565 * @return The new iterator
567 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
569 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
570 } catch (CTFReaderException e
) {
571 Activator
.getDefault().logError(e
.getMessage(), e
);
576 // ------------------------------------------------------------------------
577 // Timestamp transformation functions
578 // ------------------------------------------------------------------------
584 public CtfTmfTimestamp
createTimestamp(long ts
) {
585 return new CtfTmfTimestamp(getTimestampTransform().transform(ts
));
588 private static int fCheckpointSize
= -1;
594 public synchronized int getCheckpointSize() {
595 if (fCheckpointSize
== -1) {
596 TmfCheckpoint c
= new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
597 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
600 fCheckpointSize
= b
.position();
603 return fCheckpointSize
;
607 protected ITmfTraceIndexer
createIndexer(int interval
) {
608 return new TmfBTreeTraceIndexer(this, interval
);
615 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
616 return new CtfLocation(bufferIn
);
620 public boolean isComplete() {
621 if (getResource() == null) {
627 String sessionName
= null;
629 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
630 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
631 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
632 } catch (CoreException e
) {
633 Activator
.getDefault().logError(e
.getMessage(), e
);
634 // Something happened to the resource, assume we won't get any more
638 return host
== null || port
== null || sessionName
== null;
642 public void setComplete(final boolean isComplete
) {
643 super.setComplete(isComplete
);
646 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
647 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
648 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
650 } catch (CoreException e
) {
651 Activator
.getDefault().logError(e
.getMessage(), e
);