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