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