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