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 #ifndef FTRACE_PROCESSOR_H 16 #define FTRACE_PROCESSOR_H 17 #include <memory> 18 #include <regex> 19 #include <string> 20 #include <vector> 21 #include "cpu_detail_parser.h" 22 #include "ftrace_common_type.h" 23 #include "ftrace_event_processor.h" 24 #include "ftrace_field_processor.h" 25 #include "string_help.h" 26 #include "printk_formats_processor.h" 27 28 namespace SysTuning { 29 namespace TraceStreamer { 30 using namespace SysTuning::base; 31 constexpr uint32_t FTRACE_PAGE_SIZE = 4096; 32 constexpr uint32_t RMQ_ENTRY_ALIGN_MASK = (1 << 2) - 1; 33 class FtraceProcessor { 34 public: 35 FtraceProcessor(TraceDataCache *traceDataCache); 36 ~FtraceProcessor(); 37 38 bool SetupEvent(const std::string &desc); 39 40 bool HandlePage(FtraceCpuDetailMsg &cpuMsg, 41 CpuDetailParser &cpuDetailParser, 42 uint8_t page[], 43 bool &haveSplitSeg, 44 size_t size = FTRACE_PAGE_SIZE); IsSplitCpuTimeStampData(uint64_t CurTimeStamp,bool & haveSplitSeg)45 bool IsSplitCpuTimeStampData(uint64_t CurTimeStamp, bool &haveSplitSeg) 46 { 47 if (traceDataCache_->SplitFileMinTime() <= CurTimeStamp && 48 traceDataCache_->SplitFileMaxTime() >= CurTimeStamp) { 49 haveSplitSeg = true; 50 return true; 51 } 52 return false; 53 } 54 void HmProcessPageTraceDataEvents(RmqConsumerData *rmqData, 55 uint64_t timeStampBase, 56 FtraceCpuDetailMsg &cpuMsg, 57 CpuDetailParser &cpuDetailParser, 58 bool &haveSplitSeg); 59 bool HmParsePageData(FtraceCpuDetailMsg &cpuMsg, 60 CpuDetailParser &cpuDetailParser, 61 uint8_t *&data, 62 bool &haveSplitSeg); 63 bool HandleTgids(const std::string &tgids); 64 bool HandleCmdlines(const std::string &cmdlines); 65 66 public: 67 bool GetEventFormatById(uint32_t id, EventFormat &format); 68 69 int HeaderPageCommitSize(void); 70 bool HandleHeaderPageFormat(const std::string &formatInfo); 71 bool HandleEventFormat(const std::string &formatInfo, EventFormat &format); 72 bool HandleFieldFormat(const std::string &fieldLine, EventFormat &format); 73 bool HandleFieldType(const std::string &type, FieldFormat &field); 74 void PrintedFieldDetails(const FieldFormat &info); 75 static void HandleProtoType(FieldFormat &fieldFormat); 76 77 bool HandlePageHeader(); 78 79 // handle different page types 80 bool HandlePaddingData(const FtraceEventHeader &eventHeader); 81 bool HandleTimeExtend(const FtraceEventHeader &eventHeader); 82 bool HandleTimeStamp(const FtraceEventHeader &eventHeader); 83 bool HandleDataRecord(const FtraceEventHeader &eventHeader, 84 FtraceCpuDetailMsg &cpuMsg, 85 CpuDetailParser &cpuDetailParser); 86 87 bool HandleFtraceEvent(FtraceEvent &ftraceEvent, uint8_t data[], size_t dataSize, const EventFormat &format); 88 bool HandleFtraceCommonFields(FtraceEvent &ftraceEvent, uint8_t data[], size_t dataSize, const EventFormat &format); 89 std::unordered_map<int32_t, std::string> &GetTidToName(); 90 91 private: 92 std::regex fixedCharArrayRegex_; 93 std::regex flexDataLocArrayRegex_; 94 // first is event id, second is eventFormat 95 std::unordered_map<uint32_t, EventFormat> eventFormatDict_ = {}; 96 PageHeaderFormat pageHeaderFormat_ = {}; 97 std::string savedTgidPath_ = ""; 98 std::string savedCmdlines_ = ""; 99 100 uint8_t *curPos_ = nullptr; 101 uint8_t *curPage_ = nullptr; // cur page start 102 uint8_t *endPosOfData_ = nullptr; // end pos of event data 103 uint8_t *endPosOfPage_ = nullptr; // end pos of full page 104 uint64_t curTimestamp_ = 0; 105 PageHeader curPageHeader_ = {}; 106 107 // first is pid, second is tgid 108 std::unordered_map<int32_t, int32_t> tgidDict_ = {}; 109 // first is pid, second is taskName 110 std::unordered_map<int32_t, std::string> taskNameDict_ = {}; 111 TraceDataCache *traceDataCache_ = nullptr; 112 113 const std::string nameLinePrefix_ = "name:"; 114 const std::string idLinePrefix_ = "ID:"; 115 const std::string fieldLinePrefix_ = "field:"; 116 }; 117 } // namespace TraceStreamer 118 } // namespace SysTuning 119 #endif // FTRACE_PROCESSOR_H 120