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
.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
.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
;
73 import com
.google
.common
.collect
.ImmutableList
;
74 import com
.google
.common
.collect
.ImmutableSet
;
77 * The CTf trace handler
80 * @author Matthew khouzam
82 public class CtfTmfTrace
extends TmfTrace
83 implements ITmfTraceProperties
, ITmfPersistentlyIndexable
,
84 ITmfTraceWithPreDefinedEvents
, AutoCloseable
{
86 // -------------------------------------------
88 // -------------------------------------------
91 * Default cache size for CTF traces
93 protected static final int DEFAULT_CACHE_SIZE
= 50000;
96 * 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 CTFException 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 public void close() {
207 public synchronized void dispose() {
208 fIteratorManager
.dispose();
209 if (fTrace
!= null) {
218 * The default implementation sets the confidence to 10 if the trace is a
222 public IStatus
validate(final IProject project
, final String path
) {
224 final CTFTrace trace
= new CTFTrace(path
);
225 if (!trace
.majorIsSet()) {
226 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
228 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(trace
)) {
229 if (!ctfTraceReader
.hasMoreEvents()) {
230 // TODO: This will need an additional check when we
231 // support live traces
232 // because having no event is valid for a live trace
233 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
236 return new CtfTraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
, trace
.getEnvironment());
237 } catch (final CTFException e
) {
238 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString()); //$NON-NLS-1$
239 } catch (final BufferOverflowException e
) {
240 return new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + Messages
.CtfTmfTrace_BufferOverflowErrorMessage
); //$NON-NLS-1$
245 public Iterable
<ITmfEventAspect
> getEventAspects() {
250 * Method getCurrentLocation. This is not applicable in CTF
252 * @return null, since the trace has no knowledge of the current location
253 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
256 public ITmfLocation
getCurrentLocation() {
261 public double getLocationRatio(ITmfLocation location
) {
262 final CtfLocation curLocation
= (CtfLocation
) location
;
263 final CtfTmfContext context
= new CtfTmfContext(this);
264 context
.setLocation(curLocation
);
265 context
.seek(curLocation
.getLocationInfo());
266 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
267 final long startTime
= fIteratorManager
.getIterator(context
).getStartTime();
268 final long endTime
= fIteratorManager
.getIterator(context
).getEndTime();
269 return ((double) currentTime
.getTimestamp() - startTime
)
270 / (endTime
- startTime
);
278 * @return ITmfContext
281 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
282 CtfLocation currentLocation
= (CtfLocation
) location
;
283 CtfTmfContext context
= new CtfTmfContext(this);
284 if (fTrace
== null) {
285 context
.setLocation(null);
286 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
290 * The rank is set to 0 if the iterator seeks the beginning. If not, it
291 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
294 if (currentLocation
== null) {
295 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
298 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
299 currentLocation
= new CtfLocation(fTrace
.getCurrentEndTime() + 1, 0L);
301 context
.setLocation(currentLocation
);
302 if (location
== null) {
303 long timestamp
= fIteratorManager
.getIterator(context
).getCurrentTimestamp();
304 currentLocation
= new CtfLocation(timestamp
, 0);
306 if (context
.getRank() != 0) {
307 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
313 public synchronized ITmfContext
seekEvent(double ratio
) {
314 CtfTmfContext context
= new CtfTmfContext(this);
315 if (fTrace
== null) {
316 context
.setLocation(null);
317 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
320 final long end
= fTrace
.getCurrentEndTime();
321 final long start
= fTrace
.getCurrentStartTime();
322 final long diff
= end
- start
;
323 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
324 context
.seek(ratioTs
);
325 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
330 * Method readNextEvent.
334 * @return CtfTmfEvent
335 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
338 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
339 if (fTrace
== null) {
342 CtfTmfEvent event
= null;
343 if (context
instanceof CtfTmfContext
) {
344 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
347 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
348 event
= ctfContext
.getCurrentEvent();
351 updateAttributes(context
, event
.getTimestamp());
352 ctfContext
.advance();
353 ctfContext
.increaseRank();
361 * Ctf traces have a clock with a unique uuid that will be used to identify
362 * the host. Traces with the same clock uuid will be known to have been made
363 * on the same machine.
365 * Note: uuid is an optional field, it may not be there for a clock.
368 public String
getHostId() {
369 CTFClock clock
= fTrace
.getClock();
371 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
372 if (clockHost
!= null) {
376 return super.getHostId();
380 * Get the first callsite that matches the event name
382 * @param eventName The event name to look for
383 * @return The best callsite candidate
385 public @Nullable CtfTmfCallsite
getCallsite(String eventName
) {
386 CTFCallsite callsite
= fTrace
.getCallsite(eventName
);
387 if (callsite
!= null) {
388 return new CtfTmfCallsite(callsite
);
394 * Get the closest matching callsite for given event name and instruction
400 * The instruction pointer
401 * @return The closest matching callsite
403 public @Nullable CtfTmfCallsite
getCallsite(String eventName
, long ip
) {
404 CTFCallsite calliste
= fTrace
.getCallsite(eventName
, ip
);
405 if (calliste
!= null) {
406 return new CtfTmfCallsite(calliste
);
412 * Get the CTF environment variables defined in this CTF trace, in <name,
413 * value> form. This comes from the trace's CTF metadata.
415 * @return The CTF environment
417 public Map
<String
, String
> getEnvironment() {
418 return fTrace
.getEnvironment();
421 // -------------------------------------------
422 // ITmfTraceProperties
423 // -------------------------------------------
426 public Map
<String
, String
> getTraceProperties() {
427 Map
<String
, String
> properties
= new HashMap
<>();
428 properties
.putAll(fTrace
.getEnvironment());
429 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
433 // -------------------------------------------
435 // -------------------------------------------
438 * gets the clock offset
440 * @return the clock offset in ns
442 public long getOffset() {
443 if (fTrace
!= null) {
444 return fTrace
.getOffset();
450 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
454 * The timestamp in cycles
455 * @return The timestamp in nanoseconds
457 public long timestampCyclesToNanos(long cycles
) {
458 return fTrace
.timestampCyclesToNanos(cycles
);
462 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
466 * The timestamp in nanoseconds
467 * @return The timestamp in cycles
469 public long timestampNanoToCycles(long nanos
) {
470 return fTrace
.timestampNanoToCycles(nanos
);
474 * Gets the list of declared events
477 public Set
<CtfTmfEventType
> getContainedEventTypes() {
478 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
482 * Register an event type to this trace.
484 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
486 * FIXME This could probably be made cleaner?
489 * The event type to register
491 public void registerEventType(CtfTmfEventType eventType
) {
492 fContainedEventTypes
.put(eventType
.getName(), eventType
);
495 // -------------------------------------------
497 // -------------------------------------------
500 public CtfTmfEvent
parseEvent(ITmfContext context
) {
501 CtfTmfEvent event
= null;
502 if (context
instanceof CtfTmfContext
) {
503 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
504 event
= getNext(tmpContext
);
510 * Sets the cache size for a CtfTmfTrace.
512 protected void setCacheSize() {
513 setCacheSize(DEFAULT_CACHE_SIZE
);
516 // -------------------------------------------
517 // CtfIterator factory methods
518 // -------------------------------------------
521 * Get an iterator to the trace
523 * @return an iterator to the trace
525 public ITmfContext
createIterator() {
527 return new CtfIterator(fTrace
, this);
528 } catch (CTFException e
) {
529 Activator
.getDefault().logError(e
.getMessage(), e
);
535 * Get an iterator to the trace, , which will initially point to the given
538 * @param ctfLocationData
539 * The initial timestamp the iterator will be pointing to
542 * @return The new iterator
544 public ITmfContext
createIterator(CtfLocationInfo ctfLocationData
, long rank
) {
546 return new CtfIterator(fTrace
, this, ctfLocationData
, rank
);
547 } catch (CTFException e
) {
548 Activator
.getDefault().logError(e
.getMessage(), e
);
554 * Create the 'CtfIterator' object from a CtfTmfContext.
557 * The iterator will initially be pointing to this context
558 * @return A new CtfIterator object
561 public ITmfContext
createIteratorFromContext(CtfTmfContext context
) {
562 return fIteratorManager
.getIterator(context
);
566 * Dispose an iterator that was create with
567 * {@link #createIteratorFromContext}
570 * The last context that was pointed to by the iterator (this is
571 * the 'key' to find the correct iterator to dispose).
574 public void disposeContext(CtfTmfContext context
) {
575 fIteratorManager
.removeIterator(context
);
578 // ------------------------------------------------------------------------
579 // Timestamp transformation functions
580 // ------------------------------------------------------------------------
586 public @NonNull TmfNanoTimestamp
createTimestamp(long ts
) {
587 return new TmfNanoTimestamp(getTimestampTransform().transform(ts
));
590 private static int fCheckpointSize
= -1;
593 public synchronized int getCheckpointSize() {
594 if (fCheckpointSize
== -1) {
595 TmfCheckpoint c
= new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
596 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
599 fCheckpointSize
= b
.position();
602 return fCheckpointSize
;
606 protected ITmfTraceIndexer
createIndexer(int interval
) {
607 return new TmfBTreeTraceIndexer(this, interval
);
611 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
612 return new CtfLocation(bufferIn
);
616 public boolean isComplete() {
617 if (getResource() == null) {
623 String sessionName
= null;
625 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
626 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
627 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
628 } catch (CoreException e
) {
629 Activator
.getDefault().logError(e
.getMessage(), e
);
630 // Something happened to the resource, assume we won't get any more
634 return host
== null || port
== null || sessionName
== null;
638 public void setComplete(final boolean isComplete
) {
639 super.setComplete(isComplete
);
642 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
643 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
644 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
646 } catch (CoreException e
) {
647 Activator
.getDefault().logError(e
.getMessage(), e
);