Remove unneeded checkNotNull() calls
[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.tracecompass.ctf.core.CTFException;
36 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
37 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
38 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
39 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
40 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
41 import org.eclipse.tracecompass.ctf.core.trace.Metadata;
42 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
43 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
44 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager;
45 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
46 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
47 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
48 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
49 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
50 import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
51 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
52 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
53 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
54 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
55 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
56 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
57 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
58 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
59 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
60 import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
61 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
62 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
63 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
64 import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
65 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
66 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
67 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext;
68 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
69 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventFactory;
70 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
71 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
72 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
73
74 import com.google.common.collect.ImmutableList;
75 import com.google.common.collect.ImmutableSet;
76
77 /**
78 * The CTf trace handler
79 *
80 * @version 1.0
81 * @author Matthew khouzam
82 */
83 public class CtfTmfTrace extends TmfTrace
84 implements ITmfPropertiesProvider, ITmfPersistentlyIndexable,
85 ITmfTraceWithPreDefinedEvents {
86
87 // -------------------------------------------
88 // Constants
89 // -------------------------------------------
90
91 /**
92 * Clock offset property
93 * @since 2.0
94 */
95 public static final String CLOCK_OFFSET = "clock_offset"; //$NON-NLS-1$
96
97 /**
98 * Default cache size for CTF traces
99 */
100 protected static final int DEFAULT_CACHE_SIZE = 50000;
101
102 /**
103 * Event aspects available for all CTF traces
104 * @since 1.0
105 */
106 protected static final @NonNull Collection<@NonNull ITmfEventAspect> CTF_ASPECTS =
107 ImmutableList.of(
108 ITmfEventAspect.BaseAspects.TIMESTAMP,
109 new CtfChannelAspect(),
110 new CtfCpuAspect(),
111 ITmfEventAspect.BaseAspects.EVENT_TYPE,
112 ITmfEventAspect.BaseAspects.CONTENTS
113 );
114
115 /**
116 * The Ctf clock unique identifier field
117 */
118 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
119 private static final int CONFIDENCE = 10;
120 private static final int MIN_CONFIDENCE = 1;
121
122 // -------------------------------------------
123 // Fields
124 // -------------------------------------------
125
126 private final Map<@NonNull String, @NonNull CtfTmfEventType> fContainedEventTypes =
127 Collections.synchronizedMap(new HashMap<>());
128
129 private final CtfIteratorManager fIteratorManager = new CtfIteratorManager(this);
130
131 private final @NonNull CtfTmfEventFactory fEventFactory;
132
133 /** Reference to the CTF Trace */
134 private CTFTrace fTrace;
135
136 // -------------------------------------------
137 // Constructor
138 // -------------------------------------------
139
140 /**
141 * Default constructor
142 */
143 public CtfTmfTrace() {
144 super();
145
146 /* Use default event factory */
147 fEventFactory = CtfTmfEventFactory.instance();
148 }
149
150 /**
151 * Constructor for sub-classes to specify their own event factory.
152 *
153 * @param eventFactory
154 * The event factory to use to generate trace events
155 * @since 2.0
156 */
157 protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory) {
158 super();
159 fEventFactory = eventFactory;
160 }
161
162 // -------------------------------------------
163 // TmfTrace Overrides
164 // -------------------------------------------
165 /**
166 * Method initTrace.
167 *
168 * @param resource
169 * The resource associated with this trace
170 * @param path
171 * The path to the trace file
172 * @param eventType
173 * The type of events that will be read from this trace
174 * @throws TmfTraceException
175 * If something went wrong while reading the trace
176 */
177 @Override
178 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
179 throws TmfTraceException {
180 /*
181 * Set the cache size. This has to be done before the call to super()
182 * because the super needs to know the cache size.
183 */
184 setCacheSize();
185
186 super.initTrace(resource, path, eventType);
187
188 try {
189 this.fTrace = new CTFTrace(path);
190 CtfTmfContext ctx;
191 /* Set the start and (current) end times for this trace */
192 ctx = (CtfTmfContext) seekEvent(0L);
193 CtfTmfEvent event = getNext(ctx);
194 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
195 /* Handle the case where the trace is empty */
196 this.setStartTime(TmfTimestamp.BIG_BANG);
197 } else {
198 final ITmfTimestamp curTime = event.getTimestamp();
199 this.setStartTime(curTime);
200 this.setEndTime(curTime);
201 }
202 /*
203 * Register every event type. When you call getType, it will
204 * register a trace to that type in the TmfEventTypeManager
205 */
206 try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
207 for (IEventDeclaration ied : iter.getEventDeclarations()) {
208 CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
209 if (ctfTmfEventType == null) {
210 List<ITmfEventField> content = new ArrayList<>();
211 /* Should only return null the first time */
212 final StructDeclaration fields = ied.getFields();
213 if (fields != null) {
214 for (String fieldName : fields.getFieldsList()) {
215 content.add(new TmfEventField(checkNotNull(fieldName), null, null));
216 }
217 ITmfEventField contentTree = new TmfEventField(
218 ITmfEventField.ROOT_FIELD_ID,
219 null,
220 content.toArray(new ITmfEventField[content.size()]));
221
222 ctfTmfEventType = new CtfTmfEventType(checkNotNull(ied.getName()), contentTree);
223 fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
224 }
225 }
226 }
227 }
228 ctx.dispose();
229 } catch (final CTFException e) {
230 /*
231 * If it failed at the init(), we can assume it's because the file
232 * was not found or was not recognized as a CTF trace. Throw into
233 * the new type of exception expected by the rest of TMF.
234 */
235 throw new TmfTraceException(e.getMessage(), e);
236 }
237 }
238
239 @Override
240 public synchronized void dispose() {
241 fIteratorManager.dispose();
242 if (fTrace != null) {
243 fTrace = null;
244 }
245 super.dispose();
246 }
247
248 /**
249 * {@inheritDoc}
250 * <p>
251 * The default implementation of a CTF trace.
252 *
253 * Firstly a weak validation of the metadata is done to determine if the
254 * path is actually for a CTF trace. After that a full validation is done.
255 *
256 * If the weak and full validation are successful the confidence is set
257 * to 10.
258 *
259 * If the weak validation was successful, but the full validation fails
260 * a TraceValidationStatus with severity warning and confidence of 1 is
261 * returned.
262 *
263 * If both weak and full validation fails an error status is returned.
264 */
265 @Override
266 public IStatus validate(final IProject project, final String path) {
267 boolean isMetadataFile = false;
268 try {
269 isMetadataFile = Metadata.preValidate(path);
270 } catch (final CTFException e) {
271 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
272 }
273
274 if (isMetadataFile) {
275 // Trace is pre-validated, continue will full validation
276 try {
277 final CTFTrace trace = new CTFTrace(path);
278 if (!trace.majorIsSet()) {
279 if (isMetadataFile) {
280 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null);
281 }
282 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
283 }
284
285 // Validate using reader initialization
286 try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {}
287
288 // Trace is validated, return with confidence
289 return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
290
291 } catch (final CTFException | BufferOverflowException e ) {
292 // return warning since it's a CTF trace but with errors in it
293 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
294 }
295 }
296 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
297 }
298
299 @Override
300 public Iterable<ITmfEventAspect> getEventAspects() {
301 return CTF_ASPECTS;
302 }
303
304 /**
305 * Method getCurrentLocation. This is not applicable in CTF
306 *
307 * @return null, since the trace has no knowledge of the current location
308 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
309 */
310 @Override
311 public ITmfLocation getCurrentLocation() {
312 return null;
313 }
314
315 @Override
316 public double getLocationRatio(ITmfLocation location) {
317 final CtfLocation curLocation = (CtfLocation) location;
318 final long startTime = getStartTime().getValue();
319 final double diff = curLocation.getLocationInfo().getTimestamp() - startTime;
320 final double total = getEndTime().getValue() - startTime;
321 return Math.max(0.0, Math.min(1.0, diff / total));
322 }
323
324 /**
325 * Method seekEvent.
326 *
327 * @param location
328 * ITmfLocation<?>
329 * @return ITmfContext
330 */
331 @Override
332 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
333 CtfLocation currentLocation = (CtfLocation) location;
334 CtfTmfContext context = new CtfTmfContext(this);
335 if (fTrace == null) {
336 context.setLocation(null);
337 context.setRank(ITmfContext.UNKNOWN_RANK);
338 return context;
339 }
340 /*
341 * The rank is set to 0 if the iterator seeks the beginning. If not, it
342 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
343 * by rank for now.
344 */
345 if (currentLocation == null) {
346 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
347 context.setRank(0);
348 } else {
349 context.setRank(ITmfContext.UNKNOWN_RANK);
350 }
351 /* This will seek and update the location after the seek */
352 context.setLocation(currentLocation);
353 return context;
354 }
355
356 @Override
357 public synchronized ITmfContext seekEvent(double ratio) {
358 CtfTmfContext context = new CtfTmfContext(this);
359 if (fTrace == null) {
360 context.setLocation(null);
361 context.setRank(ITmfContext.UNKNOWN_RANK);
362 return context;
363 }
364 final long end = getEndTime().getValue();
365 final long start = getStartTime().getValue();
366 final long diff = end - start;
367 final long ratioTs = Math.round(diff * ratio) + start;
368 context.seek(ratioTs);
369 context.setRank(ITmfContext.UNKNOWN_RANK);
370 return context;
371 }
372
373 /**
374 * Method readNextEvent.
375 *
376 * @param context
377 * ITmfContext
378 * @return CtfTmfEvent
379 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
380 */
381 @Override
382 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
383 if (fTrace == null) {
384 return null;
385 }
386 CtfTmfEvent event = null;
387 if (context instanceof CtfTmfContext) {
388 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
389 return null;
390 }
391 CtfTmfContext ctfContext = (CtfTmfContext) context;
392 event = ctfContext.getCurrentEvent();
393
394 if (event != null) {
395 updateAttributes(context, event);
396 ctfContext.advance();
397 ctfContext.increaseRank();
398 }
399 }
400
401 return event;
402 }
403
404 /**
405 * Ctf traces have a clock with a unique uuid that will be used to identify
406 * the host. Traces with the same clock uuid will be known to have been made
407 * on the same machine.
408 *
409 * Note: uuid is an optional field, it may not be there for a clock.
410 */
411 @Override
412 public String getHostId() {
413 CTFClock clock = fTrace.getClock();
414 if (clock != null) {
415 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
416 if (clockHost != null) {
417 return clockHost;
418 }
419 }
420 return super.getHostId();
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
433 // -------------------------------------------
434 // ITmfPropertiesProvider
435 // -------------------------------------------
436
437 /**
438 * @since 2.0
439 */
440 @Override
441 public Map<String, String> getProperties() {
442 Map<String, String> properties = new HashMap<>();
443 properties.putAll(fTrace.getEnvironment());
444 properties.put(CLOCK_OFFSET, Long.toUnsignedString(fTrace.getOffset()));
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<@NonNull 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 the event factory for this trace to generate new events for it.
538 *
539 * @return The event factory
540 * @since 2.0
541 */
542 public @NonNull CtfTmfEventFactory getEventFactory() {
543 return fEventFactory;
544 }
545
546 /**
547 * Get an iterator to the trace
548 *
549 * @return an iterator to the trace
550 */
551 public ITmfContext createIterator() {
552 try {
553 return new CtfIterator(fTrace, this);
554 } catch (CTFException e) {
555 Activator.getDefault().logError(e.getMessage(), e);
556 }
557 return null;
558 }
559
560 /**
561 * Get an iterator to the trace, , which will initially point to the given
562 * location/rank.
563 *
564 * @param ctfLocationData
565 * The initial timestamp the iterator will be pointing to
566 * @param rank
567 * The initial rank
568 * @return The new iterator
569 */
570 public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
571 try {
572 return new CtfIterator(fTrace, this, ctfLocationData, rank);
573 } catch (CTFException e) {
574 Activator.getDefault().logError(e.getMessage(), e);
575 }
576 return null;
577 }
578
579 /**
580 * Create the 'CtfIterator' object from a CtfTmfContext.
581 *
582 * @param context
583 * The iterator will initially be pointing to this context
584 * @return A new CtfIterator object
585 * @since 1.0
586 */
587 public ITmfContext createIteratorFromContext(CtfTmfContext context) {
588 return fIteratorManager.getIterator(context);
589 }
590
591 /**
592 * Dispose an iterator that was create with
593 * {@link #createIteratorFromContext}
594 *
595 * @param context
596 * The last context that was pointed to by the iterator (this is
597 * the 'key' to find the correct iterator to dispose).
598 * @since 1.0
599 */
600 public void disposeContext(CtfTmfContext context) {
601 fIteratorManager.removeIterator(context);
602 }
603
604 // ------------------------------------------------------------------------
605 // Timestamp transformation functions
606 // ------------------------------------------------------------------------
607
608 /**
609 * @since 1.0
610 */
611 @Override
612 public @NonNull TmfNanoTimestamp createTimestamp(long ts) {
613 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
614 }
615
616 private static int fCheckpointSize = -1;
617
618 @Override
619 public synchronized int getCheckpointSize() {
620 if (fCheckpointSize == -1) {
621 TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
622 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
623 b.clear();
624 c.serialize(b);
625 fCheckpointSize = b.position();
626 }
627
628 return fCheckpointSize;
629 }
630
631 @Override
632 protected ITmfTraceIndexer createIndexer(int interval) {
633 return new TmfBTreeTraceIndexer(this, interval);
634 }
635
636 @Override
637 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
638 return new CtfLocation(bufferIn);
639 }
640
641 @Override
642 public boolean isComplete() {
643 if (getResource() == null) {
644 return true;
645 }
646
647 String host = null;
648 String port = null;
649 String sessionName = null;
650 try {
651 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
652 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
653 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
654 } catch (CoreException e) {
655 Activator.getDefault().logError(e.getMessage(), e);
656 // Something happened to the resource, assume we won't get any more
657 // data from it
658 return true;
659 }
660 return host == null || port == null || sessionName == null;
661 }
662
663 @Override
664 public void setComplete(final boolean isComplete) {
665 super.setComplete(isComplete);
666 try {
667 if (isComplete) {
668 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
669 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
670 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
671 }
672 } catch (CoreException e) {
673 Activator.getDefault().logError(e.getMessage(), e);
674 }
675 }
676 }
This page took 0.044898 seconds and 5 git commands to generate.