| 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 | |
| 13 | package org.eclipse.tracecompass.internal.ctf.core.event.types.composite; |
| 14 | |
| 15 | import java.nio.ByteOrder; |
| 16 | import java.util.ArrayList; |
| 17 | import java.util.List; |
| 18 | |
| 19 | import org.eclipse.jdt.annotation.NonNullByDefault; |
| 20 | import org.eclipse.jdt.annotation.Nullable; |
| 21 | import org.eclipse.tracecompass.ctf.core.CTFException; |
| 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; |
| 25 | import org.eclipse.tracecompass.ctf.core.event.types.Encoding; |
| 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; |
| 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 | */ |
| 57 | @NonNullByDefault |
| 58 | public final class EventHeaderCompactDeclaration extends Declaration implements IEventHeaderDeclaration { |
| 59 | |
| 60 | private static final int BASE_10 = 10; |
| 61 | /** |
| 62 | * The id is 5 bits |
| 63 | */ |
| 64 | private static final int COMPACT_ID = 5; |
| 65 | private static final int EXTENDED_VALUE = (1 << COMPACT_ID) - 1; |
| 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; |
| 78 | /** |
| 79 | * Clock identifier |
| 80 | */ |
| 81 | private static final String CLOCK = ""; //$NON-NLS-1$ |
| 82 | /** |
| 83 | * Maximum size = largest this header can be |
| 84 | */ |
| 85 | private static final int MAX_SIZE = 104; |
| 86 | /** |
| 87 | * Byte aligned |
| 88 | */ |
| 89 | private static final int ALIGN_ON_1 = 1; |
| 90 | private static final int ALIGN_ON_8 = 8; |
| 91 | |
| 92 | private final ByteOrder fByteOrder; |
| 93 | private final List<StructDeclaration> fReferenceStructs = new ArrayList<>(); |
| 94 | |
| 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 | |
| 105 | /** |
| 106 | * Event Header Declaration |
| 107 | * |
| 108 | * @param byteOrder |
| 109 | * the byteorder |
| 110 | */ |
| 111 | private EventHeaderCompactDeclaration(ByteOrder byteOrder) { |
| 112 | fByteOrder = byteOrder; |
| 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); |
| 127 | compact.addField(TIMESTAMP, IntegerDeclaration.createDeclaration(COMPACT_TS, false, BASE_10, fByteOrder, Encoding.NONE, CLOCK, ALIGN_ON_1)); |
| 128 | StructDeclaration extended = new StructDeclaration(ALIGN_ON_8); |
| 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)); |
| 131 | v.addField(COMPACT, compact); |
| 132 | v.addField(EXTENDED, extended); |
| 133 | ref.addField(VARIANT_NAME, v); |
| 134 | fReferenceStructs.add(ref); |
| 135 | } |
| 136 | |
| 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 | |
| 151 | @Override |
| 152 | public EventHeaderDefinition createDefinition(@Nullable IDefinitionScope definitionScope, String fieldName, BitBuffer input) throws CTFException { |
| 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); |
| 164 | long id = input.get(ID_SIZE, false); |
| 165 | if (id > Integer.MAX_VALUE) { |
| 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$ |
| 167 | } |
| 168 | long timestampLong = input.get(FULL_TS, false); |
| 169 | input.setByteOrder(bo); |
| 170 | return new EventHeaderDefinition(this, (int) id, timestampLong, FULL_TS); |
| 171 | |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | public long getAlignment() { |
| 176 | return ALIGN_ON_8; |
| 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 | */ |
| 191 | public boolean isCompactEventHeader(@Nullable StructDeclaration declaration) { |
| 192 | if (declaration == null) { |
| 193 | return false; |
| 194 | } |
| 195 | for (IDeclaration ref : fReferenceStructs) { |
| 196 | if (ref.isBinaryEquivalent(declaration)) { |
| 197 | return true; |
| 198 | } |
| 199 | } |
| 200 | return false; |
| 201 | } |
| 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 | } |
| 209 | |
| 210 | @Override |
| 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 | |
| 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 | |
| 246 | } |