Commit | Line | Data |
---|---|---|
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 |
13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
14 | ||
788ddcbc MK |
15 | import java.util.ArrayList; |
16 | import java.util.ListIterator; | |
17 | ||
a3fc8213 AM |
18 | import org.eclipse.core.resources.IProject; |
19 | import org.eclipse.core.resources.IResource; | |
139d5c1a | 20 | import org.eclipse.core.runtime.CoreException; |
a3fc8213 AM |
21 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; |
22 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
1191a574 | 23 | import org.eclipse.linuxtools.tmf.core.ctfadaptor.CtfTmfTimestamp.TimestampType; |
64c2cb4c | 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
a3fc8213 | 25 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
b4f71e4a | 26 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
18ab1d18 | 27 | import org.eclipse.linuxtools.tmf.core.statesystem.IStateSystemQuerier; |
a3fc8213 | 28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
4b7c469f | 29 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
a3fc8213 | 30 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
4b7c469f | 31 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
a3fc8213 | 32 | |
bfe038ff | 33 | public class CtfTmfTrace extends TmfTrace<CtfTmfEvent> implements ITmfEventParser<CtfTmfEvent>{ |
a3fc8213 | 34 | |
788ddcbc | 35 | |
324a6a4a BH |
36 | //------------------------------------------- |
37 | // Constants | |
38 | //------------------------------------------- | |
39 | /** | |
40 | * Default cache size for CTF traces | |
41 | */ | |
42 | protected static final int DEFAULT_CACHE_SIZE = 50000; | |
788ddcbc | 43 | private static final int ITER_POOL_SIZE = 128; |
64c2cb4c | 44 | |
4b7c469f MK |
45 | //------------------------------------------- |
46 | // Fields | |
47 | //------------------------------------------- | |
a3fc8213 | 48 | |
324a6a4a | 49 | /** Reference to the state system assigned to this trace */ |
d26f90fd | 50 | protected IStateSystemQuerier ss = null; |
11d6f468 | 51 | |
4b7c469f MK |
52 | /* Reference to the CTF Trace */ |
53 | private CTFTrace fTrace; | |
a3fc8213 | 54 | |
788ddcbc MK |
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 | ||
4b7c469f MK |
65 | //------------------------------------------- |
66 | // TmfTrace Overrides | |
67 | //------------------------------------------- | |
b1baa808 MK |
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 | */ | |
a3fc8213 | 76 | @Override |
25e48683 | 77 | public void initTrace(final IResource resource, final String path, final Class<CtfTmfEvent> eventType) |
b4f71e4a | 78 | throws TmfTraceException { |
4a110860 AM |
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(); | |
4b7c469f | 84 | super.initTrace(resource, path, eventType); |
324a6a4a | 85 | |
e30ce12e AM |
86 | @SuppressWarnings("unused") |
87 | CtfTmfEventType type; | |
88 | ||
a3fc8213 AM |
89 | try { |
90 | this.fTrace = new CTFTrace(path); | |
788ddcbc MK |
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)); | |
aa572e22 | 94 | } |
788ddcbc | 95 | nextIter = fIterators.listIterator(0); |
99b483fe | 96 | /* Set the start and (current) end times for this trace */ |
788ddcbc | 97 | final CtfIterator iterator = getIterator(); |
99b483fe AM |
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()); | |
99b483fe AM |
103 | this.setEndTime(iterator.getCurrentEvent().getTimestamp()); |
104 | } | |
105 | ||
25e48683 | 106 | } catch (final CTFReaderException e) { |
a3fc8213 AM |
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 | */ | |
9fa32496 | 112 | throw new TmfTraceException(e.getMessage(), e); |
a3fc8213 | 113 | } |
99b483fe | 114 | |
99b483fe | 115 | //FIXME This should be called via the ExperimentUpdated signal |
11d6f468 | 116 | buildStateSystem(); |
139d5c1a AM |
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) { | |
9fa32496 | 123 | throw new TmfTraceException(e.getMessage(), e); |
139d5c1a AM |
124 | } |
125 | } | |
a3fc8213 AM |
126 | } |
127 | ||
b1baa808 MK |
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 | */ | |
a3fc8213 | 135 | @Override |
3bd44ac8 | 136 | public boolean validate(final IProject project, final String path) { |
a3fc8213 AM |
137 | try { |
138 | final CTFTrace temp = new CTFTrace(path); | |
139 | return temp.majortIsSet(); // random test | |
25e48683 | 140 | } catch (final CTFReaderException e) { |
90235d6b AM |
141 | /* Nope, not a CTF trace we can read */ |
142 | return false; | |
a3fc8213 | 143 | } |
a3fc8213 AM |
144 | } |
145 | ||
b1baa808 | 146 | /** |
f474d36b PT |
147 | * Method getCurrentLocation. This is not applicable in CTF |
148 | * @return null, since the trace has no knowledge of the current location | |
b1baa808 MK |
149 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation() |
150 | */ | |
a3fc8213 AM |
151 | @Override |
152 | public ITmfLocation<?> getCurrentLocation() { | |
f474d36b | 153 | return null; |
a3fc8213 AM |
154 | } |
155 | ||
a3fc8213 | 156 | |
a3fc8213 AM |
157 | |
158 | @Override | |
4b7c469f MK |
159 | public double getLocationRatio(ITmfLocation<?> location) { |
160 | final CtfLocation curLocation = (CtfLocation) location; | |
788ddcbc MK |
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()) | |
4b7c469f | 168 | / (iterator.getEndTime() - iterator.getStartTime()); |
a3fc8213 AM |
169 | } |
170 | ||
788ddcbc MK |
171 | /** |
172 | * @return | |
173 | */ | |
174 | private CtfIterator getIterator() { | |
175 | if( !nextIter.hasNext()){ | |
176 | nextIter = fIterators.listIterator(0); | |
177 | } | |
178 | return nextIter.next(); | |
179 | } | |
180 | ||
64c2cb4c MK |
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){ | |
788ddcbc | 187 | CtfTmfLightweightContext iter = new CtfTmfLightweightContext(fIterators, nextIter); |
64c2cb4c MK |
188 | iter.seek(timestamp.getValue()); |
189 | return iter; | |
190 | } | |
191 | return super.seekEvent(timestamp); | |
192 | } | |
193 | ||
b1baa808 MK |
194 | /** |
195 | * Method seekEvent. | |
196 | * @param location ITmfLocation<?> | |
197 | * @return ITmfContext | |
198 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#seekEvent(ITmfLocation<?>) | |
199 | */ | |
a3fc8213 | 200 | @Override |
7e6347b0 | 201 | public ITmfContext seekEvent(final ITmfLocation<?> location) { |
ce2388e0 | 202 | CtfLocation currentLocation = (CtfLocation) location; |
788ddcbc | 203 | CtfTmfLightweightContext context = new CtfTmfLightweightContext(fIterators, nextIter); |
4a110860 AM |
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 | */ | |
11d6f468 | 209 | if (currentLocation == null) { |
ce2388e0 | 210 | currentLocation = new CtfLocation(0L); |
4a110860 | 211 | context.setRank(0); |
11d6f468 | 212 | } |
1191a574 | 213 | if (currentLocation.getLocation() == CtfLocation.INVALID_LOCATION) { |
4cf201de FC |
214 | ((CtfTmfTimestamp) getEndTime()).setType(TimestampType.NANOS); |
215 | currentLocation.setLocation(getEndTime().getValue() + 1); | |
1191a574 | 216 | } |
f474d36b | 217 | context.setLocation(currentLocation); |
64c2cb4c | 218 | if(context.getRank() != 0) { |
3bd44ac8 | 219 | context.setRank(ITmfContext.UNKNOWN_RANK); |
64c2cb4c | 220 | } |
f474d36b | 221 | return context; |
a3fc8213 AM |
222 | } |
223 | ||
a3fc8213 | 224 | |
a3fc8213 | 225 | @Override |
4b7c469f | 226 | public ITmfContext seekEvent(double ratio) { |
788ddcbc | 227 | CtfTmfLightweightContext context = new CtfTmfLightweightContext(fIterators, nextIter); |
4b7c469f | 228 | context.seek((long) (this.getNbEvents() * ratio)); |
f474d36b PT |
229 | context.setRank(ITmfContext.UNKNOWN_RANK); |
230 | return context; | |
a3fc8213 AM |
231 | } |
232 | ||
b1baa808 MK |
233 | /** |
234 | * Method readNextEvent. | |
235 | * @param context ITmfContext | |
236 | * @return CtfTmfEvent | |
c32744d6 | 237 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext) |
b1baa808 | 238 | */ |
a3fc8213 | 239 | @Override |
4b7c469f | 240 | public synchronized CtfTmfEvent getNext(final ITmfContext context) { |
f474d36b | 241 | CtfTmfEvent event = null; |
788ddcbc MK |
242 | if (context instanceof CtfTmfLightweightContext) { |
243 | CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context; | |
244 | event = ctfContext.getCurrentEvent(); | |
4a110860 | 245 | |
324a6a4a BH |
246 | if (event != null) { |
247 | updateAttributes(context, event.getTimestamp()); | |
788ddcbc MK |
248 | ctfContext.advance(); |
249 | ctfContext.increaseRank(); | |
324a6a4a | 250 | } |
f474d36b | 251 | } |
4a110860 | 252 | |
aa572e22 | 253 | return event; |
a3fc8213 AM |
254 | } |
255 | ||
b1baa808 | 256 | /** |
4b7c469f MK |
257 | * Suppressing the warning, because the 'throws' will usually happen in |
258 | * sub-classes. | |
259 | * @throws TmfTraceException | |
b1baa808 | 260 | */ |
3bd44ac8 | 261 | @SuppressWarnings({ "static-method" }) |
4b7c469f MK |
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; | |
a3fc8213 AM |
268 | } |
269 | ||
b1baa808 MK |
270 | /** |
271 | * Method getStateSystem. | |
4b7c469f | 272 | * |
b1baa808 MK |
273 | * @return IStateSystemQuerier |
274 | */ | |
d26f90fd | 275 | public IStateSystemQuerier getStateSystem() { |
11d6f468 AM |
276 | return this.ss; |
277 | } | |
278 | ||
4b7c469f MK |
279 | /** |
280 | * gets the CTFtrace that this is wrapping | |
281 | * @return the CTF trace | |
282 | */ | |
283 | public CTFTrace getCTFTrace() { | |
a3fc8213 AM |
284 | return fTrace; |
285 | } | |
a1a24d68 | 286 | |
8636b448 | 287 | |
4b7c469f MK |
288 | //------------------------------------------- |
289 | // Environment Parameters | |
290 | //------------------------------------------- | |
d26f90fd | 291 | /** |
4b7c469f MK |
292 | * Method getNbEnvVars. |
293 | * | |
294 | * @return int | |
d26f90fd | 295 | */ |
4b7c469f MK |
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 | ||
bfe038ff MK |
321 | //------------------------------------------- |
322 | // Clocks | |
323 | //------------------------------------------- | |
324 | ||
325 | public long getOffset(){ | |
326 | if( fTrace != null ) { | |
327 | return fTrace.getOffset(); | |
328 | } | |
329 | return 0; | |
330 | } | |
331 | ||
4b7c469f MK |
332 | //------------------------------------------- |
333 | // Parser | |
334 | //------------------------------------------- | |
335 | ||
336 | @Override | |
bfe038ff | 337 | public CtfTmfEvent parseEvent(ITmfContext context) { |
4b7c469f | 338 | CtfTmfEvent event = null; |
788ddcbc MK |
339 | if( context instanceof CtfTmfLightweightContext ){ |
340 | CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone(); | |
4b7c469f MK |
341 | event = itt.getCurrentEvent(); |
342 | } | |
343 | return event; | |
11d6f468 | 344 | } |
64c2cb4c | 345 | |
324a6a4a | 346 | /** |
64c2cb4c | 347 | * Sets the cache size for a CtfTmfTrace. |
324a6a4a BH |
348 | */ |
349 | protected void setCacheSize() { | |
350 | setCacheSize(DEFAULT_CACHE_SIZE); | |
351 | } | |
ce2388e0 | 352 | |
a3fc8213 | 353 | } |