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); | |
faa38350 | 114 | fTrace = null; |
53b235e1 MK |
115 | super.dispose(); |
116 | } | |
117 | ||
b1baa808 MK |
118 | /** |
119 | * Method validate. | |
120 | * @param project IProject | |
121 | * @param path String | |
122 | * @return boolean | |
123 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String) | |
124 | */ | |
a3fc8213 | 125 | @Override |
3bd44ac8 | 126 | public boolean validate(final IProject project, final String path) { |
a3fc8213 AM |
127 | try { |
128 | final CTFTrace temp = new CTFTrace(path); | |
129 | return temp.majortIsSet(); // random test | |
25e48683 | 130 | } catch (final CTFReaderException e) { |
90235d6b AM |
131 | /* Nope, not a CTF trace we can read */ |
132 | return false; | |
a3fc8213 | 133 | } |
a3fc8213 AM |
134 | } |
135 | ||
b1baa808 | 136 | /** |
f474d36b PT |
137 | * Method getCurrentLocation. This is not applicable in CTF |
138 | * @return null, since the trace has no knowledge of the current location | |
b1baa808 MK |
139 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation() |
140 | */ | |
a3fc8213 | 141 | @Override |
1e1bef82 | 142 | public ITmfLocation getCurrentLocation() { |
f474d36b | 143 | return null; |
a3fc8213 AM |
144 | } |
145 | ||
a3fc8213 | 146 | @Override |
1e1bef82 | 147 | public double getLocationRatio(ITmfLocation location) { |
4b7c469f | 148 | final CtfLocation curLocation = (CtfLocation) location; |
53b235e1 MK |
149 | final CtfTmfLightweightContext context = new CtfTmfLightweightContext(this); |
150 | context.setLocation(curLocation); | |
5976d44a FC |
151 | context.seek(curLocation.getLocationInfo()); |
152 | final CtfLocationData currentTime = ((CtfLocationData)context.getLocation().getLocationInfo()); | |
53b235e1 MK |
153 | final long startTime = getIterator(this, context).getStartTime(); |
154 | final long endTime = getIterator(this, context).getEndTime(); | |
132a02b0 | 155 | return ((double) currentTime.getTimestamp() - startTime) |
53b235e1 | 156 | / (endTime - startTime); |
a3fc8213 AM |
157 | } |
158 | ||
b1baa808 MK |
159 | /** |
160 | * Method seekEvent. | |
161 | * @param location ITmfLocation<?> | |
162 | * @return ITmfContext | |
b1baa808 | 163 | */ |
a3fc8213 | 164 | @Override |
76643eb7 | 165 | public synchronized ITmfContext seekEvent(final ITmfLocation location) { |
ce2388e0 | 166 | CtfLocation currentLocation = (CtfLocation) location; |
53b235e1 | 167 | CtfTmfLightweightContext context = new CtfTmfLightweightContext(this); |
76643eb7 BH |
168 | if (fTrace == null) { |
169 | context.setLocation(null); | |
170 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
171 | return context; | |
172 | } | |
4a110860 AM |
173 | /* |
174 | * The rank is set to 0 if the iterator seeks the beginning. If not, it | |
175 | * will be set to UNKNOWN_RANK, since CTF traces don't support seeking | |
176 | * by rank for now. | |
177 | */ | |
11d6f468 | 178 | if (currentLocation == null) { |
132a02b0 | 179 | currentLocation = new CtfLocation(new CtfLocationData(0L, 0L)); |
4a110860 | 180 | context.setRank(0); |
11d6f468 | 181 | } |
5976d44a | 182 | if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) { |
d62bb185 | 183 | currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L); |
1191a574 | 184 | } |
f474d36b | 185 | context.setLocation(currentLocation); |
7f0bab07 PT |
186 | if (location == null) { |
187 | CtfTmfEvent event = getIterator(this, context).getCurrentEvent(); | |
188 | if (event != null) { | |
d62bb185 | 189 | currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0); |
7f0bab07 PT |
190 | } |
191 | } | |
64c2cb4c | 192 | if(context.getRank() != 0) { |
3bd44ac8 | 193 | context.setRank(ITmfContext.UNKNOWN_RANK); |
64c2cb4c | 194 | } |
f474d36b | 195 | return context; |
a3fc8213 AM |
196 | } |
197 | ||
a3fc8213 | 198 | |
a3fc8213 | 199 | @Override |
76643eb7 | 200 | public synchronized ITmfContext seekEvent(double ratio) { |
53b235e1 | 201 | CtfTmfLightweightContext context = new CtfTmfLightweightContext(this); |
76643eb7 BH |
202 | if (fTrace == null) { |
203 | context.setLocation(null); | |
204 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
205 | return context; | |
206 | } | |
b2dc9e02 MK |
207 | final long end = this.getEndTime().getValue(); |
208 | final long start = this.getStartTime().getValue(); | |
209 | final long diff = end - start; | |
15e89960 | 210 | final long ratioTs = Math.round(diff * ratio) + start; |
b2dc9e02 | 211 | context.seek(ratioTs); |
f474d36b PT |
212 | context.setRank(ITmfContext.UNKNOWN_RANK); |
213 | return context; | |
a3fc8213 AM |
214 | } |
215 | ||
b1baa808 MK |
216 | /** |
217 | * Method readNextEvent. | |
218 | * @param context ITmfContext | |
219 | * @return CtfTmfEvent | |
c32744d6 | 220 | * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext) |
b1baa808 | 221 | */ |
a3fc8213 | 222 | @Override |
4b7c469f | 223 | public synchronized CtfTmfEvent getNext(final ITmfContext context) { |
faa38350 PT |
224 | if (fTrace == null) { |
225 | return null; | |
226 | } | |
f474d36b | 227 | CtfTmfEvent event = null; |
788ddcbc | 228 | if (context instanceof CtfTmfLightweightContext) { |
575beffc | 229 | if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) { |
ae09313d PT |
230 | return null; |
231 | } | |
788ddcbc MK |
232 | CtfTmfLightweightContext ctfContext = (CtfTmfLightweightContext) context; |
233 | event = ctfContext.getCurrentEvent(); | |
4a110860 | 234 | |
324a6a4a BH |
235 | if (event != null) { |
236 | updateAttributes(context, event.getTimestamp()); | |
788ddcbc MK |
237 | ctfContext.advance(); |
238 | ctfContext.increaseRank(); | |
324a6a4a | 239 | } |
f474d36b | 240 | } |
4a110860 | 241 | |
aa572e22 | 242 | return event; |
a3fc8213 AM |
243 | } |
244 | ||
4b7c469f MK |
245 | /** |
246 | * gets the CTFtrace that this is wrapping | |
247 | * @return the CTF trace | |
248 | */ | |
249 | public CTFTrace getCTFTrace() { | |
a3fc8213 AM |
250 | return fTrace; |
251 | } | |
a1a24d68 | 252 | |
8636b448 | 253 | |
4b7c469f MK |
254 | //------------------------------------------- |
255 | // Environment Parameters | |
256 | //------------------------------------------- | |
d26f90fd | 257 | /** |
4b7c469f MK |
258 | * Method getNbEnvVars. |
259 | * | |
260 | * @return int | |
d26f90fd | 261 | */ |
4b7c469f MK |
262 | public int getNbEnvVars() { |
263 | return this.fTrace.getEnvironment().size(); | |
264 | } | |
265 | ||
266 | /** | |
267 | * Method getEnvNames. | |
268 | * | |
269 | * @return String[] | |
270 | */ | |
271 | public String[] getEnvNames() { | |
272 | final String[] s = new String[getNbEnvVars()]; | |
273 | return this.fTrace.getEnvironment().keySet().toArray(s); | |
274 | } | |
275 | ||
276 | /** | |
277 | * Method getEnvValue. | |
278 | * | |
279 | * @param key | |
280 | * String | |
281 | * @return String | |
282 | */ | |
283 | public String getEnvValue(final String key) { | |
284 | return this.fTrace.getEnvironment().get(key); | |
285 | } | |
286 | ||
bfe038ff MK |
287 | //------------------------------------------- |
288 | // Clocks | |
289 | //------------------------------------------- | |
290 | ||
9ac2eb62 MK |
291 | /** |
292 | * gets the clock offset | |
293 | * @return the clock offset in ns | |
294 | */ | |
bfe038ff MK |
295 | public long getOffset(){ |
296 | if( fTrace != null ) { | |
297 | return fTrace.getOffset(); | |
298 | } | |
299 | return 0; | |
300 | } | |
301 | ||
4b7c469f MK |
302 | //------------------------------------------- |
303 | // Parser | |
304 | //------------------------------------------- | |
305 | ||
306 | @Override | |
bfe038ff | 307 | public CtfTmfEvent parseEvent(ITmfContext context) { |
4b7c469f | 308 | CtfTmfEvent event = null; |
788ddcbc MK |
309 | if( context instanceof CtfTmfLightweightContext ){ |
310 | CtfTmfLightweightContext itt = (CtfTmfLightweightContext) context.clone(); | |
4b7c469f MK |
311 | event = itt.getCurrentEvent(); |
312 | } | |
313 | return event; | |
11d6f468 | 314 | } |
64c2cb4c | 315 | |
324a6a4a | 316 | /** |
64c2cb4c | 317 | * Sets the cache size for a CtfTmfTrace. |
324a6a4a BH |
318 | */ |
319 | protected void setCacheSize() { | |
320 | setCacheSize(DEFAULT_CACHE_SIZE); | |
321 | } | |
ce2388e0 | 322 | |
53b235e1 MK |
323 | //------------------------------------------- |
324 | // Helpers | |
325 | //------------------------------------------- | |
326 | ||
327 | private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfLightweightContext context) { | |
328 | return CtfIteratorManager.getIterator(trace, context); | |
329 | } | |
a3fc8213 | 330 | } |