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 java
.util
.ArrayList
;
16 import java
.util
.ListIterator
;
18 import org
.eclipse
.core
.resources
.IProject
;
19 import org
.eclipse
.core
.resources
.IResource
;
20 import org
.eclipse
.core
.runtime
.CoreException
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
.CtfTmfTimestamp
.TimestampType
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
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
>{
36 //-------------------------------------------
38 //-------------------------------------------
40 * Default cache size for CTF traces
42 protected static final int DEFAULT_CACHE_SIZE
= 50000;
43 private static final int ITER_POOL_SIZE
= 128;
45 //-------------------------------------------
47 //-------------------------------------------
49 /** Reference to the state system assigned to this trace */
50 protected IStateSystemQuerier ss
= null;
52 /* Reference to the CTF Trace */
53 private CTFTrace fTrace
;
56 * The iterator pool. This is a necessary change since larger traces will
57 * need many contexts and each context must have to a file pointer. Since
58 * the OS supports only so many handles on a given file, but the UI must
59 * still be responsive, parallel seeks (up to ITER_POOL_SIZE requests)
60 * can be made with a fast response time.
62 private ArrayList
<CtfIterator
> fIterators
;
63 private ListIterator
<CtfIterator
> nextIter
;
65 //-------------------------------------------
67 //-------------------------------------------
70 * @param resource IResource
72 * @param eventType Class<CtfTmfEvent>
73 * @throws TmfTraceException
74 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
77 public void initTrace(final IResource resource
, final String path
, final Class
<CtfTmfEvent
> eventType
)
78 throws TmfTraceException
{
80 * Set the cache size. This has to be done before the call to super()
81 * because the super needs to know the cache size.
84 super.initTrace(resource
, path
, eventType
);
86 @SuppressWarnings("unused")
90 this.fTrace
= new CTFTrace(path
);
91 fIterators
= new ArrayList
<CtfIterator
>(ITER_POOL_SIZE
);
92 for(int i
= 0 ; i
< ITER_POOL_SIZE
; i
++){
93 fIterators
.add(new CtfIterator(this, 0, 0));
95 nextIter
= fIterators
.listIterator(0);
96 /* Set the start and (current) end times for this trace */
97 final CtfIterator iterator
= getIterator();
98 if(iterator
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) {
99 /* Handle the case where the trace is empty */
100 this.setStartTime(TmfTimestamp
.BIG_BANG
);
102 this.setStartTime(iterator
.getCurrentEvent().getTimestamp());
103 this.setEndTime(iterator
.getCurrentEvent().getTimestamp());
106 } catch (final CTFReaderException e
) {
108 * If it failed at the init(), we can assume it's because the file
109 * was not found or was not recognized as a CTF trace. Throw into
110 * the new type of exception expected by the rest of TMF.
112 throw new TmfTraceException(e
.getMessage(), e
);
115 //FIXME This should be called via the ExperimentUpdated signal
118 /* Refresh the project, so it can pick up new files that got created. */
119 if ( resource
!= null) {
121 resource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
122 } catch (CoreException e
) {
123 throw new TmfTraceException(e
.getMessage(), e
);
130 * @param project IProject
133 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
136 public boolean validate(final IProject project
, final String path
) {
138 final CTFTrace temp
= new CTFTrace(path
);
139 return temp
.majortIsSet(); // random test
140 } catch (final CTFReaderException e
) {
141 /* Nope, not a CTF trace we can read */
147 * Method getCurrentLocation. This is not applicable in CTF
148 * @return null, since the trace has no knowledge of the current location
149 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
152 public ITmfLocation
<?
> getCurrentLocation() {
159 public double getLocationRatio(ITmfLocation
<?
> location
) {
160 final CtfLocation curLocation
= (CtfLocation
) location
;
161 CtfIterator iterator
= getIterator();
162 CtfTmfLightweightContext ctx
= new CtfTmfLightweightContext(fIterators
, nextIter
);
163 ctx
.setLocation(curLocation
);
164 ctx
.seek(curLocation
.getLocation());
165 long currentTime
= ((Long
)ctx
.getLocation().getLocation());
167 return ((double) currentTime
- iterator
.getStartTime())
168 / (iterator
.getEndTime() - iterator
.getStartTime());
174 private CtfIterator
getIterator() {
175 if( !nextIter
.hasNext()){
176 nextIter
= fIterators
.listIterator(0);
178 return nextIter
.next();
182 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
185 public synchronized ITmfContext
seekEvent(ITmfTimestamp timestamp
) {
186 if( timestamp
instanceof CtfTmfTimestamp
){
187 CtfTmfLightweightContext iter
= new CtfTmfLightweightContext(fIterators
, nextIter
);
188 iter
.seek(timestamp
.getValue());
191 return super.seekEvent(timestamp
);
196 * @param location ITmfLocation<?>
197 * @return ITmfContext
198 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
201 public ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
202 CtfLocation currentLocation
= (CtfLocation
) location
;
203 CtfTmfLightweightContext context
= new CtfTmfLightweightContext(fIterators
, nextIter
);
205 * The rank is set to 0 if the iterator seeks the beginning. If not, it
206 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
209 if (currentLocation
== null) {
210 currentLocation
= new CtfLocation(0L);
213 if (currentLocation
.getLocation() == CtfLocation
.INVALID_LOCATION
) {
214 ((CtfTmfTimestamp
) getEndTime()).setType(TimestampType
.NANOS
);
215 currentLocation
.setLocation(getEndTime().getValue() + 1);
217 context
.setLocation(currentLocation
);
218 if(context
.getRank() != 0) {
219 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
226 public ITmfContext
seekEvent(double ratio
) {
227 CtfTmfLightweightContext context
= new CtfTmfLightweightContext(fIterators
, nextIter
);
228 context
.seek((long) (this.getNbEvents() * ratio
));
229 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
234 * Method readNextEvent.
235 * @param context ITmfContext
236 * @return CtfTmfEvent
237 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
240 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
241 CtfTmfEvent event
= null;
242 if (context
instanceof CtfTmfLightweightContext
) {
243 CtfTmfLightweightContext ctfContext
= (CtfTmfLightweightContext
) context
;
244 event
= ctfContext
.getCurrentEvent();
247 updateAttributes(context
, event
.getTimestamp());
248 ctfContext
.advance();
249 ctfContext
.increaseRank();
257 * Suppressing the warning, because the 'throws' will usually happen in
259 * @throws TmfTraceException
261 @SuppressWarnings({ "static-method" })
262 protected void buildStateSystem() throws TmfTraceException
{
264 * Nothing is done in the basic implementation, please specify
265 * how/if to build a state system in derived classes.
271 * Method getStateSystem.
273 * @return IStateSystemQuerier
275 public IStateSystemQuerier
getStateSystem() {
280 * gets the CTFtrace that this is wrapping
281 * @return the CTF trace
283 public CTFTrace
getCTFTrace() {
288 //-------------------------------------------
289 // Environment Parameters
290 //-------------------------------------------
292 * Method getNbEnvVars.
296 public int getNbEnvVars() {
297 return this.fTrace
.getEnvironment().size();
301 * Method getEnvNames.
305 public String
[] getEnvNames() {
306 final String
[] s
= new String
[getNbEnvVars()];
307 return this.fTrace
.getEnvironment().keySet().toArray(s
);
311 * Method getEnvValue.
317 public String
getEnvValue(final String key
) {
318 return this.fTrace
.getEnvironment().get(key
);
321 //-------------------------------------------
323 //-------------------------------------------
325 public long getOffset(){
326 if( fTrace
!= null ) {
327 return fTrace
.getOffset();
332 //-------------------------------------------
334 //-------------------------------------------
337 public CtfTmfEvent
parseEvent(ITmfContext context
) {
338 CtfTmfEvent event
= null;
339 if( context
instanceof CtfTmfLightweightContext
){
340 CtfTmfLightweightContext itt
= (CtfTmfLightweightContext
) context
.clone();
341 event
= itt
.getCurrentEvent();
347 * Sets the cache size for a CtfTmfTrace.
349 protected void setCacheSize() {
350 setCacheSize(DEFAULT_CACHE_SIZE
);