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 java
.nio
.BufferOverflowException
;
18 import java
.nio
.ByteBuffer
;
19 import java
.util
.ArrayList
;
20 import java
.util
.Collections
;
21 import java
.util
.HashMap
;
22 import java
.util
.List
;
26 import org
.eclipse
.core
.resources
.IProject
;
27 import org
.eclipse
.core
.resources
.IResource
;
28 import org
.eclipse
.core
.runtime
.CoreException
;
29 import org
.eclipse
.core
.runtime
.IStatus
;
30 import org
.eclipse
.core
.runtime
.Status
;
31 import org
.eclipse
.tracecompass
.ctf
.core
.event
.CTFClock
;
32 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
33 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFReaderException
;
34 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
35 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTraceReader
;
36 import org
.eclipse
.tracecompass
.internal
.tmf
.ctf
.core
.Activator
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEvent
;
38 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
39 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
40 import org
.eclipse
.tracecompass
.tmf
.core
.exceptions
.TmfTraceException
;
41 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.ITmfTimestamp
;
42 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfTimestamp
;
43 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
44 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfEventParser
;
45 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceProperties
;
46 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfTraceWithPreDefinedEvents
;
47 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TmfTrace
;
48 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.TraceValidationStatus
;
49 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfPersistentlyIndexable
;
50 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.ITmfTraceIndexer
;
51 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.TmfBTreeTraceIndexer
;
52 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.ITmfCheckpoint
;
53 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.indexer
.checkpoint
.TmfCheckpoint
;
54 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.location
.ITmfLocation
;
55 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
56 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocation
;
57 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfLocationInfo
;
58 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.context
.CtfTmfContext
;
59 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEvent
;
60 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.CtfTmfEventType
;
61 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.timestamp
.CtfTmfTimestamp
;
62 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.iterator
.CtfIterator
;
63 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.iterator
.CtfIteratorManager
;
65 import com
.google
.common
.collect
.ImmutableSet
;
68 * The CTf trace handler
71 * @author Matthew khouzam
73 public class CtfTmfTrace
extends TmfTrace
74 implements ITmfEventParser
, ITmfTraceProperties
, ITmfPersistentlyIndexable
,
75 ITmfTraceWithPreDefinedEvents
, AutoCloseable
{
77 // -------------------------------------------
79 // -------------------------------------------
81 * Default cache size for CTF traces
83 protected static final int DEFAULT_CACHE_SIZE
= 50000;
86 * The Ctf clock unique identifier field
88 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
89 private static final int CONFIDENCE
= 10;
91 // -------------------------------------------
93 // -------------------------------------------
95 private final Map
<String
, CtfTmfEventType
> fContainedEventTypes
=
96 Collections
.synchronizedMap(new HashMap
<String
, CtfTmfEventType
>());
98 private final CtfIteratorManager fIteratorManager
=
99 new CtfIteratorManager(this);
101 /* Reference to the CTF Trace */
102 private CTFTrace fTrace
;
104 // -------------------------------------------
105 // TmfTrace Overrides
106 // -------------------------------------------
111 * The resource associated with this trace
113 * The path to the trace file
115 * The type of events that will be read from this trace
116 * @throws TmfTraceException
117 * If something went wrong while reading the trace
120 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
121 throws TmfTraceException
{
123 * Set the cache size. This has to be done before the call to super()
124 * because the super needs to know the cache size.
128 super.initTrace(resource
, path
, eventType
);
131 this.fTrace
= new CTFTrace(path
);
133 /* Set the start and (current) end times for this trace */
134 ctx
= (CtfTmfContext
) seekEvent(0L);
135 CtfTmfEvent event
= getNext(ctx
);
136 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
137 /* Handle the case where the trace is empty */
138 this.setStartTime(TmfTimestamp
.BIG_BANG
);
140 final ITmfTimestamp curTime
= event
.getTimestamp();
141 this.setStartTime(curTime
);
142 this.setEndTime(curTime
);
145 * Register every event type. When you call getType, it will
146 * register a trace to that type in the TmfEventTypeManager
148 try (CtfIterator iter
= fIteratorManager
.getIterator(ctx
)) {
149 for (IEventDeclaration ied
: iter
.getEventDeclarations()) {
150 CtfTmfEventType ctfTmfEventType
= fContainedEventTypes
.get(ied
.getName());
151 if (ctfTmfEventType
== null) {
152 List
<ITmfEventField
> content
= new ArrayList
<>();
153 /* Should only return null the first time */
154 for (String fieldName
: ied
.getFields().getFieldsList()) {
155 content
.add(new TmfEventField(fieldName
, null, null));
157 ITmfEventField contentTree
= new TmfEventField(
158 ITmfEventField
.ROOT_FIELD_ID
,
160 content
.toArray(new ITmfEventField
[content
.size()])
163 ctfTmfEventType
= new CtfTmfEventType(ied
.getName(), contentTree
);
164 fContainedEventTypes
.put(ctfTmfEventType
.getName(), ctfTmfEventType
);
168 } catch (final CTFReaderException e
) {
170 * If it failed at the init(), we can assume it's because the file
171 * was not found or was not recognized as a CTF trace. Throw into
172 * the new type of exception expected by the rest of TMF.
174 throw new TmfTraceException(e
.getMessage(), e
);
179 * Return the iterator manager of this trace
181 * @return The iterator manager
183 public CtfIteratorManager
getIteratorManager() {
184 return fIteratorManager
;
188 public void close() {
193 public synchronized void dispose() {
194 fIteratorManager
.dispose();
195 if (fTrace
!= null) {
205 * The default implementation sets the confidence to 10 if the trace is a
209 public IStatus
validate(final IProject project
, final String path
) {
210 IStatus status
= new TraceValidationStatus(CONFIDENCE
, Activator
.PLUGIN_ID
);
211 try (final CTFTrace temp
= new CTFTrace(path
);) {
212 if (!temp
.majorIsSet()) {
213 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
215 try (CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);) {
216 if (!ctfTraceReader
.hasMoreEvents()) {
217 // TODO: This will need an additional check when we
218 // support live traces
219 // because having no event is valid for a live trace
220 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
224 } catch (final CTFReaderException e
) {
225 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + e
.toString()); //$NON-NLS-1$
226 } catch (final BufferOverflowException e
) {
227 status
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+ ": " + Messages
.CtfTmfTrace_BufferOverflowErrorMessage
); //$NON-NLS-1$
234 * Method getCurrentLocation. This is not applicable in CTF
236 * @return null, since the trace has no knowledge of the current location
237 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
241 public ITmfLocation
getCurrentLocation() {
249 public double getLocationRatio(ITmfLocation location
) {
250 final CtfLocation curLocation
= (CtfLocation
) location
;
251 final CtfTmfContext context
= new CtfTmfContext(this);
252 context
.setLocation(curLocation
);
253 context
.seek(curLocation
.getLocationInfo());
254 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
255 final long startTime
= fIteratorManager
.getIterator(context
).getStartTime();
256 final long endTime
= fIteratorManager
.getIterator(context
).getEndTime();
257 return ((double) currentTime
.getTimestamp() - startTime
)
258 / (endTime
- startTime
);
266 * @return ITmfContext
270 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
271 CtfLocation currentLocation
= (CtfLocation
) location
;
272 CtfTmfContext context
= new CtfTmfContext(this);
273 if (fTrace
== null) {
274 context
.setLocation(null);
275 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
279 * The rank is set to 0 if the iterator seeks the beginning. If not, it
280 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
283 if (currentLocation
== null) {
284 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
287 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
288 currentLocation
= new CtfLocation(getCTFTrace().getCurrentEndTime() + 1, 0L);
290 context
.setLocation(currentLocation
);
291 if (location
== null) {
292 long timestamp
= fIteratorManager
.getIterator(context
).getCurrentTimestamp();
293 currentLocation
= new CtfLocation(timestamp
, 0);
295 if (context
.getRank() != 0) {
296 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
302 public synchronized ITmfContext
seekEvent(double ratio
) {
303 CtfTmfContext context
= new CtfTmfContext(this);
304 if (fTrace
== null) {
305 context
.setLocation(null);
306 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
309 final long end
= getCTFTrace().getCurrentEndTime();
310 final long start
= getCTFTrace().getCurrentStartTime();
311 final long diff
= end
- start
;
312 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
313 context
.seek(ratioTs
);
314 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
319 * Method readNextEvent.
323 * @return CtfTmfEvent
324 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
327 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
328 if (fTrace
== null) {
331 CtfTmfEvent event
= null;
332 if (context
instanceof CtfTmfContext
) {
333 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
336 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
337 event
= ctfContext
.getCurrentEvent();
340 updateAttributes(context
, event
.getTimestamp());
341 ctfContext
.advance();
342 ctfContext
.increaseRank();
350 * gets the CTFtrace that this is wrapping
352 * @return the CTF trace
354 public CTFTrace
getCTFTrace() {
359 * Ctf traces have a clock with a unique uuid that will be used to identify
360 * the host. Traces with the same clock uuid will be known to have been made
361 * on the same machine.
363 * Note: uuid is an optional field, it may not be there for a clock.
366 public String
getHostId() {
367 CTFClock clock
= getCTFTrace().getClock();
369 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
370 if (clockHost
!= null) {
374 return super.getHostId();
377 // -------------------------------------------
378 // ITmfTraceProperties
379 // -------------------------------------------
385 public Map
<String
, String
> getTraceProperties() {
386 Map
<String
, String
> properties
= new HashMap
<>();
387 properties
.putAll(fTrace
.getEnvironment());
388 properties
.put(Messages
.CtfTmfTrace_HostID
, getHostId());
392 // -------------------------------------------
394 // -------------------------------------------
397 * gets the clock offset
399 * @return the clock offset in ns
401 public long getOffset() {
402 if (fTrace
!= null) {
403 return fTrace
.getOffset();
409 * Gets the list of declared events
414 public Set
<CtfTmfEventType
> getContainedEventTypes() {
415 return ImmutableSet
.copyOf(fContainedEventTypes
.values());
419 * Register an event type to this trace.
421 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
423 * FIXME This could probably be made cleaner?
426 * The event type to register
428 public void registerEventType(CtfTmfEventType eventType
) {
429 fContainedEventTypes
.put(eventType
.getName(), eventType
);
432 // -------------------------------------------
434 // -------------------------------------------
437 public CtfTmfEvent
parseEvent(ITmfContext context
) {
438 CtfTmfEvent event
= null;
439 if (context
instanceof CtfTmfContext
) {
440 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
441 event
= getNext(tmpContext
);
447 * Sets the cache size for a CtfTmfTrace.
449 protected void setCacheSize() {
450 setCacheSize(DEFAULT_CACHE_SIZE
);
453 // -------------------------------------------
455 // -------------------------------------------
458 * Get an iterator to the trace
460 * @return an iterator to the trace
463 public CtfIterator
createIterator() {
465 return new CtfIterator(this);
466 } catch (CTFReaderException e
) {
467 Activator
.getDefault().logError(e
.getMessage(), e
);
472 // ------------------------------------------------------------------------
473 // Timestamp transformation functions
474 // ------------------------------------------------------------------------
480 public CtfTmfTimestamp
createTimestamp(long ts
) {
481 return new CtfTmfTimestamp(getTimestampTransform().transform(ts
));
484 private static int fCheckpointSize
= -1;
490 public synchronized int getCheckpointSize() {
491 if (fCheckpointSize
== -1) {
492 TmfCheckpoint c
= new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
493 ByteBuffer b
= ByteBuffer
.allocate(ITmfCheckpoint
.MAX_SERIALIZE_SIZE
);
496 fCheckpointSize
= b
.position();
499 return fCheckpointSize
;
503 protected ITmfTraceIndexer
createIndexer(int interval
) {
504 return new TmfBTreeTraceIndexer(this, interval
);
511 public ITmfLocation
restoreLocation(ByteBuffer bufferIn
) {
512 return new CtfLocation(bufferIn
);
516 public boolean isComplete() {
517 if (getResource() == null) {
523 String sessionName
= null;
525 host
= getResource().getPersistentProperty(CtfConstants
.LIVE_HOST
);
526 port
= getResource().getPersistentProperty(CtfConstants
.LIVE_PORT
);
527 sessionName
= getResource().getPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
);
528 } catch (CoreException e
) {
529 Activator
.getDefault().logError(e
.getMessage(), e
);
530 // Something happened to the resource, assume we won't get any more data from it
533 return host
== null || port
== null || sessionName
== null;
537 public void setComplete(final boolean isComplete
) {
538 super.setComplete(isComplete
);
541 getResource().setPersistentProperty(CtfConstants
.LIVE_HOST
, null);
542 getResource().setPersistentProperty(CtfConstants
.LIVE_PORT
, null);
543 getResource().setPersistentProperty(CtfConstants
.LIVE_SESSION_NAME
, null);
545 } catch (CoreException e
) {
546 Activator
.getDefault().logError(e
.getMessage(), e
);