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