1 /* 2 * Copyright (c) 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_MODULE_MODULE_LOGGER_H 17 #define ECMASCRIPT_MODULE_MODULE_LOGGER_H 18 19 #include "ecmascript/module/js_module_source_text.h" 20 21 #include <fstream> 22 namespace panda::ecmascript { 23 24 struct ModuleLoadInfo { 25 bool isUsed {false}; 26 CSet<CString> parentModules {}; 27 CMap<CString, CSet<CString>> upLevel {}; // parent module and importName 28 double time {0}; 29 }; 30 31 class ModuleLogger { 32 public: 33 explicit ModuleLogger(EcmaVM *vm); ~ModuleLogger()34 ~ModuleLogger() 35 { 36 for (auto &info : jsModuleLoadInfo_) { 37 delete info.second; 38 } 39 jsModuleLoadInfo_.clear(); 40 } 41 void SetStartTime(const CString &recordName); 42 void SetEndTime(const CString &recordName); 43 static void SetModuleLoggerTask(EcmaVM *vm); 44 static void PrintModuleLoadInfoTask(void *data); 45 void InsertModuleLoadInfo(JSHandle<SourceTextModule> currentModule, 46 JSHandle<SourceTextModule> exportModule, 47 int32_t index); 48 void InsertParentModule(JSHandle<SourceTextModule> currentModule, JSHandle<SourceTextModule> requiredModule); 49 void InsertEntryPointModule(JSHandle<SourceTextModule> currentModule); 50 void PrintModuleLoadInfo(); 51 static std::string ToStringWithPrecision(const double num, const uint8_t n); 52 53 private: 54 static constexpr uint32_t MILLISECONDS_PER_SEC = 1000; 55 static constexpr double DOUBLE_MILLISECONDS_PER_SEC = 1000.0; 56 static constexpr uint8_t TWO = 2; 57 static constexpr uint8_t THREE = 3; 58 static constexpr uint64_t TWO_SECONDS = TWO * MILLISECONDS_PER_SEC; 59 static constexpr std::string_view FILEDIR = "/data/storage/el2/base/files/"; 60 static constexpr std::string_view SUFFIX = "_redundant_file.txt"; 61 62 NO_COPY_SEMANTIC(ModuleLogger); 63 NO_MOVE_SEMANTIC(ModuleLogger); 64 65 bool CreateResultFile(std::string &path) const; // first time 66 bool OpenResultFile(std::string &path) const; 67 ModuleLoadInfo *GetModuleLoadInfo(const CString &recordName); 68 69 void PrintSummary() const; 70 void PrintUsedFileInfo() const; 71 void PrintUnusedFileInfo() const; 72 void ProcessModuleExecuteTime(); 73 EcmaVM *vm_ {nullptr}; 74 uint32_t tid_ {0}; 75 CUnorderedMap<CString, ModuleLoadInfo*> jsModuleLoadInfo_ {}; 76 CVector<std::pair<CString, ModuleLoadInfo*>> jsModuleLoadInfoRes_; 77 uint32_t totalFileNumber_ {0}; 78 uint32_t unusedFileNumber_ {0}; 79 uint32_t usedFileNumber_ {0}; 80 int64_t totalTime_ {0}; 81 int64_t usedFileTime_ {0}; 82 int64_t unusedFileTime_ {0}; 83 Mutex mutex_; 84 }; 85 } // namespace panda::ecmascript 86 #endif // ECMASCRIPT_MODULE_MODULE_LOGGER_H 87