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 org
.eclipse
.core
.resources
.IProject
;
17 import org
.eclipse
.core
.resources
.IResource
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
30 * The CTf trace handler
33 * @author Matthew khouzam
35 public class CtfTmfTrace
extends TmfTrace
implements ITmfEventParser
{
38 //-------------------------------------------
40 //-------------------------------------------
42 * Default cache size for CTF traces
44 protected static final int DEFAULT_CACHE_SIZE
= 50000;
46 //-------------------------------------------
48 //-------------------------------------------
50 /* Reference to the CTF Trace */
51 private CTFTrace fTrace
;
55 //-------------------------------------------
57 //-------------------------------------------
62 * The resource associated with this trace
64 * The path to the trace file
66 * The type of events that will be read from this trace
67 * @throws TmfTraceException
68 * If something when wrong while reading the trace
71 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
72 throws TmfTraceException
{
74 * Set the cache size. This has to be done before the call to super()
75 * because the super needs to know the cache size.
79 super.initTrace(resource
, path
, eventType
);
81 @SuppressWarnings("unused")
85 this.fTrace
= new CTFTrace(path
);
86 CtfIteratorManager
.addTrace(this);
88 /* Set the start and (current) end times for this trace */
89 ctx
= (CtfTmfContext
) seekEvent(0L);
90 CtfTmfEvent event
= getNext(ctx
);
91 if((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
92 /* Handle the case where the trace is empty */
93 this.setStartTime(TmfTimestamp
.BIG_BANG
);
95 final ITmfTimestamp curTime
= event
.getTimestamp();
96 this.setStartTime(curTime
);
97 this.setEndTime(curTime
);
100 } catch (final CTFReaderException e
) {
102 * If it failed at the init(), we can assume it's because the file
103 * was not found or was not recognized as a CTF trace. Throw into
104 * the new type of exception expected by the rest of TMF.
106 throw new TmfTraceException(e
.getMessage(), e
);
111 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
114 public synchronized void dispose() {
115 CtfIteratorManager
.removeTrace(this);
116 if (fTrace
!= null) {
125 * @param project IProject
128 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
131 public boolean validate(final IProject project
, final String path
) {
133 final CTFTrace temp
= new CTFTrace(path
);
134 boolean valid
= temp
.majortIsSet(); // random test
137 } catch (final CTFReaderException e
) {
138 /* Nope, not a CTF trace we can read */
144 * Method getCurrentLocation. This is not applicable in CTF
145 * @return null, since the trace has no knowledge of the current location
146 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
149 public ITmfLocation
getCurrentLocation() {
154 public double getLocationRatio(ITmfLocation location
) {
155 final CtfLocation curLocation
= (CtfLocation
) location
;
156 final CtfTmfContext context
= new CtfTmfContext(this);
157 context
.setLocation(curLocation
);
158 context
.seek(curLocation
.getLocationInfo());
159 final CtfLocationInfo currentTime
= ((CtfLocationInfo
)context
.getLocation().getLocationInfo());
160 final long startTime
= getIterator(this, context
).getStartTime();
161 final long endTime
= getIterator(this, context
).getEndTime();
162 return ((double) currentTime
.getTimestamp() - startTime
)
163 / (endTime
- startTime
);
168 * @param location ITmfLocation<?>
169 * @return ITmfContext
172 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
173 CtfLocation currentLocation
= (CtfLocation
) location
;
174 CtfTmfContext context
= new CtfTmfContext(this);
175 if (fTrace
== null) {
176 context
.setLocation(null);
177 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
181 * The rank is set to 0 if the iterator seeks the beginning. If not, it
182 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
185 if (currentLocation
== null) {
186 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
189 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
190 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
192 context
.setLocation(currentLocation
);
193 if (location
== null) {
194 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
196 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
199 if(context
.getRank() != 0) {
200 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
207 public synchronized ITmfContext
seekEvent(double ratio
) {
208 CtfTmfContext context
= new CtfTmfContext(this);
209 if (fTrace
== null) {
210 context
.setLocation(null);
211 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
214 final long end
= this.getEndTime().getValue();
215 final long start
= this.getStartTime().getValue();
216 final long diff
= end
- start
;
217 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
218 context
.seek(ratioTs
);
219 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
224 * Method readNextEvent.
225 * @param context ITmfContext
226 * @return CtfTmfEvent
227 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
230 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
231 if (fTrace
== null) {
234 CtfTmfEvent event
= null;
235 if (context
instanceof CtfTmfContext
) {
236 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
239 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
240 event
= ctfContext
.getCurrentEvent();
243 updateAttributes(context
, event
.getTimestamp());
244 ctfContext
.advance();
245 ctfContext
.increaseRank();
253 * gets the CTFtrace that this is wrapping
254 * @return the CTF trace
256 public CTFTrace
getCTFTrace() {
261 //-------------------------------------------
262 // Environment Parameters
263 //-------------------------------------------
265 * Method getNbEnvVars.
269 public int getNbEnvVars() {
270 return this.fTrace
.getEnvironment().size();
274 * Method getEnvNames.
278 public String
[] getEnvNames() {
279 final String
[] s
= new String
[getNbEnvVars()];
280 return this.fTrace
.getEnvironment().keySet().toArray(s
);
284 * Method getEnvValue.
290 public String
getEnvValue(final String key
) {
291 return this.fTrace
.getEnvironment().get(key
);
294 //-------------------------------------------
296 //-------------------------------------------
299 * gets the clock offset
300 * @return the clock offset in ns
302 public long getOffset(){
303 if( fTrace
!= null ) {
304 return fTrace
.getOffset();
309 //-------------------------------------------
311 //-------------------------------------------
314 public CtfTmfEvent
parseEvent(ITmfContext context
) {
315 CtfTmfEvent event
= null;
316 if (context
instanceof CtfTmfContext
) {
317 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
318 event
= getNext(tmpContext
);
324 * Sets the cache size for a CtfTmfTrace.
326 protected void setCacheSize() {
327 setCacheSize(DEFAULT_CACHE_SIZE
);
330 //-------------------------------------------
332 //-------------------------------------------
334 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
335 return CtfIteratorManager
.getIterator(trace
, context
);
339 * Get an iterator to the trace
341 * @return an iterator to the trace
344 public CtfIterator
createIterator(){
345 return new CtfIterator(this);