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