1 /*******************************************************************************
2 * Copyright (c) 2011, 2014 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
.CTFCallsite
;
23 import org
.eclipse
.tracecompass
.ctf
.core
.event
.EventDefinition
;
24 import org
.eclipse
.tracecompass
.ctf
.core
.event
.IEventDeclaration
;
25 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.ICompositeDefinition
;
26 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDefinition
;
27 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
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
.trace
.ITmfContext
;
36 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.CtfConstants
;
37 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.event
.lookup
.CtfTmfCallsite
;
38 import org
.eclipse
.tracecompass
.tmf
.ctf
.core
.timestamp
.CtfTmfTimestamp
;
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.
46 * @author Alexandre Montplaisir
49 public class CtfTmfEvent
extends TmfEvent
50 implements ITmfSourceLookup
, ITmfModelLookup
, ITmfCustomAttributes
{
52 // ------------------------------------------------------------------------
54 // ------------------------------------------------------------------------
56 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
58 // ------------------------------------------------------------------------
60 // ------------------------------------------------------------------------
62 private final int fSourceCPU
;
63 private final long fTypeId
;
64 private final String fEventName
;
65 private final IEventDeclaration fEventDeclaration
;
67 private final EventDefinition fEvent
;
68 private ITmfEventField fContent
;
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
) {
82 String
.valueOf(cpu
), // Source
83 null, // Event type. We don't use TmfEvent's field here, we
84 // re-implement getType()
85 null, // Content handled with a lazy loaded re-implemented in
90 fEventDeclaration
= declaration
;
92 fTypeId
= declaration
.getId().longValue();
93 fEventName
= declaration
.getName();
94 fEvent
= eventDefinition
;
99 * Inner constructor to create "null" events. Don't use this directly in
100 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
101 * get an instance of an empty event.
103 * There is no need to give higher visibility to this method than package
107 * The trace associated with this event
109 CtfTmfEvent(CtfTmfTrace trace
) {
111 ITmfContext
.UNKNOWN_RANK
,
112 new CtfTmfTimestamp(-1),
115 new TmfEventField("", null, new CtfTmfEventField
[0]), //$NON-NLS-1$
119 fEventName
= EMPTY_CTF_EVENT_NAME
;
120 fEventDeclaration
= null;
121 fEvent
= EventDefinition
.NULL_EVENT
;
125 * Default constructor. Do not use directly, but it needs to be present
126 * because it's used in extension points, and the framework will use this
127 * constructor to get the class type.
129 public CtfTmfEvent() {
133 // ------------------------------------------------------------------------
134 // Getters/Setters/Predicates
135 // ------------------------------------------------------------------------
138 * Gets the cpu core the event was recorded on.
140 * @return The cpu id for a given source. In lttng it's from CPUINFO
142 public int getCPU() {
147 * Return this event's ID, according to the trace's metadata.
149 * Watch out, this ID is not constant from one trace to another for the same
150 * event types! Use "getEventName()" for a constant reference.
152 * @return The event ID
154 public long getID() {
159 public CtfTmfTrace
getTrace() {
161 * Should be of the right type, since we take a CtfTmfTrace at the
164 return (CtfTmfTrace
) super.getTrace();
168 public ITmfEventType
getType() {
169 CtfTmfEventType ctfTmfEventType
= new CtfTmfEventType(fEventName
, getContent());
171 /* Register the event type in the owning trace, but only if there is one */
172 CtfTmfTrace trace
= getTrace();
173 trace
.registerEventType(ctfTmfEventType
);
175 return ctfTmfEventType
;
182 public Set
<String
> listCustomAttributes() {
183 if (fEventDeclaration
== null) {
184 return new HashSet
<>();
186 return fEventDeclaration
.getCustomAttributes();
193 public String
getCustomAttribute(String name
) {
194 if (fEventDeclaration
== null) {
197 return fEventDeclaration
.getCustomAttribute(name
);
201 * Get the call site for this event.
203 * @return the call site information, or null if there is none
207 public CtfTmfCallsite
getCallsite() {
208 CTFCallsite callsite
= null;
209 CtfTmfTrace trace
= getTrace();
210 CTFTrace ctfTrace
= trace
.getCTFTrace();
211 /* Should not happen, but it is a good check */
212 if (ctfTrace
== null) {
215 if (getContent() != null) {
216 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
217 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
218 long ip
= (Long
) ipField
.getValue();
219 callsite
= ctfTrace
.getCallsite(fEventName
, ip
);
222 if (callsite
== null) {
223 callsite
= ctfTrace
.getCallsite(fEventName
);
225 if (callsite
!= null) {
226 return new CtfTmfCallsite(callsite
);
235 public String
getModelUri() {
236 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
240 public synchronized ITmfEventField
getContent() {
241 if (fContent
== null) {
242 fContent
= new TmfEventField(
243 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
249 * Extract the field information from the structDefinition haze-inducing
250 * mess, and put them into something ITmfEventField can cope with.
252 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
253 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
255 ICompositeDefinition structFields
= eventDef
.getFields();
256 if (structFields
!= null) {
257 if (structFields
.getFieldNames() != null) {
258 for (String curFieldName
: structFields
.getFieldNames()) {
259 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(curFieldName
), curFieldName
));
263 /* Add context information as CtfTmfEventField */
264 ICompositeDefinition structContext
= eventDef
.getContext();
265 if (structContext
!= null) {
266 for (String contextName
: structContext
.getFieldNames()) {
267 /* Prefix field name */
268 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
269 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
273 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);