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