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