Commit | Line | Data |
---|---|---|
866e5b51 | 1 | /******************************************************************************* |
ed902a2b | 2 | * Copyright (c) 2011-2014 Ericsson, Ecole Polytechnique de Montreal and others |
866e5b51 FC |
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: Matthew Khouzam - Initial API and implementation | |
10 | * Contributors: Simon Marchi - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.ctf.core.event; |
866e5b51 | 14 | |
a4fa4e36 | 15 | import java.util.ArrayList; |
866e5b51 FC |
16 | import java.util.List; |
17 | ||
a4fa4e36 | 18 | import org.eclipse.jdt.annotation.NonNull; |
f357bcd4 | 19 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; |
fbe6fa6f | 20 | import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope; |
f357bcd4 AM |
21 | import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope; |
22 | import org.eclipse.tracecompass.ctf.core.event.types.Definition; | |
778bce67 | 23 | import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition; |
fa533f33 | 24 | import org.eclipse.tracecompass.ctf.core.event.types.IDefinition; |
f357bcd4 AM |
25 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; |
26 | import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition; | |
f357bcd4 | 27 | import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration; |
a4fa4e36 | 28 | |
866e5b51 | 29 | /** |
be6df2d8 | 30 | * Representation of a particular instance of an event. |
866e5b51 | 31 | */ |
a4fa4e36 | 32 | public final class EventDefinition implements IDefinitionScope { |
866e5b51 FC |
33 | |
34 | // ------------------------------------------------------------------------ | |
35 | // Attributes | |
36 | // ------------------------------------------------------------------------ | |
37 | ||
408f954e MK |
38 | /** |
39 | * Value representing an unknown cpu number. | |
40 | * | |
41 | * @since 2.0 | |
42 | */ | |
43 | public static final int UNKNOWN_CPU = -1; | |
44 | ||
a4fa4e36 MK |
45 | /** |
46 | * A null event, can be used for testing or poison pilling | |
a4fa4e36 | 47 | */ |
408f954e | 48 | public static final @NonNull EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), UNKNOWN_CPU, -1L, null, null, null, null, null); |
a4fa4e36 | 49 | |
866e5b51 FC |
50 | /** |
51 | * The corresponding event declaration. | |
52 | */ | |
8e40ce4c | 53 | private final IEventDeclaration fDeclaration; |
866e5b51 FC |
54 | |
55 | /** | |
56 | * The timestamp of the current event. | |
57 | */ | |
a4fa4e36 | 58 | private final long fTimestamp; |
866e5b51 | 59 | |
94c255ef MK |
60 | private final ICompositeDefinition fEventHeaderDefinition; |
61 | ||
866e5b51 FC |
62 | /** |
63 | * The event context structure definition. | |
64 | */ | |
778bce67 | 65 | private final ICompositeDefinition fEventContext; |
a4fa4e36 | 66 | |
778bce67 | 67 | private final ICompositeDefinition fStreamContext; |
a4fa4e36 | 68 | |
778bce67 | 69 | private final ICompositeDefinition fPacketContext; |
866e5b51 FC |
70 | |
71 | /** | |
72 | * The event fields structure definition. | |
73 | */ | |
778bce67 | 74 | private final ICompositeDefinition fFields; |
866e5b51 FC |
75 | |
76 | /** | |
408f954e | 77 | * The cpu number of the event, can be UNKNOWN |
866e5b51 | 78 | */ |
408f954e | 79 | private final int fCpu; |
866e5b51 FC |
80 | |
81 | // ------------------------------------------------------------------------ | |
82 | // Constructors | |
83 | // ------------------------------------------------------------------------ | |
84 | ||
85 | /** | |
86 | * Constructs an event definition. | |
87 | * | |
88 | * @param declaration | |
be6df2d8 | 89 | * The corresponding event declaration |
408f954e MK |
90 | * @param cpu |
91 | * The cpu source of the event. You can use UNKNOWN_CPU if it is | |
92 | * not known. | |
94c255ef MK |
93 | * @param timestamp |
94 | * event timestamp | |
95 | * @param eventHeaderDefinition | |
408f954e MK |
96 | * The event header definition, can be null if there is no header |
97 | * definition | |
94c255ef MK |
98 | * @param eventContext |
99 | * The event context | |
100 | * @param packetContext | |
101 | * the packet context | |
102 | * @param streamContext | |
103 | * the stream context | |
104 | * @param fields | |
105 | * The event fields | |
6b6f22ef | 106 | * @since 2.0 |
94c255ef MK |
107 | */ |
108 | public EventDefinition(IEventDeclaration declaration, | |
408f954e | 109 | int cpu, |
94c255ef MK |
110 | long timestamp, |
111 | ICompositeDefinition eventHeaderDefinition, | |
112 | ICompositeDefinition streamContext, | |
113 | ICompositeDefinition eventContext, | |
114 | ICompositeDefinition packetContext, | |
115 | ICompositeDefinition fields) { | |
8e40ce4c | 116 | fDeclaration = declaration; |
94c255ef | 117 | fEventHeaderDefinition = eventHeaderDefinition; |
408f954e | 118 | fCpu = cpu; |
a4fa4e36 MK |
119 | fTimestamp = timestamp; |
120 | fFields = fields; | |
121 | fEventContext = eventContext; | |
122 | fPacketContext = packetContext; | |
123 | fStreamContext = streamContext; | |
866e5b51 FC |
124 | } |
125 | ||
126 | // ------------------------------------------------------------------------ | |
127 | // Getters/Setters/Predicates | |
128 | // ------------------------------------------------------------------------ | |
129 | ||
fbe6fa6f MK |
130 | /** |
131 | * @since 1.0 | |
132 | */ | |
866e5b51 | 133 | @Override |
fbe6fa6f | 134 | public ILexicalScope getScopePath() { |
a4fa4e36 MK |
135 | String eventName = fDeclaration.getName(); |
136 | if (eventName == null) { | |
137 | return null; | |
138 | } | |
fbe6fa6f | 139 | ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName); |
a4fa4e36 | 140 | if (myScope == null) { |
fbe6fa6f | 141 | myScope = new LexicalScope(ILexicalScope.EVENT, eventName); |
a4fa4e36 MK |
142 | } |
143 | return myScope; | |
866e5b51 FC |
144 | } |
145 | ||
9ac2eb62 MK |
146 | /** |
147 | * Gets the declaration (the form) of the data | |
148 | * | |
149 | * @return the event declaration | |
150 | */ | |
8e964be1 | 151 | public IEventDeclaration getDeclaration() { |
8e40ce4c | 152 | return fDeclaration; |
866e5b51 FC |
153 | } |
154 | ||
94c255ef MK |
155 | /** |
156 | * Get the event header | |
157 | * | |
158 | * @return the event header | |
6b6f22ef | 159 | * @since 2.0 |
94c255ef MK |
160 | */ |
161 | public ICompositeDefinition getEventHeader() { | |
162 | return fEventHeaderDefinition; | |
163 | } | |
164 | ||
9ac2eb62 MK |
165 | /** |
166 | * Gets the fields of a definition | |
167 | * | |
168 | * @return the fields of a definition in struct form. Can be null. | |
778bce67 | 169 | * @since 1.0 |
9ac2eb62 | 170 | */ |
778bce67 | 171 | public ICompositeDefinition getFields() { |
8e40ce4c | 172 | return fFields; |
866e5b51 FC |
173 | } |
174 | ||
9ac2eb62 | 175 | /** |
824b8985 | 176 | * Gets the context of this event without the context of the stream |
9ac2eb62 MK |
177 | * |
178 | * @return the context in struct form | |
778bce67 | 179 | * @since 1.0 |
9ac2eb62 | 180 | */ |
778bce67 | 181 | public ICompositeDefinition getEventContext() { |
a4fa4e36 | 182 | return fEventContext; |
866e5b51 FC |
183 | } |
184 | ||
824b8985 MK |
185 | /** |
186 | * Gets the context of this event within a stream | |
187 | * | |
188 | * @return the context in struct form | |
778bce67 | 189 | * @since 1.0 |
824b8985 | 190 | */ |
778bce67 | 191 | public ICompositeDefinition getContext() { |
824b8985 MK |
192 | |
193 | /* Most common case so far */ | |
a4fa4e36 MK |
194 | if (fStreamContext == null) { |
195 | return fEventContext; | |
824b8985 MK |
196 | } |
197 | ||
198 | /* streamContext is not null, but the context of the event is null */ | |
a4fa4e36 MK |
199 | if (fEventContext == null) { |
200 | return fStreamContext; | |
824b8985 MK |
201 | } |
202 | ||
a4fa4e36 MK |
203 | // TODO: cache if this is a performance issue |
204 | ||
824b8985 MK |
205 | /* The stream context and event context are assigned. */ |
206 | StructDeclaration mergedDeclaration = new StructDeclaration(1); | |
207 | ||
a4fa4e36 | 208 | List<Definition> fieldValues = new ArrayList<>(); |
824b8985 | 209 | |
a4fa4e36 | 210 | /* Add fields from the stream */ |
e18274f9 MK |
211 | List<String> fieldNames = fStreamContext.getFieldNames(); |
212 | for (String fieldName : fieldNames) { | |
a4fa4e36 MK |
213 | Definition definition = fStreamContext.getDefinition(fieldName); |
214 | mergedDeclaration.addField(fieldName, definition.getDeclaration()); | |
a4fa4e36 | 215 | fieldValues.add(definition); |
824b8985 MK |
216 | } |
217 | ||
a4fa4e36 MK |
218 | /* |
219 | * Add fields from the event context, overwrite the stream ones if | |
220 | * needed. | |
221 | */ | |
222 | for (String fieldName : fEventContext.getFieldNames()) { | |
223 | Definition definition = fEventContext.getDefinition(fieldName); | |
224 | mergedDeclaration.addField(fieldName, definition.getDeclaration()); | |
225 | if (fieldNames.contains(fieldName)) { | |
226 | fieldValues.set((fieldNames.indexOf(fieldName)), definition); | |
824b8985 | 227 | } else { |
a4fa4e36 | 228 | fieldValues.add(definition); |
824b8985 MK |
229 | } |
230 | } | |
e18274f9 | 231 | return new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$ |
a4fa4e36 | 232 | fieldValues.toArray(new Definition[fieldValues.size()])); |
824b8985 MK |
233 | } |
234 | ||
9ac2eb62 MK |
235 | /** |
236 | * Gets the context of packet the event is in. | |
237 | * | |
238 | * @return the packet context | |
778bce67 | 239 | * @since 1.0 |
9ac2eb62 | 240 | */ |
778bce67 | 241 | public ICompositeDefinition getPacketContext() { |
a4fa4e36 | 242 | return fPacketContext; |
866e5b51 FC |
243 | } |
244 | ||
9ac2eb62 MK |
245 | /** |
246 | * gets the CPU the event was generated by. Slightly LTTng specific | |
247 | * | |
248 | * @return The CPU the event was generated by | |
249 | */ | |
866e5b51 | 250 | public int getCPU() { |
408f954e | 251 | return fCpu; |
866e5b51 FC |
252 | } |
253 | ||
aa572e22 MK |
254 | /** |
255 | * @return the timestamp | |
256 | */ | |
257 | public long getTimestamp() { | |
8e40ce4c | 258 | return fTimestamp; |
aa572e22 MK |
259 | } |
260 | ||
866e5b51 FC |
261 | // ------------------------------------------------------------------------ |
262 | // Operations | |
263 | // ------------------------------------------------------------------------ | |
264 | ||
fa533f33 MK |
265 | /** |
266 | * @since 1.0 | |
267 | */ | |
866e5b51 | 268 | @Override |
fa533f33 | 269 | public IDefinition lookupDefinition(String lookupPath) { |
866e5b51 | 270 | if (lookupPath.equals("context")) { //$NON-NLS-1$ |
a4fa4e36 | 271 | return fEventContext; |
866e5b51 | 272 | } else if (lookupPath.equals("fields")) { //$NON-NLS-1$ |
8e40ce4c | 273 | return fFields; |
866e5b51 FC |
274 | } else { |
275 | return null; | |
276 | } | |
277 | } | |
278 | ||
279 | @Override | |
280 | public String toString() { | |
a4fa4e36 | 281 | Iterable<String> list; |
07002e0a MK |
282 | StringBuilder retString = new StringBuilder(); |
283 | final String cr = System.getProperty("line.separator");//$NON-NLS-1$ | |
866e5b51 | 284 | |
e18274f9 MK |
285 | retString.append("Event type: ").append(fDeclaration.getName()).append(cr); //$NON-NLS-1$ |
286 | retString.append("Timestamp: ").append(Long.toString(fTimestamp)).append(cr); //$NON-NLS-1$ | |
866e5b51 | 287 | |
a4fa4e36 | 288 | if (fEventContext != null) { |
8e0c9d81 | 289 | list = fEventContext.getFieldNames(); |
866e5b51 FC |
290 | |
291 | for (String field : list) { | |
e18274f9 | 292 | retString.append(field).append(" : ").append(fEventContext.getDefinition(field).toString()).append(cr); //$NON-NLS-1$ |
866e5b51 FC |
293 | } |
294 | } | |
295 | ||
8e40ce4c | 296 | if (fFields != null) { |
8e0c9d81 | 297 | list = fFields.getFieldNames(); |
866e5b51 FC |
298 | |
299 | for (String field : list) { | |
e18274f9 | 300 | retString.append(field).append(" : ").append(fFields.getDefinition(field).toString()).append(cr); //$NON-NLS-1$ |
866e5b51 FC |
301 | } |
302 | } | |
303 | ||
07002e0a | 304 | return retString.toString(); |
866e5b51 FC |
305 | } |
306 | ||
307 | } |