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);
83 CtfTmfLightweightContext ctx
;
84 /* Set the start and (current) end times for this trace */
85 ctx
= (CtfTmfLightweightContext
) 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);
120 * @param project IProject
123 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
126 public boolean validate(final IProject project
, final String path
) {
128 final CTFTrace temp
= new CTFTrace(path
);
129 return temp
.majortIsSet(); // random test
130 } catch (final CTFReaderException e
) {
131 /* Nope, not a CTF trace we can read */
137 * Method getCurrentLocation. This is not applicable in CTF
138 * @return null, since the trace has no knowledge of the current location
139 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
142 public ITmfLocation
getCurrentLocation() {
147 public double getLocationRatio(ITmfLocation location
) {
148 final CtfLocation curLocation
= (CtfLocation
) location
;
149 final CtfTmfLightweightContext context
= new CtfTmfLightweightContext(this);
150 context
.setLocation(curLocation
);
151 context
.seek(curLocation
.getLocationInfo());
152 final CtfLocationData currentTime
= ((CtfLocationData
)context
.getLocation().getLocationInfo());
153 final long startTime
= getIterator(this, context
).getStartTime();
154 final long endTime
= getIterator(this, context
).getEndTime();
155 return ((double) currentTime
.getTimestamp() - startTime
)
156 / (endTime
- startTime
);
161 * @param location ITmfLocation<?>
162 * @return ITmfContext
165 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
166 CtfLocation currentLocation
= (CtfLocation
) location
;
167 CtfTmfLightweightContext context
= new CtfTmfLightweightContext(this);
168 if (fTrace
== null) {
169 context
.setLocation(null);
170 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
174 * The rank is set to 0 if the iterator seeks the beginning. If not, it
175 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
178 if (currentLocation
== null) {
179 currentLocation
= new CtfLocation(new CtfLocationData(0L, 0L));
182 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
183 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
185 context
.setLocation(currentLocation
);
186 if (location
== null) {
187 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
189 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
192 if(context
.getRank() != 0) {
193 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
200 public synchronized ITmfContext
seekEvent(double ratio
) {
201 CtfTmfLightweightContext context
= new CtfTmfLightweightContext(this);
202 if (fTrace
== null) {
203 context
.setLocation(null);
204 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
207 final long end
= this.getEndTime().getValue();
208 final long start
= this.getStartTime().getValue();
209 final long diff
= end
- start
;
210 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
211 context
.seek(ratioTs
);
212 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
217 * Method readNextEvent.
218 * @param context ITmfContext
219 * @return CtfTmfEvent
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
223 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
224 if (fTrace
== null) {
227 CtfTmfEvent event
= null;
228 if (context
instanceof CtfTmfLightweightContext
) {
229 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
232 CtfTmfLightweightContext ctfContext
= (CtfTmfLightweightContext
) context
;
233 event
= ctfContext
.getCurrentEvent();
236 updateAttributes(context
, event
.getTimestamp());
237 ctfContext
.advance();
238 ctfContext
.increaseRank();
246 * gets the CTFtrace that this is wrapping
247 * @return the CTF trace
249 public CTFTrace
getCTFTrace() {
254 //-------------------------------------------
255 // Environment Parameters
256 //-------------------------------------------
258 * Method getNbEnvVars.
262 public int getNbEnvVars() {
263 return this.fTrace
.getEnvironment().size();
267 * Method getEnvNames.
271 public String
[] getEnvNames() {
272 final String
[] s
= new String
[getNbEnvVars()];
273 return this.fTrace
.getEnvironment().keySet().toArray(s
);
277 * Method getEnvValue.
283 public String
getEnvValue(final String key
) {
284 return this.fTrace
.getEnvironment().get(key
);
287 //-------------------------------------------
289 //-------------------------------------------
292 * gets the clock offset
293 * @return the clock offset in ns
295 public long getOffset(){
296 if( fTrace
!= null ) {
297 return fTrace
.getOffset();
302 //-------------------------------------------
304 //-------------------------------------------
307 public CtfTmfEvent
parseEvent(ITmfContext context
) {
308 CtfTmfEvent event
= null;
309 if( context
instanceof CtfTmfLightweightContext
){
310 CtfTmfLightweightContext itt
= (CtfTmfLightweightContext
) context
.clone();
311 event
= itt
.getCurrentEvent();
317 * Sets the cache size for a CtfTmfTrace.
319 protected void setCacheSize() {
320 setCacheSize(DEFAULT_CACHE_SIZE
);
323 //-------------------------------------------
325 //-------------------------------------------
327 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfLightweightContext context
) {
328 return CtfIteratorManager
.getIterator(trace
, context
);