ctf: Make events immutable
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core / src / org / eclipse / linuxtools / tmf / ctf / core / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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 * Bernd Hufmann - Updated for source and model lookup interfaces
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.ctf.core;
15
16 import java.util.ArrayList;
17 import java.util.HashSet;
18 import java.util.List;
19 import java.util.Set;
20
21 import org.eclipse.jdt.annotation.NonNull;
22 import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
23 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
24 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
25 import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition;
26 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
27 import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
29 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
30 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
31 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
32 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup;
33 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup;
34 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
35
36 /**
37 * A wrapper class around CTF's Event Definition/Declaration that maps all types
38 * of Declaration to native Java types.
39 *
40 * @version 1.0
41 * @author Alexandre Montplaisir
42 * @since 2.0
43 */
44 public class CtfTmfEvent extends TmfEvent
45 implements ITmfSourceLookup, ITmfModelLookup, ITmfCustomAttributes {
46
47 // ------------------------------------------------------------------------
48 // Constants
49 // ------------------------------------------------------------------------
50
51 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
52
53 // ------------------------------------------------------------------------
54 // Attributes
55 // ------------------------------------------------------------------------
56
57 private final int fSourceCPU;
58 private final long fTypeId;
59 private final String fEventName;
60 private final IEventDeclaration fEventDeclaration;
61 @NonNull
62 private final EventDefinition fEvent;
63 private ITmfEventField fContent;
64
65 // ------------------------------------------------------------------------
66 // Constructors
67 // ------------------------------------------------------------------------
68
69 /**
70 * Constructor used by {@link CtfTmfEventFactory#createEvent}
71 */
72 CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp,
73 String fileName, int cpu, IEventDeclaration declaration, @NonNull EventDefinition eventDefinition) {
74 super(trace,
75 rank,
76 timestamp,
77 String.valueOf(cpu), // Source
78 null, // Event type. We don't use TmfEvent's field here, we
79 // re-implement getType()
80 null, // Content handled with a lazy loaded re-implemented in
81 // getContent()
82 fileName // Reference
83 );
84
85 fEventDeclaration = declaration;
86 fSourceCPU = cpu;
87 fTypeId = declaration.getId();
88 fEventName = declaration.getName();
89 fEvent = eventDefinition;
90
91 }
92
93 /**
94 * Inner constructor to create "null" events. Don't use this directly in
95 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
96 * instance of an empty event.
97 *
98 * This needs to be public however because it's used in extension points,
99 * and the framework will use this constructor to get the class type.
100 */
101 public CtfTmfEvent() {
102 super(null,
103 ITmfContext.UNKNOWN_RANK,
104 new CtfTmfTimestamp(-1),
105 null,
106 null,
107 new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$
108 null);
109 fSourceCPU = -1;
110 fTypeId = -1;
111 fEventName = EMPTY_CTF_EVENT_NAME;
112 fEventDeclaration = null;
113 fEvent = EventDefinition.NULL_EVENT;
114 }
115
116 // ------------------------------------------------------------------------
117 // Getters/Setters/Predicates
118 // ------------------------------------------------------------------------
119
120 /**
121 * Gets the cpu core the event was recorded on.
122 *
123 * @return The cpu id for a given source. In lttng it's from CPUINFO
124 */
125 public int getCPU() {
126 return fSourceCPU;
127 }
128
129 /**
130 * Return this event's ID, according to the trace's metadata.
131 *
132 * Watch out, this ID is not constant from one trace to another for the same
133 * event types! Use "getEventName()" for a constant reference.
134 *
135 * @return The event ID
136 */
137 public long getID() {
138 return fTypeId;
139 }
140
141 @Override
142 public CtfTmfTrace getTrace() {
143 /*
144 * Should be of the right type, since we take a CtfTmfTrace at the
145 * constructor
146 */
147 return (CtfTmfTrace) super.getTrace();
148 }
149
150 @Override
151 public ITmfEventType getType() {
152 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(getTrace(), fEventName);
153 if (ctfTmfEventType == null) {
154 /* Should only return null the first time */
155 ctfTmfEventType = new CtfTmfEventType(fEventName, getTrace(), getContent());
156 }
157 return ctfTmfEventType;
158 }
159
160 /**
161 * @since 2.0
162 */
163 @Override
164 public Set<String> listCustomAttributes() {
165 if (fEventDeclaration == null) {
166 return new HashSet<>();
167 }
168 return fEventDeclaration.getCustomAttributes();
169 }
170
171 /**
172 * @since 2.0
173 */
174 @Override
175 public String getCustomAttribute(String name) {
176 if (fEventDeclaration == null) {
177 return null;
178 }
179 return fEventDeclaration.getCustomAttribute(name);
180 }
181
182 /**
183 * Get the call site for this event.
184 *
185 * @return the call site information, or null if there is none
186 * @since 2.0
187 */
188 @Override
189 public CtfTmfCallsite getCallsite() {
190 CTFCallsite callsite = null;
191 CtfTmfTrace trace = getTrace();
192 if (trace == null) {
193 return null;
194 }
195 CTFTrace ctfTrace = trace.getCTFTrace();
196 /* Should not happen, but it is a good check */
197 if (ctfTrace == null) {
198 return null;
199 }
200 if (getContent() != null) {
201 ITmfEventField ipField = getContent().getField(CtfConstants.CONTEXT_FIELD_PREFIX + CtfConstants.IP_KEY);
202 if (ipField != null && ipField.getValue() instanceof Long) {
203 long ip = (Long) ipField.getValue();
204 callsite = ctfTrace.getCallsite(fEventName, ip);
205 }
206 }
207 if (callsite == null) {
208 callsite = ctfTrace.getCallsite(fEventName);
209 }
210 if (callsite != null) {
211 return new CtfTmfCallsite(callsite);
212 }
213 return null;
214 }
215
216 /**
217 * @since 2.0
218 */
219 @Override
220 public String getModelUri() {
221 return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
222 }
223
224 @Override
225 public synchronized ITmfEventField getContent() {
226 if (fContent == null) {
227 fContent = new TmfEventField(
228 ITmfEventField.ROOT_FIELD_ID, null, parseFields(fEvent));
229 }
230 return fContent;
231 }
232
233 /**
234 * Extract the field information from the structDefinition haze-inducing
235 * mess, and put them into something ITmfEventField can cope with.
236 */
237 private static CtfTmfEventField[] parseFields(@NonNull EventDefinition eventDef) {
238 List<CtfTmfEventField> fields = new ArrayList<>();
239
240 StructDefinition structFields = eventDef.getFields();
241 if (structFields != null) {
242 if (structFields.getFieldNames() != null) {
243 for (String curFieldName : structFields.getFieldNames()) {
244 fields.add(CtfTmfEventField.parseField(structFields.getDefinition(curFieldName), curFieldName));
245 }
246 }
247 }
248 /* Add context information as CtfTmfEventField */
249 StructDefinition structContext = eventDef.getContext();
250 if (structContext != null) {
251 for (String contextName : structContext.getFieldNames()) {
252 /* Prefix field name */
253 String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + contextName;
254 fields.add(CtfTmfEventField.parseField(structContext.getDefinition(contextName), curContextName));
255 }
256 }
257
258 return fields.toArray(new CtfTmfEventField[fields.size()]);
259 }
260
261 }
This page took 0.039042 seconds and 5 git commands to generate.