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