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 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
15 import org
.eclipse
.core
.resources
.IProject
;
16 import org
.eclipse
.core
.resources
.IResource
;
17 import org
.eclipse
.core
.runtime
.CoreException
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDeclaration
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTimestamp
.TimestampType
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
32 public class CtfTmfTrace
extends TmfTrace
<CtfTmfEvent
> implements ITmfEventParser
<CtfTmfEvent
>{
34 //-------------------------------------------
36 //-------------------------------------------
38 * Default cache size for CTF traces
40 protected static final int DEFAULT_CACHE_SIZE
= 50000;
42 //-------------------------------------------
44 //-------------------------------------------
46 /** Reference to the state system assigned to this trace */
47 protected IStateSystemQuerier ss
= null;
49 /* Reference to the CTF Trace */
50 private CTFTrace fTrace
;
52 //-------------------------------------------
54 //-------------------------------------------
57 * @param resource IResource
59 * @param eventType Class<CtfTmfEvent>
60 * @throws TmfTraceException
61 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
64 public void initTrace(final IResource resource
, final String path
, final Class
<CtfTmfEvent
> eventType
)
65 throws TmfTraceException
{
67 * Set the cache size. This has to be done before the call to super()
68 * because the super needs to know the cache size.
71 super.initTrace(resource
, path
, eventType
);
73 ITmfEventField eventField
;
75 @SuppressWarnings("unused")
79 this.fTrace
= new CTFTrace(path
);
80 for( int i
=0 ; i
< this.fTrace
.getNbEventTypes(); i
++) {
81 ed
= this.fTrace
.getEventType(i
);
82 eventField
= parseDeclaration(ed
);
84 * Populate the event manager with event types that are there in
87 type
= new CtfTmfEventType(ed
.getName(), eventField
);
90 /* Set the start and (current) end times for this trace */
91 final CtfIterator iterator
= new CtfIterator(this, 0, 0);
92 if(iterator
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) {
93 /* Handle the case where the trace is empty */
94 this.setStartTime(TmfTimestamp
.BIG_BANG
);
96 this.setStartTime(iterator
.getCurrentEvent().getTimestamp());
97 this.setEndTime(iterator
.getCurrentEvent().getTimestamp());
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
);
109 //FIXME This should be called via the ExperimentUpdated signal
112 /* Refresh the project, so it can pick up new files that got created. */
113 if ( resource
!= null) {
115 resource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
116 } catch (CoreException e
) {
117 throw new TmfTraceException(e
.getMessage(), e
);
124 * @param project IProject
127 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
130 public boolean validate(@SuppressWarnings("unused") final IProject project
, final String path
) {
132 final CTFTrace temp
= new CTFTrace(path
);
133 return temp
.majortIsSet(); // random test
134 } catch (final CTFReaderException e
) {
135 /* Nope, not a CTF trace we can read */
141 * Method getCurrentLocation. This is not applicable in CTF
142 * @return null, since the trace has no knowledge of the current location
143 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
146 public ITmfLocation
<?
> getCurrentLocation() {
153 public double getLocationRatio(ITmfLocation
<?
> location
) {
154 final CtfLocation curLocation
= (CtfLocation
) location
;
155 CtfIterator iterator
= new CtfIterator(this);
156 iterator
.seek(curLocation
.getLocation());
157 return ((double) iterator
.getCurrentEvent().getTimestampValue() - iterator
159 / (iterator
.getEndTime() - iterator
.getStartTime());
164 * @param location ITmfLocation<?>
165 * @return ITmfContext
166 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
169 public ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
170 CtfLocation currentLocation
= (CtfLocation
) location
;
171 CtfIterator context
= new CtfIterator(this);
173 * The rank is set to 0 if the iterator seeks the beginning. If not, it
174 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
177 if (currentLocation
== null) {
178 currentLocation
= new CtfLocation(0L);
181 if (currentLocation
.getLocation() == CtfLocation
.INVALID_LOCATION
) {
182 ((CtfTmfTimestamp
) getEndTime()).setType(TimestampType
.NANOS
);
183 currentLocation
.setLocation(getEndTime().getValue() + 1);
185 context
.setLocation(currentLocation
);
186 if(context
.getRank() != 0)
187 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
193 public ITmfContext
seekEvent(double ratio
) {
194 CtfIterator context
= new CtfIterator(this);
195 context
.seek((long) (this.getNbEvents() * ratio
));
196 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
201 * Method readNextEvent.
202 * @param context ITmfContext
203 * @return CtfTmfEvent
204 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
207 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
208 CtfTmfEvent event
= null;
209 if (context
instanceof CtfIterator
) {
210 CtfIterator ctfIterator
= (CtfIterator
) context
;
211 event
= ctfIterator
.getCurrentEvent();
214 updateAttributes(context
, event
.getTimestamp());
215 ctfIterator
.advance();
216 ctfIterator
.increaseRank();
224 * Suppressing the warning, because the 'throws' will usually happen in
226 * @throws TmfTraceException
228 @SuppressWarnings({ "static-method", "unused" })
229 protected void buildStateSystem() throws TmfTraceException
{
231 * Nothing is done in the basic implementation, please specify
232 * how/if to build a state system in derived classes.
238 * Method getStateSystem.
240 * @return IStateSystemQuerier
242 public IStateSystemQuerier
getStateSystem() {
251 private static ITmfEventField
parseDeclaration(EventDeclaration ed
) {
252 EventDefinition eventDef
= ed
.createDefinition(null);
253 return new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
254 CtfTmfEvent
.parseFields(eventDef
));
258 * gets the CTFtrace that this is wrapping
259 * @return the CTF trace
261 public CTFTrace
getCTFTrace() {
266 //-------------------------------------------
267 // Environment Parameters
268 //-------------------------------------------
270 * Method getNbEnvVars.
274 public int getNbEnvVars() {
275 return this.fTrace
.getEnvironment().size();
279 * Method getEnvNames.
283 public String
[] getEnvNames() {
284 final String
[] s
= new String
[getNbEnvVars()];
285 return this.fTrace
.getEnvironment().keySet().toArray(s
);
289 * Method getEnvValue.
295 public String
getEnvValue(final String key
) {
296 return this.fTrace
.getEnvironment().get(key
);
299 //-------------------------------------------
301 //-------------------------------------------
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 CtfIterator
){
318 CtfIterator itt
= (CtfIterator
) context
;
319 event
= itt
.getCurrentEvent();
325 * Sets the cache size for a CtfTmfTrace.
327 protected void setCacheSize() {
328 setCacheSize(DEFAULT_CACHE_SIZE
);