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
.signal
.TmfSignalManager
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfEventParser
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
33 public class CtfTmfTrace
extends TmfTrace
<CtfTmfEvent
> implements ITmfEventParser
<CtfTmfEvent
>{
35 //-------------------------------------------
37 //-------------------------------------------
39 /* Reference to the state system assigned to this trace */
40 protected IStateSystemQuerier ss
= null;
42 /* Reference to the CTF Trace */
43 private CTFTrace fTrace
;
45 //-------------------------------------------
47 //-------------------------------------------
50 * @param resource IResource
52 * @param eventType Class<CtfTmfEvent>
53 * @throws TmfTraceException
54 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
57 public void initTrace(final IResource resource
, final String path
, final Class
<CtfTmfEvent
> eventType
)
58 throws TmfTraceException
{
59 super.initTrace(resource
, path
, eventType
);
61 ITmfEventField eventField
;
62 @SuppressWarnings("unused")
66 this.fTrace
= new CTFTrace(path
);
67 for( int i
=0 ; i
< this.fTrace
.getNbEventTypes(); i
++) {
68 ed
= this.fTrace
.getEventType(i
);
69 eventField
= parseDeclaration(ed
);
71 * Populate the event manager with event types that are there in
74 type
= new CtfTmfEventType(ed
.getName(), eventField
);
77 /* Set the start and (current) end times for this trace */
78 final CtfIterator iterator
= new CtfIterator(this, 0, 0);
79 if(iterator
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) {
80 /* Handle the case where the trace is empty */
81 this.setStartTime(TmfTimestamp
.BIG_BANG
);
83 this.setStartTime(iterator
.getCurrentEvent().getTimestamp());
87 if( iterator
.hasMoreEvents()){
88 iterator
.goToLastEvent();
90 this.setEndTime(iterator
.getCurrentEvent().getTimestamp());
93 } catch (final CTFReaderException e
) {
95 * If it failed at the init(), we can assume it's because the file
96 * was not found or was not recognized as a CTF trace. Throw into
97 * the new type of exception expected by the rest of TMF.
99 throw new TmfTraceException(e
.getMessage(), e
);
102 TmfSignalManager
.register(this);
103 //FIXME This should be called via the ExperimentUpdated signal
106 /* Refresh the project, so it can pick up new files that got created. */
107 if ( resource
!= null) {
109 resource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
110 } catch (CoreException e
) {
111 throw new TmfTraceException(e
.getMessage(), e
);
118 * @param project IProject
121 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
124 public boolean validate(@SuppressWarnings("unused") final IProject project
, final String path
) {
126 final CTFTrace temp
= new CTFTrace(path
);
127 return temp
.majortIsSet(); // random test
128 } catch (final CTFReaderException e
) {
129 /* Nope, not a CTF trace we can read */
135 * Method getCurrentLocation. This is not applicable in CTF
136 * @return null, since the trace has no knowledge of the current location
137 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
140 public ITmfLocation
<?
> getCurrentLocation() {
147 public double getLocationRatio(ITmfLocation
<?
> location
) {
148 final CtfLocation curLocation
= (CtfLocation
) location
;
149 CtfIterator iterator
= new CtfIterator(this);
150 iterator
.seek(curLocation
.getLocation());
151 return ((double) iterator
.getCurrentEvent().getTimestampValue() - iterator
153 / (iterator
.getEndTime() - iterator
.getStartTime());
158 * @param location ITmfLocation<?>
159 * @return ITmfContext
160 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
163 public ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
164 CtfLocation currentLocation
= (CtfLocation
) location
;
165 if (currentLocation
== null) {
166 currentLocation
= new CtfLocation(0L);
168 CtfIterator context
= new CtfIterator(this);
170 if (currentLocation
.getLocation() == CtfLocation
.INVALID_LOCATION
) {
171 ((CtfTmfTimestamp
) getEndTime()).setType(TimestampType
.NANOS
);
172 currentLocation
.setLocation(getEndTime().getValue() + 1);
174 context
.setLocation(currentLocation
);
175 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
181 public ITmfContext
seekEvent(double ratio
) {
182 CtfIterator context
= new CtfIterator(this);
183 context
.seek((long) (this.getNbEvents() * ratio
));
184 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
189 * Method readNextEvent.
190 * @param context ITmfContext
191 * @return CtfTmfEvent
192 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
195 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
196 CtfTmfEvent event
= null;
197 if (context
instanceof CtfIterator
) {
198 CtfIterator ctfIterator
= (CtfIterator
) context
;
199 event
= ctfIterator
.getCurrentEvent();
200 ctfIterator
.advance();
206 * Suppressing the warning, because the 'throws' will usually happen in
208 * @throws TmfTraceException
210 @SuppressWarnings({ "static-method", "unused" })
211 protected void buildStateSystem() throws TmfTraceException
{
213 * Nothing is done in the basic implementation, please specify
214 * how/if to build a state system in derived classes.
220 * Method getStateSystem.
222 * @return IStateSystemQuerier
224 public IStateSystemQuerier
getStateSystem() {
233 private static ITmfEventField
parseDeclaration(EventDeclaration ed
) {
234 EventDefinition eventDef
= ed
.createDefinition(null);
235 return new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
236 CtfTmfEvent
.parseFields(eventDef
));
240 * gets the CTFtrace that this is wrapping
241 * @return the CTF trace
243 public CTFTrace
getCTFTrace() {
248 //-------------------------------------------
249 // Environment Parameters
250 //-------------------------------------------
252 * Method getNbEnvVars.
256 public int getNbEnvVars() {
257 return this.fTrace
.getEnvironment().size();
261 * Method getEnvNames.
265 public String
[] getEnvNames() {
266 final String
[] s
= new String
[getNbEnvVars()];
267 return this.fTrace
.getEnvironment().keySet().toArray(s
);
271 * Method getEnvValue.
277 public String
getEnvValue(final String key
) {
278 return this.fTrace
.getEnvironment().get(key
);
281 //-------------------------------------------
283 //-------------------------------------------
285 public long getOffset(){
286 if( fTrace
!= null ) {
287 return fTrace
.getOffset();
292 //-------------------------------------------
294 //-------------------------------------------
297 public CtfTmfEvent
parseEvent(ITmfContext context
) {
298 CtfTmfEvent event
= null;
299 if( context
instanceof CtfIterator
){
300 CtfIterator itt
= (CtfIterator
) context
;
301 event
= itt
.getCurrentEvent();