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 struct MemoryInfo { 39 size_t size = 0; 40 int pid = 0; 41 uint64_t nid = 0; 42 uint64_t uid = 0; 43 MEMORY_TYPE type = MEMORY_TYPE::MEM_PIXELMAP; 44 OHOS::Media::AllocatorType allocType; 45 OHOS::Media::PixelFormat pixelMapFormat; 46 }; 47 48 class MemoryNodeOfPid { 49 public: 50 MemoryNodeOfPid() = default; 51 ~MemoryNodeOfPid() = default; 52 MemoryNodeOfPid(size_t size, NodeId id); 53 size_t GetMemSize(); 54 void SetMemSize(size_t size); 55 bool operator==(const MemoryNodeOfPid& other); 56 private: 57 size_t nodeSize_ = 0; 58 NodeId nodeId_ = 0; 59 }; 60 61 class RSB_EXPORT MemoryTrack { 62 public: 63 static MemoryTrack& Instance(); 64 void AddNodeRecord(const NodeId id, const MemoryInfo& info); 65 void RemoveNodeRecord(const NodeId id); 66 void DumpMemoryStatistics(DfxString& log, std::function<std::tuple<uint64_t, std::string, RectI> (uint64_t)> func); 67 void AddPictureRecord(const void* addr, MemoryInfo info); 68 void RemovePictureRecord(const void* addr); 69 void UpdatePictureInfo(const void* addr, NodeId nodeId, pid_t pid); 70 // count memory for hidumper 71 MemoryGraphic CountRSMemory(const pid_t pid); 72 float GetAppMemorySizeInMB(); GetMemNodeMap()73 const std::unordered_map<NodeId, MemoryInfo>& GetMemNodeMap() { return memNodeMap_; } 74 private: 75 MemoryTrack() = default; 76 ~MemoryTrack() = default; 77 MemoryTrack(const MemoryTrack&) = delete; 78 MemoryTrack(const MemoryTrack&&) = delete; 79 MemoryTrack& operator=(const MemoryTrack&) = delete; 80 MemoryTrack& operator=(const MemoryTrack&&) = delete; 81 const char* MemoryType2String(MEMORY_TYPE type); 82 const std::string PixelMapInfo2String(MemoryInfo info); 83 const std::string AllocatorType2String(OHOS::Media::AllocatorType); 84 const std::string PixelFormat2String(OHOS::Media::PixelFormat); 85 std::string GenerateDumpTitle(); 86 std::string GenerateDetail(MemoryInfo info, uint64_t windowId, std::string& windowName, RectI& nodeFrameRect); 87 void DumpMemoryNodeStatistics(DfxString& log); 88 void DumpMemoryPicStatistics(DfxString& log, 89 std::function<std::tuple<uint64_t, std::string, RectI> (uint64_t)> func, 90 const std::vector<MemoryInfo>& memPicRecord = {}); 91 bool RemoveNodeFromMap(const NodeId id, pid_t& pid, size_t& size); 92 void RemoveNodeOfPidFromMap(const pid_t pid, const size_t size, const NodeId id); 93 std::mutex mutex_; 94 std::unordered_map<NodeId, MemoryInfo> memNodeMap_; 95 std::unordered_map<const void*, MemoryInfo> memPicRecord_; 96 97 // Data to statistic information of Pid 98 std::unordered_map<pid_t, std::vector<MemoryNodeOfPid>> memNodeOfPidMap_; 99 }; 100 } // namespace OHOS 101 } // namespace Rosen 102 #endif