be3f94d45792899f06e4a2e407a6d41bccbd3b9c
[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(@SuppressWarnings("unused") 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 /*
173 * The rank is set to 0 if the iterator seeks the beginning. If not, it
174 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
175 * by rank for now.
176 */
177 if (currentLocation == null) {
178 currentLocation = new CtfLocation(0L);
179 context.setRank(0);
180 }
181 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
182 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
183 currentLocation.setLocation(getEndTime().getValue() + 1);
184 }
185 context.setLocation(currentLocation);
186 if(context.getRank() != 0)
187 context.setRank(ITmfContext.UNKNOWN_RANK);
188 return context;
189 }
190
191
192 @Override
193 public ITmfContext seekEvent(double ratio) {
194 CtfIterator context = new CtfIterator(this);
195 context.seek((long) (this.getNbEvents() * ratio));
196 context.setRank(ITmfContext.UNKNOWN_RANK);
197 return context;
198 }
199
200 /**
201 * Method readNextEvent.
202 * @param context ITmfContext
203 * @return CtfTmfEvent
204 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
205 */
206 @Override
207 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
208 CtfTmfEvent event = null;
209 if (context instanceof CtfIterator) {
210 CtfIterator ctfIterator = (CtfIterator) context;
211 event = ctfIterator.getCurrentEvent();
212
213 if (event != null) {
214 updateAttributes(context, event.getTimestamp());
215 ctfIterator.advance();
216 ctfIterator.increaseRank();
217 }
218 }
219
220 return event;
221 }
222
223 /**
224 * Suppressing the warning, because the 'throws' will usually happen in
225 * sub-classes.
226 * @throws TmfTraceException
227 */
228 @SuppressWarnings({ "static-method", "unused" })
229 protected void buildStateSystem() throws TmfTraceException {
230 /*
231 * Nothing is done in the basic implementation, please specify
232 * how/if to build a state system in derived classes.
233 */
234 return;
235 }
236
237 /**
238 * Method getStateSystem.
239 *
240 * @return IStateSystemQuerier
241 */
242 public IStateSystemQuerier getStateSystem() {
243 return this.ss;
244 }
245
246 /**
247 *
248 * @param ed
249 * @return
250 */
251 private static ITmfEventField parseDeclaration(EventDeclaration ed) {
252 EventDefinition eventDef = ed.createDefinition(null);
253 return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
254 CtfTmfEvent.parseFields(eventDef));
255 }
256
257 /**
258 * gets the CTFtrace that this is wrapping
259 * @return the CTF trace
260 */
261 public CTFTrace getCTFTrace() {
262 return fTrace;
263 }
264
265
266 //-------------------------------------------
267 // Environment Parameters
268 //-------------------------------------------
269 /**
270 * Method getNbEnvVars.
271 *
272 * @return int
273 */
274 public int getNbEnvVars() {
275 return this.fTrace.getEnvironment().size();
276 }
277
278 /**
279 * Method getEnvNames.
280 *
281 * @return String[]
282 */
283 public String[] getEnvNames() {
284 final String[] s = new String[getNbEnvVars()];
285 return this.fTrace.getEnvironment().keySet().toArray(s);
286 }
287
288 /**
289 * Method getEnvValue.
290 *
291 * @param key
292 * String
293 * @return String
294 */
295 public String getEnvValue(final String key) {
296 return this.fTrace.getEnvironment().get(key);
297 }
298
299 //-------------------------------------------
300 // Clocks
301 //-------------------------------------------
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 CtfIterator ){
318 CtfIterator itt = (CtfIterator) context;
319 event = itt.getCurrentEvent();
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 }
This page took 0.038605 seconds and 5 git commands to generate.