1 /*******************************************************************************
2 * Copyright (c) 2011-2013 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
.core
.ctfadaptor
;
16 import java
.util
.HashSet
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.CTFCallsite
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.IEventDeclaration
;
21 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventField
;
22 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEventType
;
23 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
24 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventField
;
25 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEventPropertySource
;
26 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfModelLookup
;
27 import org
.eclipse
.linuxtools
.tmf
.core
.event
.lookup
.ITmfSourceLookup
;
28 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfContext
;
29 import org
.eclipse
.ui
.views
.properties
.IPropertySource
;
32 * A wrapper class around CTF's Event Definition/Declaration that maps all
33 * types of Declaration to native Java types.
36 * @author Alexandre Montplaisir
39 public final class CtfTmfEvent
extends TmfEvent
implements ITmfSourceLookup
, ITmfModelLookup
{
41 // ------------------------------------------------------------------------
43 // ------------------------------------------------------------------------
45 static final String NO_STREAM
= "No stream"; //$NON-NLS-1$
46 private static final String EMPTY_CTF_EVENT_NAME
= "Empty CTF event"; //$NON-NLS-1$
48 // ------------------------------------------------------------------------
50 // ------------------------------------------------------------------------
52 private final int sourceCPU
;
53 private final long typeId
;
54 private final String eventName
;
55 private final IEventDeclaration fDeclaration
;
57 // ------------------------------------------------------------------------
59 // ------------------------------------------------------------------------
62 * Constructor used by {@link CtfTmfEventFactory#createEvent}
64 CtfTmfEvent(CtfTmfTrace trace
, long rank
, CtfTmfTimestamp timestamp
,
65 ITmfEventField content
, String fileName
, int cpu
,
66 IEventDeclaration declaration
) {
70 String
.valueOf(cpu
), // Source
71 null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
76 fDeclaration
= declaration
;
78 typeId
= declaration
.getId();
79 eventName
= declaration
.getName();
83 * Inner constructor to create "null" events. Don't use this directly in
84 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
85 * instance of an empty event.
87 * This needs to be public however because it's used in extension points,
88 * and the framework will use this constructor to get the class type.
90 public CtfTmfEvent() {
92 ITmfContext
.UNKNOWN_RANK
,
93 new CtfTmfTimestamp(-1),
96 new TmfEventField("", new CtfTmfEventField
[0]), //$NON-NLS-1$
100 this.eventName
= EMPTY_CTF_EVENT_NAME
;
101 this.fDeclaration
= null;
104 // ------------------------------------------------------------------------
105 // Getters/Setters/Predicates
106 // ------------------------------------------------------------------------
109 * Gets the cpu core the event was recorded on.
111 * @return The cpu id for a given source. In lttng it's from CPUINFO
113 public int getCPU() {
114 return this.sourceCPU
;
118 * Return this event's ID, according to the trace's metadata.
120 * Watch out, this ID is not constant from one trace to another for the same
121 * event types! Use "getEventName()" for a constant reference.
123 * @return The event ID
125 public long getID() {
130 * Gets the name of a current event.
132 * @return The event name
134 public String
getEventName() {
139 public CtfTmfTrace
getTrace() {
140 /* Should be of the right type, since we take a CtfTmfTrace at the constructor */
141 return (CtfTmfTrace
) super.getTrace();
145 public ITmfEventType
getType() {
146 CtfTmfEventType ctfTmfEventType
= CtfTmfEventType
.get(eventName
);
147 if (ctfTmfEventType
== null) {
148 /* Should only return null the first time */
149 ctfTmfEventType
= new CtfTmfEventType(this.getEventName(), this.getContent());
151 return ctfTmfEventType
;
155 * List the custom CTF attributes for events of this type.
157 * @return The list of custom attribute names. Should not be null, but could
161 public Set
<String
> listCustomAttributes() {
162 if (fDeclaration
== null) {
163 return new HashSet
<String
>();
165 return fDeclaration
.getCustomAttributes();
169 * Get the value of a custom CTF attributes for this event's type.
172 * Name of the the custom attribute
173 * @return Value of this attribute, or null if there is no attribute with
177 public String
getCustomAttribute(String name
) {
178 if (fDeclaration
== null) {
181 return fDeclaration
.getCustomAttribute(name
);
185 * Get the call site for this event.
187 * @return the call site information, or null if there is none
191 public CtfTmfCallsite
getCallsite() {
192 CTFCallsite callsite
= null;
193 if (getTrace() == null) {
196 if (getContent() != null) {
197 ITmfEventField ipField
= getContent().getField(CtfConstants
.CONTEXT_FIELD_PREFIX
+ CtfConstants
.IP_KEY
);
198 if (ipField
!= null && ipField
.getValue() instanceof Long
) {
199 long ip
= (Long
) ipField
.getValue();
200 callsite
= getTrace().getCTFTrace().getCallsite(eventName
, ip
);
203 if (callsite
== null) {
204 callsite
= getTrace().getCTFTrace().getCallsite(eventName
);
206 if (callsite
!= null) {
207 return new CtfTmfCallsite(callsite
);
216 public String
getModelUri() {
217 return getCustomAttribute(CtfConstants
.MODEL_URI_KEY
);
224 public Object
getAdapter(Class adapter
) {
225 if (adapter
== IPropertySource
.class) {
226 return new TmfEventPropertySource(this);