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