Commit | Line | Data |
---|---|---|
6c7592e1 MK |
1 | /******************************************************************************* |
2 | * Copyright (c) 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 | * Matthew Khouzam - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
f357bcd4 | 13 | package org.eclipse.tracecompass.internal.ctf.core.event.types.composite; |
6c7592e1 MK |
14 | |
15 | import java.nio.ByteOrder; | |
66aa25f0 MK |
16 | import java.util.ArrayList; |
17 | import java.util.List; | |
6c7592e1 | 18 | |
f78eb6a7 MK |
19 | import org.eclipse.jdt.annotation.NonNullByDefault; |
20 | import org.eclipse.jdt.annotation.Nullable; | |
680f9173 | 21 | import org.eclipse.tracecompass.ctf.core.CTFException; |
f357bcd4 AM |
22 | import org.eclipse.tracecompass.ctf.core.event.io.BitBuffer; |
23 | import org.eclipse.tracecompass.ctf.core.event.scope.IDefinitionScope; | |
24 | import org.eclipse.tracecompass.ctf.core.event.types.Declaration; | |
66aa25f0 | 25 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
f357bcd4 AM |
26 | import org.eclipse.tracecompass.ctf.core.event.types.EnumDeclaration; |
27 | import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration; | |
28 | import org.eclipse.tracecompass.ctf.core.event.types.IEventHeaderDeclaration; | |
29 | import org.eclipse.tracecompass.ctf.core.event.types.IntegerDeclaration; | |
30 | import org.eclipse.tracecompass.ctf.core.event.types.StructDeclaration; | |
31 | import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration; | |
6c7592e1 MK |
32 | |
33 | /** | |
34 | * An event header declaration is a declaration of a structure defined in the | |
35 | * CTF spec examples section 6.1.1 . It is used in LTTng traces. This will | |
36 | * accelerate reading of the trace. | |
37 | * | |
38 | * Reminder | |
39 | * | |
40 | * <pre> | |
41 | * struct event_header_compact { | |
42 | * enum : uint5_t { compact = 0 ... 30, extended = 31 } id; | |
43 | * variant <id> { | |
44 | * struct { | |
45 | * uint27_clock_monotonic_t timestamp; | |
46 | * } compact; | |
47 | * struct { | |
48 | * uint32_t id; | |
49 | * uint64_clock_monotonic_t timestamp; | |
50 | * } extended; | |
51 | * } v; | |
52 | * } align(8); | |
53 | * </pre> | |
54 | * | |
55 | * @author Matthew Khouzam | |
56 | */ | |
f78eb6a7 | 57 | @NonNullByDefault |
f068c622 | 58 | public final class EventHeaderCompactDeclaration extends Declaration implements IEventHeaderDeclaration { |
6c7592e1 | 59 | |
66aa25f0 | 60 | private static final int BASE_10 = 10; |
6c7592e1 MK |
61 | /** |
62 | * The id is 5 bits | |
63 | */ | |
64 | private static final int COMPACT_ID = 5; | |
66aa25f0 | 65 | private static final int EXTENDED_VALUE = (1 << COMPACT_ID) - 1; |
6c7592e1 MK |
66 | /** |
67 | * Full sized id is 32 bits | |
68 | */ | |
69 | private static final int ID_SIZE = 32; | |
70 | /** | |
71 | * Full sized timestamp is 64 bits | |
72 | */ | |
73 | private static final int FULL_TS = 64; | |
74 | /** | |
75 | * Compact timestamp is 27 bits, | |
76 | */ | |
77 | private static final int COMPACT_TS = 27; | |
66aa25f0 MK |
78 | /** |
79 | * Clock identifier | |
80 | */ | |
81 | private static final String CLOCK = ""; //$NON-NLS-1$ | |
6c7592e1 MK |
82 | /** |
83 | * Maximum size = largest this header can be | |
84 | */ | |
85 | private static final int MAX_SIZE = 104; | |
86 | /** | |
87 | * Byte aligned | |
88 | */ | |
66aa25f0 MK |
89 | private static final int ALIGN_ON_1 = 1; |
90 | private static final int ALIGN_ON_8 = 8; | |
6c7592e1 MK |
91 | |
92 | private final ByteOrder fByteOrder; | |
66aa25f0 | 93 | private final List<StructDeclaration> fReferenceStructs = new ArrayList<>(); |
6c7592e1 | 94 | |
f78eb6a7 MK |
95 | /** |
96 | * Big-Endian Large Event Header | |
97 | */ | |
98 | private static final EventHeaderCompactDeclaration EVENT_HEADER_BIG_ENDIAN = new EventHeaderCompactDeclaration(nullCheck(ByteOrder.BIG_ENDIAN)); | |
99 | ||
100 | /** | |
101 | * Little-Endian Large Event Header | |
102 | */ | |
103 | private static final EventHeaderCompactDeclaration EVENT_HEADER_LITTLE_ENDIAN = new EventHeaderCompactDeclaration(nullCheck(ByteOrder.LITTLE_ENDIAN)); | |
104 | ||
6c7592e1 MK |
105 | /** |
106 | * Event Header Declaration | |
107 | * | |
108 | * @param byteOrder | |
109 | * the byteorder | |
110 | */ | |
f78eb6a7 | 111 | private EventHeaderCompactDeclaration(ByteOrder byteOrder) { |
6c7592e1 | 112 | fByteOrder = byteOrder; |
66aa25f0 MK |
113 | populateReferences(); |
114 | } | |
115 | ||
116 | private void populateReferences() { | |
117 | if (!fReferenceStructs.isEmpty()) { | |
118 | return; | |
119 | } | |
120 | StructDeclaration ref = new StructDeclaration(ALIGN_ON_8); | |
121 | EnumDeclaration id = new EnumDeclaration(IntegerDeclaration.createDeclaration(COMPACT_ID, false, BASE_10, fByteOrder, Encoding.NONE, CLOCK, ALIGN_ON_1)); | |
122 | id.add(0, EXTENDED_VALUE - 1, COMPACT); | |
123 | id.add(EXTENDED_VALUE, EXTENDED_VALUE, EXTENDED); | |
124 | ref.addField(ID, id); | |
125 | VariantDeclaration v = new VariantDeclaration(); | |
126 | StructDeclaration compact = new StructDeclaration(ALIGN_ON_1); | |
f068c622 | 127 | compact.addField(TIMESTAMP, IntegerDeclaration.createDeclaration(COMPACT_TS, false, BASE_10, fByteOrder, Encoding.NONE, CLOCK, ALIGN_ON_1)); |
66aa25f0 | 128 | StructDeclaration extended = new StructDeclaration(ALIGN_ON_8); |
f068c622 MK |
129 | extended.addField(ID, IntegerDeclaration.createDeclaration(ID_SIZE, false, BASE_10, fByteOrder, Encoding.NONE, CLOCK, ALIGN_ON_8)); |
130 | extended.addField(TIMESTAMP, IntegerDeclaration.createDeclaration(FULL_TS, false, BASE_10, fByteOrder, Encoding.NONE, CLOCK, ALIGN_ON_8)); | |
66aa25f0 MK |
131 | v.addField(COMPACT, compact); |
132 | v.addField(EXTENDED, extended); | |
133 | ref.addField(VARIANT_NAME, v); | |
134 | fReferenceStructs.add(ref); | |
6c7592e1 MK |
135 | } |
136 | ||
f78eb6a7 MK |
137 | /** |
138 | * Gets an {@link EventHeaderCompactDeclaration} of a given ByteOrder | |
139 | * | |
140 | * @param byteOrder | |
141 | * the byte order | |
142 | * @return the header declaration | |
143 | */ | |
144 | public static EventHeaderCompactDeclaration getEventHeader(@Nullable ByteOrder byteOrder) { | |
145 | if (byteOrder == ByteOrder.BIG_ENDIAN) { | |
146 | return EVENT_HEADER_BIG_ENDIAN; | |
147 | } | |
148 | return EVENT_HEADER_LITTLE_ENDIAN; | |
149 | } | |
150 | ||
6c7592e1 | 151 | @Override |
680f9173 | 152 | public EventHeaderDefinition createDefinition(@Nullable IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFException { |
6c7592e1 MK |
153 | alignRead(input); |
154 | ByteOrder bo = input.getByteOrder(); | |
155 | input.setByteOrder(fByteOrder); | |
156 | int enumId = (int) input.get(COMPACT_ID, false); | |
157 | if (enumId != EXTENDED_VALUE) { | |
158 | long timestamp2 = input.get(COMPACT_TS, false); | |
159 | input.setByteOrder(bo); | |
160 | return new EventHeaderDefinition(this, enumId, timestamp2, COMPACT_TS); | |
161 | } | |
162 | // needed since we read 5 bits | |
163 | input.position(input.position() + 3); | |
733e6767 MK |
164 | long id = input.get(ID_SIZE, false); |
165 | if (id > Integer.MAX_VALUE) { | |
680f9173 | 166 | throw new CTFException("ID " + id + " larger than " + Integer.MAX_VALUE + " is currently unsupported by the parser"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ |
733e6767 | 167 | } |
6c7592e1 MK |
168 | long timestampLong = input.get(FULL_TS, false); |
169 | input.setByteOrder(bo); | |
733e6767 | 170 | return new EventHeaderDefinition(this, (int) id, timestampLong, FULL_TS); |
6c7592e1 MK |
171 | |
172 | } | |
173 | ||
174 | @Override | |
175 | public long getAlignment() { | |
66aa25f0 | 176 | return ALIGN_ON_8; |
6c7592e1 MK |
177 | } |
178 | ||
179 | @Override | |
180 | public int getMaximumSize() { | |
181 | return MAX_SIZE; | |
182 | } | |
183 | ||
184 | /** | |
185 | * Check if a given struct declaration is an event header | |
186 | * | |
187 | * @param declaration | |
188 | * the declaration | |
189 | * @return true if the struct is a compact event header | |
190 | */ | |
66aa25f0 | 191 | public boolean isCompactEventHeader(@Nullable StructDeclaration declaration) { |
f78eb6a7 MK |
192 | if (declaration == null) { |
193 | return false; | |
194 | } | |
66aa25f0 MK |
195 | for (IDeclaration ref : fReferenceStructs) { |
196 | if (ref.isBinaryEquivalent(declaration)) { | |
197 | return true; | |
198 | } | |
6c7592e1 | 199 | } |
66aa25f0 | 200 | return false; |
6c7592e1 | 201 | } |
f78eb6a7 MK |
202 | |
203 | private static ByteOrder nullCheck(@Nullable ByteOrder bo) { | |
204 | if (bo == null) { | |
205 | throw new IllegalStateException("Could not create byteorder"); //$NON-NLS-1$ | |
206 | } | |
207 | return bo; | |
208 | } | |
e00e6663 | 209 | |
66aa25f0 | 210 | @Override |
e00e6663 MK |
211 | public int hashCode() { |
212 | final int prime = 31; | |
213 | int result = 1; | |
214 | result = prime * result + (fByteOrder.equals(ByteOrder.BIG_ENDIAN) ? 4321 : 1234); | |
215 | return result; | |
216 | } | |
217 | ||
218 | @Override | |
219 | public boolean equals(@Nullable Object obj) { | |
220 | if (this == obj) { | |
221 | return true; | |
222 | } | |
223 | if (obj == null) { | |
224 | return false; | |
225 | } | |
226 | if (getClass() != obj.getClass()) { | |
227 | return false; | |
228 | } | |
229 | EventHeaderCompactDeclaration other = (EventHeaderCompactDeclaration) obj; | |
230 | if (!fByteOrder.equals(other.fByteOrder)) { | |
231 | return false; | |
232 | } | |
233 | return true; | |
234 | } | |
235 | ||
66aa25f0 MK |
236 | @Override |
237 | public boolean isBinaryEquivalent(@Nullable IDeclaration other) { | |
238 | for (StructDeclaration referenceStruct : fReferenceStructs) { | |
239 | if (referenceStruct.isBinaryEquivalent(other)) { | |
240 | return true; | |
241 | } | |
242 | } | |
243 | return false; | |
244 | } | |
245 | ||
6c7592e1 | 246 | } |