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 28 namespace OHOS { 29 namespace Rosen { 30 constexpr int BYTE_CONVERT = 1024; 31 enum MEMORY_TYPE { 32 MEM_PIXELMAP, 33 MEM_SKIMAGE, 34 MEM_RENDER_NODE 35 }; 36 37 struct MemoryInfo { 38 size_t size = 0; 39 int pid = 0; 40 uint64_t nid = 0; 41 MEMORY_TYPE type; 42 }; 43 44 class MemoryNodeOfPid { 45 public: 46 MemoryNodeOfPid() = default; 47 ~MemoryNodeOfPid() = default; 48 MemoryNodeOfPid(size_t size, NodeId id); 49 size_t GetMemSize(); 50 bool operator==(const MemoryNodeOfPid& other); 51 private: 52 size_t nodeSize_ = 0; 53 NodeId nodeId_ = 0; 54 }; 55 56 class RSB_EXPORT MemoryTrack { 57 public: 58 static MemoryTrack& Instance(); 59 void AddNodeRecord(const NodeId id, const MemoryInfo& info); 60 void RemoveNodeRecord(const NodeId id); 61 void DumpMemoryStatistics(DfxString& log, std::function<std::tuple<uint64_t, std::string, RectI> (uint64_t)> func); 62 void AddPictureRecord(const void* addr, MemoryInfo info); 63 void RemovePictureRecord(const void* addr); 64 void UpdatePictureInfo(const void* addr, NodeId nodeId, pid_t pid); 65 // count memory for hidumper 66 MemoryGraphic CountRSMemory(const pid_t pid); 67 private: 68 MemoryTrack() = default; 69 ~MemoryTrack() = default; 70 MemoryTrack(const MemoryTrack&) = delete; 71 MemoryTrack(const MemoryTrack&&) = delete; 72 MemoryTrack& operator=(const MemoryTrack&) = delete; 73 MemoryTrack& operator=(const MemoryTrack&&) = delete; 74 const char* MemoryType2String(MEMORY_TYPE type); 75 std::string GenerateDumpTitle(); 76 std::string GenerateDetail(MemoryInfo info, uint64_t windowId, std::string& windowName, RectI& nodeFrameRect); 77 void DumpMemoryNodeStatistics(DfxString& log); 78 void DumpMemoryPicStatistics(DfxString& log, 79 std::function<std::tuple<uint64_t, std::string, RectI> (uint64_t)> func); 80 bool RemoveNodeFromMap(const NodeId id, pid_t& pid, size_t& size); 81 void RemoveNodeOfPidFromMap(const pid_t pid, const size_t size, const NodeId id); 82 std::mutex mutex_; 83 std::unordered_map<NodeId, MemoryInfo> memNodeMap_; 84 std::unordered_map<const void*, MemoryInfo> memPicRecord_; 85 86 // Data to statistic information of Pid 87 std::unordered_map<pid_t, std::vector<MemoryNodeOfPid>> memNodeOfPidMap_; 88 }; 89 } // namespace OHOS 90 } // namespace Rosen 91 #endif