1317021Sdim//===-- xray_fdr_log_records.h  -------------------------------------------===//
2317021Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6317021Sdim//
7317021Sdim//===----------------------------------------------------------------------===//
8317021Sdim//
9317021Sdim// This file is a part of XRay, a function call tracing system.
10317021Sdim//
11317021Sdim//===----------------------------------------------------------------------===//
12317021Sdim#ifndef XRAY_XRAY_FDR_LOG_RECORDS_H
13317021Sdim#define XRAY_XRAY_FDR_LOG_RECORDS_H
14344779Sdim#include <cstdint>
15317021Sdim
16344779Sdimnamespace __xray {
17344779Sdim
18317021Sdimenum class RecordType : uint8_t { Function, Metadata };
19317021Sdim
20317021Sdim// A MetadataRecord encodes the kind of record in its first byte, and have 15
21317021Sdim// additional bytes in the end to hold free-form data.
22317021Sdimstruct alignas(16) MetadataRecord {
23317021Sdim  // A MetadataRecord must always have a type of 1.
24317021Sdim  /* RecordType */ uint8_t Type : 1;
25317021Sdim
26317021Sdim  // Each kind of record is represented as a 7-bit value (even though we use an
27317021Sdim  // unsigned 8-bit enum class to do so).
28317021Sdim  enum class RecordKinds : uint8_t {
29317021Sdim    NewBuffer,
30317021Sdim    EndOfBuffer,
31317021Sdim    NewCPUId,
32317021Sdim    TSCWrap,
33317021Sdim    WalltimeMarker,
34318384Sdim    CustomEventMarker,
35327952Sdim    CallArgument,
36327952Sdim    BufferExtents,
37341825Sdim    TypedEventMarker,
38341825Sdim    Pid,
39317021Sdim  };
40327952Sdim
41317021Sdim  // Use 7 bits to identify this record type.
42317021Sdim  /* RecordKinds */ uint8_t RecordKind : 7;
43317021Sdim  char Data[15];
44317021Sdim} __attribute__((packed));
45317021Sdim
46317021Sdimstatic_assert(sizeof(MetadataRecord) == 16, "Wrong size for MetadataRecord.");
47317021Sdim
48317021Sdimstruct alignas(8) FunctionRecord {
49317021Sdim  // A FunctionRecord must always have a type of 0.
50317021Sdim  /* RecordType */ uint8_t Type : 1;
51317021Sdim  enum class RecordKinds {
52317021Sdim    FunctionEnter = 0x00,
53317021Sdim    FunctionExit = 0x01,
54317021Sdim    FunctionTailExit = 0x02,
55317021Sdim  };
56317021Sdim  /* RecordKinds */ uint8_t RecordKind : 3;
57317021Sdim
58317021Sdim  // We only use 28 bits of the function ID, so that we can use as few bytes as
59317021Sdim  // possible. This means we only support 2^28 (268,435,456) unique function ids
60317021Sdim  // in a single binary.
61317021Sdim  int FuncId : 28;
62317021Sdim
63317021Sdim  // We use another 4 bytes to hold the delta between the previous entry's TSC.
64317021Sdim  // In case we've found that the distance is greater than the allowable 32 bits
65317021Sdim  // (either because we are running in a different CPU and the TSC might be
66317021Sdim  // different then), we should use a MetadataRecord before this FunctionRecord
67317021Sdim  // that will contain the full TSC for that CPU, and keep this to 0.
68317021Sdim  uint32_t TSCDelta;
69317021Sdim} __attribute__((packed));
70317021Sdim
71317021Sdimstatic_assert(sizeof(FunctionRecord) == 8, "Wrong size for FunctionRecord.");
72317021Sdim
73344779Sdim} // namespace __xray
74344779Sdim
75317021Sdim#endif // XRAY_XRAY_FDR_LOG_RECORDS_H
76