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