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("Address[%p] is the valid address of device: %d, ret: %d, check flag: %u",
32 : addr, deviceId, ret, memInfo.addrInfo.flag);
33 0 : return ADUMP_FAILED;
34 : }
35 3 : return ADUMP_SUCCESS;
36 : }
37 :
38 1 : void *DumpMemory::CopyHostToHost(const void *hostMem, uint64_t memSize)
39 : {
40 1 : if (hostMem == nullptr || memSize == 0UL) {
41 0 : IDE_LOGW("Input host mem is nullptr or size equal 0, ptr: %p, size: %lu", hostMem, memSize);
42 0 : return nullptr;
43 : }
44 :
45 1 : void *dstMem = nullptr;
46 1 : rtError_t rtRet = rtMallocHost(&dstMem, memSize, ADUMP_MODULE_NAME_U16);
47 1 : if (rtRet != RT_ERROR_NONE) {
48 0 : IDE_LOGE("rtMallocHost failed, ret: 0x%X", rtRet);
49 0 : return nullptr;
50 : }
51 :
52 1 : rtRet = rtMemcpy(dstMem, memSize, hostMem, memSize, RT_MEMCPY_HOST_TO_HOST);
53 1 : if (rtRet != RT_ERROR_NONE) {
54 0 : IDE_LOGE("rtMemcpy failed, ret: 0x%X", rtRet);
55 0 : FreeHost(dstMem);
56 0 : return nullptr;
57 : }
58 1 : return dstMem;
59 : }
60 :
61 72 : void *DumpMemory::CopyHostToDevice(const void *hostMem, uint64_t memSize)
62 : {
63 72 : if (hostMem == nullptr || memSize == 0) {
64 3 : IDE_LOGW("Input host mem is nullptr or size equal 0, ptr: %p, size: %lu", hostMem, memSize);
65 3 : return nullptr;
66 : }
67 :
68 69 : void *devMem = nullptr;
69 69 : rtError_t rtRet = rtMalloc(&devMem, memSize, RT_MEMORY_HBM, ADUMP_MODULE_NAME_U16);
70 69 : if (rtRet != RT_ERROR_NONE) {
71 6 : IDE_LOGE("rtMalloc failed, ret: 0x%X", rtRet);
72 6 : return nullptr;
73 : }
74 :
75 63 : rtRet = rtMemcpy(devMem, memSize, hostMem, memSize, RT_MEMCPY_HOST_TO_DEVICE);
76 63 : if (rtRet != RT_ERROR_NONE) {
77 9 : IDE_LOGE("rtMemcpy failed, ret: 0x%X", rtRet);
78 9 : FreeDevice(devMem);
79 9 : return nullptr;
80 : }
81 54 : return devMem;
82 : }
83 :
84 161 : void *DumpMemory::CopyDeviceToHost(const void *devMem, uint64_t memSize)
85 : {
86 161 : if (devMem == nullptr || memSize == 0) {
87 4 : IDE_LOGW("Input device mem is nullptr or size equal 0, ptr: %p, size: %lu", devMem, memSize);
88 4 : return nullptr;
89 : }
90 :
91 157 : void *hostMem = nullptr;
92 157 : rtError_t rtRet = rtMallocHost(&hostMem, memSize, ADUMP_MODULE_NAME_U16);
93 157 : if (rtRet != RT_ERROR_NONE) {
94 3 : IDE_LOGE("Call rtMallocHost failed, size: %lu, ret: 0x%X", memSize, rtRet);
95 3 : return nullptr;
96 : }
97 :
98 154 : rtRet = rtMemcpy(hostMem, memSize, devMem, memSize, RT_MEMCPY_DEVICE_TO_HOST);
99 154 : if (rtRet != RT_ERROR_NONE) {
100 3 : IDE_LOGE("Call rtMemcpy failed, ret: 0x%X", rtRet);
101 3 : FreeHost(hostMem);
102 3 : return nullptr;
103 : }
104 151 : return hostMem;
105 : }
106 :
107 56 : void *DumpMemory::CopyDeviceToHostEx(const void *devMem, uint64_t memSize)
108 : {
109 56 : if (devMem == nullptr || memSize == 0UL) {
110 1 : IDE_LOGW("Input device mem is nullptr or size equal 0, ptr: %p, size: %lu", devMem, memSize);
111 1 : return nullptr;
112 : }
113 :
114 55 : void *hostMem = nullptr;
115 55 : rtError_t rtRet = rtMallocHost(&hostMem, memSize, ADUMP_MODULE_NAME_U16);
116 55 : if (rtRet != RT_ERROR_NONE) {
117 0 : IDE_LOGE("rtMallocHost failed, size: %lu, ret: 0x%X", memSize, rtRet);
118 0 : return nullptr;
119 : }
120 :
121 55 : rtRet = rtMemcpyEx(hostMem, memSize, devMem, memSize, RT_MEMCPY_DEVICE_TO_HOST);
122 55 : if (rtRet != RT_ERROR_NONE) {
123 6 : IDE_LOGE("Call rtMemcpyEx failed, ret: 0x%X", rtRet);
124 6 : FreeHost(hostMem);
125 6 : return nullptr;
126 : }
127 49 : return hostMem;
128 : }
129 :
130 211 : void DumpMemory::FreeHost(void *&hostMem)
131 : {
132 211 : (void)rtFreeHost(hostMem);
133 211 : hostMem = nullptr;
134 211 : }
135 :
136 78 : void DumpMemory::FreeDevice(void *&devMem)
137 : {
138 78 : (void)rtFree(devMem);
139 78 : devMem = nullptr;
140 78 : }
141 : } // namespace Adx
|