1 /*******************************************************************************
2 * Copyright (c) 2012 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
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
12 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
14 import org
.eclipse
.core
.resources
.IProject
;
15 import org
.eclipse
.core
.resources
.IResource
;
16 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
18 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
28 * The CTf trace handler
31 * @author Matthew khouzam
33 public class CtfTmfTrace
extends TmfTrace
implements ITmfEventParser
{
36 //-------------------------------------------
38 //-------------------------------------------
40 * Default cache size for CTF traces
42 protected static final int DEFAULT_CACHE_SIZE
= 50000;
44 //-------------------------------------------
46 //-------------------------------------------
48 /* Reference to the CTF Trace */
49 private CTFTrace fTrace
;
53 //-------------------------------------------
55 //-------------------------------------------
60 * The resource associated with this trace
62 * The path to the trace file
64 * The type of events that will be read from this trace
65 * @throws TmfTraceException
66 * If something when wrong while reading the trace
69 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
70 throws TmfTraceException
{
72 * Set the cache size. This has to be done before the call to super()
73 * because the super needs to know the cache size.
77 @SuppressWarnings("unused")
81 this.fTrace
= new CTFTrace(path
);
82 CtfIteratorManager
.addTrace(this);
84 /* Set the start and (current) end times for this trace */
85 ctx
= (CtfTmfContext
) seekEvent(0L);
86 CtfTmfEvent event
= getNext(ctx
);
87 if((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
88 /* Handle the case where the trace is empty */
89 this.setStartTime(TmfTimestamp
.BIG_BANG
);
91 final ITmfTimestamp curTime
= event
.getTimestamp();
92 this.setStartTime(curTime
);
93 this.setEndTime(curTime
);
96 } catch (final CTFReaderException e
) {
98 * If it failed at the init(), we can assume it's because the file
99 * was not found or was not recognized as a CTF trace. Throw into
100 * the new type of exception expected by the rest of TMF.
102 throw new TmfTraceException(e
.getMessage(), e
);
105 super.initTrace(resource
, path
, eventType
);
109 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
112 public synchronized void dispose() {
113 CtfIteratorManager
.removeTrace(this);
114 if (fTrace
!= null) {
123 * @param project IProject
126 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
129 public boolean validate(final IProject project
, final String path
) {
131 final CTFTrace temp
= new CTFTrace(path
);
132 boolean valid
= temp
.majortIsSet(); // random test
135 } catch (final CTFReaderException e
) {
136 /* Nope, not a CTF trace we can read */
142 * Method getCurrentLocation. This is not applicable in CTF
143 * @return null, since the trace has no knowledge of the current location
144 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
147 public ITmfLocation
getCurrentLocation() {
152 public double getLocationRatio(ITmfLocation location
) {
153 final CtfLocation curLocation
= (CtfLocation
) location
;
154 final CtfTmfContext context
= new CtfTmfContext(this);
155 context
.setLocation(curLocation
);
156 context
.seek(curLocation
.getLocationInfo());
157 final CtfLocationData currentTime
= ((CtfLocationData
)context
.getLocation().getLocationInfo());
158 final long startTime
= getIterator(this, context
).getStartTime();
159 final long endTime
= getIterator(this, context
).getEndTime();
160 return ((double) currentTime
.getTimestamp() - startTime
)
161 / (endTime
- startTime
);
166 * @param location ITmfLocation<?>
167 * @return ITmfContext
170 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
171 CtfLocation currentLocation
= (CtfLocation
) location
;
172 CtfTmfContext context
= new CtfTmfContext(this);
173 if (fTrace
== null) {
174 context
.setLocation(null);
175 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
179 * The rank is set to 0 if the iterator seeks the beginning. If not, it
180 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
183 if (currentLocation
== null) {
184 currentLocation
= new CtfLocation(new CtfLocationData(0L, 0L));
187 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
188 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
190 context
.setLocation(currentLocation
);
191 if (location
== null) {
192 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
194 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
197 if(context
.getRank() != 0) {
198 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
205 public synchronized ITmfContext
seekEvent(double ratio
) {
206 CtfTmfContext context
= new CtfTmfContext(this);
207 if (fTrace
== null) {
208 context
.setLocation(null);
209 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
212 final long end
= this.getEndTime().getValue();
213 final long start
= this.getStartTime().getValue();
214 final long diff
= end
- start
;
215 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
216 context
.seek(ratioTs
);
217 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
222 * Method readNextEvent.
223 * @param context ITmfContext
224 * @return CtfTmfEvent
225 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
228 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
229 if (fTrace
== null) {
232 CtfTmfEvent event
= null;
233 if (context
instanceof CtfTmfContext
) {
234 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
237 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
238 event
= ctfContext
.getCurrentEvent();
241 updateAttributes(context
, event
.getTimestamp());
242 ctfContext
.advance();
243 ctfContext
.increaseRank();
251 * gets the CTFtrace that this is wrapping
252 * @return the CTF trace
254 public CTFTrace
getCTFTrace() {
259 //-------------------------------------------
260 // Environment Parameters
261 //-------------------------------------------
263 * Method getNbEnvVars.
267 public int getNbEnvVars() {
268 return this.fTrace
.getEnvironment().size();
272 * Method getEnvNames.
276 public String
[] getEnvNames() {
277 final String
[] s
= new String
[getNbEnvVars()];
278 return this.fTrace
.getEnvironment().keySet().toArray(s
);
282 * Method getEnvValue.
288 public String
getEnvValue(final String key
) {
289 return this.fTrace
.getEnvironment().get(key
);
292 //-------------------------------------------
294 //-------------------------------------------
297 * gets the clock offset
298 * @return the clock offset in ns
300 public long getOffset(){
301 if( fTrace
!= null ) {
302 return fTrace
.getOffset();
307 //-------------------------------------------
309 //-------------------------------------------
312 public CtfTmfEvent
parseEvent(ITmfContext context
) {
313 CtfTmfEvent event
= null;
314 if( context
instanceof CtfTmfContext
){
315 CtfTmfContext itt
= (CtfTmfContext
) context
.clone();
316 event
= itt
.getCurrentEvent();
322 * Sets the cache size for a CtfTmfTrace.
324 protected void setCacheSize() {
325 setCacheSize(DEFAULT_CACHE_SIZE
);
328 //-------------------------------------------
330 //-------------------------------------------
332 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
333 return CtfIteratorManager
.getIterator(trace
, context
);
337 * Get an iterator to the trace
339 * @return an iterator to the trace
342 public CtfIterator
createIterator(){
343 return new CtfIterator(this);