Fix NLS-related Javadoc warnings
[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.core.runtime.IStatus;
19 import org.eclipse.core.runtime.Status;
20 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
22 import org.eclipse.linuxtools.internal.tmf.core.Activator;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
24 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
25 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
26 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
27 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
30 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
31
32 /**
33 * The CTf trace handler
34 *
35 * @version 1.0
36 * @author Matthew khouzam
37 */
38 public class CtfTmfTrace extends TmfTrace implements ITmfEventParser {
39
40 // -------------------------------------------
41 // Constants
42 // -------------------------------------------
43 /**
44 * Default cache size for CTF traces
45 */
46 protected static final int DEFAULT_CACHE_SIZE = 50000;
47
48 // -------------------------------------------
49 // Fields
50 // -------------------------------------------
51
52 /* Reference to the CTF Trace */
53 private CTFTrace fTrace;
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 /*
111 * (non-Javadoc)
112 *
113 * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose()
114 */
115 @Override
116 public synchronized void dispose() {
117 CtfIteratorManager.removeTrace(this);
118 if (fTrace != null) {
119 fTrace.dispose();
120 fTrace = null;
121 }
122 super.dispose();
123 }
124
125 /**
126 * Method validate.
127 *
128 * @param project
129 * IProject
130 * @param path
131 * String
132 * @return IStatus IStatus.error or Status.OK_STATUS
133 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
134 * @since 2.0
135 */
136 @Override
137 public IStatus validate(final IProject project, final String path) {
138 IStatus validTrace = Status.OK_STATUS;
139 try {
140 final CTFTrace temp = new CTFTrace(path);
141 boolean valid = temp.majortIsSet(); // random test
142 temp.dispose();
143 if (!valid) {
144 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
145 }
146 } catch (final CTFReaderException e) {
147 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError +": " + e.toString()); //$NON-NLS-1$
148 }
149 return validTrace;
150 }
151
152 /**
153 * Method getCurrentLocation. This is not applicable in CTF
154 *
155 * @return null, since the trace has no knowledge of the current location
156 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
157 */
158 @Override
159 public ITmfLocation getCurrentLocation() {
160 return null;
161 }
162
163 @Override
164 public double getLocationRatio(ITmfLocation location) {
165 final CtfLocation curLocation = (CtfLocation) location;
166 final CtfTmfContext context = new CtfTmfContext(this);
167 context.setLocation(curLocation);
168 context.seek(curLocation.getLocationInfo());
169 final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
170 final long startTime = getIterator(this, context).getStartTime();
171 final long endTime = getIterator(this, context).getEndTime();
172 return ((double) currentTime.getTimestamp() - startTime)
173 / (endTime - startTime);
174 }
175
176 /**
177 * Method seekEvent.
178 *
179 * @param location
180 * ITmfLocation<?>
181 * @return ITmfContext
182 */
183 @Override
184 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
185 CtfLocation currentLocation = (CtfLocation) location;
186 CtfTmfContext context = new CtfTmfContext(this);
187 if (fTrace == null) {
188 context.setLocation(null);
189 context.setRank(ITmfContext.UNKNOWN_RANK);
190 return context;
191 }
192 /*
193 * The rank is set to 0 if the iterator seeks the beginning. If not, it
194 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
195 * by rank for now.
196 */
197 if (currentLocation == null) {
198 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
199 context.setRank(0);
200 }
201 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
202 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
203 }
204 context.setLocation(currentLocation);
205 if (location == null) {
206 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
207 if (event != null) {
208 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
209 }
210 }
211 if (context.getRank() != 0) {
212 context.setRank(ITmfContext.UNKNOWN_RANK);
213 }
214 return context;
215 }
216
217 @Override
218 public synchronized ITmfContext seekEvent(double ratio) {
219 CtfTmfContext context = new CtfTmfContext(this);
220 if (fTrace == null) {
221 context.setLocation(null);
222 context.setRank(ITmfContext.UNKNOWN_RANK);
223 return context;
224 }
225 final long end = this.getEndTime().getValue();
226 final long start = this.getStartTime().getValue();
227 final long diff = end - start;
228 final long ratioTs = Math.round(diff * ratio) + start;
229 context.seek(ratioTs);
230 context.setRank(ITmfContext.UNKNOWN_RANK);
231 return context;
232 }
233
234 /**
235 * Method readNextEvent.
236 *
237 * @param context
238 * ITmfContext
239 * @return CtfTmfEvent
240 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
241 */
242 @Override
243 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
244 if (fTrace == null) {
245 return null;
246 }
247 CtfTmfEvent event = null;
248 if (context instanceof CtfTmfContext) {
249 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
250 return null;
251 }
252 CtfTmfContext ctfContext = (CtfTmfContext) context;
253 event = ctfContext.getCurrentEvent();
254
255 if (event != null) {
256 updateAttributes(context, event.getTimestamp());
257 ctfContext.advance();
258 ctfContext.increaseRank();
259 }
260 }
261
262 return event;
263 }
264
265 /**
266 * gets the CTFtrace that this is wrapping
267 *
268 * @return the CTF trace
269 */
270 public CTFTrace getCTFTrace() {
271 return fTrace;
272 }
273
274 // -------------------------------------------
275 // Environment Parameters
276 // -------------------------------------------
277 /**
278 * Method getNbEnvVars.
279 *
280 * @return int
281 */
282 public int getNbEnvVars() {
283 return this.fTrace.getEnvironment().size();
284 }
285
286 /**
287 * Method getEnvNames.
288 *
289 * @return String[]
290 */
291 public String[] getEnvNames() {
292 final String[] s = new String[getNbEnvVars()];
293 return this.fTrace.getEnvironment().keySet().toArray(s);
294 }
295
296 /**
297 * Method getEnvValue.
298 *
299 * @param key
300 * String
301 * @return String
302 */
303 public String getEnvValue(final String key) {
304 return this.fTrace.getEnvironment().get(key);
305 }
306
307 // -------------------------------------------
308 // Clocks
309 // -------------------------------------------
310
311 /**
312 * gets the clock offset
313 *
314 * @return the clock offset in ns
315 */
316 public long getOffset() {
317 if (fTrace != null) {
318 return fTrace.getOffset();
319 }
320 return 0;
321 }
322
323 // -------------------------------------------
324 // Parser
325 // -------------------------------------------
326
327 @Override
328 public CtfTmfEvent parseEvent(ITmfContext context) {
329 CtfTmfEvent event = null;
330 if (context instanceof CtfTmfContext) {
331 final ITmfContext tmpContext = seekEvent(context.getLocation());
332 event = getNext(tmpContext);
333 }
334 return event;
335 }
336
337 /**
338 * Sets the cache size for a CtfTmfTrace.
339 */
340 protected void setCacheSize() {
341 setCacheSize(DEFAULT_CACHE_SIZE);
342 }
343
344 // -------------------------------------------
345 // Helpers
346 // -------------------------------------------
347
348 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfContext context) {
349 return CtfIteratorManager.getIterator(trace, context);
350 }
351
352 /**
353 * Get an iterator to the trace
354 *
355 * @return an iterator to the trace
356 * @since 2.0
357 */
358 public CtfIterator createIterator() {
359 return new CtfIterator(this);
360 }
361 }
This page took 0.037944 seconds and 5 git commands to generate.