• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_PGO_PROFILER_MANAGER_H
17 #define ECMASCRIPT_PGO_PROFILER_MANAGER_H
18 
19 #include <atomic>
20 #include <csignal>
21 #include <memory>
22 
23 #include "ecmascript/pgo_profiler/pgo_profiler.h"
24 #include "ecmascript/pgo_profiler/pgo_profiler_decoder.h"
25 #include "ecmascript/pgo_profiler/pgo_profiler_encoder.h"
26 #include "ecmascript/pgo_profiler/pgo_utils.h"
27 
28 namespace panda::ecmascript::pgo {
29 using ApGenMode = PGOProfilerEncoder::ApGenMode;
30 
31 class PGOProfilerManager {
32 public:
33     PGOProfilerManager() = default;
34     ~PGOProfilerManager() = default;
35     NO_COPY_SEMANTIC(PGOProfilerManager);
36     NO_MOVE_SEMANTIC(PGOProfilerManager);
37 
38     static PGOProfilerManager* PUBLIC_API GetInstance();
39     static void SavingSignalHandler(int signo);
40     void Initialize(const std::string& outDir, uint32_t hotnessThreshold);
41     void SetBundleName(const std::string& bundleName);
42     const std::string GetBundleName() const;
43     void SetRequestAotCallback(const RequestAotCallback& cb);
44     bool RequestAot(const std::string& bundleName, const std::string& moduleName, RequestAotMode triggerMode);
45     void Destroy();
46     std::shared_ptr<PGOProfiler> BuildProfiler(EcmaVM* vm, bool isEnable);
47     bool IsEnable() const;
48     void Destroy(std::shared_ptr<PGOProfiler>& profiler);
49     void Reset(const std::shared_ptr<PGOProfiler>& profiler, bool isEnable);
50     void SamplePandaFileInfo(uint32_t checksum, const CString& abcName);
51     void SetModuleName(const std::string& moduleName);
52     bool PUBLIC_API GetPandaFileId(const CString& abcName, ApEntityId& entryId) const;
53     bool GetPandaFileDesc(ApEntityId abcId, CString& desc) const;
54     void SetApGenMode(ApGenMode mode);
55     ApGenMode GetApGenMode() const;
56     void Merge(PGOProfiler* profiler);
57     void RegisterSavingSignal();
58     bool IsDisableAot() const;
59     void SetDisableAot(bool state);
60     void SetDisablePGO(bool state);
61     void PushAllProfilersToPendingList();
62     bool PUBLIC_API BinaryToText(const std::string& inPath, const std::string& outPath, uint32_t hotnessThreshold);
63     static bool PUBLIC_API MergeApFiles(const std::string &inFiles, const std::string &outPath,
64                                         uint32_t hotnessThreshold, ApGenMode mode);
65     static bool PUBLIC_API MergeApFiles(std::unordered_map<CString, uint32_t> &fileNameToChecksumMap,
66                                         PGOProfilerDecoder &merger);
67     void SetIsApFileCompatible(bool isCompatible);
68     bool GetIsApFileCompatible() const;
69     size_t GetMaxAotMethodSize() const;
70     void SetMaxAotMethodSize(uint32_t value);
71     bool IsBigMethod(uint32_t methodSize) const;
72     std::shared_ptr<PGOInfo> GetPGOInfo() const;
73     bool ResetOutPathByModuleName(const std::string& moduleName);
74     bool ResetOutPath(const std::string& fileName);
75     static bool ResetOutPath(const std::string& path, std::string& realPath, const std::string& fileName);
76     const std::string& GetOutPath() const;
77     void RequestAot();
78     void PostResetOutPathTask(const std::string& moduleName);
79     bool IsInitialized() const;
80     static Mutex& GetPGOInfoMutex();
81     void TryDispatchDumpTask(PGOProfiler* profiler);
82     bool IsTaskRunning() const;
83     void SetIsTaskRunning(bool isTaskRunning);
84     void DumpPendingProfilers();
85     void SavePGOInfo();
86     void SetForceDump(bool forceDump);
87     bool IsForceDump() const;
88     void DispatchDumpTask();
89     bool IsProfilerDestroyed(PGOProfiler* profiler);
90 
91 private:
92     bool InitializeData();
93 
94     ConcurrentGuardValue v_;
95     bool disableAot_ {false};
96     bool disablePGO_ {false};
97     Mutex requestAotCallbackMutex_;
98     RequestAotCallback requestAotCallback_;
99     std::atomic_bool enableSignalSaving_ { false };
100     Mutex profilersMutex_; // protect profilers_
101     std::set<std::shared_ptr<PGOProfiler>> profilers_;
102     bool isApFileCompatible_ {true};
103     uint32_t maxAotMethodSize_ {0};
104     std::shared_ptr<PGOInfo> pgoInfo_;
105     std::string outDir_;
106     std::string outPath_;
107     std::string bundleName_;
108     std::string moduleName_;
109     uint32_t hotnessThreshold_;
110     std::atomic_bool isInitialized_ {false};
111     std::atomic_bool hasPostModuleName_ {false};
112     Mutex resetOutPathMutex_;
113     ApGenMode apGenMode_ {ApGenMode::MERGE};
114     PGOPendingProfilers pendingProfilers_;
115     std::atomic_bool isTaskRunning_ {false};
116     std::atomic_bool forceDump_ {false};
117     Mutex dumpTaskMutex_; // protect dispatch dump task
118 };
119 
120 class ResetOutPathTask : public Task {
121 public:
ResetOutPathTask(std::string moduleName,int32_t id)122     ResetOutPathTask(std::string moduleName, int32_t id): Task(id), moduleName_(std::move(moduleName)) {};
123     virtual ~ResetOutPathTask() override = default;
124 
Run(uint32_t threadIndex)125     bool Run([[maybe_unused]] uint32_t threadIndex) override
126     {
127         ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "ResetOutPathTask::Run");
128         PGOProfilerManager::GetInstance()->ResetOutPathByModuleName(moduleName_);
129         return true;
130     }
131 
GetTaskType()132     TaskType GetTaskType() const override
133     {
134         return TaskType::PGO_RESET_OUT_PATH_TASK;
135     }
136 
137     NO_COPY_SEMANTIC(ResetOutPathTask);
138     NO_MOVE_SEMANTIC(ResetOutPathTask);
139 
140 private:
141     std::string moduleName_;
142 };
143 
144 class PGODumpTask : public Task {
145 public:
PGODumpTask(int32_t id)146     explicit PGODumpTask(int32_t id): Task(id) {};
147     virtual ~PGODumpTask() override = default;
148 
Run(uint32_t threadIndex)149     bool Run([[maybe_unused]] uint32_t threadIndex) override
150     {
151         ECMA_BYTRACE_NAME(HITRACE_TAG_ARK, "PGOProfilerTask::Run");
152         PGOProfilerManager::GetInstance()->DumpPendingProfilers();
153         return true;
154     }
155 
GetTaskType()156     TaskType GetTaskType() const override
157     {
158         return TaskType::PGO_DUMP_TASK;
159     }
160 
161     NO_COPY_SEMANTIC(PGODumpTask);
162     NO_MOVE_SEMANTIC(PGODumpTask);
163 };
164 } // namespace panda::ecmascript::pgo
165 #endif  // ECMASCRIPT_PGO_PROFILER_MANAGER_H
166