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 "ebpf_data_parser.h" 26 #include "file.h" 27 #include "htrace_clock_detail_parser.h" 28 #include "htrace_cpu_detail_parser.h" 29 #include "htrace_cpu_data_parser.h" 30 #include "htrace_disk_io_parser.h" 31 #include "htrace_file_header.h" 32 #include "htrace_hidump_parser.h" 33 #include "htrace_hilog_parser.h" 34 #include "htrace_hisysevent_parser.h" 35 #include "htrace_mem_parser.h" 36 #include "htrace_native_hook_parser.h" 37 #include "htrace_network_parser.h" 38 #include "htrace_process_parser.h" 39 #include "htrace_symbols_detail_parser.h" 40 #include "log.h" 41 #include "parser_base.h" 42 #if defined WITH_PERF 43 #include "perf_data_parser.h" 44 #endif 45 #include "string_help.h" 46 #include "trace_data/trace_data_cache.h" 47 #include "trace_streamer_filters.h" 48 #include "ts_common.h" 49 50 namespace SysTuning { 51 namespace TraceStreamer { 52 using namespace SysTuning::base; 53 #if defined WITH_PERF 54 using namespace OHOS::Developtools::HiPerf; 55 #endif 56 class HtraceParser : public ParserBase { 57 public: 58 HtraceParser(TraceDataCache* dataCache, const TraceStreamerFilters* filters); 59 ~HtraceParser(); 60 void ParseTraceDataSegment(std::unique_ptr<uint8_t[]> bufferStr, size_t size) override; 61 void WaitForParserEnd(); 62 private: 63 bool ParseDataRecursively(std::deque<uint8_t>::iterator& packagesBegin, size_t& currentLength); 64 void ParseTraceDataItem(const std::string& buffer) override; 65 void FilterData(HtraceDataSegment& seg); 66 void ParserData(HtraceDataSegment& dataSeg); 67 private: 68 void ParseMemory(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 69 void ParseHilog(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 70 void ParseFtrace(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 71 void ParseNativeHook(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 72 void ParseFPS(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 73 void ParseCpuUsage(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 74 void ParseNetwork(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 75 void ParseDiskIO(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 76 void ParseProcess(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 77 void ParseHisysevent(const ProfilerPluginData& pluginData, HtraceDataSegment &dataSeg); 78 void ParseThread(); 79 int GetNextSegment(); 80 void FilterThread(); 81 enum ErrorCode { 82 ERROR_CODE_EXIT = -2, 83 ERROR_CODE_NODATA = -1 84 }; 85 bool InitProfilerTraceFileHeader(); 86 ProfilerTraceFileHeader profilerTraceFileHeader_; 87 uint64_t htraceCurentLength_ = 0; 88 bool hasGotSegLength_ = false; 89 bool hasGotHeader_ = false; 90 uint32_t nextLength_ = 0; 91 const size_t PACKET_SEG_LENGTH = 4; 92 const size_t PACKET_HEADER_LENGTH = 1024; 93 TraceDataCache* traceDataCache_; 94 std::unique_ptr<HtraceCpuDetailParser> htraceCpuDetailParser_; 95 std::unique_ptr<HtraceSymbolsDetailParser> htraceSymbolsDetailParser_; 96 std::unique_ptr<HtraceMemParser> htraceMemParser_; 97 std::unique_ptr<HtraceClockDetailParser> htraceClockDetailParser_; 98 std::unique_ptr<HtraceHiLogParser> htraceHiLogParser_; 99 std::unique_ptr<HtraceNativeHookParser> htraceNativeHookParser_; 100 std::unique_ptr<HtraceHidumpParser> htraceHidumpParser_; 101 std::unique_ptr<HtraceCpuDataParser> cpuUsageParser_; 102 std::unique_ptr<HtraceNetworkParser> networkParser_; 103 std::unique_ptr<HtraceDiskIOParser> diskIOParser_; 104 std::unique_ptr<HtraceProcessParser> processParser_; 105 std::unique_ptr<HtraceHisyseventParser> hisyseventParser_; 106 #if WITH_PERF 107 std::unique_ptr<PerfDataParser> perfDataParser_; 108 #endif 109 std::unique_ptr<EbpfDataParser> ebpfDataParser_; 110 std::atomic<bool> filterThreadStarted_{false}; 111 const int MAX_SEG_ARRAY_SIZE = 10000; 112 std::unique_ptr<HtraceDataSegment[]> dataSegArray_; 113 int rawDataHead_ = 0; 114 bool toExit_ = false; 115 bool exited_ = false; 116 int filterHead_ = 0; 117 int parseHead_ = 0; 118 size_t sizeAll_ = 0; 119 size_t htraceLength_ = 1024; 120 const int sleepDur_ = 100; 121 bool parseThreadStarted_ = false; 122 const int maxThread_ = 4; // 4 is the best on ubuntu 113MB/s, max 138MB/s, 6 is best on mac m1 21MB/s, 123 int parserThreadCount_ = 0; 124 std::mutex dataSegMux_ = {}; 125 bool supportThread_ = false; 126 ClockId dataSourceTypeTraceClockid_ = TS_CLOCK_UNKNOW; 127 ClockId dataSourceTypeMemClockid_ = TS_CLOCK_UNKNOW; 128 ClockId dataSourceTypeHilogClockid_ = TS_CLOCK_UNKNOW; 129 ClockId dataSourceTypeAllocationClockid_ = TS_CLOCK_UNKNOW; 130 ClockId dataSourceTypeFpsClockid_ = TS_CLOCK_UNKNOW; 131 ClockId dataSourceTypeNetworkClockid_ = TS_CLOCK_UNKNOW; 132 ClockId dataSourceTypeDiskioClockid_ = TS_CLOCK_UNKNOW; 133 ClockId dataSourceTypeCpuClockid_ = TS_CLOCK_UNKNOW; 134 ClockId dataSourceTypeProcessClockid_ = TS_CLOCK_UNKNOW; 135 ClockId dataSourceTypeHisyseventClockid_ = TS_CLOCK_UNKNOW; 136 }; 137 } // namespace TraceStreamer 138 } // namespace SysTuning 139 140 #endif // HTRACE_PARSER_H_ 141