• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 ECMASCRIPT_COMPILER_LOG_H
17 #define ECMASCRIPT_COMPILER_LOG_H
18 
19 #include <algorithm>
20 #include <cstdint>
21 #include <iostream>
22 #include <map>
23 #include <set>
24 #include <sstream>
25 #include <string>
26 #include <vector>
27 #include <iomanip>
28 #include "ecmascript/log_wrapper.h"
29 #include "ecmascript/mem/clock_scope.h"
30 #include "ecmascript/mem/c_string.h"
31 #include "ecmascript/compiler/argument_accessor.h"
32 
33 namespace panda::ecmascript::kungfu {
34 class AotMethodLogList;
35 class MethodLogList;
36 
37 class CompilerLog {
38 public:
39     explicit CompilerLog(const std::string &logOpt);
40     CompilerLog() = default;
41     ~CompilerLog() = default;
42 
AllMethod()43     bool AllMethod() const
44     {
45         return allMethod_;
46     }
47 
CertainMethod()48     bool CertainMethod() const
49     {
50         return cerMethod_;
51     }
52 
NoneMethod()53     bool NoneMethod() const
54     {
55         return noneMethod_;
56     }
57 
OutputCIR()58     bool OutputCIR() const
59     {
60         return outputCIR_;
61     }
62 
OutputLLIR()63     bool OutputLLIR() const
64     {
65         return outputLLIR_;
66     }
67 
OutputASM()68     bool OutputASM() const
69     {
70         return outputASM_;
71     }
72 
OutputType()73     bool OutputType() const
74     {
75         return outputType_;
76     }
77 
GetEnableCompilerLogTime()78     bool GetEnableCompilerLogTime() const
79     {
80         return compilerLogTime_;
81     }
82 
SetEnableCompilerLogTime(bool compilerLogTime)83     void SetEnableCompilerLogTime(bool compilerLogTime)
84     {
85         compilerLogTime_ = compilerLogTime;
86     }
87 
GetEnableMethodLog()88     bool GetEnableMethodLog() const
89     {
90         return enableMethodLog_;
91     }
92 
SetEnableMethodLog(bool enableMethodLog)93     void SetEnableMethodLog(bool enableMethodLog)
94     {
95         enableMethodLog_ = enableMethodLog;
96     }
97 
EnableMethodCIRLog()98     bool EnableMethodCIRLog() const
99     {
100         return GetEnableMethodLog() && OutputCIR();
101     }
102 
EnableMethodASMLog()103     bool EnableMethodASMLog() const
104     {
105         return GetEnableMethodLog() && OutputASM();
106     }
107 
108     void SetMethodLog(const std::string &fileName, const std::string &methodName, AotMethodLogList *logList);
109     void SetStubLog(const std::string &stubName, MethodLogList *logList);
110     void AddCompiledMethod(const std::string& name, const CString& recordName);
111     void RemoveCompiledMethod(const std::string& name, const CString& recordName);
112     void Print() const;
113     void AddMethodTime(const std::string& name, uint32_t id, double time);
114     void AddPassTime(const std::string& name, double time);
115     int GetIndex();
116     void SetPGOMismatchResult(uint32_t &totalMethodCount, uint32_t &mismatchMethodCount,
117                               std::set<std::pair<std::string, CString>> &mismatchMethodSet);
118 
119     std::map<std::string, int> nameIndex_;
120 
121 private:
122     static constexpr int RECORD_LENS = 64;
123     static constexpr int PASS_LENS = 32;
124     static constexpr int METHOD_LENS = 16;
125     static constexpr int OFFSET_LENS = 8;
126     static constexpr int PERCENT_LENS = 4;
127     static constexpr int TIME_LENS = 8;
128     static constexpr int MILLION_TIME = 1000;
129     static constexpr int HUNDRED_TIME = 100;
130 
131     void PrintPassTime() const;
132     void PrintMethodTime() const;
133     void PrintTime() const;
134     void PrintCompiledMethod() const;
135     void PrintPGOMismatchedMethod() const;
136 
137     int idx_ {0};
138     bool allMethod_ {false};
139     bool cerMethod_ {false};
140     bool noneMethod_ {false};
141     bool outputCIR_ {false};
142     bool outputLLIR_ {false};
143     bool outputASM_ {false};
144     bool outputType_ {false};
145     bool compilerLogTime_ {true};
146     bool enableMethodLog_ {false};
147     std::map<std::string, double> timePassMap_ {};
148     std::map<std::pair<uint32_t, std::string>, double> timeMethodMap_ {};
149     std::set<std::pair<std::string, CString>> compiledMethodSet_ {};
150     uint32_t totalPGOMethodCount_ {0};
151     uint32_t mismatchPGOMethodCount_ {0};
152     std::set<std::pair<std::string, CString>> mismatchPGOMethodSet_ {};
153 };
154 
155 class MethodLogList {
156 public:
MethodLogList(const std::string & logMethods)157     explicit MethodLogList(const std::string &logMethods) : methods_(logMethods) {}
158     ~MethodLogList() = default;
159     bool IncludesMethod(const std::string &methodName) const;
160 private:
161     std::string methods_ {};
162 };
163 
164 class AotMethodLogList : public MethodLogList {
165 public:
166     static const char fileSplitSign = ':';
167     static const char methodSplitSign = ',';
168 
AotMethodLogList(const std::string & logMethods)169     explicit AotMethodLogList(const std::string &logMethods) : MethodLogList(logMethods)
170     {
171         ParseFileMethodsName(logMethods);
172     }
173     ~AotMethodLogList() = default;
174 
175     bool IncludesMethod(const std::string &fileName, const std::string &methodName) const;
176 
177 private:
178     std::vector<std::string> spiltString(const std::string &str, const char ch);
179     void ParseFileMethodsName(const std::string &logMethods);
180     std::map<std::string, std::vector<std::string>> fileMethods_ {};
181 };
182 
183 class TimeScope : public ClockScope {
184 public:
185     TimeScope(std::string name, std::string methodName, uint32_t methodOffset, CompilerLog* log);
186     TimeScope(std::string name, CompilerLog* log);
187     ~TimeScope();
188 
189 private:
190     static constexpr int PASS_LENS = 32;
191     static constexpr int METHOD_LENS = 16;
192     static constexpr int OFFSET_LENS = 8;
193     static constexpr int TIME_LENS = 8;
194     static constexpr int MILLION_TIME = 1000;
195 
196     std::string name_ {""};
197     double startTime_ {0};
198     double timeUsed_ {0};
199     std::string methodName_ {""};
200     uint32_t methodOffset_ {0};
201     CompilerLog *log_ {nullptr};
202 
203     const std::string GetShortName(const std::string& methodName);
204 };
205 
206 class PGOTypeLogList {
207 public:
PGOTypeLogList(Circuit * circuit)208     explicit PGOTypeLogList(Circuit *circuit) : acc_(circuit) {}
209     ~PGOTypeLogList() = default;
210     void CollectGateTypeLogInfo(GateRef gate, bool isBinOp);
211     void PrintPGOTypeLog();
212 private:
213     GateAccessor acc_;
214     std::string log_ {};
215 };
216 }  // namespace panda::ecmascript::kungfu
217 #endif  // ECMASCRIPT_COMPILER_LOG_H
218