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