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