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
.ICompositeDefinition
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDefinition
;
27 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFTrace
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfCustomAttributes
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfSourceLookup
;
35 import org
.eclipse
.linuxtools
.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()} to get an
97 * instance of an empty event.
99 * This needs to be public however because it's used in extension points,
100 * and the framework will use this constructor to get the class type.
102 public CtfTmfEvent() {
104 ITmfContext
.UNKNOWN_RANK
,
105 new CtfTmfTimestamp(-1),
108 new TmfEventField("", null, new CtfTmfEventField
[0]), //$NON-NLS-1$
112 fEventName
= EMPTY_CTF_EVENT_NAME
;
113 fEventDeclaration
= null;
114 fEvent
= EventDefinition
.NULL_EVENT
;
117 // ------------------------------------------------------------------------
118 // Getters/Setters/Predicates
119 // ------------------------------------------------------------------------
122 * Gets the cpu core the event was recorded on.
124 * @return The cpu id for a given source. In lttng it's from CPUINFO
126 public int getCPU() {
131 * Return this event's ID, according to the trace's metadata.
133 * Watch out, this ID is not constant from one trace to another for the same
134 * event types! Use "getEventName()" for a constant reference.
136 * @return The event ID
138 public long getID() {
143 public CtfTmfTrace
getTrace() {
145 * Should be of the right type, since we take a CtfTmfTrace at the
148 return (CtfTmfTrace
) super.getTrace();
152 public ITmfEventType
getType() {
153 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(getTrace(), fEventName
);
154 if (ctfTmfEventType
== null) {
155 /* Should only return null the first time */
156 ctfTmfEventType
= new CtfTmfEventType(fEventName
, getTrace(), getContent());
158 return ctfTmfEventType
;
165 public Set
<String
> listCustomAttributes() {
166 if (fEventDeclaration
== null) {
167 return new HashSet
<>();
169 return fEventDeclaration
.getCustomAttributes();
176 public String
getCustomAttribute(String name
) {
177 if (fEventDeclaration
== null) {
180 return fEventDeclaration
.getCustomAttribute(name
);
184 * Get the call site for this event.
186 * @return the call site information, or null if there is none
190 public CtfTmfCallsite
getCallsite() {
191 CTFCallsite callsite
= null;
192 CtfTmfTrace trace
= getTrace();
196 CTFTrace ctfTrace
= trace
.getCTFTrace();
197 /* Should not happen, but it is a good check */
198 if (ctfTrace
== null) {
201 if (getContent() != null) {
202 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
203 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
204 long ip
= (Long
) ipField
.getValue();
205 callsite
= ctfTrace
.getCallsite(fEventName
, ip
);
208 if (callsite
== null) {
209 callsite
= ctfTrace
.getCallsite(fEventName
);
211 if (callsite
!= null) {
212 return new CtfTmfCallsite(callsite
);
221 public String
getModelUri() {
222 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
226 public synchronized ITmfEventField
getContent() {
227 if (fContent
== null) {
228 fContent
= new TmfEventField(
229 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
235 * Extract the field information from the structDefinition haze-inducing
236 * mess, and put them into something ITmfEventField can cope with.
238 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
239 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
241 ICompositeDefinition structFields
= eventDef
.getFields();
242 if (structFields
!= null) {
243 if (structFields
.getFieldNames() != null) {
244 for (String curFieldName
: structFields
.getFieldNames()) {
245 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(curFieldName
), curFieldName
));
249 /* Add context information as CtfTmfEventField */
250 ICompositeDefinition structContext
= eventDef
.getContext();
251 if (structContext
!= null) {
252 for (String contextName
: structContext
.getFieldNames()) {
253 /* Prefix field name */
254 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
255 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
259 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);