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 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/trace_data_cache.h" 30 #include "trace_streamer_filters.h" 31 #include "unordered_map" 32 #include "htrace_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 SetProfilerHeader(const ProfilerTraceFileHeader& header); 42 void SetSpliteTimeRange(uint64_t splitFileMinTs, uint64_t splitFileMaxTs); 43 bool AddAndSplitEbpfData(std::deque<uint8_t>& dequeBuffer); RecordEbpfProfilerHeader(uint8_t * buffer,uint32_t len)44 void RecordEbpfProfilerHeader(uint8_t* buffer, uint32_t len) 45 { 46 (void)memcpy_s(&profilerHeader_, sizeof(profilerHeader_), buffer, len); 47 } GetEbpfSplitResult()48 const auto& GetEbpfSplitResult() 49 { 50 return ebpfSplitResult_; 51 } ClearEbpfSplitResult()52 auto ClearEbpfSplitResult() 53 { 54 splittedLen_ = 0; 55 usefulDataLen_ = 0; 56 ebpfBuffer_.clear(); 57 ebpfSplitResult_.clear(); 58 splitEbpfHeader_ = nullptr; 59 offsetOfEbpfDataInFile_ = 0; 60 } 61 62 private: 63 bool SplitEbpfHeader(std::deque<uint8_t>& dequeBuffer); 64 void SplitEbpfBodyData(std::deque<uint8_t>& dequeBuffer); 65 void AppendSplitOriginSegResult(uint32_t segLen); 66 template <typename FixedHeader> AppendSplitResultWithFixedHeader(uint32_t segLen,std::deque<uint8_t> & dequeBuffer,FixedHeader & fixedHeader)67 void AppendSplitResultWithFixedHeader(uint32_t segLen, std::deque<uint8_t>& dequeBuffer, FixedHeader& fixedHeader) 68 { 69 std::copy_n(dequeBuffer.begin() + EBPF_TITLE_SIZE, sizeof(FixedHeader), reinterpret_cast<char*>(&fixedHeader)); 70 if (fixedHeader.endTime <= splitFileMaxTs_ && fixedHeader.startTime >= splitFileMinTs_) { 71 AppendSplitOriginSegResult(segLen); 72 } 73 } 74 uint64_t splittedLen_ = 0; 75 uint64_t usefulDataLen_ = 0; 76 std::deque<uint8_t> ebpfBuffer_; 77 uint64_t offsetOfEbpfDataInFile_ = 0; 78 uint64_t splitFileMinTs_ = INVALID_UINT64; 79 uint64_t splitFileMaxTs_ = INVALID_UINT64; 80 std::vector<HtraceSplitResult> ebpfSplitResult_; 81 std::unique_ptr<EbpfDataHeader> splitEbpfHeader_; 82 ProfilerTraceFileHeader profilerHeader_; 83 }; 84 } // namespace TraceStreamer 85 } // namespace SysTuning 86 #endif // EBPF_SPLITTER_H 87