Commit | Line | Data |
---|---|---|
a3fc8213 | 1 | /******************************************************************************* |
8e964be1 | 2 | * Copyright (c) 2011-2013 Ericsson |
a3fc8213 AM |
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 | * | |
58f3bc52 AM |
9 | * Contributors: |
10 | * Alexandre Montplaisir - Initial API and implementation | |
a3fc8213 AM |
11 | *******************************************************************************/ |
12 | ||
13 | package org.eclipse.linuxtools.tmf.core.ctfadaptor; | |
14 | ||
15 | import java.util.ArrayList; | |
16 | import java.util.HashMap; | |
8e964be1 | 17 | import java.util.HashSet; |
aa572e22 | 18 | import java.util.Iterator; |
a3fc8213 AM |
19 | import java.util.List; |
20 | import java.util.Map.Entry; | |
8e964be1 | 21 | import java.util.Set; |
a3fc8213 | 22 | |
4c9d2941 | 23 | import org.eclipse.linuxtools.ctf.core.event.CTFCallsite; |
a3fc8213 | 24 | import org.eclipse.linuxtools.ctf.core.event.EventDefinition; |
8e964be1 | 25 | import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration; |
a3fc8213 | 26 | import org.eclipse.linuxtools.ctf.core.event.types.Definition; |
4c9d2941 | 27 | import org.eclipse.linuxtools.ctf.core.event.types.IntegerDefinition; |
a3fc8213 | 28 | import org.eclipse.linuxtools.ctf.core.event.types.StructDefinition; |
a3fc8213 AM |
29 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
30 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventField; | |
31 | import org.eclipse.linuxtools.tmf.core.event.ITmfEventType; | |
32 | import org.eclipse.linuxtools.tmf.core.event.ITmfTimestamp; | |
306dc902 | 33 | import org.eclipse.linuxtools.tmf.core.event.TmfEventField; |
93bfd50a PT |
34 | import org.eclipse.linuxtools.tmf.core.event.TmfEventPropertySource; |
35 | import org.eclipse.ui.views.properties.IPropertySource; | |
a3fc8213 AM |
36 | |
37 | /** | |
d09f973b FC |
38 | * A wrapper class around CTF's Event Definition/Declaration that maps all |
39 | * types of Declaration to native Java types. | |
6256d8ad | 40 | * |
d09f973b FC |
41 | * @version 1.0 |
42 | * @author Alexandre Montplaisir | |
93bfd50a | 43 | * @since 2.0 |
a3fc8213 | 44 | */ |
bd54d363 | 45 | public final class CtfTmfEvent implements ITmfEvent, Cloneable { |
a3fc8213 AM |
46 | |
47 | // ------------------------------------------------------------------------ | |
48 | // Constants | |
49 | // ------------------------------------------------------------------------ | |
50 | ||
51 | private static final String NO_STREAM = "No stream"; //$NON-NLS-1$ | |
52 | private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$ | |
aa572e22 | 53 | |
26859ddb BH |
54 | /** Prefix for Context information stored as CtfTmfEventfield */ |
55 | private static final String CONTEXT_FIELD_PREFIX = "context."; //$NON-NLS-1$ | |
a3fc8213 AM |
56 | |
57 | // ------------------------------------------------------------------------ | |
58 | // Attributes | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | private final CtfTmfTrace fTrace; | |
58f3bc52 | 62 | private final ITmfTimestamp fTimestamp; |
a3fc8213 AM |
63 | private final int sourceCPU; |
64 | private final long typeId; | |
65 | private final String eventName; | |
66 | private final String fileName; | |
67 | ||
306dc902 | 68 | private final TmfEventField fContent; |
8e964be1 | 69 | private final IEventDeclaration fDeclaration; |
a3fc8213 AM |
70 | |
71 | // ------------------------------------------------------------------------ | |
72 | // Constructors | |
73 | // ------------------------------------------------------------------------ | |
74 | ||
75 | /** | |
76 | * Usual CTFEvent constructor, where we read an event from the trace (via | |
77 | * the StreamInputReader). | |
78 | * | |
79 | * @param eventDef | |
063f0d27 AM |
80 | * CTF EventDefinition object corresponding to this trace event |
81 | * @param fileName | |
82 | * The path to the trace file | |
83 | * @param originTrace | |
84 | * The trace from which this event originates | |
a3fc8213 | 85 | */ |
ce2388e0 | 86 | public CtfTmfEvent(EventDefinition eventDef, String fileName, |
a3fc8213 AM |
87 | CtfTmfTrace originTrace) { |
88 | this.fTrace = originTrace; | |
89 | ||
90 | if (eventDef == null) { | |
58f3bc52 | 91 | this.fTimestamp = new CtfTmfTimestamp(-1); |
a3fc8213 AM |
92 | this.sourceCPU = -1; |
93 | this.typeId = -1; | |
94 | this.fileName = NO_STREAM; | |
95 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
96 | this.fContent = null; | |
8e964be1 | 97 | this.fDeclaration = null; |
a3fc8213 AM |
98 | return; |
99 | } | |
100 | ||
101 | /* Read the base event info */ | |
58f3bc52 AM |
102 | long ts = this.getTrace().getCTFTrace().timestampCyclesToNanos(eventDef.getTimestamp()); |
103 | this.fTimestamp = new CtfTmfTimestamp(ts); | |
a3fc8213 AM |
104 | this.sourceCPU = eventDef.getCPU(); |
105 | this.typeId = eventDef.getDeclaration().getId(); | |
106 | this.eventName = eventDef.getDeclaration().getName(); | |
ce2388e0 | 107 | this.fileName = fileName; |
a3fc8213 AM |
108 | |
109 | /* Read the fields */ | |
306dc902 | 110 | this.fContent = new TmfEventField(ITmfEventField.ROOT_FIELD_ID, parseFields(eventDef)); |
8e964be1 MK |
111 | |
112 | /* Keep a reference to this event's CTF declaration */ | |
113 | this.fDeclaration = eventDef.getDeclaration(); | |
a3fc8213 AM |
114 | } |
115 | ||
116 | /** | |
117 | * Extract the field information from the structDefinition haze-inducing | |
118 | * mess, and put them into something ITmfEventField can cope with. | |
a3fc8213 | 119 | */ |
4c9d2941 | 120 | private CtfTmfEventField[] parseFields(EventDefinition eventDef) { |
a3fc8213 AM |
121 | List<CtfTmfEventField> fields = new ArrayList<CtfTmfEventField>(); |
122 | ||
123 | StructDefinition structFields = eventDef.getFields(); | |
124 | HashMap<String, Definition> definitions = structFields.getDefinitions(); | |
4c9d2941 | 125 | String curFieldName = null; |
a3fc8213 AM |
126 | Definition curFieldDef; |
127 | CtfTmfEventField curField; | |
aa572e22 MK |
128 | Iterator<Entry<String, Definition>> it = definitions.entrySet().iterator(); |
129 | while(it.hasNext()) { | |
130 | Entry<String, Definition> entry = it.next(); | |
a3fc8213 AM |
131 | curFieldName = entry.getKey(); |
132 | curFieldDef = entry.getValue(); | |
133 | curField = CtfTmfEventField.parseField(curFieldDef, curFieldName); | |
a3fc8213 AM |
134 | fields.add(curField); |
135 | } | |
136 | ||
26859ddb | 137 | /* Add context information as CtfTmfEventField */ |
4c9d2941 | 138 | long ip = -1; |
26859ddb BH |
139 | StructDefinition structContext = eventDef.getContext(); |
140 | if (structContext != null) { | |
141 | definitions = structContext.getDefinitions(); | |
142 | String curContextName; | |
143 | Definition curContextDef; | |
144 | CtfTmfEventField curContext; | |
145 | it = definitions.entrySet().iterator(); | |
146 | while(it.hasNext()) { | |
147 | Entry<String, Definition> entry = it.next(); | |
4c9d2941 MK |
148 | /* This is to get the instruction pointer if available */ |
149 | if (entry.getKey().equals("_ip") && //$NON-NLS-1$ | |
150 | (entry.getValue() instanceof IntegerDefinition)) { | |
151 | ip = ((IntegerDefinition) entry.getValue()).getValue(); | |
152 | } | |
26859ddb BH |
153 | /* Prefix field name to */ |
154 | curContextName = CONTEXT_FIELD_PREFIX + entry.getKey(); | |
155 | curContextDef = entry.getValue(); | |
156 | curContext = CtfTmfEventField.parseField(curContextDef, curContextName); | |
157 | fields.add(curContext); | |
158 | } | |
159 | } | |
4c9d2941 MK |
160 | /* Add callsite */ |
161 | final String name = eventDef.getDeclaration().getName(); | |
162 | List<CTFCallsite> eventList = fTrace.getCTFTrace().getCallsiteCandidates(name); | |
84115199 | 163 | if (!eventList.isEmpty()) { |
4c9d2941 MK |
164 | final String callsite = "callsite"; //$NON-NLS-1$ |
165 | if (eventList.size() == 1 || ip == -1) { | |
166 | CTFCallsite cs = eventList.get(0); | |
167 | fields.add(new CTFStringField(cs.toString(), callsite)); | |
168 | } else { | |
169 | fields.add(new CTFStringField( | |
170 | fTrace.getCTFTrace().getCallsite(name, ip).toString(), | |
171 | callsite)); | |
172 | } | |
173 | } | |
26859ddb | 174 | |
a3fc8213 AM |
175 | return fields.toArray(new CtfTmfEventField[fields.size()]); |
176 | } | |
177 | ||
178 | /** | |
179 | * Copy constructor | |
180 | * | |
181 | * @param other | |
063f0d27 | 182 | * CtfTmfEvent to copy |
a3fc8213 AM |
183 | */ |
184 | public CtfTmfEvent(CtfTmfEvent other) { | |
58f3bc52 | 185 | /* There is only one reference to the trace, so we can shallow-copy it */ |
a3fc8213 | 186 | this.fTrace = other.getTrace(); |
58f3bc52 | 187 | |
80349bf7 AM |
188 | /* Copy the timestamp (immutable) */ |
189 | this.fTimestamp = other.fTimestamp; | |
58f3bc52 | 190 | |
a3fc8213 | 191 | /* Primitives, those will be copied by value */ |
a3fc8213 AM |
192 | this.sourceCPU = other.sourceCPU; |
193 | this.typeId = other.typeId; | |
194 | ||
195 | /* Strings are immutable, it's safe to shallow-copy them */ | |
196 | this.eventName = other.eventName; | |
197 | this.fileName = other.fileName; | |
198 | ||
80349bf7 AM |
199 | /* Copy the fields over (immutable) */ |
200 | this.fContent = other.fContent; | |
8e964be1 MK |
201 | |
202 | /* | |
203 | * Copy the reference to the custom attributes (should be the same | |
204 | * object for all events of this type) | |
205 | */ | |
206 | this.fDeclaration = other.fDeclaration; | |
a3fc8213 AM |
207 | } |
208 | ||
209 | /** | |
b8a6e46d AM |
210 | * Inner constructor to create "null" events. Don't use this directly in |
211 | * normal usage, use CtfTmfEvent.getNullEvent() to get an instance of an | |
212 | * empty event. | |
c26afeaf | 213 | * |
b8a6e46d AM |
214 | * This needs to be public however because it's used in extension points, |
215 | * and the framework will use this constructor to get the class type. | |
a3fc8213 | 216 | */ |
ce2388e0 | 217 | public CtfTmfEvent() { |
a3fc8213 | 218 | this.fTrace = null; |
58f3bc52 | 219 | this.fTimestamp = new CtfTmfTimestamp(-1); |
a3fc8213 AM |
220 | this.sourceCPU = -1; |
221 | this.typeId = -1; | |
222 | this.fileName = NO_STREAM; | |
223 | this.eventName = EMPTY_CTF_EVENT_NAME; | |
306dc902 | 224 | this.fContent = new TmfEventField("", new CtfTmfEventField[0]); //$NON-NLS-1$ |
8e964be1 | 225 | this.fDeclaration = null; |
a3fc8213 AM |
226 | } |
227 | ||
228 | // ------------------------------------------------------------------------ | |
229 | // Getters/Setters/Predicates | |
230 | // ------------------------------------------------------------------------ | |
231 | ||
58f3bc52 | 232 | private static CtfTmfEvent nullEvent = new CtfTmfEvent(); |
a3fc8213 AM |
233 | |
234 | /** | |
235 | * Get a null event | |
236 | * | |
58f3bc52 AM |
237 | * @return An empty event. |
238 | */ | |
a3fc8213 | 239 | public static CtfTmfEvent getNullEvent() { |
a3fc8213 AM |
240 | return nullEvent; |
241 | } | |
242 | ||
a3fc8213 AM |
243 | /** |
244 | * Gets the cpu core the event was recorded on. | |
245 | * | |
58f3bc52 AM |
246 | * @return The cpu id for a given source. In lttng it's from CPUINFO |
247 | */ | |
a3fc8213 AM |
248 | public int getCPU() { |
249 | return this.sourceCPU; | |
250 | } | |
251 | ||
252 | /** | |
58f3bc52 | 253 | * Return this event's ID, according to the trace's metadata. |
a3fc8213 | 254 | * |
58f3bc52 AM |
255 | * Watch out, this ID is not constant from one trace to another for the same |
256 | * event types! Use "getEventName()" for a constant reference. | |
257 | * | |
258 | * @return The event ID | |
259 | */ | |
a3fc8213 AM |
260 | public long getID() { |
261 | return this.typeId; | |
262 | } | |
263 | ||
264 | /** | |
265 | * Gets the name of a current event. | |
266 | * | |
58f3bc52 AM |
267 | * @return The event name |
268 | */ | |
a3fc8213 AM |
269 | public String getEventName() { |
270 | return eventName; | |
271 | } | |
272 | ||
273 | /** | |
274 | * Gets the channel name of a field. | |
275 | * | |
58f3bc52 AM |
276 | * @return The channel name. |
277 | */ | |
a3fc8213 AM |
278 | public String getChannelName() { |
279 | return this.fileName; | |
280 | } | |
281 | ||
282 | @Override | |
283 | public CtfTmfTrace getTrace() { | |
284 | return fTrace; | |
285 | } | |
286 | ||
287 | @Override | |
288 | public long getRank() { | |
289 | // TODO Auto-generated method stub | |
290 | return 0; | |
291 | } | |
292 | ||
a3fc8213 AM |
293 | @Override |
294 | public ITmfTimestamp getTimestamp() { | |
a3fc8213 AM |
295 | return fTimestamp; |
296 | } | |
297 | ||
298 | @Override | |
299 | public String getSource() { | |
ce2388e0 | 300 | // TODO Returns CPU for now |
58f3bc52 | 301 | return Integer.toString(getCPU()); |
a3fc8213 AM |
302 | } |
303 | ||
304 | @Override | |
305 | public ITmfEventType getType() { | |
c26afeaf MD |
306 | CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName); |
307 | if( ctfTmfEventType == null ){ | |
308 | ctfTmfEventType = new CtfTmfEventType( this.getEventName(), this.getContent()); | |
309 | } | |
310 | return ctfTmfEventType; | |
a3fc8213 AM |
311 | } |
312 | ||
313 | @Override | |
314 | public ITmfEventField getContent() { | |
315 | return fContent; | |
316 | } | |
317 | ||
318 | @Override | |
319 | public String getReference() { | |
58f3bc52 | 320 | return getChannelName(); |
a3fc8213 AM |
321 | } |
322 | ||
8e964be1 MK |
323 | /** |
324 | * List the custom CTF attributes for events of this type. | |
325 | * | |
326 | * @return The list of custom attribute names. Should not be null, but could | |
327 | * be empty. | |
328 | * @since 2.0 | |
329 | */ | |
330 | public Set<String> listCustomAttributes() { | |
331 | if (fDeclaration == null) { | |
332 | return new HashSet<String>(); | |
333 | } | |
334 | return fDeclaration.getCustomAttributes(); | |
335 | } | |
336 | ||
337 | /** | |
338 | * Get the value of a custom CTF attributes for this event's type. | |
339 | * | |
340 | * @param name | |
341 | * Name of the the custom attribute | |
342 | * @return Value of this attribute, or null if there is no attribute with | |
343 | * that name | |
344 | * @since 2.0 | |
345 | */ | |
346 | public String getCustomAttribute(String name) { | |
347 | if (fDeclaration == null) { | |
348 | return null; | |
349 | } | |
350 | return fDeclaration.getCustomAttribute(name); | |
351 | } | |
352 | ||
a3fc8213 AM |
353 | @Override |
354 | public CtfTmfEvent clone() { | |
355 | return new CtfTmfEvent(this); | |
356 | } | |
93bfd50a | 357 | |
531987c8 PT |
358 | /** |
359 | * @since 2.0 | |
360 | */ | |
93bfd50a PT |
361 | @Override |
362 | public Object getAdapter(Class adapter) { | |
363 | if (adapter == IPropertySource.class) { | |
364 | return new TmfEventPropertySource(this); | |
365 | } | |
366 | return null; | |
367 | } | |
a3fc8213 | 368 | } |