1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef EBPF_DATA_STD_TYPE_H 16 #define EBPF_DATA_STD_TYPE_H 17 #include <elf.h> 18 #include <map> 19 #include "ts_common.h" 20 namespace SysTuning { 21 namespace EbpfStdtype { 22 struct EbpfDataHeader { 23 static constexpr uint32_t EBPF_DATA_HEADER_SIZE = 1024; 24 static constexpr uint64_t HEADER_MAGIC = 0x454C494646504245uLL; 25 struct HeaderData { 26 uint64_t magic = HEADER_MAGIC; 27 uint32_t headSize = EBPF_DATA_HEADER_SIZE; 28 uint32_t version; 29 uint32_t clock; 30 uint32_t cmdLineLen; 31 } __attribute__((packed)); 32 static constexpr uint32_t EBPF_COMMAND_MAX_SIZE = EBPF_DATA_HEADER_SIZE - sizeof(HeaderData); 33 HeaderData header; 34 char cmdline[EBPF_COMMAND_MAX_SIZE] = {'\0'}; 35 }; 36 37 enum EBPF_DATA_TYPE { 38 ITEM_EVENT_MAPS = 0, 39 ITEM_SYMBOL_INFO, 40 ITEM_EVENT_FS, 41 ITEM_EVENT_VM, 42 ITEM_EVENT_BIO, 43 ITEM_EVENT_STR, 44 }; 45 46 struct EbpfTypeAndLength { 47 uint32_t type = 0; 48 uint32_t length = 0; 49 } __attribute__((packed)); 50 const uint32_t EBPF_TITLE_SIZE = sizeof(EbpfTypeAndLength); 51 52 struct MapsFixedHeader { 53 uint64_t start = 0; 54 uint64_t end = 0; 55 uint32_t offset = 0; 56 uint32_t pid = 0; 57 uint32_t fileNameLen = 0; 58 } __attribute__((packed)); 59 60 enum SYSTEM_ENTRY_VALUE { ELF32_SYM = 16, ELF64_SYM = 24 }; 61 62 struct ElfEventFixedHeader { 63 uint64_t textVaddr = 0; 64 uint32_t textOffset = 0; 65 uint32_t strTabLen = 0; 66 uint32_t symTabLen = 0; 67 uint32_t fileNameLen = 0; 68 // value range: SYSTEM_ENTRY_LENGTH 69 uint32_t symEntLen = 0; 70 } __attribute__((packed)); 71 // the data following are 72 // uint8_t aa[strTabLen] // xx, bb, cc, dd 73 // uint8_t bb[symTabLen] //symEntLen elf64_sym, lef32_sym 74 75 const int ARGS_MAX = 4; 76 const int MAX_TRACER_SIZE = 8; 77 const int SINGLE_IP_SIZE = 8; 78 const int MAX_FS_EVENT_SZIE = 16; 79 const int MAX_PROCESS_NAME_SZIE = 16; 80 struct FsFixedHeader { 81 uint32_t pid = 0; 82 uint32_t tid = 0; 83 char tracerName[MAX_TRACER_SIZE] = {0}; 84 uint64_t startTime = 0; 85 uint64_t endTime = 0; 86 char eventType[MAX_FS_EVENT_SZIE] = {0}; 87 int32_t ret = 0; 88 uint16_t nrUserIPs = 0; 89 uint16_t type = 0; 90 uint64_t args[ARGS_MAX] = {0}; 91 char processName[MAX_PROCESS_NAME_SZIE] = {0}; 92 } __attribute__((packed)); 93 // the data following are ips ... 94 95 const int MAX_TRACER_NAME_LEN = 8; 96 const int MAX_TYPE_NAME_LEN = 16; 97 const int MAX_COMM_LEN = 16; 98 struct PagedMemoryFixedHeader { 99 uint32_t pid = 0; 100 uint32_t tid = 0; 101 char tagName[MAX_TRACER_NAME_LEN] = {0}; 102 uint64_t startTime = 0; 103 uint64_t endTime = 0; 104 char typeName[MAX_TYPE_NAME_LEN] = {0}; 105 uint64_t addr = 0; 106 uint32_t size = 0; 107 uint16_t nips = 0; 108 uint16_t type = 0; 109 char comm[MAX_COMM_LEN] = {0}; 110 } __attribute__((packed)); 111 112 struct BIOFixedHeader { 113 uint32_t pid = 0; 114 uint32_t tid = 0; 115 char processName[MAX_PROCESS_NAME_SZIE] = {0}; 116 uint64_t startTime = 0; 117 uint64_t endTime = 0; 118 uint32_t prio = 0; 119 uint32_t size = 0; 120 uint64_t blkcnt = 0; 121 uint32_t nips = 0; 122 uint32_t type = 0; 123 char typeName[MAX_PROCESS_NAME_SZIE] = {0}; 124 } __attribute__((packed)); 125 126 struct StrEventFixedHeader { 127 uint32_t pid = 0; 128 uint32_t tid = 0; 129 uint64_t startTime = 0; 130 uint32_t srcTracer = 0; 131 uint32_t srcType = 0; 132 uint32_t strLen = 0; 133 uint32_t padding = 0; 134 } __attribute__((packed)); 135 136 // EBPF clock relation 137 enum EbpfClockType { 138 EBPF_CLOCK_REALTIME = 0, 139 EBPF_CLOCK_BOOTTIME, 140 EBPF_CLOCK_MONOTONIC, 141 EBPF_CLOCK_MONOTONIC_RAW, 142 }; 143 144 const std::map<uint32_t, uint32_t> ebpfToTSClockType_ = { 145 {EBPF_CLOCK_REALTIME, TS_CLOCK_REALTIME}, 146 {EBPF_CLOCK_BOOTTIME, TS_CLOCK_BOOTTIME}, 147 {EBPF_CLOCK_MONOTONIC, TS_MONOTONIC}, 148 {EBPF_CLOCK_MONOTONIC_RAW, TS_MONOTONIC_RAW}, 149 }; 150 151 struct SymbolAndFilePathIndex { SymbolAndFilePathIndexSymbolAndFilePathIndex152 SymbolAndFilePathIndex() {} SymbolAndFilePathIndexSymbolAndFilePathIndex153 SymbolAndFilePathIndex(bool invalidValue) : flag(invalidValue) {} 154 bool flag = false; 155 DataIndex symbolIndex = INVALID_UINT64; 156 DataIndex filePathIndex = INVALID_UINT64; 157 }; 158 // file system event type 159 enum FUC_SUB_TYPE { 160 SYS_OPENAT2 = 1, 161 SYS_READ = 2, 162 SYS_WRITE = 3, 163 SYS_PREAD64 = 4, 164 SYS_PWRITE64 = 5, 165 SYS_READV = 6, 166 SYS_WRITEV = 7, 167 SYS_PREADV = 8, 168 SYS_PWRITEV = 9, 169 SYS_CLOSE = 10, 170 }; 171 172 const std::map<FUC_SUB_TYPE, const std::string> subTypeToName = { 173 {SYS_OPENAT2, "openat2"}, {SYS_READ, "read"}, {SYS_WRITE, "write"}, {SYS_PREAD64, "pread64"}, 174 {SYS_PWRITE64, "pwrite64"}, {SYS_READV, "readv"}, {SYS_WRITEV, "writev"}, {SYS_PREADV, "preadv"}, 175 {SYS_PWRITEV, "pwritev"}, {SYS_CLOSE, "close"}}; 176 177 enum FUC_SUMMARY_TYPE { 178 OPEN = 0, 179 CLOSE = 1, 180 READ = 2, 181 WRITE = 3, 182 }; 183 const std::map<uint32_t, uint32_t> fucSubToSummaryType = { 184 {SYS_OPENAT2, OPEN}, 185 {SYS_READ, READ}, 186 {SYS_WRITE, WRITE}, 187 {SYS_PREAD64, READ}, 188 {SYS_PWRITE64, WRITE}, 189 {SYS_READV, READ}, 190 {SYS_WRITEV, WRITE}, 191 {SYS_PREADV, READ}, 192 {SYS_PWRITEV, WRITE}, 193 {SYS_CLOSE, CLOSE}, 194 }; 195 const uint32_t MIN_USER_IP = 0xffffffff; 196 } // namespace EbpfStdtype 197 } // namespace SysTuning 198 #endif // EBPF_DATA_STD_TYPE_H 199