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