14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MODULE_MODULE_LOGGER_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MODULE_MODULE_LOGGER_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/module/js_module_source_text.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_ci#include <fstream>
224514f5e3Sopenharmony_cinamespace panda::ecmascript {
234514f5e3Sopenharmony_ci
244514f5e3Sopenharmony_cistruct ModuleLoadInfo {
254514f5e3Sopenharmony_ci    bool isUsed {false};
264514f5e3Sopenharmony_ci    CSet<CString> parentModules {};
274514f5e3Sopenharmony_ci    CMap<CString, CSet<CString>> upLevel {}; // parent module and importName
284514f5e3Sopenharmony_ci    double time {0};
294514f5e3Sopenharmony_ci};
304514f5e3Sopenharmony_ci
314514f5e3Sopenharmony_ciclass ModuleLogger {
324514f5e3Sopenharmony_cipublic:
334514f5e3Sopenharmony_ci    explicit ModuleLogger(EcmaVM *vm);
344514f5e3Sopenharmony_ci    ~ModuleLogger()
354514f5e3Sopenharmony_ci    {
364514f5e3Sopenharmony_ci        for (auto &info : jsModuleLoadInfo_) {
374514f5e3Sopenharmony_ci            delete info.second;
384514f5e3Sopenharmony_ci        }
394514f5e3Sopenharmony_ci        jsModuleLoadInfo_.clear();
404514f5e3Sopenharmony_ci    }
414514f5e3Sopenharmony_ci    void SetStartTime(const CString &recordName);
424514f5e3Sopenharmony_ci    void SetEndTime(const CString &recordName);
434514f5e3Sopenharmony_ci    void PostModuleLoggerTask(int32_t id, EcmaVM *vm);
444514f5e3Sopenharmony_ci    void InsertModuleLoadInfo(JSHandle<SourceTextModule> currentModule,
454514f5e3Sopenharmony_ci                              JSHandle<SourceTextModule> exportModule,
464514f5e3Sopenharmony_ci                              int32_t index);
474514f5e3Sopenharmony_ci    void InsertParentModule(JSHandle<SourceTextModule> currentModule, JSHandle<SourceTextModule> requiredModule);
484514f5e3Sopenharmony_ci    void InsertEntryPointModule(JSHandle<SourceTextModule> currentModule);
494514f5e3Sopenharmony_ci    void PrintModuleLoadInfo();
504514f5e3Sopenharmony_ci    static std::string ToStringWithPrecision(const double num, const uint8_t n);
514514f5e3Sopenharmony_ci
524514f5e3Sopenharmony_ciprivate:
534514f5e3Sopenharmony_ci    static constexpr const int MILLISECONDS_PER_SEC = 1000;
544514f5e3Sopenharmony_ci    static constexpr const double DOUBLE_MILLISECONDS_PER_SEC = 1000.0;
554514f5e3Sopenharmony_ci    static constexpr const int TWO = 2;
564514f5e3Sopenharmony_ci    static constexpr const int THREE = 3;
574514f5e3Sopenharmony_ci    static constexpr const double TWO_SECONDS = TWO * MILLISECONDS_PER_SEC * MILLISECONDS_PER_SEC;
584514f5e3Sopenharmony_ci    static constexpr const char FILEDIR[] = "/data/storage/el2/base/files/";
594514f5e3Sopenharmony_ci    static constexpr const char SUFFIX[] = "_redundant_file.txt";
604514f5e3Sopenharmony_ci
614514f5e3Sopenharmony_ci    NO_COPY_SEMANTIC(ModuleLogger);
624514f5e3Sopenharmony_ci    NO_MOVE_SEMANTIC(ModuleLogger);
634514f5e3Sopenharmony_ci
644514f5e3Sopenharmony_ci    bool CreateResultFile(std::string &path) const; // first time
654514f5e3Sopenharmony_ci    bool OpenResultFile(std::string &path) const;
664514f5e3Sopenharmony_ci    ModuleLoadInfo *GetModuleLoadInfo(const CString &recordName);
674514f5e3Sopenharmony_ci
684514f5e3Sopenharmony_ci    void PrintSummary() const;
694514f5e3Sopenharmony_ci    void PrintUsedFileInfo() const;
704514f5e3Sopenharmony_ci    void PrintUnusedFileInfo() const;
714514f5e3Sopenharmony_ci    void ProcessModuleExecuteTime();
724514f5e3Sopenharmony_ci    EcmaVM *vm_ {nullptr};
734514f5e3Sopenharmony_ci    CUnorderedMap<CString, ModuleLoadInfo*> jsModuleLoadInfo_ {};
744514f5e3Sopenharmony_ci    uint32_t totalFileNumber_ {0};
754514f5e3Sopenharmony_ci    uint32_t unusedFileNumber_ {0};
764514f5e3Sopenharmony_ci    uint32_t usedFileNumber_ {0};
774514f5e3Sopenharmony_ci    int64_t totalTime_ {0};
784514f5e3Sopenharmony_ci    int64_t usedFileTime_ {0};
794514f5e3Sopenharmony_ci    int64_t unusedFileTime_ {0};
804514f5e3Sopenharmony_ci    Mutex mutex_;
814514f5e3Sopenharmony_ci
824514f5e3Sopenharmony_ci    class ModuleLoggerTask : public Task {
834514f5e3Sopenharmony_ci    public:
844514f5e3Sopenharmony_ci        ModuleLoggerTask(int32_t id, EcmaVM *vm)
854514f5e3Sopenharmony_ci            : Task(id), vm_(vm) {}
864514f5e3Sopenharmony_ci        ~ModuleLoggerTask() override = default;
874514f5e3Sopenharmony_ci        bool Run([[maybe_unused]]uint32_t threadIndex) override;
884514f5e3Sopenharmony_ci
894514f5e3Sopenharmony_ci        NO_COPY_SEMANTIC(ModuleLoggerTask);
904514f5e3Sopenharmony_ci        NO_MOVE_SEMANTIC(ModuleLoggerTask);
914514f5e3Sopenharmony_ci    private:
924514f5e3Sopenharmony_ci        EcmaVM *vm_ {nullptr};
934514f5e3Sopenharmony_ci    };
944514f5e3Sopenharmony_ci};
954514f5e3Sopenharmony_ci} // namespace panda::ecmascript
964514f5e3Sopenharmony_ci#endif // ECMASCRIPT_MODULE_MODULE_LOGGER_H
97