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
.trace
.CTFReaderException
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
27 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceProperties
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
39 * The CTf trace handler
42 * @author Matthew khouzam
44 public class CtfTmfTrace
extends TmfTrace
45 implements ITmfEventParser
, ITmfTraceProperties
{
47 // -------------------------------------------
49 // -------------------------------------------
51 * Default cache size for CTF traces
53 protected static final int DEFAULT_CACHE_SIZE
= 50000;
55 // -------------------------------------------
57 // -------------------------------------------
59 /* Reference to the CTF Trace */
60 private CTFTrace fTrace
;
62 // -------------------------------------------
64 // -------------------------------------------
69 * The resource associated with this trace
71 * The path to the trace file
73 * The type of events that will be read from this trace
74 * @throws TmfTraceException
75 * If something when wrong while reading the trace
78 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
79 throws TmfTraceException
{
81 * Set the cache size. This has to be done before the call to super()
82 * because the super needs to know the cache size.
86 super.initTrace(resource
, path
, eventType
);
88 @SuppressWarnings("unused")
92 this.fTrace
= new CTFTrace(path
);
93 CtfIteratorManager
.addTrace(this);
95 /* Set the start and (current) end times for this trace */
96 ctx
= (CtfTmfContext
) seekEvent(0L);
97 CtfTmfEvent event
= getNext(ctx
);
98 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
99 /* Handle the case where the trace is empty */
100 this.setStartTime(TmfTimestamp
.BIG_BANG
);
102 final ITmfTimestamp curTime
= event
.getTimestamp();
103 this.setStartTime(curTime
);
104 this.setEndTime(curTime
);
107 } catch (final CTFReaderException e
) {
109 * If it failed at the init(), we can assume it's because the file
110 * was not found or was not recognized as a CTF trace. Throw into
111 * the new type of exception expected by the rest of TMF.
113 throw new TmfTraceException(e
.getMessage(), e
);
118 public synchronized void dispose() {
119 CtfIteratorManager
.removeTrace(this);
120 if (fTrace
!= null) {
134 * @return IStatus IStatus.error or Status.OK_STATUS
135 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
139 public IStatus
validate(final IProject project
, final String path
) {
140 IStatus validTrace
= Status
.OK_STATUS
;
142 final CTFTrace temp
= new CTFTrace(path
);
143 if (!temp
.majortIsSet()) {
144 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
146 CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);
147 if (!ctfTraceReader
.hasMoreEvents()) {
148 // TODO: This will need an additional check when we support live traces
149 // because having no event is valid for a live trace
150 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
152 ctfTraceReader
.dispose();
155 } catch (final CTFReaderException e
) {
156 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+": " + e
.toString()); //$NON-NLS-1$
162 * Method getCurrentLocation. This is not applicable in CTF
164 * @return null, since the trace has no knowledge of the current location
165 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
168 public ITmfLocation
getCurrentLocation() {
173 public double getLocationRatio(ITmfLocation location
) {
174 final CtfLocation curLocation
= (CtfLocation
) location
;
175 final CtfTmfContext context
= new CtfTmfContext(this);
176 context
.setLocation(curLocation
);
177 context
.seek(curLocation
.getLocationInfo());
178 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
179 final long startTime
= getIterator(this, context
).getStartTime();
180 final long endTime
= getIterator(this, context
).getEndTime();
181 return ((double) currentTime
.getTimestamp() - startTime
)
182 / (endTime
- startTime
);
190 * @return ITmfContext
193 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
194 CtfLocation currentLocation
= (CtfLocation
) location
;
195 CtfTmfContext context
= new CtfTmfContext(this);
196 if (fTrace
== null) {
197 context
.setLocation(null);
198 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
202 * The rank is set to 0 if the iterator seeks the beginning. If not, it
203 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
206 if (currentLocation
== null) {
207 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
210 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
211 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
213 context
.setLocation(currentLocation
);
214 if (location
== null) {
215 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
217 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
220 if (context
.getRank() != 0) {
221 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
227 public synchronized ITmfContext
seekEvent(double ratio
) {
228 CtfTmfContext context
= new CtfTmfContext(this);
229 if (fTrace
== null) {
230 context
.setLocation(null);
231 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
234 final long end
= this.getEndTime().getValue();
235 final long start
= this.getStartTime().getValue();
236 final long diff
= end
- start
;
237 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
238 context
.seek(ratioTs
);
239 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
244 * Method readNextEvent.
248 * @return CtfTmfEvent
249 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
252 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
253 if (fTrace
== null) {
256 CtfTmfEvent event
= null;
257 if (context
instanceof CtfTmfContext
) {
258 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
261 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
262 event
= ctfContext
.getCurrentEvent();
265 updateAttributes(context
, event
.getTimestamp());
266 ctfContext
.advance();
267 ctfContext
.increaseRank();
275 * gets the CTFtrace that this is wrapping
277 * @return the CTF trace
279 public CTFTrace
getCTFTrace() {
283 // -------------------------------------------
284 // ITmfTraceProperties
285 // -------------------------------------------
291 public Map
<String
, String
> getTraceProperties() {
292 return Collections
.unmodifiableMap(fTrace
.getEnvironment());
295 // -------------------------------------------
297 // -------------------------------------------
300 * gets the clock offset
302 * @return the clock offset in ns
304 public long getOffset() {
305 if (fTrace
!= null) {
306 return fTrace
.getOffset();
312 * Returns whether or not an event is in the metadata of the trace,
313 * therefore if it can possibly be in the trace. It does not verify whether
314 * or not the event is actually in the trace
317 * The name of the event to check
318 * @return Whether the event is in the metadata or not
321 public boolean hasEvent(final String eventName
) {
322 Map
<Long
, IEventDeclaration
> events
= fTrace
.getEvents(0L);
323 if (events
!= null) {
324 for (IEventDeclaration decl
: events
.values()) {
325 if (decl
.getName().equals(eventName
)) {
334 * Return whether all requested events are in the metadata
337 * The array of events to check for
338 * @return Whether all events are in the metadata
341 public boolean hasAllEvents(String
[] names
) {
342 for (String name
: names
) {
343 if (!hasEvent(name
)) {
351 * Returns whether the metadata contains at least one of the requested
355 * The array of event names of check for
356 * @return Whether one of the event is present in trace metadata
359 public boolean hasAtLeastOneOfEvents(String
[] names
) {
360 for (String name
: names
) {
361 if (hasEvent(name
)) {
368 // -------------------------------------------
370 // -------------------------------------------
373 public CtfTmfEvent
parseEvent(ITmfContext context
) {
374 CtfTmfEvent event
= null;
375 if (context
instanceof CtfTmfContext
) {
376 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
377 event
= getNext(tmpContext
);
383 * Sets the cache size for a CtfTmfTrace.
385 protected void setCacheSize() {
386 setCacheSize(DEFAULT_CACHE_SIZE
);
389 // -------------------------------------------
391 // -------------------------------------------
393 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
394 return CtfIteratorManager
.getIterator(trace
, context
);
398 * Get an iterator to the trace
400 * @return an iterator to the trace
403 public CtfIterator
createIterator() {
404 return new CtfIterator(this);