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