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