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