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 PERF_DATA_PARSER_H 16 #define PERF_DATA_PARSER_H 17 #include <linux/perf_event.h> 18 #include <cstddef> 19 #include <cstdint> 20 #include <deque> 21 #include <set> 22 #include "common_types.h" 23 #include "event_parser_base.h" 24 #include "pbreader_file_header.h" 25 #include "htrace_plugin_time_parser.h" 26 #if is_mingw 27 #define unw_word_t uint64_t 28 #endif 29 #include "numerical_to_string.h" 30 #include "perf_events.h" 31 #include "perf_file_format.h" 32 #include "perf_file_reader.h" 33 #include "quatra_map.h" 34 #include "report.h" 35 #include "trace_data_cache.h" 36 #include "trace_streamer_filters.h" 37 38 namespace SysTuning { 39 namespace TraceStreamer { 40 using namespace OHOS::Developtools::HiPerf; 41 enum class SplitPerfState { 42 STARTING, 43 PARSING_HEAD, 44 WAIT_FOR_ATTR, 45 PARSING_ATTR, 46 WAIT_FOR_DATA, 47 PARSING_DATA, 48 PARSING_FEATURE_SECTION, 49 WAIT_FOR_FINISH 50 }; 51 class PerfDataParser : public EventParserBase, public HtracePluginTimeParser { 52 public: 53 PerfDataParser(TraceDataCache *dataCache, const TraceStreamerFilters *ctx); 54 ~PerfDataParser(); 55 uint64_t InitPerfDataAndLoad(const std::deque<uint8_t> &dequeBuffer, 56 uint64_t size, 57 uint64_t offset, 58 bool isSplitFile, 59 bool isFinish); 60 void Finish(); 61 void PerfReloadSymbolFiles(const std::vector<std::unique_ptr<SymbolsFile>> &symbolsFiles); GetPerfSplitResult()62 const auto &GetPerfSplitResult() 63 { 64 return splitResult_; 65 } ClearPerfSplitResult()66 void ClearPerfSplitResult() 67 { 68 SplitDataWithdraw(); 69 perfSplitError_ = false; 70 if (traceDataCache_->traceStartTime_ == INVALID_UINT64 || traceDataCache_->traceEndTime_ == 0) { 71 traceDataCache_->MixTraceTime(GetPluginStartTime(), GetPluginEndTime()); 72 } 73 return; 74 } SplitDataWithdraw()75 void SplitDataWithdraw() 76 { 77 processedLength_ = 0; 78 splitDataEnd_ = false; 79 perfDataOffset_ = 0; 80 featureSection_.reset(); 81 featureSectioSize_ = 0; 82 splitState_ = SplitPerfState::STARTING; 83 splitResult_.clear(); 84 splitDataSize_ = 0; 85 return; 86 } RecordPerfProfilerHeader(uint8_t * buffer,uint32_t len)87 void RecordPerfProfilerHeader(uint8_t *buffer, uint32_t len) 88 { 89 (void)memcpy_s(&profilerHeader_, sizeof(profilerHeader_), buffer, len); 90 hasProfilerHead_ = true; 91 } 92 93 private: 94 void SetHM(); 95 bool Reload(); 96 bool LoadPerfData(); 97 void UpdateEventConfigInfo(); 98 void UpdateCmdlineInfo() const; 99 void LoadEventDesc(); 100 void UpdateReportWorkloadInfo() const; 101 void UpdateSymbolAndFilesData(); 102 void UpdateClockType(); 103 bool RecordCallBack(std::unique_ptr<PerfEventRecord> record); 104 void UpdatePerfSampleData(uint32_t callChainId, std::unique_ptr<PerfRecordSample> &sample); 105 std::tuple<uint64_t, DataIndex> GetFileIdWithLikelyFilePath(const std::string &inputFilePath); 106 bool ReloadPerfFile(const std::unique_ptr<SymbolsFile> &symbolsFile, uint64_t &fileId, DataIndex &filePathIndex); 107 void ReloadPerfCallChain(const std::unique_ptr<SymbolsFile> &symbolsFile, uint64_t fileId, DataIndex filePathIndex); 108 uint32_t UpdateCallChainUnCompressed(const std::unique_ptr<PerfRecordSample> &sample); 109 SplitPerfState DataLengthProcessing(const std::deque<uint8_t> &dequeBuffer, 110 perf_event_header &dataHeader, 111 uint64_t size, 112 uint64_t &processedLen, 113 bool &invalid); 114 uint64_t SplitPerfData(const std::deque<uint8_t> &dequeBuffer, uint64_t size, uint64_t offset, bool isFinish); 115 116 uint64_t DataProcessingLength(const std::deque<uint8_t> &dequeBuffer, 117 uint64_t size, 118 uint64_t offset, 119 bool isFinish); 120 bool SplitPerfStarting(const std::deque<uint8_t> &dequeBuffer, 121 uint64_t size, 122 uint64_t &processedLen, 123 bool &invalid); 124 bool SplitPerfParsingHead(const std::deque<uint8_t> &dequeBuffer, 125 uint64_t size, 126 uint64_t &processedLen, 127 bool &invalid); 128 bool SplitPerfWaitForAttr(const std::deque<uint8_t> &dequeBuffer, 129 uint64_t size, 130 uint64_t &processedLen, 131 bool &invalid); 132 bool SplitPerfParsingAttr(const std::deque<uint8_t> &dequeBuffer, 133 uint64_t size, 134 uint64_t &processedLen, 135 bool &invalid); 136 bool SplitPerfWaitForData(const std::deque<uint8_t> &dequeBuffer, 137 uint64_t size, 138 uint64_t &processedLen, 139 bool &invalid); 140 bool SplitPerfParsingData(const std::deque<uint8_t> &dequeBuffer, 141 uint64_t size, 142 uint64_t &processedLen, 143 bool &invalid); 144 bool SplitPerfParsingFeatureSection(const std::deque<uint8_t> &dequeBuffer, 145 uint64_t size, 146 uint64_t &processedLen, 147 bool &invalid); 148 bool SplitPerfWaitForFinish(const std::deque<uint8_t> &dequeBuffer, 149 uint64_t size, 150 uint64_t &processedLen, 151 bool &invalid); 152 153 uint32_t callChainId_ = 0; 154 std::unique_ptr<PerfFileReader> recordDataReader_ = nullptr; 155 std::unique_ptr<uint8_t[]> buffer_ = {}; 156 size_t bufferSize_ = 0; 157 bool cpuOffMode_ = false; 158 std::unique_ptr<Report> report_ = nullptr; 159 uint32_t useClockId_ = 0; 160 uint32_t clockId_ = 0; 161 enum PerfClockType { 162 PERF_CLOCK_REALTIME = 0, 163 PERF_CLOCK_MONOTONIC, 164 PERF_CLOCK_MONOTONIC_RAW = 4, 165 PERF_CLOCK_BOOTTIME = 7, 166 }; 167 DataIndex configNameIndex_ = 0; 168 DataIndex workloaderIndex_ = 0; 169 DataIndex cmdlineIndex_ = 0; 170 DataIndex runingStateIndex_ = 0; 171 DataIndex suspendStatIndex_ = 0; 172 DataIndex unkonwnStateIndex_ = 0; 173 std::unordered_multimap<uint64_t, uint64_t> tidToPid_ = {}; 174 const std::map<uint32_t, uint32_t> perfToTSClockType_ = {{PERF_CLOCK_REALTIME, TS_CLOCK_REALTIME}, 175 {PERF_CLOCK_MONOTONIC, TS_MONOTONIC}, 176 {PERF_CLOCK_MONOTONIC_RAW, TS_MONOTONIC_RAW}, 177 {PERF_CLOCK_BOOTTIME, TS_CLOCK_BOOTTIME}}; 178 std::map<uint64_t, uint64_t> fileDataDictIdToFileId_ = {}; 179 std::hash<std::string_view> hashFun_; 180 DoubleMap<uint32_t, uint64_t, uint32_t> pidAndStackHashToCallChainId_; 181 std::unordered_map<uint32_t, std::tuple<pid_t, pid_t>> callChainIdToThreadInfo_ = {}; 182 const std::string tmpPerfData_ = "ts_tmp.perf.data"; 183 const std::string cpuOffEventName_ = "sched:sched_switch"; 184 const std::string wakingEventName_ = "sched:sched_waking"; 185 186 ProfilerTraceFileHeader profilerHeader_; 187 bool hasProfilerHead_ = false; 188 perf_file_header perfHeader_; 189 uint32_t featureCount_ = 0; 190 uint64_t sampleType_ = 0; 191 uint32_t sampleTimeOffset_ = 0; 192 uint64_t processedLength_ = 0; 193 bool splitDataEnd_ = false; 194 bool perfSplitError_ = false; 195 int32_t perfDataOffset_ = 0; 196 std::unique_ptr<uint8_t[]> featureSection_ = nullptr; 197 size_t featureSectioSize_ = 0; 198 SplitPerfState splitState_ = SplitPerfState::STARTING; 199 uint64_t splitDataSize_ = 0; 200 std::vector<HtraceSplitResult> splitResult_; 201 bool stackCompressedMode_ = false; 202 std::set<uint32_t> savedCompressedCallChainId_ = {}; 203 uint32_t compressFailedCallChainId_ = INVALID_UINT32; 204 }; 205 } // namespace TraceStreamer 206 } // namespace SysTuning 207 #endif // PERF_DATA_PARSER_H 208