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 32 namespace panda::ecmascript::kungfu { 33 class AotMethodLogList; 34 35 class CompilerLog { 36 public: 37 explicit CompilerLog(const std::string &logOpt, bool TraceBC = false); 38 ~CompilerLog() = default; 39 AllMethod()40 bool AllMethod() const 41 { 42 return allMethod_; 43 } 44 CertainMethod()45 bool CertainMethod() const 46 { 47 return cerMethod_; 48 } 49 NoneMethod()50 bool NoneMethod() const 51 { 52 return noneMethod_; 53 } 54 OutputCIR()55 bool OutputCIR() const 56 { 57 return outputCIR_; 58 } 59 OutputLLIR()60 bool OutputLLIR() const 61 { 62 return outputLLIR_; 63 } 64 OutputASM()65 bool OutputASM() const 66 { 67 return outputASM_; 68 } 69 OutputType()70 bool OutputType() const 71 { 72 return outputType_; 73 } 74 IsTraceBC()75 bool IsTraceBC() const 76 { 77 return traceBc_; 78 } 79 GetEnableCompilerLogTime()80 bool GetEnableCompilerLogTime() const 81 { 82 return compilerLogTime_; 83 } 84 SetEnableCompilerLogTime(bool compilerLogTime)85 void SetEnableCompilerLogTime(bool compilerLogTime) 86 { 87 compilerLogTime_ = compilerLogTime; 88 } 89 GetEnableMethodLog()90 bool GetEnableMethodLog() const 91 { 92 return enableMethodLog_; 93 } 94 SetEnableMethodLog(bool enableMethodLog)95 void SetEnableMethodLog(bool enableMethodLog) 96 { 97 enableMethodLog_ = enableMethodLog; 98 } 99 EnableMethodCIRLog()100 bool EnableMethodCIRLog() const 101 { 102 return GetEnableMethodLog() && OutputCIR(); 103 } 104 105 void SetMethodLog(const std::string &fileName, const CString& recordName, 106 const std::string &methodName, AotMethodLogList *logList); 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 112 std::map<std::string, int> nameIndex_; 113 114 private: 115 static constexpr int RECORD_LENS = 64; 116 static constexpr int PASS_LENS = 32; 117 static constexpr int METHOD_LENS = 16; 118 static constexpr int OFFSET_LENS = 8; 119 static constexpr int PERCENT_LENS = 4; 120 static constexpr int TIME_LENS = 8; 121 static constexpr int MILLION_TIME = 1000; 122 static constexpr int HUNDRED_TIME = 100; 123 124 void PrintPassTime() const; 125 void PrintMethodTime() const; 126 void PrintTime() const; 127 void PrintCompiledMethod() const; 128 void AddCompiledMethod(const std::string& name, const CString& recordName); 129 130 int idx_ {0}; 131 bool allMethod_ {false}; 132 bool cerMethod_ {false}; 133 bool noneMethod_ {false}; 134 bool outputCIR_ {false}; 135 bool outputLLIR_ {false}; 136 bool outputASM_ {false}; 137 bool outputType_ {false}; 138 bool traceBc_ {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 std::set<std::pair<std::string, CString>> compiledMethodSet_ {}; 144 }; 145 146 class MethodLogList { 147 public: MethodLogList(const std::string & logMethods)148 explicit MethodLogList(const std::string &logMethods) : methods_(logMethods) {} 149 ~MethodLogList() = default; 150 bool IncludesMethod(const std::string &methodName) const; 151 private: 152 std::string methods_ {}; 153 }; 154 155 class AotMethodLogList : public MethodLogList { 156 public: 157 static const char fileSplitSign = ':'; 158 static const char methodSplitSign = ','; 159 AotMethodLogList(const std::string & logMethods)160 explicit AotMethodLogList(const std::string &logMethods) : MethodLogList(logMethods) 161 { 162 ParseFileMethodsName(logMethods); 163 } 164 ~AotMethodLogList() = default; 165 166 bool IncludesMethod(const std::string &fileName, const std::string &methodName) const; 167 168 private: 169 std::vector<std::string> spiltString(const std::string &str, const char ch); 170 void ParseFileMethodsName(const std::string &logMethods); 171 std::map<std::string, std::vector<std::string>> fileMethods_ {}; 172 }; 173 174 class TimeScope : public ClockScope { 175 public: 176 TimeScope(std::string name, std::string methodName, uint32_t methodOffset, CompilerLog* log); 177 TimeScope(std::string name, CompilerLog* log); 178 ~TimeScope(); 179 180 private: 181 static constexpr int PASS_LENS = 32; 182 static constexpr int METHOD_LENS = 16; 183 static constexpr int OFFSET_LENS = 8; 184 static constexpr int TIME_LENS = 8; 185 static constexpr int MILLION_TIME = 1000; 186 187 std::string name_ {""}; 188 double startTime_ {0}; 189 double timeUsed_ {0}; 190 std::string methodName_ {""}; 191 uint32_t methodOffset_ {0}; 192 CompilerLog *log_ {nullptr}; 193 194 const std::string GetShortName(const std::string& methodName); 195 }; 196 } // namespace panda::ecmascript::kungfu 197 #endif // ECMASCRIPT_COMPILER_LOG_H 198