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