d81825d3a39f6830d5487758d024017505ee126e
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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:
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
15
16 import org.eclipse.core.resources.IProject;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
19 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
20 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
21 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
22 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
23 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
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 CTF Trace */
51 private CTFTrace fTrace;
52
53
54
55 //-------------------------------------------
56 // TmfTrace Overrides
57 //-------------------------------------------
58 /**
59 * Method initTrace.
60 *
61 * @param resource
62 * The resource associated with this trace
63 * @param path
64 * The path to the trace file
65 * @param eventType
66 * The type of events that will be read from this trace
67 * @throws TmfTraceException
68 * If something when wrong while reading the trace
69 */
70 @Override
71 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
72 throws TmfTraceException {
73 /*
74 * Set the cache size. This has to be done before the call to super()
75 * because the super needs to know the cache size.
76 */
77 setCacheSize();
78
79 super.initTrace(resource, path, eventType);
80
81 @SuppressWarnings("unused")
82 CtfTmfEventType type;
83
84 try {
85 this.fTrace = new CTFTrace(path);
86 CtfIteratorManager.addTrace(this);
87 CtfTmfContext ctx;
88 /* Set the start and (current) end times for this trace */
89 ctx = (CtfTmfContext) seekEvent(0L);
90 CtfTmfEvent event = getNext(ctx);
91 if((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
92 /* Handle the case where the trace is empty */
93 this.setStartTime(TmfTimestamp.BIG_BANG);
94 } else {
95 final ITmfTimestamp curTime = event.getTimestamp();
96 this.setStartTime(curTime);
97 this.setEndTime(curTime);
98 }
99
100 } catch (final CTFReaderException e) {
101 /*
102 * If it failed at the init(), we can assume it's because the file
103 * was not found or was not recognized as a CTF trace. Throw into
104 * the new type of exception expected by the rest of TMF.
105 */
106 throw new TmfTraceException(e.getMessage(), e);
107 }
108 }
109
110 /* (non-Javadoc)
111 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
112 */
113 @Override
114 public synchronized void dispose() {
115 CtfIteratorManager.removeTrace(this);
116 if (fTrace != null) {
117 fTrace.dispose();
118 fTrace = null;
119 }
120 super.dispose();
121 }
122
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 */
130 @Override
131 public boolean validate(final IProject project, final String path) {
132 try {
133 final CTFTrace temp = new CTFTrace(path);
134 boolean valid = temp.majortIsSet(); // random test
135 temp.dispose();
136 return valid;
137 } catch (final CTFReaderException e) {
138 /* Nope, not a CTF trace we can read */
139 return false;
140 }
141 }
142
143 /**
144 * Method getCurrentLocation. This is not applicable in CTF
145 * @return null, since the trace has no knowledge of the current location
146 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
147 */
148 @Override
149 public ITmfLocation getCurrentLocation() {
150 return null;
151 }
152
153 @Override
154 public double getLocationRatio(ITmfLocation location) {
155 final CtfLocation curLocation = (CtfLocation) location;
156 final CtfTmfContext context = new CtfTmfContext(this);
157 context.setLocation(curLocation);
158 context.seek(curLocation.getLocationInfo());
159 final CtfLocationInfo currentTime = ((CtfLocationInfo)context.getLocation().getLocationInfo());
160 final long startTime = getIterator(this, context).getStartTime();
161 final long endTime = getIterator(this, context).getEndTime();
162 return ((double) currentTime.getTimestamp() - startTime)
163 / (endTime - startTime);
164 }
165
166 /**
167 * Method seekEvent.
168 * @param location ITmfLocation<?>
169 * @return ITmfContext
170 */
171 @Override
172 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
173 CtfLocation currentLocation = (CtfLocation) location;
174 CtfTmfContext context = new CtfTmfContext(this);
175 if (fTrace == null) {
176 context.setLocation(null);
177 context.setRank(ITmfContext.UNKNOWN_RANK);
178 return context;
179 }
180 /*
181 * The rank is set to 0 if the iterator seeks the beginning. If not, it
182 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
183 * by rank for now.
184 */
185 if (currentLocation == null) {
186 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
187 context.setRank(0);
188 }
189 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
190 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
191 }
192 context.setLocation(currentLocation);
193 if (location == null) {
194 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
195 if (event != null) {
196 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
197 }
198 }
199 if(context.getRank() != 0) {
200 context.setRank(ITmfContext.UNKNOWN_RANK);
201 }
202 return context;
203 }
204
205
206 @Override
207 public synchronized ITmfContext seekEvent(double ratio) {
208 CtfTmfContext context = new CtfTmfContext(this);
209 if (fTrace == null) {
210 context.setLocation(null);
211 context.setRank(ITmfContext.UNKNOWN_RANK);
212 return context;
213 }
214 final long end = this.getEndTime().getValue();
215 final long start = this.getStartTime().getValue();
216 final long diff = end - start;
217 final long ratioTs = Math.round(diff * ratio) + start;
218 context.seek(ratioTs);
219 context.setRank(ITmfContext.UNKNOWN_RANK);
220 return context;
221 }
222
223 /**
224 * Method readNextEvent.
225 * @param context ITmfContext
226 * @return CtfTmfEvent
227 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
228 */
229 @Override
230 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
231 if (fTrace == null) {
232 return null;
233 }
234 CtfTmfEvent event = null;
235 if (context instanceof CtfTmfContext) {
236 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
237 return null;
238 }
239 CtfTmfContext ctfContext = (CtfTmfContext) context;
240 event = ctfContext.getCurrentEvent();
241
242 if (event != null) {
243 updateAttributes(context, event.getTimestamp());
244 ctfContext.advance();
245 ctfContext.increaseRank();
246 }
247 }
248
249 return event;
250 }
251
252 /**
253 * gets the CTFtrace that this is wrapping
254 * @return the CTF trace
255 */
256 public CTFTrace getCTFTrace() {
257 return fTrace;
258 }
259
260
261 //-------------------------------------------
262 // Environment Parameters
263 //-------------------------------------------
264 /**
265 * Method getNbEnvVars.
266 *
267 * @return int
268 */
269 public int getNbEnvVars() {
270 return this.fTrace.getEnvironment().size();
271 }
272
273 /**
274 * Method getEnvNames.
275 *
276 * @return String[]
277 */
278 public String[] getEnvNames() {
279 final String[] s = new String[getNbEnvVars()];
280 return this.fTrace.getEnvironment().keySet().toArray(s);
281 }
282
283 /**
284 * Method getEnvValue.
285 *
286 * @param key
287 * String
288 * @return String
289 */
290 public String getEnvValue(final String key) {
291 return this.fTrace.getEnvironment().get(key);
292 }
293
294 //-------------------------------------------
295 // Clocks
296 //-------------------------------------------
297
298 /**
299 * gets the clock offset
300 * @return the clock offset in ns
301 */
302 public long getOffset(){
303 if( fTrace != null ) {
304 return fTrace.getOffset();
305 }
306 return 0;
307 }
308
309 //-------------------------------------------
310 // Parser
311 //-------------------------------------------
312
313 @Override
314 public CtfTmfEvent parseEvent(ITmfContext context) {
315 CtfTmfEvent event = null;
316 if (context instanceof CtfTmfContext) {
317 final ITmfContext tmpContext = seekEvent(context.getLocation());
318 event = getNext(tmpContext);
319 }
320 return event;
321 }
322
323 /**
324 * Sets the cache size for a CtfTmfTrace.
325 */
326 protected void setCacheSize() {
327 setCacheSize(DEFAULT_CACHE_SIZE);
328 }
329
330 //-------------------------------------------
331 // Helpers
332 //-------------------------------------------
333
334 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfContext context) {
335 return CtfIteratorManager.getIterator(trace, context);
336 }
337
338 /**
339 * Get an iterator to the trace
340 *
341 * @return an iterator to the trace
342 * @since 2.0
343 */
344 public CtfIterator createIterator(){
345 return new CtfIterator(this);
346 }
347 }
This page took 0.038623 seconds and 5 git commands to generate.