• 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(JSThread *thread, 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     void AsyncSave();
59     void SetDisablePGO(bool state);
60     void PushAllProfilersToPendingList();
61     bool PUBLIC_API BinaryToText(const std::string& inPath, const std::string& outPath, uint32_t hotnessThreshold);
62     static bool PUBLIC_API MergeApFiles(const std::string &inFiles, const std::string &outPath,
63                                         uint32_t hotnessThreshold, ApGenMode mode);
64     static bool PUBLIC_API MergeApFiles(std::unordered_map<CString, uint32_t> &fileNameToChecksumMap,
65                                         PGOProfilerDecoder &merger);
66     void SetIsApFileCompatible(bool isCompatible);
67     bool GetIsApFileCompatible() const;
68     size_t GetMaxAotMethodSize() const;
69     void SetMaxAotMethodSize(uint32_t value);
70     bool IsBigMethod(uint32_t methodSize) const;
71     std::shared_ptr<PGOInfo> GetPGOInfo() const;
72     bool ResetOutPathByModuleName(const std::string& moduleName);
73     bool ResetOutPath(const std::string& fileName);
74     static bool ResetOutPath(const std::string& path, std::string& realPath, const std::string& fileName);
75     const std::string& GetOutPath() const;
76     void RequestAot();
77     void PostResetOutPathTask(const std::string& moduleName);
78     bool IsInitialized() const;
79     static Mutex& GetPGOInfoMutex();
80     void TryDispatchDumpTask(PGOProfiler* profiler);
81     bool IsTaskRunning() const;
82     void SetIsTaskRunning(bool isTaskRunning);
83     void DumpPendingProfilersByDumpThread();
84     void SavePGOInfo();
85     void SetForceDump(bool forceDump);
86     bool IsForceDump() const;
87     void DispatchDumpTask();
88     bool IsProfilerDestroyed(PGOProfiler* profiler);
89     void ResetSaveTimestamp(EcmaVM* vm, bool isBackground);
90 
91 private:
92     bool InitializeData();
93 
94     ConcurrentGuardValue v_;
95     bool disablePGO_ {false};
96     Mutex requestAotCallbackMutex_;
97     RequestAotCallback requestAotCallback_;
98     std::atomic_bool enableSignalSaving_ { false };
99     Mutex profilersMutex_; // protect profilers_
100     std::set<std::shared_ptr<PGOProfiler>> profilers_;
101     bool isApFileCompatible_ {true};
102     uint32_t maxAotMethodSize_ {0};
103     std::shared_ptr<PGOInfo> pgoInfo_;
104     std::string outDir_;
105     std::string outPath_;
106     std::string bundleName_;
107     std::string moduleName_;
108     uint32_t hotnessThreshold_;
109     std::atomic_bool isInitialized_ {false};
110     std::atomic_bool hasPostModuleName_ {false};
111     Mutex resetOutPathMutex_;
112     ApGenMode apGenMode_ {ApGenMode::MERGE};
113     PGOPendingProfilers pendingProfilers_;
114     std::atomic_bool isTaskRunning_ {false};
115     std::atomic_bool forceDump_ {false};
116     Mutex dumpTaskMutex_; // protect dispatch dump task
117 };
118 
119 class ResetOutPathTask : public common::Task {
120 public:
ResetOutPathTask(std::string moduleName,int32_t id)121     ResetOutPathTask(std::string moduleName, int32_t id): common::Task(id), moduleName_(std::move(moduleName)) {};
122     virtual ~ResetOutPathTask() override = default;
123 
Run(uint32_t threadIndex)124     bool Run([[maybe_unused]] uint32_t threadIndex) override
125     {
126         ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "ResetOutPathTask::Run", "");
127         PGOProfilerManager::GetInstance()->ResetOutPathByModuleName(moduleName_);
128         return true;
129     }
130 
GetTaskType()131     common::TaskType GetTaskType() const override
132     {
133         return common::TaskType::PGO_RESET_OUT_PATH_TASK;
134     }
135 
136     NO_COPY_SEMANTIC(ResetOutPathTask);
137     NO_MOVE_SEMANTIC(ResetOutPathTask);
138 
139 private:
140     std::string moduleName_;
141 };
142 
143 class PGODumpTask : public common::Task {
144 public:
PGODumpTask(int32_t id)145     explicit PGODumpTask(int32_t id): common::Task(id) {};
146     virtual ~PGODumpTask() override = default;
147 
Run(uint32_t threadIndex)148     bool Run([[maybe_unused]] uint32_t threadIndex) override
149     {
150         ECMA_BYTRACE_NAME(HITRACE_LEVEL_COMMERCIAL, HITRACE_TAG_ARK, "PGOProfilerTask::Run", "");
151         PGOProfilerManager::GetInstance()->DumpPendingProfilersByDumpThread();
152         return true;
153     }
154 
GetTaskType()155     common::TaskType GetTaskType() const override
156     {
157         return common::TaskType::PGO_DUMP_TASK;
158     }
159 
160     NO_COPY_SEMANTIC(PGODumpTask);
161     NO_MOVE_SEMANTIC(PGODumpTask);
162 };
163 } // namespace panda::ecmascript::pgo
164 #endif  // ECMASCRIPT_PGO_PROFILER_MANAGER_H
165