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