Add events structures, indexes and event caches per trace
[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 java.util.ArrayList;
16 import java.util.ListIterator;
17
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
22 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
23 import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
25 import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
26 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
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 //-------------------------------------------
37 // Constants
38 //-------------------------------------------
39 /**
40 * Default cache size for CTF traces
41 */
42 protected static final int DEFAULT_CACHE_SIZE = 50000;
43 private static final int ITER_POOL_SIZE = 128;
44
45 //-------------------------------------------
46 // Fields
47 //-------------------------------------------
48
49 /** Reference to the state system assigned to this trace */
50 protected IStateSystemQuerier ss = null;
51
52 /* Reference to the CTF Trace */
53 private CTFTrace fTrace;
54
55 /*
56 * The iterator pool. This is a necessary change since larger traces will
57 * need many contexts and each context must have to a file pointer. Since
58 * the OS supports only so many handles on a given file, but the UI must
59 * still be responsive, parallel seeks (up to ITER_POOL_SIZE requests)
60 * can be made with a fast response time.
61 * */
62 private ArrayList<CtfIterator> fIterators;
63 private ListIterator<CtfIterator> nextIter;
64
65 //-------------------------------------------
66 // TmfTrace Overrides
67 //-------------------------------------------
68 /**
69 * Method initTrace.
70 * @param resource IResource
71 * @param path String
72 * @param eventType Class<CtfTmfEvent>
73 * @throws TmfTraceException
74 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#initTrace(IResource, String, Class<CtfTmfEvent>)
75 */
76 @Override
77 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
78 throws TmfTraceException {
79 /*
80 * Set the cache size. This has to be done before the call to super()
81 * because the super needs to know the cache size.
82 */
83 setCacheSize();
84 super.initTrace(resource, path, eventType);
85
86 @SuppressWarnings("unused")
87 CtfTmfEventType type;
88
89 try {
90 this.fTrace = new CTFTrace(path);
91 fIterators = new ArrayList<CtfIterator>(ITER_POOL_SIZE);
92 for(int i = 0 ; i < ITER_POOL_SIZE; i++){
93 fIterators.add(new CtfIterator(this, 0, 0));
94 }
95 nextIter = fIterators.listIterator(0);
96 /* Set the start and (current) end times for this trace */
97 final CtfIterator iterator = getIterator();
98 if(iterator.getLocation().equals(CtfIterator.NULL_LOCATION)) {
99 /* Handle the case where the trace is empty */
100 this.setStartTime(TmfTimestamp.BIG_BANG);
101 } else {
102 this.setStartTime(iterator.getCurrentEvent().getTimestamp());
103 this.setEndTime(iterator.getCurrentEvent().getTimestamp());
104 }
105
106 } catch (final CTFReaderException e) {
107 /*
108 * If it failed at the init(), we can assume it's because the file
109 * was not found or was not recognized as a CTF trace. Throw into
110 * the new type of exception expected by the rest of TMF.
111 */
112 throw new TmfTraceException(e.getMessage(), e);
113 }
114
115 //FIXME This should be called via the ExperimentUpdated signal
116 buildStateSystem();
117
118 /* Refresh the project, so it can pick up new files that got created. */
119 if ( resource != null) {
120 try {
121 resource.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
122 } catch (CoreException e) {
123 throw new TmfTraceException(e.getMessage(), e);
124 }
125 }
126 }
127
128 /**
129 * Method validate.
130 * @param project IProject
131 * @param path String
132 * @return boolean
133 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
134 */
135 @Override
136 public boolean validate(final IProject project, final String path) {
137 try {
138 final CTFTrace temp = new CTFTrace(path);
139 return temp.majortIsSet(); // random test
140 } catch (final CTFReaderException e) {
141 /* Nope, not a CTF trace we can read */
142 return false;
143 }
144 }
145
146 /**
147 * Method getCurrentLocation. This is not applicable in CTF
148 * @return null, since the trace has no knowledge of the current location
149 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
150 */
151 @Override
152 public ITmfLocation<?> getCurrentLocation() {
153 return null;
154 }
155
156
157
158 @Override
159 public double getLocationRatio(ITmfLocation<?> location) {
160 final CtfLocation curLocation = (CtfLocation) location;
161 CtfIterator iterator = getIterator();
162 CtfTmfLightweightContext ctx = new CtfTmfLightweightContext(fIterators, nextIter);
163 ctx.setLocation(curLocation);
164 ctx.seek(curLocation.getLocation());
165 long currentTime = ((Long)ctx.getLocation().getLocation());
166
167 return ((double) currentTime - iterator.getStartTime())
168 / (iterator.getEndTime() - iterator.getStartTime());
169 }
170
171 /**
172 * @return
173 */
174 private CtfIterator getIterator() {
175 if( !nextIter.hasNext()){
176 nextIter = fIterators.listIterator(0);
177 }
178 return nextIter.next();
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(fIterators, nextIter);
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 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
199 */
200 @Override
201 public ITmfContext seekEvent(final ITmfLocation<?> location) {
202 CtfLocation currentLocation = (CtfLocation) location;
203 CtfTmfLightweightContext context = new CtfTmfLightweightContext(fIterators, nextIter);
204 /*
205 * The rank is set to 0 if the iterator seeks the beginning. If not, it
206 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
207 * by rank for now.
208 */
209 if (currentLocation == null) {
210 currentLocation = new CtfLocation(0L);
211 context.setRank(0);
212 }
213 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
214 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
215 currentLocation.setLocation(getEndTime().getValue() + 1);
216 }
217 context.setLocation(currentLocation);
218 if(context.getRank() != 0) {
219 context.setRank(ITmfContext.UNKNOWN_RANK);
220 }
221 return context;
222 }
223
224
225 @Override
226 public ITmfContext seekEvent(double ratio) {
227 CtfTmfLightweightContext context = new CtfTmfLightweightContext(fIterators, nextIter);
228 context.seek((long) (this.getNbEvents() * ratio));
229 context.setRank(ITmfContext.UNKNOWN_RANK);
230 return context;
231 }
232
233 /**
234 * Method readNextEvent.
235 * @param context ITmfContext
236 * @return CtfTmfEvent
237 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
238 */
239 @Override
240 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
241 CtfTmfEvent event = null;
242 if (context instanceof CtfTmfLightweightContext) {
243 CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context;
244 event = ctfContext.getCurrentEvent();
245
246 if (event != null) {
247 updateAttributes(context, event.getTimestamp());
248 ctfContext.advance();
249 ctfContext.increaseRank();
250 }
251 }
252
253 return event;
254 }
255
256 /**
257 * Suppressing the warning, because the 'throws' will usually happen in
258 * sub-classes.
259 * @throws TmfTraceException
260 */
261 @SuppressWarnings({ "static-method" })
262 protected void buildStateSystem() throws TmfTraceException {
263 /*
264 * Nothing is done in the basic implementation, please specify
265 * how/if to build a state system in derived classes.
266 */
267 return;
268 }
269
270 /**
271 * Method getStateSystem.
272 *
273 * @return IStateSystemQuerier
274 */
275 public IStateSystemQuerier getStateSystem() {
276 return this.ss;
277 }
278
279 /**
280 * gets the CTFtrace that this is wrapping
281 * @return the CTF trace
282 */
283 public CTFTrace getCTFTrace() {
284 return fTrace;
285 }
286
287
288 //-------------------------------------------
289 // Environment Parameters
290 //-------------------------------------------
291 /**
292 * Method getNbEnvVars.
293 *
294 * @return int
295 */
296 public int getNbEnvVars() {
297 return this.fTrace.getEnvironment().size();
298 }
299
300 /**
301 * Method getEnvNames.
302 *
303 * @return String[]
304 */
305 public String[] getEnvNames() {
306 final String[] s = new String[getNbEnvVars()];
307 return this.fTrace.getEnvironment().keySet().toArray(s);
308 }
309
310 /**
311 * Method getEnvValue.
312 *
313 * @param key
314 * String
315 * @return String
316 */
317 public String getEnvValue(final String key) {
318 return this.fTrace.getEnvironment().get(key);
319 }
320
321 //-------------------------------------------
322 // Clocks
323 //-------------------------------------------
324
325 public long getOffset(){
326 if( fTrace != null ) {
327 return fTrace.getOffset();
328 }
329 return 0;
330 }
331
332 //-------------------------------------------
333 // Parser
334 //-------------------------------------------
335
336 @Override
337 public CtfTmfEvent parseEvent(ITmfContext context) {
338 CtfTmfEvent event = null;
339 if( context instanceof CtfTmfLightweightContext ){
340 CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone();
341 event = itt.getCurrentEvent();
342 }
343 return event;
344 }
345
346 /**
347 * Sets the cache size for a CtfTmfTrace.
348 */
349 protected void setCacheSize() {
350 setCacheSize(DEFAULT_CACHE_SIZE);
351 }
352
353 }
This page took 0.061092 seconds and 6 git commands to generate.