Remove context clone and add trace ranks to experiment location
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
CommitLineData
b1baa808 1/*******************************************************************************
ea271da6 2 * Copyright (c) 2012, 2013 Ericsson
b1baa808
MK
3 *
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
8 *
ea271da6
PT
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
b1baa808
MK
12 *******************************************************************************/
13
a3fc8213
AM
14package org.eclipse.linuxtools.tmf.core.ctfadaptor;
15
a3fc8213
AM
16import org.eclipse.core.resources.IProject;
17import org.eclipse.core.resources.IResource;
18import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
19import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
6256d8ad 20import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
64c2cb4c 21import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
a3fc8213 22import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 23import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
a3fc8213 24import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
4b7c469f 25import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
a3fc8213 26import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
4b7c469f 27import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
a3fc8213 28
9ac2eb62 29/**
d09f973b
FC
30 * The CTf trace handler
31 *
32 * @version 1.0
33 * @author Matthew khouzam
9ac2eb62 34 */
1e1bef82 35public class CtfTmfTrace extends TmfTrace implements ITmfEventParser {
a3fc8213 36
788ddcbc 37
324a6a4a
BH
38 //-------------------------------------------
39 // Constants
40 //-------------------------------------------
41 /**
42 * Default cache size for CTF traces
43 */
44 protected static final int DEFAULT_CACHE_SIZE = 50000;
64c2cb4c 45
4b7c469f
MK
46 //-------------------------------------------
47 // Fields
48 //-------------------------------------------
a3fc8213 49
4b7c469f
MK
50 /* Reference to the CTF Trace */
51 private CTFTrace fTrace;
a3fc8213 52
53b235e1 53
788ddcbc 54
4b7c469f
MK
55 //-------------------------------------------
56 // TmfTrace Overrides
57 //-------------------------------------------
b1baa808
MK
58 /**
59 * Method initTrace.
063f0d27
AM
60 *
61 * @param resource
62 * The resource associated with this trace
63 * @param path
64 * The path to the trace file
65 * @param eventType
66 * The type of events that will be read from this trace
b1baa808 67 * @throws TmfTraceException
063f0d27 68 * If something when wrong while reading the trace
b1baa808 69 */
a3fc8213 70 @Override
6256d8ad 71 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
b4f71e4a 72 throws TmfTraceException {
4a110860
AM
73 /*
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.
76 */
77 setCacheSize();
324a6a4a 78
e30ce12e
AM
79 @SuppressWarnings("unused")
80 CtfTmfEventType type;
81
a3fc8213
AM
82 try {
83 this.fTrace = new CTFTrace(path);
53b235e1 84 CtfIteratorManager.addTrace(this);
81a2d02e 85 CtfTmfContext ctx;
99b483fe 86 /* Set the start and (current) end times for this trace */
81a2d02e 87 ctx = (CtfTmfContext) seekEvent(0L);
132a02b0 88 CtfTmfEvent event = getNext(ctx);
30bf5897 89 if((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
99b483fe
AM
90 /* Handle the case where the trace is empty */
91 this.setStartTime(TmfTimestamp.BIG_BANG);
92 } else {
132a02b0 93 final ITmfTimestamp curTime = event.getTimestamp();
21fb02fa
MK
94 this.setStartTime(curTime);
95 this.setEndTime(curTime);
99b483fe
AM
96 }
97
25e48683 98 } catch (final CTFReaderException e) {
a3fc8213
AM
99 /*
100 * If it failed at the init(), we can assume it's because the file
101 * was not found or was not recognized as a CTF trace. Throw into
102 * the new type of exception expected by the rest of TMF.
103 */
9fa32496 104 throw new TmfTraceException(e.getMessage(), e);
a3fc8213 105 }
99b483fe 106
200789b3 107 super.initTrace(resource, path, eventType);
a3fc8213
AM
108 }
109
53b235e1
MK
110 /* (non-Javadoc)
111 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
112 */
113 @Override
114 public synchronized void dispose() {
115 CtfIteratorManager.removeTrace(this);
5d1c6919
PT
116 if (fTrace != null) {
117 fTrace.dispose();
118 fTrace = null;
119 }
53b235e1
MK
120 super.dispose();
121 }
122
b1baa808
MK
123 /**
124 * Method validate.
125 * @param project IProject
126 * @param path String
127 * @return boolean
128 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
129 */
a3fc8213 130 @Override
3bd44ac8 131 public boolean validate(final IProject project, final String path) {
a3fc8213
AM
132 try {
133 final CTFTrace temp = new CTFTrace(path);
81fe3479
PT
134 boolean valid = temp.majortIsSet(); // random test
135 temp.dispose();
136 return valid;
25e48683 137 } catch (final CTFReaderException e) {
90235d6b
AM
138 /* Nope, not a CTF trace we can read */
139 return false;
a3fc8213 140 }
a3fc8213
AM
141 }
142
b1baa808 143 /**
f474d36b
PT
144 * Method getCurrentLocation. This is not applicable in CTF
145 * @return null, since the trace has no knowledge of the current location
b1baa808
MK
146 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
147 */
a3fc8213 148 @Override
1e1bef82 149 public ITmfLocation getCurrentLocation() {
f474d36b 150 return null;
a3fc8213
AM
151 }
152
a3fc8213 153 @Override
1e1bef82 154 public double getLocationRatio(ITmfLocation location) {
4b7c469f 155 final CtfLocation curLocation = (CtfLocation) location;
81a2d02e 156 final CtfTmfContext context = new CtfTmfContext(this);
53b235e1 157 context.setLocation(curLocation);
5976d44a 158 context.seek(curLocation.getLocationInfo());
f5df94f8 159 final CtfLocationInfo currentTime = ((CtfLocationInfo)context.getLocation().getLocationInfo());
53b235e1
MK
160 final long startTime = getIterator(this, context).getStartTime();
161 final long endTime = getIterator(this, context).getEndTime();
132a02b0 162 return ((double) currentTime.getTimestamp() - startTime)
53b235e1 163 / (endTime - startTime);
a3fc8213
AM
164 }
165
b1baa808
MK
166 /**
167 * Method seekEvent.
168 * @param location ITmfLocation<?>
169 * @return ITmfContext
b1baa808 170 */
a3fc8213 171 @Override
76643eb7 172 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
ce2388e0 173 CtfLocation currentLocation = (CtfLocation) location;
81a2d02e 174 CtfTmfContext context = new CtfTmfContext(this);
76643eb7
BH
175 if (fTrace == null) {
176 context.setLocation(null);
177 context.setRank(ITmfContext.UNKNOWN_RANK);
178 return context;
179 }
4a110860
AM
180 /*
181 * The rank is set to 0 if the iterator seeks the beginning. If not, it
182 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
183 * by rank for now.
184 */
11d6f468 185 if (currentLocation == null) {
f5df94f8 186 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
4a110860 187 context.setRank(0);
11d6f468 188 }
5976d44a 189 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
d62bb185 190 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
1191a574 191 }
f474d36b 192 context.setLocation(currentLocation);
7f0bab07
PT
193 if (location == null) {
194 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
195 if (event != null) {
d62bb185 196 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
7f0bab07
PT
197 }
198 }
64c2cb4c 199 if(context.getRank() != 0) {
3bd44ac8 200 context.setRank(ITmfContext.UNKNOWN_RANK);
64c2cb4c 201 }
f474d36b 202 return context;
a3fc8213
AM
203 }
204
a3fc8213 205
a3fc8213 206 @Override
76643eb7 207 public synchronized ITmfContext seekEvent(double ratio) {
81a2d02e 208 CtfTmfContext context = new CtfTmfContext(this);
76643eb7
BH
209 if (fTrace == null) {
210 context.setLocation(null);
211 context.setRank(ITmfContext.UNKNOWN_RANK);
212 return context;
213 }
b2dc9e02
MK
214 final long end = this.getEndTime().getValue();
215 final long start = this.getStartTime().getValue();
216 final long diff = end - start;
15e89960 217 final long ratioTs = Math.round(diff * ratio) + start;
b2dc9e02 218 context.seek(ratioTs);
f474d36b
PT
219 context.setRank(ITmfContext.UNKNOWN_RANK);
220 return context;
a3fc8213
AM
221 }
222
b1baa808
MK
223 /**
224 * Method readNextEvent.
225 * @param context ITmfContext
226 * @return CtfTmfEvent
c32744d6 227 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 228 */
a3fc8213 229 @Override
4b7c469f 230 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
faa38350
PT
231 if (fTrace == null) {
232 return null;
233 }
f474d36b 234 CtfTmfEvent event = null;
81a2d02e 235 if (context instanceof CtfTmfContext) {
575beffc 236 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
ae09313d
PT
237 return null;
238 }
81a2d02e 239 CtfTmfContext ctfContext = (CtfTmfContext) context;
788ddcbc 240 event = ctfContext.getCurrentEvent();
4a110860 241
324a6a4a
BH
242 if (event != null) {
243 updateAttributes(context, event.getTimestamp());
788ddcbc
MK
244 ctfContext.advance();
245 ctfContext.increaseRank();
324a6a4a 246 }
f474d36b 247 }
4a110860 248
aa572e22 249 return event;
a3fc8213
AM
250 }
251
4b7c469f
MK
252 /**
253 * gets the CTFtrace that this is wrapping
254 * @return the CTF trace
255 */
256 public CTFTrace getCTFTrace() {
a3fc8213
AM
257 return fTrace;
258 }
a1a24d68 259
8636b448 260
4b7c469f
MK
261 //-------------------------------------------
262 // Environment Parameters
263 //-------------------------------------------
d26f90fd 264 /**
4b7c469f
MK
265 * Method getNbEnvVars.
266 *
267 * @return int
d26f90fd 268 */
4b7c469f
MK
269 public int getNbEnvVars() {
270 return this.fTrace.getEnvironment().size();
271 }
272
273 /**
274 * Method getEnvNames.
275 *
276 * @return String[]
277 */
278 public String[] getEnvNames() {
279 final String[] s = new String[getNbEnvVars()];
280 return this.fTrace.getEnvironment().keySet().toArray(s);
281 }
282
283 /**
284 * Method getEnvValue.
285 *
286 * @param key
287 * String
288 * @return String
289 */
290 public String getEnvValue(final String key) {
291 return this.fTrace.getEnvironment().get(key);
292 }
293
bfe038ff
MK
294 //-------------------------------------------
295 // Clocks
296 //-------------------------------------------
297
9ac2eb62
MK
298 /**
299 * gets the clock offset
300 * @return the clock offset in ns
301 */
bfe038ff
MK
302 public long getOffset(){
303 if( fTrace != null ) {
304 return fTrace.getOffset();
305 }
306 return 0;
307 }
308
4b7c469f
MK
309 //-------------------------------------------
310 // Parser
311 //-------------------------------------------
312
313 @Override
bfe038ff 314 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f 315 CtfTmfEvent event = null;
ea271da6
PT
316 if (context instanceof CtfTmfContext) {
317 final ITmfContext tmpContext = seekEvent(context.getLocation());
318 event = getNext(tmpContext);
4b7c469f
MK
319 }
320 return event;
11d6f468 321 }
64c2cb4c 322
324a6a4a 323 /**
64c2cb4c 324 * Sets the cache size for a CtfTmfTrace.
324a6a4a
BH
325 */
326 protected void setCacheSize() {
327 setCacheSize(DEFAULT_CACHE_SIZE);
328 }
ce2388e0 329
53b235e1
MK
330 //-------------------------------------------
331 // Helpers
332 //-------------------------------------------
333
81a2d02e 334 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfContext context) {
53b235e1
MK
335 return CtfIteratorManager.getIterator(trace, context);
336 }
36dd544c
MK
337
338 /**
339 * Get an iterator to the trace
340 *
341 * @return an iterator to the trace
ed59ab27 342 * @since 2.0
36dd544c
MK
343 */
344 public CtfIterator createIterator(){
345 return new CtfIterator(this);
346 }
a3fc8213 347}
This page took 0.083644 seconds and 5 git commands to generate.