Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #include "dump_memory.h"
12 : #include "runtime/mem.h"
13 : #include "log/hdc_log.h"
14 : #include "adump_pub.h"
15 :
16 : namespace Adx {
17 : namespace {
18 : constexpr uint16_t ADUMP_MODULE_NAME_U16 = static_cast<uint16_t>(IDEDD);
19 : } // namespace
20 :
21 3 : int32_t DumpMemory::CheckDeviceMemory(int32_t deviceId, const void* addr)
22 : {
23 3 : uint64_t* deviceAddr[1] = {static_cast<uint64_t*>(const_cast<void*>(addr))};
24 3 : rtMemInfo_t memInfo{};
25 3 : memInfo.addrInfo.addr = static_cast<uint64_t**>(deviceAddr);
26 3 : memInfo.addrInfo.cnt = 1;
27 3 : memInfo.addrInfo.memType = RT_MEM_MASK_DEV_TYPE | RT_MEM_MASK_RSVD_TYPE;
28 3 : memInfo.addrInfo.flag = true;
29 3 : rtError_t ret = rtMemGetInfoByType(deviceId, RT_MEM_INFO_TYPE_ADDR_CHECK, &memInfo);
30 3 : if ((ret != RT_ERROR_NONE) || (memInfo.addrInfo.flag == false)) {
31 0 : IDE_LOGE(
32 : "Address[%p] is the valid address of device: %d, ret: %d, check flag: %u", addr, deviceId, ret,
33 : memInfo.addrInfo.flag);
34 0 : return ADUMP_FAILED;
35 : }
36 3 : return ADUMP_SUCCESS;
37 : }
38 :
39 1 : void* DumpMemory::CopyHostToHost(const void* hostMem, uint64_t memSize)
40 : {
41 1 : if (hostMem == nullptr || memSize == 0UL) {
42 0 : IDE_LOGW("Input host mem is nullptr or size equal 0, ptr: %p, size: %lu", hostMem, memSize);
43 0 : return nullptr;
44 : }
45 :
46 1 : void* dstMem = nullptr;
47 1 : rtError_t rtRet = rtMallocHost(&dstMem, memSize, ADUMP_MODULE_NAME_U16);
48 1 : if (rtRet != RT_ERROR_NONE) {
49 0 : IDE_LOGE("rtMallocHost failed, ret: 0x%X", rtRet);
50 0 : return nullptr;
51 : }
52 :
53 1 : rtRet = rtMemcpy(dstMem, memSize, hostMem, memSize, RT_MEMCPY_HOST_TO_HOST);
54 1 : if (rtRet != RT_ERROR_NONE) {
55 0 : IDE_LOGE("rtMemcpy failed, ret: 0x%X", rtRet);
56 0 : FreeHost(dstMem);
57 0 : return nullptr;
58 : }
59 1 : return dstMem;
60 : }
61 :
62 72 : void* DumpMemory::CopyHostToDevice(const void* hostMem, uint64_t memSize)
63 : {
64 72 : if (hostMem == nullptr || memSize == 0) {
65 3 : IDE_LOGW("Input host mem is nullptr or size equal 0, ptr: %p, size: %lu", hostMem, memSize);
66 3 : return nullptr;
67 : }
68 :
69 69 : void* devMem = nullptr;
70 69 : rtError_t rtRet = rtMalloc(&devMem, memSize, RT_MEMORY_HBM, ADUMP_MODULE_NAME_U16);
71 69 : if (rtRet != RT_ERROR_NONE) {
72 6 : IDE_LOGE("rtMalloc failed, ret: 0x%X", rtRet);
73 6 : return nullptr;
74 : }
75 :
76 63 : rtRet = rtMemcpy(devMem, memSize, hostMem, memSize, RT_MEMCPY_HOST_TO_DEVICE);
77 63 : if (rtRet != RT_ERROR_NONE) {
78 9 : IDE_LOGE("rtMemcpy failed, ret: 0x%X", rtRet);
79 9 : FreeDevice(devMem);
80 9 : return nullptr;
81 : }
82 54 : return devMem;
83 : }
84 :
85 161 : void* DumpMemory::CopyDeviceToHost(const void* devMem, uint64_t memSize)
86 : {
87 161 : if (devMem == nullptr || memSize == 0) {
88 4 : IDE_LOGW("Input device mem is nullptr or size equal 0, ptr: %p, size: %lu", devMem, memSize);
89 4 : return nullptr;
90 : }
91 :
92 157 : void* hostMem = nullptr;
93 157 : rtError_t rtRet = rtMallocHost(&hostMem, memSize, ADUMP_MODULE_NAME_U16);
94 157 : if (rtRet != RT_ERROR_NONE) {
95 3 : IDE_LOGE("Call rtMallocHost failed, size: %lu, ret: 0x%X", memSize, rtRet);
96 3 : return nullptr;
97 : }
98 :
99 154 : rtRet = rtMemcpy(hostMem, memSize, devMem, memSize, RT_MEMCPY_DEVICE_TO_HOST);
100 154 : if (rtRet != RT_ERROR_NONE) {
101 3 : IDE_LOGE("Call rtMemcpy failed, ret: 0x%X", rtRet);
102 3 : FreeHost(hostMem);
103 3 : return nullptr;
104 : }
105 151 : return hostMem;
106 : }
107 :
108 56 : void* DumpMemory::CopyDeviceToHostEx(const void* devMem, uint64_t memSize)
109 : {
110 56 : if (devMem == nullptr || memSize == 0UL) {
111 1 : IDE_LOGW("Input device mem is nullptr or size equal 0, ptr: %p, size: %lu", devMem, memSize);
112 1 : return nullptr;
113 : }
114 :
115 55 : void* hostMem = nullptr;
116 55 : rtError_t rtRet = rtMallocHost(&hostMem, memSize, ADUMP_MODULE_NAME_U16);
117 55 : if (rtRet != RT_ERROR_NONE) {
118 0 : IDE_LOGE("rtMallocHost failed, size: %lu, ret: 0x%X", memSize, rtRet);
119 0 : return nullptr;
120 : }
121 :
122 55 : rtRet = rtMemcpyEx(hostMem, memSize, devMem, memSize, RT_MEMCPY_DEVICE_TO_HOST);
123 55 : if (rtRet != RT_ERROR_NONE) {
124 6 : IDE_LOGE("Call rtMemcpyEx failed, ret: 0x%X", rtRet);
125 6 : FreeHost(hostMem);
126 6 : return nullptr;
127 : }
128 49 : return hostMem;
129 : }
130 :
131 211 : void DumpMemory::FreeHost(void*& hostMem)
132 : {
133 211 : (void)rtFreeHost(hostMem);
134 211 : hostMem = nullptr;
135 211 : }
136 :
137 78 : void DumpMemory::FreeDevice(void*& devMem)
138 : {
139 78 : (void)rtFree(devMem);
140 78 : devMem = nullptr;
141 78 : }
142 : } // namespace Adx
|