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 | ||
a3fc8213 AM |
12 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; |
13 | ||
a3fc8213 AM |
14 | import org.eclipse.core.resources.IProject; |
15 | import org.eclipse.core.resources.IResource; | |
16 | import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException; | |
17 | import org.eclipse.linuxtools.ctf.core.trace.CTFTrace; | |
6256d8ad | 18 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
64c2cb4c | 19 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; |
a3fc8213 | 20 | import org.eclipse.linuxtools.tmf.core.event.TmfTimestamp; |
b4f71e4a | 21 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
a3fc8213 | 22 | import org.eclipse.linuxtools.tmf.core.trace.ITmfContext; |
4b7c469f | 23 | import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser; |
a3fc8213 | 24 | import org.eclipse.linuxtools.tmf.core.trace.ITmfLocation; |
4b7c469f | 25 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
a3fc8213 | 26 | |
9ac2eb62 | 27 | /** |
d09f973b FC |
28 | * The CTf trace handler |
29 | * | |
30 | * @version 1.0 | |
31 | * @author Matthew khouzam | |
9ac2eb62 | 32 | */ |
1e1bef82 | 33 | public class CtfTmfTrace extends TmfTrace implements ITmfEventParser { |
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; | |
64c2cb4c | 43 | |
4b7c469f MK |
44 | //------------------------------------------- |
45 | // Fields | |
46 | //------------------------------------------- | |
a3fc8213 | 47 | |
4b7c469f MK |
48 | /* Reference to the CTF Trace */ |
49 | private CTFTrace fTrace; | |
a3fc8213 | 50 | |
53b235e1 | 51 | |
788ddcbc | 52 | |
4b7c469f MK |
53 | //------------------------------------------- |
54 | // TmfTrace Overrides | |
55 | //------------------------------------------- | |
b1baa808 MK |
56 | /** |
57 | * Method initTrace. | |
063f0d27 AM |
58 | * |
59 | * @param resource | |
60 | * The resource associated with this trace | |
61 | * @param path | |
62 | * The path to the trace file | |
63 | * @param eventType | |
64 | * The type of events that will be read from this trace | |
b1baa808 | 65 | * @throws TmfTraceException |
063f0d27 | 66 | * If something when wrong while reading the trace |
b1baa808 | 67 | */ |
a3fc8213 | 68 | @Override |
6256d8ad | 69 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) |
b4f71e4a | 70 | throws TmfTraceException { |
4a110860 AM |
71 | /* |
72 | * Set the cache size. This has to be done before the call to super() | |
73 | * because the super needs to know the cache size. | |
74 | */ | |
75 | setCacheSize(); | |
324a6a4a | 76 | |
e30ce12e AM |
77 | @SuppressWarnings("unused") |
78 | CtfTmfEventType type; | |
79 | ||
a3fc8213 AM |
80 | try { |
81 | this.fTrace = new CTFTrace(path); | |
53b235e1 MK |
82 | CtfIteratorManager.addTrace(this); |
83 | CtfTmfLightweightContext ctx; | |
99b483fe | 84 | /* Set the start and (current) end times for this trace */ |
132a02b0 MK |
85 | ctx = (CtfTmfLightweightContext) seekEvent(0L); |
86 | CtfTmfEvent event = getNext(ctx); | |
30bf5897 | 87 | if((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) { |
99b483fe AM |
88 | /* Handle the case where the trace is empty */ |
89 | this.setStartTime(TmfTimestamp.BIG_BANG); | |
90 | } else { | |
132a02b0 | 91 | final ITmfTimestamp curTime = event.getTimestamp(); |
21fb02fa MK |
92 | this.setStartTime(curTime); |
93 | this.setEndTime(curTime); | |
99b483fe AM |
94 | } |
95 | ||
25e48683 | 96 | } catch (final CTFReaderException e) { |
a3fc8213 AM |
97 | /* |
98 | * If it failed at the init(), we can assume it's because the file | |
99 | * was not found or was not recognized as a CTF trace. Throw into | |
100 | * the new type of exception expected by the rest of TMF. | |
101 | */ | |
9fa32496 | 102 | throw new TmfTraceException(e.getMessage(), e); |
a3fc8213 | 103 | } |
99b483fe | 104 | |
200789b3 | 105 | super.initTrace(resource, path, eventType); |
a3fc8213 AM |
106 | } |
107 | ||
53b235e1 MK |
108 | /* (non-Javadoc) |
109 | * @see org.eclipse.linuxtools.tmf.core.trace.TmfTrace#dispose() | |
110 | */ | |
111 | @Override | |
112 | public synchronized void dispose() { | |
113 | CtfIteratorManager.removeTrace(this); | |
5d1c6919 PT |
114 | if (fTrace != null) { |
115 | fTrace.dispose(); | |
116 | fTrace = null; | |
117 | } | |
53b235e1 MK |
118 | super.dispose(); |
119 | } | |
120 | ||
b1baa808 MK |
121 | /** |
122 | * Method validate. | |
123 | * @param project IProject | |
124 | * @param path String | |
125 | * @return boolean | |
126 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String) | |
127 | */ | |
a3fc8213 | 128 | @Override |
3bd44ac8 | 129 | public boolean validate(final IProject project, final String path) { |
a3fc8213 AM |
130 | try { |
131 | final CTFTrace temp = new CTFTrace(path); | |
81fe3479 PT |
132 | boolean valid = temp.majortIsSet(); // random test |
133 | temp.dispose(); | |
134 | return valid; | |
25e48683 | 135 | } catch (final CTFReaderException e) { |
90235d6b AM |
136 | /* Nope, not a CTF trace we can read */ |
137 | return false; | |
a3fc8213 | 138 | } |
a3fc8213 AM |
139 | } |
140 | ||
b1baa808 | 141 | /** |
f474d36b PT |
142 | * Method getCurrentLocation. This is not applicable in CTF |
143 | * @return null, since the trace has no knowledge of the current location | |
b1baa808 MK |
144 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation() |
145 | */ | |
a3fc8213 | 146 | @Override |
1e1bef82 | 147 | public ITmfLocation getCurrentLocation() { |
f474d36b | 148 | return null; |
a3fc8213 AM |
149 | } |
150 | ||
a3fc8213 | 151 | @Override |
1e1bef82 | 152 | public double getLocationRatio(ITmfLocation location) { |
4b7c469f | 153 | final CtfLocation curLocation = (CtfLocation) location; |
53b235e1 MK |
154 | final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this); |
155 | context.setLocation(curLocation); | |
5976d44a FC |
156 | context.seek(curLocation.getLocationInfo()); |
157 | final CtfLocationData currentTime = ((CtfLocationData)context.getLocation().getLocationInfo()); | |
53b235e1 MK |
158 | final long startTime = getIterator(this, context).getStartTime(); |
159 | final long endTime = getIterator(this, context).getEndTime(); | |
132a02b0 | 160 | return ((double) currentTime.getTimestamp() - startTime) |
53b235e1 | 161 | / (endTime - startTime); |
a3fc8213 AM |
162 | } |
163 | ||
b1baa808 MK |
164 | /** |
165 | * Method seekEvent. | |
166 | * @param location ITmfLocation<?> | |
167 | * @return ITmfContext | |
b1baa808 | 168 | */ |
a3fc8213 | 169 | @Override |
76643eb7 | 170 | public synchronized ITmfContext seekEvent(final ITmfLocation location) { |
ce2388e0 | 171 | CtfLocation currentLocation = (CtfLocation) location; |
53b235e1 | 172 | CtfTmfLightweightContext context = new CtfTmfLightweightContext(this); |
76643eb7 BH |
173 | if (fTrace == null) { |
174 | context.setLocation(null); | |
175 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
176 | return context; | |
177 | } | |
4a110860 AM |
178 | /* |
179 | * The rank is set to 0 if the iterator seeks the beginning. If not, it | |
180 | * will be set to UNKNOWN_RANK, since CTF traces don't support seeking | |
181 | * by rank for now. | |
182 | */ | |
11d6f468 | 183 | if (currentLocation == null) { |
132a02b0 | 184 | currentLocation = new CtfLocation(new CtfLocationData(0L, 0L)); |
4a110860 | 185 | context.setRank(0); |
11d6f468 | 186 | } |
5976d44a | 187 | if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) { |
d62bb185 | 188 | currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L); |
1191a574 | 189 | } |
f474d36b | 190 | context.setLocation(currentLocation); |
7f0bab07 PT |
191 | if (location == null) { |
192 | CtfTmfEvent event = getIterator(this, context).getCurrentEvent(); | |
193 | if (event != null) { | |
d62bb185 | 194 | currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0); |
7f0bab07 PT |
195 | } |
196 | } | |
64c2cb4c | 197 | if(context.getRank() != 0) { |
3bd44ac8 | 198 | context.setRank(ITmfContext.UNKNOWN_RANK); |
64c2cb4c | 199 | } |
f474d36b | 200 | return context; |
a3fc8213 AM |
201 | } |
202 | ||
a3fc8213 | 203 | |
a3fc8213 | 204 | @Override |
76643eb7 | 205 | public synchronized ITmfContext seekEvent(double ratio) { |
53b235e1 | 206 | CtfTmfLightweightContext context = new CtfTmfLightweightContext(this); |
76643eb7 BH |
207 | if (fTrace == null) { |
208 | context.setLocation(null); | |
209 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
210 | return context; | |
211 | } | |
b2dc9e02 MK |
212 | final long end = this.getEndTime().getValue(); |
213 | final long start = this.getStartTime().getValue(); | |
214 | final long diff = end - start; | |
15e89960 | 215 | final long ratioTs = Math.round(diff * ratio) + start; |
b2dc9e02 | 216 | context.seek(ratioTs); |
f474d36b PT |
217 | context.setRank(ITmfContext.UNKNOWN_RANK); |
218 | return context; | |
a3fc8213 AM |
219 | } |
220 | ||
b1baa808 MK |
221 | /** |
222 | * Method readNextEvent. | |
223 | * @param context ITmfContext | |
224 | * @return CtfTmfEvent | |
c32744d6 | 225 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext) |
b1baa808 | 226 | */ |
a3fc8213 | 227 | @Override |
4b7c469f | 228 | public synchronized CtfTmfEvent getNext(final ITmfContext context) { |
faa38350 PT |
229 | if (fTrace == null) { |
230 | return null; | |
231 | } | |
f474d36b | 232 | CtfTmfEvent event = null; |
788ddcbc | 233 | if (context instanceof CtfTmfLightweightContext) { |
575beffc | 234 | if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) { |
ae09313d PT |
235 | return null; |
236 | } | |
788ddcbc MK |
237 | CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context; |
238 | event = ctfContext.getCurrentEvent(); | |
4a110860 | 239 | |
324a6a4a BH |
240 | if (event != null) { |
241 | updateAttributes(context, event.getTimestamp()); | |
788ddcbc MK |
242 | ctfContext.advance(); |
243 | ctfContext.increaseRank(); | |
324a6a4a | 244 | } |
f474d36b | 245 | } |
4a110860 | 246 | |
aa572e22 | 247 | return event; |
a3fc8213 AM |
248 | } |
249 | ||
4b7c469f MK |
250 | /** |
251 | * gets the CTFtrace that this is wrapping | |
252 | * @return the CTF trace | |
253 | */ | |
254 | public CTFTrace getCTFTrace() { | |
a3fc8213 AM |
255 | return fTrace; |
256 | } | |
a1a24d68 | 257 | |
8636b448 | 258 | |
4b7c469f MK |
259 | //------------------------------------------- |
260 | // Environment Parameters | |
261 | //------------------------------------------- | |
d26f90fd | 262 | /** |
4b7c469f MK |
263 | * Method getNbEnvVars. |
264 | * | |
265 | * @return int | |
d26f90fd | 266 | */ |
4b7c469f MK |
267 | public int getNbEnvVars() { |
268 | return this.fTrace.getEnvironment().size(); | |
269 | } | |
270 | ||
271 | /** | |
272 | * Method getEnvNames. | |
273 | * | |
274 | * @return String[] | |
275 | */ | |
276 | public String[] getEnvNames() { | |
277 | final String[] s = new String[getNbEnvVars()]; | |
278 | return this.fTrace.getEnvironment().keySet().toArray(s); | |
279 | } | |
280 | ||
281 | /** | |
282 | * Method getEnvValue. | |
283 | * | |
284 | * @param key | |
285 | * String | |
286 | * @return String | |
287 | */ | |
288 | public String getEnvValue(final String key) { | |
289 | return this.fTrace.getEnvironment().get(key); | |
290 | } | |
291 | ||
bfe038ff MK |
292 | //------------------------------------------- |
293 | // Clocks | |
294 | //------------------------------------------- | |
295 | ||
9ac2eb62 MK |
296 | /** |
297 | * gets the clock offset | |
298 | * @return the clock offset in ns | |
299 | */ | |
bfe038ff MK |
300 | public long getOffset(){ |
301 | if( fTrace != null ) { | |
302 | return fTrace.getOffset(); | |
303 | } | |
304 | return 0; | |
305 | } | |
306 | ||
4b7c469f MK |
307 | //------------------------------------------- |
308 | // Parser | |
309 | //------------------------------------------- | |
310 | ||
311 | @Override | |
bfe038ff | 312 | public CtfTmfEvent parseEvent(ITmfContext context) { |
4b7c469f | 313 | CtfTmfEvent event = null; |
788ddcbc MK |
314 | if( context instanceof CtfTmfLightweightContext ){ |
315 | CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone(); | |
4b7c469f MK |
316 | event = itt.getCurrentEvent(); |
317 | } | |
318 | return event; | |
11d6f468 | 319 | } |
64c2cb4c | 320 | |
324a6a4a | 321 | /** |
64c2cb4c | 322 | * Sets the cache size for a CtfTmfTrace. |
324a6a4a BH |
323 | */ |
324 | protected void setCacheSize() { | |
325 | setCacheSize(DEFAULT_CACHE_SIZE); | |
326 | } | |
ce2388e0 | 327 | |
53b235e1 MK |
328 | //------------------------------------------- |
329 | // Helpers | |
330 | //------------------------------------------- | |
331 | ||
332 | private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfLightweightContext context) { | |
333 | return CtfIteratorManager.getIterator(trace, context); | |
334 | } | |
a3fc8213 | 335 | } |