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 #ifndef RS_MEMORY_MANAGER_H
16 #define RS_MEMORY_MANAGER_H
17 #include <vector>
18 
19 #include "image/gpu_context.h"
20 
21 #include "memory/rs_dfx_string.h"
22 #include "memory/rs_memory_graphic.h"
23 #include "memory/rs_memory_snapshot.h"
24 #include "memory/rs_memory_track.h"
25 #include "pipeline/rs_surface_render_node.h"
26 
27 namespace OHOS::Rosen {
28 
29 class MemoryManager {
30 public:
31     static void DumpMemoryUsage(DfxString& log, std::string& type);
32     static void DumpPidMemory(DfxString& log, int pid, const Drawing::GPUContext* gpuContext);
33     static void DumpDrawingGpuMemory(DfxString& log, const Drawing::GPUContext* grContext,
34         std::vector<std::pair<NodeId, std::string>>& nodeTags);
35     // Count memory for hidumper
36     static MemoryGraphic CountPidMemory(int pid, const Drawing::GPUContext* gpuContext);
37     static void CountMemory(std::vector<pid_t> pids,
38         const Drawing::GPUContext* gpuContext, std::vector<MemoryGraphic>& mems);
39     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, NodeId surfaceNodeId);
40     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, pid_t pid);
41     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, Drawing::GPUResourceTag& tag);
42     static void ReleaseUnlockGpuResource(Drawing::GPUContext* gpuContext, std::set<pid_t> exitedPidSet);
43     static void PurgeCacheBetweenFrames(Drawing::GPUContext* gpuContext, bool scratchResourceOnly,
44         std::set<pid_t>& exitedPidSet, std::set<pid_t>& protectedPidSet);
45     static void ReleaseAllGpuResource(Drawing::GPUContext* gpuContext, pid_t pid);
46     static void ReleaseAllGpuResource(Drawing::GPUContext* gpuContext, Drawing::GPUResourceTag& tag);
47     static void ReleaseUnlockGpuResource(Drawing::GPUContext* grContext, bool scratchResourcesOnly = true);
48     static void ReleaseUnlockAndSafeCacheGpuResource(Drawing::GPUContext* grContext);
49     static float GetAppGpuMemoryInMB(Drawing::GPUContext* gpuContext);
50     static void InitMemoryLimit();
51     static void SetGpuMemoryLimit(Drawing::GPUContext* gpuContext);
52     static void MemoryOverCheck(Drawing::GPUContext* gpuContext);
53     static void MemoryOverflow(pid_t pid, size_t overflowMemory, bool isGpu);
54     static void VmaDefragment(Drawing::GPUContext* gpuContext);
55 
56     static void SetGpuCacheSuppressWindowSwitch(Drawing::GPUContext* gpuContext, bool enabled);
57     static void SetGpuMemoryAsyncReclaimerSwitch(
58         Drawing::GPUContext* gpuContext, bool enabled, const std::function<void()>& setThreadPriority);
59     static void FlushGpuMemoryInWaitQueue(Drawing::GPUContext* gpuContext);
60     static void SuppressGpuCacheBelowCertainRatio(
61         Drawing::GPUContext* gpuContext, const std::function<bool(void)>& nextFrameHasArrived);
62 private:
63     // rs memory = rs + skia cpu + skia gpu
64     static void DumpRenderServiceMemory(DfxString& log);
65     static void DumpDrawingCpuMemory(DfxString& log);
66     static void DumpGpuCache(DfxString& log, const Drawing::GPUContext* gpuContext,
67         Drawing::GPUResourceTag* tag, std::string& name);
68     static void DumpAllGpuInfo(DfxString& log, const Drawing::GPUContext* grContext,
69         std::vector<std::pair<NodeId, std::string>>& nodeTags);
70     //jemalloc info
71     static void DumpGpuStats(DfxString& log, const Drawing::GPUContext* gpuContext);
72     static void DumpMallocStat(std::string& log);
73     static void DumpMemorySnapshot(DfxString& log);
74     static void MemoryOverReport(const pid_t pid, const MemorySnapshotInfo& info, const std::string& bundleName,
75         const std::string& reportName);
76     static void TotalMemoryOverReport(const std::unordered_map<pid_t, MemorySnapshotInfo>& infoMap);
77     static void ErasePidInfo(const std::set<pid_t>& exitedPidSet);
78 
79     static std::mutex mutex_;
80     static std::unordered_map<pid_t, std::pair<std::string, uint64_t>> pidInfo_;
81     static uint32_t frameCount_;
82     static uint64_t memoryWarning_;
83     static uint64_t gpuMemoryControl_;
84     static uint64_t totalMemoryReportTime_;
85 };
86 } // namespace OHOS::Rosen
87 #endif