1 /* 2 * Copyright (c) 2024 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 #ifndef RS_VULKAN_MEM_STAT_H 17 #define RS_VULKAN_MEM_STAT_H 18 19 #include <mutex> 20 #include <string> 21 22 #include "image/trace_memory_dump.h" 23 #include "platform/common/rs_log.h" 24 25 namespace OHOS { 26 namespace Rosen { 27 28 inline const char* RESOURCE_CATEGORY = "Image"; 29 inline const char* RESOURCE_TYPE = "RS_VULKAN_IMG"; 30 31 class RsVulkanMemStat { 32 public: 33 RsVulkanMemStat() = default; 34 ~RsVulkanMemStat() = default; 35 InsertResource(const std::string & name,const uint64_t size)36 void InsertResource(const std::string& name, const uint64_t size) 37 { 38 std::lock_guard<std::mutex> lock(mMutex); 39 mResources[name] = { 40 .size = size, 41 }; 42 } 43 DeleteResource(const std::string & name)44 void DeleteResource(const std::string& name) 45 { 46 std::lock_guard<std::mutex> lock(mMutex); 47 auto it = mResources.find(name); 48 if (it != mResources.end()) { 49 mResources.erase(name); 50 } else { 51 ROSEN_LOGE("DeleteResource Error, name is invalid"); 52 } 53 } 54 DumpMemoryStatistics(Drawing::TraceMemoryDump * memoryDump)55 void DumpMemoryStatistics(Drawing::TraceMemoryDump *memoryDump) 56 { 57 std::lock_guard<std::mutex> lock(mMutex); 58 if (!memoryDump) { 59 return; 60 } 61 for (auto it = mResources.begin(); it != mResources.end(); it++) { 62 memoryDump->DumpNumericValue(it->first.c_str(), "size", "bytes", it->second.size); 63 memoryDump->DumpStringValue(it->first.c_str(), "type", RESOURCE_TYPE); 64 memoryDump->DumpStringValue(it->first.c_str(), "category", RESOURCE_CATEGORY); 65 } 66 } 67 68 private: 69 struct MemoryInfo { 70 uint64_t size; 71 }; 72 std::unordered_map<std::string, MemoryInfo> mResources; 73 std::mutex mMutex; 74 }; 75 76 } 77 } 78 79 #endif