1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
12 *******************************************************************************/
14 package org
.eclipse
.linuxtools
.tmf
.core
.ctfadaptor
;
16 import org
.eclipse
.core
.resources
.IProject
;
17 import org
.eclipse
.core
.resources
.IResource
;
18 import org
.eclipse
.core
.runtime
.IStatus
;
19 import org
.eclipse
.core
.runtime
.Status
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
22 import org
.eclipse
.linuxtools
.internal
.tmf
.core
.Activator
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.ITmfTimestamp
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.timestamp
.TmfTimestamp
;
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
;
33 * The CTf trace handler
36 * @author Matthew khouzam
38 public class CtfTmfTrace
extends TmfTrace
implements ITmfEventParser
{
40 // -------------------------------------------
42 // -------------------------------------------
44 * Default cache size for CTF traces
46 protected static final int DEFAULT_CACHE_SIZE
= 50000;
48 // -------------------------------------------
50 // -------------------------------------------
52 /* Reference to the CTF Trace */
53 private CTFTrace fTrace
;
55 // -------------------------------------------
57 // -------------------------------------------
62 * The resource associated with this trace
64 * The path to the trace file
66 * The type of events that will be read from this trace
67 * @throws TmfTraceException
68 * If something when wrong while reading the trace
71 public void initTrace(final IResource resource
, final String path
, final Class
<?
extends ITmfEvent
> eventType
)
72 throws TmfTraceException
{
74 * Set the cache size. This has to be done before the call to super()
75 * because the super needs to know the cache size.
79 super.initTrace(resource
, path
, eventType
);
81 @SuppressWarnings("unused")
85 this.fTrace
= new CTFTrace(path
);
86 CtfIteratorManager
.addTrace(this);
88 /* Set the start and (current) end times for this trace */
89 ctx
= (CtfTmfContext
) seekEvent(0L);
90 CtfTmfEvent event
= getNext(ctx
);
91 if ((ctx
.getLocation().equals(CtfIterator
.NULL_LOCATION
)) || (ctx
.getCurrentEvent() == null)) {
92 /* Handle the case where the trace is empty */
93 this.setStartTime(TmfTimestamp
.BIG_BANG
);
95 final ITmfTimestamp curTime
= event
.getTimestamp();
96 this.setStartTime(curTime
);
97 this.setEndTime(curTime
);
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
);
113 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
116 public synchronized void dispose() {
117 CtfIteratorManager
.removeTrace(this);
118 if (fTrace
!= null) {
132 * @return IStatus IStatus.error or Status.OK_STATUS
133 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
137 public IStatus
validate(final IProject project
, final String path
) {
138 IStatus validTrace
= Status
.OK_STATUS
;
140 final CTFTrace temp
= new CTFTrace(path
);
141 boolean valid
= temp
.majortIsSet(); // random test
144 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_MajorNotSet
);
146 } catch (final CTFReaderException e
) {
147 validTrace
= new Status(IStatus
.ERROR
, Activator
.PLUGIN_ID
, Messages
.CtfTmfTrace_ReadingError
+": " + e
.toString()); //$NON-NLS-1$
153 * Method getCurrentLocation. This is not applicable in CTF
155 * @return null, since the trace has no knowledge of the current location
156 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
159 public ITmfLocation
getCurrentLocation() {
164 public double getLocationRatio(ITmfLocation location
) {
165 final CtfLocation curLocation
= (CtfLocation
) location
;
166 final CtfTmfContext context
= new CtfTmfContext(this);
167 context
.setLocation(curLocation
);
168 context
.seek(curLocation
.getLocationInfo());
169 final CtfLocationInfo currentTime
= ((CtfLocationInfo
) context
.getLocation().getLocationInfo());
170 final long startTime
= getIterator(this, context
).getStartTime();
171 final long endTime
= getIterator(this, context
).getEndTime();
172 return ((double) currentTime
.getTimestamp() - startTime
)
173 / (endTime
- startTime
);
181 * @return ITmfContext
184 public synchronized ITmfContext
seekEvent(final ITmfLocation location
) {
185 CtfLocation currentLocation
= (CtfLocation
) location
;
186 CtfTmfContext context
= new CtfTmfContext(this);
187 if (fTrace
== null) {
188 context
.setLocation(null);
189 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
193 * The rank is set to 0 if the iterator seeks the beginning. If not, it
194 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
197 if (currentLocation
== null) {
198 currentLocation
= new CtfLocation(new CtfLocationInfo(0L, 0L));
201 if (currentLocation
.getLocationInfo() == CtfLocation
.INVALID_LOCATION
) {
202 currentLocation
= new CtfLocation(getEndTime().getValue() + 1, 0L);
204 context
.setLocation(currentLocation
);
205 if (location
== null) {
206 CtfTmfEvent event
= getIterator(this, context
).getCurrentEvent();
208 currentLocation
= new CtfLocation(event
.getTimestamp().getValue(), 0);
211 if (context
.getRank() != 0) {
212 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
218 public synchronized ITmfContext
seekEvent(double ratio
) {
219 CtfTmfContext context
= new CtfTmfContext(this);
220 if (fTrace
== null) {
221 context
.setLocation(null);
222 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
225 final long end
= this.getEndTime().getValue();
226 final long start
= this.getStartTime().getValue();
227 final long diff
= end
- start
;
228 final long ratioTs
= Math
.round(diff
* ratio
) + start
;
229 context
.seek(ratioTs
);
230 context
.setRank(ITmfContext
.UNKNOWN_RANK
);
235 * Method readNextEvent.
239 * @return CtfTmfEvent
240 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
243 public synchronized CtfTmfEvent
getNext(final ITmfContext context
) {
244 if (fTrace
== null) {
247 CtfTmfEvent event
= null;
248 if (context
instanceof CtfTmfContext
) {
249 if (context
.getLocation() == null || CtfLocation
.INVALID_LOCATION
.equals(context
.getLocation().getLocationInfo())) {
252 CtfTmfContext ctfContext
= (CtfTmfContext
) context
;
253 event
= ctfContext
.getCurrentEvent();
256 updateAttributes(context
, event
.getTimestamp());
257 ctfContext
.advance();
258 ctfContext
.increaseRank();
266 * gets the CTFtrace that this is wrapping
268 * @return the CTF trace
270 public CTFTrace
getCTFTrace() {
274 // -------------------------------------------
275 // Environment Parameters
276 // -------------------------------------------
278 * Method getNbEnvVars.
282 public int getNbEnvVars() {
283 return this.fTrace
.getEnvironment().size();
287 * Method getEnvNames.
291 public String
[] getEnvNames() {
292 final String
[] s
= new String
[getNbEnvVars()];
293 return this.fTrace
.getEnvironment().keySet().toArray(s
);
297 * Method getEnvValue.
303 public String
getEnvValue(final String key
) {
304 return this.fTrace
.getEnvironment().get(key
);
307 // -------------------------------------------
309 // -------------------------------------------
312 * gets the clock offset
314 * @return the clock offset in ns
316 public long getOffset() {
317 if (fTrace
!= null) {
318 return fTrace
.getOffset();
323 // -------------------------------------------
325 // -------------------------------------------
328 public CtfTmfEvent
parseEvent(ITmfContext context
) {
329 CtfTmfEvent event
= null;
330 if (context
instanceof CtfTmfContext
) {
331 final ITmfContext tmpContext
= seekEvent(context
.getLocation());
332 event
= getNext(tmpContext
);
338 * Sets the cache size for a CtfTmfTrace.
340 protected void setCacheSize() {
341 setCacheSize(DEFAULT_CACHE_SIZE
);
344 // -------------------------------------------
346 // -------------------------------------------
348 private static CtfIterator
getIterator(CtfTmfTrace trace
, CtfTmfContext context
) {
349 return CtfIteratorManager
.getIterator(trace
, context
);
353 * Get an iterator to the trace
355 * @return an iterator to the trace
358 public CtfIterator
createIterator() {
359 return new CtfIterator(this);