performance enhancement in seekevent by time
[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
3bd44ac8 130 public boolean validate(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 171 CtfIterator context = new CtfIterator(this);
3bd44ac8
FC
172 context.setRank(ITmfContext.UNKNOWN_RANK);
173
4a110860
AM
174 /*
175 * The rank is set to 0 if the iterator seeks the beginning. If not, it
176 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
177 * by rank for now.
178 */
11d6f468 179 if (currentLocation == null) {
ce2388e0 180 currentLocation = new CtfLocation(0L);
4a110860 181 context.setRank(0);
11d6f468 182 }
1191a574 183 if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) {
4cf201de
FC
184 ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS);
185 currentLocation.setLocation(getEndTime().getValue() + 1);
1191a574 186 }
f474d36b 187 context.setLocation(currentLocation);
4a110860 188 if(context.getRank() != 0)
3bd44ac8 189 context.setRank(ITmfContext.UNKNOWN_RANK);
f474d36b 190 return context;
a3fc8213
AM
191 }
192
a3fc8213 193
a3fc8213 194 @Override
4b7c469f 195 public ITmfContext seekEvent(double ratio) {
f474d36b 196 CtfIterator context = new CtfIterator(this);
4b7c469f 197 context.seek((long) (this.getNbEvents() * ratio));
f474d36b
PT
198 context.setRank(ITmfContext.UNKNOWN_RANK);
199 return context;
a3fc8213
AM
200 }
201
b1baa808
MK
202 /**
203 * Method readNextEvent.
204 * @param context ITmfContext
205 * @return CtfTmfEvent
c32744d6 206 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 207 */
a3fc8213 208 @Override
4b7c469f 209 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
f474d36b
PT
210 CtfTmfEvent event = null;
211 if (context instanceof CtfIterator) {
212 CtfIterator ctfIterator = (CtfIterator) context;
213 event = ctfIterator.getCurrentEvent();
4a110860 214
324a6a4a
BH
215 if (event != null) {
216 updateAttributes(context, event.getTimestamp());
4a110860
AM
217 ctfIterator.advance();
218 ctfIterator.increaseRank();
324a6a4a 219 }
f474d36b 220 }
4a110860 221
aa572e22 222 return event;
a3fc8213
AM
223 }
224
b1baa808 225 /**
4b7c469f
MK
226 * Suppressing the warning, because the 'throws' will usually happen in
227 * sub-classes.
228 * @throws TmfTraceException
b1baa808 229 */
3bd44ac8 230 @SuppressWarnings({ "static-method" })
4b7c469f
MK
231 protected void buildStateSystem() throws TmfTraceException {
232 /*
233 * Nothing is done in the basic implementation, please specify
234 * how/if to build a state system in derived classes.
235 */
236 return;
a3fc8213
AM
237 }
238
b1baa808
MK
239 /**
240 * Method getStateSystem.
4b7c469f 241 *
b1baa808
MK
242 * @return IStateSystemQuerier
243 */
d26f90fd 244 public IStateSystemQuerier getStateSystem() {
11d6f468
AM
245 return this.ss;
246 }
247
b1baa808 248 /**
4b7c469f
MK
249 *
250 * @param ed
251 * @return
b1baa808 252 */
4b7c469f
MK
253 private static ITmfEventField parseDeclaration(EventDeclaration ed) {
254 EventDefinition eventDef = ed.createDefinition(null);
255 return new CtfTmfContent(ITmfEventField.ROOT_FIELD_ID,
256 CtfTmfEvent.parseFields(eventDef));
257 }
258
259 /**
260 * gets the CTFtrace that this is wrapping
261 * @return the CTF trace
262 */
263 public CTFTrace getCTFTrace() {
a3fc8213
AM
264 return fTrace;
265 }
a1a24d68 266
8636b448 267
4b7c469f
MK
268 //-------------------------------------------
269 // Environment Parameters
270 //-------------------------------------------
d26f90fd 271 /**
4b7c469f
MK
272 * Method getNbEnvVars.
273 *
274 * @return int
d26f90fd 275 */
4b7c469f
MK
276 public int getNbEnvVars() {
277 return this.fTrace.getEnvironment().size();
278 }
279
280 /**
281 * Method getEnvNames.
282 *
283 * @return String[]
284 */
285 public String[] getEnvNames() {
286 final String[] s = new String[getNbEnvVars()];
287 return this.fTrace.getEnvironment().keySet().toArray(s);
288 }
289
290 /**
291 * Method getEnvValue.
292 *
293 * @param key
294 * String
295 * @return String
296 */
297 public String getEnvValue(final String key) {
298 return this.fTrace.getEnvironment().get(key);
299 }
300
bfe038ff
MK
301 //-------------------------------------------
302 // Clocks
303 //-------------------------------------------
304
305 public long getOffset(){
306 if( fTrace != null ) {
307 return fTrace.getOffset();
308 }
309 return 0;
310 }
311
4b7c469f
MK
312 //-------------------------------------------
313 // Parser
314 //-------------------------------------------
315
316 @Override
bfe038ff 317 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f
MK
318 CtfTmfEvent event = null;
319 if( context instanceof CtfIterator ){
408e65d2 320 CtfIterator itt = (CtfIterator) context.clone();
4b7c469f
MK
321 event = itt.getCurrentEvent();
322 }
323 return event;
11d6f468 324 }
324a6a4a
BH
325
326 /**
327 * Sets the cache size for a CtfTmfTrace.
328 */
329 protected void setCacheSize() {
330 setCacheSize(DEFAULT_CACHE_SIZE);
331 }
ce2388e0 332
a3fc8213 333}
This page took 0.066539 seconds and 5 git commands to generate.