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