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 MEMORY_TRACK 16 #define MEMORY_TRACK 17 18 #include <mutex> 19 #include <vector> 20 21 #include "include/core/SkImage.h" 22 23 #include "common/rs_common_def.h" 24 #include "common/rs_rect.h" 25 #include "memory/rs_dfx_string.h" 26 #include "memory/rs_memory_graphic.h" 27 #include "pixel_map.h" 28 29 namespace OHOS { 30 namespace Rosen { 31 constexpr int BYTE_CONVERT = 1024; 32 enum MEMORY_TYPE { 33 MEM_PIXELMAP, 34 MEM_SKIMAGE, 35 MEM_RENDER_NODE 36 }; 37 38 #ifdef RS_MEMORY_INFO_MANAGER 39 enum NODE_ON_TREE_STATUS { 40 STATUS_INVALID, 41 STATUS_ON_TREE, 42 STATUS_ON_TREE_IN_ROOT, 43 STATUS_OFF_TREE_IN_ROOT, 44 STATUS_OFF_TREE, 45 }; 46 #endif 47 48 struct MemoryInfo { 49 size_t size = 0; 50 int pid = 0; 51 uint64_t nid = 0; 52 uint64_t uid = 0; 53 MEMORY_TYPE type = MEMORY_TYPE::MEM_PIXELMAP; 54 OHOS::Media::AllocatorType allocType; 55 OHOS::Media::PixelFormat pixelMapFormat; 56 #ifdef RS_MEMORY_INFO_MANAGER 57 bool rootNodeStatusChangeFlag = false; 58 bool isOnTree = true; 59 #endif 60 }; 61 62 class MemoryNodeOfPid { 63 public: 64 MemoryNodeOfPid() = default; 65 ~MemoryNodeOfPid() = default; 66 MemoryNodeOfPid(size_t size, NodeId id); 67 size_t GetMemSize(); 68 void SetMemSize(size_t size); 69 bool operator==(const MemoryNodeOfPid& other); 70 private: 71 size_t nodeSize_ = 0; 72 NodeId nodeId_ = 0; 73 }; 74 75 class RSB_EXPORT MemoryTrack { 76 public: 77 static MemoryTrack& Instance(); 78 void AddNodeRecord(const NodeId id, const MemoryInfo& info); 79 void RemoveNodeRecord(const NodeId id); 80 void DumpMemoryStatistics(DfxString& log, 81 std::function<std::tuple<uint64_t, std::string, RectI, bool> (uint64_t)> func); 82 void AddPictureRecord(const void* addr, MemoryInfo info); 83 void RemovePictureRecord(const void* addr); 84 void UpdatePictureInfo(const void* addr, NodeId nodeId, pid_t pid); 85 // count memory for hidumper 86 MemoryGraphic CountRSMemory(const pid_t pid); 87 float GetAppMemorySizeInMB(); GetMemNodeMap()88 const std::unordered_map<NodeId, MemoryInfo>& GetMemNodeMap() { return memNodeMap_; } 89 #ifdef RS_MEMORY_INFO_MANAGER 90 void SetGlobalRootNodeStatusChangeFlag(bool flag); 91 bool GetGlobalRootNodeStatusChangeFlag(); 92 NODE_ON_TREE_STATUS GetNodeOnTreeStatus(const void* addr); 93 void SetNodeOnTreeStatus(NodeId nodeId, bool rootNodeStatusChangeFlag, bool isOnTree); 94 #endif 95 private: 96 MemoryTrack() = default; 97 ~MemoryTrack() = default; 98 MemoryTrack(const MemoryTrack&) = delete; 99 MemoryTrack(const MemoryTrack&&) = delete; 100 MemoryTrack& operator=(const MemoryTrack&) = delete; 101 MemoryTrack& operator=(const MemoryTrack&&) = delete; 102 const char* MemoryType2String(MEMORY_TYPE type); 103 const std::string PixelMapInfo2String(MemoryInfo info); 104 const std::string AllocatorType2String(OHOS::Media::AllocatorType); 105 const std::string PixelFormat2String(OHOS::Media::PixelFormat); 106 std::string GenerateDumpTitle(); 107 std::string GenerateDetail(MemoryInfo info, uint64_t windowId, std::string& windowName, RectI& nodeFrameRect); 108 void DumpMemoryNodeStatistics(DfxString& log); 109 void DumpMemoryPicStatistics(DfxString& log, 110 std::function<std::tuple<uint64_t, std::string, RectI, bool> (uint64_t)> func, 111 const std::vector<MemoryInfo>& memPicRecord = {}); 112 bool RemoveNodeFromMap(const NodeId id, pid_t& pid, size_t& size); 113 void RemoveNodeOfPidFromMap(const pid_t pid, const size_t size, const NodeId id); 114 std::mutex mutex_; 115 std::unordered_map<NodeId, MemoryInfo> memNodeMap_; 116 std::unordered_map<const void*, MemoryInfo> memPicRecord_; 117 118 // Data to statistic information of Pid 119 std::unordered_map<pid_t, std::vector<MemoryNodeOfPid>> memNodeOfPidMap_; 120 121 #ifdef RS_MEMORY_INFO_MANAGER 122 std::atomic<bool> globalRootNodeStatusChangeFlag{false}; 123 #endif 124 }; 125 } // namespace OHOS 126 } // namespace Rosen 127 #endif