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