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