1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2023. All rights reserved. 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 16 #ifndef EBPF_SPLITTER_H 17 #define EBPF_SPLITTER_H 18 #ifndef is_linux 19 #include "dfx_nonlinux_define.h" 20 #else 21 #include <elf.h> 22 #endif 23 #include <string> 24 #include "ebpf_data_structure.h" 25 #include "event_parser_base.h" 26 #include "process_filter.h" 27 #include "quatra_map.h" 28 #include "string_help.h" 29 #include "trace_data_cache.h" 30 #include "trace_streamer_filters.h" 31 #include "unordered_map" 32 #include "pbreader_file_header.h" 33 #include "common_types.h" 34 35 namespace SysTuning { 36 namespace TraceStreamer { 37 using namespace SysTuning::EbpfStdtype; 38 class EbpfSplitter { 39 public: 40 void SetEbpfDataOffset(uint64_t offset); 41 void SetSpliteTimeRange(uint64_t splitFileMinTs, uint64_t splitFileMaxTs); 42 bool AddAndSplitEbpfData(std::deque<uint8_t> &dequeBuffer); RecordEbpfProfilerHeader(uint8_t * buffer,uint32_t len)43 void RecordEbpfProfilerHeader(uint8_t *buffer, uint32_t len) 44 { 45 (void)memcpy_s(&profilerHeader_, sizeof(profilerHeader_), buffer, len); 46 } GetEbpfSplitResult()47 const auto &GetEbpfSplitResult() 48 { 49 return ebpfSplitResult_; 50 } ClearEbpfSplitResult()51 auto ClearEbpfSplitResult() 52 { 53 splittedLen_ = 0; 54 usefulDataLen_ = 0; 55 ebpfBuffer_.clear(); 56 ebpfSplitResult_.clear(); 57 splitEbpfHeader_ = nullptr; 58 offsetOfEbpfDataInFile_ = 0; 59 } 60 61 private: 62 bool SplitEbpfHeader(std::deque<uint8_t> &dequeBuffer); 63 void SplitEbpfBodyData(std::deque<uint8_t> &dequeBuffer); 64 void AppendSplitOriginSegResult(uint32_t segLen); 65 template <typename FixedHeader> AppendSplitResultWithFixedHeader(uint32_t segLen,std::deque<uint8_t> & dequeBuffer,FixedHeader & fixedHeader)66 void AppendSplitResultWithFixedHeader(uint32_t segLen, std::deque<uint8_t> &dequeBuffer, FixedHeader &fixedHeader) 67 { 68 std::copy_n(dequeBuffer.begin() + EBPF_TITLE_SIZE, sizeof(FixedHeader), reinterpret_cast<char *>(&fixedHeader)); 69 if (fixedHeader.endTime <= splitFileMaxTs_ && fixedHeader.startTime >= splitFileMinTs_) { 70 AppendSplitOriginSegResult(segLen); 71 } 72 } 73 uint64_t splittedLen_ = 0; 74 uint64_t usefulDataLen_ = 0; 75 std::deque<uint8_t> ebpfBuffer_; 76 uint64_t offsetOfEbpfDataInFile_ = 0; 77 uint64_t splitFileMinTs_ = INVALID_UINT64; 78 uint64_t splitFileMaxTs_ = INVALID_UINT64; 79 std::vector<HtraceSplitResult> ebpfSplitResult_; 80 std::unique_ptr<EbpfDataHeader> splitEbpfHeader_; 81 ProfilerTraceFileHeader profilerHeader_; 82 }; 83 } // namespace TraceStreamer 84 } // namespace SysTuning 85 #endif // EBPF_SPLITTER_H 86