tmf: Consolidate all state systems in ITmfTrace
[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 CtfTmfLightweightContext ctx;
84 /* Set the start and (current) end times for this trace */
85 ctx = (CtfTmfLightweightContext) 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 fTrace = null;
115 super.dispose();
116 }
117
118 /**
119 * Method validate.
120 * @param project IProject
121 * @param path String
122 * @return boolean
123 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
124 */
125 @Override
126 public boolean validate(final IProject project, final String path) {
127 try {
128 final CTFTrace temp = new CTFTrace(path);
129 return temp.majortIsSet(); // random test
130 } catch (final CTFReaderException e) {
131 /* Nope, not a CTF trace we can read */
132 return false;
133 }
134 }
135
136 /**
137 * Method getCurrentLocation. This is not applicable in CTF
138 * @return null, since the trace has no knowledge of the current location
139 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
140 */
141 @Override
142 public ITmfLocation getCurrentLocation() {
143 return null;
144 }
145
146 @Override
147 public double getLocationRatio(ITmfLocation location) {
148 final CtfLocation curLocation = (CtfLocation) location;
149 final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
150 context.setLocation(curLocation);
151 context.seek(curLocation.getLocationInfo());
152 final CtfLocationData currentTime = ((CtfLocationData)context.getLocation().getLocationInfo());
153 final long startTime = getIterator(this, context).getStartTime();
154 final long endTime = getIterator(this, context).getEndTime();
155 return ((double) currentTime.getTimestamp() - startTime)
156 / (endTime - startTime);
157 }
158
159 /**
160 * Method seekEvent.
161 * @param location ITmfLocation<?>
162 * @return ITmfContext
163 */
164 @Override
165 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
166 CtfLocation currentLocation = (CtfLocation) location;
167 CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
168 if (fTrace == null) {
169 context.setLocation(null);
170 context.setRank(ITmfContext.UNKNOWN_RANK);
171 return context;
172 }
173 /*
174 * The rank is set to 0 if the iterator seeks the beginning. If not, it
175 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
176 * by rank for now.
177 */
178 if (currentLocation == null) {
179 currentLocation = new CtfLocation(new CtfLocationData(0L, 0L));
180 context.setRank(0);
181 }
182 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
183 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
184 }
185 context.setLocation(currentLocation);
186 if (location == null) {
187 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
188 if (event != null) {
189 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
190 }
191 }
192 if(context.getRank() != 0) {
193 context.setRank(ITmfContext.UNKNOWN_RANK);
194 }
195 return context;
196 }
197
198
199 @Override
200 public synchronized ITmfContext seekEvent(double ratio) {
201 CtfTmfLightweightContext context = new CtfTmfLightweightContext(this);
202 if (fTrace == null) {
203 context.setLocation(null);
204 context.setRank(ITmfContext.UNKNOWN_RANK);
205 return context;
206 }
207 final long end = this.getEndTime().getValue();
208 final long start = this.getStartTime().getValue();
209 final long diff = end - start;
210 final long ratioTs = Math.round(diff * ratio) + start;
211 context.seek(ratioTs);
212 context.setRank(ITmfContext.UNKNOWN_RANK);
213 return context;
214 }
215
216 /**
217 * Method readNextEvent.
218 * @param context ITmfContext
219 * @return CtfTmfEvent
220 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
221 */
222 @Override
223 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
224 if (fTrace == null) {
225 return null;
226 }
227 CtfTmfEvent event = null;
228 if (context instanceof CtfTmfLightweightContext) {
229 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
230 return null;
231 }
232 CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context;
233 event = ctfContext.getCurrentEvent();
234
235 if (event != null) {
236 updateAttributes(context, event.getTimestamp());
237 ctfContext.advance();
238 ctfContext.increaseRank();
239 }
240 }
241
242 return event;
243 }
244
245 /**
246 * gets the CTFtrace that this is wrapping
247 * @return the CTF trace
248 */
249 public CTFTrace getCTFTrace() {
250 return fTrace;
251 }
252
253
254 //-------------------------------------------
255 // Environment Parameters
256 //-------------------------------------------
257 /**
258 * Method getNbEnvVars.
259 *
260 * @return int
261 */
262 public int getNbEnvVars() {
263 return this.fTrace.getEnvironment().size();
264 }
265
266 /**
267 * Method getEnvNames.
268 *
269 * @return String[]
270 */
271 public String[] getEnvNames() {
272 final String[] s = new String[getNbEnvVars()];
273 return this.fTrace.getEnvironment().keySet().toArray(s);
274 }
275
276 /**
277 * Method getEnvValue.
278 *
279 * @param key
280 * String
281 * @return String
282 */
283 public String getEnvValue(final String key) {
284 return this.fTrace.getEnvironment().get(key);
285 }
286
287 //-------------------------------------------
288 // Clocks
289 //-------------------------------------------
290
291 /**
292 * gets the clock offset
293 * @return the clock offset in ns
294 */
295 public long getOffset(){
296 if( fTrace != null ) {
297 return fTrace.getOffset();
298 }
299 return 0;
300 }
301
302 //-------------------------------------------
303 // Parser
304 //-------------------------------------------
305
306 @Override
307 public CtfTmfEvent parseEvent(ITmfContext context) {
308 CtfTmfEvent event = null;
309 if( context instanceof CtfTmfLightweightContext ){
310 CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone();
311 event = itt.getCurrentEvent();
312 }
313 return event;
314 }
315
316 /**
317 * Sets the cache size for a CtfTmfTrace.
318 */
319 protected void setCacheSize() {
320 setCacheSize(DEFAULT_CACHE_SIZE);
321 }
322
323 //-------------------------------------------
324 // Helpers
325 //-------------------------------------------
326
327 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfLightweightContext context) {
328 return CtfIteratorManager.getIterator(trace, context);
329 }
330 }
This page took 0.058453 seconds and 6 git commands to generate.