ctf: replace HashMaps with ArrayLists for EventDeclaration storage
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ctf.core / src / org / eclipse / linuxtools / 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.linuxtools.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.linuxtools.ctf.core.event.CTFCallsite;
23 import org.eclipse.linuxtools.ctf.core.event.EventDefinition;
24 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
25 import org.eclipse.linuxtools.ctf.core.event.types.ICompositeDefinition;
26 import org.eclipse.linuxtools.ctf.core.event.types.IDefinition;
27 import org.eclipse.linuxtools.ctf.core.trace.CTFTrace;
28 import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes;
29 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
30 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
31 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
32 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
33 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup;
34 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup;
35 import org.eclipse.linuxtools.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()} to get an
97 * instance of an empty event.
98 *
99 * This needs to be public however because it's used in extension points,
100 * and the framework will use this constructor to get the class type.
101 */
102 public CtfTmfEvent() {
103 super(null,
104 ITmfContext.UNKNOWN_RANK,
105 new CtfTmfTimestamp(-1),
106 null,
107 null,
108 new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$
109 null);
110 fSourceCPU = -1;
111 fTypeId = -1;
112 fEventName = EMPTY_CTF_EVENT_NAME;
113 fEventDeclaration = null;
114 fEvent = EventDefinition.NULL_EVENT;
115 }
116
117 // ------------------------------------------------------------------------
118 // Getters/Setters/Predicates
119 // ------------------------------------------------------------------------
120
121 /**
122 * Gets the cpu core the event was recorded on.
123 *
124 * @return The cpu id for a given source. In lttng it's from CPUINFO
125 */
126 public int getCPU() {
127 return fSourceCPU;
128 }
129
130 /**
131 * Return this event's ID, according to the trace's metadata.
132 *
133 * Watch out, this ID is not constant from one trace to another for the same
134 * event types! Use "getEventName()" for a constant reference.
135 *
136 * @return The event ID
137 */
138 public long getID() {
139 return fTypeId;
140 }
141
142 @Override
143 public CtfTmfTrace getTrace() {
144 /*
145 * Should be of the right type, since we take a CtfTmfTrace at the
146 * constructor
147 */
148 return (CtfTmfTrace) super.getTrace();
149 }
150
151 @Override
152 public ITmfEventType getType() {
153 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(getTrace(), fEventName);
154 if (ctfTmfEventType == null) {
155 /* Should only return null the first time */
156 ctfTmfEventType = new CtfTmfEventType(fEventName, getTrace(), getContent());
157 }
158 return ctfTmfEventType;
159 }
160
161 /**
162 * @since 2.0
163 */
164 @Override
165 public Set<String> listCustomAttributes() {
166 if (fEventDeclaration == null) {
167 return new HashSet<>();
168 }
169 return fEventDeclaration.getCustomAttributes();
170 }
171
172 /**
173 * @since 2.0
174 */
175 @Override
176 public String getCustomAttribute(String name) {
177 if (fEventDeclaration == null) {
178 return null;
179 }
180 return fEventDeclaration.getCustomAttribute(name);
181 }
182
183 /**
184 * Get the call site for this event.
185 *
186 * @return the call site information, or null if there is none
187 * @since 2.0
188 */
189 @Override
190 public CtfTmfCallsite getCallsite() {
191 CTFCallsite callsite = null;
192 CtfTmfTrace trace = getTrace();
193 if (trace == null) {
194 return null;
195 }
196 CTFTrace ctfTrace = trace.getCTFTrace();
197 /* Should not happen, but it is a good check */
198 if (ctfTrace == null) {
199 return null;
200 }
201 if (getContent() != null) {
202 ITmfEventField ipField = getContent().getField(CtfConstants.CONTEXT_FIELD_PREFIX + CtfConstants.IP_KEY);
203 if (ipField != null && ipField.getValue() instanceof Long) {
204 long ip = (Long) ipField.getValue();
205 callsite = ctfTrace.getCallsite(fEventName, ip);
206 }
207 }
208 if (callsite == null) {
209 callsite = ctfTrace.getCallsite(fEventName);
210 }
211 if (callsite != null) {
212 return new CtfTmfCallsite(callsite);
213 }
214 return null;
215 }
216
217 /**
218 * @since 2.0
219 */
220 @Override
221 public String getModelUri() {
222 return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
223 }
224
225 @Override
226 public synchronized ITmfEventField getContent() {
227 if (fContent == null) {
228 fContent = new TmfEventField(
229 ITmfEventField.ROOT_FIELD_ID, null, parseFields(fEvent));
230 }
231 return fContent;
232 }
233
234 /**
235 * Extract the field information from the structDefinition haze-inducing
236 * mess, and put them into something ITmfEventField can cope with.
237 */
238 private static CtfTmfEventField[] parseFields(@NonNull EventDefinition eventDef) {
239 List<CtfTmfEventField> fields = new ArrayList<>();
240
241 ICompositeDefinition structFields = eventDef.getFields();
242 if (structFields != null) {
243 if (structFields.getFieldNames() != null) {
244 for (String curFieldName : structFields.getFieldNames()) {
245 fields.add(CtfTmfEventField.parseField((IDefinition) structFields.getDefinition(curFieldName), curFieldName));
246 }
247 }
248 }
249 /* Add context information as CtfTmfEventField */
250 ICompositeDefinition structContext = eventDef.getContext();
251 if (structContext != null) {
252 for (String contextName : structContext.getFieldNames()) {
253 /* Prefix field name */
254 String curContextName = CtfConstants.CONTEXT_FIELD_PREFIX + contextName;
255 fields.add(CtfTmfEventField.parseField((IDefinition) structContext.getDefinition(contextName), curContextName));
256 }
257 }
258
259 return fields.toArray(new CtfTmfEventField[fields.size()]);
260 }
261
262 }
This page took 0.035421 seconds and 5 git commands to generate.