ctf: Remove AutoCloseable from CtfTmfTrace
[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 {
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 synchronized void dispose() {
210 fIteratorManager.dispose();
211 if (fTrace != null) {
212 fTrace = null;
213 }
214 super.dispose();
215 }
216
217 /**
218 * {@inheritDoc}
219 * <p>
220 * The default implementation of a CTF trace.
221 *
222 * Firstly a weak validation of the metadata is done to determine if the
223 * path is actually for a CTF trace. After that a full validation is done.
224 *
225 * If the weak and full validation are successful the confidence is set
226 * to 10.
227 *
228 * If the weak validation was successful, but the full validation fails
229 * a TraceValidationStatus with severity warning and confidence of 1 is
230 * returned.
231 *
232 * If both weak and full validation fails an error status is returned.
233 */
234 @Override
235 public IStatus validate(final IProject project, final String path) {
236 boolean isMetadataFile = false;
237 try {
238 isMetadataFile = Metadata.preValidate(path);
239 } catch (final CTFException e) {
240 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
241 }
242
243 if (isMetadataFile) {
244 // Trace is pre-validated, continue will full validation
245 try {
246 final CTFTrace trace = new CTFTrace(path);
247 if (!trace.majorIsSet()) {
248 if (isMetadataFile) {
249 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null);
250 }
251 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
252 }
253
254 // Validate using reader initialization
255 try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {}
256
257 // Trace is validated, return with confidence
258 return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
259
260 } catch (final CTFException | BufferOverflowException e ) {
261 // return warning since it's a CTF trace but with errors in it
262 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
263 }
264 }
265 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
266 }
267
268 @Override
269 public Iterable<ITmfEventAspect> getEventAspects() {
270 return CTF_ASPECTS;
271 }
272
273 /**
274 * Method getCurrentLocation. This is not applicable in CTF
275 *
276 * @return null, since the trace has no knowledge of the current location
277 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
278 */
279 @Override
280 public ITmfLocation getCurrentLocation() {
281 return null;
282 }
283
284 @Override
285 public double getLocationRatio(ITmfLocation location) {
286 final CtfLocation curLocation = (CtfLocation) location;
287 final long startTime = getStartTime().getValue();
288 final double diff = curLocation.getLocationInfo().getTimestamp() - startTime;
289 final double total = getEndTime().getValue() - startTime;
290 return Math.max(0.0, Math.min(1.0, diff / total));
291 }
292
293 /**
294 * Method seekEvent.
295 *
296 * @param location
297 * ITmfLocation<?>
298 * @return ITmfContext
299 */
300 @Override
301 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
302 CtfLocation currentLocation = (CtfLocation) location;
303 CtfTmfContext context = new CtfTmfContext(this);
304 if (fTrace == null) {
305 context.setLocation(null);
306 context.setRank(ITmfContext.UNKNOWN_RANK);
307 return context;
308 }
309 /*
310 * The rank is set to 0 if the iterator seeks the beginning. If not, it
311 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
312 * by rank for now.
313 */
314 if (currentLocation == null) {
315 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
316 context.setRank(0);
317 } else {
318 context.setRank(ITmfContext.UNKNOWN_RANK);
319 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
320 currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
321 }
322 }
323 /* This will seek and update the location after the seek */
324 context.setLocation(currentLocation);
325 return context;
326 }
327
328 @Override
329 public synchronized ITmfContext seekEvent(double ratio) {
330 CtfTmfContext context = new CtfTmfContext(this);
331 if (fTrace == null) {
332 context.setLocation(null);
333 context.setRank(ITmfContext.UNKNOWN_RANK);
334 return context;
335 }
336 final long end = getEndTime().getValue();
337 final long start = getStartTime().getValue();
338 final long diff = end - start;
339 final long ratioTs = Math.round(diff * ratio) + start;
340 context.seek(ratioTs);
341 context.setRank(ITmfContext.UNKNOWN_RANK);
342 return context;
343 }
344
345 /**
346 * Method readNextEvent.
347 *
348 * @param context
349 * ITmfContext
350 * @return CtfTmfEvent
351 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
352 */
353 @Override
354 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
355 if (fTrace == null) {
356 return null;
357 }
358 CtfTmfEvent event = null;
359 if (context instanceof CtfTmfContext) {
360 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
361 return null;
362 }
363 CtfTmfContext ctfContext = (CtfTmfContext) context;
364 event = ctfContext.getCurrentEvent();
365
366 if (event != null) {
367 updateAttributes(context, event);
368 ctfContext.advance();
369 ctfContext.increaseRank();
370 }
371 }
372
373 return event;
374 }
375
376 /**
377 * Ctf traces have a clock with a unique uuid that will be used to identify
378 * the host. Traces with the same clock uuid will be known to have been made
379 * on the same machine.
380 *
381 * Note: uuid is an optional field, it may not be there for a clock.
382 */
383 @Override
384 public String getHostId() {
385 CTFClock clock = fTrace.getClock();
386 if (clock != null) {
387 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
388 if (clockHost != null) {
389 return clockHost;
390 }
391 }
392 return super.getHostId();
393 }
394
395 /**
396 * Get the first callsite that matches the event name
397 *
398 * @param eventName The event name to look for
399 * @return The best callsite candidate
400 */
401 public @Nullable CtfTmfCallsite getCallsite(String eventName) {
402 CTFCallsite callsite = fTrace.getCallsite(eventName);
403 if (callsite != null) {
404 return new CtfTmfCallsite(callsite);
405 }
406 return null;
407 }
408
409 /**
410 * Get the closest matching callsite for given event name and instruction
411 * pointer
412 *
413 * @param eventName
414 * The event name
415 * @param ip
416 * The instruction pointer
417 * @return The closest matching callsite
418 */
419 public @Nullable CtfTmfCallsite getCallsite(String eventName, long ip) {
420 CTFCallsite calliste = fTrace.getCallsite(eventName, ip);
421 if (calliste != null) {
422 return new CtfTmfCallsite(calliste);
423 }
424 return null;
425 }
426
427 /**
428 * Get the CTF environment variables defined in this CTF trace, in <name,
429 * value> form. This comes from the trace's CTF metadata.
430 *
431 * @return The CTF environment
432 */
433 public Map<String, String> getEnvironment() {
434 return fTrace.getEnvironment();
435 }
436
437 // -------------------------------------------
438 // ITmfTraceProperties
439 // -------------------------------------------
440
441 @Override
442 public Map<String, String> getTraceProperties() {
443 Map<String, String> properties = new HashMap<>();
444 properties.putAll(fTrace.getEnvironment());
445 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
446 return properties;
447 }
448
449 // -------------------------------------------
450 // Clocks
451 // -------------------------------------------
452
453 /**
454 * gets the clock offset
455 *
456 * @return the clock offset in ns
457 */
458 public long getOffset() {
459 if (fTrace != null) {
460 return fTrace.getOffset();
461 }
462 return 0;
463 }
464
465 /**
466 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
467 * for this trace.
468 *
469 * @param cycles
470 * The timestamp in cycles
471 * @return The timestamp in nanoseconds
472 */
473 public long timestampCyclesToNanos(long cycles) {
474 return fTrace.timestampCyclesToNanos(cycles);
475 }
476
477 /**
478 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
479 * for this trace.
480 *
481 * @param nanos
482 * The timestamp in nanoseconds
483 * @return The timestamp in cycles
484 */
485 public long timestampNanoToCycles(long nanos) {
486 return fTrace.timestampNanoToCycles(nanos);
487 }
488
489 /**
490 * Gets the list of declared events
491 */
492 @Override
493 public Set<CtfTmfEventType> getContainedEventTypes() {
494 return ImmutableSet.copyOf(fContainedEventTypes.values());
495 }
496
497 /**
498 * Register an event type to this trace.
499 *
500 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
501 *
502 * FIXME This could probably be made cleaner?
503 *
504 * @param eventType
505 * The event type to register
506 */
507 public void registerEventType(CtfTmfEventType eventType) {
508 fContainedEventTypes.put(eventType.getName(), eventType);
509 }
510
511 // -------------------------------------------
512 // Parser
513 // -------------------------------------------
514
515 @Override
516 public CtfTmfEvent parseEvent(ITmfContext context) {
517 CtfTmfEvent event = null;
518 if (context instanceof CtfTmfContext) {
519 final ITmfContext tmpContext = seekEvent(context.getLocation());
520 event = getNext(tmpContext);
521 }
522 return event;
523 }
524
525 /**
526 * Sets the cache size for a CtfTmfTrace.
527 */
528 protected void setCacheSize() {
529 setCacheSize(DEFAULT_CACHE_SIZE);
530 }
531
532 // -------------------------------------------
533 // CtfIterator factory methods
534 // -------------------------------------------
535
536 /**
537 * Get an iterator to the trace
538 *
539 * @return an iterator to the trace
540 */
541 public ITmfContext createIterator() {
542 try {
543 return new CtfIterator(fTrace, this);
544 } catch (CTFException e) {
545 Activator.getDefault().logError(e.getMessage(), e);
546 }
547 return null;
548 }
549
550 /**
551 * Get an iterator to the trace, , which will initially point to the given
552 * location/rank.
553 *
554 * @param ctfLocationData
555 * The initial timestamp the iterator will be pointing to
556 * @param rank
557 * The initial rank
558 * @return The new iterator
559 */
560 public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
561 try {
562 return new CtfIterator(fTrace, this, ctfLocationData, rank);
563 } catch (CTFException e) {
564 Activator.getDefault().logError(e.getMessage(), e);
565 }
566 return null;
567 }
568
569 /**
570 * Create the 'CtfIterator' object from a CtfTmfContext.
571 *
572 * @param context
573 * The iterator will initially be pointing to this context
574 * @return A new CtfIterator object
575 * @since 1.0
576 */
577 public ITmfContext createIteratorFromContext(CtfTmfContext context) {
578 return fIteratorManager.getIterator(context);
579 }
580
581 /**
582 * Dispose an iterator that was create with
583 * {@link #createIteratorFromContext}
584 *
585 * @param context
586 * The last context that was pointed to by the iterator (this is
587 * the 'key' to find the correct iterator to dispose).
588 * @since 1.0
589 */
590 public void disposeContext(CtfTmfContext context) {
591 fIteratorManager.removeIterator(context);
592 }
593
594 // ------------------------------------------------------------------------
595 // Timestamp transformation functions
596 // ------------------------------------------------------------------------
597
598 /**
599 * @since 1.0
600 */
601 @Override
602 public @NonNull TmfNanoTimestamp createTimestamp(long ts) {
603 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
604 }
605
606 private static int fCheckpointSize = -1;
607
608 @Override
609 public synchronized int getCheckpointSize() {
610 if (fCheckpointSize == -1) {
611 TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
612 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
613 b.clear();
614 c.serialize(b);
615 fCheckpointSize = b.position();
616 }
617
618 return fCheckpointSize;
619 }
620
621 @Override
622 protected ITmfTraceIndexer createIndexer(int interval) {
623 return new TmfBTreeTraceIndexer(this, interval);
624 }
625
626 @Override
627 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
628 return new CtfLocation(bufferIn);
629 }
630
631 @Override
632 public boolean isComplete() {
633 if (getResource() == null) {
634 return true;
635 }
636
637 String host = null;
638 String port = null;
639 String sessionName = null;
640 try {
641 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
642 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
643 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
644 } catch (CoreException e) {
645 Activator.getDefault().logError(e.getMessage(), e);
646 // Something happened to the resource, assume we won't get any more
647 // data from it
648 return true;
649 }
650 return host == null || port == null || sessionName == null;
651 }
652
653 @Override
654 public void setComplete(final boolean isComplete) {
655 super.setComplete(isComplete);
656 try {
657 if (isComplete) {
658 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
659 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
660 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
661 }
662 } catch (CoreException e) {
663 Activator.getDefault().logError(e.getMessage(), e);
664 }
665 }
666 }
This page took 0.047379 seconds and 6 git commands to generate.