tmf: extract UpdateJob class and introduce TmfPieChartViewer
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / ctf / core / event / EventDefinition.java
CommitLineData
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 13package org.eclipse.tracecompass.ctf.core.event;
866e5b51 14
a4fa4e36 15import java.util.ArrayList;
866e5b51
FC
16import java.util.List;
17
a4fa4e36 18import org.eclipse.jdt.annotation.NonNull;
f357bcd4 19import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope;
fbe6fa6f 20import org.eclipse.tracecompass.ctf.core.event.scope.ILexicalScope;
f357bcd4
AM
21import org.eclipse.tracecompass.ctf.core.event.scope.LexicalScope;
22import org.eclipse.tracecompass.ctf.core.event.types.Definition;
778bce67 23import org.eclipse.tracecompass.ctf.core.event.types.ICompositeDefinition;
fa533f33 24import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
f357bcd4
AM
25import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration;
26import org.eclipse.tracecompass.ctf.core.event.types.StructDefinition;
27import org.eclipse.tracecompass.ctf.core.trace.CTFStreamInputReader;
28import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
a4fa4e36 29
866e5b51 30/**
be6df2d8 31 * Representation of a particular instance of an event.
866e5b51 32 */
a4fa4e36 33public 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
a4fa4e36
MK
41 */
42 @NonNull
43 public static final EventDefinition NULL_EVENT = new EventDefinition(new EventDeclaration(), null, -1L, null, null, null, null);
44
866e5b51
FC
45 /**
46 * The corresponding event declaration.
47 */
8e40ce4c 48 private final IEventDeclaration fDeclaration;
866e5b51
FC
49
50 /**
51 * The timestamp of the current event.
52 */
a4fa4e36 53 private final long fTimestamp;
866e5b51 54
94c255ef
MK
55 private final ICompositeDefinition fEventHeaderDefinition;
56
866e5b51
FC
57 /**
58 * The event context structure definition.
59 */
778bce67 60 private final ICompositeDefinition fEventContext;
a4fa4e36 61
778bce67 62 private final ICompositeDefinition fStreamContext;
a4fa4e36 63
778bce67 64 private final ICompositeDefinition fPacketContext;
866e5b51
FC
65
66 /**
67 * The event fields structure definition.
68 */
778bce67 69 private final ICompositeDefinition 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
778bce67 97 * @since 1.0
866e5b51 98 */
8e964be1 99 public EventDefinition(IEventDeclaration declaration,
d84419e1 100 CTFStreamInputReader streamInputReader,
a4fa4e36 101 long timestamp,
778bce67
MK
102 ICompositeDefinition streamContext,
103 ICompositeDefinition eventContext,
104 ICompositeDefinition packetContext,
105 ICompositeDefinition fields) {
94c255ef
MK
106 this(declaration, streamInputReader, timestamp, null, streamContext,
107 eventContext, packetContext, fields);
108 }
109
110 /**
111 * Constructs an event definition.
112 *
113 * @param declaration
114 * The corresponding event declaration
115 * @param streamInputReader
116 * The SIR from where this EventDef was read
117 * @param timestamp
118 * event timestamp
119 * @param eventHeaderDefinition
120 * the event header definition, can be null
121 * @param eventContext
122 * The event context
123 * @param packetContext
124 * the packet context
125 * @param streamContext
126 * the stream context
127 * @param fields
128 * The event fields
129 * @since 1.1
130 */
131 public EventDefinition(IEventDeclaration declaration,
132 CTFStreamInputReader streamInputReader,
133 long timestamp,
134 ICompositeDefinition eventHeaderDefinition,
135 ICompositeDefinition streamContext,
136 ICompositeDefinition eventContext,
137 ICompositeDefinition packetContext,
138 ICompositeDefinition fields) {
8e40ce4c 139 fDeclaration = declaration;
94c255ef 140 fEventHeaderDefinition = eventHeaderDefinition;
8e40ce4c 141 fStreamInputReader = streamInputReader;
a4fa4e36
MK
142 fTimestamp = timestamp;
143 fFields = fields;
144 fEventContext = eventContext;
145 fPacketContext = packetContext;
146 fStreamContext = streamContext;
866e5b51
FC
147 }
148
149 // ------------------------------------------------------------------------
150 // Getters/Setters/Predicates
151 // ------------------------------------------------------------------------
152
fbe6fa6f
MK
153 /**
154 * @since 1.0
155 */
866e5b51 156 @Override
fbe6fa6f 157 public ILexicalScope getScopePath() {
a4fa4e36
MK
158 String eventName = fDeclaration.getName();
159 if (eventName == null) {
160 return null;
161 }
fbe6fa6f 162 ILexicalScope myScope = ILexicalScope.EVENT.getChild(eventName);
a4fa4e36 163 if (myScope == null) {
fbe6fa6f 164 myScope = new LexicalScope(ILexicalScope.EVENT, eventName);
a4fa4e36
MK
165 }
166 return myScope;
866e5b51
FC
167 }
168
9ac2eb62
MK
169 /**
170 * Gets the declaration (the form) of the data
171 *
172 * @return the event declaration
173 */
8e964be1 174 public IEventDeclaration getDeclaration() {
8e40ce4c 175 return fDeclaration;
866e5b51
FC
176 }
177
94c255ef
MK
178 /**
179 * Get the event header
180 *
181 * @return the event header
182 * @since 1.1
183 */
184 public ICompositeDefinition getEventHeader() {
185 return fEventHeaderDefinition;
186 }
187
9ac2eb62
MK
188 /**
189 * Gets the fields of a definition
190 *
191 * @return the fields of a definition in struct form. Can be null.
778bce67 192 * @since 1.0
9ac2eb62 193 */
778bce67 194 public ICompositeDefinition getFields() {
8e40ce4c 195 return fFields;
866e5b51
FC
196 }
197
9ac2eb62 198 /**
824b8985 199 * Gets the context of this event without the context of the stream
9ac2eb62
MK
200 *
201 * @return the context in struct form
778bce67 202 * @since 1.0
9ac2eb62 203 */
778bce67 204 public ICompositeDefinition getEventContext() {
a4fa4e36 205 return fEventContext;
866e5b51
FC
206 }
207
824b8985
MK
208 /**
209 * Gets the context of this event within a stream
210 *
211 * @return the context in struct form
778bce67 212 * @since 1.0
824b8985 213 */
778bce67 214 public ICompositeDefinition getContext() {
824b8985
MK
215
216 /* Most common case so far */
a4fa4e36
MK
217 if (fStreamContext == null) {
218 return fEventContext;
824b8985
MK
219 }
220
221 /* streamContext is not null, but the context of the event is null */
a4fa4e36
MK
222 if (fEventContext == null) {
223 return fStreamContext;
824b8985
MK
224 }
225
a4fa4e36
MK
226 // TODO: cache if this is a performance issue
227
824b8985
MK
228 /* The stream context and event context are assigned. */
229 StructDeclaration mergedDeclaration = new StructDeclaration(1);
230
a4fa4e36 231 List<Definition> fieldValues = new ArrayList<>();
824b8985 232
a4fa4e36 233 /* Add fields from the stream */
e18274f9
MK
234 List<String> fieldNames = fStreamContext.getFieldNames();
235 for (String fieldName : fieldNames) {
a4fa4e36
MK
236 Definition definition = fStreamContext.getDefinition(fieldName);
237 mergedDeclaration.addField(fieldName, definition.getDeclaration());
a4fa4e36 238 fieldValues.add(definition);
824b8985
MK
239 }
240
a4fa4e36
MK
241 /*
242 * Add fields from the event context, overwrite the stream ones if
243 * needed.
244 */
245 for (String fieldName : fEventContext.getFieldNames()) {
246 Definition definition = fEventContext.getDefinition(fieldName);
247 mergedDeclaration.addField(fieldName, definition.getDeclaration());
248 if (fieldNames.contains(fieldName)) {
249 fieldValues.set((fieldNames.indexOf(fieldName)), definition);
824b8985 250 } else {
a4fa4e36 251 fieldValues.add(definition);
824b8985
MK
252 }
253 }
e18274f9 254 return new StructDefinition(mergedDeclaration, this, "context", //$NON-NLS-1$
a4fa4e36 255 fieldValues.toArray(new Definition[fieldValues.size()]));
824b8985
MK
256 }
257
9ac2eb62
MK
258 /**
259 * Gets the stream input reader that this event was made by
260 *
261 * @return the parent
262 */
d84419e1 263 public CTFStreamInputReader getStreamInputReader() {
8e40ce4c 264 return fStreamInputReader;
866e5b51
FC
265 }
266
9ac2eb62
MK
267 /**
268 * Gets the context of packet the event is in.
269 *
270 * @return the packet context
778bce67 271 * @since 1.0
9ac2eb62 272 */
778bce67 273 public ICompositeDefinition getPacketContext() {
a4fa4e36 274 return fPacketContext;
866e5b51
FC
275 }
276
9ac2eb62
MK
277 /**
278 * gets the CPU the event was generated by. Slightly LTTng specific
279 *
280 * @return The CPU the event was generated by
281 */
866e5b51 282 public int getCPU() {
8e40ce4c 283 return fStreamInputReader.getCPU();
866e5b51
FC
284 }
285
aa572e22
MK
286 /**
287 * @return the timestamp
288 */
289 public long getTimestamp() {
8e40ce4c 290 return fTimestamp;
aa572e22
MK
291 }
292
866e5b51
FC
293 // ------------------------------------------------------------------------
294 // Operations
295 // ------------------------------------------------------------------------
296
fa533f33
MK
297 /**
298 * @since 1.0
299 */
866e5b51 300 @Override
fa533f33 301 public IDefinition lookupDefinition(String lookupPath) {
866e5b51 302 if (lookupPath.equals("context")) { //$NON-NLS-1$
a4fa4e36 303 return fEventContext;
866e5b51 304 } else if (lookupPath.equals("fields")) { //$NON-NLS-1$
8e40ce4c 305 return fFields;
866e5b51
FC
306 } else {
307 return null;
308 }
309 }
310
311 @Override
312 public String toString() {
a4fa4e36 313 Iterable<String> list;
07002e0a
MK
314 StringBuilder retString = new StringBuilder();
315 final String cr = System.getProperty("line.separator");//$NON-NLS-1$
866e5b51 316
e18274f9
MK
317 retString.append("Event type: ").append(fDeclaration.getName()).append(cr); //$NON-NLS-1$
318 retString.append("Timestamp: ").append(Long.toString(fTimestamp)).append(cr); //$NON-NLS-1$
866e5b51 319
a4fa4e36 320 if (fEventContext != null) {
8e0c9d81 321 list = fEventContext.getFieldNames();
866e5b51
FC
322
323 for (String field : list) {
e18274f9 324 retString.append(field).append(" : ").append(fEventContext.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
866e5b51
FC
325 }
326 }
327
8e40ce4c 328 if (fFields != null) {
8e0c9d81 329 list = fFields.getFieldNames();
866e5b51
FC
330
331 for (String field : list) {
e18274f9 332 retString.append(field).append(" : ").append(fFields.getDefinition(field).toString()).append(cr); //$NON-NLS-1$
866e5b51
FC
333 }
334 }
335
07002e0a 336 return retString.toString();
866e5b51
FC
337 }
338
339}
This page took 0.087809 seconds and 5 git commands to generate.