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 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 103 void SetMethodLog(const std::string &fileName, const std::string &methodName, AotMethodLogList *logList); 104 void SetStubLog(const std::string &stubName, MethodLogList *logList); 105 void AddCompiledMethod(const std::string& name, const CString& recordName); 106 void RemoveCompiledMethod(const std::string& name, const CString& recordName); 107 void Print() const; 108 void AddMethodTime(const std::string& name, uint32_t id, double time); 109 void AddPassTime(const std::string& name, double time); 110 int GetIndex(); 111 void SetPGOMismatchResult(uint32_t &totalMethodCount, uint32_t &mismatchMethodCount, 112 std::set<std::pair<std::string, CString>> &mismatchMethodSet); 113 114 std::map<std::string, int> nameIndex_; 115 116 private: 117 static constexpr int RECORD_LENS = 64; 118 static constexpr int PASS_LENS = 32; 119 static constexpr int METHOD_LENS = 16; 120 static constexpr int OFFSET_LENS = 8; 121 static constexpr int PERCENT_LENS = 4; 122 static constexpr int TIME_LENS = 8; 123 static constexpr int MILLION_TIME = 1000; 124 static constexpr int HUNDRED_TIME = 100; 125 126 void PrintPassTime() const; 127 void PrintMethodTime() const; 128 void PrintTime() const; 129 void PrintCompiledMethod() const; 130 void PrintPGOMismatchedMethod() const; 131 132 int idx_ {0}; 133 bool allMethod_ {false}; 134 bool cerMethod_ {false}; 135 bool noneMethod_ {false}; 136 bool outputCIR_ {false}; 137 bool outputLLIR_ {false}; 138 bool outputASM_ {false}; 139 bool outputType_ {false}; 140 bool compilerLogTime_ {true}; 141 bool enableMethodLog_ {false}; 142 std::map<std::string, double> timePassMap_ {}; 143 std::map<std::pair<uint32_t, std::string>, double> timeMethodMap_ {}; 144 std::set<std::pair<std::string, CString>> compiledMethodSet_ {}; 145 uint32_t totalPGOMethodCount_ {0}; 146 uint32_t mismatchPGOMethodCount_ {0}; 147 std::set<std::pair<std::string, CString>> mismatchPGOMethodSet_ {}; 148 }; 149 150 class MethodLogList { 151 public: MethodLogList(const std::string & logMethods)152 explicit MethodLogList(const std::string &logMethods) : methods_(logMethods) {} 153 ~MethodLogList() = default; 154 bool IncludesMethod(const std::string &methodName) const; 155 private: 156 std::string methods_ {}; 157 }; 158 159 class AotMethodLogList : public MethodLogList { 160 public: 161 static const char fileSplitSign = ':'; 162 static const char methodSplitSign = ','; 163 AotMethodLogList(const std::string & logMethods)164 explicit AotMethodLogList(const std::string &logMethods) : MethodLogList(logMethods) 165 { 166 ParseFileMethodsName(logMethods); 167 } 168 ~AotMethodLogList() = default; 169 170 bool IncludesMethod(const std::string &fileName, const std::string &methodName) const; 171 172 private: 173 std::vector<std::string> spiltString(const std::string &str, const char ch); 174 void ParseFileMethodsName(const std::string &logMethods); 175 std::map<std::string, std::vector<std::string>> fileMethods_ {}; 176 }; 177 178 class TimeScope : public ClockScope { 179 public: 180 TimeScope(std::string name, std::string methodName, uint32_t methodOffset, CompilerLog* log); 181 TimeScope(std::string name, CompilerLog* log); 182 ~TimeScope(); 183 184 private: 185 static constexpr int PASS_LENS = 32; 186 static constexpr int METHOD_LENS = 16; 187 static constexpr int OFFSET_LENS = 8; 188 static constexpr int TIME_LENS = 8; 189 static constexpr int MILLION_TIME = 1000; 190 191 std::string name_ {""}; 192 double startTime_ {0}; 193 double timeUsed_ {0}; 194 std::string methodName_ {""}; 195 uint32_t methodOffset_ {0}; 196 CompilerLog *log_ {nullptr}; 197 198 const std::string GetShortName(const std::string& methodName); 199 }; 200 201 class PGOTypeLogList { 202 public: PGOTypeLogList(Circuit * circuit)203 explicit PGOTypeLogList(Circuit *circuit) : acc_(circuit) {} 204 ~PGOTypeLogList() = default; 205 void CollectGateTypeLogInfo(GateRef gate, bool isBinOp); 206 void PrintPGOTypeLog(); 207 private: 208 GateAccessor acc_; 209 std::string log_ {}; 210 }; 211 } // namespace panda::ecmascript::kungfu 212 #endif // ECMASCRIPT_COMPILER_LOG_H 213