Replace location by context in checkpoint indexer
[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
a3fc8213
AM
15import org.eclipse.core.resources.IProject;
16import org.eclipse.core.resources.IResource;
139d5c1a 17import org.eclipse.core.runtime.CoreException;
aa572e22
MK
18import org.eclipse.linuxtools.ctf.core.event.EventDeclaration;
19import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
a3fc8213
AM
20import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
21import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
1191a574 22import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType;
aa572e22 23import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
a3fc8213 24import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp;
b4f71e4a 25import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
18ab1d18 26import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier;
a3fc8213 27import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
4b7c469f 28import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
a3fc8213 29import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation;
4b7c469f 30import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
a3fc8213 31
bfe038ff 32public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{
a3fc8213 33
324a6a4a
BH
34 //-------------------------------------------
35 // Constants
36 //-------------------------------------------
37 /**
38 * Default cache size for CTF traces
39 */
40 protected static final int DEFAULT_CACHE_SIZE = 50000;
41
4b7c469f
MK
42 //-------------------------------------------
43 // Fields
44 //-------------------------------------------
a3fc8213 45
324a6a4a 46 /** Reference to the state system assigned to this trace */
d26f90fd 47 protected IStateSystemQuerier ss = null;
11d6f468 48
4b7c469f
MK
49 /* Reference to the CTF Trace */
50 private CTFTrace fTrace;
a3fc8213 51
4b7c469f
MK
52 //-------------------------------------------
53 // TmfTrace Overrides
54 //-------------------------------------------
b1baa808
MK
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 */
a3fc8213 63 @Override
25e48683 64 public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType)
b4f71e4a 65 throws TmfTraceException {
4a110860
AM
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();
4b7c469f 71 super.initTrace(resource, path, eventType);
e30ce12e
AM
72 EventDeclaration ed;
73 ITmfEventField eventField;
324a6a4a 74
e30ce12e
AM
75 @SuppressWarnings("unused")
76 CtfTmfEventType type;
77
a3fc8213
AM
78 try {
79 this.fTrace = new CTFTrace(path);
aa572e22 80 for( int i =0 ; i< this.fTrace.getNbEventTypes(); i++) {
e30ce12e
AM
81 ed = this.fTrace.getEventType(i);
82 eventField = parseDeclaration(ed);
99b483fe
AM
83 /*
84 * Populate the event manager with event types that are there in
85 * the beginning.
86 */
e30ce12e 87 type = new CtfTmfEventType(ed.getName(), eventField);
aa572e22 88 }
99b483fe
AM
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());
99b483fe
AM
97 this.setEndTime(iterator.getCurrentEvent().getTimestamp());
98 }
99
25e48683 100 } catch (final CTFReaderException e) {
a3fc8213
AM
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 */
9fa32496 106 throw new TmfTraceException(e.getMessage(), e);
a3fc8213 107 }
99b483fe 108
99b483fe 109 //FIXME This should be called via the ExperimentUpdated signal
11d6f468 110 buildStateSystem();
139d5c1a
AM
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) {
9fa32496 117 throw new TmfTraceException(e.getMessage(), e);
139d5c1a
AM
118 }
119 }
a3fc8213
AM
120 }
121
b1baa808
MK
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 */
a3fc8213 129 @Override
4b7c469f 130 public boolean validate(@SuppressWarnings("unused") final IProject project, final String path) {
a3fc8213
AM
131 try {
132 final CTFTrace temp = new CTFTrace(path);
133 return temp.majortIsSet(); // random test
25e48683 134 } catch (final CTFReaderException e) {
90235d6b
AM
135 /* Nope, not a CTF trace we can read */
136 return false;
a3fc8213 137 }
a3fc8213
AM
138 }
139
b1baa808 140 /**
f474d36b
PT
141 * Method getCurrentLocation. This is not applicable in CTF
142 * @return null, since the trace has no knowledge of the current location
b1baa808
MK
143 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
144 */
a3fc8213
AM
145 @Override
146 public ITmfLocation<?> getCurrentLocation() {
f474d36b 147 return null;
a3fc8213
AM
148 }
149
a3fc8213 150
a3fc8213
AM
151
152 @Override
4b7c469f
MK
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());
a3fc8213
AM
160 }
161
b1baa808
MK
162 /**
163 * Method seekEvent.
164 * @param location ITmfLocation<?>
165 * @return ITmfContext
166 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>)
167 */
a3fc8213 168 @Override
7e6347b0 169 public ITmfContext seekEvent(final ITmfLocation<?> location) {
ce2388e0 170 CtfLocation currentLocation = (CtfLocation) location;
4a110860
AM
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 */
11d6f468 177 if (currentLocation == null) {
ce2388e0 178 currentLocation = new CtfLocation(0L);
4a110860 179 context.setRank(0);
11d6f468 180 }
1191a574 181 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
4cf201de
FC
182 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
183 currentLocation.setLocation(getEndTime().getValue() + 1);
1191a574 184 }
f474d36b 185 context.setLocation(currentLocation);
4a110860 186 if(context.getRank() != 0)
f474d36b
PT
187 context.setRank(ITmfContext.UNKNOWN_RANK);
188 return context;
a3fc8213
AM
189 }
190
a3fc8213 191
a3fc8213 192 @Override
4b7c469f 193 public ITmfContext seekEvent(double ratio) {
f474d36b 194 CtfIterator context = new CtfIterator(this);
4b7c469f 195 context.seek((long) (this.getNbEvents() * ratio));
f474d36b
PT
196 context.setRank(ITmfContext.UNKNOWN_RANK);
197 return context;
a3fc8213
AM
198 }
199
b1baa808
MK
200 /**
201 * Method readNextEvent.
202 * @param context ITmfContext
203 * @return CtfTmfEvent
c32744d6 204 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 205 */
a3fc8213 206 @Override
4b7c469f 207 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
f474d36b
PT
208 CtfTmfEvent event = null;
209 if (context instanceof CtfIterator) {
210 CtfIterator ctfIterator = (CtfIterator) context;
211 event = ctfIterator.getCurrentEvent();
4a110860 212
324a6a4a
BH
213 if (event != null) {
214 updateAttributes(context, event.getTimestamp());
4a110860
AM
215 ctfIterator.advance();
216 ctfIterator.increaseRank();
324a6a4a 217 }
f474d36b 218 }
4a110860 219
aa572e22 220 return event;
a3fc8213
AM
221 }
222
b1baa808 223 /**
4b7c469f
MK
224 * Suppressing the warning, because the 'throws' will usually happen in
225 * sub-classes.
226 * @throws TmfTraceException
b1baa808 227 */
4b7c469f
MK
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;
a3fc8213
AM
235 }
236
b1baa808
MK
237 /**
238 * Method getStateSystem.
4b7c469f 239 *
b1baa808
MK
240 * @return IStateSystemQuerier
241 */
d26f90fd 242 public IStateSystemQuerier getStateSystem() {
11d6f468
AM
243 return this.ss;
244 }
245
b1baa808 246 /**
4b7c469f
MK
247 *
248 * @param ed
249 * @return
b1baa808 250 */
4b7c469f
MK
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() {
a3fc8213
AM
262 return fTrace;
263 }
a1a24d68 264
8636b448 265
4b7c469f
MK
266 //-------------------------------------------
267 // Environment Parameters
268 //-------------------------------------------
d26f90fd 269 /**
4b7c469f
MK
270 * Method getNbEnvVars.
271 *
272 * @return int
d26f90fd 273 */
4b7c469f
MK
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
bfe038ff
MK
299 //-------------------------------------------
300 // Clocks
301 //-------------------------------------------
302
303 public long getOffset(){
304 if( fTrace != null ) {
305 return fTrace.getOffset();
306 }
307 return 0;
308 }
309
4b7c469f
MK
310 //-------------------------------------------
311 // Parser
312 //-------------------------------------------
313
314 @Override
bfe038ff 315 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f
MK
316 CtfTmfEvent event = null;
317 if( context instanceof CtfIterator ){
408e65d2 318 CtfIterator itt = (CtfIterator) context.clone();
4b7c469f
MK
319 event = itt.getCurrentEvent();
320 }
321 return event;
11d6f468 322 }
324a6a4a
BH
323
324 /**
325 * Sets the cache size for a CtfTmfTrace.
326 */
327 protected void setCacheSize() {
328 setCacheSize(DEFAULT_CACHE_SIZE);
329 }
ce2388e0 330
a3fc8213 331}
This page took 0.046652 seconds and 5 git commands to generate.