1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 Ericsson
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 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
16 import java
.util
.Collections
;
19 import org
.eclipse
.core
.resources
.IProject
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.runtime
.IStatus
;
22 import org
.eclipse
.core
.runtime
.Status
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceProperties
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
40 * The CTf trace handler
43 * @author Matthew khouzam
45 public class CtfTmfTrace
extends TmfTrace
46 implements ITmfEventParser
, ITmfTraceProperties
{
48 // -------------------------------------------
50 // -------------------------------------------
52 * Default cache size for CTF traces
54 protected static final int DEFAULT_CACHE_SIZE
= 50000;
57 * The Ctf clock unique identifier field
59 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
61 // -------------------------------------------
63 // -------------------------------------------
65 /* Reference to the CTF Trace */
66 private CTFTrace fTrace
;
68 // -------------------------------------------
70 // -------------------------------------------
75 * The resource associated with this trace
77 * The path to the trace file
79 * The type of events that will be read from this trace
80 * @throws TmfTraceException
81 * If something when wrong while reading the trace
84 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
85 throws TmfTraceException
{
87 * Set the cache size. This has to be done before the call to super()
88 * because the super needs to know the cache size.
92 super.initTrace(resource
, path
, eventType
);
94 @SuppressWarnings("unused")
98 this.fTrace
= new CTFTrace(path
);
99 CtfIteratorManager
.addTrace(this);
101 /* Set the start and (current) end times for this trace */
102 ctx
= (CtfTmfContext
) seekEvent(0L);
103 CtfTmfEvent event
= getNext(ctx
);
104 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
105 /* Handle the case where the trace is empty */
106 this.setStartTime(TmfTimestamp
.BIG_BANG
);
108 final ITmfTimestamp curTime
= event
.getTimestamp();
109 this.setStartTime(curTime
);
110 this.setEndTime(curTime
);
113 } catch (final CTFReaderException e
) {
115 * If it failed at the init(), we can assume it's because the file
116 * was not found or was not recognized as a CTF trace. Throw into
117 * the new type of exception expected by the rest of TMF.
119 throw new TmfTraceException(e
.getMessage(), e
);
124 public synchronized void dispose() {
125 CtfIteratorManager
.removeTrace(this);
126 if (fTrace
!= null) {
140 * @return IStatus IStatus.error or Status.OK_STATUS
141 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
145 public IStatus
validate(final IProject project
, final String path
) {
146 IStatus validTrace
= Status
.OK_STATUS
;
148 final CTFTrace temp
= new CTFTrace(path
);
149 if (!temp
.majortIsSet()) {
150 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
152 CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);
153 if (!ctfTraceReader
.hasMoreEvents()) {
154 // TODO: This will need an additional check when we support live traces
155 // because having no event is valid for a live trace
156 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
158 ctfTraceReader
.dispose();
161 } catch (final CTFReaderException e
) {
162 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+": " + e
.toString()); //$NON-NLS-1$
168 * Method getCurrentLocation. This is not applicable in CTF
170 * @return null, since the trace has no knowledge of the current location
171 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
174 public ITmfLocation
getCurrentLocation() {
179 public double getLocationRatio(ITmfLocation location
) {
180 final CtfLocation curLocation
= (CtfLocation
) location
;
181 final CtfTmfContext context
= new CtfTmfContext(this);
182 context
.setLocation(curLocation
);
183 context
.seek(curLocation
.getLocationInfo());
184 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
185 final long startTime
= getIterator(this, context
).getStartTime();
186 final long endTime
= getIterator(this, context
).getEndTime();
187 return ((double) currentTime
.getTimestamp() - startTime
)
188 / (endTime
- startTime
);
196 * @return ITmfContext
199 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
200 CtfLocation currentLocation
= (CtfLocation
) location
;
201 CtfTmfContext context
= new CtfTmfContext(this);
202 if (fTrace
== null) {
203 context
.setLocation(null);
204 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
208 * The rank is set to 0 if the iterator seeks the beginning. If not, it
209 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
212 if (currentLocation
== null) {
213 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
216 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
217 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
219 context
.setLocation(currentLocation
);
220 if (location
== null) {
221 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
223 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
226 if (context
.getRank() != 0) {
227 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
233 public synchronized ITmfContext
seekEvent(double ratio
) {
234 CtfTmfContext context
= new CtfTmfContext(this);
235 if (fTrace
== null) {
236 context
.setLocation(null);
237 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
240 final long end
= this.getEndTime().getValue();
241 final long start
= this.getStartTime().getValue();
242 final long diff
= end
- start
;
243 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
244 context
.seek(ratioTs
);
245 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
250 * Method readNextEvent.
254 * @return CtfTmfEvent
255 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
258 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
259 if (fTrace
== null) {
262 CtfTmfEvent event
= null;
263 if (context
instanceof CtfTmfContext
) {
264 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
267 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
268 event
= ctfContext
.getCurrentEvent();
271 updateAttributes(context
, event
.getTimestamp());
272 ctfContext
.advance();
273 ctfContext
.increaseRank();
281 * gets the CTFtrace that this is wrapping
283 * @return the CTF trace
285 public CTFTrace
getCTFTrace() {
290 * Ctf traces have a clock with a unique uuid that will be used to identify
291 * the host. Traces with the same clock uuid will be known to have been made
292 * on the same machine.
294 * Note: uuid is an optional field, it may not be there for a clock.
297 public String
getHostId() {
298 CTFClock clock
= getCTFTrace().getClock();
300 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
301 if (clockHost
!= null) {
305 return super.getHostId();
308 // -------------------------------------------
309 // ITmfTraceProperties
310 // -------------------------------------------
316 public Map
<String
, String
> getTraceProperties() {
317 return Collections
.unmodifiableMap(fTrace
.getEnvironment());
320 // -------------------------------------------
322 // -------------------------------------------
325 * gets the clock offset
327 * @return the clock offset in ns
329 public long getOffset() {
330 if (fTrace
!= null) {
331 return fTrace
.getOffset();
337 * Returns whether or not an event is in the metadata of the trace,
338 * therefore if it can possibly be in the trace. It does not verify whether
339 * or not the event is actually in the trace
342 * The name of the event to check
343 * @return Whether the event is in the metadata or not
346 public boolean hasEvent(final String eventName
) {
347 Map
<Long
, IEventDeclaration
> events
= fTrace
.getEvents(0L);
348 if (events
!= null) {
349 for (IEventDeclaration decl
: events
.values()) {
350 if (decl
.getName().equals(eventName
)) {
359 * Return whether all requested events are in the metadata
362 * The array of events to check for
363 * @return Whether all events are in the metadata
366 public boolean hasAllEvents(String
[] names
) {
367 for (String name
: names
) {
368 if (!hasEvent(name
)) {
376 * Returns whether the metadata contains at least one of the requested
380 * The array of event names of check for
381 * @return Whether one of the event is present in trace metadata
384 public boolean hasAtLeastOneOfEvents(String
[] names
) {
385 for (String name
: names
) {
386 if (hasEvent(name
)) {
393 // -------------------------------------------
395 // -------------------------------------------
398 public CtfTmfEvent
parseEvent(ITmfContext context
) {
399 CtfTmfEvent event
= null;
400 if (context
instanceof CtfTmfContext
) {
401 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
402 event
= getNext(tmpContext
);
408 * Sets the cache size for a CtfTmfTrace.
410 protected void setCacheSize() {
411 setCacheSize(DEFAULT_CACHE_SIZE
);
414 // -------------------------------------------
416 // -------------------------------------------
418 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
419 return CtfIteratorManager
.getIterator(trace
, context
);
423 * Get an iterator to the trace
425 * @return an iterator to the trace
428 public CtfIterator
createIterator() {
429 return new CtfIterator(this);