tmf: Bump plugins version to 2.0 for Kepler branch
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
CommitLineData
b1baa808
MK
1/*******************************************************************************
2 * Copyright (c) 2012 Ericsson
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 *
9 * Contributors: Matthew Khouzam - Initial API and implementation
10 *******************************************************************************/
11
a3fc8213
AM
12package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
a3fc8213
AM
14import org.eclipse.core.resources.IProject;
15import org.eclipse.core.resources.IResource;
139d5c1a 16import org.eclipse.core.runtime.CoreException;
a3fc8213
AM
17import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
18import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
1191a574 19import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
64c2cb4c 20import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
a3fc8213 21import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 22import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
18ab1d18 23import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
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 */
bfe038ff 35public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{
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
324a6a4a 50 /** Reference to the state system assigned to this trace */
d26f90fd 51 protected IStateSystemQuerier ss = null;
11d6f468 52
4b7c469f
MK
53 /* Reference to the CTF Trace */
54 private CTFTrace fTrace;
a3fc8213 55
53b235e1 56
788ddcbc 57
4b7c469f
MK
58 //-------------------------------------------
59 // TmfTrace Overrides
60 //-------------------------------------------
b1baa808
MK
61 /**
62 * Method initTrace.
063f0d27
AM
63 *
64 * @param resource
65 * The resource associated with this trace
66 * @param path
67 * The path to the trace file
68 * @param eventType
69 * The type of events that will be read from this trace
b1baa808 70 * @throws TmfTraceException
063f0d27 71 * If something when wrong while reading the trace
b1baa808 72 */
a3fc8213 73 @Override
25e48683 74 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
b4f71e4a 75 throws TmfTraceException {
4a110860
AM
76 /*
77 * Set the cache size. This has to be done before the call to super()
78 * because the super needs to know the cache size.
79 */
80 setCacheSize();
4b7c469f 81 super.initTrace(resource, path, eventType);
324a6a4a 82
e30ce12e
AM
83 @SuppressWarnings("unused")
84 CtfTmfEventType type;
85
a3fc8213
AM
86 try {
87 this.fTrace = new CTFTrace(path);
53b235e1
MK
88 CtfIteratorManager.addTrace(this);
89 CtfTmfLightweightContext ctx;
99b483fe 90 /* Set the start and (current) end times for this trace */
53b235e1
MK
91 ctx = new CtfTmfLightweightContext(this);
92 ctx.setLocation(new CtfLocation(0L));
93 if(ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) {
99b483fe
AM
94 /* Handle the case where the trace is empty */
95 this.setStartTime(TmfTimestamp.BIG_BANG);
96 } else {
21fb02fa
MK
97 final ITmfTimestamp curTime = ctx.getCurrentEvent().getTimestamp();
98 this.setStartTime(curTime);
99 this.setEndTime(curTime);
99b483fe
AM
100 }
101
25e48683 102 } catch (final CTFReaderException e) {
a3fc8213
AM
103 /*
104 * If it failed at the init(), we can assume it's because the file
105 * was not found or was not recognized as a CTF trace. Throw into
106 * the new type of exception expected by the rest of TMF.
107 */
9fa32496 108 throw new TmfTraceException(e.getMessage(), e);
a3fc8213 109 }
99b483fe 110
99b483fe 111 //FIXME This should be called via the ExperimentUpdated signal
11d6f468 112 buildStateSystem();
139d5c1a
AM
113
114 /* Refresh the project, so it can pick up new files that got created. */
115 if ( resource != null) {
116 try {
117 resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
118 } catch (CoreException e) {
9fa32496 119 throw new TmfTraceException(e.getMessage(), e);
139d5c1a
AM
120 }
121 }
a3fc8213
AM
122 }
123
53b235e1
MK
124 /* (non-Javadoc)
125 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
126 */
127 @Override
128 public synchronized void dispose() {
129 CtfIteratorManager.removeTrace(this);
130 super.dispose();
131 }
132
b1baa808
MK
133 /**
134 * Method validate.
135 * @param project IProject
136 * @param path String
137 * @return boolean
138 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
139 */
a3fc8213 140 @Override
3bd44ac8 141 public boolean validate(final IProject project, final String path) {
a3fc8213
AM
142 try {
143 final CTFTrace temp = new CTFTrace(path);
144 return temp.majortIsSet(); // random test
25e48683 145 } catch (final CTFReaderException e) {
90235d6b
AM
146 /* Nope, not a CTF trace we can read */
147 return false;
a3fc8213 148 }
a3fc8213
AM
149 }
150
b1baa808 151 /**
f474d36b
PT
152 * Method getCurrentLocation. This is not applicable in CTF
153 * @return null, since the trace has no knowledge of the current location
b1baa808
MK
154 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
155 */
a3fc8213
AM
156 @Override
157 public ITmfLocation<?> getCurrentLocation() {
f474d36b 158 return null;
a3fc8213
AM
159 }
160
a3fc8213 161
a3fc8213
AM
162
163 @Override
4b7c469f
MK
164 public double getLocationRatio(ITmfLocation<?> location) {
165 final CtfLocation curLocation = (CtfLocation) location;
53b235e1
MK
166 final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
167 context.setLocation(curLocation);
168 context.seek(curLocation.getLocation());
169 final long currentTime = ((Long)context.getLocation().getLocation());
170 final long startTime = getIterator(this, context).getStartTime();
171 final long endTime = getIterator(this, context).getEndTime();
172 return ((double) currentTime - startTime)
173 / (endTime - startTime);
a3fc8213
AM
174 }
175
53b235e1
MK
176
177
178
788ddcbc 179
64c2cb4c
MK
180 /* (non-Javadoc)
181 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
182 */
183 @Override
184 public synchronized ITmfContext seekEvent(ITmfTimestamp timestamp) {
185 if( timestamp instanceof CtfTmfTimestamp){
53b235e1 186 CtfTmfLightweightContext iter = new CtfTmfLightweightContext(this);
64c2cb4c
MK
187 iter.seek(timestamp.getValue());
188 return iter;
189 }
190 return super.seekEvent(timestamp);
191 }
192
b1baa808
MK
193 /**
194 * Method seekEvent.
195 * @param location ITmfLocation<?>
196 * @return ITmfContext
b1baa808 197 */
a3fc8213 198 @Override
7e6347b0 199 public ITmfContext seekEvent(final ITmfLocation<?> location) {
ce2388e0 200 CtfLocation currentLocation = (CtfLocation) location;
53b235e1 201 CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
4a110860
AM
202 /*
203 * The rank is set to 0 if the iterator seeks the beginning. If not, it
204 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
205 * by rank for now.
206 */
11d6f468 207 if (currentLocation == null) {
ce2388e0 208 currentLocation = new CtfLocation(0L);
4a110860 209 context.setRank(0);
11d6f468 210 }
1191a574 211 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
4cf201de
FC
212 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
213 currentLocation.setLocation(getEndTime().getValue() + 1);
1191a574 214 }
f474d36b 215 context.setLocation(currentLocation);
64c2cb4c 216 if(context.getRank() != 0) {
3bd44ac8 217 context.setRank(ITmfContext.UNKNOWN_RANK);
64c2cb4c 218 }
f474d36b 219 return context;
a3fc8213
AM
220 }
221
a3fc8213 222
a3fc8213 223 @Override
4b7c469f 224 public ITmfContext seekEvent(double ratio) {
53b235e1 225 CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
4b7c469f 226 context.seek((long) (this.getNbEvents() * ratio));
f474d36b
PT
227 context.setRank(ITmfContext.UNKNOWN_RANK);
228 return context;
a3fc8213
AM
229 }
230
b1baa808
MK
231 /**
232 * Method readNextEvent.
233 * @param context ITmfContext
234 * @return CtfTmfEvent
c32744d6 235 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 236 */
a3fc8213 237 @Override
4b7c469f 238 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
f474d36b 239 CtfTmfEvent event = null;
788ddcbc 240 if (context instanceof CtfTmfLightweightContext) {
81fd789c 241 if (CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocation())) {
ae09313d
PT
242 return null;
243 }
788ddcbc
MK
244 CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context;
245 event = ctfContext.getCurrentEvent();
4a110860 246
324a6a4a
BH
247 if (event != null) {
248 updateAttributes(context, event.getTimestamp());
788ddcbc
MK
249 ctfContext.advance();
250 ctfContext.increaseRank();
324a6a4a 251 }
f474d36b 252 }
4a110860 253
aa572e22 254 return event;
a3fc8213
AM
255 }
256
b1baa808 257 /**
4b7c469f
MK
258 * Suppressing the warning, because the 'throws' will usually happen in
259 * sub-classes.
063f0d27 260 *
4b7c469f 261 * @throws TmfTraceException
b1baa808 262 */
063f0d27 263 @SuppressWarnings("unused")
4b7c469f
MK
264 protected void buildStateSystem() throws TmfTraceException {
265 /*
266 * Nothing is done in the basic implementation, please specify
267 * how/if to build a state system in derived classes.
268 */
269 return;
a3fc8213
AM
270 }
271
b1baa808
MK
272 /**
273 * Method getStateSystem.
4b7c469f 274 *
b1baa808
MK
275 * @return IStateSystemQuerier
276 */
d26f90fd 277 public IStateSystemQuerier getStateSystem() {
11d6f468
AM
278 return this.ss;
279 }
280
4b7c469f
MK
281 /**
282 * gets the CTFtrace that this is wrapping
283 * @return the CTF trace
284 */
285 public CTFTrace getCTFTrace() {
a3fc8213
AM
286 return fTrace;
287 }
a1a24d68 288
8636b448 289
4b7c469f
MK
290 //-------------------------------------------
291 // Environment Parameters
292 //-------------------------------------------
d26f90fd 293 /**
4b7c469f
MK
294 * Method getNbEnvVars.
295 *
296 * @return int
d26f90fd 297 */
4b7c469f
MK
298 public int getNbEnvVars() {
299 return this.fTrace.getEnvironment().size();
300 }
301
302 /**
303 * Method getEnvNames.
304 *
305 * @return String[]
306 */
307 public String[] getEnvNames() {
308 final String[] s = new String[getNbEnvVars()];
309 return this.fTrace.getEnvironment().keySet().toArray(s);
310 }
311
312 /**
313 * Method getEnvValue.
314 *
315 * @param key
316 * String
317 * @return String
318 */
319 public String getEnvValue(final String key) {
320 return this.fTrace.getEnvironment().get(key);
321 }
322
bfe038ff
MK
323 //-------------------------------------------
324 // Clocks
325 //-------------------------------------------
326
9ac2eb62
MK
327 /**
328 * gets the clock offset
329 * @return the clock offset in ns
330 */
bfe038ff
MK
331 public long getOffset(){
332 if( fTrace != null ) {
333 return fTrace.getOffset();
334 }
335 return 0;
336 }
337
4b7c469f
MK
338 //-------------------------------------------
339 // Parser
340 //-------------------------------------------
341
342 @Override
bfe038ff 343 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f 344 CtfTmfEvent event = null;
788ddcbc
MK
345 if( context instanceof CtfTmfLightweightContext ){
346 CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone();
4b7c469f
MK
347 event = itt.getCurrentEvent();
348 }
349 return event;
11d6f468 350 }
64c2cb4c 351
324a6a4a 352 /**
64c2cb4c 353 * Sets the cache size for a CtfTmfTrace.
324a6a4a
BH
354 */
355 protected void setCacheSize() {
356 setCacheSize(DEFAULT_CACHE_SIZE);
357 }
ce2388e0 358
53b235e1
MK
359 //-------------------------------------------
360 // Helpers
361 //-------------------------------------------
362
363 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfLightweightContext context) {
364 return CtfIteratorManager.getIterator(trace, context);
365 }
a3fc8213 366}
This page took 0.080153 seconds and 5 git commands to generate.