ctf: Avoid direct dependency from the lttng plugins to the ctf.core one
[deliverable/tracecompass.git] / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / trace / CtfTmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2014 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.event.CTFCallsite;
37 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
38 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
39 import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException;
40 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
41 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
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.TmfTimestamp;
52 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
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;
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;
69 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
70 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
71 import org.eclipse.tracecompass.tmf.ctf.core.event.lookup.CtfTmfCallsite;
72 import org.eclipse.tracecompass.tmf.ctf.core.timestamp.CtfTmfTimestamp;
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, AutoCloseable {
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 */
99 private static final @NonNull Collection<ITmfEventAspect> CTF_ASPECTS =
100 checkNotNull(ImmutableList.of(
101 ITmfEventAspect.BaseAspects.TIMESTAMP,
102 new CtfChannelAspect(),
103 new CtfCpuAspect(),
104 ITmfEventAspect.BaseAspects.EVENT_TYPE,
105 ITmfEventAspect.BaseAspects.CONTENTS
106 ));
107
108 /**
109 * The Ctf clock unique identifier field
110 */
111 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
112 private static final int CONFIDENCE = 10;
113
114 // -------------------------------------------
115 // Fields
116 // -------------------------------------------
117
118 private final Map<String, CtfTmfEventType> fContainedEventTypes =
119 Collections.synchronizedMap(new HashMap<String, CtfTmfEventType>());
120
121 private final CtfIteratorManager fIteratorManager =
122 new CtfIteratorManager(this);
123
124 /* Reference to the CTF Trace */
125 private CTFTrace fTrace;
126
127 // -------------------------------------------
128 // TmfTrace Overrides
129 // -------------------------------------------
130 /**
131 * Method initTrace.
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
139 * @throws TmfTraceException
140 * If something went wrong while reading the trace
141 */
142 @Override
143 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
144 throws TmfTraceException {
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();
150
151 super.initTrace(resource, path, eventType);
152
153 try {
154 this.fTrace = new CTFTrace(path);
155 CtfTmfContext ctx;
156 /* Set the start and (current) end times for this trace */
157 ctx = (CtfTmfContext) seekEvent(0L);
158 CtfTmfEvent event = getNext(ctx);
159 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
160 /* Handle the case where the trace is empty */
161 this.setStartTime(TmfTimestamp.BIG_BANG);
162 } else {
163 final ITmfTimestamp curTime = event.getTimestamp();
164 this.setStartTime(curTime);
165 this.setEndTime(curTime);
166 }
167 /*
168 * Register every event type. When you call getType, it will
169 * register a trace to that type in the TmfEventTypeManager
170 */
171 try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
172 for (IEventDeclaration ied : iter.getEventDeclarations()) {
173 CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
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
186 ctfTmfEventType = new CtfTmfEventType(ied.getName(), contentTree);
187 fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
188 }
189 }
190 }
191 } catch (final CTFReaderException e) {
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 */
197 throw new TmfTraceException(e.getMessage(), e);
198 }
199 }
200
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
210 @Override
211 public void close() {
212 dispose();
213 }
214
215 @Override
216 public synchronized void dispose() {
217 fIteratorManager.dispose();
218 if (fTrace != null) {
219 fTrace = null;
220 }
221 super.dispose();
222 }
223
224 /**
225 * {@inheritDoc}
226 * <p>
227 * The default implementation sets the confidence to 10 if the trace is a
228 * valid CTF trace.
229 */
230 @Override
231 public IStatus validate(final IProject project, final String path) {
232 IStatus status = new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
233 try {
234 final CTFTrace temp = new CTFTrace(path);
235 if (!temp.majorIsSet()) {
236 status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
237 } else {
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 }
245 }
246 }
247 } catch (final CTFReaderException e) {
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$
251 }
252
253 return status;
254 }
255
256 @Override
257 public Iterable<ITmfEventAspect> getEventAspects() {
258 return CTF_ASPECTS;
259 }
260
261 /**
262 * Method getCurrentLocation. This is not applicable in CTF
263 *
264 * @return null, since the trace has no knowledge of the current location
265 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
266 * @since 3.0
267 */
268 @Override
269 public ITmfLocation getCurrentLocation() {
270 return null;
271 }
272
273 /**
274 * @since 3.0
275 */
276 @Override
277 public double getLocationRatio(ITmfLocation location) {
278 final CtfLocation curLocation = (CtfLocation) location;
279 final CtfTmfContext context = new CtfTmfContext(this);
280 context.setLocation(curLocation);
281 context.seek(curLocation.getLocationInfo());
282 final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
283 final long startTime = fIteratorManager.getIterator(context).getStartTime();
284 final long endTime = fIteratorManager.getIterator(context).getEndTime();
285 return ((double) currentTime.getTimestamp() - startTime)
286 / (endTime - startTime);
287 }
288
289 /**
290 * Method seekEvent.
291 *
292 * @param location
293 * ITmfLocation<?>
294 * @return ITmfContext
295 * @since 3.0
296 */
297 @Override
298 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
299 CtfLocation currentLocation = (CtfLocation) location;
300 CtfTmfContext context = new CtfTmfContext(this);
301 if (fTrace == null) {
302 context.setLocation(null);
303 context.setRank(ITmfContext.UNKNOWN_RANK);
304 return context;
305 }
306 /*
307 * The rank is set to 0 if the iterator seeks the beginning. If not, it
308 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
309 * by rank for now.
310 */
311 if (currentLocation == null) {
312 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
313 context.setRank(0);
314 }
315 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
316 currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
317 }
318 context.setLocation(currentLocation);
319 if (location == null) {
320 long timestamp = fIteratorManager.getIterator(context).getCurrentTimestamp();
321 currentLocation = new CtfLocation(timestamp, 0);
322 }
323 if (context.getRank() != 0) {
324 context.setRank(ITmfContext.UNKNOWN_RANK);
325 }
326 return context;
327 }
328
329 @Override
330 public synchronized ITmfContext seekEvent(double ratio) {
331 CtfTmfContext context = new CtfTmfContext(this);
332 if (fTrace == null) {
333 context.setLocation(null);
334 context.setRank(ITmfContext.UNKNOWN_RANK);
335 return context;
336 }
337 final long end = fTrace.getCurrentEndTime();
338 final long start = fTrace.getCurrentStartTime();
339 final long diff = end - start;
340 final long ratioTs = Math.round(diff * ratio) + start;
341 context.seek(ratioTs);
342 context.setRank(ITmfContext.UNKNOWN_RANK);
343 return context;
344 }
345
346 /**
347 * Method readNextEvent.
348 *
349 * @param context
350 * ITmfContext
351 * @return CtfTmfEvent
352 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
353 */
354 @Override
355 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
356 if (fTrace == null) {
357 return null;
358 }
359 CtfTmfEvent event = null;
360 if (context instanceof CtfTmfContext) {
361 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
362 return null;
363 }
364 CtfTmfContext ctfContext = (CtfTmfContext) context;
365 event = ctfContext.getCurrentEvent();
366
367 if (event != null) {
368 updateAttributes(context, event.getTimestamp());
369 ctfContext.advance();
370 ctfContext.increaseRank();
371 }
372 }
373
374 return event;
375 }
376
377 /**
378 * Ctf traces have a clock with a unique uuid that will be used to identify
379 * the host. Traces with the same clock uuid will be known to have been made
380 * on the same machine.
381 *
382 * Note: uuid is an optional field, it may not be there for a clock.
383 */
384 @Override
385 public String getHostId() {
386 CTFClock clock = fTrace.getClock();
387 if (clock != null) {
388 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
389 if (clockHost != null) {
390 return clockHost;
391 }
392 }
393 return super.getHostId();
394 }
395
396 /**
397 * Get the first callsite that matches the event name
398 *
399 * @param eventName The event name to look for
400 * @return The best callsite candidate
401 */
402 public @Nullable CtfTmfCallsite getCallsite(String eventName) {
403 CTFCallsite callsite = fTrace.getCallsite(eventName);
404 if (callsite != null) {
405 return new CtfTmfCallsite(callsite);
406 }
407 return null;
408 }
409
410 /**
411 * Get the closest matching callsite for given event name and instruction
412 * pointer
413 *
414 * @param eventName
415 * The event name
416 * @param ip
417 * The instruction pointer
418 * @return The closest matching callsite
419 */
420 public @Nullable CtfTmfCallsite getCallsite(String eventName, long ip) {
421 CTFCallsite calliste = fTrace.getCallsite(eventName, ip);
422 if (calliste != null) {
423 return new CtfTmfCallsite(calliste);
424 }
425 return null;
426 }
427
428 /**
429 * Get the CTF environment variables defined in this CTF trace, in <name,
430 * value> form. This comes from the trace's CTF metadata.
431 *
432 * @return The CTF environment
433 */
434 public Map<String, String> getEnvironment() {
435 return fTrace.getEnvironment();
436 }
437
438 // -------------------------------------------
439 // ITmfTraceProperties
440 // -------------------------------------------
441
442 /**
443 * @since 2.0
444 */
445 @Override
446 public Map<String, String> getTraceProperties() {
447 Map<String, String> properties = new HashMap<>();
448 properties.putAll(fTrace.getEnvironment());
449 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
450 return properties;
451 }
452
453 // -------------------------------------------
454 // Clocks
455 // -------------------------------------------
456
457 /**
458 * gets the clock offset
459 *
460 * @return the clock offset in ns
461 */
462 public long getOffset() {
463 if (fTrace != null) {
464 return fTrace.getOffset();
465 }
466 return 0;
467 }
468
469 /**
470 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
471 * for this trace.
472 *
473 * @param cycles
474 * The timestamp in cycles
475 * @return The timestamp in nanoseconds
476 */
477 public long timestampCyclesToNanos(long cycles) {
478 return fTrace.timestampCyclesToNanos(cycles);
479 }
480
481 /**
482 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
483 * for this trace.
484 *
485 * @param nanos
486 * The timestamp in nanoseconds
487 * @return The timestamp in cycles
488 */
489 public long timestampNanoToCycles(long nanos) {
490 return fTrace.timestampNanoToCycles(nanos);
491 }
492
493 /**
494 * Gets the list of declared events
495 *
496 * @since 3.0
497 */
498 @Override
499 public Set<CtfTmfEventType> getContainedEventTypes() {
500 return ImmutableSet.copyOf(fContainedEventTypes.values());
501 }
502
503 /**
504 * Register an event type to this trace.
505 *
506 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
507 *
508 * FIXME This could probably be made cleaner?
509 *
510 * @param eventType
511 * The event type to register
512 */
513 public void registerEventType(CtfTmfEventType eventType) {
514 fContainedEventTypes.put(eventType.getName(), eventType);
515 }
516
517 // -------------------------------------------
518 // Parser
519 // -------------------------------------------
520
521 @Override
522 public CtfTmfEvent parseEvent(ITmfContext context) {
523 CtfTmfEvent event = null;
524 if (context instanceof CtfTmfContext) {
525 final ITmfContext tmpContext = seekEvent(context.getLocation());
526 event = getNext(tmpContext);
527 }
528 return event;
529 }
530
531 /**
532 * Sets the cache size for a CtfTmfTrace.
533 */
534 protected void setCacheSize() {
535 setCacheSize(DEFAULT_CACHE_SIZE);
536 }
537
538 // -------------------------------------------
539 // CtfIterator factory methods
540 // -------------------------------------------
541
542 /**
543 * Get an iterator to the trace
544 *
545 * @return an iterator to the trace
546 * @since 2.0
547 */
548 public ITmfContext createIterator() {
549 try {
550 return new CtfIterator(fTrace, this);
551 } catch (CTFReaderException e) {
552 Activator.getDefault().logError(e.getMessage(), e);
553 }
554 return null;
555 }
556
557 /**
558 * Get an iterator to the trace, , which will initially point to the given
559 * location/rank.
560 *
561 * @param ctfLocationData
562 * The initial timestamp the iterator will be pointing to
563 * @param rank
564 * The initial rank
565 * @return The new iterator
566 */
567 public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
568 try {
569 return new CtfIterator(fTrace, this, ctfLocationData, rank);
570 } catch (CTFReaderException e) {
571 Activator.getDefault().logError(e.getMessage(), e);
572 }
573 return null;
574 }
575
576 // ------------------------------------------------------------------------
577 // Timestamp transformation functions
578 // ------------------------------------------------------------------------
579
580 /**
581 * @since 3.0
582 */
583 @Override
584 public CtfTmfTimestamp createTimestamp(long ts) {
585 return new CtfTmfTimestamp(getTimestampTransform().transform(ts));
586 }
587
588 private static int fCheckpointSize = -1;
589
590 /**
591 * @since 3.0
592 */
593 @Override
594 public synchronized int getCheckpointSize() {
595 if (fCheckpointSize == -1) {
596 TmfCheckpoint c = new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
597 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
598 b.clear();
599 c.serialize(b);
600 fCheckpointSize = b.position();
601 }
602
603 return fCheckpointSize;
604 }
605
606 @Override
607 protected ITmfTraceIndexer createIndexer(int interval) {
608 return new TmfBTreeTraceIndexer(this, interval);
609 }
610
611 /**
612 * @since 3.0
613 */
614 @Override
615 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
616 return new CtfLocation(bufferIn);
617 }
618
619 @Override
620 public boolean isComplete() {
621 if (getResource() == null) {
622 return true;
623 }
624
625 String host = null;
626 String port = null;
627 String sessionName = null;
628 try {
629 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
630 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
631 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
632 } catch (CoreException e) {
633 Activator.getDefault().logError(e.getMessage(), e);
634 // Something happened to the resource, assume we won't get any more
635 // data from it
636 return true;
637 }
638 return host == null || port == null || sessionName == null;
639 }
640
641 @Override
642 public void setComplete(final boolean isComplete) {
643 super.setComplete(isComplete);
644 try {
645 if (isComplete) {
646 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
647 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
648 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
649 }
650 } catch (CoreException e) {
651 Activator.getDefault().logError(e.getMessage(), e);
652 }
653 }
654 }
This page took 0.077329 seconds and 6 git commands to generate.