Merge branch 'master' into lttng-luna
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfTrace.java
1 /*******************************************************************************
2 * Copyright (c) 2012, 2013 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.linuxtools.tmf.core.ctfadaptor;
16
17 import java.nio.BufferOverflowException;
18 import java.nio.ByteBuffer;
19 import java.util.Collections;
20 import java.util.Map;
21
22 import org.eclipse.core.resources.IProject;
23 import org.eclipse.core.resources.IResource;
24 import org.eclipse.core.runtime.IStatus;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.linuxtools.ctf.core.event.CTFClock;
27 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
28 import org.eclipse.linuxtools.ctf.core.trace.CTFReaderException;
29 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
30 import org.eclipse.linuxtools.ctf.core.trace.CTFTraceReader;
31 import org.eclipse.linuxtools.internal.tmf.core.Activator;
32 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
33 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
34 import org.eclipse.linuxtools.tmf.core.timestamp.ITmfTimestamp;
35 import org.eclipse.linuxtools.tmf.core.timestamp.TmfTimestamp;
36 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
37 import org.eclipse.linuxtools.tmf.core.trace.ITmfEventParser;
38 import org.eclipse.linuxtools.tmf.core.trace.ITmfTraceProperties;
39 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
40 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfPersistentlyIndexable;
41 import org.eclipse.linuxtools.tmf.core.trace.indexer.ITmfTraceIndexer;
42 import org.eclipse.linuxtools.tmf.core.trace.indexer.TmfBTreeTraceIndexer;
43 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.ITmfCheckpoint;
44 import org.eclipse.linuxtools.tmf.core.trace.indexer.checkpoint.TmfCheckpoint;
45 import org.eclipse.linuxtools.tmf.core.trace.location.ITmfLocation;
46
47 /**
48 * The CTf trace handler
49 *
50 * @version 1.0
51 * @author Matthew khouzam
52 */
53 public class CtfTmfTrace extends TmfTrace
54 implements ITmfEventParser, ITmfTraceProperties, ITmfPersistentlyIndexable {
55
56 // -------------------------------------------
57 // Constants
58 // -------------------------------------------
59 /**
60 * Default cache size for CTF traces
61 */
62 protected static final int DEFAULT_CACHE_SIZE = 50000;
63
64 /*
65 * The Ctf clock unique identifier field
66 */
67 private static final String CLOCK_HOST_PROPERTY = "uuid"; //$NON-NLS-1$
68
69 // -------------------------------------------
70 // Fields
71 // -------------------------------------------
72
73 /* Reference to the CTF Trace */
74 private CTFTrace fTrace;
75
76 // -------------------------------------------
77 // TmfTrace Overrides
78 // -------------------------------------------
79 /**
80 * Method initTrace.
81 *
82 * @param resource
83 * The resource associated with this trace
84 * @param path
85 * The path to the trace file
86 * @param eventType
87 * The type of events that will be read from this trace
88 * @throws TmfTraceException
89 * If something when wrong while reading the trace
90 */
91 @Override
92 public void initTrace(final IResource resource, final String path, final Class<? extends ITmfEvent> eventType)
93 throws TmfTraceException {
94 /*
95 * Set the cache size. This has to be done before the call to super()
96 * because the super needs to know the cache size.
97 */
98 setCacheSize();
99
100 super.initTrace(resource, path, eventType);
101
102 try {
103 this.fTrace = new CTFTrace(path);
104 CtfIteratorManager.addTrace(this);
105 CtfTmfContext ctx;
106 /* Set the start and (current) end times for this trace */
107 ctx = (CtfTmfContext) seekEvent(0L);
108 CtfTmfEvent event = getNext(ctx);
109 if ((ctx.getLocation().equals(CtfIterator.NULL_LOCATION)) || (ctx.getCurrentEvent() == null)) {
110 /* Handle the case where the trace is empty */
111 this.setStartTime(TmfTimestamp.BIG_BANG);
112 } else {
113 final ITmfTimestamp curTime = event.getTimestamp();
114 this.setStartTime(curTime);
115 this.setEndTime(curTime);
116 }
117
118 } catch (final CTFReaderException e) {
119 /*
120 * If it failed at the init(), we can assume it's because the file
121 * was not found or was not recognized as a CTF trace. Throw into
122 * the new type of exception expected by the rest of TMF.
123 */
124 throw new TmfTraceException(e.getMessage(), e);
125 }
126 }
127
128 @Override
129 public synchronized void dispose() {
130 CtfIteratorManager.removeTrace(this);
131 if (fTrace != null) {
132 fTrace.dispose();
133 fTrace = null;
134 }
135 super.dispose();
136 }
137
138 /**
139 * Method validate.
140 *
141 * @param project
142 * IProject
143 * @param path
144 * String
145 * @return IStatus IStatus.error or Status.OK_STATUS
146 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#validate(IProject, String)
147 * @since 2.0
148 */
149 @Override
150 public IStatus validate(final IProject project, final String path) {
151 IStatus validTrace = Status.OK_STATUS;
152 try {
153 final CTFTrace temp = new CTFTrace(path);
154 if (!temp.majortIsSet()) {
155 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_MajorNotSet);
156 } else {
157 CTFTraceReader ctfTraceReader = new CTFTraceReader(temp);
158 if (!ctfTraceReader.hasMoreEvents()) {
159 // TODO: This will need an additional check when we support live traces
160 // because having no event is valid for a live trace
161 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_NoEvent);
162 }
163 ctfTraceReader.dispose();
164 }
165 temp.dispose();
166 } catch (final CTFReaderException e) {
167 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError +": " + e.toString()); //$NON-NLS-1$
168 } catch (final BufferOverflowException e){
169 validTrace = new Status(IStatus.ERROR, Activator.PLUGIN_ID, Messages.CtfTmfTrace_ReadingError +": " + Messages.CtfTmfTrace_BufferOverflowErrorMessage); //$NON-NLS-1$
170 }
171
172 return validTrace;
173 }
174
175 /**
176 * Method getCurrentLocation. This is not applicable in CTF
177 *
178 * @return null, since the trace has no knowledge of the current location
179 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getCurrentLocation()
180 * @since 3.0
181 */
182 @Override
183 public ITmfLocation getCurrentLocation() {
184 return null;
185 }
186
187 /**
188 * @since 3.0
189 */
190 @Override
191 public double getLocationRatio(ITmfLocation location) {
192 final CtfLocation curLocation = (CtfLocation) location;
193 final CtfTmfContext context = new CtfTmfContext(this);
194 context.setLocation(curLocation);
195 context.seek(curLocation.getLocationInfo());
196 final CtfLocationInfo currentTime = ((CtfLocationInfo) context.getLocation().getLocationInfo());
197 final long startTime = getIterator(this, context).getStartTime();
198 final long endTime = getIterator(this, context).getEndTime();
199 return ((double) currentTime.getTimestamp() - startTime)
200 / (endTime - startTime);
201 }
202
203 /**
204 * Method seekEvent.
205 *
206 * @param location
207 * ITmfLocation<?>
208 * @return ITmfContext
209 * @since 3.0
210 */
211 @Override
212 public synchronized ITmfContext seekEvent(final ITmfLocation location) {
213 CtfLocation currentLocation = (CtfLocation) location;
214 CtfTmfContext context = new CtfTmfContext(this);
215 if (fTrace == null) {
216 context.setLocation(null);
217 context.setRank(ITmfContext.UNKNOWN_RANK);
218 return context;
219 }
220 /*
221 * The rank is set to 0 if the iterator seeks the beginning. If not, it
222 * will be set to UNKNOWN_RANK, since CTF traces don't support seeking
223 * by rank for now.
224 */
225 if (currentLocation == null) {
226 currentLocation = new CtfLocation(new CtfLocationInfo(0L, 0L));
227 context.setRank(0);
228 }
229 if (currentLocation.getLocationInfo() == CtfLocation.INVALID_LOCATION) {
230 currentLocation = new CtfLocation(getEndTime().getValue() + 1, 0L);
231 }
232 context.setLocation(currentLocation);
233 if (location == null) {
234 CtfTmfEvent event = getIterator(this, context).getCurrentEvent();
235 if (event != null) {
236 currentLocation = new CtfLocation(event.getTimestamp().getValue(), 0);
237 }
238 }
239 if (context.getRank() != 0) {
240 context.setRank(ITmfContext.UNKNOWN_RANK);
241 }
242 return context;
243 }
244
245 @Override
246 public synchronized ITmfContext seekEvent(double ratio) {
247 CtfTmfContext context = new CtfTmfContext(this);
248 if (fTrace == null) {
249 context.setLocation(null);
250 context.setRank(ITmfContext.UNKNOWN_RANK);
251 return context;
252 }
253 final long end = this.getEndTime().getValue();
254 final long start = this.getStartTime().getValue();
255 final long diff = end - start;
256 final long ratioTs = Math.round(diff * ratio) + start;
257 context.seek(ratioTs);
258 context.setRank(ITmfContext.UNKNOWN_RANK);
259 return context;
260 }
261
262 /**
263 * Method readNextEvent.
264 *
265 * @param context
266 * ITmfContext
267 * @return CtfTmfEvent
268 * @see org.eclipse.linuxtools.tmf.core.trace.ITmfTrace#getNext(ITmfContext)
269 */
270 @Override
271 public synchronized CtfTmfEvent getNext(final ITmfContext context) {
272 if (fTrace == null) {
273 return null;
274 }
275 CtfTmfEvent event = null;
276 if (context instanceof CtfTmfContext) {
277 if (context.getLocation() == null || CtfLocation.INVALID_LOCATION.equals(context.getLocation().getLocationInfo())) {
278 return null;
279 }
280 CtfTmfContext ctfContext = (CtfTmfContext) context;
281 event = ctfContext.getCurrentEvent();
282
283 if (event != null) {
284 updateAttributes(context, event.getTimestamp());
285 ctfContext.advance();
286 ctfContext.increaseRank();
287 }
288 }
289
290 return event;
291 }
292
293 /**
294 * gets the CTFtrace that this is wrapping
295 *
296 * @return the CTF trace
297 */
298 public CTFTrace getCTFTrace() {
299 return fTrace;
300 }
301
302 /**
303 * Ctf traces have a clock with a unique uuid that will be used to identify
304 * the host. Traces with the same clock uuid will be known to have been made
305 * on the same machine.
306 *
307 * Note: uuid is an optional field, it may not be there for a clock.
308 */
309 @Override
310 public String getHostId() {
311 CTFClock clock = getCTFTrace().getClock();
312 if (clock != null) {
313 String clockHost = (String) clock.getProperty(CLOCK_HOST_PROPERTY);
314 if (clockHost != null) {
315 return clockHost;
316 }
317 }
318 return super.getHostId();
319 }
320
321 // -------------------------------------------
322 // ITmfTraceProperties
323 // -------------------------------------------
324
325 /**
326 * @since 2.0
327 */
328 @Override
329 public Map<String, String> getTraceProperties() {
330 return Collections.unmodifiableMap(fTrace.getEnvironment());
331 }
332
333 // -------------------------------------------
334 // Clocks
335 // -------------------------------------------
336
337 /**
338 * gets the clock offset
339 *
340 * @return the clock offset in ns
341 */
342 public long getOffset() {
343 if (fTrace != null) {
344 return fTrace.getOffset();
345 }
346 return 0;
347 }
348
349 /**
350 * Returns whether or not an event is in the metadata of the trace,
351 * therefore if it can possibly be in the trace. It does not verify whether
352 * or not the event is actually in the trace
353 *
354 * @param eventName
355 * The name of the event to check
356 * @return Whether the event is in the metadata or not
357 * @since 2.1
358 */
359 public boolean hasEvent(final String eventName) {
360 Map<Long, IEventDeclaration> events = fTrace.getEvents(0L);
361 if (events != null) {
362 for (IEventDeclaration decl : events.values()) {
363 if (decl.getName().equals(eventName)) {
364 return true;
365 }
366 }
367 }
368 return false;
369 }
370
371 /**
372 * Return whether all requested events are in the metadata
373 *
374 * @param names
375 * The array of events to check for
376 * @return Whether all events are in the metadata
377 * @since 2.1
378 */
379 public boolean hasAllEvents(String[] names) {
380 for (String name : names) {
381 if (!hasEvent(name)) {
382 return false;
383 }
384 }
385 return true;
386 }
387
388 /**
389 * Returns whether the metadata contains at least one of the requested
390 * events
391 *
392 * @param names
393 * The array of event names of check for
394 * @return Whether one of the event is present in trace metadata
395 * @since 2.1
396 */
397 public boolean hasAtLeastOneOfEvents(String[] names) {
398 for (String name : names) {
399 if (hasEvent(name)) {
400 return true;
401 }
402 }
403 return false;
404 }
405
406 // -------------------------------------------
407 // Parser
408 // -------------------------------------------
409
410 @Override
411 public CtfTmfEvent parseEvent(ITmfContext context) {
412 CtfTmfEvent event = null;
413 if (context instanceof CtfTmfContext) {
414 final ITmfContext tmpContext = seekEvent(context.getLocation());
415 event = getNext(tmpContext);
416 }
417 return event;
418 }
419
420 /**
421 * Sets the cache size for a CtfTmfTrace.
422 */
423 protected void setCacheSize() {
424 setCacheSize(DEFAULT_CACHE_SIZE);
425 }
426
427 // -------------------------------------------
428 // Helpers
429 // -------------------------------------------
430
431 private static CtfIterator getIterator(CtfTmfTrace trace, CtfTmfContext context) {
432 return CtfIteratorManager.getIterator(trace, context);
433 }
434
435 /**
436 * Get an iterator to the trace
437 *
438 * @return an iterator to the trace
439 * @since 2.0
440 */
441 public CtfIterator createIterator() {
442 return new CtfIterator(this);
443 }
444
445 // ------------------------------------------------------------------------
446 // Timestamp transformation functions
447 // ------------------------------------------------------------------------
448
449 /**
450 * @since 3.0
451 */
452 @Override
453 public CtfTmfTimestamp createTimestamp(long ts) {
454 return new CtfTmfTimestamp(getTimestampTransform().transform(ts));
455 }
456
457 private static int fCheckpointSize = -1;
458
459 @Override
460 public synchronized int getCheckpointSize() {
461 if (fCheckpointSize == -1) {
462 TmfCheckpoint c = new TmfCheckpoint(new CtfTmfTimestamp(0), new CtfLocation(0, 0), 0);
463 ByteBuffer b = ByteBuffer.allocate(ITmfCheckpoint.MAX_SERIALIZE_SIZE);
464 b.clear();
465 c.serialize(b);
466 fCheckpointSize = b.position();
467 }
468
469 return fCheckpointSize;
470 }
471
472 @Override
473 protected ITmfTraceIndexer createIndexer(int interval) {
474 return new TmfBTreeTraceIndexer(this, interval);
475 }
476
477 @Override
478 public ITmfLocation restoreLocation(ByteBuffer bufferIn) {
479 return new CtfLocation(bufferIn);
480 }
481 }
This page took 0.042104 seconds and 5 git commands to generate.