tmf: Make TmfEventField immutable
[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 /* Copy the timestamp (immutable) */
190 this.fTimestamp = other.fTimestamp;
191
192 /* Primitives, those will be copied by value */
193 this.sourceCPU = other.sourceCPU;
194 this.typeId = other.typeId;
195
196 /* Strings are immutable, it's safe to shallow-copy them */
197 this.eventName = other.eventName;
198 this.fileName = other.fileName;
199
200 /* Copy the fields over (immutable) */
201 this.fContent = other.fContent;
202
203 /*
204 * Copy the reference to the custom attributes (should be the same
205 * object for all events of this type)
206 */
207 this.fDeclaration = other.fDeclaration;
208 }
209
210 /**
211 * Inner constructor to create "null" events. Don't use this directly in
212 * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an
213 * empty event.
214 *
215 * This needs to be public however because it's used in extension points,
216 * and the framework will use this constructor to get the class type.
217 */
218 public CtfTmfEvent() {
219 this.fTrace = null;
220 this.fTimestamp = new CtfTmfTimestamp(-1);
221 this.sourceCPU = -1;
222 this.typeId = -1;
223 this.fileName = NO_STREAM;
224 this.eventName = EMPTY_CTF_EVENT_NAME;
225 this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$
226 this.fDeclaration = null;
227 }
228
229 // ------------------------------------------------------------------------
230 // Getters/Setters/Predicates
231 // ------------------------------------------------------------------------
232
233 private static CtfTmfEvent nullEvent = new CtfTmfEvent();
234
235 /**
236 * Get a null event
237 *
238 * @return An empty event.
239 */
240 public static CtfTmfEvent getNullEvent() {
241 return nullEvent;
242 }
243
244 /**
245 * Gets the cpu core the event was recorded on.
246 *
247 * @return The cpu id for a given source. In lttng it's from CPUINFO
248 */
249 public int getCPU() {
250 return this.sourceCPU;
251 }
252
253 /**
254 * Return this event's ID, according to the trace's metadata.
255 *
256 * Watch out, this ID is not constant from one trace to another for the same
257 * event types! Use "getEventName()" for a constant reference.
258 *
259 * @return The event ID
260 */
261 public long getID() {
262 return this.typeId;
263 }
264
265 /**
266 * Gets the name of a current event.
267 *
268 * @return The event name
269 */
270 public String getEventName() {
271 return eventName;
272 }
273
274 /**
275 * Gets the channel name of a field.
276 *
277 * @return The channel name.
278 */
279 public String getChannelName() {
280 return this.fileName;
281 }
282
283 @Override
284 public CtfTmfTrace getTrace() {
285 return fTrace;
286 }
287
288 @Override
289 public long getRank() {
290 // TODO Auto-generated method stub
291 return 0;
292 }
293
294 @Override
295 public ITmfTimestamp getTimestamp() {
296 return fTimestamp;
297 }
298
299 @Override
300 public String getSource() {
301 // TODO Returns CPU for now
302 return Integer.toString(getCPU());
303 }
304
305 @Override
306 public ITmfEventType getType() {
307 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
308 if( ctfTmfEventType == null ){
309 ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent());
310 }
311 return ctfTmfEventType;
312 }
313
314 @Override
315 public ITmfEventField getContent() {
316 return fContent;
317 }
318
319 @Override
320 public String getReference() {
321 return getChannelName();
322 }
323
324 /**
325 * List the custom CTF attributes for events of this type.
326 *
327 * @return The list of custom attribute names. Should not be null, but could
328 * be empty.
329 * @since 2.0
330 */
331 public Set<String> listCustomAttributes() {
332 if (fDeclaration == null) {
333 return new HashSet<String>();
334 }
335 return fDeclaration.getCustomAttributes();
336 }
337
338 /**
339 * Get the value of a custom CTF attributes for this event's type.
340 *
341 * @param name
342 * Name of the the custom attribute
343 * @return Value of this attribute, or null if there is no attribute with
344 * that name
345 * @since 2.0
346 */
347 public String getCustomAttribute(String name) {
348 if (fDeclaration == null) {
349 return null;
350 }
351 return fDeclaration.getCustomAttribute(name);
352 }
353
354 @Override
355 public CtfTmfEvent clone() {
356 return new CtfTmfEvent(this);
357 }
358
359 /**
360 * @since 2.0
361 */
362 @Override
363 public Object getAdapter(Class adapter) {
364 if (adapter == IPropertySource.class) {
365 return new TmfEventPropertySource(this);
366 }
367 return null;
368 }
369 }
This page took 0.058795 seconds and 5 git commands to generate.