• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 protected:
54     static void WriteThreadId(std::ofstream &stream, uint32_t threadId);
55     static void WriteThreadStatus(std::ofstream &stream, SampleInfo::ThreadStatus threadStatus);
56 
57 private:
58     virtual std::ofstream &ResolveStream(const SampleInfo &sample) = 0;
59 
60     std::string ResolveName(const panda_file::File *pf, uint64_t fileId) const;
61 
62 private:
63     ModuleMap *modulesMap_ {nullptr};
64     MethodMap *methodsMap_ {nullptr};
65 };
66 
67 class SingleCSVDumper final : public TraceDumper {
68 public:
69     NO_COPY_SEMANTIC(SingleCSVDumper);
70     NO_MOVE_SEMANTIC(SingleCSVDumper);
71 
SingleCSVDumper(const char * filename,DumpType option,ModuleMap * modulesMap,MethodMap * methodsMap,bool buildColdGraph)72     explicit SingleCSVDumper(const char *filename, DumpType option, ModuleMap *modulesMap, MethodMap *methodsMap,
73                              bool buildColdGraph)
74         : TraceDumper(modulesMap, methodsMap),
75           stream_(std::ofstream(filename)),
76           option_(option),
77           buildColdGraph_(buildColdGraph)
78     {
79     }
80     ~SingleCSVDumper() override = default;
81 
82 private:
83     std::ofstream &ResolveStream(const SampleInfo &sample) override;
84 
85 private:
86     std::ofstream stream_;
87     DumpType option_;
88     bool buildColdGraph_;
89 };
90 
91 class MultipleCSVDumper final : public TraceDumper {
92 public:
93     NO_COPY_SEMANTIC(MultipleCSVDumper);
94     NO_MOVE_SEMANTIC(MultipleCSVDumper);
95 
MultipleCSVDumper(const char * filename,ModuleMap * modulesMap,MethodMap * methodsMap,bool buildColdGraph)96     explicit MultipleCSVDumper(const char *filename, ModuleMap *modulesMap, MethodMap *methodsMap, bool buildColdGraph)
97         : TraceDumper(modulesMap, methodsMap), filename_(filename), buildColdGraph_(buildColdGraph)
98     {
99     }
100     ~MultipleCSVDumper() override = default;
101 
102 private:
103     std::ofstream &ResolveStream(const SampleInfo &sample) override;
104 
105     static std::string AddThreadIdToFilename(const std::string &filename, uint32_t threadId);
106 
107 private:
108     std::string filename_;
109     std::unordered_map<size_t, std::ofstream> threadIdMap_;
110     bool buildColdGraph_;
111 };
112 
113 }  // namespace ark::tooling::sampler
114 
115 #endif  // PANDA_TOOLS_SAMPLER_TRACE_DUMPER_H
116