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