tmf.ctf: Split the classes into proper packages
[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 java.nio.BufferOverflowException;
18 import java.nio.ByteBuffer;
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25
26 import org.eclipse.core.resources.IProject;
27 import org.eclipse.core.resources.IResource;
28 import org.eclipse.core.runtime.CoreException;
29 import org.eclipse.core.runtime.IStatus;
30 import org.eclipse.core.runtime.Status;
31 import org.eclipse.tracecompass.ctf.core.event.CTFClock;
32 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
33 import org.eclipse.tracecompass.ctf.core.trace.CTFReaderException;
34 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
35 import org.eclipse.tracecompass.ctf.core.trace.CTFTraceReader;
36 import org.eclipse.tracecompass.internal.tmf.ctf.core.Activator;
37 import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
38 import org.eclipse.tracecompass.tmf.core.event.ITmfEventField;
39 import org.eclipse.tracecompass.tmf.core.event.TmfEventField;
40 import org.eclipse.tracecompass.tmf.core.exceptions.TmfTraceException;
41 import org.eclipse.tracecompass.tmf.core.timestamp.ITmfTimestamp;
42 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimestamp;
43 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
44 import org.eclipse.tracecompass.tmf.core.trace.ITmfEventParser;
45 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceProperties;
46 import org.eclipse.tracecompass.tmf.core.trace.ITmfTraceWithPreDefinedEvents;
47 import org.eclipse.tracecompass.tmf.core.trace.TmfTrace;
48 import org.eclipse.tracecompass.tmf.core.trace.TraceValidationStatus;
49 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
50 import org.eclipse.tracecompass.tmf.core.trace.indexer.ITmfTraceIndexer;
51 import org.eclipse.tracecompass.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
52 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
53 import org.eclipse.tracecompass.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
54 import org.eclipse.tracecompass.tmf.core.trace.location.ITmfLocation;
55 import org.eclipse.tracecompass.tmf.ctf.core.CtfConstants;
56 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocation;
57 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfLocationInfo;
58 import org.eclipse.tracecompass.tmf.ctf.core.context.CtfTmfContext;
59 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEvent;
60 import org.eclipse.tracecompass.tmf.ctf.core.event.CtfTmfEventType;
61 import org.eclipse.tracecompass.tmf.ctf.core.timestamp.CtfTmfTimestamp;
62 import org.eclipse.tracecompass.tmf.ctf.core.trace.iterator.CtfIterator;
63 import org.eclipse.tracecompass.tmf.ctf.core.trace.iterator.CtfIteratorManager;
64
65 import com.google.common.collect.ImmutableSet;
66
67 /**
68 * The CTf trace handler
69 *
70 * @version 1.0
71 * @author Matthew khouzam
72 */
73 public class CtfTmfTrace extends TmfTrace
74 implements ITmfEventParser, ITmfTraceProperties, ITmfPersistentlyIndexable,
75 ITmfTraceWithPreDefinedEvents, AutoCloseable {
76
77 // -------------------------------------------
78 // Constants
79 // -------------------------------------------
80 /**
81 * Default cache size for CTF traces
82 */
83 protected static final int DEFAULT_CACHE_SIZE = 50000;
84
85 /*
86 * The Ctf clock unique identifier field
87 */
88 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
89 private static final int CONFIDENCE = 10;
90
91 // -------------------------------------------
92 // Fields
93 // -------------------------------------------
94
95 private final Map<String, CtfTmfEventType> fContainedEventTypes =
96 Collections.synchronizedMap(new HashMap<String, CtfTmfEventType>());
97
98 private final CtfIteratorManager fIteratorManager =
99 new CtfIteratorManager(this);
100
101 /* Reference to the CTF Trace */
102 private CTFTrace fTrace;
103
104 // -------------------------------------------
105 // TmfTrace Overrides
106 // -------------------------------------------
107 /**
108 * Method initTrace.
109 *
110 * @param resource
111 * The resource associated with this trace
112 * @param path
113 * The path to the trace file
114 * @param eventType
115 * The type of events that will be read from this trace
116 * @throws TmfTraceException
117 * If something went wrong while reading the trace
118 */
119 @Override
120 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
121 throws TmfTraceException {
122 /*
123 * Set the cache size. This has to be done before the call to super()
124 * because the super needs to know the cache size.
125 */
126 setCacheSize();
127
128 super.initTrace(resource, path, eventType);
129
130 try {
131 this.fTrace = new CTFTrace(path);
132 CtfTmfContext ctx;
133 /* Set the start and (current) end times for this trace */
134 ctx = (CtfTmfContext) seekEvent(0L);
135 CtfTmfEvent event = getNext(ctx);
136 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
137 /* Handle the case where the trace is empty */
138 this.setStartTime(TmfTimestamp.BIG_BANG);
139 } else {
140 final ITmfTimestamp curTime = event.getTimestamp();
141 this.setStartTime(curTime);
142 this.setEndTime(curTime);
143 }
144 /*
145 * Register every event type. When you call getType, it will
146 * register a trace to that type in the TmfEventTypeManager
147 */
148 try (CtfIterator iter = fIteratorManager.getIterator(ctx)) {
149 for (IEventDeclaration ied : iter.getEventDeclarations()) {
150 CtfTmfEventType ctfTmfEventType = fContainedEventTypes.get(ied.getName());
151 if (ctfTmfEventType == null) {
152 List<ITmfEventField> content = new ArrayList<>();
153 /* Should only return null the first time */
154 for (String fieldName : ied.getFields().getFieldsList()) {
155 content.add(new TmfEventField(fieldName, null, null));
156 }
157 ITmfEventField contentTree = new TmfEventField(
158 ITmfEventField.ROOT_FIELD_ID,
159 null,
160 content.toArray(new ITmfEventField[content.size()])
161 );
162
163 ctfTmfEventType = new CtfTmfEventType(ied.getName(), contentTree);
164 fContainedEventTypes.put(ctfTmfEventType.getName(), ctfTmfEventType);
165 }
166 }
167 }
168 } catch (final CTFReaderException e) {
169 /*
170 * If it failed at the init(), we can assume it's because the file
171 * was not found or was not recognized as a CTF trace. Throw into
172 * the new type of exception expected by the rest of TMF.
173 */
174 throw new TmfTraceException(e.getMessage(), e);
175 }
176 }
177
178 /**
179 * Return the iterator manager of this trace
180 *
181 * @return The iterator manager
182 */
183 public CtfIteratorManager getIteratorManager() {
184 return fIteratorManager;
185 }
186
187 @Override
188 public void close() {
189 dispose();
190 }
191
192 @Override
193 public synchronized void dispose() {
194 fIteratorManager.dispose();
195 if (fTrace != null) {
196 fTrace.close();
197 fTrace = null;
198 }
199 super.dispose();
200 }
201
202 /**
203 * {@inheritDoc}
204 * <p>
205 * The default implementation sets the confidence to 10 if the trace is a
206 * valid CTF trace.
207 */
208 @Override
209 public IStatus validate(final IProject project, final String path) {
210 IStatus status = new TraceValidationStatus(CONFIDENCE, Activator.PLUGIN_ID);
211 try (final CTFTrace temp = new CTFTrace(path);) {
212 if (!temp.majorIsSet()) {
213 status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
214 } else {
215 try (CTFTraceReader ctfTraceReader = new CTFTraceReader(temp);) {
216 if (!ctfTraceReader.hasMoreEvents()) {
217 // TODO: This will need an additional check when we
218 // support live traces
219 // because having no event is valid for a live trace
220 status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_NoEvent);
221 }
222 }
223 }
224 } catch (final CTFReaderException e) {
225 status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + e.toString()); //$NON-NLS-1$
226 } catch (final BufferOverflowException e) {
227 status = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError + ": " + Messages.CtfTmfTrace_BufferOverflowErrorMessage); //$NON-NLS-1$
228 }
229
230 return status;
231 }
232
233 /**
234 * Method getCurrentLocation. This is not applicable in CTF
235 *
236 * @return null, since the trace has no knowledge of the current location
237 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getCurrentLocation()
238 * @since 3.0
239 */
240 @Override
241 public ITmfLocation getCurrentLocation() {
242 return null;
243 }
244
245 /**
246 * @since 3.0
247 */
248 @Override
249 public double getLocationRatio(ITmfLocation location) {
250 final CtfLocation curLocation = (CtfLocation) location;
251 final CtfTmfContext context = new CtfTmfContext(this);
252 context.setLocation(curLocation);
253 context.seek(curLocation.getLocationInfo());
254 final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
255 final long startTime = fIteratorManager.getIterator(context).getStartTime();
256 final long endTime = fIteratorManager.getIterator(context).getEndTime();
257 return ((double) currentTime.getTimestamp() - startTime)
258 / (endTime - startTime);
259 }
260
261 /**
262 * Method seekEvent.
263 *
264 * @param location
265 * ITmfLocation<?>
266 * @return ITmfContext
267 * @since 3.0
268 */
269 @Override
270 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
271 CtfLocation currentLocation = (CtfLocation) location;
272 CtfTmfContext context = new CtfTmfContext(this);
273 if (fTrace == null) {
274 context.setLocation(null);
275 context.setRank(ITmfContext.UNKNOWN_RANK);
276 return context;
277 }
278 /*
279 * The rank is set to 0 if the iterator seeks the beginning. If not, it
280 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
281 * by rank for now.
282 */
283 if (currentLocation == null) {
284 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
285 context.setRank(0);
286 }
287 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
288 currentLocation = new CtfLocation(getCTFTrace().getCurrentEndTime() + 1, 0L);
289 }
290 context.setLocation(currentLocation);
291 if (location == null) {
292 long timestamp = fIteratorManager.getIterator(context).getCurrentTimestamp();
293 currentLocation = new CtfLocation(timestamp, 0);
294 }
295 if (context.getRank() != 0) {
296 context.setRank(ITmfContext.UNKNOWN_RANK);
297 }
298 return context;
299 }
300
301 @Override
302 public synchronized ITmfContext seekEvent(double ratio) {
303 CtfTmfContext context = new CtfTmfContext(this);
304 if (fTrace == null) {
305 context.setLocation(null);
306 context.setRank(ITmfContext.UNKNOWN_RANK);
307 return context;
308 }
309 final long end = getCTFTrace().getCurrentEndTime();
310 final long start = getCTFTrace().getCurrentStartTime();
311 final long diff = end - start;
312 final long ratioTs = Math.round(diff * ratio) + start;
313 context.seek(ratioTs);
314 context.setRank(ITmfContext.UNKNOWN_RANK);
315 return context;
316 }
317
318 /**
319 * Method readNextEvent.
320 *
321 * @param context
322 * ITmfContext
323 * @return CtfTmfEvent
324 * @see org.eclipse.tracecompass.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
325 */
326 @Override
327 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
328 if (fTrace == null) {
329 return null;
330 }
331 CtfTmfEvent event = null;
332 if (context instanceof CtfTmfContext) {
333 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
334 return null;
335 }
336 CtfTmfContext ctfContext = (CtfTmfContext) context;
337 event = ctfContext.getCurrentEvent();
338
339 if (event != null) {
340 updateAttributes(context, event.getTimestamp());
341 ctfContext.advance();
342 ctfContext.increaseRank();
343 }
344 }
345
346 return event;
347 }
348
349 /**
350 * gets the CTFtrace that this is wrapping
351 *
352 * @return the CTF trace
353 */
354 public CTFTrace getCTFTrace() {
355 return fTrace;
356 }
357
358 /**
359 * Ctf traces have a clock with a unique uuid that will be used to identify
360 * the host. Traces with the same clock uuid will be known to have been made
361 * on the same machine.
362 *
363 * Note: uuid is an optional field, it may not be there for a clock.
364 */
365 @Override
366 public String getHostId() {
367 CTFClock clock = getCTFTrace().getClock();
368 if (clock != null) {
369 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
370 if (clockHost != null) {
371 return clockHost;
372 }
373 }
374 return super.getHostId();
375 }
376
377 // -------------------------------------------
378 // ITmfTraceProperties
379 // -------------------------------------------
380
381 /**
382 * @since 2.0
383 */
384 @Override
385 public Map<String, String> getTraceProperties() {
386 Map<String, String> properties = new HashMap<>();
387 properties.putAll(fTrace.getEnvironment());
388 properties.put(Messages.CtfTmfTrace_HostID, getHostId());
389 return properties;
390 }
391
392 // -------------------------------------------
393 // Clocks
394 // -------------------------------------------
395
396 /**
397 * gets the clock offset
398 *
399 * @return the clock offset in ns
400 */
401 public long getOffset() {
402 if (fTrace != null) {
403 return fTrace.getOffset();
404 }
405 return 0;
406 }
407
408 /**
409 * Gets the list of declared events
410 *
411 * @since 3.0
412 */
413 @Override
414 public Set<CtfTmfEventType> getContainedEventTypes() {
415 return ImmutableSet.copyOf(fContainedEventTypes.values());
416 }
417
418 /**
419 * Register an event type to this trace.
420 *
421 * Public visibility so that {@link CtfTmfEvent#getType} can call it.
422 *
423 * FIXME This could probably be made cleaner?
424 *
425 * @param eventType
426 * The event type to register
427 */
428 public void registerEventType(CtfTmfEventType eventType) {
429 fContainedEventTypes.put(eventType.getName(), eventType);
430 }
431
432 // -------------------------------------------
433 // Parser
434 // -------------------------------------------
435
436 @Override
437 public CtfTmfEvent parseEvent(ITmfContext context) {
438 CtfTmfEvent event = null;
439 if (context instanceof CtfTmfContext) {
440 final ITmfContext tmpContext = seekEvent(context.getLocation());
441 event = getNext(tmpContext);
442 }
443 return event;
444 }
445
446 /**
447 * Sets the cache size for a CtfTmfTrace.
448 */
449 protected void setCacheSize() {
450 setCacheSize(DEFAULT_CACHE_SIZE);
451 }
452
453 // -------------------------------------------
454 // Helpers
455 // -------------------------------------------
456
457 /**
458 * Get an iterator to the trace
459 *
460 * @return an iterator to the trace
461 * @since 2.0
462 */
463 public CtfIterator createIterator() {
464 try {
465 return new CtfIterator(this);
466 } catch (CTFReaderException e) {
467 Activator.getDefault().logError(e.getMessage(), e);
468 }
469 return null;
470 }
471
472 // ------------------------------------------------------------------------
473 // Timestamp transformation functions
474 // ------------------------------------------------------------------------
475
476 /**
477 * @since 3.0
478 */
479 @Override
480 public CtfTmfTimestamp createTimestamp(long ts) {
481 return new CtfTmfTimestamp(getTimestampTransform().transform(ts));
482 }
483
484 private static int fCheckpointSize = -1;
485
486 /**
487 * @since 3.0
488 */
489 @Override
490 public synchronized int getCheckpointSize() {
491 if (fCheckpointSize == -1) {
492 TmfCheckpoint c = new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
493 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
494 b.clear();
495 c.serialize(b);
496 fCheckpointSize = b.position();
497 }
498
499 return fCheckpointSize;
500 }
501
502 @Override
503 protected ITmfTraceIndexer createIndexer(int interval) {
504 return new TmfBTreeTraceIndexer(this, interval);
505 }
506
507 /**
508 * @since 3.0
509 */
510 @Override
511 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
512 return new CtfLocation(bufferIn);
513 }
514
515 @Override
516 public boolean isComplete() {
517 if (getResource() == null) {
518 return true;
519 }
520
521 String host = null;
522 String port = null;
523 String sessionName = null;
524 try {
525 host = getResource().getPersistentProperty(CtfConstants.LIVE_HOST);
526 port = getResource().getPersistentProperty(CtfConstants.LIVE_PORT);
527 sessionName = getResource().getPersistentProperty(CtfConstants.LIVE_SESSION_NAME);
528 } catch (CoreException e) {
529 Activator.getDefault().logError(e.getMessage(), e);
530 // Something happened to the resource, assume we won't get any more data from it
531 return true;
532 }
533 return host == null || port == null || sessionName == null;
534 }
535
536 @Override
537 public void setComplete(final boolean isComplete) {
538 super.setComplete(isComplete);
539 try {
540 if (isComplete) {
541 getResource().setPersistentProperty(CtfConstants.LIVE_HOST, null);
542 getResource().setPersistentProperty(CtfConstants.LIVE_PORT, null);
543 getResource().setPersistentProperty(CtfConstants.LIVE_SESSION_NAME, null);
544 }
545 } catch (CoreException e) {
546 Activator.getDefault().logError(e.getMessage(), e);
547 }
548 }
549 }
This page took 0.047914 seconds and 5 git commands to generate.