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 HTRACE_PARSER_H 17 #define HTRACE_PARSER_H 18 #include <cstdint> 19 #include <limits> 20 #include <map> 21 #include <stdexcept> 22 #include <string> 23 #include <thread> 24 #include "common_types.h" 25 #include "common_types.pbreader.h" 26 #include "ebpf_data_parser.h" 27 #include "elf_parser.h" 28 #include "file.h" 29 #include "htrace_clock_detail_parser.h" 30 #include "htrace_cpu_detail_parser.h" 31 #include "htrace_cpu_data_parser.h" 32 #include "htrace_disk_io_parser.h" 33 #include "htrace_file_header.h" 34 #include "htrace_hidump_parser.h" 35 #include "htrace_hilog_parser.h" 36 #include "htrace_hisysevent_parser.h" 37 #include "htrace_js_memory_parser.h" 38 #include "htrace_mem_parser.h" 39 #include "htrace_native_hook_parser.h" 40 #include "htrace_network_parser.h" 41 #include "htrace_process_parser.h" 42 #include "htrace_symbols_detail_parser.h" 43 #include "log.h" 44 #include "parser_base.h" 45 #include "perf_data_parser.h" 46 #include "proto_reader_help.h" 47 #include "string_help.h" 48 #include "trace_data/trace_data_cache.h" 49 #include "trace_streamer_filters.h" 50 #include "ts_common.h" 51 52 namespace SysTuning { 53 namespace TraceStreamer { 54 using namespace SysTuning::base; 55 using namespace OHOS::Developtools::HiPerf::ELF; 56 using namespace OHOS::Developtools::HiPerf; 57 class HtraceParser : public ParserBase { 58 public: 59 HtraceParser(TraceDataCache* dataCache, const TraceStreamerFilters* filters); 60 ~HtraceParser(); 61 void ParseTraceDataSegment(std::unique_ptr<uint8_t[]> bufferStr, size_t size) override; 62 bool ReparseSymbolFilesAndResymbolization(std::string& symbolsPath, std::vector<std::string>& symbolsPaths); 63 void WaitForParserEnd(); 64 void EnableFileSeparate(bool enabled); 65 66 void GetSymbols(std::unique_ptr<ElfFile> elfPtr, 67 std::shared_ptr<ElfSymbolTable> symbols, 68 const std::string& filename); 69 bool ParserFileSO(std::string& directory, std::vector<std::string>& relativeFilePaths); 70 void TraceDataSegmentEnd(); 71 void StoreTraceDataSegment(std::unique_ptr<uint8_t[]> bufferStr, size_t size); 72 73 private: 74 bool ParseDataRecursively(std::deque<uint8_t>::iterator& packagesBegin, size_t& currentLength); 75 void ParseTraceDataItem(const std::string& buffer) override; 76 void FilterData(HtraceDataSegment& seg); 77 void ParserData(HtraceDataSegment& dataSeg); 78 79 private: 80 void ParseMemory(ProtoReader::ProfilerPluginData_Reader* pluginDataZero, HtraceDataSegment& dataSeg); 81 void ParseHilog(HtraceDataSegment& dataSeg); 82 void ParseFtrace(HtraceDataSegment& dataSeg); 83 void ParseFPS(HtraceDataSegment& dataSeg); 84 void ParseCpuUsage(HtraceDataSegment& dataSeg); 85 void ParseNetwork(HtraceDataSegment& dataSeg); 86 void ParseDiskIO(HtraceDataSegment& dataSeg); 87 void ParseProcess(HtraceDataSegment& dataSeg); 88 void ParseHisysevent(HtraceDataSegment& dataSeg); 89 void ParseHisyseventConfig(HtraceDataSegment& dataSeg); 90 void ParseJSMemory(HtraceDataSegment& dataSeg); 91 void ParseJSMemoryConfig(HtraceDataSegment& dataSeg); 92 void ParseThread(); 93 int32_t GetNextSegment(); 94 void FilterThread(); 95 96 bool InitProfilerTraceFileHeader(); 97 ProfilerTraceFileHeader profilerTraceFileHeader_; 98 uint64_t htraceCurentLength_ = 0; 99 bool hasGotSegLength_ = false; 100 bool hasGotHeader_ = false; 101 uint32_t nextLength_ = 0; 102 const size_t PACKET_SEG_LENGTH = 4; 103 const size_t PACKET_HEADER_LENGTH = 1024; 104 TraceDataCache* traceDataCache_; 105 std::unique_ptr<HtraceCpuDetailParser> htraceCpuDetailParser_; 106 std::unique_ptr<HtraceSymbolsDetailParser> htraceSymbolsDetailParser_; 107 std::unique_ptr<HtraceMemParser> htraceMemParser_; 108 std::unique_ptr<HtraceClockDetailParser> htraceClockDetailParser_; 109 std::unique_ptr<HtraceHiLogParser> htraceHiLogParser_; 110 std::unique_ptr<HtraceNativeHookParser> htraceNativeHookParser_; 111 std::unique_ptr<HtraceHidumpParser> htraceHidumpParser_; 112 std::unique_ptr<HtraceCpuDataParser> cpuUsageParser_; 113 std::unique_ptr<HtraceNetworkParser> networkParser_; 114 std::unique_ptr<HtraceDiskIOParser> diskIOParser_; 115 std::unique_ptr<HtraceProcessParser> processParser_; 116 std::unique_ptr<HtraceHisyseventParser> hisyseventParser_; 117 std::unique_ptr<HtraceJSMemoryParser> jsMemoryParser_; 118 std::unique_ptr<PerfDataParser> perfDataParser_; 119 std::unique_ptr<EbpfDataParser> ebpfDataParser_; 120 std::atomic<bool> filterThreadStarted_{false}; 121 const int32_t MAX_SEG_ARRAY_SIZE = 10000; 122 std::unique_ptr<HtraceDataSegment[]> dataSegArray_; 123 int32_t rawDataHead_ = 0; 124 bool toExit_ = false; 125 bool exited_ = false; 126 int32_t filterHead_ = 0; 127 int32_t parseHead_ = 0; 128 size_t htraceLength_ = 1024; 129 const int32_t sleepDur_ = 100; 130 bool parseThreadStarted_ = false; 131 const int32_t maxThread_ = 4; // 4 is the best on ubuntu 113MB/s, max 138MB/s, 6 is best on mac m1 21MB/s, 132 int32_t parserThreadCount_ = 0; 133 std::mutex htraceDataSegMux_ = {}; 134 bool supportThread_ = false; 135 ClockId dataSourceTypeTraceClockid_ = TS_CLOCK_UNKNOW; 136 ClockId dataSourceTypeMemClockid_ = TS_CLOCK_UNKNOW; 137 ClockId dataSourceTypeHilogClockid_ = TS_CLOCK_UNKNOW; 138 ClockId dataSourceTypeNativeHookClockid_ = TS_CLOCK_UNKNOW; 139 ClockId dataSourceTypeFpsClockid_ = TS_CLOCK_UNKNOW; 140 ClockId dataSourceTypeNetworkClockid_ = TS_CLOCK_UNKNOW; 141 ClockId dataSourceTypeDiskioClockid_ = TS_CLOCK_UNKNOW; 142 ClockId dataSourceTypeCpuClockid_ = TS_CLOCK_UNKNOW; 143 ClockId dataSourceTypeProcessClockid_ = TS_CLOCK_UNKNOW; 144 ClockId dataSourceTypeHisyseventClockid_ = TS_CLOCK_UNKNOW; 145 ClockId dataSourceTypeJSMemoryClockid_ = TS_CLOCK_UNKNOW; 146 std::shared_ptr<std::vector<std::shared_ptr<ElfSymbolTable>>> elfSymbolTables_; 147 }; 148 } // namespace TraceStreamer 149 } // namespace SysTuning 150 151 #endif // HTRACE_PARSER_H_ 152