Remove UI dependencies in tmf.core.
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.core / src / org / eclipse / linuxtools / tmf / core / ctfadaptor / CtfTmfEvent.java
1 /*******************************************************************************
2 * Copyright (c) 2011, 2013 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:
10 * Alexandre Montplaisir - Initial API and implementation
11 * Bernd Hufmann - Updated for source and model lookup interfaces
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.tmf.core.ctfadaptor;
15
16 import java.util.HashSet;
17 import java.util.Set;
18
19 import org.eclipse.linuxtools.ctf.core.event.CTFCallsite;
20 import org.eclipse.linuxtools.ctf.core.event.IEventDeclaration;
21 import org.eclipse.linuxtools.tmf.core.event.ITmfCustomAttributes;
22 import org.eclipse.linuxtools.tmf.core.event.ITmfEventField;
23 import org.eclipse.linuxtools.tmf.core.event.ITmfEventType;
24 import org.eclipse.linuxtools.tmf.core.event.TmfEvent;
25 import org.eclipse.linuxtools.tmf.core.event.TmfEventField;
26 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfModelLookup;
27 import org.eclipse.linuxtools.tmf.core.event.lookup.ITmfSourceLookup;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfContext;
29
30 /**
31 * A wrapper class around CTF's Event Definition/Declaration that maps all
32 * types of Declaration to native Java types.
33 *
34 * @version 1.0
35 * @author Alexandre Montplaisir
36 * @since 2.0
37 */
38 public final class CtfTmfEvent extends TmfEvent
39 implements ITmfSourceLookup, ITmfModelLookup, ITmfCustomAttributes {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
44
45 static final String NO_STREAM = "No stream"; //$NON-NLS-1$
46 private static final String EMPTY_CTF_EVENT_NAME = "Empty CTF event"; //$NON-NLS-1$
47
48 // ------------------------------------------------------------------------
49 // Attributes
50 // ------------------------------------------------------------------------
51
52 private final int sourceCPU;
53 private final long typeId;
54 private final String eventName;
55 private final IEventDeclaration fDeclaration;
56
57 // ------------------------------------------------------------------------
58 // Constructors
59 // ------------------------------------------------------------------------
60
61 /**
62 * Constructor used by {@link CtfTmfEventFactory#createEvent}
63 */
64 CtfTmfEvent(CtfTmfTrace trace, long rank, CtfTmfTimestamp timestamp,
65 ITmfEventField content, String fileName, int cpu,
66 IEventDeclaration declaration) {
67 super(trace,
68 rank,
69 timestamp,
70 String.valueOf(cpu), // Source
71 null, // Event type. We don't use TmfEvent's field here, we re-implement getType()
72 content,
73 fileName // Reference
74 );
75
76 fDeclaration = declaration;
77 sourceCPU = cpu;
78 typeId = declaration.getId();
79 eventName = declaration.getName();
80 }
81
82 /**
83 * Inner constructor to create "null" events. Don't use this directly in
84 * normal usage, use {@link CtfTmfEventFactory#getNullEvent()} to get an
85 * instance of an empty event.
86 *
87 * This needs to be public however because it's used in extension points,
88 * and the framework will use this constructor to get the class type.
89 */
90 public CtfTmfEvent() {
91 super(null,
92 ITmfContext.UNKNOWN_RANK,
93 new CtfTmfTimestamp(-1),
94 null,
95 null,
96 new TmfEventField("", null, new CtfTmfEventField[0]), //$NON-NLS-1$
97 NO_STREAM);
98 this.sourceCPU = -1;
99 this.typeId = -1;
100 this.eventName = EMPTY_CTF_EVENT_NAME;
101 this.fDeclaration = null;
102 }
103
104 // ------------------------------------------------------------------------
105 // Getters/Setters/Predicates
106 // ------------------------------------------------------------------------
107
108 /**
109 * Gets the cpu core the event was recorded on.
110 *
111 * @return The cpu id for a given source. In lttng it's from CPUINFO
112 */
113 public int getCPU() {
114 return this.sourceCPU;
115 }
116
117 /**
118 * Return this event's ID, according to the trace's metadata.
119 *
120 * Watch out, this ID is not constant from one trace to another for the same
121 * event types! Use "getEventName()" for a constant reference.
122 *
123 * @return The event ID
124 */
125 public long getID() {
126 return this.typeId;
127 }
128
129 /**
130 * Gets the name of a current event.
131 *
132 * @return The event name
133 */
134 public String getEventName() {
135 return eventName;
136 }
137
138 @Override
139 public CtfTmfTrace getTrace() {
140 /* Should be of the right type, since we take a CtfTmfTrace at the constructor */
141 return (CtfTmfTrace) super.getTrace();
142 }
143
144 @Override
145 public ITmfEventType getType() {
146 CtfTmfEventType ctfTmfEventType = CtfTmfEventType.get(eventName);
147 if (ctfTmfEventType == null) {
148 /* Should only return null the first time */
149 ctfTmfEventType = new CtfTmfEventType(this.getEventName(), this.getContent());
150 }
151 return ctfTmfEventType;
152 }
153
154 /**
155 * @since 2.0
156 */
157 @Override
158 public Set<String> listCustomAttributes() {
159 if (fDeclaration == null) {
160 return new HashSet<String>();
161 }
162 return fDeclaration.getCustomAttributes();
163 }
164
165 /**
166 * @since 2.0
167 */
168 @Override
169 public String getCustomAttribute(String name) {
170 if (fDeclaration == null) {
171 return null;
172 }
173 return fDeclaration.getCustomAttribute(name);
174 }
175
176 /**
177 * Get the call site for this event.
178 *
179 * @return the call site information, or null if there is none
180 * @since 2.0
181 */
182 @Override
183 public CtfTmfCallsite getCallsite() {
184 CTFCallsite callsite = null;
185 if (getTrace() == null) {
186 return null;
187 }
188 if (getContent() != null) {
189 ITmfEventField ipField = getContent().getField(CtfConstants.CONTEXT_FIELD_PREFIX + CtfConstants.IP_KEY);
190 if (ipField != null && ipField.getValue() instanceof Long) {
191 long ip = (Long) ipField.getValue();
192 callsite = getTrace().getCTFTrace().getCallsite(eventName, ip);
193 }
194 }
195 if (callsite == null) {
196 callsite = getTrace().getCTFTrace().getCallsite(eventName);
197 }
198 if (callsite != null) {
199 return new CtfTmfCallsite(callsite);
200 }
201 return null;
202 }
203
204 /**
205 * @since 2.0
206 */
207 @Override
208 public String getModelUri() {
209 return getCustomAttribute(CtfConstants.MODEL_URI_KEY);
210 }
211
212 }
This page took 0.036202 seconds and 6 git commands to generate.