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 #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 "event_parser_base.h" 23 #include "htrace_plugin_time_parser.h" 24 #include "log.h" 25 #if is_mingw 26 #define unw_word_t uint64_t 27 #endif 28 #include "perf_events.h" 29 #include "perf_file_format.h" 30 #include "perf_file_reader.h" 31 #include "quatra_map.h" 32 #include "report.h" 33 #include "trace_data/trace_data_cache.h" 34 #include "trace_streamer_filters.h" 35 36 namespace SysTuning { 37 namespace TraceStreamer { 38 using namespace OHOS::Developtools::HiPerf; 39 class PerfDataParser : public EventParserBase, public HtracePluginTimeParser { 40 public: 41 PerfDataParser(TraceDataCache* dataCache, const TraceStreamerFilters* ctx); 42 ~PerfDataParser(); 43 void InitPerfDataAndLoad(const std::deque<uint8_t> dequeBuffer, uint64_t size); 44 void Finish(); 45 bool PerfReloadSymbolFiles(std::vector<std::string>& symbolsPaths); 46 47 private: 48 bool Reload(); 49 bool LoadPerfData(); 50 void UpdateEventConfigInfo(); 51 void UpdateCmdlineInfo() const; 52 void LoadEventDesc(); 53 void UpdateReportWorkloadInfo() const; 54 void UpdateSymbolAndFilesData(); 55 void UpdateClockType(); 56 bool RecordCallBack(std::unique_ptr<PerfEventRecord> record); 57 void UpdatePerfSampleData(uint32_t callChainId, std::unique_ptr<PerfRecordSample>& sample); 58 uint32_t UpdatePerfCallChainData(const std::unique_ptr<PerfRecordSample>& sample); 59 60 class CallStackTemp { 61 public: CallStackTemp()62 CallStackTemp() {} CallStackTemp(uint32_t depth,uint64_t vaddr,uint64_t fileId,uint32_t symbolId)63 CallStackTemp(uint32_t depth, uint64_t vaddr, uint64_t fileId, uint32_t symbolId) 64 : depth_(depth), vaddrInFile_(vaddr), fileId_(fileId), symbolId_(symbolId) 65 { 66 } ~CallStackTemp()67 ~CallStackTemp() {} 68 uint32_t depth_ = 0; 69 uint64_t vaddrInFile_ = 0; 70 uint64_t fileId_ = 0; 71 uint32_t symbolId_ = 0; 72 }; 73 uint32_t callChainId_ = 0; 74 std::unique_ptr<PerfFileReader> recordDataReader_ = nullptr; 75 const std::string cpuOffEventName_ = "sched:sched_switch"; 76 const std::string wakingEventName_ = "sched:sched_waking"; 77 std::unique_ptr<uint8_t[]> buffer_ = {}; 78 size_t bufferSize_ = 0; 79 bool cpuOffMode_ = false; 80 std::unique_ptr<Report> report_ = nullptr; 81 uint32_t useClockId_ = 0; 82 uint32_t clockId_ = 0; 83 enum PerfClockType { 84 PERF_CLOCK_REALTIME = 0, 85 PERF_CLOCK_MONOTONIC, 86 PERF_CLOCK_MONOTONIC_RAW = 4, 87 PERF_CLOCK_BOOTTIME = 7, 88 }; 89 DataIndex configNameIndex_ = 0; 90 DataIndex workloaderIndex_ = 0; 91 DataIndex cmdlineIndex_ = 0; 92 DataIndex runingStateIndex_ = 0; 93 DataIndex suspendStatIndex_ = 0; 94 DataIndex unkonwnStateIndex_ = 0; 95 std::unordered_multimap<uint64_t, uint64_t> tidToPid_ = {}; 96 const std::map<uint32_t, uint32_t> perfToTSClockType_ = {{PERF_CLOCK_REALTIME, TS_CLOCK_REALTIME}, 97 {PERF_CLOCK_MONOTONIC, TS_MONOTONIC}, 98 {PERF_CLOCK_MONOTONIC_RAW, TS_MONOTONIC_RAW}, 99 {PERF_CLOCK_BOOTTIME, TS_CLOCK_BOOTTIME}}; 100 std::map<uint64_t, uint64_t> fileDataDictIdToFileId_ = {}; 101 QuatraMap<uint64_t, uint64_t, uint64_t, uint64_t, uint32_t> frameToCallChainId_; 102 const std::string tmpPerfData_ = "ts_tmp.perf.data"; 103 }; 104 } // namespace TraceStreamer 105 } // namespace SysTuning 106 #endif // PERF_DATA_PARSER_H 107