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
.NonNull
;
24 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
25 import org
.eclipse
.jdt
.annotation
.Nullable
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
28 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
29 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfCustomAttributes
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
34 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
35 import org
.eclipse
.tracecompass
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
36 import org
.eclipse
.tracecompass
.tmf
.core
.timestamp
.TmfNanoTimestamp
;
37 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
38 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
39 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
42 * A wrapper class around CTF's Event Definition/Declaration that maps all types
43 * of Declaration to native Java types.
45 * @author Alexandre Montplaisir
48 public class CtfTmfEvent
extends TmfEvent
49 implements ITmfModelLookup
, ITmfCustomAttributes
{
51 // ------------------------------------------------------------------------
53 // ------------------------------------------------------------------------
55 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
57 // ------------------------------------------------------------------------
59 // Not part of this event's "definition", but used to populate lazy-loaded
61 // ------------------------------------------------------------------------
63 private final @Nullable IEventDeclaration fEventDeclaration
;
64 private final EventDefinition fEvent
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 /* Fields that are introduced by and part of this event's definition. */
71 private final int fSourceCpu
;
72 private final String fChannel
;
74 /** Field to override {@link TmfEvent#getName()}, to bypass the type-getting */
75 private final String fEventName
;
77 /** Lazy-loaded field containing the event's payload */
78 private transient @Nullable ITmfEventField fContent
;
80 /** Lazy-loaded field for the type, overriding TmfEvent's field */
81 private transient @Nullable CtfTmfEventType fEventType
;
83 // ------------------------------------------------------------------------
85 // ------------------------------------------------------------------------
88 * Constructor, used by {@link CtfTmfEventFactory#createEvent}.
90 * Only subclasses should call this. It is imperative that the subclass also
91 * has a constructor with the EXACT same parameter signature, because the
92 * factory will look for a constructor with the same arguments.
95 * The trace to which this event belongs
97 * The rank of the event
101 * The CTF channel of this event
105 * The event declaration
106 * @param eventDefinition
107 * The event definition
110 protected CtfTmfEvent(CtfTmfTrace trace
,
112 TmfNanoTimestamp timestamp
,
115 IEventDeclaration declaration
,
116 EventDefinition eventDefinition
) {
121 * Event type. We don't use TmfEvent's field here, we
122 * re-implement getType().
126 * Content handled with a lazy-loaded field re-implemented in
131 fEventDeclaration
= declaration
;
133 fEventName
= checkNotNull(declaration
.getName());
134 fEvent
= eventDefinition
;
139 * Inner constructor to create "null" events. Don't use this directly in
140 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
141 * get an instance of an empty event.
143 * There is no need to give higher visibility to this method than package
147 * The trace associated with this event
149 CtfTmfEvent(CtfTmfTrace trace
) {
151 ITmfContext
.UNKNOWN_RANK
,
152 new TmfNanoTimestamp(-1),
154 new TmfEventField("", null, new CtfTmfEventField
[0])); //$NON-NLS-1$
156 fEventName
= EMPTY_CTF_EVENT_NAME
;
157 fEventDeclaration
= null;
158 fEvent
= EventDefinition
.NULL_EVENT
;
159 fChannel
= ""; //$NON-NLS-1$
163 * Default constructor. Do not use directly, but it needs to be present
164 * because it's used in extension points, and the framework will use this
165 * constructor to get the class type.
167 * @deprecated Should not be called by normal code
170 public CtfTmfEvent() {
173 fEventName
= EMPTY_CTF_EVENT_NAME
;
174 fEventDeclaration
= null;
175 fEvent
= EventDefinition
.NULL_EVENT
;
176 fChannel
= ""; //$NON-NLS-1$
179 // ------------------------------------------------------------------------
180 // Getters/Setters/Predicates
181 // ------------------------------------------------------------------------
184 * Gets the cpu core the event was recorded on.
186 * @return The cpu id for a given source. In lttng it's from CPUINFO
188 public int getCPU() {
193 * Return the CTF trace's channel from which this event originates.
195 * @return The event's channel
198 public String
getChannel() {
203 * Return this event's reference.
205 * @return The event's reference
206 * @deprecated This method was replaced by {@link #getChannel()}.
209 public String
getReference() {
213 // ------------------------------------------------------------------------
215 // ------------------------------------------------------------------------
218 public CtfTmfTrace
getTrace() {
220 * Should be of the right type, since we take a CtfTmfTrace at the
223 return (CtfTmfTrace
) super.getTrace();
227 public synchronized ITmfEventType
getType() {
228 CtfTmfEventType type
= fEventType
;
230 type
= new CtfTmfEventType(fEventName
, getContent());
233 * Register the event type in the owning trace, but only if there is
236 getTrace().registerEventType(type
);
243 public String
getName() {
248 public synchronized ITmfEventField
getContent() {
249 ITmfEventField content
= fContent
;
250 if (content
== null) {
251 content
= new TmfEventField(
252 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
259 * Extract the field information from the structDefinition haze-inducing
260 * mess, and put them into something ITmfEventField can cope with.
262 private static CtfTmfEventField
[] parseFields(EventDefinition eventDef
) {
263 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
265 ICompositeDefinition structFields
= eventDef
.getFields();
266 if (structFields
!= null) {
267 if (structFields
.getFieldNames() != null) {
268 for (String curFieldName
: structFields
.getFieldNames()) {
269 String fn
= checkNotNull(curFieldName
);
270 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(fn
), fn
));
274 /* Add context information as CtfTmfEventField */
275 ICompositeDefinition structContext
= eventDef
.getContext();
276 if (structContext
!= null) {
277 for (String contextName
: structContext
.getFieldNames()) {
278 /* Prefix field name */
279 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
280 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
284 return checkNotNull(fields
.toArray(new @NonNull CtfTmfEventField
[fields
.size()]));
287 // ------------------------------------------------------------------------
288 // ITmfCustomAttributes
289 // ------------------------------------------------------------------------
292 public Set
<String
> listCustomAttributes() {
293 IEventDeclaration declaration
= fEventDeclaration
;
294 if (declaration
== null) {
295 return new HashSet
<>();
297 return declaration
.getCustomAttributes();
301 public @Nullable String
getCustomAttribute(@Nullable String name
) {
302 IEventDeclaration declaration
= fEventDeclaration
;
303 if (declaration
== null) {
306 return declaration
.getCustomAttribute(name
);
310 public @Nullable String
getModelUri() {
311 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
314 // ------------------------------------------------------------------------
316 // ------------------------------------------------------------------------
319 public int hashCode() {
320 final int prime
= 31;
321 int result
= super.hashCode();
322 result
= prime
* result
+ getCPU();
323 result
= prime
* result
+ getChannel().hashCode();
328 public boolean equals(@Nullable Object obj
) {
329 if (!super.equals(obj
)) {
332 /* super.equals() checks that the classes are the same */
333 CtfTmfEvent other
= checkNotNull((CtfTmfEvent
) obj
);
334 if (getCPU() != other
.getCPU()) {
337 if (!getChannel().equals(other
.getChannel())) {