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