Remove unneeded checkNotNull() calls
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / trace / CtfTmfTrace.java
CommitLineData
b1baa808 1/*******************************************************************************
da707390 2 * Copyright (c) 2012, 2015 Ericsson, École Polytechnique de Montréal
b1baa808
MK
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 *
ea271da6
PT
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 * Patrick Tasse - Updated for removal of context clone
e73a4ba5 12 * Geneviève Bastien - Added the createTimestamp function
b1baa808
MK
13 *******************************************************************************/
14
9722e5d7 15package org.eclipse.tracecompass.tmf.ctf.core.trace;
a3fc8213 16
5db5a3a4
AM
17import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
18
96c0f2af 19import java.nio.BufferOverflowException;
032ecd45 20import java.nio.ByteBuffer;
409bea20 21import java.util.ArrayList;
f9ff7d40 22import java.util.Collection;
e600c338 23import java.util.Collections;
2db2b43a 24import java.util.HashMap;
409bea20 25import java.util.List;
299e494e 26import java.util.Map;
409bea20 27import java.util.Set;
299e494e 28
a3fc8213
AM
29import org.eclipse.core.resources.IProject;
30import org.eclipse.core.resources.IResource;
6fd3c6e9 31import org.eclipse.core.runtime.CoreException;
a94410d9
MK
32import org.eclipse.core.runtime.IStatus;
33import org.eclipse.core.runtime.Status;
8192209b 34import org.eclipse.jdt.annotation.NonNull;
680f9173 35import org.eclipse.tracecompass.ctf.core.CTFException;
f357bcd4
AM
36import org.eclipse.tracecompass.ctf.core.event.CTFClock;
37import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
0c2409c1 38import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
f357bcd4
AM
39import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
40import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
d16bb0dd 41import org.eclipse.tracecompass.ctf.core.trace.Metadata;
2bdf0193 42import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
fe71057b
AM
43import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIterator;
44import org.eclipse.tracecompass.internal.tmf.ctf.core.trace.iterator.CtfIteratorManager;
2bdf0193
AM
45import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
46import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
2bdf0193 47import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
f9ff7d40 48import org.eclipse.tracecompass.tmf.core.event.aspect.ITmfEventAspect;
2bdf0193 49import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
234838b2 50import org.eclipse.tracecompass.tmf.core.project.model.ITmfPropertiesProvider;
2bdf0193 51import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
da707390 52import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
2bdf0193
AM
53import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
54import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
2bdf0193
AM
55import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
56import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
d16bb0dd 57import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
2bdf0193
AM
58import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
59import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
60import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
61import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
62import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
63import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
9722e5d7
AM
64import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
65import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
66import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
67import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext;
68import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
8e376474 69import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventFactory;
9722e5d7 70import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
f9ff7d40
AM
71import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfChannelAspect;
72import org.eclipse.tracecompass.tmf.ctf.core.event.aspect.CtfCpuAspect;
a3fc8213 73
f9ff7d40 74import com.google.common.collect.ImmutableList;
409bea20
GB
75import com.google.common.collect.ImmutableSet;
76
9ac2eb62 77/**
d09f973b
FC
78 * The CTf trace handler
79 *
80 * @version 1.0
81 * @author Matthew khouzam
9ac2eb62 82 */
22307af3 83public class CtfTmfTrace extends TmfTrace
234838b2 84 implements ITmfPropertiesProvider, ITmfPersistentlyIndexable,
0ff9e595 85 ITmfTraceWithPreDefinedEvents {
a3fc8213 86
a94410d9
MK
87 // -------------------------------------------
88 // Constants
89 // -------------------------------------------
f9ff7d40 90
06a93628
MK
91 /**
92 * Clock offset property
93 * @since 2.0
94 */
95 public static final String CLOCK_OFFSET = "clock_offset"; //$NON-NLS-1$
96
324a6a4a
BH
97 /**
98 * Default cache size for CTF traces
99 */
100 protected static final int DEFAULT_CACHE_SIZE = 50000;
64c2cb4c 101
f9ff7d40
AM
102 /**
103 * Event aspects available for all CTF traces
dbc7991d 104 * @since 1.0
f9ff7d40 105 */
df2597e0 106 protected static final @NonNull Collection<@NonNull ITmfEventAspect> CTF_ASPECTS =
0e4f957e 107 ImmutableList.of(
f9ff7d40
AM
108 ITmfEventAspect.BaseAspects.TIMESTAMP,
109 new CtfChannelAspect(),
110 new CtfCpuAspect(),
111 ITmfEventAspect.BaseAspects.EVENT_TYPE,
112 ITmfEventAspect.BaseAspects.CONTENTS
0e4f957e 113 );
f9ff7d40
AM
114
115 /**
bb52f9bc
GB
116 * The Ctf clock unique identifier field
117 */
118 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
cd43d683 119 private static final int CONFIDENCE = 10;
d16bb0dd 120 private static final int MIN_CONFIDENCE = 1;
bb52f9bc 121
a94410d9
MK
122 // -------------------------------------------
123 // Fields
124 // -------------------------------------------
a3fc8213 125
df2597e0
AM
126 private final Map<@NonNull String, @NonNull CtfTmfEventType> fContainedEventTypes =
127 Collections.synchronizedMap(new HashMap<>());
e600c338 128
df2597e0 129 private final CtfIteratorManager fIteratorManager = new CtfIteratorManager(this);
6a0ec7bc 130
8e376474
AM
131 private final @NonNull CtfTmfEventFactory fEventFactory;
132
133 /** Reference to the CTF Trace */
4b7c469f 134 private CTFTrace fTrace;
a3fc8213 135
8e376474
AM
136 // -------------------------------------------
137 // Constructor
138 // -------------------------------------------
139
140 /**
141 * Default constructor
142 */
143 public CtfTmfTrace() {
144 super();
145
146 /* Use default event factory */
147 fEventFactory = CtfTmfEventFactory.instance();
148 }
149
150 /**
151 * Constructor for sub-classes to specify their own event factory.
152 *
153 * @param eventFactory
154 * The event factory to use to generate trace events
155 * @since 2.0
156 */
157 protected CtfTmfTrace(@NonNull CtfTmfEventFactory eventFactory) {
158 super();
159 fEventFactory = eventFactory;
160 }
161
a94410d9
MK
162 // -------------------------------------------
163 // TmfTrace Overrides
164 // -------------------------------------------
b1baa808
MK
165 /**
166 * Method initTrace.
063f0d27
AM
167 *
168 * @param resource
169 * The resource associated with this trace
170 * @param path
171 * The path to the trace file
172 * @param eventType
173 * The type of events that will be read from this trace
b1baa808 174 * @throws TmfTraceException
07804639 175 * If something went wrong while reading the trace
b1baa808 176 */
a3fc8213 177 @Override
6256d8ad 178 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
b4f71e4a 179 throws TmfTraceException {
4a110860
AM
180 /*
181 * Set the cache size. This has to be done before the call to super()
182 * because the super needs to know the cache size.
183 */
184 setCacheSize();
324a6a4a 185
32c16b50
GB
186 super.initTrace(resource, path, eventType);
187
a3fc8213
AM
188 try {
189 this.fTrace = new CTFTrace(path);
81a2d02e 190 CtfTmfContext ctx;
99b483fe 191 /* Set the start and (current) end times for this trace */
81a2d02e 192 ctx = (CtfTmfContext) seekEvent(0L);
132a02b0 193 CtfTmfEvent event = getNext(ctx);
a94410d9 194 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
99b483fe
AM
195 /* Handle the case where the trace is empty */
196 this.setStartTime(TmfTimestamp.BIG_BANG);
197 } else {
132a02b0 198 final ITmfTimestamp curTime = event.getTimestamp();
21fb02fa
MK
199 this.setStartTime(curTime);
200 this.setEndTime(curTime);
99b483fe 201 }
409bea20
GB
202 /*
203 * Register every event type. When you call getType, it will
204 * register a trace to that type in the TmfEventTypeManager
205 */
6a0ec7bc 206 try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
409bea20 207 for (IEventDeclaration ied : iter.getEventDeclarations()) {
e600c338 208 CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
409bea20
GB
209 if (ctfTmfEventType == null) {
210 List<ITmfEventField> content = new ArrayList<>();
211 /* Should only return null the first time */
0c2409c1
MK
212 final StructDeclaration fields = ied.getFields();
213 if (fields != null) {
214 for (String fieldName : fields.getFieldsList()) {
215 content.add(new TmfEventField(checkNotNull(fieldName), null, null));
216 }
217 ITmfEventField contentTree = new TmfEventField(
218 ITmfEventField.ROOT_FIELD_ID,
219 null,
220 content.toArray(new ITmfEventField[content.size()]));
221
aa353506 222 ctfTmfEventType = new CtfTmfEventType(checkNotNull(ied.getName()), contentTree);
0c2409c1 223 fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
409bea20 224 }
409bea20
GB
225 }
226 }
227 }
dd5615b7 228 ctx.dispose();
680f9173 229 } catch (final CTFException e) {
a3fc8213
AM
230 /*
231 * If it failed at the init(), we can assume it's because the file
232 * was not found or was not recognized as a CTF trace. Throw into
233 * the new type of exception expected by the rest of TMF.
234 */
9fa32496 235 throw new TmfTraceException(e.getMessage(), e);
a3fc8213 236 }
a3fc8213
AM
237 }
238
53b235e1
MK
239 @Override
240 public synchronized void dispose() {
6a0ec7bc 241 fIteratorManager.dispose();
5d1c6919 242 if (fTrace != null) {
5d1c6919
PT
243 fTrace = null;
244 }
53b235e1
MK
245 super.dispose();
246 }
247
b1baa808 248 /**
cd43d683
PT
249 * {@inheritDoc}
250 * <p>
d16bb0dd
BH
251 * The default implementation of a CTF trace.
252 *
253 * Firstly a weak validation of the metadata is done to determine if the
254 * path is actually for a CTF trace. After that a full validation is done.
255 *
256 * If the weak and full validation are successful the confidence is set
257 * to 10.
258 *
259 * If the weak validation was successful, but the full validation fails
260 * a TraceValidationStatus with severity warning and confidence of 1 is
261 * returned.
262 *
263 * If both weak and full validation fails an error status is returned.
b1baa808 264 */
a3fc8213 265 @Override
a94410d9 266 public IStatus validate(final IProject project, final String path) {
f841e903 267 boolean isMetadataFile = false;
b562a24f 268 try {
f841e903 269 isMetadataFile = Metadata.preValidate(path);
d16bb0dd
BH
270 } catch (final CTFException e) {
271 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
272 }
273
f841e903 274 if (isMetadataFile) {
d16bb0dd
BH
275 // Trace is pre-validated, continue will full validation
276 try {
277 final CTFTrace trace = new CTFTrace(path);
278 if (!trace.majorIsSet()) {
f841e903 279 if (isMetadataFile) {
d16bb0dd
BH
280 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet, null);
281 }
282 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
b5354daa 283 }
d16bb0dd
BH
284
285 // Validate using reader initialization
286 try (CTFTraceReader ctfTraceReader = new CTFTraceReader(trace)) {}
287
288 // Trace is validated, return with confidence
289 return new CtfTraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID, trace.getEnvironment());
290
f841e903
BH
291 } catch (final CTFException | BufferOverflowException e ) {
292 // return warning since it's a CTF trace but with errors in it
293 return new TraceValidationStatus(MIN_CONFIDENCE, IStatus.WARNING, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString(), e); //$NON-NLS-1$
a94410d9 294 }
a3fc8213 295 }
d16bb0dd 296 return new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError);
a3fc8213
AM
297 }
298
f9ff7d40
AM
299 @Override
300 public Iterable<ITmfEventAspect> getEventAspects() {
301 return CTF_ASPECTS;
302 }
303
b1baa808 304 /**
f474d36b 305 * Method getCurrentLocation. This is not applicable in CTF
a94410d9 306 *
f474d36b 307 * @return null, since the trace has no knowledge of the current location
2bdf0193 308 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
b1baa808 309 */
a3fc8213 310 @Override
1e1bef82 311 public ITmfLocation getCurrentLocation() {
f474d36b 312 return null;
a3fc8213
AM
313 }
314
a3fc8213 315 @Override
1e1bef82 316 public double getLocationRatio(ITmfLocation location) {
4b7c469f 317 final CtfLocation curLocation = (CtfLocation) location;
4a1f6c41
PT
318 final long startTime = getStartTime().getValue();
319 final double diff = curLocation.getLocationInfo().getTimestamp() - startTime;
320 final double total = getEndTime().getValue() - startTime;
321 return Math.max(0.0, Math.min(1.0, diff / total));
a3fc8213
AM
322 }
323
b1baa808
MK
324 /**
325 * Method seekEvent.
a94410d9
MK
326 *
327 * @param location
328 * ITmfLocation<?>
b1baa808 329 * @return ITmfContext
b1baa808 330 */
a3fc8213 331 @Override
76643eb7 332 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
ce2388e0 333 CtfLocation currentLocation = (CtfLocation) location;
81a2d02e 334 CtfTmfContext context = new CtfTmfContext(this);
76643eb7
BH
335 if (fTrace == null) {
336 context.setLocation(null);
337 context.setRank(ITmfContext.UNKNOWN_RANK);
338 return context;
339 }
4a110860
AM
340 /*
341 * The rank is set to 0 if the iterator seeks the beginning. If not, it
342 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
343 * by rank for now.
344 */
11d6f468 345 if (currentLocation == null) {
f5df94f8 346 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
4a110860 347 context.setRank(0);
544c79ad 348 } else {
3bd44ac8 349 context.setRank(ITmfContext.UNKNOWN_RANK);
64c2cb4c 350 }
544c79ad
PT
351 /* This will seek and update the location after the seek */
352 context.setLocation(currentLocation);
f474d36b 353 return context;
a3fc8213
AM
354 }
355
a3fc8213 356 @Override
76643eb7 357 public synchronized ITmfContext seekEvent(double ratio) {
81a2d02e 358 CtfTmfContext context = new CtfTmfContext(this);
76643eb7
BH
359 if (fTrace == null) {
360 context.setLocation(null);
361 context.setRank(ITmfContext.UNKNOWN_RANK);
362 return context;
363 }
4a1f6c41
PT
364 final long end = getEndTime().getValue();
365 final long start = getStartTime().getValue();
b2dc9e02 366 final long diff = end - start;
15e89960 367 final long ratioTs = Math.round(diff * ratio) + start;
b2dc9e02 368 context.seek(ratioTs);
f474d36b
PT
369 context.setRank(ITmfContext.UNKNOWN_RANK);
370 return context;
a3fc8213
AM
371 }
372
b1baa808
MK
373 /**
374 * Method readNextEvent.
a94410d9
MK
375 *
376 * @param context
377 * ITmfContext
b1baa808 378 * @return CtfTmfEvent
2bdf0193 379 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
b1baa808 380 */
a3fc8213 381 @Override
4b7c469f 382 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
faa38350
PT
383 if (fTrace == null) {
384 return null;
385 }
f474d36b 386 CtfTmfEvent event = null;
81a2d02e 387 if (context instanceof CtfTmfContext) {
575beffc 388 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
ae09313d
PT
389 return null;
390 }
81a2d02e 391 CtfTmfContext ctfContext = (CtfTmfContext) context;
788ddcbc 392 event = ctfContext.getCurrentEvent();
4a110860 393
324a6a4a 394 if (event != null) {
5904c11e 395 updateAttributes(context, event);
788ddcbc
MK
396 ctfContext.advance();
397 ctfContext.increaseRank();
324a6a4a 398 }
f474d36b 399 }
4a110860 400
aa572e22 401 return event;
a3fc8213
AM
402 }
403
bb52f9bc
GB
404 /**
405 * Ctf traces have a clock with a unique uuid that will be used to identify
406 * the host. Traces with the same clock uuid will be known to have been made
407 * on the same machine.
408 *
409 * Note: uuid is an optional field, it may not be there for a clock.
410 */
411 @Override
412 public String getHostId() {
fe71057b 413 CTFClock clock = fTrace.getClock();
bb52f9bc
GB
414 if (clock != null) {
415 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
416 if (clockHost != null) {
417 return clockHost;
418 }
419 }
420 return super.getHostId();
421 }
422
fe71057b
AM
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
a94410d9 433 // -------------------------------------------
234838b2 434 // ITmfPropertiesProvider
a94410d9 435 // -------------------------------------------
4b7c469f 436
234838b2
GB
437 /**
438 * @since 2.0
439 */
22307af3 440 @Override
234838b2 441 public Map<String, String> getProperties() {
2db2b43a
GB
442 Map<String, String> properties = new HashMap<>();
443 properties.putAll(fTrace.getEnvironment());
06a93628 444 properties.put(CLOCK_OFFSET, Long.toUnsignedString(fTrace.getOffset()));
2db2b43a
GB
445 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
446 return properties;
4b7c469f
MK
447 }
448
a94410d9
MK
449 // -------------------------------------------
450 // Clocks
451 // -------------------------------------------
bfe038ff 452
9ac2eb62
MK
453 /**
454 * gets the clock offset
a94410d9 455 *
9ac2eb62
MK
456 * @return the clock offset in ns
457 */
a94410d9
MK
458 public long getOffset() {
459 if (fTrace != null) {
bfe038ff
MK
460 return fTrace.getOffset();
461 }
462 return 0;
463 }
464
fe71057b
AM
465 /**
466 * Convert a CTF timestamp in CPU cycles to its equivalent in nanoseconds
467 * for this trace.
468 *
469 * @param cycles
470 * The timestamp in cycles
471 * @return The timestamp in nanoseconds
472 */
473 public long timestampCyclesToNanos(long cycles) {
474 return fTrace.timestampCyclesToNanos(cycles);
475 }
476
477 /**
478 * Convert a CTF timestamp in nanoseconds to its equivalent in CPU cycles
479 * for this trace.
480 *
481 * @param nanos
482 * The timestamp in nanoseconds
483 * @return The timestamp in cycles
484 */
485 public long timestampNanoToCycles(long nanos) {
486 return fTrace.timestampNanoToCycles(nanos);
487 }
488
3480bf12 489 /**
409bea20 490 * Gets the list of declared events
3480bf12 491 */
409bea20 492 @Override
df2597e0 493 public Set<@NonNull CtfTmfEventType> getContainedEventTypes() {
e600c338
AM
494 return ImmutableSet.copyOf(fContainedEventTypes.values());
495 }
496
497 /**
498 * Register an event type to this trace.
499 *
9722e5d7 500 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
e600c338
AM
501 *
502 * FIXME This could probably be made cleaner?
9722e5d7
AM
503 *
504 * @param eventType
505 * The event type to register
e600c338 506 */
9722e5d7 507 public void registerEventType(CtfTmfEventType eventType) {
e600c338 508 fContainedEventTypes.put(eventType.getName(), eventType);
3480bf12
GB
509 }
510
a94410d9
MK
511 // -------------------------------------------
512 // Parser
513 // -------------------------------------------
4b7c469f
MK
514
515 @Override
bfe038ff 516 public CtfTmfEvent parseEvent(ITmfContext context) {
4b7c469f 517 CtfTmfEvent event = null;
ea271da6
PT
518 if (context instanceof CtfTmfContext) {
519 final ITmfContext tmpContext = seekEvent(context.getLocation());
520 event = getNext(tmpContext);
4b7c469f
MK
521 }
522 return event;
11d6f468 523 }
64c2cb4c 524
324a6a4a 525 /**
64c2cb4c 526 * Sets the cache size for a CtfTmfTrace.
324a6a4a
BH
527 */
528 protected void setCacheSize() {
529 setCacheSize(DEFAULT_CACHE_SIZE);
530 }
ce2388e0 531
a94410d9 532 // -------------------------------------------
fe71057b 533 // CtfIterator factory methods
a94410d9 534 // -------------------------------------------
53b235e1 535
8e376474
AM
536 /**
537 * Get the event factory for this trace to generate new events for it.
538 *
539 * @return The event factory
540 * @since 2.0
541 */
542 public @NonNull CtfTmfEventFactory getEventFactory() {
543 return fEventFactory;
544 }
545
36dd544c
MK
546 /**
547 * Get an iterator to the trace
548 *
549 * @return an iterator to the trace
550 */
fe71057b
AM
551 public ITmfContext createIterator() {
552 try {
553 return new CtfIterator(fTrace, this);
680f9173 554 } catch (CTFException e) {
fe71057b
AM
555 Activator.getDefault().logError(e.getMessage(), e);
556 }
557 return null;
558 }
559
560 /**
561 * Get an iterator to the trace, , which will initially point to the given
562 * location/rank.
563 *
564 * @param ctfLocationData
565 * The initial timestamp the iterator will be pointing to
566 * @param rank
567 * The initial rank
568 * @return The new iterator
569 */
570 public ITmfContext createIterator(CtfLocationInfo ctfLocationData, long rank) {
db8e8f7d 571 try {
fe71057b 572 return new CtfIterator(fTrace, this, ctfLocationData, rank);
680f9173 573 } catch (CTFException e) {
91e7f946 574 Activator.getDefault().logError(e.getMessage(), e);
db8e8f7d
AM
575 }
576 return null;
36dd544c 577 }
e73a4ba5 578
0451d4d7
AM
579 /**
580 * Create the 'CtfIterator' object from a CtfTmfContext.
581 *
582 * @param context
583 * The iterator will initially be pointing to this context
584 * @return A new CtfIterator object
585 * @since 1.0
586 */
587 public ITmfContext createIteratorFromContext(CtfTmfContext context) {
588 return fIteratorManager.getIterator(context);
589 }
590
591 /**
592 * Dispose an iterator that was create with
593 * {@link #createIteratorFromContext}
594 *
595 * @param context
596 * The last context that was pointed to by the iterator (this is
597 * the 'key' to find the correct iterator to dispose).
598 * @since 1.0
599 */
600 public void disposeContext(CtfTmfContext context) {
601 fIteratorManager.removeIterator(context);
602 }
603
e73a4ba5
GB
604 // ------------------------------------------------------------------------
605 // Timestamp transformation functions
606 // ------------------------------------------------------------------------
607
da707390
MK
608 /**
609 * @since 1.0
610 */
e73a4ba5 611 @Override
da707390
MK
612 public @NonNull TmfNanoTimestamp createTimestamp(long ts) {
613 return new TmfNanoTimestamp(getTimestampTransform().transform(ts));
e73a4ba5 614 }
032ecd45
MAL
615
616 private static int fCheckpointSize = -1;
617
618 @Override
619 public synchronized int getCheckpointSize() {
620 if (fCheckpointSize == -1) {
da707390 621 TmfCheckpoint c = new TmfCheckpoint(new TmfNanoTimestamp(0), new CtfLocation(0, 0), 0);
032ecd45
MAL
622 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
623 b.clear();
624 c.serialize(b);
625 fCheckpointSize = b.position();
626 }
627
628 return fCheckpointSize;
629 }
630
631 @Override
632 protected ITmfTraceIndexer createIndexer(int interval) {
633 return new TmfBTreeTraceIndexer(this, interval);
634 }
635
636 @Override
637 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
638 return new CtfLocation(bufferIn);
639 }
6fd3c6e9
MAL
640
641 @Override
642 public boolean isComplete() {
643 if (getResource() == null) {
644 return true;
645 }
646
647 String host = null;
648 String port = null;
649 String sessionName = null;
650 try {
651 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
652 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
653 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
654 } catch (CoreException e) {
655 Activator.getDefault().logError(e.getMessage(), e);
b562a24f
MK
656 // Something happened to the resource, assume we won't get any more
657 // data from it
6fd3c6e9
MAL
658 return true;
659 }
660 return host == null || port == null || sessionName == null;
661 }
662
663 @Override
664 public void setComplete(final boolean isComplete) {
665 super.setComplete(isComplete);
666 try {
667 if (isComplete) {
668 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
669 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
670 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
671 }
672 } catch (CoreException e) {
673 Activator.getDefault().logError(e.getMessage(), e);
674 }
675 }
a3fc8213 676}
This page took 0.151699 seconds and 5 git commands to generate.