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) { return DumpManager::Instance().IsEnableDump(dumpType); }
69 :
70 8 : bool AdumpIsDumpEnable(DumpType dumpType, uint64_t& dumpSwitch)
71 : {
72 8 : dumpSwitch = DumpManager::Instance().AdumpGetDumpSwitch();
73 8 : return DumpManager::Instance().IsEnableDump(dumpType);
74 : }
75 :
76 129 : int32_t AdumpSetDumpConfig(DumpType dumpType, const DumpConfig& dumpConfig)
77 : {
78 129 : return DumpManager::Instance().SetDumpConfig(dumpType, dumpConfig);
79 : }
80 :
81 33 : int32_t AdumpSetDump(const char* dumpConfigData, size_t dumpConfigSize)
82 : {
83 33 : return DumpManager::Instance().SetDumpConfig(dumpConfigData, dumpConfigSize);
84 : }
85 :
86 6 : int32_t AdumpSetDumpConfig(const DumpConfigInfo configInfo)
87 : {
88 6 : return DumpManager::Instance().SetDumpConfig(
89 6 : configInfo.dumpConfigData, configInfo.dumpConfigSize, configInfo.dumpConfigPath);
90 : }
91 :
92 15 : int32_t AdumpUnSetDump() { return DumpManager::Instance().UnSetDumpConfig(); }
93 :
94 6 : int32_t AdumpDumpTensor(
95 : const std::string& opType, const std::string& opName, const std::vector<TensorInfo>& tensors, aclrtStream stream)
96 : {
97 6 : return DumpManager::Instance().DumpOperator(opType, opName, tensors, stream);
98 : }
99 :
100 3 : int32_t AdumpDumpTensorV2(
101 : const std::string& opType, const std::string& opName, const std::vector<TensorInfoV2>& tensors, aclrtStream stream)
102 : {
103 3 : return DumpManager::Instance().DumpOperatorV2(opType, opName, tensors, stream);
104 : }
105 :
106 18 : int32_t AdumpDumpTensorWithCfg(
107 : const std::string& opType, const std::string& opName, const std::vector<TensorInfo>& tensors, aclrtStream stream,
108 : const DumpCfg& dumpCfg)
109 : {
110 18 : return DumpManager::Instance().DumpOperatorWithCfg(opType, opName, tensors, stream, dumpCfg);
111 : }
112 :
113 3 : int32_t AdumpAddExceptionOperatorInfo(const OperatorInfo& opInfo)
114 : {
115 3 : DumpManager::Instance().AddExceptionOp(opInfo);
116 3 : return ADUMP_SUCCESS;
117 : }
118 :
119 3 : int32_t AdumpAddExceptionOperatorInfoV2(const OperatorInfoV2& opInfo)
120 : {
121 3 : DumpManager::Instance().AddExceptionOpV2(opInfo);
122 3 : return ADUMP_SUCCESS;
123 : }
124 :
125 6 : int32_t AdumpDelExceptionOperatorInfo(uint32_t deviceId, uint32_t streamId)
126 : {
127 6 : return DumpManager::Instance().DelExceptionOp(deviceId, streamId);
128 : }
129 :
130 1 : void AdumpPrintWorkSpace(
131 : const void* workSpaceAddr, const size_t dumpWorkSpaceSize, aclrtStream stream, const char* opType)
132 : {
133 1 : AdxPrintWorkSpace(workSpaceAddr, dumpWorkSpaceSize, stream, opType, true);
134 1 : }
135 :
136 1 : void AdumpPrintWorkSpace(
137 : const void* workSpaceAddr, const size_t dumpWorkSpaceSize, aclrtStream stream, const char* opType, bool enableSync)
138 : {
139 1 : AdxPrintWorkSpace(workSpaceAddr, dumpWorkSpaceSize, stream, opType, enableSync);
140 1 : }
141 :
142 1 : void AdumpPrintAndGetTimeStampInfo(
143 : const void* workSpaceAddr, const size_t dumpWorkSpaceSize, aclrtStream stream, const char* opType,
144 : std::vector<MsprofAicTimeStampInfo>& timeStampInfo)
145 : {
146 1 : AdxPrintTimeStamp(workSpaceAddr, dumpWorkSpaceSize, stream, opType, timeStampInfo);
147 1 : }
148 :
149 2 : void AdumpPrintSetConfig(const AdumpPrintConfig& config) { AdxPrintSetConfig(config); }
150 :
151 9 : int32_t AdumpRegExceptionDumpCallback(ExceptionDumpCallback callback)
152 : {
153 9 : return DumpManager::Instance().RegisterExceptionDumpCallback(callback);
154 : }
155 :
156 9 : int32_t AdumpUnregExceptionDumpCallback(ExceptionDumpCallback callback)
157 : {
158 9 : return DumpManager::Instance().UnregisterExceptionDumpCallback(callback);
159 : }
160 :
161 : #ifndef __ADUMP_LLT
162 : static void __attribute__((constructor)) AdumpInit(void) { (void)DumpManager::Instance(); }
163 : #endif
164 :
165 : // 内部接口
166 5 : uint64_t GetDFXInfoChunkCursor(uint8_t bufferId)
167 : {
168 5 : if (bufferId == 0U) {
169 4 : return g_dynamicWriteIdx.load(std::memory_order_relaxed);
170 : }
171 1 : return g_staticWriteIdx.load(std::memory_order_relaxed);
172 : }
173 : } // namespace Adx
|