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
.core
.runtime
.IPath
;
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
.component
.TmfEventProvider
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfTimestamp
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimeRange
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfTimestamp
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfDataRequest
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.request
.ITmfEventRequest
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignal
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.statesystem
.IStateSystemQuerier
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfLocation
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
39 public class CtfTmfTrace
extends TmfEventProvider
<CtfTmfEvent
> implements ITmfTrace
<CtfTmfEvent
> {
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 // ------------------------------------------------------------------------
47 // ------------------------------------------------------------------------
50 private CTFTrace fTrace
;
52 // The number of events collected
53 protected long fNbEvents
= 0;
55 // The time span of the event stream
56 private ITmfTimestamp fStartTime
= TmfTimestamp
.BIG_CRUNCH
;
57 private ITmfTimestamp fEndTime
= TmfTimestamp
.BIG_BANG
;
60 private IResource fResource
;
62 /* Reference to the state system assigned to this trace */
63 protected IStateSystemQuerier ss
= null;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 public CtfTmfTrace() {
75 * @param resource IResource
77 * @param eventType Class<CtfTmfEvent>
78 * @throws TmfTraceException
79 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
82 public void initTrace(final IResource resource
, final String path
, final Class
<CtfTmfEvent
> eventType
)
83 throws TmfTraceException
{
84 this.fResource
= resource
;
86 this.fTrace
= new CTFTrace(path
);
87 for( int i
=0 ; i
< this.fTrace
.getNbEventTypes(); i
++) {
88 EventDeclaration ed
= this.fTrace
.getEventType(i
);
89 ITmfEventField eventField
= parseDeclaration(ed
);
90 new CtfTmfEventType(ed
.getName(), eventField
);
92 } catch (final CTFReaderException e
) {
94 * If it failed at the init(), we can assume it's because the file
95 * was not found or was not recognized as a CTF trace. Throw into
96 * the new type of exception expected by the rest of TMF.
98 throw new TmfTraceException(e
.getMessage());
100 CtfIterator iterator
= new CtfIterator(this, 0, 0);
101 setStartTime(TmfTimestamp
.BIG_BANG
);
102 if( !iterator
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) {
103 setStartTime(iterator
.getCurrentEvent().getTimestamp());
105 TmfSignalManager
.register(this);
106 // FIXME this should become a request
109 /* Refresh the project, so it can pick up new files that got created. */
110 if ( resource
!= null) {
112 resource
.getProject().refreshLocal(IResource
.DEPTH_INFINITE
, null);
113 } catch (CoreException e
) {
114 throw new TmfTraceException(e
.getMessage());
119 private static ITmfEventField
parseDeclaration(EventDeclaration ed
) {
120 EventDefinition eventDef
= ed
.createDefinition(null);
121 return new CtfTmfContent(ITmfEventField
.ROOT_FIELD_ID
,
122 CtfTmfEvent
.parseFields(eventDef
));
127 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#dispose()
130 public void dispose() {
131 TmfSignalManager
.deregister(this);
136 * @param signal TmfSignal
137 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#broadcast(TmfSignal)
140 public void broadcast(final TmfSignal signal
) {
141 TmfSignalManager
.dispatchSignal(signal
);
146 * @param project IProject
149 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
152 public boolean validate(final IProject project
, final String path
) {
154 final CTFTrace temp
= new CTFTrace(path
);
155 return temp
.majortIsSet(); // random test
156 } catch (final CTFReaderException e
) {
157 /* Nope, not a CTF trace we can read */
162 // ------------------------------------------------------------------------
164 // ------------------------------------------------------------------------
167 * Method getEventType.
168 * @return the trace path
169 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEventType()
172 public Class
<CtfTmfEvent
> getEventType() {
177 * Method getNbEnvVars.
180 public int getNbEnvVars() {
181 return this.fTrace
.getEnvironment().size();
186 * Method getEnvNames.
189 public String
[] getEnvNames() {
190 final String
[] s
= new String
[getNbEnvVars()];
191 return this.fTrace
.getEnvironment().keySet().toArray(s
);
195 * Method getEnvValue.
199 public String
getEnvValue(final String key
) {
200 return this.fTrace
.getEnvironment().get(key
);
206 * @return the trace path * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getPath()
209 public String
getPath() {
210 return this.fTrace
.getPath();
216 * @see org.eclipse.linuxtools.tmf.core.component.ITmfComponent#getName()
219 public String
getName() {
220 String traceName
= (fResource
!= null) ? fResource
.getName() : null;
221 // If no resource was provided, extract the display name the trace path
222 if (traceName
== null) {
223 final String path
= this.fTrace
.getPath();
224 final int sep
= path
.lastIndexOf(IPath
.SEPARATOR
);
225 traceName
= (sep
>= 0) ? path
.substring(sep
+ 1) : path
;
231 * Method getCacheSize.
233 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCacheSize()
236 public int getCacheSize() {
237 return 50000; // not true, but it works
241 * Method getNbEvents.
243 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNbEvents()
246 public long getNbEvents() {
247 return this.fNbEvents
;
251 * Method getTimeRange.
252 * @return TmfTimeRange
253 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getTimeRange()
256 public TmfTimeRange
getTimeRange() {
257 return new TmfTimeRange(this.fStartTime
, this.fEndTime
);
261 * Method getStartTime.
262 * @return ITmfTimestamp
263 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStartTime()
266 public ITmfTimestamp
getStartTime() {
267 return this.fStartTime
;
272 * @return ITmfTimestamp
273 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getEndTime()
276 public ITmfTimestamp
getEndTime() {
277 return this.fEndTime
;
281 * Method getCurrentLocation. This is not applicable in CTF
282 * @return null, since the trace has no knowledge of the current location
283 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
286 public ITmfLocation
<?
> getCurrentLocation() {
290 // ------------------------------------------------------------------------
292 // ------------------------------------------------------------------------
295 * Method setTimeRange.
296 * @param range TmfTimeRange
298 protected void setTimeRange(final TmfTimeRange range
) {
299 this.fStartTime
= range
.getStartTime();
300 this.fEndTime
= range
.getEndTime();
304 * Method setStartTime.
305 * @param startTime ITmfTimestamp
307 protected void setStartTime(final ITmfTimestamp startTime
) {
308 this.fStartTime
= startTime
;
313 * @param endTime ITmfTimestamp
315 protected void setEndTime(final ITmfTimestamp endTime
) {
316 this.fEndTime
= endTime
;
319 // ------------------------------------------------------------------------
321 // ------------------------------------------------------------------------
325 * @param request ITmfDataRequest<CtfTmfEvent>
326 * @return ITmfContext
329 public ITmfContext
armRequest(final ITmfDataRequest
<CtfTmfEvent
> request
) {
330 if ((request
instanceof ITmfEventRequest
<?
>)
331 && !TmfTimestamp
.BIG_BANG
332 .equals(((ITmfEventRequest
<CtfTmfEvent
>) request
)
333 .getRange().getStartTime())
334 && (request
.getIndex() == 0)) {
335 final ITmfContext context
= seekEvent(((ITmfEventRequest
<CtfTmfEvent
>) request
)
336 .getRange().getStartTime());
337 ((ITmfEventRequest
<CtfTmfEvent
>) request
)
338 .setStartIndex((int) context
.getRank());
341 return seekEvent(request
.getIndex());
345 * The trace reader keeps its own iterator: the "context" parameter here
348 * If you wish to specify a new context, instantiate a new CtfIterator and
349 * seek() it to where you want, and use that to read events.
351 * FIXME merge with getNextEvent below once they both use the same parameter
353 * @param context ITmfContext
354 * @return CtfTmfEvent
357 public CtfTmfEvent
getNext(final ITmfContext context
) {
358 return readNextEvent(context
);
361 // ------------------------------------------------------------------------
363 // ------------------------------------------------------------------------
367 * @param location ITmfLocation<?>
368 * @return ITmfContext
369 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
372 public ITmfContext
seekEvent(final ITmfLocation
<?
> location
) {
373 CtfLocation currentLocation
= (CtfLocation
) location
;
374 if (currentLocation
== null) {
375 currentLocation
= new CtfLocation(0L);
377 CtfIterator context
= new CtfIterator(this);
378 context
.setLocation(currentLocation
);
379 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
384 * Method getLocationRatio.
385 * @param location ITmfLocation<?>
387 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getLocationRatio(ITmfLocation<?>)
390 public double getLocationRatio(final ITmfLocation
<?
> location
) {
391 final CtfLocation curLocation
= (CtfLocation
) location
;
392 CtfIterator iterator
= new CtfIterator(this);
393 iterator
.seek(curLocation
.getLocation());
394 return ((double) iterator
.getCurrentEvent().getTimestampValue() - iterator
396 / (iterator
.getEndTime() - iterator
.getStartTime());
400 * Method getStreamingInterval.
402 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getStreamingInterval()
405 public long getStreamingInterval() {
411 * @param timestamp ITmfTimestamp
412 * @return ITmfContext
413 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfTimestamp)
416 public ITmfContext
seekEvent(final ITmfTimestamp timestamp
) {
417 CtfIterator context
= new CtfIterator(this);
418 context
.seek(timestamp
.getValue());
419 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
426 * @return ITmfContext
427 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(long)
430 public ITmfContext
seekEvent(final long rank
) {
431 CtfIterator context
= new CtfIterator(this);
432 context
.seekRank(rank
);
433 context
.setRank(rank
);
439 * @param ratio double
440 * @return ITmfContext
441 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(double)
444 public ITmfContext
seekEvent(final double ratio
) {
445 CtfIterator context
= new CtfIterator(this);
446 context
.seek((long) (this.fNbEvents
* ratio
));
447 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
452 * Method readNextEvent.
453 * @param context ITmfContext
454 * @return CtfTmfEvent
455 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#readNextEvent(ITmfContext)
458 public CtfTmfEvent
readNextEvent(final ITmfContext context
) {
459 CtfTmfEvent event
= null;
460 if (context
instanceof CtfIterator
) {
461 CtfIterator ctfIterator
= (CtfIterator
) context
;
462 event
= ctfIterator
.getCurrentEvent();
463 ctfIterator
.advance();
469 * Method getResource.
471 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getResource()
474 public IResource
getResource() {
475 return this.fResource
;
479 * Method getStateSystem.
480 * @return IStateSystemQuerier
482 public IStateSystemQuerier
getStateSystem() {
487 * Method getCTFTrace.
490 CTFTrace
getCTFTrace() {
496 * Suppressing the warning, because the 'throws' will usually happen in
498 * @throws TmfTraceException
500 protected void buildStateSystem() throws TmfTraceException
{
502 * Nothing is done in the basic implementation, please specify
503 * how/if to build a state system in derived classes.