Fix #2 for bug381411
[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
13 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15 import org.eclipse.core.resources.IProject;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
19 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
20 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
22 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
24 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26 import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
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 public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{
33
34 //-------------------------------------------
35 // Constants
36 //-------------------------------------------
37 /**
38 * Default cache size for CTF traces
39 */
40 protected static final int DEFAULT_CACHE_SIZE = 50000;
41
42 //-------------------------------------------
43 // Fields
44 //-------------------------------------------
45
46 /** Reference to the state system assigned to this trace */
47 protected IStateSystemQuerier ss = null;
48
49 /* Reference to the CTF Trace */
50 private CTFTrace fTrace;
51
52 //-------------------------------------------
53 // TmfTrace Overrides
54 //-------------------------------------------
55 /**
56 * Method initTrace.
57 * @param resource IResource
58 * @param path String
59 * @param eventType Class<CtfTmfEvent>
60 * @throws TmfTraceException
61 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
62 */
63 @Override
64 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
65 throws TmfTraceException {
66 /*
67 * Set the cache size. This has to be done before the call to super()
68 * because the super needs to know the cache size.
69 */
70 setCacheSize();
71 super.initTrace(resource, path, eventType);
72 EventDeclaration ed;
73 ITmfEventField eventField;
74
75 @SuppressWarnings("unused")
76 CtfTmfEventType type;
77
78 try {
79 this.fTrace = new CTFTrace(path);
80 for( int i =0 ; i< this.fTrace.getNbEventTypes(); i++) {
81 ed = this.fTrace.getEventType(i);
82 eventField = parseDeclaration(ed);
83 /*
84 * Populate the event manager with event types that are there in
85 * the beginning.
86 */
87 type = new CtfTmfEventType(ed.getName(), eventField);
88 }
89
90 /* Set the start and (current) end times for this trace */
91 final CtfIterator iterator = new CtfIterator(this, 0, 0);
92 if(iterator.getLocation().equals(CtfIterator.NULL_LOCATION)) {
93 /* Handle the case where the trace is empty */
94 this.setStartTime(TmfTimestamp.BIG_BANG);
95 } else {
96 this.setStartTime(iterator.getCurrentEvent().getTimestamp());
97 this.setEndTime(iterator.getCurrentEvent().getTimestamp());
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 //FIXME This should be called via the ExperimentUpdated signal
110 buildStateSystem();
111
112 /* Refresh the project, so it can pick up new files that got created. */
113 if ( resource != null) {
114 try {
115 resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
116 } catch (CoreException e) {
117 throw new TmfTraceException(e.getMessage(), e);
118 }
119 }
120 }
121
122 /**
123 * Method validate.
124 * @param project IProject
125 * @param path String
126 * @return boolean
127 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
128 */
129 @Override
130 public boolean validate(final IProject project, final String path) {
131 try {
132 final CTFTrace temp = new CTFTrace(path);
133 return temp.majortIsSet(); // random test
134 } catch (final CTFReaderException e) {
135 /* Nope, not a CTF trace we can read */
136 return false;
137 }
138 }
139
140 /**
141 * Method getCurrentLocation. This is not applicable in CTF
142 * @return null, since the trace has no knowledge of the current location
143 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
144 */
145 @Override
146 public ITmfLocation<?> getCurrentLocation() {
147 return null;
148 }
149
150
151
152 @Override
153 public double getLocationRatio(ITmfLocation<?> location) {
154 final CtfLocation curLocation = (CtfLocation) location;
155 CtfIterator iterator = new CtfIterator(this);
156 iterator.seek(curLocation.getLocation());
157 return ((double) iterator.getCurrentEvent().getTimestampValue() - iterator
158 .getStartTime())
159 / (iterator.getEndTime() - iterator.getStartTime());
160 }
161
162 /**
163 * Method seekEvent.
164 * @param location ITmfLocation<?>
165 * @return ITmfContext
166 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
167 */
168 @Override
169 public ITmfContext seekEvent(final ITmfLocation<?> location) {
170 CtfLocation currentLocation = (CtfLocation) location;
171 CtfIterator context = new CtfIterator(this);
172 context.setRank(ITmfContext.UNKNOWN_RANK);
173
174 /*
175 * The rank is set to 0 if the iterator seeks the beginning. If not, it
176 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
177 * by rank for now.
178 */
179 if (currentLocation == null) {
180 currentLocation = new CtfLocation(0L);
181 context.setRank(0);
182 }
183 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
184 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
185 currentLocation.setLocation(getEndTime().getValue() + 1);
186 }
187 context.setLocation(currentLocation);
188 if(context.getRank() != 0)
189 context.setRank(ITmfContext.UNKNOWN_RANK);
190 return context;
191 }
192
193
194 @Override
195 public ITmfContext seekEvent(double ratio) {
196 CtfIterator context = new CtfIterator(this);
197 context.seek((long) (this.getNbEvents() * ratio));
198 context.setRank(ITmfContext.UNKNOWN_RANK);
199 return context;
200 }
201
202 /**
203 * Method readNextEvent.
204 * @param context ITmfContext
205 * @return CtfTmfEvent
206 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
207 */
208 @Override
209 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
210 CtfTmfEvent event = null;
211 if (context instanceof CtfIterator) {
212 CtfIterator ctfIterator = (CtfIterator) context;
213 event = ctfIterator.getCurrentEvent();
214
215 if (event != null) {
216 updateAttributes(context, event.getTimestamp());
217 ctfIterator.advance();
218 ctfIterator.increaseRank();
219 }
220 }
221
222 return event;
223 }
224
225 /**
226 * Suppressing the warning, because the 'throws' will usually happen in
227 * sub-classes.
228 * @throws TmfTraceException
229 */
230 @SuppressWarnings({ "static-method" })
231 protected void buildStateSystem() throws TmfTraceException {
232 /*
233 * Nothing is done in the basic implementation, please specify
234 * how/if to build a state system in derived classes.
235 */
236 return;
237 }
238
239 /**
240 * Method getStateSystem.
241 *
242 * @return IStateSystemQuerier
243 */
244 public IStateSystemQuerier getStateSystem() {
245 return this.ss;
246 }
247
248 /**
249 *
250 * @param ed
251 * @return
252 */
253 private static ITmfEventField parseDeclaration(EventDeclaration ed) {
254 EventDefinition eventDef = ed.createDefinition(null);
255 return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
256 CtfTmfEvent.parseFields(eventDef));
257 }
258
259 /**
260 * gets the CTFtrace that this is wrapping
261 * @return the CTF trace
262 */
263 public CTFTrace getCTFTrace() {
264 return fTrace;
265 }
266
267
268 //-------------------------------------------
269 // Environment Parameters
270 //-------------------------------------------
271 /**
272 * Method getNbEnvVars.
273 *
274 * @return int
275 */
276 public int getNbEnvVars() {
277 return this.fTrace.getEnvironment().size();
278 }
279
280 /**
281 * Method getEnvNames.
282 *
283 * @return String[]
284 */
285 public String[] getEnvNames() {
286 final String[] s = new String[getNbEnvVars()];
287 return this.fTrace.getEnvironment().keySet().toArray(s);
288 }
289
290 /**
291 * Method getEnvValue.
292 *
293 * @param key
294 * String
295 * @return String
296 */
297 public String getEnvValue(final String key) {
298 return this.fTrace.getEnvironment().get(key);
299 }
300
301 //-------------------------------------------
302 // Clocks
303 //-------------------------------------------
304
305 public long getOffset(){
306 if( fTrace != null ) {
307 return fTrace.getOffset();
308 }
309 return 0;
310 }
311
312 //-------------------------------------------
313 // Parser
314 //-------------------------------------------
315
316 @Override
317 public CtfTmfEvent parseEvent(ITmfContext context) {
318 CtfTmfEvent event = null;
319 if( context instanceof CtfIterator ){
320 CtfIterator itt = (CtfIterator) context.clone();
321 event = itt.getCurrentEvent();
322 }
323 return event;
324 }
325
326 /**
327 * Sets the cache size for a CtfTmfTrace.
328 */
329 protected void setCacheSize() {
330 setCacheSize(DEFAULT_CACHE_SIZE);
331 }
332
333 }
This page took 0.038685 seconds and 6 git commands to generate.