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