ctf: Introduce IEventDefinition
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.tmf.ctf.core / src / org / eclipse / tracecompass / tmf / ctf / core / event / CtfTmfEventFactory.java
CommitLineData
6cfa0200 1/*******************************************************************************
da707390 2 * Copyright (c) 2013, 2015 Ericsson
6cfa0200
AM
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
9722e5d7 13package org.eclipse.tracecompass.tmf.ctf.core.event;
6cfa0200 14
df2597e0
AM
15import org.eclipse.jdt.annotation.NonNullByDefault;
16import org.eclipse.jdt.annotation.Nullable;
f357bcd4 17import org.eclipse.tracecompass.ctf.core.CTFStrings;
f357bcd4 18import org.eclipse.tracecompass.ctf.core.event.IEventDeclaration;
e8ece272 19import org.eclipse.tracecompass.ctf.core.event.IEventDefinition;
f357bcd4
AM
20import org.eclipse.tracecompass.ctf.core.event.types.IDefinition;
21import org.eclipse.tracecompass.ctf.core.event.types.IntegerDefinition;
da707390 22import org.eclipse.tracecompass.tmf.core.timestamp.TmfNanoTimestamp;
2bdf0193
AM
23import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
24import org.eclipse.tracecompass.tmf.core.trace.ITmfContext;
9722e5d7 25import org.eclipse.tracecompass.tmf.ctf.core.trace.CtfTmfTrace;
6cfa0200
AM
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
6cfa0200 34 */
df2597e0 35@NonNullByDefault
8e376474 36public class CtfTmfEventFactory {
6cfa0200 37
df2597e0 38 private static final CtfTmfEventFactory INSTANCE = new CtfTmfEventFactory();
a4fa4e36 39
60fb38b8 40 /**
8e376474
AM
41 * The file name to use when none is specified.
42 *
43 * FIXME Externalize?
44 *
45 * @since 2.0
60fb38b8 46 */
df2597e0 47 protected static final String NO_STREAM = "No stream"; //$NON-NLS-1$
6cfa0200
AM
48
49 /**
8e376474
AM
50 * Protected constructor, only for use by sub-classes. Users should call
51 * the {@link #instance()} method instead.
6cfa0200 52 *
8e376474
AM
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 */
df2597e0 63 public static CtfTmfEventFactory instance() {
8e376474
AM
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
6cfa0200
AM
72 * @param eventDef
73 * CTF EventDefinition object corresponding to this trace event
74 * @param fileName
75 * The path to the trace file
6cfa0200 76 * @return The newly-built CtfTmfEvent
8e376474 77 * @since 2.0
6cfa0200 78 */
e8ece272 79 public CtfTmfEvent createEvent(CtfTmfTrace trace, IEventDefinition eventDef, @Nullable String fileName) {
6cfa0200
AM
80
81 /* Prepare what to pass to CtfTmfEvent's constructor */
c26d0fe0
AM
82 final IEventDeclaration eventDecl = eventDef.getDeclaration();
83 final long ts = eventDef.getTimestamp();
8e376474 84 final TmfNanoTimestamp timestamp = trace.createTimestamp(trace.timestampCyclesToNanos(ts));
6cfa0200
AM
85
86 int sourceCPU = eventDef.getCPU();
87
df2597e0 88 String reference = (fileName == null ? NO_STREAM : fileName);
6cfa0200 89
c26d0fe0
AM
90 /* Handle the special case of lost events */
91 if (eventDecl.getName().equals(CTFStrings.LOST_EVENT_NAME)) {
8e376474 92 return createLostEvent(trace, eventDef, eventDecl, ts, timestamp, sourceCPU, reference);
c26d0fe0
AM
93 }
94
95 /* Handle standard event types */
8e376474 96 return new CtfTmfEvent(trace,
6cfa0200
AM
97 ITmfContext.UNKNOWN_RANK,
98 timestamp,
c26d0fe0 99 reference, // filename
6cfa0200 100 sourceCPU,
a4fa4e36
MK
101 eventDecl,
102 eventDef);
6cfa0200
AM
103 }
104
8e376474
AM
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,
e8ece272 126 IEventDefinition eventDef,
8e376474
AM
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();
8253063c 145 TmfNanoTimestamp timestampEnd = trace.createTimestamp(
8e376474
AM
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 }
6cfa0200
AM
158
159 /**
160 * Get an instance of a null event.
161 *
ca5b04ad 162 * @param trace
8e376474 163 * The trace to which the new null event will belong
6cfa0200 164 * @return An empty event
8e376474 165 * @since 2.0
6cfa0200 166 */
8e376474
AM
167 public static CtfTmfEvent getNullEvent(CtfTmfTrace trace) {
168 return new CtfTmfEvent(trace);
6cfa0200
AM
169 }
170
437bb7c0 171
6cfa0200 172}
This page took 0.149584 seconds and 5 git commands to generate.