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