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
.nio
.BufferOverflowException
;
17 import java
.util
.Collections
;
20 import org
.eclipse
.core
.resources
.IProject
;
21 import org
.eclipse
.core
.resources
.IResource
;
22 import org
.eclipse
.core
.runtime
.IStatus
;
23 import org
.eclipse
.core
.runtime
.Status
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFClock
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
28 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
29 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
36 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
37 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTraceProperties
;
38 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
41 * The CTf trace handler
44 * @author Matthew khouzam
46 public class CtfTmfTrace
extends TmfTrace
47 implements ITmfEventParser
, ITmfTraceProperties
{
49 // -------------------------------------------
51 // -------------------------------------------
53 * Default cache size for CTF traces
55 protected static final int DEFAULT_CACHE_SIZE
= 50000;
58 * The Ctf clock unique identifier field
60 private static final String CLOCK_HOST_PROPERTY
= "uuid"; //$NON-NLS-1$
62 // -------------------------------------------
64 // -------------------------------------------
66 /* Reference to the CTF Trace */
67 private CTFTrace fTrace
;
69 // -------------------------------------------
71 // -------------------------------------------
76 * The resource associated with this trace
78 * The path to the trace file
80 * The type of events that will be read from this trace
81 * @throws TmfTraceException
82 * If something when wrong while reading the trace
85 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
86 throws TmfTraceException
{
88 * Set the cache size. This has to be done before the call to super()
89 * because the super needs to know the cache size.
93 super.initTrace(resource
, path
, eventType
);
95 @SuppressWarnings("unused")
99 this.fTrace
= new CTFTrace(path
);
100 CtfIteratorManager
.addTrace(this);
102 /* Set the start and (current) end times for this trace */
103 ctx
= (CtfTmfContext
) seekEvent(0L);
104 CtfTmfEvent event
= getNext(ctx
);
105 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
106 /* Handle the case where the trace is empty */
107 this.setStartTime(TmfTimestamp
.BIG_BANG
);
109 final ITmfTimestamp curTime
= event
.getTimestamp();
110 this.setStartTime(curTime
);
111 this.setEndTime(curTime
);
114 } catch (final CTFReaderException e
) {
116 * If it failed at the init(), we can assume it's because the file
117 * was not found or was not recognized as a CTF trace. Throw into
118 * the new type of exception expected by the rest of TMF.
120 throw new TmfTraceException(e
.getMessage(), e
);
125 public synchronized void dispose() {
126 CtfIteratorManager
.removeTrace(this);
127 if (fTrace
!= null) {
141 * @return IStatus IStatus.error or Status.OK_STATUS
142 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
146 public IStatus
validate(final IProject project
, final String path
) {
147 IStatus validTrace
= Status
.OK_STATUS
;
149 final CTFTrace temp
= new CTFTrace(path
);
150 if (!temp
.majortIsSet()) {
151 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
153 CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);
154 if (!ctfTraceReader
.hasMoreEvents()) {
155 // TODO: This will need an additional check when we support live traces
156 // because having no event is valid for a live trace
157 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
159 ctfTraceReader
.dispose();
162 } catch (final CTFReaderException e
) {
163 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+": " + e
.toString()); //$NON-NLS-1$
164 } catch (final BufferOverflowException e
){
165 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+": " + Messages
.CtfTmfTrace_BufferOverflowErrorMessage
); //$NON-NLS-1$
172 * Method getCurrentLocation. This is not applicable in CTF
174 * @return null, since the trace has no knowledge of the current location
175 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
178 public ITmfLocation
getCurrentLocation() {
183 public double getLocationRatio(ITmfLocation location
) {
184 final CtfLocation curLocation
= (CtfLocation
) location
;
185 final CtfTmfContext context
= new CtfTmfContext(this);
186 context
.setLocation(curLocation
);
187 context
.seek(curLocation
.getLocationInfo());
188 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
189 final long startTime
= getIterator(this, context
).getStartTime();
190 final long endTime
= getIterator(this, context
).getEndTime();
191 return ((double) currentTime
.getTimestamp() - startTime
)
192 / (endTime
- startTime
);
200 * @return ITmfContext
203 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
204 CtfLocation currentLocation
= (CtfLocation
) location
;
205 CtfTmfContext context
= new CtfTmfContext(this);
206 if (fTrace
== null) {
207 context
.setLocation(null);
208 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
212 * The rank is set to 0 if the iterator seeks the beginning. If not, it
213 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
216 if (currentLocation
== null) {
217 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
220 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
221 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
223 context
.setLocation(currentLocation
);
224 if (location
== null) {
225 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
227 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
230 if (context
.getRank() != 0) {
231 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
237 public synchronized ITmfContext
seekEvent(double ratio
) {
238 CtfTmfContext context
= new CtfTmfContext(this);
239 if (fTrace
== null) {
240 context
.setLocation(null);
241 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
244 final long end
= this.getEndTime().getValue();
245 final long start
= this.getStartTime().getValue();
246 final long diff
= end
- start
;
247 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
248 context
.seek(ratioTs
);
249 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
254 * Method readNextEvent.
258 * @return CtfTmfEvent
259 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
262 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
263 if (fTrace
== null) {
266 CtfTmfEvent event
= null;
267 if (context
instanceof CtfTmfContext
) {
268 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
271 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
272 event
= ctfContext
.getCurrentEvent();
275 updateAttributes(context
, event
.getTimestamp());
276 ctfContext
.advance();
277 ctfContext
.increaseRank();
285 * gets the CTFtrace that this is wrapping
287 * @return the CTF trace
289 public CTFTrace
getCTFTrace() {
294 * Ctf traces have a clock with a unique uuid that will be used to identify
295 * the host. Traces with the same clock uuid will be known to have been made
296 * on the same machine.
298 * Note: uuid is an optional field, it may not be there for a clock.
301 public String
getHostId() {
302 CTFClock clock
= getCTFTrace().getClock();
304 String clockHost
= (String
) clock
.getProperty(CLOCK_HOST_PROPERTY
);
305 if (clockHost
!= null) {
309 return super.getHostId();
312 // -------------------------------------------
313 // ITmfTraceProperties
314 // -------------------------------------------
320 public Map
<String
, String
> getTraceProperties() {
321 return Collections
.unmodifiableMap(fTrace
.getEnvironment());
324 // -------------------------------------------
326 // -------------------------------------------
329 * gets the clock offset
331 * @return the clock offset in ns
333 public long getOffset() {
334 if (fTrace
!= null) {
335 return fTrace
.getOffset();
341 * Returns whether or not an event is in the metadata of the trace,
342 * therefore if it can possibly be in the trace. It does not verify whether
343 * or not the event is actually in the trace
346 * The name of the event to check
347 * @return Whether the event is in the metadata or not
350 public boolean hasEvent(final String eventName
) {
351 Map
<Long
, IEventDeclaration
> events
= fTrace
.getEvents(0L);
352 if (events
!= null) {
353 for (IEventDeclaration decl
: events
.values()) {
354 if (decl
.getName().equals(eventName
)) {
363 * Return whether all requested events are in the metadata
366 * The array of events to check for
367 * @return Whether all events are in the metadata
370 public boolean hasAllEvents(String
[] names
) {
371 for (String name
: names
) {
372 if (!hasEvent(name
)) {
380 * Returns whether the metadata contains at least one of the requested
384 * The array of event names of check for
385 * @return Whether one of the event is present in trace metadata
388 public boolean hasAtLeastOneOfEvents(String
[] names
) {
389 for (String name
: names
) {
390 if (hasEvent(name
)) {
397 // -------------------------------------------
399 // -------------------------------------------
402 public CtfTmfEvent
parseEvent(ITmfContext context
) {
403 CtfTmfEvent event
= null;
404 if (context
instanceof CtfTmfContext
) {
405 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
406 event
= getNext(tmpContext
);
412 * Sets the cache size for a CtfTmfTrace.
414 protected void setCacheSize() {
415 setCacheSize(DEFAULT_CACHE_SIZE
);
418 // -------------------------------------------
420 // -------------------------------------------
422 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
423 return CtfIteratorManager
.getIterator(trace
, context
);
427 * Get an iterator to the trace
429 * @return an iterator to the trace
432 public CtfIterator
createIterator() {
433 return new CtfIterator(this);