tmf: Introduce the ITmfStatistics interface
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
... / ...
CommitLineData
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
12package org.eclipse.linuxtools.tmf.core.ctfadaptor;
13
14import org.eclipse.core.resources.IProject;
15import org.eclipse.core.resources.IResource;
16import org.eclipse.core.runtime.CoreException;
17import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
18import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
19import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
20import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
21import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
22import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
23import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
24import org.eclipse.linuxtools.tmf.core.statesystem.ITmfStateSystem;
25import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
26import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
27import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
28import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
29
30/**
31 * The CTf trace handler
32 *
33 * @version 1.0
34 * @author Matthew khouzam
35 */
36public class CtfTmfTrace extends TmfTrace implements ITmfEventParser {
37
38
39 //-------------------------------------------
40 // Constants
41 //-------------------------------------------
42 /**
43 * Default cache size for CTF traces
44 */
45 protected static final int DEFAULT_CACHE_SIZE = 50000;
46
47 //-------------------------------------------
48 // Fields
49 //-------------------------------------------
50
51 /** Reference to the state system assigned to this trace */
52 protected ITmfStateSystem ss = null;
53
54 /* Reference to the CTF Trace */
55 private CTFTrace fTrace;
56
57
58
59 //-------------------------------------------
60 // TmfTrace Overrides
61 //-------------------------------------------
62 /**
63 * Method initTrace.
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
71 * @throws TmfTraceException
72 * If something when wrong while reading the trace
73 */
74 @Override
75 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
76 throws TmfTraceException {
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();
82
83 @SuppressWarnings("unused")
84 CtfTmfEventType type;
85
86 try {
87 this.fTrace = new CTFTrace(path);
88 CtfIteratorManager.addTrace(this);
89 CtfTmfLightweightContext ctx;
90 /* Set the start and (current) end times for this trace */
91 ctx = (CtfTmfLightweightContext) seekEvent(0L);
92 CtfTmfEvent event = getNext(ctx);
93 if((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
94 /* Handle the case where the trace is empty */
95 this.setStartTime(TmfTimestamp.BIG_BANG);
96 } else {
97 final ITmfTimestamp curTime = event.getTimestamp();
98 this.setStartTime(curTime);
99 this.setEndTime(curTime);
100 }
101
102 } catch (final CTFReaderException e) {
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 */
108 throw new TmfTraceException(e.getMessage(), e);
109 }
110
111 super.initTrace(resource, path, eventType);
112
113 //FIXME This should be called via the ExperimentUpdated signal
114 buildStateSystem();
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) {
121 throw new TmfTraceException(e.getMessage(), e);
122 }
123 }
124 }
125
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
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 */
142 @Override
143 public boolean validate(final IProject project, final String path) {
144 try {
145 final CTFTrace temp = new CTFTrace(path);
146 return temp.majortIsSet(); // random test
147 } catch (final CTFReaderException e) {
148 /* Nope, not a CTF trace we can read */
149 return false;
150 }
151 }
152
153 /**
154 * Method getCurrentLocation. This is not applicable in CTF
155 * @return null, since the trace has no knowledge of the current location
156 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
157 */
158 @Override
159 public ITmfLocation getCurrentLocation() {
160 return null;
161 }
162
163
164
165 @Override
166 public double getLocationRatio(ITmfLocation location) {
167 final CtfLocation curLocation = (CtfLocation) location;
168 final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
169 context.setLocation(curLocation);
170 context.seek(curLocation.getLocationInfo());
171 final CtfLocationData currentTime = ((CtfLocationData)context.getLocation().getLocationInfo());
172 final long startTime = getIterator(this, context).getStartTime();
173 final long endTime = getIterator(this, context).getEndTime();
174 return ((double) currentTime.getTimestamp() - startTime)
175 / (endTime - startTime);
176 }
177
178
179
180
181
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){
188 CtfTmfLightweightContext iter = new CtfTmfLightweightContext(this);
189 iter.seek(timestamp.getValue());
190 return iter;
191 }
192 return super.seekEvent(timestamp);
193 }
194
195 /**
196 * Method seekEvent.
197 * @param location ITmfLocation<?>
198 * @return ITmfContext
199 */
200 @Override
201 public ITmfContext seekEvent(final ITmfLocation location) {
202 CtfLocation currentLocation = (CtfLocation) location;
203 CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
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 */
209 if (currentLocation == null) {
210 currentLocation = new CtfLocation(new CtfLocationData(0L, 0L));
211 context.setRank(0);
212 }
213 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
214 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
215 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
216 }
217 context.setLocation(currentLocation);
218 if (location == null) {
219 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
220 if (event != null) {
221 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
222 }
223 }
224 if(context.getRank() != 0) {
225 context.setRank(ITmfContext.UNKNOWN_RANK);
226 }
227 return context;
228 }
229
230
231 @Override
232 public ITmfContext seekEvent(double ratio) {
233 CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
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);
239 context.setRank(ITmfContext.UNKNOWN_RANK);
240 return context;
241 }
242
243 /**
244 * Method readNextEvent.
245 * @param context ITmfContext
246 * @return CtfTmfEvent
247 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
248 */
249 @Override
250 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
251 CtfTmfEvent event = null;
252 if (context instanceof CtfTmfLightweightContext) {
253 if (CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
254 return null;
255 }
256 CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context;
257 event = ctfContext.getCurrentEvent();
258
259 if (event != null) {
260 updateAttributes(context, event.getTimestamp());
261 ctfContext.advance();
262 ctfContext.increaseRank();
263 }
264 }
265
266 return event;
267 }
268
269 /**
270 * Suppressing the warning, because the 'throws' will usually happen in
271 * sub-classes.
272 *
273 * @throws TmfTraceException
274 */
275 @SuppressWarnings("unused")
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;
282 }
283
284 /**
285 * @since 2.0
286 */
287 @Override
288 public ITmfStateSystem getStateSystem() {
289 return this.ss;
290 }
291
292 /**
293 * gets the CTFtrace that this is wrapping
294 * @return the CTF trace
295 */
296 public CTFTrace getCTFTrace() {
297 return fTrace;
298 }
299
300
301 //-------------------------------------------
302 // Environment Parameters
303 //-------------------------------------------
304 /**
305 * Method getNbEnvVars.
306 *
307 * @return int
308 */
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
334 //-------------------------------------------
335 // Clocks
336 //-------------------------------------------
337
338 /**
339 * gets the clock offset
340 * @return the clock offset in ns
341 */
342 public long getOffset(){
343 if( fTrace != null ) {
344 return fTrace.getOffset();
345 }
346 return 0;
347 }
348
349 //-------------------------------------------
350 // Parser
351 //-------------------------------------------
352
353 @Override
354 public CtfTmfEvent parseEvent(ITmfContext context) {
355 CtfTmfEvent event = null;
356 if( context instanceof CtfTmfLightweightContext ){
357 CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone();
358 event = itt.getCurrentEvent();
359 }
360 return event;
361 }
362
363 /**
364 * Sets the cache size for a CtfTmfTrace.
365 */
366 protected void setCacheSize() {
367 setCacheSize(DEFAULT_CACHE_SIZE);
368 }
369
370 //-------------------------------------------
371 // Helpers
372 //-------------------------------------------
373
374 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfLightweightContext context) {
375 return CtfIteratorManager.getIterator(trace, context);
376 }
377}
This page took 0.028348 seconds and 5 git commands to generate.