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 "aicpu_context.h"
11 :
12 : #include <map>
13 : #include <memory>
14 : #include <mutex>
15 : #include <thread>
16 : #include <vector>
17 : #include <atomic>
18 : #include "driver/ascend_hal_define.h"
19 : #include "aicpu_sharder_log.h"
20 :
21 : namespace {
22 : // current thread context
23 : thread_local aicpu::aicpuContext_t g_curCtx;
24 : // current thread prof context
25 : thread_local aicpu::aicpuProfContext_t g_curProfCtx;
26 : // task moniter context
27 : std::unique_ptr<std::string[]> g_opsname(nullptr);
28 : thread_local uint32_t g_threadIndex = UINT32_MAX;
29 : uint32_t g_aicpuCoreCnt = 0U;
30 : thread_local std::map<std::string, std::string> g_threadLocalAicpuCtx;
31 : thread_local aicpu::streamAndTaskId_t g_streamAndTaskId;
32 : thread_local uint32_t g_blockIdx = 0U;
33 : thread_local uint32_t g_blockNum = 0U;
34 : // aicpu run mode
35 : uint32_t g_runMode = static_cast<uint32_t>(aicpu::AicpuRunMode::THREAD_MODE);
36 : // uniqueVfId
37 : std::atomic<uint32_t> g_uniqueVfId;
38 : bool g_isCustAicpuSd = false;
39 : std::mutex g_sqeIdMtx;
40 : constexpr uint32_t INITIAL_SQE_IQ = 0x80000000U;
41 : uint32_t g_sqeId = INITIAL_SQE_IQ;
42 :
43 : enum class AicpuDfxInfoRet : int32_t {
44 : OK = 0,
45 : NOT_SET = 1,
46 : INVALID_PARAM = -1,
47 : };
48 :
49 : // context info
50 : std::mutex g_defaultMutex;
51 : std::vector<std::map<std::string, std::string>> g_defaultThreadCtx;
52 : std::mutex g_profMutex;
53 : std::vector<std::map<std::string, std::string>> g_profThreadCtx;
54 : std::mutex g_debugMutex;
55 : std::vector<std::map<std::string, std::string>> g_debugThreadCtx;
56 : std::mutex g_funcMapMutex;
57 : std::map<uint32_t, std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>> g_funcMap;
58 :
59 : typedef struct {
60 : std::mutex dfxInfoMutex;
61 : uint64_t dfxInfoAddr = 0U;
62 : bool dfxInfoSet = false;
63 : } DfxStorer;
64 : DfxStorer g_dfxStorer = {};
65 :
66 11 : std::map<std::string, std::string>& GetThreadCtx(const aicpu::CtxType type, const uint32_t threadIndex)
67 : {
68 11 : const size_t thredId = static_cast<size_t>(threadIndex);
69 11 : if (type == aicpu::CTX_DEBUG) {
70 8 : const std::unique_lock<std::mutex> locker(g_defaultMutex);
71 8 : if (thredId >= g_debugThreadCtx.size()) {
72 1 : g_debugThreadCtx.resize(thredId + static_cast<size_t>(1));
73 : }
74 8 : return g_debugThreadCtx[thredId];
75 11 : } else if (type == aicpu::CTX_PROF) {
76 1 : const std::unique_lock<std::mutex> locker(g_profMutex);
77 1 : if (thredId >= g_profThreadCtx.size()) {
78 1 : g_profThreadCtx.resize(thredId + static_cast<size_t>(1));
79 : }
80 1 : return g_profThreadCtx[thredId];
81 1 : } else {
82 2 : const std::unique_lock<std::mutex> locker(g_debugMutex);
83 2 : if (thredId >= g_defaultThreadCtx.size()) {
84 1 : g_defaultThreadCtx.resize(thredId + static_cast<size_t>(1));
85 : }
86 2 : return g_defaultThreadCtx[thredId];
87 2 : }
88 : }
89 : } // namespace
90 :
91 : namespace aicpu {
92 24 : __attribute__((visibility("default"))) status_t aicpuSetContext(aicpuContext_t* ctx)
93 : {
94 24 : g_curCtx = *ctx;
95 24 : return AICPU_ERROR_NONE;
96 : }
97 :
98 12 : __attribute__((visibility("default"))) status_t aicpuGetContext(aicpuContext_t* ctx)
99 : {
100 12 : *ctx = g_curCtx;
101 12 : return AICPU_ERROR_NONE;
102 : }
103 :
104 5 : void GetSqeId(const uint32_t num, uint32_t& start, uint32_t& end)
105 : {
106 5 : std::lock_guard<std::mutex> lk(g_sqeIdMtx);
107 5 : start = g_sqeId;
108 5 : g_sqeId += num;
109 5 : end = g_sqeId;
110 5 : if (start >= end) {
111 2 : g_sqeId = INITIAL_SQE_IQ;
112 2 : start = g_sqeId;
113 2 : g_sqeId += num;
114 2 : end = g_sqeId;
115 2 : if (start >= end) {
116 : // Num reached the maximum.
117 1 : AICPUE_LOGW("The num[%u] exceeds the maximum number that can be applied for.", num);
118 1 : g_sqeId = INITIAL_SQE_IQ;
119 1 : return;
120 : }
121 1 : AICPUE_LOGW("The num[%u] exceeds the max, start will begin form initial value.", num);
122 : }
123 4 : return;
124 5 : }
125 :
126 129 : status_t aicpuSetProfContext(const aicpuProfContext_t& ctx)
127 : {
128 129 : g_curProfCtx = ctx;
129 129 : return AICPU_ERROR_NONE;
130 : }
131 :
132 7 : const aicpuProfContext_t& aicpuGetProfContext() { return g_curProfCtx; }
133 :
134 14 : status_t InitTaskMonitorContext(uint32_t aicpuCoreCnt)
135 : {
136 14 : if (aicpuCoreCnt == 0U) {
137 1 : AICPUE_LOGE("invalid aicpu core count[%u]", aicpuCoreCnt);
138 1 : return AICPU_ERROR_FAILED;
139 : }
140 13 : g_aicpuCoreCnt = aicpuCoreCnt;
141 13 : AICPUE_LOGI("aicpu core count[%u]", aicpuCoreCnt);
142 37 : g_opsname.reset(new (std::nothrow) std::string[aicpuCoreCnt]);
143 13 : if (g_opsname == nullptr) {
144 0 : AICPUE_LOGE("malloc ops name momery for task monitor failed");
145 0 : return AICPU_ERROR_FAILED;
146 : }
147 37 : for (uint32_t idx = 0U; idx < aicpuCoreCnt; ++idx) {
148 24 : g_opsname[static_cast<size_t>(idx)] = "null";
149 : }
150 13 : return AICPU_ERROR_NONE;
151 : }
152 :
153 30 : status_t SetAicpuThreadIndex(uint32_t threadIndex)
154 : {
155 30 : g_threadIndex = threadIndex;
156 30 : return AICPU_ERROR_NONE;
157 : }
158 :
159 160 : uint32_t GetAicpuThreadIndex() { return g_threadIndex; }
160 :
161 248 : status_t SetOpname(const std::string& opname)
162 : {
163 248 : if ((g_opsname != nullptr) && (g_threadIndex < g_aicpuCoreCnt)) {
164 241 : AICPUE_LOGI("set op name to %s for thread[%u]", opname.c_str(), g_threadIndex);
165 238 : g_opsname[static_cast<size_t>(g_threadIndex)] = opname;
166 240 : return AICPU_ERROR_NONE;
167 : }
168 : // maintenance function, if failed just print event log
169 7 : AICPUE_RUN_LOGW(
170 : "set op name[%s] failed, thread index[%u] should be less than total aicpu core count[%u],"
171 : " and ops name array addr cannot null",
172 : opname.c_str(), g_threadIndex, g_aicpuCoreCnt);
173 7 : return AICPU_ERROR_NONE;
174 : }
175 :
176 20 : status_t GetOpname(uint32_t threadIndex, std::string& opname)
177 : {
178 20 : if ((g_opsname != nullptr) && (threadIndex < g_aicpuCoreCnt)) {
179 19 : opname = g_opsname[static_cast<size_t>(threadIndex)];
180 19 : return AICPU_ERROR_NONE;
181 : }
182 1 : opname = "null";
183 : // maintenance function, if failed just print event log
184 1 : AICPUE_RUN_LOGW(
185 : "get op name failed, thread index[%u] should be less than total aicpu core count[%u],"
186 : " and ops name array addr cannot null",
187 : g_threadIndex, g_aicpuCoreCnt);
188 1 : return AICPU_ERROR_NONE;
189 : }
190 :
191 137 : status_t SetTaskAndStreamId(uint64_t taskId, uint32_t streamId)
192 : {
193 137 : g_streamAndTaskId.taskId = taskId;
194 137 : g_streamAndTaskId.streamId = streamId;
195 137 : AICPUE_LOGI("Set taskId:[%lu] and streamId:[%u] success.", taskId, streamId);
196 137 : return AICPU_ERROR_NONE;
197 : }
198 :
199 36 : status_t GetTaskAndStreamId(uint64_t& taskId, uint32_t& streamId)
200 : {
201 36 : taskId = g_streamAndTaskId.taskId;
202 36 : streamId = g_streamAndTaskId.streamId;
203 36 : AICPUE_LOGI("Get taskId:[%lu] and streamId:[%u] success.", taskId, streamId);
204 36 : return AICPU_ERROR_NONE;
205 : }
206 :
207 121 : status_t SetBlockIdxAndBlockNum(uint32_t blockIdx, uint32_t blockNum)
208 : {
209 121 : g_blockIdx = blockIdx;
210 121 : g_blockNum = blockNum;
211 121 : AICPUE_LOGI("Set blockIdx:[%u] and blockNum:[%u] success.", blockIdx, blockNum);
212 121 : return AICPU_ERROR_NONE;
213 : }
214 :
215 1 : uint32_t GetBlockIdx() { return g_blockIdx; }
216 :
217 1 : uint32_t GetBlockNum() { return g_blockNum; }
218 :
219 23 : status_t SetAicpuRunMode(uint32_t runMode)
220 : {
221 23 : g_runMode = runMode;
222 23 : AICPUE_LOGI("Set runMode:[%u] success.", runMode);
223 23 : return AICPU_ERROR_NONE;
224 : }
225 :
226 62 : status_t GetAicpuRunMode(uint32_t& runMode)
227 : {
228 62 : runMode = g_runMode;
229 62 : return AICPU_ERROR_NONE;
230 : }
231 :
232 278 : status_t SetThreadLocalCtx(const std::string& key, const std::string& value)
233 : {
234 278 : if (key.empty()) {
235 1 : AICPUE_LOGE("set thread local context failed, key is empty");
236 1 : return AICPU_ERROR_FAILED;
237 : }
238 : try {
239 277 : g_threadLocalAicpuCtx[key] = value;
240 0 : } catch (std::exception& e) {
241 0 : AICPUE_LOGE("set thread local context failed, %s", e.what());
242 0 : return AICPU_ERROR_FAILED;
243 0 : }
244 280 : return AICPU_ERROR_NONE;
245 : }
246 :
247 142 : status_t GetThreadLocalCtx(const std::string& key, std::string& value)
248 : {
249 142 : if (key.empty()) {
250 1 : AICPUE_LOGE("get thread local context failed, key is empty");
251 1 : return AICPU_ERROR_FAILED;
252 : }
253 141 : const auto iter = g_threadLocalAicpuCtx.find(key);
254 140 : if (iter != g_threadLocalAicpuCtx.end()) {
255 130 : value = iter->second;
256 132 : return AICPU_ERROR_NONE;
257 : }
258 10 : AICPUE_LOGW("get thread local context failed, no such key[%s]", key.c_str());
259 10 : return AICPU_ERROR_FAILED;
260 : }
261 :
262 2 : status_t RemoveThreadLocalCtx(const std::string& key)
263 : {
264 2 : const auto iter = g_threadLocalAicpuCtx.find(key);
265 2 : if (iter != g_threadLocalAicpuCtx.end()) {
266 1 : (void)g_threadLocalAicpuCtx.erase(iter);
267 1 : return AICPU_ERROR_NONE;
268 : }
269 1 : AICPUE_LOGE("remove thread local context failed, no such key[%s]", key.c_str());
270 1 : return AICPU_ERROR_FAILED;
271 : }
272 :
273 3 : const std::map<std::string, std::string>& GetAllThreadCtxInfo(aicpu::CtxType type, uint32_t threadIndex)
274 : {
275 3 : AICPUE_LOGI("Get all thread ctx info begin, thread index:%u", threadIndex);
276 3 : auto& ctx = GetThreadCtx(type, threadIndex);
277 3 : return ctx;
278 : }
279 :
280 4 : status_t RegisterEventCallback(
281 : const uint32_t eventId, const uint32_t subeventId, std::function<void(void*)> func, const bool isNeedClear)
282 : {
283 4 : const std::lock_guard<std::mutex> locker(g_funcMapMutex);
284 4 : std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>& subMap = g_funcMap[eventId];
285 4 : const auto it = subMap.insert({subeventId, {func, isNeedClear}});
286 4 : if (!it.second) {
287 1 : AICPUE_LOGE(
288 : "register event call function failed, repulicate register callback "
289 : "function by eventId[%u] subeventId[%u]",
290 : eventId, subeventId);
291 1 : return AICPU_ERROR_FAILED;
292 : }
293 3 : return AICPU_ERROR_NONE;
294 4 : }
295 :
296 4 : status_t DoEventCallback(const uint32_t eventId, const uint32_t subeventId, void* const param)
297 : {
298 4 : const std::lock_guard<std::mutex> locker(g_funcMapMutex);
299 4 : const auto iter = g_funcMap.find(eventId);
300 4 : if (iter == g_funcMap.end()) {
301 2 : AICPUE_RUN_LOGW(
302 : "do event callback function failed, cannot find callback function by "
303 : "eventId[%u] subeventId[%u]",
304 : eventId, subeventId);
305 2 : return AICPU_ERROR_FAILED;
306 : }
307 :
308 2 : std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>& subMap = iter->second;
309 2 : const auto subIter = subMap.find(subeventId);
310 2 : if (subIter == subMap.end()) {
311 1 : AICPUE_RUN_LOGW(
312 : "do event callback function failed, cannot find callback function by "
313 : "eventId[%u] subeventId[%u]",
314 : eventId, subeventId);
315 1 : return AICPU_ERROR_FAILED;
316 : }
317 1 : ((subIter->second).first)(param);
318 : // erase func after call
319 1 : if ((subIter->second).second) {
320 1 : (void)subMap.erase(subIter);
321 : }
322 1 : return AICPU_ERROR_NONE;
323 4 : }
324 :
325 3 : status_t UnRegisterCallback(const uint32_t eventId, const uint32_t subeventId)
326 : {
327 3 : const std::lock_guard<std::mutex> locker(g_funcMapMutex);
328 3 : const auto iter = g_funcMap.find(eventId);
329 3 : if (iter == g_funcMap.end()) {
330 1 : AICPUE_RUN_LOGW(
331 : "skip unregister event callback function, cannot find callback function by eventId[%u] "
332 : "subeventId[%u]",
333 : eventId, subeventId);
334 1 : return AICPU_ERROR_NONE;
335 : }
336 :
337 2 : std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>& subMap = iter->second;
338 2 : const auto subIter = subMap.find(subeventId);
339 2 : if (subIter == subMap.end()) {
340 1 : AICPUE_RUN_LOGW(
341 : "skip unregister event callback function, cannot find callback function by eventId[%u] "
342 : "subeventId[%u]",
343 : eventId, subeventId);
344 1 : return AICPU_ERROR_NONE;
345 : }
346 1 : (void)subMap.erase(subIter);
347 1 : return AICPU_ERROR_NONE;
348 3 : }
349 :
350 : using AicpuStreamDvpp = struct {
351 : uint8_t* dvppBuff;
352 : uint64_t dvppBuffLen;
353 : int32_t channelId;
354 : };
355 :
356 : static pthread_rwlock_t g_streamAndChannelMapLock[AICPU_DVPP_CHL_BUTT] = {
357 : PTHREAD_RWLOCK_INITIALIZER, PTHREAD_RWLOCK_INITIALIZER};
358 : static std::map<uint32_t, AicpuStreamDvpp> g_streamAndChannelMap[AICPU_DVPP_CHL_BUTT];
359 :
360 4 : void SetStreamDvppBuffBychlType(const AicpuDvppChlType chlType, const uint64_t buffLen, uint8_t* buff)
361 : {
362 4 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
363 1 : AICPUE_LOGE("chlType is invalid, chlType[%d].", static_cast<int32_t>(chlType));
364 1 : return;
365 : }
366 :
367 3 : uint64_t taskId = 0U;
368 3 : uint32_t streamId = 0U;
369 3 : if (GetTaskAndStreamId(taskId, streamId) != AICPU_ERROR_NONE) {
370 1 : AICPUE_LOGE("Get taskId and streamId failed.");
371 1 : return;
372 : }
373 :
374 2 : (void)pthread_rwlock_rdlock(&g_streamAndChannelMapLock[chlType]);
375 2 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
376 2 : if (iter != g_streamAndChannelMap[chlType].end()) {
377 2 : iter->second.dvppBuff = buff;
378 2 : iter->second.dvppBuffLen = buffLen;
379 2 : AICPUE_LOGI("Set dvpp len [%lu], stream [%u].", buffLen, streamId);
380 : }
381 2 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
382 2 : return;
383 : }
384 :
385 3 : void SetStreamDvppBuffByStreamId(
386 : const AicpuDvppChlType chlType, const uint32_t streamId, const uint64_t buffLen, uint8_t* buff)
387 : {
388 3 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
389 1 : AICPUE_LOGE("chlType is invalid, chlType[%d].", static_cast<int32_t>(chlType));
390 1 : return;
391 : }
392 :
393 2 : (void)pthread_rwlock_rdlock(&g_streamAndChannelMapLock[chlType]);
394 2 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
395 2 : if (iter != g_streamAndChannelMap[chlType].end()) {
396 2 : iter->second.dvppBuff = buff;
397 2 : iter->second.dvppBuffLen = buffLen;
398 2 : AICPUE_LOGI("Set dvpp len [%lu], stream [%u].", buffLen, streamId);
399 : }
400 2 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
401 2 : return;
402 : }
403 :
404 4 : void GetDvppBufAndLenBychlType(const AicpuDvppChlType chlType, uint8_t** buff, uint64_t* buffLen)
405 : {
406 4 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
407 1 : AICPUE_LOGE("chlType is invalid, chlType[%d].", static_cast<int32_t>(chlType));
408 1 : return;
409 : }
410 :
411 3 : uint64_t taskId = 0U;
412 3 : uint32_t streamId = 0U;
413 3 : if (GetTaskAndStreamId(taskId, streamId) != AICPU_ERROR_NONE) {
414 1 : AICPUE_LOGE("Get taskId and streamId failed. taskId[%lu] streamId[%u]", taskId, streamId);
415 1 : return;
416 : }
417 :
418 2 : (void)pthread_rwlock_rdlock(&g_streamAndChannelMapLock[chlType]);
419 2 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
420 2 : if (iter != g_streamAndChannelMap[chlType].end()) {
421 2 : *buff = iter->second.dvppBuff;
422 2 : *buffLen = iter->second.dvppBuffLen;
423 2 : AICPUE_LOGI("Get dvpp len [%lu], stream [%u].", *buffLen, streamId);
424 : }
425 2 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
426 2 : return;
427 : }
428 :
429 3 : void GetDvppBufAndLenByStreamId(const uint32_t streamId, const AicpuDvppChlType chlType, uint8_t** buff)
430 : {
431 3 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
432 1 : AICPUE_LOGE("chlType is invalid, chlType[%d].", static_cast<int32_t>(chlType));
433 1 : return;
434 : }
435 :
436 2 : (void)pthread_rwlock_rdlock(&g_streamAndChannelMapLock[chlType]);
437 2 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
438 2 : if (iter != g_streamAndChannelMap[chlType].end()) {
439 2 : *buff = iter->second.dvppBuff;
440 2 : AICPUE_LOGI("GetDvppBufAndLenByStreamId stream [%d].", streamId);
441 : }
442 2 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
443 2 : return;
444 : }
445 :
446 23 : int32_t GetStreamDvppChannelId(uint32_t streamId, AicpuDvppChlType chlType)
447 : {
448 23 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
449 1 : return -1;
450 : }
451 :
452 22 : int32_t channelId = -1;
453 22 : (void)pthread_rwlock_rdlock(&g_streamAndChannelMapLock[chlType]);
454 22 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
455 22 : if (iter != g_streamAndChannelMap[chlType].end()) {
456 8 : channelId = iter->second.channelId;
457 : }
458 22 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
459 22 : return channelId;
460 : }
461 :
462 11 : int32_t GetCurTaskDvppChannelId(AicpuDvppChlType chlType)
463 : {
464 11 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
465 1 : return -1;
466 : }
467 :
468 10 : uint64_t taskId = 0U;
469 10 : uint32_t streamId = 0U;
470 10 : (void)GetTaskAndStreamId(taskId, streamId);
471 :
472 10 : return GetStreamDvppChannelId(streamId, chlType);
473 : }
474 :
475 9 : int32_t InitStreamDvppChannel(uint32_t streamId, AicpuDvppChlType chlType, int32_t channelId)
476 : {
477 9 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
478 1 : return -1;
479 : }
480 :
481 8 : int32_t streamChannel = channelId;
482 8 : (void)pthread_rwlock_wrlock(&g_streamAndChannelMapLock[chlType]);
483 8 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
484 8 : if (iter == g_streamAndChannelMap[chlType].end()) {
485 : AicpuStreamDvpp aicpuStream;
486 5 : aicpuStream.dvppBuffLen = 0LLU;
487 5 : aicpuStream.dvppBuff = nullptr;
488 5 : aicpuStream.channelId = streamChannel;
489 5 : (void)g_streamAndChannelMap[chlType].insert(std::pair<uint32_t, AicpuStreamDvpp>(streamId, aicpuStream));
490 : } else {
491 3 : streamChannel = iter->second.channelId;
492 : }
493 8 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
494 8 : return streamChannel;
495 : }
496 :
497 5 : int32_t UnInitStreamDvppChannel(uint32_t streamId, AicpuDvppChlType chlType)
498 : {
499 5 : if (chlType >= AICPU_DVPP_CHL_BUTT) {
500 1 : return -1;
501 : }
502 :
503 4 : int32_t streamChannel = -1;
504 4 : (void)pthread_rwlock_wrlock(&g_streamAndChannelMapLock[chlType]);
505 4 : const std::map<uint32_t, AicpuStreamDvpp>::iterator iter = g_streamAndChannelMap[chlType].find(streamId);
506 4 : if (iter != g_streamAndChannelMap[chlType].end()) {
507 4 : iter->second.dvppBuffLen = 0LLU;
508 4 : streamChannel = iter->second.channelId;
509 4 : (void)g_streamAndChannelMap[chlType].erase(iter);
510 : }
511 4 : (void)pthread_rwlock_unlock(&g_streamAndChannelMapLock[chlType]);
512 4 : return streamChannel;
513 : }
514 :
515 10 : uint32_t GetUniqueVfId() { return g_uniqueVfId; }
516 :
517 30 : void SetUniqueVfId(const uint32_t uniqueVfId) { g_uniqueVfId = uniqueVfId; }
518 :
519 15 : void SetCustAicpuSdFlag(const bool isCustAicpuSdFlag) { g_isCustAicpuSd = isCustAicpuSdFlag; }
520 :
521 135 : bool IsCustAicpuSd() { return g_isCustAicpuSd; }
522 :
523 2 : void AicpuSetDfxInfo(const uint64_t dfxInfoAddr)
524 : {
525 2 : std::lock_guard<std::mutex> lock(g_dfxStorer.dfxInfoMutex);
526 2 : g_dfxStorer.dfxInfoAddr = dfxInfoAddr;
527 2 : g_dfxStorer.dfxInfoSet = true;
528 2 : }
529 : } // namespace aicpu
530 :
531 4 : aicpu::status_t SetThreadCtxInfo(aicpu::CtxType type, const std::string& key, const std::string& value)
532 : {
533 4 : if (key.empty()) {
534 2 : AICPUE_LOGE("Set thread context failed, context type[%d], key is empty", static_cast<int32_t>(type));
535 2 : return aicpu::AICPU_ERROR_FAILED;
536 : }
537 :
538 2 : auto& ctx = GetThreadCtx(type, g_threadIndex);
539 : try {
540 2 : ctx[key] = value;
541 0 : } catch (std::exception& aicpuExp) {
542 0 : AICPUE_LOGE("Set thread context failed, context type[%d], %s", static_cast<int32_t>(type), aicpuExp.what());
543 0 : return aicpu::AICPU_ERROR_FAILED;
544 0 : }
545 2 : return aicpu::AICPU_ERROR_NONE;
546 : }
547 :
548 3 : aicpu::status_t GetThreadCtxInfo(aicpu::CtxType type, const std::string& key, std::string& value)
549 : {
550 3 : if (key.empty()) {
551 1 : AICPUE_LOGE("Get thread context failed, context type[%d], key is empty", static_cast<int32_t>(type));
552 1 : return aicpu::AICPU_ERROR_FAILED;
553 : }
554 :
555 2 : auto& ctx = GetThreadCtx(type, g_threadIndex);
556 2 : const auto iter = ctx.find(key);
557 2 : if (iter != ctx.end()) {
558 1 : value = iter->second;
559 1 : return aicpu::AICPU_ERROR_NONE;
560 : }
561 1 : AICPUE_LOGE(
562 : "Get thread context failed, context type[%d], no such key[%s]", static_cast<int32_t>(type), key.c_str());
563 1 : return aicpu::AICPU_ERROR_FAILED;
564 : }
565 :
566 4 : aicpu::status_t RemoveThreadCtxInfo(aicpu::CtxType type, const std::string& key)
567 : {
568 4 : auto& ctx = GetThreadCtx(type, g_threadIndex);
569 4 : const auto iter = ctx.find(key);
570 4 : if (iter != ctx.end()) {
571 2 : (void)ctx.erase(iter);
572 2 : return aicpu::AICPU_ERROR_NONE;
573 : }
574 2 : AICPUE_LOGE(
575 : "Remove thread context failed, context type[%d], no such key[%s]", static_cast<int32_t>(type), key.c_str());
576 2 : return aicpu::AICPU_ERROR_FAILED;
577 : }
578 :
579 1 : uint32_t AicpuGetBlockIdx() { return g_blockIdx; }
580 :
581 1 : uint32_t AicpuGetBlockNum() { return g_blockNum; }
582 :
583 1 : uint64_t AicpuGetTaskId() { return g_streamAndTaskId.taskId; }
584 :
585 1 : uint32_t AicpuGetStreamId() { return g_streamAndTaskId.streamId; }
586 :
587 3 : int32_t AicpuGetDfxInfo(uint64_t* dfxInfoAddr)
588 : {
589 3 : if (dfxInfoAddr == nullptr) {
590 1 : return static_cast<int32_t>(AicpuDfxInfoRet::INVALID_PARAM);
591 : }
592 2 : std::lock_guard<std::mutex> lock(g_dfxStorer.dfxInfoMutex);
593 2 : if (!g_dfxStorer.dfxInfoSet) {
594 1 : return static_cast<int32_t>(AicpuDfxInfoRet::NOT_SET);
595 : }
596 1 : *dfxInfoAddr = g_dfxStorer.dfxInfoAddr;
597 1 : return static_cast<int32_t>(AicpuDfxInfoRet::OK);
598 2 : }
|