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