1 /*******************************************************************************
2 * Copyright (c) 2011, 2015 Ericsson
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
10 * Alexandre Montplaisir - Initial API and implementation
11 * Bernd Hufmann - Updated for source and model lookup interfaces
12 *******************************************************************************/
14 package org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
;
16 import static org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
.checkNotNull
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashSet
;
20 import java
.util
.List
;
23 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
24 import org
.eclipse
.jdt
.annotation
.Nullable
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfCustomAttributes
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
38 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
41 * A wrapper class around CTF's Event Definition/Declaration that maps all types
42 * of Declaration to native Java types.
44 * @author Alexandre Montplaisir
47 public class CtfTmfEvent
extends TmfEvent
48 implements ITmfModelLookup
, ITmfCustomAttributes
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
56 // ------------------------------------------------------------------------
58 // Not part of this event's "definition", but used to populate lazy-loaded
60 // ------------------------------------------------------------------------
62 private final @Nullable IEventDeclaration fEventDeclaration
;
63 private final EventDefinition fEvent
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
69 /* Fields that are introduced by and part of this event's definition. */
70 private final int fSourceCpu
;
71 private final String fChannel
;
73 /** Field to override {@link TmfEvent#getName()}, to bypass the type-getting */
74 private final String fEventName
;
76 /** Lazy-loaded field containing the event's payload */
77 private transient @Nullable ITmfEventField fContent
;
79 /** Lazy-loaded field for the type, overriding TmfEvent's field */
80 private transient @Nullable CtfTmfEventType fEventType
;
82 // ------------------------------------------------------------------------
84 // ------------------------------------------------------------------------
87 * Constructor, used by {@link CtfTmfEventFactory#createEvent}.
89 * Only subclasses should call this. It is imperative that the subclass also
90 * has a constructor with the EXACT same parameter signature, because the
91 * factory will look for a constructor with the same arguments.
94 * The trace to which this event belongs
96 * The rank of the event
100 * The CTF channel of this event
104 * The event declaration
105 * @param eventDefinition
106 * The event definition
109 protected CtfTmfEvent(CtfTmfTrace trace
,
111 TmfNanoTimestamp timestamp
,
114 IEventDeclaration declaration
,
115 EventDefinition eventDefinition
) {
120 * Event type. We don't use TmfEvent's field here, we
121 * re-implement getType().
125 * Content handled with a lazy-loaded field re-implemented in
130 fEventDeclaration
= declaration
;
132 fEventName
= checkNotNull(declaration
.getName());
133 fEvent
= eventDefinition
;
138 * Inner constructor to create "null" events. Don't use this directly in
139 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
140 * get an instance of an empty event.
142 * There is no need to give higher visibility to this method than package
146 * The trace associated with this event
148 CtfTmfEvent(CtfTmfTrace trace
) {
150 ITmfContext
.UNKNOWN_RANK
,
151 new TmfNanoTimestamp(-1),
153 new TmfEventField("", null, new CtfTmfEventField
[0])); //$NON-NLS-1$
155 fEventName
= EMPTY_CTF_EVENT_NAME
;
156 fEventDeclaration
= null;
157 fEvent
= EventDefinition
.NULL_EVENT
;
158 fChannel
= ""; //$NON-NLS-1$
162 * Default constructor. Do not use directly, but it needs to be present
163 * because it's used in extension points, and the framework will use this
164 * constructor to get the class type.
166 * @deprecated Should not be called by normal code
169 public CtfTmfEvent() {
172 fEventName
= EMPTY_CTF_EVENT_NAME
;
173 fEventDeclaration
= null;
174 fEvent
= EventDefinition
.NULL_EVENT
;
175 fChannel
= ""; //$NON-NLS-1$
178 // ------------------------------------------------------------------------
179 // Getters/Setters/Predicates
180 // ------------------------------------------------------------------------
183 * Gets the cpu core the event was recorded on.
185 * @return The cpu id for a given source. In lttng it's from CPUINFO
187 public int getCPU() {
192 * Return the CTF trace's channel from which this event originates.
194 * @return The event's channel
197 public String
getChannel() {
202 * Return this event's reference.
204 * @return The event's reference
205 * @deprecated This method was replaced by {@link #getChannel()}.
208 public String
getReference() {
212 // ------------------------------------------------------------------------
214 // ------------------------------------------------------------------------
217 public CtfTmfTrace
getTrace() {
219 * Should be of the right type, since we take a CtfTmfTrace at the
222 return (CtfTmfTrace
) super.getTrace();
226 public synchronized ITmfEventType
getType() {
227 CtfTmfEventType type
= fEventType
;
229 type
= new CtfTmfEventType(fEventName
, getContent());
232 * Register the event type in the owning trace, but only if there is
235 getTrace().registerEventType(type
);
242 public String
getName() {
247 public synchronized ITmfEventField
getContent() {
248 ITmfEventField content
= fContent
;
249 if (content
== null) {
250 content
= new TmfEventField(
251 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
258 * Extract the field information from the structDefinition haze-inducing
259 * mess, and put them into something ITmfEventField can cope with.
261 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
262 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
264 ICompositeDefinition structFields
= eventDef
.getFields();
265 if (structFields
!= null) {
266 if (structFields
.getFieldNames() != null) {
267 for (String curFieldName
: structFields
.getFieldNames()) {
268 String fn
= checkNotNull(curFieldName
);
269 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(fn
), fn
));
273 /* Add context information as CtfTmfEventField */
274 ICompositeDefinition structContext
= eventDef
.getContext();
275 if (structContext
!= null) {
276 for (String contextName
: structContext
.getFieldNames()) {
277 /* Prefix field name */
278 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
279 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
283 return checkNotNull(fields
.toArray(new CtfTmfEventField
[fields
.size()]));
286 // ------------------------------------------------------------------------
287 // ITmfCustomAttributes
288 // ------------------------------------------------------------------------
291 public Set
<String
> listCustomAttributes() {
292 IEventDeclaration declaration
= fEventDeclaration
;
293 if (declaration
== null) {
294 return new HashSet
<>();
296 return checkNotNull(declaration
.getCustomAttributes());
300 public @Nullable String
getCustomAttribute(@Nullable String name
) {
301 IEventDeclaration declaration
= fEventDeclaration
;
302 if (declaration
== null) {
305 return declaration
.getCustomAttribute(name
);
309 public @Nullable String
getModelUri() {
310 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
313 // ------------------------------------------------------------------------
315 // ------------------------------------------------------------------------
318 public int hashCode() {
319 final int prime
= 31;
320 int result
= super.hashCode();
321 result
= prime
* result
+ getCPU();
322 result
= prime
* result
+ getChannel().hashCode();
327 public boolean equals(@Nullable Object obj
) {
328 if (!super.equals(obj
)) {
331 /* super.equals() checks that the classes are the same */
332 CtfTmfEvent other
= checkNotNull((CtfTmfEvent
) obj
);
333 if (getCPU() != other
.getCPU()) {
336 if (!getChannel().equals(other
.getChannel())) {