ctf: Fix CtfTmfTrace.readEnd() to use createTimestamp()
[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, 2017 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.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Objects;
29 import java.util.Set;
30
31 import org.eclipse.core.resources.IProject;
32 import org.eclipse.core.resources.IResource;
33 import org.eclipse.core.runtime.CoreException;
34 import org.eclipse.core.runtime.IStatus;
35 import org.eclipse.core.runtime.Status;
36 import org.eclipse.jdt.annotation.NonNull;
37 import org.eclipse.jdt.annotation.Nullable;
38 import org.eclipse.tracecompass.ctf.core.CTFException;
39 import org.eclipse.tracecompass.ctf.core.event.CTFCallsite;
40 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
41 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
42 import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
43 import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInput;
44 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
45 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
46 import org.eclipse.tracecompass.ctf.core.trace.ICTFStream;
47 import org.eclipse.tracecompass.ctf.core.trace.Metadata;
48 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
49 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
50 import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager;
51 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
52 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
53 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
54 import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
55 import org.eclipse.tracecompass.tmf.core.event.aspect.TmfBaseAspects;
56 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
57 import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
58 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
59 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
60 import org.eclipse.tracecompass.tmf.core.trace.ICyclesConverter;
61 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
62 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceKnownSize;
63 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
64 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
65 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
66 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
67 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
68 import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
69 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
70 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
71 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
72 import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
73 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
74 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
75 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext;
76 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
77 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventFactory;
78 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
79 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
80 import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
81 import org.eclipse.tracecompass.tmf.ctf.core.event.lookup.CtfTmfCallsite;
82
83 import com.google.common.collect.ImmutableList;
84 import com.google.common.collect.ImmutableSet;
85
86 /**
87 * The CTf trace handler
88 *
89 * @version 1.0
90 * @author Matthew khouzam
91 */
92 public class CtfTmfTrace extends TmfTrace
93 implements ITmfPropertiesProvider, ITmfPersistentlyIndexable,
94 ITmfTraceWithPreDefinedEvents, ITmfTraceKnownSize, ICyclesConverter {
95
96 // -------------------------------------------
97 // Constants
98 // -------------------------------------------
99
100 /**
101 * Clock offset property
102 *
103 * @since 1.2
104 */
105 public static final String CLOCK_OFFSET = "clock_offset"; //$NON-NLS-1$
106
107 /**
108 * Default cache size for CTF traces
109 */
110 protected static final int DEFAULT_CACHE_SIZE = 50000;
111
112 /**
113 * Event aspects available for all CTF traces
114 *
115 * @since 1.0
116 */
117 protected static final @NonNull Collection<@NonNull ITmfEventAspect<?>> CTF_ASPECTS = ImmutableList.of(
118 TmfBaseAspects.getTimestampAspect(),
119 new CtfChannelAspect(),
120 new CtfCpuAspect(),
121 TmfBaseAspects.getEventTypeAspect(),
122 TmfBaseAspects.getContentsAspect());
123
124 /**
125 * The Ctf clock unique identifier field
126 */
127 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
128 private static final int CONFIDENCE = 10;
129 private static final int MIN_CONFIDENCE = 1;
130
131 /**
132 * This is a reduction factor to avoid overflows.
133 */
134 private static final long REDUCTION_FACTOR = 4096;
135
136 /**
137 * Average CTF event size, used to estimate the trace size. (Inspired by
138 * empirical observations with LTTng kernel traces, to avoid hanging at 100%
139 * for too long)
140 *
141 * TODO: Find a more suitable approximation, perhaps per concrete trace type
142 * or per trace directly with the metadata
143 */
144 private static final int CTF_AVG_EVENT_SIZE = 16;
145
146 // -------------------------------------------
147 // Fields
148 // -------------------------------------------
149
150 private final Map<@NonNull String, @NonNull CtfTmfEventType> fContainedEventTypes = Collections.synchronizedMap(new HashMap<>());
151
152 private final CtfIteratorManager fIteratorManager = new CtfIteratorManager(this);
153
154 private final @NonNull CtfTmfEventFactory fEventFactory;
155
156 /** Reference to the CTF Trace */
157 private CTFTrace fTrace;
158
159 // -------------------------------------------
160 // Constructor
161 // -------------------------------------------
162
163 /**
164 * Default constructor
165 */
166 public CtfTmfTrace() {
167 super();
168
169 /* Use default event factory */
170 fEventFactory = CtfTmfEventFactory.instance();
171 }
172
173 /**
174 * Constructor for sub-classes to specify their own event factory.
175 *
176 * @param eventFactory
177 * The event factory to use to generate trace events
178 * @since 2.0
179 */
180 protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory) {
181 super();
182 fEventFactory = eventFactory;
183 }
184
185 // -------------------------------------------
186 // TmfTrace Overrides
187 // -------------------------------------------
188 /**
189 * Method initTrace.
190 *
191 * @param resource
192 * The resource associated with this trace
193 * @param path
194 * The path to the trace file
195 * @param eventType
196 * The type of events that will be read from this trace
197 * @throws TmfTraceException
198 * If something went wrong while reading the trace
199 */
200 @Override
201 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
202 throws TmfTraceException {
203 /*
204 * Set the cache size. This has to be done before the call to super()
205 * because the super needs to know the cache size.
206 */
207 setCacheSize();
208
209 super.initTrace(resource, path, eventType);
210
211 try {
212 this.fTrace = new CTFTrace(path);
213 CtfTmfContext ctx;
214 /* Set the start and (current) end times for this trace */
215 ctx = (CtfTmfContext) seekEvent(0L);
216 CtfTmfEvent event = getNext(ctx);
217 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
218 /* Handle the case where the trace is empty */
219 this.setStartTime(TmfTimestamp.BIG_BANG);
220 } else {
221 final ITmfTimestamp curTime = event.getTimestamp();
222 this.setStartTime(curTime);
223 this.setEndTime(curTime);
224 }
225 /*
226 * Register every event type. When you call getType, it will
227 * register a trace to that type in the TmfEventTypeManager
228 */
229 try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
230 Set<@NonNull ITmfEventField> streamContextNames = new HashSet<>();
231 for (IEventDeclaration ied : iter.getEventDeclarations()) {
232 CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
233 if (ctfTmfEventType == null) {
234 List<ITmfEventField> content = new ArrayList<>();
235
236 /* Should only return null the first time */
237 final StructDeclaration fields = ied.getFields();
238 if (fields != null) {
239 for (String fieldName : fields.getFieldsList()) {
240 content.add(new TmfEventField(checkNotNull(fieldName), null, null));
241 }
242 }
243
244 /* Add stream contexts */
245 final StructDeclaration streamContexts = ied.getStream().getEventContextDecl();
246 if (streamContextNames.isEmpty()) {
247 if (streamContexts != null) {
248 for (String fieldName : streamContexts.getFieldsList()) {
249 streamContextNames.add(new TmfEventField(checkNotNull(CtfConstants.CONTEXT_FIELD_PREFIX + fieldName), null, null));
250 }
251 }
252 }
253 content.addAll(streamContextNames);
254
255 if (!content.isEmpty()) {
256 ITmfEventField contentTree = new TmfEventField(
257 ITmfEventField.ROOT_FIELD_ID,
258 null,
259 content.toArray(new ITmfEventField[content.size()]));
260
261 ctfTmfEventType = new CtfTmfEventType(checkNotNull(ied.getName()), contentTree);
262 fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
263 }
264 }
265 }
266 }
267 ctx.dispose();
268 } catch (final CTFException e) {
269 /*
270 * If it failed at the init(), we can assume it's because the file
271 * was not found or was not recognized as a CTF trace. Throw into
272 * the new type of exception expected by the rest of TMF.
273 */
274 throw new TmfTraceException(e.getMessage(), e);
275 }
276 }
277
278 @Override
279 public synchronized void dispose() {
280 fIteratorManager.dispose();
281 if (fTrace != null) {
282 fTrace = null;
283 }
284 super.dispose();
285 }
286
287 /**
288 * {@inheritDoc}
289 * <p>
290 * The default implementation of a CTF trace.
291 *
292 * Firstly a weak validation of the metadata is done to determine if the
293 * path is actually for a CTF trace. After that a full validation is done.
294 *
295 * If the weak and full validation are successful the confidence is set to
296 * 10.
297 *
298 * If the weak validation was successful, but the full validation fails a
299 * TraceValidationStatus with severity warning and confidence of 1 is
300 * returned.
301 *
302 * If both weak and full validation fails an error status is returned.
303 */
304 @Override
305 public IStatus validate(final IProject project, final String path) {
306 boolean isMetadataFile = false;
307 try {
308 isMetadataFile = Metadata.preValidate(path);
309 } catch (final CTFException e) {
310 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
311 }
312
313 if (isMetadataFile) {
314 // Trace is pre-validated, continue will full validation
315 try {
316 final CTFTrace trace = new CTFTrace(path);
317 if (!trace.majorIsSet()) {
318 if (isMetadataFile) {
319 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null);
320 }
321 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
322 }
323
324 // Validate using reader initialization
325 try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {
326 // do nothing
327 }
328
329 // Trace is validated, return with confidence
330 return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
331
332 } catch (final CTFException | BufferOverflowException e) {
333 // return warning since it's a CTF trace but with errors in it
334 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
335 }
336 }
337 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
338 }
339
340 @Override
341 public Iterable<ITmfEventAspect<?>> getEventAspects() {
342 return CTF_ASPECTS;
343 }
344
345 /**
346 * Method getCurrentLocation. This is not applicable in CTF
347 *
348 * @return null, since the trace has no knowledge of the current location
349 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
350 */
351 @Override
352 public ITmfLocation getCurrentLocation() {
353 return null;
354 }
355
356 @Override
357 public double getLocationRatio(ITmfLocation location) {
358 final CtfLocation curLocation = (CtfLocation) location;
359 final long startTime = getStartTime().getValue();
360 final double diff = curLocation.getLocationInfo().getTimestamp() - startTime;
361 final double total = getEndTime().getValue() - startTime;
362 return Math.max(0.0, Math.min(1.0, diff / total));
363 }
364
365 /**
366 * Method seekEvent.
367 *
368 * @param location
369 * ITmfLocation<?>
370 * @return ITmfContext
371 */
372 @Override
373 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
374 CtfLocation currentLocation = (CtfLocation) location;
375 CtfTmfContext context = new CtfTmfContext(this);
376 if (fTrace == null) {
377 context.setLocation(null);
378 context.setRank(ITmfContext.UNKNOWN_RANK);
379 return context;
380 }
381 /*
382 * The rank is set to 0 if the iterator seeks the beginning. If not, it
383 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
384 * by rank for now.
385 */
386 if (currentLocation == null) {
387 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
388 context.setRank(0);
389 } else {
390 context.setRank(ITmfContext.UNKNOWN_RANK);
391 }
392 /* This will seek and update the location after the seek */
393 context.setLocation(currentLocation);
394 return context;
395 }
396
397 @Override
398 public synchronized ITmfContext seekEvent(double ratio) {
399 CtfTmfContext context = new CtfTmfContext(this);
400 if (fTrace == null) {
401 context.setLocation(null);
402 context.setRank(ITmfContext.UNKNOWN_RANK);
403 return context;
404 }
405 final long end = getEndTime().getValue();
406 final long start = getStartTime().getValue();
407 final long diff = end - start;
408 final long ratioTs = Math.round(diff * ratio) + start;
409 context.seek(ratioTs);
410 context.setRank(ITmfContext.UNKNOWN_RANK);
411 return context;
412 }
413
414 /**
415 * Method readNextEvent.
416 *
417 * @param context
418 * ITmfContext
419 * @return CtfTmfEvent
420 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
421 */
422 @Override
423 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
424 if (fTrace == null) {
425 return null;
426 }
427 CtfTmfEvent event = null;
428 if (context instanceof CtfTmfContext) {
429 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
430 return null;
431 }
432 CtfTmfContext ctfContext = (CtfTmfContext) context;
433 event = ctfContext.getCurrentEvent();
434
435 if (event != null) {
436 updateAttributes(context, event);
437 ctfContext.advance();
438 ctfContext.increaseRank();
439 }
440 }
441
442 return event;
443 }
444
445 /**
446 * Ctf traces have a clock with a unique uuid that will be used to identify
447 * the host. Traces with the same clock uuid will be known to have been made
448 * on the same machine.
449 *
450 * Note: uuid is an optional field, it may not be there for a clock.
451 */
452 @Override
453 public String getHostId() {
454 CTFClock clock = fTrace.getClock();
455 if (clock != null) {
456 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
457 if (clockHost != null) {
458 return clockHost;
459 }
460 }
461 return super.getHostId();
462 }
463
464 /**
465 * Get the first callsite that matches the event name
466 *
467 * @param eventName
468 * The event name to look for
469 * @return The best callsite candidate
470 * @since 2.2
471 */
472 public @Nullable CtfTmfCallsite getCallsite(String eventName) {
473 for (ICTFStream stream : fTrace.getStreams()) {
474 for (IEventDeclaration eventDeclaration : stream.getEventDeclarations()) {
475 if (eventDeclaration != null && Objects.equals(eventDeclaration.getName(), eventName) &&
476 !eventDeclaration.getCallsites().isEmpty()) {
477 return new CtfTmfCallsite(eventDeclaration.getCallsites().get(0));
478 }
479 }
480 }
481 return null;
482 }
483
484 /**
485 * Get the closest matching callsite for given event name and instruction
486 * pointer
487 *
488 * @param eventName
489 * The event name
490 * @param ip
491 * The instruction pointer
492 * @return The closest matching callsite
493 * @since 2.2
494 */
495 public @Nullable CtfTmfCallsite getCallsite(String eventName, long ip) {
496 CtfTmfCallsite callsite = null;
497 for (ICTFStream stream : fTrace.getStreams()) {
498 for (IEventDeclaration eventDeclaration : stream.getEventDeclarations()) {
499 if (eventDeclaration != null && Objects.equals(eventDeclaration.getName(), eventName)) {
500 for (CTFCallsite cs : eventDeclaration.getCallsites()) {
501 if (cs.getIp() >= ip && (callsite == null || cs.getIp() < callsite.getIntructionPointer())) {
502 callsite = new CtfTmfCallsite(cs);
503 }
504 }
505 }
506 }
507 }
508 return callsite;
509 }
510
511 /**
512 * Get the CTF environment variables defined in this CTF trace, in <name,
513 * value> form. This comes from the trace's CTF metadata.
514 *
515 * @return The CTF environment
516 */
517 public Map<String, String> getEnvironment() {
518 return fTrace.getEnvironment();
519 }
520
521 // -------------------------------------------
522 // ITmfPropertiesProvider
523 // -------------------------------------------
524
525 /**
526 * @since 2.0
527 */
528 @Override
529 public Map<String, String> getProperties() {
530 Map<String, String> properties = new HashMap<>();
531 properties.putAll(fTrace.getEnvironment());
532 properties.put(CLOCK_OFFSET, Long.toUnsignedString(fTrace.getOffset()));
533 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
534 return properties;
535 }
536
537 // -------------------------------------------
538 // Clocks
539 // -------------------------------------------
540
541 /**
542 * Gets the clock offset with respect to POSIX.1 Epoch in cycles
543 *
544 * @return the clock offset with respect to POSIX.1 Epoch in cycles
545 */
546 public long getOffset() {
547 if (fTrace != null) {
548 return fTrace.getOffset();
549 }
550 return 0;
551 }
552
553 /**
554 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
555 * for this trace.
556 *
557 * @param cycles
558 * The timestamp in cycles, relative to the clock offset
559 * @return The timestamp in nanoseconds, relative to POSIX.1 Epoch
560 */
561 public long timestampCyclesToNanos(long cycles) {
562 return fTrace.timestampCyclesToNanos(cycles);
563 }
564
565 /**
566 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
567 * for this trace.
568 *
569 * @param nanos
570 * The timestamp in nanoseconds, relative to POSIX.1 Epoch
571 * @return The timestamp in cycles, relative to the clock offset
572 */
573 public long timestampNanoToCycles(long nanos) {
574 return fTrace.timestampNanoToCycles(nanos);
575 }
576
577 /**
578 * Gets the list of declared events
579 */
580 @Override
581 public Set<@NonNull CtfTmfEventType> getContainedEventTypes() {
582 return ImmutableSet.copyOf(fContainedEventTypes.values());
583 }
584
585 /**
586 * Register an event type to this trace.
587 *
588 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
589 *
590 * FIXME This could probably be made cleaner?
591 *
592 * @param eventType
593 * The event type to register
594 */
595 public void registerEventType(CtfTmfEventType eventType) {
596 fContainedEventTypes.put(eventType.getName(), eventType);
597 }
598
599 // -------------------------------------------
600 // Parser
601 // -------------------------------------------
602
603 @Override
604 public CtfTmfEvent parseEvent(ITmfContext context) {
605 CtfTmfEvent event = null;
606 if (context instanceof CtfTmfContext) {
607 final ITmfContext tmpContext = seekEvent(context.getLocation());
608 event = getNext(tmpContext);
609 }
610 return event;
611 }
612
613 /**
614 * Sets the cache size for a CtfTmfTrace.
615 */
616 protected void setCacheSize() {
617 setCacheSize(DEFAULT_CACHE_SIZE);
618 }
619
620 // -------------------------------------------
621 // CtfIterator factory methods
622 // -------------------------------------------
623
624 /**
625 * Get the event factory for this trace to generate new events for it.
626 *
627 * @return The event factory
628 * @since 2.0
629 */
630 public @NonNull CtfTmfEventFactory getEventFactory() {
631 return fEventFactory;
632 }
633
634 /**
635 * Get an iterator to the trace
636 *
637 * @return an iterator to the trace
638 */
639 public ITmfContext createIterator() {
640 try {
641 return new CtfIterator(fTrace, this);
642 } catch (CTFException e) {
643 Activator.getDefault().logError(e.getMessage(), e);
644 }
645 return null;
646 }
647
648 /**
649 * Get an iterator to the trace, , which will initially point to the given
650 * location/rank.
651 *
652 * @param ctfLocationData
653 * The initial timestamp the iterator will be pointing to
654 * @param rank
655 * The initial rank
656 * @return The new iterator
657 */
658 public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
659 try {
660 return new CtfIterator(fTrace, this, ctfLocationData, rank);
661 } catch (CTFException e) {
662 Activator.getDefault().logError(e.getMessage(), e);
663 }
664 return null;
665 }
666
667 /**
668 * Create the 'CtfIterator' object from a CtfTmfContext.
669 *
670 * @param context
671 * The iterator will initially be pointing to this context
672 * @return A new CtfIterator object
673 * @since 1.0
674 */
675 public ITmfContext createIteratorFromContext(CtfTmfContext context) {
676 return fIteratorManager.getIterator(context);
677 }
678
679 /**
680 * Dispose an iterator that was create with
681 * {@link #createIteratorFromContext}
682 *
683 * @param context
684 * The last context that was pointed to by the iterator (this is
685 * the 'key' to find the correct iterator to dispose).
686 * @since 1.0
687 */
688 public void disposeContext(CtfTmfContext context) {
689 fIteratorManager.removeIterator(context);
690 }
691
692 // ------------------------------------------------------------------------
693 // Timestamp transformation functions
694 // ------------------------------------------------------------------------
695
696 /**
697 * @since 1.0
698 */
699 @Override
700 public @NonNull ITmfTimestamp createTimestamp(long ts) {
701 return TmfTimestamp.fromNanos(getTimestampTransform().transform(ts));
702 }
703
704 private static int fCheckpointSize = -1;
705
706 @Override
707 public synchronized int getCheckpointSize() {
708 if (fCheckpointSize == -1) {
709 TmfCheckpoint c = new TmfCheckpoint(TmfTimestamp.fromNanos(0), new CtfLocation(0, 0), 0);
710 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
711 b.clear();
712 c.serialize(b);
713 fCheckpointSize = b.position();
714 }
715
716 return fCheckpointSize;
717 }
718
719 @Override
720 protected ITmfTraceIndexer createIndexer(int interval) {
721 return new TmfBTreeTraceIndexer(this, interval);
722 }
723
724 @Override
725 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
726 return new CtfLocation(bufferIn);
727 }
728
729 @Override
730 public boolean isComplete() {
731 if (getResource() == null) {
732 return true;
733 }
734
735 String host = null;
736 String port = null;
737 String sessionName = null;
738 try {
739 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
740 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
741 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
742 } catch (CoreException e) {
743 Activator.getDefault().logError(e.getMessage(), e);
744 // Something happened to the resource, assume we won't get any more
745 // data from it
746 return true;
747 }
748 return host == null || port == null || sessionName == null;
749 }
750
751 @Override
752 public void setComplete(final boolean isComplete) {
753 super.setComplete(isComplete);
754 try {
755 if (isComplete) {
756 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
757 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
758 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
759 }
760 } catch (CoreException e) {
761 Activator.getDefault().logError(e.getMessage(), e);
762 }
763 }
764
765 /**
766 * @return the number of estimated chunks of events read. This reads the
767 * file size of the trace and divides it by a factor and the average
768 * event size, this is not accurate but can give a ball park figure
769 * of how much is done.
770 * @since 2.1
771 */
772 @Override
773 public int size() {
774 long size = 0;
775 CTFTrace trace = fTrace;
776 if (trace != null) {
777 Iterable<ICTFStream> streams = trace.getStreams();
778 for (ICTFStream stream : streams) {
779 for (CTFStreamInput si : stream.getStreamInputs()) {
780 size += si.getFile().length();
781 }
782 }
783 }
784 return (int) (size / REDUCTION_FACTOR / CTF_AVG_EVENT_SIZE);
785 }
786
787 /**
788 * @return the number of events divided a reduction factor. Is monotonic.
789 * @since 2.1
790 */
791 @Override
792 public int progress() {
793 return (int) (getNbEvents() / REDUCTION_FACTOR);
794 }
795
796 /**
797 * @since 2.2
798 */
799 @Override
800 public long cyclesToNanos(long cycles) {
801 // CTFTrace adds the clock offset in cycles to the input
802 return fTrace.timestampCyclesToNanos(cycles - fTrace.getOffset());
803 }
804
805 /**
806 * @since 2.2
807 */
808 @Override
809 public long nanosToCycles(long nanos) {
810 // CTFTrace subtracts the clock offset in cycles from the output
811 return fTrace.timestampNanoToCycles(nanos) + fTrace.getOffset();
812 }
813
814 /**
815 * @since 2.2
816 */
817 @Override
818 public ITmfTimestamp readStart() {
819 return getStartTime();
820 }
821
822 /**
823 * @since 2.2
824 */
825 @Override
826 public ITmfTimestamp readEnd() {
827 try (CTFTraceReader reader = new CTFTraceReader(fTrace)) {
828 reader.goToLastEvent();
829 long end = reader.getEndTime();
830 return createTimestamp(end);
831 } catch (CTFException e) {
832 return null;
833 }
834 }
835 }
This page took 0.049218 seconds and 5 git commands to generate.