d0632bb49ca172074dad5e234188e68b381e1ed4
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / event / CtfTmfEventFactory.java
1 /*******************************************************************************
2 * Copyright (c) 2013, 2015 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 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ctf.core.event;
14
15 import org.eclipse.jdt.annotation.NonNull;
16 import org.eclipse.tracecompass.ctf.core.CTFStrings;
17 import org.eclipse.tracecompass.ctf.core.event.EventDefinition;
18 import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
19 import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
20 import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
21 import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
22 import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
23 import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
24 import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
25
26 /**
27 * Factory for CtfTmfEvent's.
28 *
29 * This code was moved out of CtfTmfEvent to provide better separation between
30 * the parsing/instantiation of events, and the usual TMF API implementations.
31 *
32 * @author Alexandre Montplaisir
33 */
34 public class CtfTmfEventFactory {
35
36 private static final @NonNull CtfTmfEventFactory INSTANCE = new CtfTmfEventFactory();
37
38 /**
39 * The file name to use when none is specified.
40 *
41 * FIXME Externalize?
42 *
43 * @since 2.0
44 */
45 protected static final @NonNull String NO_STREAM = "No stream"; //$NON-NLS-1$
46
47 /**
48 * Protected constructor, only for use by sub-classes. Users should call
49 * the {@link #instance()} method instead.
50 *
51 * @since 2.0
52 */
53 protected CtfTmfEventFactory() {}
54
55 /**
56 * Get the singleton factory instance
57 *
58 * @return The instance
59 * @since 2.0
60 */
61 public static @NonNull CtfTmfEventFactory instance() {
62 return INSTANCE;
63 }
64
65 /**
66 * Factory method to instantiate new CTF events.
67 *
68 * @param trace
69 * The trace to which the new event will belong
70 * @param eventDef
71 * CTF EventDefinition object corresponding to this trace event
72 * @param fileName
73 * The path to the trace file
74 * @return The newly-built CtfTmfEvent
75 * @since 2.0
76 */
77 public CtfTmfEvent createEvent(CtfTmfTrace trace, EventDefinition eventDef, String fileName) {
78
79 /* Prepare what to pass to CtfTmfEvent's constructor */
80 final IEventDeclaration eventDecl = eventDef.getDeclaration();
81 final long ts = eventDef.getTimestamp();
82 final TmfNanoTimestamp timestamp = trace.createTimestamp(trace.timestampCyclesToNanos(ts));
83
84 int sourceCPU = eventDef.getCPU();
85
86 String reference = fileName == null ? NO_STREAM : fileName;
87
88 /* Handle the special case of lost events */
89 if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
90 return createLostEvent(trace, eventDef, eventDecl, ts, timestamp, sourceCPU, reference);
91 }
92
93 /* Handle standard event types */
94 return new CtfTmfEvent(trace,
95 ITmfContext.UNKNOWN_RANK,
96 timestamp,
97 reference, // filename
98 sourceCPU,
99 eventDecl,
100 eventDef);
101 }
102
103 /**
104 * Create a new CTF lost event.
105 *
106 * @param trace
107 * The trace to which the new event will belong
108 * @param eventDef
109 * The CTF event definition
110 * @param eventDecl
111 * The CTF event declaration
112 * @param ts
113 * The event's timestamp
114 * @param timestamp
115 * The event's timestamp (FIXME again??)
116 * @param sourceCPU
117 * The source CPU
118 * @param fileName
119 * The file name
120 * @return The new lost event
121 * @since 2.0
122 */
123 protected static CtfTmfEvent createLostEvent(CtfTmfTrace trace,
124 EventDefinition eventDef,
125 final IEventDeclaration eventDecl,
126 final long ts,
127 final TmfNanoTimestamp timestamp,
128 int sourceCPU,
129 String fileName) {
130
131 IDefinition nbLostEventsDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_FIELD);
132 IDefinition durationDef = eventDef.getFields().getDefinition(CTFStrings.LOST_EVENTS_DURATION);
133 if (!(nbLostEventsDef instanceof IntegerDefinition) || !(durationDef instanceof IntegerDefinition)) {
134 /*
135 * One or both of these fields doesn't exist, or is not of the right
136 * type. The event claims to be a "lost event", but is malformed.
137 * Log it and return a null event instead.
138 */
139 return getNullEvent(trace);
140 }
141 long nbLostEvents = ((IntegerDefinition) nbLostEventsDef).getValue();
142 long duration = ((IntegerDefinition) durationDef).getValue();
143 TmfNanoTimestamp timestampEnd = new TmfNanoTimestamp(
144 trace.timestampCyclesToNanos(ts) + duration);
145
146 CtfTmfLostEvent lostEvent = new CtfTmfLostEvent(trace,
147 ITmfContext.UNKNOWN_RANK,
148 fileName,
149 sourceCPU,
150 eventDecl,
151 new TmfTimeRange(timestamp, timestampEnd),
152 nbLostEvents,
153 eventDef);
154 return lostEvent;
155 }
156
157 /**
158 * Get an instance of a null event.
159 *
160 * @param trace
161 * The trace to which the new null event will belong
162 * @return An empty event
163 * @since 2.0
164 */
165 public static CtfTmfEvent getNullEvent(CtfTmfTrace trace) {
166 return new CtfTmfEvent(trace);
167 }
168
169
170 }
This page took 0.038104 seconds and 4 git commands to generate.