1 /** 2 * Copyright (c) 2021-2024 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 PANDA_TOOLS_SAMPLER_TRACE_DUMPER_H 17 #define PANDA_TOOLS_SAMPLER_TRACE_DUMPER_H 18 19 #include <unordered_map> 20 #include <fstream> 21 22 #include "libpandabase/macros.h" 23 #include "libpandabase/utils/logger.h" 24 #include "libpandafile/file-inl.h" 25 #include "libpandafile/class_data_accessor-inl.h" 26 27 #include "runtime/tooling/sampler/sample_info.h" 28 29 namespace ark::tooling::sampler { 30 31 using ModuleMap = std::unordered_map<uintptr_t, std::unique_ptr<const panda_file::File>>; 32 using MethodMap = std::unordered_map<const panda_file::File *, std::unordered_map<uint32_t, std::string>>; 33 34 enum class DumpType { 35 WITHOUT_THREAD_SEPARATION, // Creates single csv file without separating threads 36 THREAD_SEPARATION_BY_TID, // Creates single csv file with separating threads by thread_id 37 THREAD_SEPARATION_BY_CSV // Creates csv file for each thread 38 }; 39 40 class TraceDumper { 41 public: 42 NO_COPY_SEMANTIC(TraceDumper); 43 NO_MOVE_SEMANTIC(TraceDumper); 44 TraceDumper(ModuleMap * modulesMap,MethodMap * methodsMap)45 explicit TraceDumper(ModuleMap *modulesMap, MethodMap *methodsMap) 46 : modulesMap_(modulesMap), methodsMap_(methodsMap) 47 { 48 } 49 virtual ~TraceDumper() = default; 50 51 void DumpTraces(const SampleInfo &sample, size_t count); 52 53 void SetBuildSystemFrames(bool buildSystemFrames); 54 55 protected: 56 static void WriteThreadId(std::ofstream &stream, uint32_t threadId); 57 static void WriteThreadStatus(std::ofstream &stream, SampleInfo::ThreadStatus threadStatus); 58 59 private: 60 virtual std::ofstream &ResolveStream(const SampleInfo &sample) = 0; 61 62 std::string ResolveName(const panda_file::File *pf, uint64_t fileId) const; 63 64 private: 65 ModuleMap *modulesMap_ {nullptr}; 66 MethodMap *methodsMap_ {nullptr}; 67 68 bool buildSystemFrames_ {false}; 69 }; 70 71 class SingleCSVDumper final : public TraceDumper { 72 public: 73 NO_COPY_SEMANTIC(SingleCSVDumper); 74 NO_MOVE_SEMANTIC(SingleCSVDumper); 75 SingleCSVDumper(const char * filename,DumpType option,ModuleMap * modulesMap,MethodMap * methodsMap,bool buildColdGraph)76 explicit SingleCSVDumper(const char *filename, DumpType option, ModuleMap *modulesMap, MethodMap *methodsMap, 77 bool buildColdGraph) 78 : TraceDumper(modulesMap, methodsMap), 79 stream_(std::ofstream(filename)), 80 option_(option), 81 buildColdGraph_(buildColdGraph) 82 { 83 } 84 ~SingleCSVDumper() override = default; 85 86 private: 87 std::ofstream &ResolveStream(const SampleInfo &sample) override; 88 89 private: 90 std::ofstream stream_; 91 DumpType option_; 92 bool buildColdGraph_; 93 }; 94 95 class MultipleCSVDumper final : public TraceDumper { 96 public: 97 NO_COPY_SEMANTIC(MultipleCSVDumper); 98 NO_MOVE_SEMANTIC(MultipleCSVDumper); 99 MultipleCSVDumper(const char * filename,ModuleMap * modulesMap,MethodMap * methodsMap,bool buildColdGraph)100 explicit MultipleCSVDumper(const char *filename, ModuleMap *modulesMap, MethodMap *methodsMap, bool buildColdGraph) 101 : TraceDumper(modulesMap, methodsMap), filename_(filename), buildColdGraph_(buildColdGraph) 102 { 103 } 104 ~MultipleCSVDumper() override = default; 105 106 private: 107 std::ofstream &ResolveStream(const SampleInfo &sample) override; 108 109 static std::string AddThreadIdToFilename(const std::string &filename, uint32_t threadId); 110 111 private: 112 std::string filename_; 113 std::unordered_map<size_t, std::ofstream> threadIdMap_; 114 bool buildColdGraph_; 115 }; 116 117 } // namespace ark::tooling::sampler 118 119 #endif // PANDA_TOOLS_SAMPLER_TRACE_DUMPER_H 120