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