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
.tracecompass
.ctf
.core
.event
.EventDefinition
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
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
.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
.event
.lookup
.CtfTmfCallsite
;
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
47 public class CtfTmfEvent
extends TmfEvent
48 implements ITmfSourceLookup
, ITmfModelLookup
, ITmfCustomAttributes
{
50 // ------------------------------------------------------------------------
52 // ------------------------------------------------------------------------
54 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
56 // ------------------------------------------------------------------------
58 // ------------------------------------------------------------------------
60 private final int fSourceCPU
;
61 private final long fTypeId
;
62 private final String fEventName
;
63 private final IEventDeclaration fEventDeclaration
;
64 private final @NonNull EventDefinition fEvent
;
65 private final String fReference
;
67 /** Lazy-loaded field containing the event's payload */
68 private ITmfEventField fContent
;
70 private CtfTmfEventType fCtfTmfEventType
;
72 // ------------------------------------------------------------------------
74 // ------------------------------------------------------------------------
77 * Constructor used by {@link CtfTmfEventFactory#createEvent}
79 CtfTmfEvent(CtfTmfTrace trace
, long rank
, TmfNanoTimestamp timestamp
,
80 String fileName
, int cpu
, IEventDeclaration declaration
, @NonNull EventDefinition eventDefinition
) {
85 * Event type. We don't use TmfEvent's field here, we
86 * re-implement getType().
90 * Content handled with a lazy-loaded field re-implemented in
95 fEventDeclaration
= declaration
;
97 fTypeId
= declaration
.getId().longValue();
98 fEventName
= declaration
.getName();
99 fEvent
= eventDefinition
;
100 fReference
= fileName
;
104 * Inner constructor to create "null" events. Don't use this directly in
105 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
106 * get an instance of an empty event.
108 * There is no need to give higher visibility to this method than package
112 * The trace associated with this event
114 CtfTmfEvent(CtfTmfTrace trace
) {
116 ITmfContext
.UNKNOWN_RANK
,
117 new TmfNanoTimestamp(-1),
119 new TmfEventField("", null, new CtfTmfEventField
[0])); //$NON-NLS-1$
122 fEventName
= EMPTY_CTF_EVENT_NAME
;
123 fEventDeclaration
= null;
124 fEvent
= EventDefinition
.NULL_EVENT
;
129 * Default constructor. Do not use directly, but it needs to be present
130 * because it's used in extension points, and the framework will use this
131 * constructor to get the class type.
133 public CtfTmfEvent() {
137 // ------------------------------------------------------------------------
138 // Getters/Setters/Predicates
139 // ------------------------------------------------------------------------
142 * Gets the cpu core the event was recorded on.
144 * @return The cpu id for a given source. In lttng it's from CPUINFO
146 public int getCPU() {
151 * Return this event's ID, according to the trace's metadata.
153 * Watch out, this ID is not constant from one trace to another for the same
154 * event types! Use "getEventName()" for a constant reference.
156 * @return The event ID
158 public long getID() {
163 * Return this event's reference
165 * @return The event's reference
167 public String
getReference() {
172 public CtfTmfTrace
getTrace() {
174 * Should be of the right type, since we take a CtfTmfTrace at the
177 return (CtfTmfTrace
) super.getTrace();
181 public ITmfEventType
getType() {
182 if (fCtfTmfEventType
== null) {
183 fCtfTmfEventType
= new CtfTmfEventType(fEventName
, getContent());
186 * Register the event type in the owning trace, but only if there is
189 getTrace().registerEventType(fCtfTmfEventType
);
191 return fCtfTmfEventType
;
195 public String
getName() {
200 public Set
<String
> listCustomAttributes() {
201 if (fEventDeclaration
== null) {
202 return new HashSet
<>();
204 return fEventDeclaration
.getCustomAttributes();
208 public String
getCustomAttribute(String name
) {
209 if (fEventDeclaration
== null) {
212 return fEventDeclaration
.getCustomAttribute(name
);
216 * Get the call site for this event.
218 * @return the call site information, or null if there is none
221 public CtfTmfCallsite
getCallsite() {
222 CtfTmfCallsite callsite
= null;
223 CtfTmfTrace trace
= getTrace();
225 if (getContent() != null) {
226 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
227 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
228 long ip
= (Long
) ipField
.getValue();
229 callsite
= trace
.getCallsite(fEventName
, ip
);
232 if (callsite
== null) {
233 callsite
= trace
.getCallsite(fEventName
);
239 public String
getModelUri() {
240 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
244 public synchronized ITmfEventField
getContent() {
245 if (fContent
== null) {
246 fContent
= new TmfEventField(
247 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
253 * Extract the field information from the structDefinition haze-inducing
254 * mess, and put them into something ITmfEventField can cope with.
256 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
257 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
259 ICompositeDefinition structFields
= eventDef
.getFields();
260 if (structFields
!= null) {
261 if (structFields
.getFieldNames() != null) {
262 for (String curFieldName
: structFields
.getFieldNames()) {
263 String fn
= checkNotNull(curFieldName
);
264 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(fn
), fn
));
268 /* Add context information as CtfTmfEventField */
269 ICompositeDefinition structContext
= eventDef
.getContext();
270 if (structContext
!= null) {
271 for (String contextName
: structContext
.getFieldNames()) {
272 /* Prefix field name */
273 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
274 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
278 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);