CTF: Add support for custom attributes in an event
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011-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 * Alexandre Montplaisir - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.Iterator;
19 import java.util.List;
20 import java.util.Map.Entry;
21 import java.util.Set;
22
23 import org.eclipse.core.runtime.IAdaptable;
24 import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
25 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
27 import org.eclipse.linuxtools.ctf.core.event.types.Definition;
28 import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition;
29 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
30 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
31 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
32 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
33 import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp;
34 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
35 import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource;
36 import org.eclipse.ui.views.properties.IPropertySource;
37
38 /**
39 * A wrapper class around CTF's Event Definition/Declaration that maps all
40 * types of Declaration to native Java types.
41 *
42 * @version 1.0
43 * @author Alexandre Montplaisir
44 * @since 2.0
45 */
46 public final class CtfTmfEvent implements ITmfEvent, IAdaptable, Cloneable {
47
48 // ------------------------------------------------------------------------
49 // Constants
50 // ------------------------------------------------------------------------
51
52 private static final String NO_STREAM = "No stream"; //$NON-NLS-1$
53 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
54
55 /** Prefix for Context information stored as CtfTmfEventfield */
56 private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$
57
58 // ------------------------------------------------------------------------
59 // Attributes
60 // ------------------------------------------------------------------------
61
62 private final CtfTmfTrace fTrace;
63 private final ITmfTimestamp fTimestamp;
64 private final int sourceCPU;
65 private final long typeId;
66 private final String eventName;
67 private final String fileName;
68
69 private final TmfEventField fContent;
70 private final IEventDeclaration fDeclaration;
71
72 // ------------------------------------------------------------------------
73 // Constructors
74 // ------------------------------------------------------------------------
75
76 /**
77 * Usual CTFEvent constructor, where we read an event from the trace (via
78 * the StreamInputReader).
79 *
80 * @param eventDef
81 * CTF EventDefinition object corresponding to this trace event
82 * @param fileName
83 * The path to the trace file
84 * @param originTrace
85 * The trace from which this event originates
86 */
87 public CtfTmfEvent(EventDefinition eventDef, String fileName,
88 CtfTmfTrace originTrace) {
89 this.fTrace = originTrace;
90
91 if (eventDef == null) {
92 this.fTimestamp = new CtfTmfTimestamp(-1);
93 this.sourceCPU = -1;
94 this.typeId = -1;
95 this.fileName = NO_STREAM;
96 this.eventName = EMPTY_CTF_EVENT_NAME;
97 this.fContent = null;
98 this.fDeclaration = null;
99 return;
100 }
101
102 /* Read the base event info */
103 long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp());
104 this.fTimestamp = new CtfTmfTimestamp(ts);
105 this.sourceCPU = eventDef.getCPU();
106 this.typeId = eventDef.getDeclaration().getId();
107 this.eventName = eventDef.getDeclaration().getName();
108 this.fileName = fileName;
109
110 /* Read the fields */
111 this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef));
112
113 /* Keep a reference to this event's CTF declaration */
114 this.fDeclaration = eventDef.getDeclaration();
115 }
116
117 /**
118 * Extract the field information from the structDefinition haze-inducing
119 * mess, and put them into something ITmfEventField can cope with.
120 */
121 private CtfTmfEventField[] parseFields(EventDefinition eventDef) {
122 List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>();
123
124 StructDefinition structFields = eventDef.getFields();
125 HashMap<String, Definition> definitions = structFields.getDefinitions();
126 String curFieldName = null;
127 Definition curFieldDef;
128 CtfTmfEventField curField;
129 Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator();
130 while(it.hasNext()) {
131 Entry<String, Definition> entry = it.next();
132 curFieldName = entry.getKey();
133 curFieldDef = entry.getValue();
134 curField = CtfTmfEventField.parseField(curFieldDef, curFieldName);
135 fields.add(curField);
136 }
137
138 /* Add context information as CtfTmfEventField */
139 long ip = -1;
140 StructDefinition structContext = eventDef.getContext();
141 if (structContext != null) {
142 definitions = structContext.getDefinitions();
143 String curContextName;
144 Definition curContextDef;
145 CtfTmfEventField curContext;
146 it = definitions.entrySet().iterator();
147 while(it.hasNext()) {
148 Entry<String, Definition> entry = it.next();
149 /* This is to get the instruction pointer if available */
150 if (entry.getKey().equals("_ip") && //$NON-NLS-1$
151 (entry.getValue() instanceof IntegerDefinition)) {
152 ip = ((IntegerDefinition) entry.getValue()).getValue();
153 }
154 /* Prefix field name to */
155 curContextName = CONTEXT_FIELD_PREFIX + entry.getKey();
156 curContextDef = entry.getValue();
157 curContext = CtfTmfEventField.parseField(curContextDef, curContextName);
158 fields.add(curContext);
159 }
160 }
161 /* Add callsite */
162 final String name = eventDef.getDeclaration().getName();
163 List<CTFCallsite> eventList = fTrace.getCTFTrace().getCallsiteCandidates(name);
164 if (!eventList.isEmpty()) {
165 final String callsite = "callsite"; //$NON-NLS-1$
166 if (eventList.size() == 1 || ip == -1) {
167 CTFCallsite cs = eventList.get(0);
168 fields.add(new CTFStringField(cs.toString(), callsite));
169 } else {
170 fields.add(new CTFStringField(
171 fTrace.getCTFTrace().getCallsite(name, ip).toString(),
172 callsite));
173 }
174 }
175
176 return fields.toArray(new CtfTmfEventField[fields.size()]);
177 }
178
179 /**
180 * Copy constructor
181 *
182 * @param other
183 * CtfTmfEvent to copy
184 */
185 public CtfTmfEvent(CtfTmfEvent other) {
186 /* There is only one reference to the trace, so we can shallow-copy it */
187 this.fTrace = other.getTrace();
188
189 /*
190 * Copy the timestamp
191 * FIXME This can be switched to a shallow-copy once timestamps are
192 * made immutable.
193 */
194 this.fTimestamp = new CtfTmfTimestamp(other.fTimestamp.getValue());
195
196 /* Primitives, those will be copied by value */
197 this.sourceCPU = other.sourceCPU;
198 this.typeId = other.typeId;
199
200 /* Strings are immutable, it's safe to shallow-copy them */
201 this.eventName = other.eventName;
202 this.fileName = other.fileName;
203
204 /* Copy the fields over */
205 this.fContent = other.fContent.clone();
206
207 /*
208 * Copy the reference to the custom attributes (should be the same
209 * object for all events of this type)
210 */
211 this.fDeclaration = other.fDeclaration;
212 }
213
214 /**
215 * Inner constructor to create "null" events. Don't use this directly in
216 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
217 * empty event.
218 *
219 * This needs to be public however because it's used in extension points,
220 * and the framework will use this constructor to get the class type.
221 */
222 public CtfTmfEvent() {
223 this.fTrace = null;
224 this.fTimestamp = new CtfTmfTimestamp(-1);
225 this.sourceCPU = -1;
226 this.typeId = -1;
227 this.fileName = NO_STREAM;
228 this.eventName = EMPTY_CTF_EVENT_NAME;
229 this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$
230 this.fDeclaration = null;
231 }
232
233 // ------------------------------------------------------------------------
234 // Getters/Setters/Predicates
235 // ------------------------------------------------------------------------
236
237 private static CtfTmfEvent nullEvent = new CtfTmfEvent();
238
239 /**
240 * Get a null event
241 *
242 * @return An empty event.
243 */
244 public static CtfTmfEvent getNullEvent() {
245 return nullEvent;
246 }
247
248 /**
249 * Gets the cpu core the event was recorded on.
250 *
251 * @return The cpu id for a given source. In lttng it's from CPUINFO
252 */
253 public int getCPU() {
254 return this.sourceCPU;
255 }
256
257 /**
258 * Return this event's ID, according to the trace's metadata.
259 *
260 * Watch out, this ID is not constant from one trace to another for the same
261 * event types! Use "getEventName()" for a constant reference.
262 *
263 * @return The event ID
264 */
265 public long getID() {
266 return this.typeId;
267 }
268
269 /**
270 * Gets the name of a current event.
271 *
272 * @return The event name
273 */
274 public String getEventName() {
275 return eventName;
276 }
277
278 /**
279 * Gets the channel name of a field.
280 *
281 * @return The channel name.
282 */
283 public String getChannelName() {
284 return this.fileName;
285 }
286
287 @Override
288 public CtfTmfTrace getTrace() {
289 return fTrace;
290 }
291
292 @Override
293 public long getRank() {
294 // TODO Auto-generated method stub
295 return 0;
296 }
297
298 @Override
299 public ITmfTimestamp getTimestamp() {
300 return fTimestamp;
301 }
302
303 @Override
304 public String getSource() {
305 // TODO Returns CPU for now
306 return Integer.toString(getCPU());
307 }
308
309 @Override
310 public ITmfEventType getType() {
311 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
312 if( ctfTmfEventType == null ){
313 ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent());
314 }
315 return ctfTmfEventType;
316 }
317
318 @Override
319 public ITmfEventField getContent() {
320 return fContent;
321 }
322
323 @Override
324 public String getReference() {
325 return getChannelName();
326 }
327
328 /**
329 * List the custom CTF attributes for events of this type.
330 *
331 * @return The list of custom attribute names. Should not be null, but could
332 * be empty.
333 * @since 2.0
334 */
335 public Set<String> listCustomAttributes() {
336 if (fDeclaration == null) {
337 return new HashSet<String>();
338 }
339 return fDeclaration.getCustomAttributes();
340 }
341
342 /**
343 * Get the value of a custom CTF attributes for this event's type.
344 *
345 * @param name
346 * Name of the the custom attribute
347 * @return Value of this attribute, or null if there is no attribute with
348 * that name
349 * @since 2.0
350 */
351 public String getCustomAttribute(String name) {
352 if (fDeclaration == null) {
353 return null;
354 }
355 return fDeclaration.getCustomAttribute(name);
356 }
357
358 @Override
359 public CtfTmfEvent clone() {
360 return new CtfTmfEvent(this);
361 }
362
363 /**
364 * @since 2.0
365 */
366 @Override
367 public Object getAdapter(Class adapter) {
368 if (adapter == IPropertySource.class) {
369 return new TmfEventPropertySource(this);
370 }
371 return null;
372 }
373 }
This page took 0.059596 seconds and 6 git commands to generate.