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