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
;
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
;
38 * A wrapper class around CTF's Event Definition/Declaration that maps all types
39 * of Declaration to native Java types.
42 * @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
;
63 private final EventDefinition fEvent
;
64 private ITmfEventField fContent
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 * Constructor used by {@link CtfTmfEventFactory#createEvent}
73 CtfTmfEvent(CtfTmfTrace trace
, long rank
, CtfTmfTimestamp timestamp
,
74 String fileName
, int cpu
, IEventDeclaration declaration
, @NonNull EventDefinition eventDefinition
) {
78 String
.valueOf(cpu
), // Source
79 null, // Event type. We don't use TmfEvent's field here, we
80 // re-implement getType()
81 null, // Content handled with a lazy loaded re-implemented in
86 fEventDeclaration
= declaration
;
88 fTypeId
= declaration
.getId().longValue();
89 fEventName
= declaration
.getName();
90 fEvent
= eventDefinition
;
95 * Inner constructor to create "null" events. Don't use this directly in
96 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
97 * get an instance of an empty event.
99 * There is no need to give higher visibility to this method than package
103 * The trace associated with this event
105 CtfTmfEvent(CtfTmfTrace trace
) {
107 ITmfContext
.UNKNOWN_RANK
,
108 new CtfTmfTimestamp(-1),
111 new TmfEventField("", null, new CtfTmfEventField
[0]), //$NON-NLS-1$
115 fEventName
= EMPTY_CTF_EVENT_NAME
;
116 fEventDeclaration
= null;
117 fEvent
= EventDefinition
.NULL_EVENT
;
121 * Default constructor. Do not use directly, but it needs to be present
122 * because it's used in extension points, and the framework will use this
123 * constructor to get the class type.
125 public CtfTmfEvent() {
129 // ------------------------------------------------------------------------
130 // Getters/Setters/Predicates
131 // ------------------------------------------------------------------------
134 * Gets the cpu core the event was recorded on.
136 * @return The cpu id for a given source. In lttng it's from CPUINFO
138 public int getCPU() {
143 * Return this event's ID, according to the trace's metadata.
145 * Watch out, this ID is not constant from one trace to another for the same
146 * event types! Use "getEventName()" for a constant reference.
148 * @return The event ID
150 public long getID() {
155 public CtfTmfTrace
getTrace() {
157 * Should be of the right type, since we take a CtfTmfTrace at the
160 return (CtfTmfTrace
) super.getTrace();
164 public ITmfEventType
getType() {
165 CtfTmfEventType ctfTmfEventType
= new CtfTmfEventType(fEventName
, getContent());
167 /* Register the event type in the owning trace, but only if there is one */
168 CtfTmfTrace trace
= getTrace();
169 trace
.registerEventType(ctfTmfEventType
);
171 return ctfTmfEventType
;
178 public Set
<String
> listCustomAttributes() {
179 if (fEventDeclaration
== null) {
180 return new HashSet
<>();
182 return fEventDeclaration
.getCustomAttributes();
189 public String
getCustomAttribute(String name
) {
190 if (fEventDeclaration
== null) {
193 return fEventDeclaration
.getCustomAttribute(name
);
197 * Get the call site for this event.
199 * @return the call site information, or null if there is none
203 public CtfTmfCallsite
getCallsite() {
204 CTFCallsite callsite
= null;
205 CtfTmfTrace trace
= getTrace();
206 CTFTrace ctfTrace
= trace
.getCTFTrace();
207 /* Should not happen, but it is a good check */
208 if (ctfTrace
== null) {
211 if (getContent() != null) {
212 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
213 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
214 long ip
= (Long
) ipField
.getValue();
215 callsite
= ctfTrace
.getCallsite(fEventName
, ip
);
218 if (callsite
== null) {
219 callsite
= ctfTrace
.getCallsite(fEventName
);
221 if (callsite
!= null) {
222 return new CtfTmfCallsite(callsite
);
231 public String
getModelUri() {
232 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
236 public synchronized ITmfEventField
getContent() {
237 if (fContent
== null) {
238 fContent
= new TmfEventField(
239 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
245 * Extract the field information from the structDefinition haze-inducing
246 * mess, and put them into something ITmfEventField can cope with.
248 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
249 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
251 ICompositeDefinition structFields
= eventDef
.getFields();
252 if (structFields
!= null) {
253 if (structFields
.getFieldNames() != null) {
254 for (String curFieldName
: structFields
.getFieldNames()) {
255 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(curFieldName
), curFieldName
));
259 /* Add context information as CtfTmfEventField */
260 ICompositeDefinition structContext
= eventDef
.getContext();
261 if (structContext
!= null) {
262 for (String contextName
: structContext
.getFieldNames()) {
263 /* Prefix field name */
264 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
265 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
269 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);