1 /*******************************************************************************
2 * Copyright (c) 2014 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.ctf
.core
.event
.types
.composite
;
15 import java
.nio
.ByteOrder
;
17 import org
.eclipse
.linuxtools
.ctf
.core
.event
.io
.BitBuffer
;
18 import org
.eclipse
.linuxtools
.ctf
.core
.event
.scope
.IDefinitionScope
;
19 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.Declaration
;
20 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.EnumDeclaration
;
21 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IDeclaration
;
22 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IEventHeaderDeclaration
;
23 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.IntegerDeclaration
;
24 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.StructDeclaration
;
25 import org
.eclipse
.linuxtools
.ctf
.core
.event
.types
.VariantDeclaration
;
26 import org
.eclipse
.linuxtools
.ctf
.core
.trace
.CTFReaderException
;
29 * An event header declaration is a declaration of a structure defined in the
30 * CTF spec examples section 6.1.1 . It is used in LTTng traces. This will
31 * accelerate reading of the trace.
36 * struct event_header_compact {
37 * enum : uint5_t { compact = 0 ... 30, extended = 31 } id;
40 * uint27_clock_monotonic_t timestamp;
44 * uint64_clock_monotonic_t timestamp;
50 * @author Matthew Khouzam
52 public class EventHeaderCompactDeclaration
extends Declaration
implements IEventHeaderDeclaration
{
54 private static final int COMPACT_SIZE
= 1;
55 private static final int VARIANT_SIZE
= 2;
56 private static final int EXTENDED_FIELD_SIZE
= 2;
60 private static final int COMPACT_ID
= 5;
61 private static final int EXTENDED_VALUE
= 31;
63 * Full sized id is 32 bits
65 private static final int ID_SIZE
= 32;
67 * Full sized timestamp is 64 bits
69 private static final int FULL_TS
= 64;
71 * Compact timestamp is 27 bits,
73 private static final int COMPACT_TS
= 27;
75 * Maximum size = largest this header can be
77 private static final int MAX_SIZE
= 104;
81 private static final int ALIGN
= 8;
83 * Name of the variant according to the spec
85 private static final String V
= "v"; //$NON-NLS-1$
87 private final ByteOrder fByteOrder
;
90 * Event Header Declaration
95 public EventHeaderCompactDeclaration(ByteOrder byteOrder
) {
96 fByteOrder
= byteOrder
;
100 public EventHeaderDefinition
createDefinition(IDefinitionScope definitionScope
, String fieldName
, BitBuffer input
) throws CTFReaderException
{
102 ByteOrder bo
= input
.getByteOrder();
103 input
.setByteOrder(fByteOrder
);
104 int enumId
= (int) input
.get(COMPACT_ID
, false);
105 if (enumId
!= EXTENDED_VALUE
) {
106 long timestamp2
= input
.get(COMPACT_TS
, false);
107 input
.setByteOrder(bo
);
108 return new EventHeaderDefinition(this, enumId
, timestamp2
, COMPACT_TS
);
110 // needed since we read 5 bits
111 input
.position(input
.position() + 3);
112 long id
= input
.get(ID_SIZE
, false);
113 if (id
> Integer
.MAX_VALUE
) {
114 throw new CTFReaderException("ID " + id
+ " larger than " + Integer
.MAX_VALUE
+ " is currently unsupported by the parser"); //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$
116 long timestampLong
= input
.get(FULL_TS
, false);
117 input
.setByteOrder(bo
);
118 return new EventHeaderDefinition(this, (int) id
, timestampLong
, FULL_TS
);
123 public long getAlignment() {
128 public int getMaximumSize() {
133 * Check if a given struct declaration is an event header
137 * @return true if the struct is a compact event header
139 public static boolean isCompactEventHeader(StructDeclaration declaration
) {
141 IDeclaration iDeclaration
= declaration
.getFields().get(ID
);
142 if (!(iDeclaration
instanceof EnumDeclaration
)) {
145 EnumDeclaration eId
= (EnumDeclaration
) iDeclaration
;
146 if (eId
.getContainerType().getLength() != COMPACT_ID
) {
149 iDeclaration
= declaration
.getFields().get(V
);
151 if (!(iDeclaration
instanceof VariantDeclaration
)) {
154 VariantDeclaration vDec
= (VariantDeclaration
) iDeclaration
;
155 if (!vDec
.hasField(COMPACT
) || !vDec
.hasField(EXTENDED
)) {
158 if (vDec
.getFields().size() != VARIANT_SIZE
) {
161 iDeclaration
= vDec
.getFields().get(COMPACT
);
162 if (!(iDeclaration
instanceof StructDeclaration
)) {
165 StructDeclaration compactDec
= (StructDeclaration
) iDeclaration
;
166 if (compactDec
.getFields().size() != COMPACT_SIZE
) {
169 if (!compactDec
.hasField(TIMESTAMP
)) {
172 iDeclaration
= compactDec
.getFields().get(TIMESTAMP
);
173 if (!(iDeclaration
instanceof IntegerDeclaration
)) {
176 IntegerDeclaration tsDec
= (IntegerDeclaration
) iDeclaration
;
177 if (tsDec
.getLength() != COMPACT_TS
|| tsDec
.isSigned()) {
180 iDeclaration
= vDec
.getFields().get(EXTENDED
);
181 if (!(iDeclaration
instanceof StructDeclaration
)) {
184 StructDeclaration extendedDec
= (StructDeclaration
) iDeclaration
;
185 if (!extendedDec
.hasField(TIMESTAMP
)) {
188 if (extendedDec
.getFields().size() != EXTENDED_FIELD_SIZE
) {
191 iDeclaration
= extendedDec
.getFields().get(TIMESTAMP
);
192 if (!(iDeclaration
instanceof IntegerDeclaration
)) {
195 tsDec
= (IntegerDeclaration
) iDeclaration
;
196 if (tsDec
.getLength() != FULL_TS
|| tsDec
.isSigned()) {
199 iDeclaration
= extendedDec
.getFields().get(ID
);
200 if (!(iDeclaration
instanceof IntegerDeclaration
)) {
203 IntegerDeclaration iId
= (IntegerDeclaration
) iDeclaration
;
204 if (iId
.getLength() != ID_SIZE
|| iId
.isSigned()) {