ctf: Read and display custom event attributes
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011-2013 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Bernd Hufmann - Updated for source and model lookup interfaces
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
15
16 import java.util.HashSet;
17 import java.util.Set;
18
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.ITmfCustomAttributes;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
26 import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource;
27 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup;
28 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup;
29 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
30 import org.eclipse.ui.views.properties.IPropertySource;
31
32 /**
33 * A wrapper class around CTF's Event Definition/Declaration that maps all
34 * types of Declaration to native Java types.
35 *
36 * @version 1.0
37 * @author Alexandre Montplaisir
38 * @since 2.0
39 */
40 public final class CtfTmfEvent extends TmfEvent
41 implements ITmfSourceLookup, ITmfModelLookup, ITmfCustomAttributes {
42
43 // ------------------------------------------------------------------------
44 // Constants
45 // ------------------------------------------------------------------------
46
47 static final String NO_STREAM = "No stream"; //$NON-NLS-1$
48 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
49
50 // ------------------------------------------------------------------------
51 // Attributes
52 // ------------------------------------------------------------------------
53
54 private final int sourceCPU;
55 private final long typeId;
56 private final String eventName;
57 private final IEventDeclaration fDeclaration;
58
59 // ------------------------------------------------------------------------
60 // Constructors
61 // ------------------------------------------------------------------------
62
63 /**
64 * Constructor used by {@link CtfTmfEventFactory#createEvent}
65 */
66 CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp,
67 ITmfEventField content, String fileName, int cpu,
68 IEventDeclaration declaration) {
69 super(trace,
70 rank,
71 timestamp,
72 String.valueOf(cpu), // Source
73 null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
74 content,
75 fileName // Reference
76 );
77
78 fDeclaration = declaration;
79 sourceCPU = cpu;
80 typeId = declaration.getId();
81 eventName = declaration.getName();
82 }
83
84 /**
85 * Inner constructor to create "null" events. Don't use this directly in
86 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
87 * instance of an empty event.
88 *
89 * This needs to be public however because it's used in extension points,
90 * and the framework will use this constructor to get the class type.
91 */
92 public CtfTmfEvent() {
93 super(null,
94 ITmfContext.UNKNOWN_RANK,
95 new CtfTmfTimestamp(-1),
96 null,
97 null,
98 new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$
99 NO_STREAM);
100 this.sourceCPU = -1;
101 this.typeId = -1;
102 this.eventName = EMPTY_CTF_EVENT_NAME;
103 this.fDeclaration = null;
104 }
105
106 // ------------------------------------------------------------------------
107 // Getters/Setters/Predicates
108 // ------------------------------------------------------------------------
109
110 /**
111 * Gets the cpu core the event was recorded on.
112 *
113 * @return The cpu id for a given source. In lttng it's from CPUINFO
114 */
115 public int getCPU() {
116 return this.sourceCPU;
117 }
118
119 /**
120 * Return this event's ID, according to the trace's metadata.
121 *
122 * Watch out, this ID is not constant from one trace to another for the same
123 * event types! Use "getEventName()" for a constant reference.
124 *
125 * @return The event ID
126 */
127 public long getID() {
128 return this.typeId;
129 }
130
131 /**
132 * Gets the name of a current event.
133 *
134 * @return The event name
135 */
136 public String getEventName() {
137 return eventName;
138 }
139
140 @Override
141 public CtfTmfTrace getTrace() {
142 /* Should be of the right type, since we take a CtfTmfTrace at the constructor */
143 return (CtfTmfTrace) super.getTrace();
144 }
145
146 @Override
147 public ITmfEventType getType() {
148 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
149 if (ctfTmfEventType == null) {
150 /* Should only return null the first time */
151 ctfTmfEventType = new CtfTmfEventType(this.getEventName(), this.getContent());
152 }
153 return ctfTmfEventType;
154 }
155
156 /**
157 * @since 2.0
158 */
159 @Override
160 public Set<String> listCustomAttributes() {
161 if (fDeclaration == null) {
162 return new HashSet<String>();
163 }
164 return fDeclaration.getCustomAttributes();
165 }
166
167 /**
168 * @since 2.0
169 */
170 @Override
171 public String getCustomAttribute(String name) {
172 if (fDeclaration == null) {
173 return null;
174 }
175 return fDeclaration.getCustomAttribute(name);
176 }
177
178 /**
179 * Get the call site for this event.
180 *
181 * @return the call site information, or null if there is none
182 * @since 2.0
183 */
184 @Override
185 public CtfTmfCallsite getCallsite() {
186 CTFCallsite callsite = null;
187 if (getTrace() == null) {
188 return null;
189 }
190 if (getContent() != null) {
191 ITmfEventField ipField = getContent().getField(CtfConstants.CONTEXT_FIELD_PREFIX + CtfConstants.IP_KEY);
192 if (ipField != null && ipField.getValue() instanceof Long) {
193 long ip = (Long) ipField.getValue();
194 callsite = getTrace().getCTFTrace().getCallsite(eventName, ip);
195 }
196 }
197 if (callsite == null) {
198 callsite = getTrace().getCTFTrace().getCallsite(eventName);
199 }
200 if (callsite != null) {
201 return new CtfTmfCallsite(callsite);
202 }
203 return null;
204 }
205
206 /**
207 * @since 2.0
208 */
209 @Override
210 public String getModelUri() {
211 return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
212 }
213
214 /**
215 * @since 2.0
216 */
217 @Override
218 public Object getAdapter(Class adapter) {
219 if (adapter == IPropertySource.class) {
220 return new TmfEventPropertySource(this);
221 }
222 return null;
223 }
224 }
This page took 0.036048 seconds and 5 git commands to generate.