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