57e26caeb236b16d2b12e750f57914f5a08ab185
[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.ctf.core.trace.CTFTrace;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
26 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
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
31 /**
32 * A wrapper class around CTF's Event Definition/Declaration that maps all types
33 * of Declaration to native Java types.
34 *
35 * @version 1.0
36 * @author Alexandre Montplaisir
37 * @since 2.0
38 */
39 public class CtfTmfEvent extends TmfEvent
40 implements ITmfSourceLookup, ITmfModelLookup, ITmfCustomAttributes {
41
42 // ------------------------------------------------------------------------
43 // Constants
44 // ------------------------------------------------------------------------
45
46 static final String NO_STREAM = "No stream"; //$NON-NLS-1$
47 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52
53 private final int sourceCPU;
54 private final long typeId;
55 private final String eventName;
56 private final IEventDeclaration fDeclaration;
57
58 // ------------------------------------------------------------------------
59 // Constructors
60 // ------------------------------------------------------------------------
61
62 /**
63 * Constructor used by {@link CtfTmfEventFactory#createEvent}
64 */
65 CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp,
66 ITmfEventField content, String fileName, int cpu,
67 IEventDeclaration declaration) {
68 super(trace,
69 rank,
70 timestamp,
71 String.valueOf(cpu), // Source
72 null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
73 content,
74 fileName // Reference
75 );
76
77 fDeclaration = declaration;
78 sourceCPU = cpu;
79 typeId = declaration.getId();
80 eventName = declaration.getName();
81
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 /*
143 * Should be of the right type, since we take a CtfTmfTrace at the
144 * constructor
145 */
146 return (CtfTmfTrace) super.getTrace();
147 }
148
149 @Override
150 public ITmfEventType getType() {
151 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
152 if (ctfTmfEventType == null) {
153 /* Should only return null the first time */
154 ctfTmfEventType = new CtfTmfEventType(this.getEventName(), this.getContent());
155 }
156 return ctfTmfEventType;
157 }
158
159 /**
160 * @since 2.0
161 */
162 @Override
163 public Set<String> listCustomAttributes() {
164 if (fDeclaration == null) {
165 return new HashSet<>();
166 }
167 return fDeclaration.getCustomAttributes();
168 }
169
170 /**
171 * @since 2.0
172 */
173 @Override
174 public String getCustomAttribute(String name) {
175 if (fDeclaration == null) {
176 return null;
177 }
178 return fDeclaration.getCustomAttribute(name);
179 }
180
181 /**
182 * Get the call site for this event.
183 *
184 * @return the call site information, or null if there is none
185 * @since 2.0
186 */
187 @Override
188 public CtfTmfCallsite getCallsite() {
189 CTFCallsite callsite = null;
190 CtfTmfTrace trace = getTrace();
191 if (trace == null) {
192 return null;
193 }
194 CTFTrace ctfTrace = trace.getCTFTrace();
195 /* Should not happen, but it is a good check */
196 if (ctfTrace == null) {
197 return null;
198 }
199 if (getContent() != null) {
200 ITmfEventField ipField = getContent().getField(CtfConstants.CONTEXT_FIELD_PREFIX + CtfConstants.IP_KEY);
201 if (ipField != null && ipField.getValue() instanceof Long) {
202 long ip = (Long) ipField.getValue();
203 callsite = ctfTrace.getCallsite(eventName, ip);
204 }
205 }
206 if (callsite == null) {
207 callsite = ctfTrace.getCallsite(eventName);
208 }
209 if (callsite != null) {
210 return new CtfTmfCallsite(callsite);
211 }
212 return null;
213 }
214
215 /**
216 * @since 2.0
217 */
218 @Override
219 public String getModelUri() {
220 return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
221 }
222
223 }
This page took 0.036323 seconds and 5 git commands to generate.