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