Remove all existing @since annotations
[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.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.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 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 /**
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 */
267 @Override
268 public ITmfLocation getCurrentLocation() {
269 return null;
270 }
271
272 @Override
273 public double getLocationRatio(ITmfLocation location) {
274 final CtfLocation curLocation = (CtfLocation) location;
275 final CtfTmfContext context = new CtfTmfContext(this);
276 context.setLocation(curLocation);
277 context.seek(curLocation.getLocationInfo());
278 final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
279 final long startTime = fIteratorManager.getIterator(context).getStartTime();
280 final long endTime = fIteratorManager.getIterator(context).getEndTime();
281 return ((double) currentTime.getTimestamp() - startTime)
282 / (endTime - startTime);
283 }
284
285 /**
286 * Method seekEvent.
287 *
288 * @param location
289 * ITmfLocation<?>
290 * @return ITmfContext
291 */
292 @Override
293 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
294 CtfLocation currentLocation = (CtfLocation) location;
295 CtfTmfContext context = new CtfTmfContext(this);
296 if (fTrace == null) {
297 context.setLocation(null);
298 context.setRank(ITmfContext.UNKNOWN_RANK);
299 return context;
300 }
301 /*
302 * The rank is set to 0 if the iterator seeks the beginning. If not, it
303 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
304 * by rank for now.
305 */
306 if (currentLocation == null) {
307 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
308 context.setRank(0);
309 }
310 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
311 currentLocation = new CtfLocation(fTrace.getCurrentEndTime() + 1, 0L);
312 }
313 context.setLocation(currentLocation);
314 if (location == null) {
315 long timestamp = fIteratorManager.getIterator(context).getCurrentTimestamp();
316 currentLocation = new CtfLocation(timestamp, 0);
317 }
318 if (context.getRank() != 0) {
319 context.setRank(ITmfContext.UNKNOWN_RANK);
320 }
321 return context;
322 }
323
324 @Override
325 public synchronized ITmfContext seekEvent(double ratio) {
326 CtfTmfContext context = new CtfTmfContext(this);
327 if (fTrace == null) {
328 context.setLocation(null);
329 context.setRank(ITmfContext.UNKNOWN_RANK);
330 return context;
331 }
332 final long end = fTrace.getCurrentEndTime();
333 final long start = fTrace.getCurrentStartTime();
334 final long diff = end - start;
335 final long ratioTs = Math.round(diff * ratio) + start;
336 context.seek(ratioTs);
337 context.setRank(ITmfContext.UNKNOWN_RANK);
338 return context;
339 }
340
341 /**
342 * Method readNextEvent.
343 *
344 * @param context
345 * ITmfContext
346 * @return CtfTmfEvent
347 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
348 */
349 @Override
350 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
351 if (fTrace == null) {
352 return null;
353 }
354 CtfTmfEvent event = null;
355 if (context instanceof CtfTmfContext) {
356 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
357 return null;
358 }
359 CtfTmfContext ctfContext = (CtfTmfContext) context;
360 event = ctfContext.getCurrentEvent();
361
362 if (event != null) {
363 updateAttributes(context, event.getTimestamp());
364 ctfContext.advance();
365 ctfContext.increaseRank();
366 }
367 }
368
369 return event;
370 }
371
372 /**
373 * Ctf traces have a clock with a unique uuid that will be used to identify
374 * the host. Traces with the same clock uuid will be known to have been made
375 * on the same machine.
376 *
377 * Note: uuid is an optional field, it may not be there for a clock.
378 */
379 @Override
380 public String getHostId() {
381 CTFClock clock = fTrace.getClock();
382 if (clock != null) {
383 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
384 if (clockHost != null) {
385 return clockHost;
386 }
387 }
388 return super.getHostId();
389 }
390
391 /**
392 * Get the first callsite that matches the event name
393 *
394 * @param eventName The event name to look for
395 * @return The best callsite candidate
396 */
397 public @Nullable CtfTmfCallsite getCallsite(String eventName) {
398 CTFCallsite callsite = fTrace.getCallsite(eventName);
399 if (callsite != null) {
400 return new CtfTmfCallsite(callsite);
401 }
402 return null;
403 }
404
405 /**
406 * Get the closest matching callsite for given event name and instruction
407 * pointer
408 *
409 * @param eventName
410 * The event name
411 * @param ip
412 * The instruction pointer
413 * @return The closest matching callsite
414 */
415 public @Nullable CtfTmfCallsite getCallsite(String eventName, long ip) {
416 CTFCallsite calliste = fTrace.getCallsite(eventName, ip);
417 if (calliste != null) {
418 return new CtfTmfCallsite(calliste);
419 }
420 return null;
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 // ITmfTraceProperties
435 // -------------------------------------------
436
437 @Override
438 public Map<String, String> getTraceProperties() {
439 Map<String, String> properties = new HashMap<>();
440 properties.putAll(fTrace.getEnvironment());
441 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
442 return properties;
443 }
444
445 // -------------------------------------------
446 // Clocks
447 // -------------------------------------------
448
449 /**
450 * gets the clock offset
451 *
452 * @return the clock offset in ns
453 */
454 public long getOffset() {
455 if (fTrace != null) {
456 return fTrace.getOffset();
457 }
458 return 0;
459 }
460
461 /**
462 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
463 * for this trace.
464 *
465 * @param cycles
466 * The timestamp in cycles
467 * @return The timestamp in nanoseconds
468 */
469 public long timestampCyclesToNanos(long cycles) {
470 return fTrace.timestampCyclesToNanos(cycles);
471 }
472
473 /**
474 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
475 * for this trace.
476 *
477 * @param nanos
478 * The timestamp in nanoseconds
479 * @return The timestamp in cycles
480 */
481 public long timestampNanoToCycles(long nanos) {
482 return fTrace.timestampNanoToCycles(nanos);
483 }
484
485 /**
486 * Gets the list of declared events
487 */
488 @Override
489 public Set<CtfTmfEventType> getContainedEventTypes() {
490 return ImmutableSet.copyOf(fContainedEventTypes.values());
491 }
492
493 /**
494 * Register an event type to this trace.
495 *
496 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
497 *
498 * FIXME This could probably be made cleaner?
499 *
500 * @param eventType
501 * The event type to register
502 */
503 public void registerEventType(CtfTmfEventType eventType) {
504 fContainedEventTypes.put(eventType.getName(), eventType);
505 }
506
507 // -------------------------------------------
508 // Parser
509 // -------------------------------------------
510
511 @Override
512 public CtfTmfEvent parseEvent(ITmfContext context) {
513 CtfTmfEvent event = null;
514 if (context instanceof CtfTmfContext) {
515 final ITmfContext tmpContext = seekEvent(context.getLocation());
516 event = getNext(tmpContext);
517 }
518 return event;
519 }
520
521 /**
522 * Sets the cache size for a CtfTmfTrace.
523 */
524 protected void setCacheSize() {
525 setCacheSize(DEFAULT_CACHE_SIZE);
526 }
527
528 // -------------------------------------------
529 // CtfIterator factory methods
530 // -------------------------------------------
531
532 /**
533 * Get an iterator to the trace
534 *
535 * @return an iterator to the trace
536 */
537 public ITmfContext createIterator() {
538 try {
539 return new CtfIterator(fTrace, this);
540 } catch (CTFReaderException e) {
541 Activator.getDefault().logError(e.getMessage(), e);
542 }
543 return null;
544 }
545
546 /**
547 * Get an iterator to the trace, , which will initially point to the given
548 * location/rank.
549 *
550 * @param ctfLocationData
551 * The initial timestamp the iterator will be pointing to
552 * @param rank
553 * The initial rank
554 * @return The new iterator
555 */
556 public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
557 try {
558 return new CtfIterator(fTrace, this, ctfLocationData, rank);
559 } catch (CTFReaderException e) {
560 Activator.getDefault().logError(e.getMessage(), e);
561 }
562 return null;
563 }
564
565 // ------------------------------------------------------------------------
566 // Timestamp transformation functions
567 // ------------------------------------------------------------------------
568
569 @Override
570 public CtfTmfTimestamp createTimestamp(long ts) {
571 return new CtfTmfTimestamp(getTimestampTransform().transform(ts));
572 }
573
574 private static int fCheckpointSize = -1;
575
576 @Override
577 public synchronized int getCheckpointSize() {
578 if (fCheckpointSize == -1) {
579 TmfCheckpoint c = new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
580 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
581 b.clear();
582 c.serialize(b);
583 fCheckpointSize = b.position();
584 }
585
586 return fCheckpointSize;
587 }
588
589 @Override
590 protected ITmfTraceIndexer createIndexer(int interval) {
591 return new TmfBTreeTraceIndexer(this, interval);
592 }
593
594 @Override
595 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
596 return new CtfLocation(bufferIn);
597 }
598
599 @Override
600 public boolean isComplete() {
601 if (getResource() == null) {
602 return true;
603 }
604
605 String host = null;
606 String port = null;
607 String sessionName = null;
608 try {
609 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
610 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
611 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
612 } catch (CoreException e) {
613 Activator.getDefault().logError(e.getMessage(), e);
614 // Something happened to the resource, assume we won't get any more
615 // data from it
616 return true;
617 }
618 return host == null || port == null || sessionName == null;
619 }
620
621 @Override
622 public void setComplete(final boolean isComplete) {
623 super.setComplete(isComplete);
624 try {
625 if (isComplete) {
626 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
627 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
628 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
629 }
630 } catch (CoreException e) {
631 Activator.getDefault().logError(e.getMessage(), e);
632 }
633 }
634 }
This page took 0.057801 seconds and 5 git commands to generate.