Clean up duplicate function
[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
4b7c469f 12
a3fc8213
AM
13package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
a3fc8213
AM
15import org.eclipse.core.resources.IProject;
16import org.eclipse.core.resources.IResource;
139d5c1a 17import org.eclipse.core.runtime.CoreException;
aa572e22
MK
18import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
19import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
a3fc8213
AM
20import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
1191a574 22import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
aa572e22 23import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
64c2cb4c 24import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
a3fc8213 25import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 26import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
18ab1d18 27import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
a3fc8213 28import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
4b7c469f 29import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
a3fc8213 30import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
4b7c469f 31import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
a3fc8213 32
bfe038ff 33public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{
a3fc8213 34
324a6a4a
BH
35 //-------------------------------------------
36 // Constants
37 //-------------------------------------------
38 /**
39 * Default cache size for CTF traces
40 */
41 protected static final int DEFAULT_CACHE_SIZE = 50000;
64c2cb4c 42
4b7c469f
MK
43 //-------------------------------------------
44 // Fields
45 //-------------------------------------------
a3fc8213 46
324a6a4a 47 /** Reference to the state system assigned to this trace */
d26f90fd 48 protected IStateSystemQuerier ss = null;
11d6f468 49
4b7c469f
MK
50 /* Reference to the CTF Trace */
51 private CTFTrace fTrace;
a3fc8213 52
4b7c469f
MK
53 //-------------------------------------------
54 // TmfTrace Overrides
55 //-------------------------------------------
b1baa808
MK
56 /**
57 * Method initTrace.
58 * @param resource IResource
59 * @param path String
60 * @param eventType Class<CtfTmfEvent>
61 * @throws TmfTraceException
62 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
63 */
a3fc8213 64 @Override
25e48683 65 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
b4f71e4a 66 throws TmfTraceException {
4a110860
AM
67 /*
68 * Set the cache size. This has to be done before the call to super()
69 * because the super needs to know the cache size.
70 */
71 setCacheSize();
4b7c469f 72 super.initTrace(resource, path, eventType);
e30ce12e
AM
73 EventDeclaration ed;
74 ITmfEventField eventField;
324a6a4a 75
e30ce12e
AM
76 @SuppressWarnings("unused")
77 CtfTmfEventType type;
78
a3fc8213
AM
79 try {
80 this.fTrace = new CTFTrace(path);
aa572e22 81 for( int i =0 ; i< this.fTrace.getNbEventTypes(); i++) {
e30ce12e
AM
82 ed = this.fTrace.getEventType(i);
83 eventField = parseDeclaration(ed);
99b483fe
AM
84 /*
85 * Populate the event manager with event types that are there in
86 * the beginning.
87 */
e30ce12e 88 type = new CtfTmfEventType(ed.getName(), eventField);
aa572e22 89 }
99b483fe
AM
90
91 /* Set the start and (current) end times for this trace */
92 final CtfIterator iterator = new CtfIterator(this, 0, 0);
93 if(iterator.getLocation().equals(CtfIterator.NULL_LOCATION)) {
94 /* Handle the case where the trace is empty */
95 this.setStartTime(TmfTimestamp.BIG_BANG);
96 } else {
97 this.setStartTime(iterator.getCurrentEvent().getTimestamp());
99b483fe
AM
98 this.setEndTime(iterator.getCurrentEvent().getTimestamp());
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
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);
134 return temp.majortIsSet(); // random test
25e48683 135 } catch (final CTFReaderException e) {
90235d6b
AM
136 /* Nope, not a CTF trace we can read */
137 return false;
a3fc8213 138 }
a3fc8213
AM
139 }
140
b1baa808 141 /**
f474d36b
PT
142 * Method getCurrentLocation. This is not applicable in CTF
143 * @return null, since the trace has no knowledge of the current location
b1baa808
MK
144 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
145 */
a3fc8213
AM
146 @Override
147 public ITmfLocation<?> getCurrentLocation() {
f474d36b 148 return null;
a3fc8213
AM
149 }
150
a3fc8213 151
a3fc8213
AM
152
153 @Override
4b7c469f
MK
154 public double getLocationRatio(ITmfLocation<?> location) {
155 final CtfLocation curLocation = (CtfLocation) location;
156 CtfIterator iterator = new CtfIterator(this);
157 iterator.seek(curLocation.getLocation());
158 return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
159 .getStartTime())
160 / (iterator.getEndTime() - iterator.getStartTime());
a3fc8213
AM
161 }
162
64c2cb4c
MK
163 /* (non-Javadoc)
164 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#seekEvent(org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp)
165 */
166 @Override
167 public synchronized ITmfContext seekEvent(ITmfTimestamp timestamp) {
168 if( timestamp instanceof CtfTmfTimestamp){
169 CtfIterator iter = new CtfIterator(this);
170 iter.seek(timestamp.getValue());
171 return iter;
172 }
173 return super.seekEvent(timestamp);
174 }
175
b1baa808
MK
176 /**
177 * Method seekEvent.
178 * @param location ITmfLocation<?>
179 * @return ITmfContext
180 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
181 */
a3fc8213 182 @Override
7e6347b0 183 public ITmfContext seekEvent(final ITmfLocation<?> location) {
ce2388e0 184 CtfLocation currentLocation = (CtfLocation) location;
4a110860 185 CtfIterator context = new CtfIterator(this);
3bd44ac8
FC
186 context.setRank(ITmfContext.UNKNOWN_RANK);
187
4a110860
AM
188 /*
189 * The rank is set to 0 if the iterator seeks the beginning. If not, it
190 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
191 * by rank for now.
192 */
11d6f468 193 if (currentLocation == null) {
ce2388e0 194 currentLocation = new CtfLocation(0L);
4a110860 195 context.setRank(0);
11d6f468 196 }
1191a574 197 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
4cf201de
FC
198 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
199 currentLocation.setLocation(getEndTime().getValue() + 1);
1191a574 200 }
f474d36b 201 context.setLocation(currentLocation);
64c2cb4c 202 if(context.getRank() != 0) {
3bd44ac8 203 context.setRank(ITmfContext.UNKNOWN_RANK);
64c2cb4c 204 }
f474d36b 205 return context;
a3fc8213
AM
206 }
207
a3fc8213 208
a3fc8213 209 @Override
4b7c469f 210 public ITmfContext seekEvent(double ratio) {
f474d36b 211 CtfIterator context = new CtfIterator(this);
4b7c469f 212 context.seek((long) (this.getNbEvents() * ratio));
f474d36b
PT
213 context.setRank(ITmfContext.UNKNOWN_RANK);
214 return context;
a3fc8213
AM
215 }
216
b1baa808
MK
217 /**
218 * Method readNextEvent.
219 * @param context ITmfContext
220 * @return CtfTmfEvent
c32744d6 221 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 222 */
a3fc8213 223 @Override
4b7c469f 224 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
f474d36b
PT
225 CtfTmfEvent event = null;
226 if (context instanceof CtfIterator) {
227 CtfIterator ctfIterator = (CtfIterator) context;
228 event = ctfIterator.getCurrentEvent();
4a110860 229
324a6a4a
BH
230 if (event != null) {
231 updateAttributes(context, event.getTimestamp());
4a110860
AM
232 ctfIterator.advance();
233 ctfIterator.increaseRank();
324a6a4a 234 }
f474d36b 235 }
4a110860 236
aa572e22 237 return event;
a3fc8213
AM
238 }
239
b1baa808 240 /**
4b7c469f
MK
241 * Suppressing the warning, because the 'throws' will usually happen in
242 * sub-classes.
243 * @throws TmfTraceException
b1baa808 244 */
3bd44ac8 245 @SuppressWarnings({ "static-method" })
4b7c469f
MK
246 protected void buildStateSystem() throws TmfTraceException {
247 /*
248 * Nothing is done in the basic implementation, please specify
249 * how/if to build a state system in derived classes.
250 */
251 return;
a3fc8213
AM
252 }
253
b1baa808
MK
254 /**
255 * Method getStateSystem.
4b7c469f 256 *
b1baa808
MK
257 * @return IStateSystemQuerier
258 */
d26f90fd 259 public IStateSystemQuerier getStateSystem() {
11d6f468
AM
260 return this.ss;
261 }
262
b1baa808 263 /**
4b7c469f
MK
264 *
265 * @param ed
266 * @return
b1baa808 267 */
4b7c469f
MK
268 private static ITmfEventField parseDeclaration(EventDeclaration ed) {
269 EventDefinition eventDef = ed.createDefinition(null);
270 return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
271 CtfTmfEvent.parseFields(eventDef));
272 }
273
274 /**
275 * gets the CTFtrace that this is wrapping
276 * @return the CTF trace
277 */
278 public CTFTrace getCTFTrace() {
a3fc8213
AM
279 return fTrace;
280 }
a1a24d68 281
8636b448 282
4b7c469f
MK
283 //-------------------------------------------
284 // Environment Parameters
285 //-------------------------------------------
d26f90fd 286 /**
4b7c469f
MK
287 * Method getNbEnvVars.
288 *
289 * @return int
d26f90fd 290 */
4b7c469f
MK
291 public int getNbEnvVars() {
292 return this.fTrace.getEnvironment().size();
293 }
294
295 /**
296 * Method getEnvNames.
297 *
298 * @return String[]
299 */
300 public String[] getEnvNames() {
301 final String[] s = new String[getNbEnvVars()];
302 return this.fTrace.getEnvironment().keySet().toArray(s);
303 }
304
305 /**
306 * Method getEnvValue.
307 *
308 * @param key
309 * String
310 * @return String
311 */
312 public String getEnvValue(final String key) {
313 return this.fTrace.getEnvironment().get(key);
314 }
315
bfe038ff
MK
316 //-------------------------------------------
317 // Clocks
318 //-------------------------------------------
319
320 public long getOffset(){
321 if( fTrace != null ) {
322 return fTrace.getOffset();
323 }
324 return 0;
325 }
326
4b7c469f
MK
327 //-------------------------------------------
328 // Parser
329 //-------------------------------------------
330
331 @Override
bfe038ff 332 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f
MK
333 CtfTmfEvent event = null;
334 if( context instanceof CtfIterator ){
408e65d2 335 CtfIterator itt = (CtfIterator) context.clone();
4b7c469f
MK
336 event = itt.getCurrentEvent();
337 }
338 return event;
11d6f468 339 }
64c2cb4c 340
324a6a4a 341 /**
64c2cb4c 342 * Sets the cache size for a CtfTmfTrace.
324a6a4a
BH
343 */
344 protected void setCacheSize() {
345 setCacheSize(DEFAULT_CACHE_SIZE);
346 }
ce2388e0 347
a3fc8213 348}
This page took 0.05265 seconds and 5 git commands to generate.