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 java
.util
.ArrayList
;
17 import java
.util
.HashSet
;
18 import java
.util
.List
;
21 import org
.eclipse
.jdt
.annotation
.NonNull
;
22 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
26 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfCustomAttributes
;
27 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventField
;
28 import org
.eclipse
.tracecompass
.tmf
.core
.event
.ITmfEventType
;
29 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEvent
;
30 import org
.eclipse
.tracecompass
.tmf
.core
.event
.TmfEventField
;
31 import org
.eclipse
.tracecompass
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
32 import org
.eclipse
.tracecompass
.tmf
.core
.event
.lookup
.ITmfSourceLookup
;
33 import org
.eclipse
.tracecompass
.tmf
.core
.trace
.ITmfContext
;
34 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
35 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.lookup
.CtfTmfCallsite
;
36 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.timestamp
.CtfTmfTimestamp
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.trace
.CtfTmfTrace
;
40 * A wrapper class around CTF's Event Definition/Declaration that maps all types
41 * of Declaration to native Java types.
43 * @author Alexandre Montplaisir
45 public class CtfTmfEvent
extends TmfEvent
46 implements ITmfSourceLookup
, ITmfModelLookup
, ITmfCustomAttributes
{
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
54 // ------------------------------------------------------------------------
56 // ------------------------------------------------------------------------
58 private final int fSourceCPU
;
59 private final long fTypeId
;
60 private final String fEventName
;
61 private final IEventDeclaration fEventDeclaration
;
62 private final @NonNull EventDefinition fEvent
;
63 private final String fReference
;
65 /** Lazy-loaded field containing the event's payload */
66 private ITmfEventField fContent
;
68 private CtfTmfEventType fCtfTmfEventType
;
70 // ------------------------------------------------------------------------
72 // ------------------------------------------------------------------------
75 * Constructor used by {@link CtfTmfEventFactory#createEvent}
77 CtfTmfEvent(CtfTmfTrace trace
, long rank
, CtfTmfTimestamp timestamp
,
78 String fileName
, int cpu
, IEventDeclaration declaration
, @NonNull EventDefinition eventDefinition
) {
83 * Event type. We don't use TmfEvent's field here, we
84 * re-implement getType().
88 * Content handled with a lazy-loaded field re-implemented in
93 fEventDeclaration
= declaration
;
95 fTypeId
= declaration
.getId().longValue();
96 fEventName
= declaration
.getName();
97 fEvent
= eventDefinition
;
98 fReference
= fileName
;
102 * Inner constructor to create "null" events. Don't use this directly in
103 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
104 * get an instance of an empty event.
106 * There is no need to give higher visibility to this method than package
110 * The trace associated with this event
112 CtfTmfEvent(CtfTmfTrace trace
) {
114 ITmfContext
.UNKNOWN_RANK
,
115 new CtfTmfTimestamp(-1),
117 new TmfEventField("", null, new CtfTmfEventField
[0])); //$NON-NLS-1$
120 fEventName
= EMPTY_CTF_EVENT_NAME
;
121 fEventDeclaration
= null;
122 fEvent
= EventDefinition
.NULL_EVENT
;
127 * Default constructor. Do not use directly, but it needs to be present
128 * because it's used in extension points, and the framework will use this
129 * constructor to get the class type.
131 public CtfTmfEvent() {
135 // ------------------------------------------------------------------------
136 // Getters/Setters/Predicates
137 // ------------------------------------------------------------------------
140 * Gets the cpu core the event was recorded on.
142 * @return The cpu id for a given source. In lttng it's from CPUINFO
144 public int getCPU() {
149 * Return this event's ID, according to the trace's metadata.
151 * Watch out, this ID is not constant from one trace to another for the same
152 * event types! Use "getEventName()" for a constant reference.
154 * @return The event ID
156 public long getID() {
161 * Return this event's reference
163 * @return The event's reference
165 public String
getReference() {
170 public CtfTmfTrace
getTrace() {
172 * Should be of the right type, since we take a CtfTmfTrace at the
175 return (CtfTmfTrace
) super.getTrace();
179 public ITmfEventType
getType() {
180 if (fCtfTmfEventType
== null) {
181 fCtfTmfEventType
= new CtfTmfEventType(fEventName
, getContent());
184 * Register the event type in the owning trace, but only if there is
187 getTrace().registerEventType(fCtfTmfEventType
);
189 return fCtfTmfEventType
;
193 public Set
<String
> listCustomAttributes() {
194 if (fEventDeclaration
== null) {
195 return new HashSet
<>();
197 return fEventDeclaration
.getCustomAttributes();
201 public String
getCustomAttribute(String name
) {
202 if (fEventDeclaration
== null) {
205 return fEventDeclaration
.getCustomAttribute(name
);
209 * Get the call site for this event.
211 * @return the call site information, or null if there is none
214 public CtfTmfCallsite
getCallsite() {
215 CtfTmfCallsite callsite
= null;
216 CtfTmfTrace trace
= getTrace();
218 if (getContent() != null) {
219 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
220 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
221 long ip
= (Long
) ipField
.getValue();
222 callsite
= trace
.getCallsite(fEventName
, ip
);
225 if (callsite
== null) {
226 callsite
= trace
.getCallsite(fEventName
);
232 public String
getModelUri() {
233 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
237 public synchronized ITmfEventField
getContent() {
238 if (fContent
== null) {
239 fContent
= new TmfEventField(
240 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
246 * Extract the field information from the structDefinition haze-inducing
247 * mess, and put them into something ITmfEventField can cope with.
249 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
250 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
252 ICompositeDefinition structFields
= eventDef
.getFields();
253 if (structFields
!= null) {
254 if (structFields
.getFieldNames() != null) {
255 for (String curFieldName
: structFields
.getFieldNames()) {
256 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(curFieldName
), curFieldName
));
260 /* Add context information as CtfTmfEventField */
261 ICompositeDefinition structContext
= eventDef
.getContext();
262 if (structContext
!= null) {
263 for (String contextName
: structContext
.getFieldNames()) {
264 /* Prefix field name */
265 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
266 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
270 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);