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