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