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