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
.core
.runtime
.CoreException
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
19 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTimestamp
.TimestampType
;
20 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
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
<CtfTmfEvent
> implements ITmfEventParser
<CtfTmfEvent
>{
38 //-------------------------------------------
40 //-------------------------------------------
42 * Default cache size for CTF traces
44 protected static final int DEFAULT_CACHE_SIZE
= 50000;
46 //-------------------------------------------
48 //-------------------------------------------
50 /** Reference to the state system assigned to this trace */
51 protected IStateSystemQuerier ss
= null;
53 /* Reference to the CTF Trace */
54 private CTFTrace fTrace
;
58 //-------------------------------------------
60 //-------------------------------------------
65 * The resource associated with this trace
67 * The path to the trace file
69 * The type of events that will be read from this trace
70 * @throws TmfTraceException
71 * If something when wrong while reading the trace
74 public void initTrace(final IResource resource
, final String path
, final Class
<CtfTmfEvent
> eventType
)
75 throws TmfTraceException
{
77 * Set the cache size. This has to be done before the call to super()
78 * because the super needs to know the cache size.
81 super.initTrace(resource
, path
, eventType
);
83 @SuppressWarnings("unused")
87 this.fTrace
= new CTFTrace(path
);
88 CtfIteratorManager
.addTrace(this);
89 CtfTmfLightweightContext ctx
;
90 /* Set the start and (current) end times for this trace */
91 ctx
= new CtfTmfLightweightContext(this);
92 ctx
.setLocation(new CtfLocation(0L));
93 if((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
94 /* Handle the case where the trace is empty */
95 this.setStartTime(TmfTimestamp
.BIG_BANG
);
97 final ITmfTimestamp curTime
= ctx
.getCurrentEvent().getTimestamp();
98 this.setStartTime(curTime
);
99 this.setEndTime(curTime
);
102 } catch (final CTFReaderException e
) {
104 * If it failed at the init(), we can assume it's because the file
105 * was not found or was not recognized as a CTF trace. Throw into
106 * the new type of exception expected by the rest of TMF.
108 throw new TmfTraceException(e
.getMessage(), e
);
111 //FIXME This should be called via the ExperimentUpdated signal
114 /* Refresh the project, so it can pick up new files that got created. */
115 if ( resource
!= null) {
117 resource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
118 } catch (CoreException e
) {
119 throw new TmfTraceException(e
.getMessage(), e
);
125 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
128 public synchronized void dispose() {
129 CtfIteratorManager
.removeTrace(this);
135 * @param project IProject
138 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
141 public boolean validate(final IProject project
, final String path
) {
143 final CTFTrace temp
= new CTFTrace(path
);
144 return temp
.majortIsSet(); // random test
145 } catch (final CTFReaderException e
) {
146 /* Nope, not a CTF trace we can read */
152 * Method getCurrentLocation. This is not applicable in CTF
153 * @return null, since the trace has no knowledge of the current location
154 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
157 public ITmfLocation
<?
> getCurrentLocation() {
164 public double getLocationRatio(ITmfLocation
<?
> location
) {
165 final CtfLocation curLocation
= (CtfLocation
) location
;
166 final CtfTmfLightweightContext context
= new CtfTmfLightweightContext(this);
167 context
.setLocation(curLocation
);
168 context
.seek(curLocation
.getLocation());
169 final long currentTime
= ((Long
)context
.getLocation().getLocation());
170 final long startTime
= getIterator(this, context
).getStartTime();
171 final long endTime
= getIterator(this, context
).getEndTime();
172 return ((double) currentTime
- startTime
)
173 / (endTime
- startTime
);
181 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
184 public synchronized ITmfContext
seekEvent(ITmfTimestamp timestamp
) {
185 if( timestamp
instanceof CtfTmfTimestamp
){
186 CtfTmfLightweightContext iter
= new CtfTmfLightweightContext(this);
187 iter
.seek(timestamp
.getValue());
190 return super.seekEvent(timestamp
);
195 * @param location ITmfLocation<?>
196 * @return ITmfContext
199 public ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
200 CtfLocation currentLocation
= (CtfLocation
) location
;
201 CtfTmfLightweightContext context
= new CtfTmfLightweightContext(this);
203 * The rank is set to 0 if the iterator seeks the beginning. If not, it
204 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
207 if (currentLocation
== null) {
208 currentLocation
= new CtfLocation(0L);
211 if (currentLocation
.getLocation() == CtfLocation
.INVALID_LOCATION
) {
212 ((CtfTmfTimestamp
) getEndTime()).setType(TimestampType
.NANOS
);
213 currentLocation
.setLocation(getEndTime().getValue() + 1);
215 context
.setLocation(currentLocation
);
216 if(context
.getRank() != 0) {
217 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
224 public ITmfContext
seekEvent(double ratio
) {
225 CtfTmfLightweightContext context
= new CtfTmfLightweightContext(this);
226 context
.seek((long) (this.getNbEvents() * ratio
));
227 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
232 * Method readNextEvent.
233 * @param context ITmfContext
234 * @return CtfTmfEvent
235 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
238 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
239 CtfTmfEvent event
= null;
240 if (context
instanceof CtfTmfLightweightContext
) {
241 if (CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocation())) {
244 CtfTmfLightweightContext ctfContext
= (CtfTmfLightweightContext
) context
;
245 event
= ctfContext
.getCurrentEvent();
248 updateAttributes(context
, event
.getTimestamp());
249 ctfContext
.advance();
250 ctfContext
.increaseRank();
258 * Suppressing the warning, because the 'throws' will usually happen in
261 * @throws TmfTraceException
263 @SuppressWarnings("unused")
264 protected void buildStateSystem() throws TmfTraceException
{
266 * Nothing is done in the basic implementation, please specify
267 * how/if to build a state system in derived classes.
273 * Method getStateSystem.
275 * @return IStateSystemQuerier
277 public IStateSystemQuerier
getStateSystem() {
282 * gets the CTFtrace that this is wrapping
283 * @return the CTF trace
285 public CTFTrace
getCTFTrace() {
290 //-------------------------------------------
291 // Environment Parameters
292 //-------------------------------------------
294 * Method getNbEnvVars.
298 public int getNbEnvVars() {
299 return this.fTrace
.getEnvironment().size();
303 * Method getEnvNames.
307 public String
[] getEnvNames() {
308 final String
[] s
= new String
[getNbEnvVars()];
309 return this.fTrace
.getEnvironment().keySet().toArray(s
);
313 * Method getEnvValue.
319 public String
getEnvValue(final String key
) {
320 return this.fTrace
.getEnvironment().get(key
);
323 //-------------------------------------------
325 //-------------------------------------------
328 * gets the clock offset
329 * @return the clock offset in ns
331 public long getOffset(){
332 if( fTrace
!= null ) {
333 return fTrace
.getOffset();
338 //-------------------------------------------
340 //-------------------------------------------
343 public CtfTmfEvent
parseEvent(ITmfContext context
) {
344 CtfTmfEvent event
= null;
345 if( context
instanceof CtfTmfLightweightContext
){
346 CtfTmfLightweightContext itt
= (CtfTmfLightweightContext
) context
.clone();
347 event
= itt
.getCurrentEvent();
353 * Sets the cache size for a CtfTmfTrace.
355 protected void setCacheSize() {
356 setCacheSize(DEFAULT_CACHE_SIZE
);
359 //-------------------------------------------
361 //-------------------------------------------
363 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfLightweightContext context
) {
364 return CtfIteratorManager
.getIterator(trace
, context
);