• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "memory/rs_dfx_string.h"
23 #include "memory/rs_tag_tracker.h"
24 #include "platform/common/rs_log.h"
25 
26 namespace OHOS {
27 namespace Rosen {
28 
29 class RsVulkanMemStat {
30 public:
31     RsVulkanMemStat() = default;
32     ~RsVulkanMemStat() = default;
33 
InsertResource(const std::string & name,pid_t pid,const uint64_t size)34     void InsertResource(const std::string& name, pid_t pid, const uint64_t size)
35     {
36         std::lock_guard<std::mutex> lock(mMutex);
37         mResources[name] = {
38             .size = size,
39             .pid = pid,
40         };
41     }
42 
DeleteResource(const std::string & name)43     void DeleteResource(const std::string& name)
44     {
45         std::lock_guard<std::mutex> lock(mMutex);
46         auto it = mResources.find(name);
47         if (it != mResources.end()) {
48             mResources.erase(name);
49         } else {
50             ROSEN_LOGE("DeleteResource Error, name is invalid");
51         }
52     }
53 
DumpMemoryStatistics(DfxString & log)54     void DumpMemoryStatistics(DfxString& log)
55     {
56         uint64_t totalSize = 0;
57         uint64_t totalCount = 0;
58         struct DumpInfo {
59             uint64_t size = 0;
60             uint64_t count = 0;
61         };
62         std::unordered_map<pid_t, DumpInfo> pidDumpInfoMap;
63         {
64             std::lock_guard<std::mutex> lock(mMutex);
65             totalCount = mResources.size();
66             for (const auto& it : mResources) {
67                 auto& dumpInfo = pidDumpInfoMap[it.second.pid];
68                 dumpInfo.size += it.second.size;
69                 ++dumpInfo.count;
70                 totalSize += it.second.size;
71             }
72         }
73         log.AppendFormat("\n------------\nVulkan Memory Statistics: Count: %lu, Size: %lu\n", totalCount, totalSize);
74         for (const auto& pidIt : pidDumpInfoMap) {
75             log.AppendFormat("  pid: %d, count: %lu, size: %lu\n", pidIt.first, pidIt.second.count, pidIt.second.size);
76         }
77     }
78 
79 private:
80     struct MemoryInfo {
81         uint64_t size = 0;
82         pid_t pid = 0;
83     };
84     std::unordered_map<std::string, MemoryInfo> mResources;
85     std::mutex mMutex;
86 };
87 
88 }
89 }
90 
91 #endif