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