• 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 #include "include/gpu/vk/GrVulkanTracker.h"
17 #include "include/gpu/vk/GrVulkanTrackerInterface.h"
18 #include "src/gpu/vk/GrVkImage.h"
19 #include <deque>
20 #include <parameters.h>
21 
22 static thread_local ParallelDebug::VkImageInvokeRecord CALLER_;
23 static thread_local std::deque<ParallelDebug::VkImageDestroyRecord> DELETE_;
24 static constexpr size_t DESTROY_RECORD_CAPACITY = 100;
25 static std::atomic<int64_t> count;
26 
GetNanoSecords()27 static inline int64_t GetNanoSecords()
28 {
29     struct timespec ts {};
30     clock_gettime(CLOCK_REALTIME, &ts);
31     return ts.tv_sec * 100000000LL + ts.tv_nsec;
32 }
33 
IsVkImageDfxEnabled()34 bool ParallelDebug::IsVkImageDfxEnabled()
35 {
36     static const bool dfxEnabled =
37         std::atoi(OHOS::system::GetParameter("persist.sys.graphic.openVkImageMemoryDfx", "0").c_str()) != 0;
38     return dfxEnabled;
39 }
40 
GetNodeId()41 uint64_t ParallelDebug::GetNodeId()
42 {
43     return CALLER_.nodeId_;
44 }
45 
RecordNodeId(uint64_t nodeId)46 void ParallelDebug::RecordNodeId(uint64_t nodeId)
47 {
48     CALLER_.nodeId_ = nodeId;
49 }
50 
GenerateVkImageInvokeRecord()51 ParallelDebug::VkImageInvokeRecord* ParallelDebug::GenerateVkImageInvokeRecord()
52 {
53     auto caller = new ParallelDebug::VkImageInvokeRecord();
54     caller->nodeId_ = CALLER_.nodeId_;
55     return caller;
56 }
57 
DestroyVkImageInvokeRecord(ParallelDebug::VkImageInvokeRecord * vkImageInvokeRecord)58 void ParallelDebug::DestroyVkImageInvokeRecord(ParallelDebug::VkImageInvokeRecord* vkImageInvokeRecord)
59 {
60     delete vkImageInvokeRecord;
61 }
62 
Dump(std::stringstream & ss)63 void ParallelDebug::VkImageInvokeRecord::Dump(std::stringstream& ss)
64 {
65     ss << (nodeId_ ? ", nodeId: " + std::to_string(nodeId_) : "");
66 }
67 
Record(VkImage image,bool borrow,const ParallelDebug::VkImageInvokeRecord * call,VkDeviceMemory fMemory)68 void ParallelDebug::VkImageDestroyRecord::Record(VkImage image,
69                                                 bool borrow,
70                                                 const ParallelDebug::VkImageInvokeRecord* call,
71                                                 VkDeviceMemory fMemory)
72 {
73     if (call == nullptr) {
74         return;
75     }
76     DELETE_.push_back({image, borrow, *call, fMemory, GetNanoSecords()});
77     if (DELETE_.size() > DESTROY_RECORD_CAPACITY) {
78         DELETE_.pop_front();
79     }
80 }
81 
DumpAllDestroyVkImage(std::stringstream & ss)82 void ParallelDebug::DumpAllDestroyVkImage(std::stringstream &ss)
83 {
84     for (auto& del : DELETE_) {
85         del.Dump(ss);
86         ss << "\n";
87     }
88 }
89 
Dump(std::stringstream & ss)90 void ParallelDebug::VkImageDestroyRecord::Dump(std::stringstream& ss)
91 {
92     char timeStr[20];
93     struct tm timeInfo;
94     time_t seconds = timeStamp / 1000000000LL;
95     localtime_r(&seconds, &timeInfo);
96     std::strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", &timeInfo);
97     ss << timeStr << "VkImage: " << image_ << ", " << borrowed_ << ", " << memory_ << " ";
98     caller_.Dump(ss);
99 }
100 
101 namespace RealAllocConfig {
102 // OH ISSUE: isRealAlloc indicates whether the Vulkan memory(external and proxy)
103 // in the current thread context should be calculated.
104 static thread_local bool isRealAlloc = false;
105 
GetRealAllocStatus()106 bool GetRealAllocStatus()
107 {
108     return isRealAlloc;
109 }
110 
SetRealAllocStatus(bool ret)111 void SetRealAllocStatus(bool ret)
112 {
113     isRealAlloc = ret;
114 }
115 }