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
.trace
.CTFReaderException
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTraceReader
;
26 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
37 * The CTf trace handler
40 * @author Matthew khouzam
42 public class CtfTmfTrace
extends TmfTrace
implements ITmfEventParser
{
44 // -------------------------------------------
46 // -------------------------------------------
48 * Default cache size for CTF traces
50 protected static final int DEFAULT_CACHE_SIZE
= 50000;
52 // -------------------------------------------
54 // -------------------------------------------
56 /* Reference to the CTF Trace */
57 private CTFTrace fTrace
;
59 // -------------------------------------------
61 // -------------------------------------------
66 * The resource associated with this trace
68 * The path to the trace file
70 * The type of events that will be read from this trace
71 * @throws TmfTraceException
72 * If something when wrong while reading the trace
75 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
76 throws TmfTraceException
{
78 * Set the cache size. This has to be done before the call to super()
79 * because the super needs to know the cache size.
83 super.initTrace(resource
, path
, eventType
);
85 @SuppressWarnings("unused")
89 this.fTrace
= new CTFTrace(path
);
90 CtfIteratorManager
.addTrace(this);
92 /* Set the start and (current) end times for this trace */
93 ctx
= (CtfTmfContext
) seekEvent(0L);
94 CtfTmfEvent event
= getNext(ctx
);
95 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
96 /* Handle the case where the trace is empty */
97 this.setStartTime(TmfTimestamp
.BIG_BANG
);
99 final ITmfTimestamp curTime
= event
.getTimestamp();
100 this.setStartTime(curTime
);
101 this.setEndTime(curTime
);
104 } catch (final CTFReaderException e
) {
106 * If it failed at the init(), we can assume it's because the file
107 * was not found or was not recognized as a CTF trace. Throw into
108 * the new type of exception expected by the rest of TMF.
110 throw new TmfTraceException(e
.getMessage(), e
);
115 public synchronized void dispose() {
116 CtfIteratorManager
.removeTrace(this);
117 if (fTrace
!= null) {
131 * @return IStatus IStatus.error or Status.OK_STATUS
132 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
136 public IStatus
validate(final IProject project
, final String path
) {
137 IStatus validTrace
= Status
.OK_STATUS
;
139 final CTFTrace temp
= new CTFTrace(path
);
140 if (!temp
.majortIsSet()) {
141 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
143 CTFTraceReader ctfTraceReader
= new CTFTraceReader(temp
);
144 if (!ctfTraceReader
.hasMoreEvents()) {
145 // TODO: This will need an additional check when we support live traces
146 // because having no event is valid for a live trace
147 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_NoEvent
);
149 ctfTraceReader
.dispose();
152 } catch (final CTFReaderException e
) {
153 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+": " + e
.toString()); //$NON-NLS-1$
159 * Method getCurrentLocation. This is not applicable in CTF
161 * @return null, since the trace has no knowledge of the current location
162 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
165 public ITmfLocation
getCurrentLocation() {
170 public double getLocationRatio(ITmfLocation location
) {
171 final CtfLocation curLocation
= (CtfLocation
) location
;
172 final CtfTmfContext context
= new CtfTmfContext(this);
173 context
.setLocation(curLocation
);
174 context
.seek(curLocation
.getLocationInfo());
175 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
176 final long startTime
= getIterator(this, context
).getStartTime();
177 final long endTime
= getIterator(this, context
).getEndTime();
178 return ((double) currentTime
.getTimestamp() - startTime
)
179 / (endTime
- startTime
);
187 * @return ITmfContext
190 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
191 CtfLocation currentLocation
= (CtfLocation
) location
;
192 CtfTmfContext context
= new CtfTmfContext(this);
193 if (fTrace
== null) {
194 context
.setLocation(null);
195 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
199 * The rank is set to 0 if the iterator seeks the beginning. If not, it
200 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
203 if (currentLocation
== null) {
204 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
207 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
208 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
210 context
.setLocation(currentLocation
);
211 if (location
== null) {
212 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
214 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
217 if (context
.getRank() != 0) {
218 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
224 public synchronized ITmfContext
seekEvent(double ratio
) {
225 CtfTmfContext context
= new CtfTmfContext(this);
226 if (fTrace
== null) {
227 context
.setLocation(null);
228 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
231 final long end
= this.getEndTime().getValue();
232 final long start
= this.getStartTime().getValue();
233 final long diff
= end
- start
;
234 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
235 context
.seek(ratioTs
);
236 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
241 * Method readNextEvent.
245 * @return CtfTmfEvent
246 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
249 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
250 if (fTrace
== null) {
253 CtfTmfEvent event
= null;
254 if (context
instanceof CtfTmfContext
) {
255 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
258 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
259 event
= ctfContext
.getCurrentEvent();
262 updateAttributes(context
, event
.getTimestamp());
263 ctfContext
.advance();
264 ctfContext
.increaseRank();
272 * gets the CTFtrace that this is wrapping
274 * @return the CTF trace
276 public CTFTrace
getCTFTrace() {
280 // -------------------------------------------
281 // Environment Parameters
282 // -------------------------------------------
285 * Get the map of environment variables of this trace.
287 * @return The map of env vars
290 public Map
<String
, String
> getEnvironment() {
291 return Collections
.unmodifiableMap(fTrace
.getEnvironment());
294 // -------------------------------------------
296 // -------------------------------------------
299 * gets the clock offset
301 * @return the clock offset in ns
303 public long getOffset() {
304 if (fTrace
!= null) {
305 return fTrace
.getOffset();
310 // -------------------------------------------
312 // -------------------------------------------
315 public CtfTmfEvent
parseEvent(ITmfContext context
) {
316 CtfTmfEvent event
= null;
317 if (context
instanceof CtfTmfContext
) {
318 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
319 event
= getNext(tmpContext
);
325 * Sets the cache size for a CtfTmfTrace.
327 protected void setCacheSize() {
328 setCacheSize(DEFAULT_CACHE_SIZE
);
331 // -------------------------------------------
333 // -------------------------------------------
335 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
336 return CtfIteratorManager
.getIterator(trace
, context
);
340 * Get an iterator to the trace
342 * @return an iterator to the trace
345 public CtfIterator
createIterator() {
346 return new CtfIterator(this);