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