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
;
66 private final @NonNull EventDefinition fEvent
;
67 private final String fSource
;
68 private final String fReference
;
70 /** Lazy-loaded field containing the event's payload */
71 private ITmfEventField fContent
;
73 // ------------------------------------------------------------------------
75 // ------------------------------------------------------------------------
78 * Constructor used by {@link CtfTmfEventFactory#createEvent}
80 CtfTmfEvent(CtfTmfTrace trace
, long rank
, CtfTmfTimestamp timestamp
,
81 String fileName
, int cpu
, IEventDeclaration declaration
, @NonNull EventDefinition eventDefinition
) {
86 * Event type. We don't use TmfEvent's field here, we
87 * re-implement getType().
91 * Content handled with a lazy-loaded field re-implemented in
97 fEventDeclaration
= declaration
;
99 fTypeId
= declaration
.getId().longValue();
100 fEventName
= declaration
.getName();
101 fEvent
= eventDefinition
;
102 fSource
= String
.valueOf(cpu
);
103 fReference
= fileName
;
107 * Inner constructor to create "null" events. Don't use this directly in
108 * normal usage, use {@link CtfTmfEventFactory#getNullEvent(CtfTmfTrace)} to
109 * get an instance of an empty event.
111 * There is no need to give higher visibility to this method than package
115 * The trace associated with this event
117 CtfTmfEvent(CtfTmfTrace trace
) {
119 ITmfContext
.UNKNOWN_RANK
,
120 new CtfTmfTimestamp(-1),
122 new TmfEventField("", null, new CtfTmfEventField
[0])); //$NON-NLS-1$
125 fEventName
= EMPTY_CTF_EVENT_NAME
;
126 fEventDeclaration
= null;
127 fEvent
= EventDefinition
.NULL_EVENT
;
133 * Default constructor. Do not use directly, but it needs to be present
134 * because it's used in extension points, and the framework will use this
135 * constructor to get the class type.
137 public CtfTmfEvent() {
141 // ------------------------------------------------------------------------
142 // Getters/Setters/Predicates
143 // ------------------------------------------------------------------------
146 * Gets the cpu core the event was recorded on.
148 * @return The cpu id for a given source. In lttng it's from CPUINFO
150 public int getCPU() {
155 * Return this event's ID, according to the trace's metadata.
157 * Watch out, this ID is not constant from one trace to another for the same
158 * event types! Use "getEventName()" for a constant reference.
160 * @return The event ID
162 public long getID() {
167 * Return this event's source CPU.
169 * @return The source CPU
171 public String
getSource() {
176 * Return this event's reference
178 * @return The event's reference
180 public String
getReference() {
185 public CtfTmfTrace
getTrace() {
187 * Should be of the right type, since we take a CtfTmfTrace at the
190 return (CtfTmfTrace
) super.getTrace();
194 public ITmfEventType
getType() {
195 CtfTmfEventType ctfTmfEventType
= new CtfTmfEventType(fEventName
, getContent());
197 /* Register the event type in the owning trace, but only if there is one */
198 CtfTmfTrace trace
= getTrace();
199 trace
.registerEventType(ctfTmfEventType
);
201 return ctfTmfEventType
;
208 public Set
<String
> listCustomAttributes() {
209 if (fEventDeclaration
== null) {
210 return new HashSet
<>();
212 return fEventDeclaration
.getCustomAttributes();
219 public String
getCustomAttribute(String name
) {
220 if (fEventDeclaration
== null) {
223 return fEventDeclaration
.getCustomAttribute(name
);
227 * Get the call site for this event.
229 * @return the call site information, or null if there is none
233 public CtfTmfCallsite
getCallsite() {
234 CTFCallsite callsite
= null;
235 CtfTmfTrace trace
= getTrace();
236 CTFTrace ctfTrace
= trace
.getCTFTrace();
237 /* Should not happen, but it is a good check */
238 if (ctfTrace
== null) {
241 if (getContent() != null) {
242 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
243 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
244 long ip
= (Long
) ipField
.getValue();
245 callsite
= ctfTrace
.getCallsite(fEventName
, ip
);
248 if (callsite
== null) {
249 callsite
= ctfTrace
.getCallsite(fEventName
);
251 if (callsite
!= null) {
252 return new CtfTmfCallsite(callsite
);
261 public String
getModelUri() {
262 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
266 public synchronized ITmfEventField
getContent() {
267 if (fContent
== null) {
268 fContent
= new TmfEventField(
269 ITmfEventField
.ROOT_FIELD_ID
, null, parseFields(fEvent
));
275 * Extract the field information from the structDefinition haze-inducing
276 * mess, and put them into something ITmfEventField can cope with.
278 private static CtfTmfEventField
[] parseFields(@NonNull EventDefinition eventDef
) {
279 List
<CtfTmfEventField
> fields
= new ArrayList
<>();
281 ICompositeDefinition structFields
= eventDef
.getFields();
282 if (structFields
!= null) {
283 if (structFields
.getFieldNames() != null) {
284 for (String curFieldName
: structFields
.getFieldNames()) {
285 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structFields
.getDefinition(curFieldName
), curFieldName
));
289 /* Add context information as CtfTmfEventField */
290 ICompositeDefinition structContext
= eventDef
.getContext();
291 if (structContext
!= null) {
292 for (String contextName
: structContext
.getFieldNames()) {
293 /* Prefix field name */
294 String curContextName
= CtfConstants
.CONTEXT_FIELD_PREFIX
+ contextName
;
295 fields
.add(CtfTmfEventField
.parseField((IDefinition
) structContext
.getDefinition(contextName
), curContextName
));
299 return fields
.toArray(new CtfTmfEventField
[fields
.size()]);