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
.linuxtools
.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
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.EventDefinition
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfCustomAttributes
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfSourceLookup
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
37 * A wrapper class around CTF's Event Definition/Declaration that maps all types
38 * of Declaration to native Java types.
41 * @author Alexandre Montplaisir
44 public class CtfTmfEvent
extends TmfEvent
45 implements ITmfSourceLookup
, ITmfModelLookup
, ITmfCustomAttributes
{
47 // ------------------------------------------------------------------------
49 // ------------------------------------------------------------------------
51 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
53 // ------------------------------------------------------------------------
55 // ------------------------------------------------------------------------
57 private final int fSourceCPU
;
58 private final long fTypeId
;
59 private final String fEventName
;
60 private final IEventDeclaration fEventDeclaration
;
62 private final EventDefinition fEvent
;
63 private ITmfEventField fContent
;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 * Constructor used by {@link CtfTmfEventFactory#createEvent}
72 CtfTmfEvent(CtfTmfTrace trace
, long rank
, CtfTmfTimestamp timestamp
,
73 String fileName
, int cpu
, IEventDeclaration declaration
, @NonNull EventDefinition eventDefinition
) {
77 String
.valueOf(cpu
), // Source
78 null, // Event type. We don't use TmfEvent's field here, we
79 // re-implement getType()
80 null, // Content handled with a lazy loaded re-implemented in
85 fEventDeclaration
= declaration
;
87 fTypeId
= declaration
.getId();
88 fEventName
= declaration
.getName();
89 fEvent
= eventDefinition
;
94 * Inner constructor to create "null" events. Don't use this directly in
95 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
96 * instance of an empty event.
98 * This needs to be public however because it's used in extension points,
99 * and the framework will use this constructor to get the class type.
101 public CtfTmfEvent() {
103 ITmfContext
.UNKNOWN_RANK
,
104 new CtfTmfTimestamp(-1),
107 new TmfEventField("", null, new CtfTmfEventField
[0]), //$NON-NLS-1$
111 fEventName
= EMPTY_CTF_EVENT_NAME
;
112 fEventDeclaration
= null;
113 fEvent
= EventDefinition
.NULL_EVENT
;
116 // ------------------------------------------------------------------------
117 // Getters/Setters/Predicates
118 // ------------------------------------------------------------------------
121 * Gets the cpu core the event was recorded on.
123 * @return The cpu id for a given source. In lttng it's from CPUINFO
125 public int getCPU() {
130 * Return this event's ID, according to the trace's metadata.
132 * Watch out, this ID is not constant from one trace to another for the same
133 * event types! Use "getEventName()" for a constant reference.
135 * @return The event ID
137 public long getID() {
142 public CtfTmfTrace
getTrace() {
144 * Should be of the right type, since we take a CtfTmfTrace at the
147 return (CtfTmfTrace
) super.getTrace();
151 public ITmfEventType
getType() {
152 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(getTrace(), fEventName
);
153 if (ctfTmfEventType
== null) {
154 /* Should only return null the first time */
155 ctfTmfEventType
= new CtfTmfEventType(fEventName
, getTrace(), getContent());
157 return ctfTmfEventType
;
164 public Set
<String
> listCustomAttributes() {
165 if (fEventDeclaration
== null) {
166 return new HashSet
<>();
168 return fEventDeclaration
.getCustomAttributes();
175 public String
getCustomAttribute(String name
) {
176 if (fEventDeclaration
== null) {
179 return fEventDeclaration
.getCustomAttribute(name
);
183 * Get the call site for this event.
185 * @return the call site information, or null if there is none
189 public CtfTmfCallsite
getCallsite() {
190 CTFCallsite callsite
= null;
191 CtfTmfTrace trace
= getTrace();
195 CTFTrace ctfTrace
= trace
.getCTFTrace();
196 /* Should not happen, but it is a good check */
197 if (ctfTrace
== null) {
200 if (getContent() != null) {
201 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
202 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
203 long ip
= (Long
) ipField
.getValue();
204 callsite
= ctfTrace
.getCallsite(fEventName
, ip
);
207 if (callsite
== null) {
208 callsite
= ctfTrace
.getCallsite(fEventName
);
210 if (callsite
!= null) {
211 return new CtfTmfCallsite(callsite
);
220 public String
getModelUri() {
221 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
225 public synchronized ITmfEventField
getContent() {
226 if (fContent
== null) {
227 fContent
= new TmfEventField(
228 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
234 * Extract the field information from the structDefinition haze-inducing
235 * mess, and put them into something ITmfEventField can cope with.
237 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
238 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
240 StructDefinition structFields
= eventDef
.getFields();
241 if (structFields
!= null) {
242 if (structFields
.getFieldNames() != null) {
243 for (String curFieldName
: structFields
.getFieldNames()) {
244 fields
.add(CtfTmfEventField
.parseField(structFields
.getDefinition(curFieldName
), curFieldName
));
248 /* Add context information as CtfTmfEventField */
249 StructDefinition structContext
= eventDef
.getContext();
250 if (structContext
!= null) {
251 for (String contextName
: structContext
.getFieldNames()) {
252 /* Prefix field name */
253 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
254 fields
.add(CtfTmfEventField
.parseField(structContext
.getDefinition(contextName
), curContextName
));
258 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);