1 /*
2 * Copyright (c) 2025 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 "src/gpu/ganesh/vk/GrVulkanTracker.h"
17
18 #include "include/gpu/vk/GrVulkanTrackerInterface.h"
19
20 #include <deque>
21 #include <parameters.h>
22
23 static thread_local ParallelDebug::VkImageInvokeRecord g_caller;
24 static thread_local std::deque<ParallelDebug::VkImageDestroyRecord> g_delete;
25 static constexpr size_t DESTROY_RECORD_CAPACITY = 100;
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 g_caller.nodeId_;
44 }
45
RecordNodeId(uint64_t nodeId)46 void ParallelDebug::RecordNodeId(uint64_t nodeId)
47 {
48 g_caller.nodeId_ = nodeId;
49 }
50
GenerateVkImageInvokeRecord()51 ParallelDebug::VkImageInvokeRecord* ParallelDebug::GenerateVkImageInvokeRecord()
52 {
53 auto caller = new ParallelDebug::VkImageInvokeRecord();
54 caller->nodeId_ = g_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 memory)68 void ParallelDebug::VkImageDestroyRecord::Record(VkImage image,
69 bool borrow,
70 const ParallelDebug::VkImageInvokeRecord* call,
71 VkDeviceMemory memory)
72 {
73 if (call == nullptr) {
74 return;
75 }
76 g_delete.push_back({image, borrow, *call, memory, GetNanoSecords()});
77 if (g_delete.size() > DESTROY_RECORD_CAPACITY) {
78 g_delete.pop_front();
79 }
80 }
81
DumpAllDestroyVkImage(std::stringstream & ss)82 void ParallelDebug::DumpAllDestroyVkImage(std::stringstream &ss)
83 {
84 for (auto& del : g_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