Commit | Line | Data |
---|---|---|
b1baa808 | 1 | /******************************************************************************* |
da707390 | 2 | * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal |
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 | |
e73a4ba5 | 12 | * Geneviève Bastien - Added the createTimestamp function |
b1baa808 MK |
13 | *******************************************************************************/ |
14 | ||
9722e5d7 | 15 | package org.eclipse.tracecompass.tmf.ctf.core.trace; |
a3fc8213 | 16 | |
5db5a3a4 AM |
17 | import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull; |
18 | ||
96c0f2af | 19 | import java.nio.BufferOverflowException; |
032ecd45 | 20 | import java.nio.ByteBuffer; |
409bea20 | 21 | import java.util.ArrayList; |
f9ff7d40 | 22 | import java.util.Collection; |
e600c338 | 23 | import java.util.Collections; |
2db2b43a | 24 | import java.util.HashMap; |
409bea20 | 25 | import java.util.List; |
299e494e | 26 | import java.util.Map; |
409bea20 | 27 | import java.util.Set; |
299e494e | 28 | |
a3fc8213 AM |
29 | import org.eclipse.core.resources.IProject; |
30 | import org.eclipse.core.resources.IResource; | |
6fd3c6e9 | 31 | import org.eclipse.core.runtime.CoreException; |
a94410d9 MK |
32 | import org.eclipse.core.runtime.IStatus; |
33 | import org.eclipse.core.runtime.Status; | |
8192209b | 34 | import org.eclipse.jdt.annotation.NonNull; |
fe71057b | 35 | import org.eclipse.jdt.annotation.Nullable; |
680f9173 | 36 | import org.eclipse.tracecompass.ctf.core.CTFException; |
fe71057b | 37 | import org.eclipse.tracecompass.ctf.core.event.CTFCallsite; |
f357bcd4 AM |
38 | import org.eclipse.tracecompass.ctf.core.event.CTFClock; |
39 | import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration; | |
0c2409c1 | 40 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; |
f357bcd4 AM |
41 | import org.eclipse.tracecompass.ctf.core.trace.CTFTrace; |
42 | import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader; | |
d16bb0dd | 43 | import org.eclipse.tracecompass.ctf.core.trace.Metadata; |
2bdf0193 | 44 | import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator; |
fe71057b AM |
45 | import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator; |
46 | import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager; | |
2bdf0193 AM |
47 | import org.eclipse.tracecompass.tmf.core.event.ITmfEvent; |
48 | import org.eclipse.tracecompass.tmf.core.event.ITmfEventField; | |
2bdf0193 | 49 | import org.eclipse.tracecompass.tmf.core.event.TmfEventField; |
f9ff7d40 | 50 | import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect; |
2bdf0193 AM |
51 | import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException; |
52 | import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp; | |
da707390 | 53 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp; |
2bdf0193 AM |
54 | import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp; |
55 | import org.eclipse.tracecompass.tmf.core.trace.ITmfContext; | |
2bdf0193 AM |
56 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties; |
57 | import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents; | |
58 | import org.eclipse.tracecompass.tmf.core.trace.TmfTrace; | |
d16bb0dd | 59 | import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus; |
2bdf0193 AM |
60 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable; |
61 | import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer; | |
62 | import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer; | |
63 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint; | |
64 | import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint; | |
65 | import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation; | |
9722e5d7 AM |
66 | import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants; |
67 | import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation; | |
68 | import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo; | |
69 | import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext; | |
70 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent; | |
71 | import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType; | |
f9ff7d40 AM |
72 | import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect; |
73 | import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect; | |
fe71057b | 74 | import org.eclipse.tracecompass.tmf.ctf.core.event.lookup.CtfTmfCallsite; |
a3fc8213 | 75 | |
f9ff7d40 | 76 | import com.google.common.collect.ImmutableList; |
409bea20 GB |
77 | import com.google.common.collect.ImmutableSet; |
78 | ||
9ac2eb62 | 79 | /** |
d09f973b FC |
80 | * The CTf trace handler |
81 | * | |
82 | * @version 1.0 | |
83 | * @author Matthew khouzam | |
9ac2eb62 | 84 | */ |
22307af3 | 85 | public class CtfTmfTrace extends TmfTrace |
5733be39 | 86 | implements ITmfTraceProperties, ITmfPersistentlyIndexable, |
409bea20 | 87 | ITmfTraceWithPreDefinedEvents, AutoCloseable { |
a3fc8213 | 88 | |
a94410d9 MK |
89 | // ------------------------------------------- |
90 | // Constants | |
91 | // ------------------------------------------- | |
f9ff7d40 | 92 | |
324a6a4a BH |
93 | /** |
94 | * Default cache size for CTF traces | |
95 | */ | |
96 | protected static final int DEFAULT_CACHE_SIZE = 50000; | |
64c2cb4c | 97 | |
f9ff7d40 AM |
98 | /** |
99 | * Event aspects available for all CTF traces | |
dbc7991d | 100 | * @since 1.0 |
f9ff7d40 | 101 | */ |
312f397a | 102 | protected static final @NonNull Collection<ITmfEventAspect> CTF_ASPECTS = |
5db5a3a4 | 103 | checkNotNull(ImmutableList.of( |
f9ff7d40 AM |
104 | ITmfEventAspect.BaseAspects.TIMESTAMP, |
105 | new CtfChannelAspect(), | |
106 | new CtfCpuAspect(), | |
107 | ITmfEventAspect.BaseAspects.EVENT_TYPE, | |
108 | ITmfEventAspect.BaseAspects.CONTENTS | |
5db5a3a4 | 109 | )); |
f9ff7d40 AM |
110 | |
111 | /** | |
bb52f9bc GB |
112 | * The Ctf clock unique identifier field |
113 | */ | |
114 | private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$ | |
cd43d683 | 115 | private static final int CONFIDENCE = 10; |
d16bb0dd | 116 | private static final int MIN_CONFIDENCE = 1; |
bb52f9bc | 117 | |
a94410d9 MK |
118 | // ------------------------------------------- |
119 | // Fields | |
120 | // ------------------------------------------- | |
a3fc8213 | 121 | |
e600c338 AM |
122 | private final Map<String, CtfTmfEventType> fContainedEventTypes = |
123 | Collections.synchronizedMap(new HashMap<String, CtfTmfEventType>()); | |
124 | ||
6a0ec7bc AM |
125 | private final CtfIteratorManager fIteratorManager = |
126 | new CtfIteratorManager(this); | |
127 | ||
4b7c469f MK |
128 | /* Reference to the CTF Trace */ |
129 | private CTFTrace fTrace; | |
a3fc8213 | 130 | |
a94410d9 MK |
131 | // ------------------------------------------- |
132 | // TmfTrace Overrides | |
133 | // ------------------------------------------- | |
b1baa808 MK |
134 | /** |
135 | * Method initTrace. | |
063f0d27 AM |
136 | * |
137 | * @param resource | |
138 | * The resource associated with this trace | |
139 | * @param path | |
140 | * The path to the trace file | |
141 | * @param eventType | |
142 | * The type of events that will be read from this trace | |
b1baa808 | 143 | * @throws TmfTraceException |
07804639 | 144 | * If something went wrong while reading the trace |
b1baa808 | 145 | */ |
a3fc8213 | 146 | @Override |
6256d8ad | 147 | public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType) |
b4f71e4a | 148 | throws TmfTraceException { |
4a110860 AM |
149 | /* |
150 | * Set the cache size. This has to be done before the call to super() | |
151 | * because the super needs to know the cache size. | |
152 | */ | |
153 | setCacheSize(); | |
324a6a4a | 154 | |
32c16b50 GB |
155 | super.initTrace(resource, path, eventType); |
156 | ||
a3fc8213 AM |
157 | try { |
158 | this.fTrace = new CTFTrace(path); | |
81a2d02e | 159 | CtfTmfContext ctx; |
99b483fe | 160 | /* Set the start and (current) end times for this trace */ |
81a2d02e | 161 | ctx = (CtfTmfContext) seekEvent(0L); |
132a02b0 | 162 | CtfTmfEvent event = getNext(ctx); |
a94410d9 | 163 | if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) { |
99b483fe AM |
164 | /* Handle the case where the trace is empty */ |
165 | this.setStartTime(TmfTimestamp.BIG_BANG); | |
166 | } else { | |
132a02b0 | 167 | final ITmfTimestamp curTime = event.getTimestamp(); |
21fb02fa MK |
168 | this.setStartTime(curTime); |
169 | this.setEndTime(curTime); | |
99b483fe | 170 | } |
409bea20 GB |
171 | /* |
172 | * Register every event type. When you call getType, it will | |
173 | * register a trace to that type in the TmfEventTypeManager | |
174 | */ | |
6a0ec7bc | 175 | try (CtfIterator iter = fIteratorManager.getIterator(ctx)) { |
409bea20 | 176 | for (IEventDeclaration ied : iter.getEventDeclarations()) { |
e600c338 | 177 | CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName()); |
409bea20 GB |
178 | if (ctfTmfEventType == null) { |
179 | List<ITmfEventField> content = new ArrayList<>(); | |
180 | /* Should only return null the first time */ | |
0c2409c1 MK |
181 | final StructDeclaration fields = ied.getFields(); |
182 | if (fields != null) { | |
183 | for (String fieldName : fields.getFieldsList()) { | |
184 | content.add(new TmfEventField(checkNotNull(fieldName), null, null)); | |
185 | } | |
186 | ITmfEventField contentTree = new TmfEventField( | |
187 | ITmfEventField.ROOT_FIELD_ID, | |
188 | null, | |
189 | content.toArray(new ITmfEventField[content.size()])); | |
190 | ||
191 | ctfTmfEventType = new CtfTmfEventType(ied.getName(), contentTree); | |
192 | fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType); | |
409bea20 | 193 | } |
409bea20 GB |
194 | } |
195 | } | |
196 | } | |
dd5615b7 | 197 | ctx.dispose(); |
680f9173 | 198 | } catch (final CTFException e) { |
a3fc8213 AM |
199 | /* |
200 | * If it failed at the init(), we can assume it's because the file | |
201 | * was not found or was not recognized as a CTF trace. Throw into | |
202 | * the new type of exception expected by the rest of TMF. | |
203 | */ | |
9fa32496 | 204 | throw new TmfTraceException(e.getMessage(), e); |
a3fc8213 | 205 | } |
a3fc8213 AM |
206 | } |
207 | ||
090c006e AM |
208 | @Override |
209 | public void close() { | |
210 | dispose(); | |
211 | } | |
212 | ||
53b235e1 MK |
213 | @Override |
214 | public synchronized void dispose() { | |
6a0ec7bc | 215 | fIteratorManager.dispose(); |
5d1c6919 | 216 | if (fTrace != null) { |
5d1c6919 PT |
217 | fTrace = null; |
218 | } | |
53b235e1 MK |
219 | super.dispose(); |
220 | } | |
221 | ||
b1baa808 | 222 | /** |
cd43d683 PT |
223 | * {@inheritDoc} |
224 | * <p> | |
d16bb0dd BH |
225 | * The default implementation of a CTF trace. |
226 | * | |
227 | * Firstly a weak validation of the metadata is done to determine if the | |
228 | * path is actually for a CTF trace. After that a full validation is done. | |
229 | * | |
230 | * If the weak and full validation are successful the confidence is set | |
231 | * to 10. | |
232 | * | |
233 | * If the weak validation was successful, but the full validation fails | |
234 | * a TraceValidationStatus with severity warning and confidence of 1 is | |
235 | * returned. | |
236 | * | |
237 | * If both weak and full validation fails an error status is returned. | |
b1baa808 | 238 | */ |
a3fc8213 | 239 | @Override |
a94410d9 | 240 | public IStatus validate(final IProject project, final String path) { |
f841e903 | 241 | boolean isMetadataFile = false; |
b562a24f | 242 | try { |
f841e903 | 243 | isMetadataFile = Metadata.preValidate(path); |
d16bb0dd BH |
244 | } catch (final CTFException e) { |
245 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$ | |
246 | } | |
247 | ||
f841e903 | 248 | if (isMetadataFile) { |
d16bb0dd BH |
249 | // Trace is pre-validated, continue will full validation |
250 | try { | |
251 | final CTFTrace trace = new CTFTrace(path); | |
252 | if (!trace.majorIsSet()) { | |
f841e903 | 253 | if (isMetadataFile) { |
d16bb0dd BH |
254 | return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null); |
255 | } | |
256 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet); | |
b5354daa | 257 | } |
d16bb0dd BH |
258 | |
259 | // Validate using reader initialization | |
260 | try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {} | |
261 | ||
262 | // Trace is validated, return with confidence | |
263 | return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment()); | |
264 | ||
f841e903 BH |
265 | } catch (final CTFException | BufferOverflowException e ) { |
266 | // return warning since it's a CTF trace but with errors in it | |
267 | return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$ | |
a94410d9 | 268 | } |
a3fc8213 | 269 | } |
d16bb0dd | 270 | return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError); |
a3fc8213 AM |
271 | } |
272 | ||
f9ff7d40 AM |
273 | @Override |
274 | public Iterable<ITmfEventAspect> getEventAspects() { | |
275 | return CTF_ASPECTS; | |
276 | } | |
277 | ||
b1baa808 | 278 | /** |
f474d36b | 279 | * Method getCurrentLocation. This is not applicable in CTF |
a94410d9 | 280 | * |
f474d36b | 281 | * @return null, since the trace has no knowledge of the current location |
2bdf0193 | 282 | * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation() |
b1baa808 | 283 | */ |
a3fc8213 | 284 | @Override |
1e1bef82 | 285 | public ITmfLocation getCurrentLocation() { |
f474d36b | 286 | return null; |
a3fc8213 AM |
287 | } |
288 | ||
a3fc8213 | 289 | @Override |
1e1bef82 | 290 | public double getLocationRatio(ITmfLocation location) { |
4b7c469f | 291 | final CtfLocation curLocation = (CtfLocation) location; |
4a1f6c41 PT |
292 | final long startTime = getStartTime().getValue(); |
293 | final double diff = curLocation.getLocationInfo().getTimestamp() - startTime; | |
294 | final double total = getEndTime().getValue() - startTime; | |
295 | return Math.max(0.0, Math.min(1.0, diff / total)); | |
a3fc8213 AM |
296 | } |
297 | ||
b1baa808 MK |
298 | /** |
299 | * Method seekEvent. | |
a94410d9 MK |
300 | * |
301 | * @param location | |
302 | * ITmfLocation<?> | |
b1baa808 | 303 | * @return ITmfContext |
b1baa808 | 304 | */ |
a3fc8213 | 305 | @Override |
76643eb7 | 306 | public synchronized ITmfContext seekEvent(final ITmfLocation location) { |
ce2388e0 | 307 | CtfLocation currentLocation = (CtfLocation) location; |
81a2d02e | 308 | CtfTmfContext context = new CtfTmfContext(this); |
76643eb7 BH |
309 | if (fTrace == null) { |
310 | context.setLocation(null); | |
311 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
312 | return context; | |
313 | } | |
4a110860 AM |
314 | /* |
315 | * The rank is set to 0 if the iterator seeks the beginning. If not, it | |
316 | * will be set to UNKNOWN_RANK, since CTF traces don't support seeking | |
317 | * by rank for now. | |
318 | */ | |
11d6f468 | 319 | if (currentLocation == null) { |
f5df94f8 | 320 | currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L)); |
4a110860 | 321 | context.setRank(0); |
544c79ad | 322 | } else { |
3bd44ac8 | 323 | context.setRank(ITmfContext.UNKNOWN_RANK); |
544c79ad PT |
324 | if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) { |
325 | currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L); | |
326 | } | |
64c2cb4c | 327 | } |
544c79ad PT |
328 | /* This will seek and update the location after the seek */ |
329 | context.setLocation(currentLocation); | |
f474d36b | 330 | return context; |
a3fc8213 AM |
331 | } |
332 | ||
a3fc8213 | 333 | @Override |
76643eb7 | 334 | public synchronized ITmfContext seekEvent(double ratio) { |
81a2d02e | 335 | CtfTmfContext context = new CtfTmfContext(this); |
76643eb7 BH |
336 | if (fTrace == null) { |
337 | context.setLocation(null); | |
338 | context.setRank(ITmfContext.UNKNOWN_RANK); | |
339 | return context; | |
340 | } | |
4a1f6c41 PT |
341 | final long end = getEndTime().getValue(); |
342 | final long start = getStartTime().getValue(); | |
b2dc9e02 | 343 | final long diff = end - start; |
15e89960 | 344 | final long ratioTs = Math.round(diff * ratio) + start; |
b2dc9e02 | 345 | context.seek(ratioTs); |
f474d36b PT |
346 | context.setRank(ITmfContext.UNKNOWN_RANK); |
347 | return context; | |
a3fc8213 AM |
348 | } |
349 | ||
b1baa808 MK |
350 | /** |
351 | * Method readNextEvent. | |
a94410d9 MK |
352 | * |
353 | * @param context | |
354 | * ITmfContext | |
b1baa808 | 355 | * @return CtfTmfEvent |
2bdf0193 | 356 | * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext) |
b1baa808 | 357 | */ |
a3fc8213 | 358 | @Override |
4b7c469f | 359 | public synchronized CtfTmfEvent getNext(final ITmfContext context) { |
faa38350 PT |
360 | if (fTrace == null) { |
361 | return null; | |
362 | } | |
f474d36b | 363 | CtfTmfEvent event = null; |
81a2d02e | 364 | if (context instanceof CtfTmfContext) { |
575beffc | 365 | if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) { |
ae09313d PT |
366 | return null; |
367 | } | |
81a2d02e | 368 | CtfTmfContext ctfContext = (CtfTmfContext) context; |
788ddcbc | 369 | event = ctfContext.getCurrentEvent(); |
4a110860 | 370 | |
324a6a4a | 371 | if (event != null) { |
5904c11e | 372 | updateAttributes(context, event); |
788ddcbc MK |
373 | ctfContext.advance(); |
374 | ctfContext.increaseRank(); | |
324a6a4a | 375 | } |
f474d36b | 376 | } |
4a110860 | 377 | |
aa572e22 | 378 | return event; |
a3fc8213 AM |
379 | } |
380 | ||
bb52f9bc GB |
381 | /** |
382 | * Ctf traces have a clock with a unique uuid that will be used to identify | |
383 | * the host. Traces with the same clock uuid will be known to have been made | |
384 | * on the same machine. | |
385 | * | |
386 | * Note: uuid is an optional field, it may not be there for a clock. | |
387 | */ | |
388 | @Override | |
389 | public String getHostId() { | |
fe71057b | 390 | CTFClock clock = fTrace.getClock(); |
bb52f9bc GB |
391 | if (clock != null) { |
392 | String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY); | |
393 | if (clockHost != null) { | |
394 | return clockHost; | |
395 | } | |
396 | } | |
397 | return super.getHostId(); | |
398 | } | |
399 | ||
fe71057b AM |
400 | /** |
401 | * Get the first callsite that matches the event name | |
402 | * | |
403 | * @param eventName The event name to look for | |
404 | * @return The best callsite candidate | |
405 | */ | |
406 | public @Nullable CtfTmfCallsite getCallsite(String eventName) { | |
407 | CTFCallsite callsite = fTrace.getCallsite(eventName); | |
408 | if (callsite != null) { | |
409 | return new CtfTmfCallsite(callsite); | |
410 | } | |
411 | return null; | |
412 | } | |
413 | ||
414 | /** | |
415 | * Get the closest matching callsite for given event name and instruction | |
416 | * pointer | |
417 | * | |
418 | * @param eventName | |
419 | * The event name | |
420 | * @param ip | |
421 | * The instruction pointer | |
422 | * @return The closest matching callsite | |
423 | */ | |
424 | public @Nullable CtfTmfCallsite getCallsite(String eventName, long ip) { | |
425 | CTFCallsite calliste = fTrace.getCallsite(eventName, ip); | |
426 | if (calliste != null) { | |
427 | return new CtfTmfCallsite(calliste); | |
428 | } | |
429 | return null; | |
430 | } | |
431 | ||
432 | /** | |
433 | * Get the CTF environment variables defined in this CTF trace, in <name, | |
434 | * value> form. This comes from the trace's CTF metadata. | |
435 | * | |
436 | * @return The CTF environment | |
437 | */ | |
438 | public Map<String, String> getEnvironment() { | |
439 | return fTrace.getEnvironment(); | |
440 | } | |
441 | ||
a94410d9 | 442 | // ------------------------------------------- |
22307af3 | 443 | // ITmfTraceProperties |
a94410d9 | 444 | // ------------------------------------------- |
4b7c469f | 445 | |
22307af3 AM |
446 | @Override |
447 | public Map<String, String> getTraceProperties() { | |
2db2b43a GB |
448 | Map<String, String> properties = new HashMap<>(); |
449 | properties.putAll(fTrace.getEnvironment()); | |
450 | properties.put(Messages.CtfTmfTrace_HostID, getHostId()); | |
451 | return properties; | |
4b7c469f MK |
452 | } |
453 | ||
a94410d9 MK |
454 | // ------------------------------------------- |
455 | // Clocks | |
456 | // ------------------------------------------- | |
bfe038ff | 457 | |
9ac2eb62 MK |
458 | /** |
459 | * gets the clock offset | |
a94410d9 | 460 | * |
9ac2eb62 MK |
461 | * @return the clock offset in ns |
462 | */ | |
a94410d9 MK |
463 | public long getOffset() { |
464 | if (fTrace != null) { | |
bfe038ff MK |
465 | return fTrace.getOffset(); |
466 | } | |
467 | return 0; | |
468 | } | |
469 | ||
fe71057b AM |
470 | /** |
471 | * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds | |
472 | * for this trace. | |
473 | * | |
474 | * @param cycles | |
475 | * The timestamp in cycles | |
476 | * @return The timestamp in nanoseconds | |
477 | */ | |
478 | public long timestampCyclesToNanos(long cycles) { | |
479 | return fTrace.timestampCyclesToNanos(cycles); | |
480 | } | |
481 | ||
482 | /** | |
483 | * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles | |
484 | * for this trace. | |
485 | * | |
486 | * @param nanos | |
487 | * The timestamp in nanoseconds | |
488 | * @return The timestamp in cycles | |
489 | */ | |
490 | public long timestampNanoToCycles(long nanos) { | |
491 | return fTrace.timestampNanoToCycles(nanos); | |
492 | } | |
493 | ||
3480bf12 | 494 | /** |
409bea20 | 495 | * Gets the list of declared events |
3480bf12 | 496 | */ |
409bea20 | 497 | @Override |
e600c338 AM |
498 | public Set<CtfTmfEventType> getContainedEventTypes() { |
499 | return ImmutableSet.copyOf(fContainedEventTypes.values()); | |
500 | } | |
501 | ||
502 | /** | |
503 | * Register an event type to this trace. | |
504 | * | |
9722e5d7 | 505 | * Public visibility so that {@link CtfTmfEvent#getType} can call it. |
e600c338 AM |
506 | * |
507 | * FIXME This could probably be made cleaner? | |
9722e5d7 AM |
508 | * |
509 | * @param eventType | |
510 | * The event type to register | |
e600c338 | 511 | */ |
9722e5d7 | 512 | public void registerEventType(CtfTmfEventType eventType) { |
e600c338 | 513 | fContainedEventTypes.put(eventType.getName(), eventType); |
3480bf12 GB |
514 | } |
515 | ||
a94410d9 MK |
516 | // ------------------------------------------- |
517 | // Parser | |
518 | // ------------------------------------------- | |
4b7c469f MK |
519 | |
520 | @Override | |
bfe038ff | 521 | public CtfTmfEvent parseEvent(ITmfContext context) { |
4b7c469f | 522 | CtfTmfEvent event = null; |
ea271da6 PT |
523 | if (context instanceof CtfTmfContext) { |
524 | final ITmfContext tmpContext = seekEvent(context.getLocation()); | |
525 | event = getNext(tmpContext); | |
4b7c469f MK |
526 | } |
527 | return event; | |
11d6f468 | 528 | } |
64c2cb4c | 529 | |
324a6a4a | 530 | /** |
64c2cb4c | 531 | * Sets the cache size for a CtfTmfTrace. |
324a6a4a BH |
532 | */ |
533 | protected void setCacheSize() { | |
534 | setCacheSize(DEFAULT_CACHE_SIZE); | |
535 | } | |
ce2388e0 | 536 | |
a94410d9 | 537 | // ------------------------------------------- |
fe71057b | 538 | // CtfIterator factory methods |
a94410d9 | 539 | // ------------------------------------------- |
53b235e1 | 540 | |
36dd544c MK |
541 | /** |
542 | * Get an iterator to the trace | |
543 | * | |
544 | * @return an iterator to the trace | |
545 | */ | |
fe71057b AM |
546 | public ITmfContext createIterator() { |
547 | try { | |
548 | return new CtfIterator(fTrace, this); | |
680f9173 | 549 | } catch (CTFException e) { |
fe71057b AM |
550 | Activator.getDefault().logError(e.getMessage(), e); |
551 | } | |
552 | return null; | |
553 | } | |
554 | ||
555 | /** | |
556 | * Get an iterator to the trace, , which will initially point to the given | |
557 | * location/rank. | |
558 | * | |
559 | * @param ctfLocationData | |
560 | * The initial timestamp the iterator will be pointing to | |
561 | * @param rank | |
562 | * The initial rank | |
563 | * @return The new iterator | |
564 | */ | |
565 | public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) { | |
db8e8f7d | 566 | try { |
fe71057b | 567 | return new CtfIterator(fTrace, this, ctfLocationData, rank); |
680f9173 | 568 | } catch (CTFException e) { |
91e7f946 | 569 | Activator.getDefault().logError(e.getMessage(), e); |
db8e8f7d AM |
570 | } |
571 | return null; | |
36dd544c | 572 | } |
e73a4ba5 | 573 | |
0451d4d7 AM |
574 | /** |
575 | * Create the 'CtfIterator' object from a CtfTmfContext. | |
576 | * | |
577 | * @param context | |
578 | * The iterator will initially be pointing to this context | |
579 | * @return A new CtfIterator object | |
580 | * @since 1.0 | |
581 | */ | |
582 | public ITmfContext createIteratorFromContext(CtfTmfContext context) { | |
583 | return fIteratorManager.getIterator(context); | |
584 | } | |
585 | ||
586 | /** | |
587 | * Dispose an iterator that was create with | |
588 | * {@link #createIteratorFromContext} | |
589 | * | |
590 | * @param context | |
591 | * The last context that was pointed to by the iterator (this is | |
592 | * the 'key' to find the correct iterator to dispose). | |
593 | * @since 1.0 | |
594 | */ | |
595 | public void disposeContext(CtfTmfContext context) { | |
596 | fIteratorManager.removeIterator(context); | |
597 | } | |
598 | ||
e73a4ba5 GB |
599 | // ------------------------------------------------------------------------ |
600 | // Timestamp transformation functions | |
601 | // ------------------------------------------------------------------------ | |
602 | ||
da707390 MK |
603 | /** |
604 | * @since 1.0 | |
605 | */ | |
e73a4ba5 | 606 | @Override |
da707390 MK |
607 | public @NonNull TmfNanoTimestamp createTimestamp(long ts) { |
608 | return new TmfNanoTimestamp(getTimestampTransform().transform(ts)); | |
e73a4ba5 | 609 | } |
032ecd45 MAL |
610 | |
611 | private static int fCheckpointSize = -1; | |
612 | ||
613 | @Override | |
614 | public synchronized int getCheckpointSize() { | |
615 | if (fCheckpointSize == -1) { | |
da707390 | 616 | TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0); |
032ecd45 MAL |
617 | ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE); |
618 | b.clear(); | |
619 | c.serialize(b); | |
620 | fCheckpointSize = b.position(); | |
621 | } | |
622 | ||
623 | return fCheckpointSize; | |
624 | } | |
625 | ||
626 | @Override | |
627 | protected ITmfTraceIndexer createIndexer(int interval) { | |
628 | return new TmfBTreeTraceIndexer(this, interval); | |
629 | } | |
630 | ||
631 | @Override | |
632 | public ITmfLocation restoreLocation(ByteBuffer bufferIn) { | |
633 | return new CtfLocation(bufferIn); | |
634 | } | |
6fd3c6e9 MAL |
635 | |
636 | @Override | |
637 | public boolean isComplete() { | |
638 | if (getResource() == null) { | |
639 | return true; | |
640 | } | |
641 | ||
642 | String host = null; | |
643 | String port = null; | |
644 | String sessionName = null; | |
645 | try { | |
646 | host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST); | |
647 | port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT); | |
648 | sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME); | |
649 | } catch (CoreException e) { | |
650 | Activator.getDefault().logError(e.getMessage(), e); | |
b562a24f MK |
651 | // Something happened to the resource, assume we won't get any more |
652 | // data from it | |
6fd3c6e9 MAL |
653 | return true; |
654 | } | |
655 | return host == null || port == null || sessionName == null; | |
656 | } | |
657 | ||
658 | @Override | |
659 | public void setComplete(final boolean isComplete) { | |
660 | super.setComplete(isComplete); | |
661 | try { | |
662 | if (isComplete) { | |
663 | getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null); | |
664 | getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null); | |
665 | getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null); | |
666 | } | |
667 | } catch (CoreException e) { | |
668 | Activator.getDefault().logError(e.getMessage(), e); | |
669 | } | |
670 | } | |
a3fc8213 | 671 | } |