ctf: Fix leaking context in CtfTmfTrace.initTrace()
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / trace / CtfTmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
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 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
12 * Geneviève Bastien - Added the createTimestamp function
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.tmf.ctf.core.trace;
16
17 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
19 import java.nio.BufferOverflowException;
20 import java.nio.ByteBuffer;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Collections;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.eclipse.core.resources.IProject;
30 import org.eclipse.core.resources.IResource;
31 import org.eclipse.core.runtime.CoreException;
32 import org.eclipse.core.runtime.IStatus;
33 import org.eclipse.core.runtime.Status;
34 import org.eclipse.jdt.annotation.NonNull;
35 import org.eclipse.jdt.annotation.Nullable;
36 import org.eclipse.tracecompass.ctf.core.CTFException;
37 import org.eclipse.tracecompass.ctf.core.event.CTFCallsite;
38 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
39 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
40 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
41 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
42 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
43 import org.eclipse.tracecompass.ctf.core.trace.Metadata;
44 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
45 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
46 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager;
47 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
48 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
49 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
50 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
51 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
52 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
53 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
54 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
55 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
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;
59 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
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;
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;
72 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
73 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
74 import org.eclipse.tracecompass.tmf.ctf.core.event.lookup.CtfTmfCallsite;
75
76 import com.google.common.collect.ImmutableList;
77 import com.google.common.collect.ImmutableSet;
78
79 /**
80 * The CTf trace handler
81 *
82 * @version 1.0
83 * @author Matthew khouzam
84 */
85 public class CtfTmfTrace extends TmfTrace
86 implements ITmfTraceProperties, ITmfPersistentlyIndexable,
87 ITmfTraceWithPreDefinedEvents, AutoCloseable {
88
89 // -------------------------------------------
90 // Constants
91 // -------------------------------------------
92
93 /**
94 * Default cache size for CTF traces
95 */
96 protected static final int DEFAULT_CACHE_SIZE = 50000;
97
98 /**
99 * Event aspects available for all CTF traces
100 * @since 1.0
101 */
102 protected static final @NonNull Collection<ITmfEventAspect> CTF_ASPECTS =
103 checkNotNull(ImmutableList.of(
104 ITmfEventAspect.BaseAspects.TIMESTAMP,
105 new CtfChannelAspect(),
106 new CtfCpuAspect(),
107 ITmfEventAspect.BaseAspects.EVENT_TYPE,
108 ITmfEventAspect.BaseAspects.CONTENTS
109 ));
110
111 /**
112 * The Ctf clock unique identifier field
113 */
114 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
115 private static final int CONFIDENCE = 10;
116 private static final int MIN_CONFIDENCE = 1;
117
118 // -------------------------------------------
119 // Fields
120 // -------------------------------------------
121
122 private final Map<String, CtfTmfEventType> fContainedEventTypes =
123 Collections.synchronizedMap(new HashMap<String, CtfTmfEventType>());
124
125 private final CtfIteratorManager fIteratorManager =
126 new CtfIteratorManager(this);
127
128 /* Reference to the CTF Trace */
129 private CTFTrace fTrace;
130
131 // -------------------------------------------
132 // TmfTrace Overrides
133 // -------------------------------------------
134 /**
135 * Method initTrace.
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
143 * @throws TmfTraceException
144 * If something went wrong while reading the trace
145 */
146 @Override
147 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
148 throws TmfTraceException {
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();
154
155 super.initTrace(resource, path, eventType);
156
157 try {
158 this.fTrace = new CTFTrace(path);
159 CtfTmfContext ctx;
160 /* Set the start and (current) end times for this trace */
161 ctx = (CtfTmfContext) seekEvent(0L);
162 CtfTmfEvent event = getNext(ctx);
163 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
164 /* Handle the case where the trace is empty */
165 this.setStartTime(TmfTimestamp.BIG_BANG);
166 } else {
167 final ITmfTimestamp curTime = event.getTimestamp();
168 this.setStartTime(curTime);
169 this.setEndTime(curTime);
170 }
171 /*
172 * Register every event type. When you call getType, it will
173 * register a trace to that type in the TmfEventTypeManager
174 */
175 try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
176 for (IEventDeclaration ied : iter.getEventDeclarations()) {
177 CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
178 if (ctfTmfEventType == null) {
179 List<ITmfEventField> content = new ArrayList<>();
180 /* Should only return null the first time */
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);
193 }
194 }
195 }
196 }
197 ctx.dispose();
198 } catch (final CTFException e) {
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 */
204 throw new TmfTraceException(e.getMessage(), e);
205 }
206 }
207
208 @Override
209 public void close() {
210 dispose();
211 }
212
213 @Override
214 public synchronized void dispose() {
215 fIteratorManager.dispose();
216 if (fTrace != null) {
217 fTrace = null;
218 }
219 super.dispose();
220 }
221
222 /**
223 * {@inheritDoc}
224 * <p>
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.
238 */
239 @Override
240 public IStatus validate(final IProject project, final String path) {
241 boolean isMetadataFile = false;
242 try {
243 isMetadataFile = Metadata.preValidate(path);
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
248 if (isMetadataFile) {
249 // Trace is pre-validated, continue will full validation
250 try {
251 final CTFTrace trace = new CTFTrace(path);
252 if (!trace.majorIsSet()) {
253 if (isMetadataFile) {
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);
257 }
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
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$
268 }
269 }
270 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
271 }
272
273 @Override
274 public Iterable<ITmfEventAspect> getEventAspects() {
275 return CTF_ASPECTS;
276 }
277
278 /**
279 * Method getCurrentLocation. This is not applicable in CTF
280 *
281 * @return null, since the trace has no knowledge of the current location
282 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
283 */
284 @Override
285 public ITmfLocation getCurrentLocation() {
286 return null;
287 }
288
289 @Override
290 public double getLocationRatio(ITmfLocation location) {
291 final CtfLocation curLocation = (CtfLocation) location;
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));
296 }
297
298 /**
299 * Method seekEvent.
300 *
301 * @param location
302 * ITmfLocation<?>
303 * @return ITmfContext
304 */
305 @Override
306 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
307 CtfLocation currentLocation = (CtfLocation) location;
308 CtfTmfContext context = new CtfTmfContext(this);
309 if (fTrace == null) {
310 context.setLocation(null);
311 context.setRank(ITmfContext.UNKNOWN_RANK);
312 return context;
313 }
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 */
319 if (currentLocation == null) {
320 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
321 context.setRank(0);
322 } else {
323 context.setRank(ITmfContext.UNKNOWN_RANK);
324 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
325 currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
326 }
327 }
328 /* This will seek and update the location after the seek */
329 context.setLocation(currentLocation);
330 return context;
331 }
332
333 @Override
334 public synchronized ITmfContext seekEvent(double ratio) {
335 CtfTmfContext context = new CtfTmfContext(this);
336 if (fTrace == null) {
337 context.setLocation(null);
338 context.setRank(ITmfContext.UNKNOWN_RANK);
339 return context;
340 }
341 final long end = getEndTime().getValue();
342 final long start = getStartTime().getValue();
343 final long diff = end - start;
344 final long ratioTs = Math.round(diff * ratio) + start;
345 context.seek(ratioTs);
346 context.setRank(ITmfContext.UNKNOWN_RANK);
347 return context;
348 }
349
350 /**
351 * Method readNextEvent.
352 *
353 * @param context
354 * ITmfContext
355 * @return CtfTmfEvent
356 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
357 */
358 @Override
359 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
360 if (fTrace == null) {
361 return null;
362 }
363 CtfTmfEvent event = null;
364 if (context instanceof CtfTmfContext) {
365 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
366 return null;
367 }
368 CtfTmfContext ctfContext = (CtfTmfContext) context;
369 event = ctfContext.getCurrentEvent();
370
371 if (event != null) {
372 updateAttributes(context, event);
373 ctfContext.advance();
374 ctfContext.increaseRank();
375 }
376 }
377
378 return event;
379 }
380
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() {
390 CTFClock clock = fTrace.getClock();
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
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
442 // -------------------------------------------
443 // ITmfTraceProperties
444 // -------------------------------------------
445
446 @Override
447 public Map<String, String> getTraceProperties() {
448 Map<String, String> properties = new HashMap<>();
449 properties.putAll(fTrace.getEnvironment());
450 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
451 return properties;
452 }
453
454 // -------------------------------------------
455 // Clocks
456 // -------------------------------------------
457
458 /**
459 * gets the clock offset
460 *
461 * @return the clock offset in ns
462 */
463 public long getOffset() {
464 if (fTrace != null) {
465 return fTrace.getOffset();
466 }
467 return 0;
468 }
469
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
494 /**
495 * Gets the list of declared events
496 */
497 @Override
498 public Set<CtfTmfEventType> getContainedEventTypes() {
499 return ImmutableSet.copyOf(fContainedEventTypes.values());
500 }
501
502 /**
503 * Register an event type to this trace.
504 *
505 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
506 *
507 * FIXME This could probably be made cleaner?
508 *
509 * @param eventType
510 * The event type to register
511 */
512 public void registerEventType(CtfTmfEventType eventType) {
513 fContainedEventTypes.put(eventType.getName(), eventType);
514 }
515
516 // -------------------------------------------
517 // Parser
518 // -------------------------------------------
519
520 @Override
521 public CtfTmfEvent parseEvent(ITmfContext context) {
522 CtfTmfEvent event = null;
523 if (context instanceof CtfTmfContext) {
524 final ITmfContext tmpContext = seekEvent(context.getLocation());
525 event = getNext(tmpContext);
526 }
527 return event;
528 }
529
530 /**
531 * Sets the cache size for a CtfTmfTrace.
532 */
533 protected void setCacheSize() {
534 setCacheSize(DEFAULT_CACHE_SIZE);
535 }
536
537 // -------------------------------------------
538 // CtfIterator factory methods
539 // -------------------------------------------
540
541 /**
542 * Get an iterator to the trace
543 *
544 * @return an iterator to the trace
545 */
546 public ITmfContext createIterator() {
547 try {
548 return new CtfIterator(fTrace, this);
549 } catch (CTFException e) {
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) {
566 try {
567 return new CtfIterator(fTrace, this, ctfLocationData, rank);
568 } catch (CTFException e) {
569 Activator.getDefault().logError(e.getMessage(), e);
570 }
571 return null;
572 }
573
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
599 // ------------------------------------------------------------------------
600 // Timestamp transformation functions
601 // ------------------------------------------------------------------------
602
603 /**
604 * @since 1.0
605 */
606 @Override
607 public @NonNull TmfNanoTimestamp createTimestamp(long ts) {
608 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
609 }
610
611 private static int fCheckpointSize = -1;
612
613 @Override
614 public synchronized int getCheckpointSize() {
615 if (fCheckpointSize == -1) {
616 TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
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 }
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);
651 // Something happened to the resource, assume we won't get any more
652 // data from it
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 }
671 }
This page took 0.048287 seconds and 6 git commands to generate.