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