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