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