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 : #include <atomic>
11 : #include "adump_pub.h"
12 : #include "adump_api.h"
13 : #include "dump_manager.h"
14 : #include "dump_printf.h"
15 :
16 : namespace Adx {
17 : namespace {
18 : std::atomic<uint64_t> g_dynamicWriteIdx{0};
19 : std::atomic<uint64_t> g_staticWriteIdx{0};
20 :
21 : constexpr uint64_t MAGIC_NUM = 0xA5A5A5A500000000;
22 : constexpr uint64_t FLIP_NUM_MASK = 0b01111111;
23 : constexpr uint16_t FLIP_NUM_SHIFT_BITS = 24;
24 : constexpr uint64_t STATIC_BUFFER_ID = 0x080000000;
25 : } // namespace
26 :
27 168013 : void *AdumpGetDFXInfoAddrForDynamic(uint32_t space, uint64_t &atomicIndex)
28 : {
29 168013 : if (space > DFX_MAX_TENSOR_NUM || g_dynamicChunk == nullptr) {
30 1 : return nullptr;
31 : }
32 :
33 168012 : auto nextWriteCursor = g_dynamicWriteIdx.fetch_add(space + RESERVE_SPACE);
34 168012 : uint64_t flipNum = nextWriteCursor / DYNAMIC_RING_CHUNK_SIZE;
35 168012 : uint64_t offset = nextWriteCursor % DYNAMIC_RING_CHUNK_SIZE;
36 168012 : atomicIndex = MAGIC_NUM | ((flipNum & FLIP_NUM_MASK) << FLIP_NUM_SHIFT_BITS) | offset;
37 168012 : g_dynamicChunk[offset] = MAGIC_NUM | space;
38 168012 : g_dynamicChunk[offset + 1] = atomicIndex;
39 168012 : return g_dynamicChunk + offset + RESERVE_SPACE;
40 : }
41 :
42 168011 : void *AdumpGetDFXInfoAddrForStatic(uint32_t space, uint64_t &atomicIndex)
43 : {
44 168011 : if (space > DFX_MAX_TENSOR_NUM || g_staticChunk == nullptr) {
45 1 : return nullptr;
46 : }
47 :
48 168010 : auto nextWriteCursor = g_staticWriteIdx.fetch_add(space + RESERVE_SPACE);
49 168010 : uint64_t flipNum = nextWriteCursor / STATIC_RING_CHUNK_SIZE;
50 168010 : uint64_t offset = nextWriteCursor % STATIC_RING_CHUNK_SIZE;
51 168010 : atomicIndex = MAGIC_NUM | STATIC_BUFFER_ID | ((flipNum & FLIP_NUM_MASK) << FLIP_NUM_SHIFT_BITS) | offset;
52 168010 : g_staticChunk[offset] = MAGIC_NUM | space;
53 168010 : g_staticChunk[offset + 1] = atomicIndex;
54 168010 : return g_staticChunk + offset + RESERVE_SPACE;
55 : }
56 :
57 33 : uint64_t AdumpGetDumpSwitch(const DumpType dumpType)
58 : {
59 33 : bool isEnable = DumpManager::Instance().IsEnableDump(dumpType);
60 33 : if (dumpType == DumpType::OPERATOR && isEnable) {
61 6 : return DumpManager::Instance().AdumpGetDumpSwitch();
62 27 : } else if (dumpType == DumpType::OP_OVERFLOW && isEnable) {
63 3 : return DumpManager::Instance().AdumpGetDumpSwitch();
64 : }
65 24 : return isEnable;
66 : }
67 :
68 69 : bool AdumpIsDumpEnable(DumpType dumpType)
69 : {
70 69 : return DumpManager::Instance().IsEnableDump(dumpType);
71 : }
72 :
73 8 : bool AdumpIsDumpEnable(DumpType dumpType, uint64_t &dumpSwitch)
74 : {
75 8 : dumpSwitch = DumpManager::Instance().AdumpGetDumpSwitch();
76 8 : return DumpManager::Instance().IsEnableDump(dumpType);
77 : }
78 :
79 129 : int32_t AdumpSetDumpConfig(DumpType dumpType, const DumpConfig &dumpConfig)
80 : {
81 129 : return DumpManager::Instance().SetDumpConfig(dumpType, dumpConfig);
82 : }
83 :
84 33 : int32_t AdumpSetDump(const char *dumpConfigData, size_t dumpConfigSize)
85 : {
86 33 : return DumpManager::Instance().SetDumpConfig(dumpConfigData, dumpConfigSize);
87 : }
88 :
89 6 : int32_t AdumpSetDumpConfig(const DumpConfigInfo configInfo)
90 : {
91 6 : return DumpManager::Instance().SetDumpConfig(configInfo.dumpConfigData, configInfo.dumpConfigSize, configInfo.dumpConfigPath);
92 : }
93 :
94 15 : int32_t AdumpUnSetDump()
95 : {
96 15 : return DumpManager::Instance().UnSetDumpConfig();
97 : }
98 :
99 6 : int32_t AdumpDumpTensor(const std::string &opType, const std::string &opName, const std::vector<TensorInfo> &tensors,
100 : aclrtStream stream)
101 : {
102 6 : return DumpManager::Instance().DumpOperator(opType, opName, tensors, stream);
103 : }
104 :
105 3 : int32_t AdumpDumpTensorV2(const std::string &opType, const std::string &opName, const std::vector<TensorInfoV2> &tensors,
106 : aclrtStream stream)
107 : {
108 3 : return DumpManager::Instance().DumpOperatorV2(opType, opName, tensors, stream);
109 : }
110 :
111 18 : int32_t AdumpDumpTensorWithCfg(const std::string &opType, const std::string &opName,
112 : const std::vector<TensorInfo> &tensors, aclrtStream stream, const DumpCfg& dumpCfg)
113 : {
114 18 : return DumpManager::Instance().DumpOperatorWithCfg(opType, opName, tensors, stream, dumpCfg);
115 : }
116 :
117 3 : int32_t AdumpAddExceptionOperatorInfo(const OperatorInfo &opInfo)
118 : {
119 3 : DumpManager::Instance().AddExceptionOp(opInfo);
120 3 : return ADUMP_SUCCESS;
121 : }
122 :
123 3 : int32_t AdumpAddExceptionOperatorInfoV2(const OperatorInfoV2 &opInfo)
124 : {
125 3 : DumpManager::Instance().AddExceptionOpV2(opInfo);
126 3 : return ADUMP_SUCCESS;
127 : }
128 :
129 6 : int32_t AdumpDelExceptionOperatorInfo(uint32_t deviceId, uint32_t streamId)
130 : {
131 6 : return DumpManager::Instance().DelExceptionOp(deviceId, streamId);
132 : }
133 :
134 1 : void AdumpPrintWorkSpace(const void *workSpaceAddr, const size_t dumpWorkSpaceSize, aclrtStream stream,
135 : const char *opType)
136 : {
137 1 : AdxPrintWorkSpace(workSpaceAddr, dumpWorkSpaceSize, stream, opType, true);
138 1 : }
139 :
140 1 : void AdumpPrintWorkSpace(const void *workSpaceAddr, const size_t dumpWorkSpaceSize, aclrtStream stream,
141 : const char *opType, bool enableSync)
142 : {
143 1 : AdxPrintWorkSpace(workSpaceAddr, dumpWorkSpaceSize, stream, opType, enableSync);
144 1 : }
145 :
146 1 : void AdumpPrintAndGetTimeStampInfo(const void *workSpaceAddr, const size_t dumpWorkSpaceSize, aclrtStream stream,
147 : const char *opType, std::vector<MsprofAicTimeStampInfo> &timeStampInfo)
148 : {
149 1 : AdxPrintTimeStamp(workSpaceAddr, dumpWorkSpaceSize, stream, opType, timeStampInfo);
150 1 : }
151 :
152 2 : void AdumpPrintSetConfig(const AdumpPrintConfig &config)
153 : {
154 2 : AdxPrintSetConfig(config);
155 2 : }
156 :
157 9 : int32_t AdumpRegExceptionDumpCallback(ExceptionDumpCallback callback)
158 : {
159 9 : return DumpManager::Instance().RegisterExceptionDumpCallback(callback);
160 : }
161 :
162 9 : int32_t AdumpUnregExceptionDumpCallback(ExceptionDumpCallback callback)
163 : {
164 9 : return DumpManager::Instance().UnregisterExceptionDumpCallback(callback);
165 : }
166 :
167 : #ifndef __ADUMP_LLT
168 : static void __attribute__((constructor)) AdumpInit(void)
169 : {
170 : (void)DumpManager::Instance();
171 : }
172 : #endif
173 :
174 : // 内部接口
175 5 : uint64_t GetDFXInfoChunkCursor(uint8_t bufferId)
176 : {
177 5 : if (bufferId == 0U) {
178 4 : return g_dynamicWriteIdx.load(std::memory_order_relaxed);
179 : }
180 1 : return g_staticWriteIdx.load(std::memory_order_relaxed);
181 : }
182 : } // namespace Adx
|