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() { return g_curProfCtx; }
120 :
121 14 : status_t InitTaskMonitorContext(uint32_t aicpuCoreCnt)
122 : {
123 14 : if (aicpuCoreCnt == 0U) {
124 1 : AICPUE_LOGE("invalid aicpu core count[%u]", aicpuCoreCnt);
125 1 : return AICPU_ERROR_FAILED;
126 : }
127 13 : g_aicpuCoreCnt = aicpuCoreCnt;
128 13 : AICPUE_LOGI("aicpu core count[%u]", aicpuCoreCnt);
129 37 : g_opsname.reset(new (std::nothrow) std::string[aicpuCoreCnt]);
130 13 : if (g_opsname == nullptr) {
131 0 : AICPUE_LOGE("malloc ops name momery for task monitor failed");
132 0 : return AICPU_ERROR_FAILED;
133 : }
134 37 : for (uint32_t idx = 0U; idx < aicpuCoreCnt; ++idx) {
135 24 : g_opsname[static_cast<size_t>(idx)] = "null";
136 : }
137 13 : return AICPU_ERROR_NONE;
138 : }
139 :
140 30 : status_t SetAicpuThreadIndex(uint32_t threadIndex)
141 : {
142 30 : g_threadIndex = threadIndex;
143 30 : return AICPU_ERROR_NONE;
144 : }
145 :
146 162 : uint32_t GetAicpuThreadIndex() { return g_threadIndex; }
147 :
148 247 : status_t SetOpname(const std::string& opname)
149 : {
150 247 : if ((g_opsname != nullptr) && (g_threadIndex < g_aicpuCoreCnt)) {
151 236 : AICPUE_LOGI("set op name to %s for thread[%u]", opname.c_str(), g_threadIndex);
152 236 : g_opsname[static_cast<size_t>(g_threadIndex)] = opname;
153 243 : return AICPU_ERROR_NONE;
154 : }
155 : // maintenance function, if failed just print event log
156 7 : AICPUE_RUN_LOGW(
157 : "set op name[%s] failed, thread index[%u] should be less than total aicpu core count[%u],"
158 : " and ops name array addr cannot null",
159 : opname.c_str(), g_threadIndex, g_aicpuCoreCnt);
160 7 : return AICPU_ERROR_NONE;
161 : }
162 :
163 20 : status_t GetOpname(uint32_t threadIndex, std::string& opname)
164 : {
165 20 : if ((g_opsname != nullptr) && (threadIndex < g_aicpuCoreCnt)) {
166 19 : opname = g_opsname[static_cast<size_t>(threadIndex)];
167 19 : return AICPU_ERROR_NONE;
168 : }
169 1 : opname = "null";
170 : // maintenance function, if failed just print event log
171 1 : AICPUE_RUN_LOGW(
172 : "get op name failed, thread index[%u] should be less than total aicpu core count[%u],"
173 : " and ops name array addr cannot null",
174 : g_threadIndex, g_aicpuCoreCnt);
175 1 : return AICPU_ERROR_NONE;
176 : }
177 :
178 136 : status_t SetTaskAndStreamId(uint64_t taskId, uint32_t streamId)
179 : {
180 136 : g_streamAndTaskId.taskId = taskId;
181 136 : g_streamAndTaskId.streamId = streamId;
182 136 : AICPUE_LOGI("Set taskId:[%lu] and streamId:[%u] success.", taskId, streamId);
183 137 : return AICPU_ERROR_NONE;
184 : }
185 :
186 36 : status_t GetTaskAndStreamId(uint64_t& taskId, uint32_t& streamId)
187 : {
188 36 : taskId = g_streamAndTaskId.taskId;
189 36 : streamId = g_streamAndTaskId.streamId;
190 36 : AICPUE_LOGI("Get taskId:[%lu] and streamId:[%u] success.", taskId, streamId);
191 36 : return AICPU_ERROR_NONE;
192 : }
193 :
194 123 : status_t SetBlockIdxAndBlockNum(uint32_t blockIdx, uint32_t blockNum)
195 : {
196 123 : g_blockIdx = blockIdx;
197 123 : g_blockNum = blockNum;
198 123 : AICPUE_LOGI("Set blockIdx:[%u] and blockNum:[%u] success.", blockIdx, blockNum);
199 123 : return AICPU_ERROR_NONE;
200 : }
201 :
202 1 : uint32_t GetBlockIdx() { return g_blockIdx; }
203 :
204 1 : uint32_t GetBlockNum() { return g_blockNum; }
205 :
206 23 : status_t SetAicpuRunMode(uint32_t runMode)
207 : {
208 23 : g_runMode = runMode;
209 23 : AICPUE_LOGI("Set runMode:[%u] success.", runMode);
210 23 : return AICPU_ERROR_NONE;
211 : }
212 :
213 63 : status_t GetAicpuRunMode(uint32_t& runMode)
214 : {
215 63 : runMode = g_runMode;
216 63 : return AICPU_ERROR_NONE;
217 : }
218 :
219 275 : status_t SetThreadLocalCtx(const std::string& key, const std::string& value)
220 : {
221 275 : if (key.empty()) {
222 1 : AICPUE_LOGE("set thread local context failed, key is empty");
223 1 : return AICPU_ERROR_FAILED;
224 : }
225 : try {
226 274 : g_threadLocalAicpuCtx[key] = value;
227 0 : } catch (std::exception& e) {
228 0 : AICPUE_LOGE("set thread local context failed, %s", e.what());
229 0 : return AICPU_ERROR_FAILED;
230 0 : }
231 280 : return AICPU_ERROR_NONE;
232 : }
233 :
234 144 : status_t GetThreadLocalCtx(const std::string& key, std::string& value)
235 : {
236 144 : if (key.empty()) {
237 1 : AICPUE_LOGE("get thread local context failed, key is empty");
238 1 : return AICPU_ERROR_FAILED;
239 : }
240 143 : const auto iter = g_threadLocalAicpuCtx.find(key);
241 139 : if (iter != g_threadLocalAicpuCtx.end()) {
242 129 : value = iter->second;
243 132 : return AICPU_ERROR_NONE;
244 : }
245 11 : AICPUE_LOGW("get thread local context failed, no such key[%s]", key.c_str());
246 11 : return AICPU_ERROR_FAILED;
247 : }
248 :
249 2 : status_t RemoveThreadLocalCtx(const std::string& key)
250 : {
251 2 : const auto iter = g_threadLocalAicpuCtx.find(key);
252 2 : if (iter != g_threadLocalAicpuCtx.end()) {
253 1 : (void)g_threadLocalAicpuCtx.erase(iter);
254 1 : return AICPU_ERROR_NONE;
255 : }
256 1 : AICPUE_LOGE("remove thread local context failed, no such key[%s]", key.c_str());
257 1 : return AICPU_ERROR_FAILED;
258 : }
259 :
260 3 : const std::map<std::string, std::string>& GetAllThreadCtxInfo(aicpu::CtxType type, uint32_t threadIndex)
261 : {
262 3 : AICPUE_LOGI("Get all thread ctx info begin, thread index:%u", threadIndex);
263 3 : auto& ctx = GetThreadCtx(type, threadIndex);
264 3 : return ctx;
265 : }
266 :
267 4 : status_t RegisterEventCallback(
268 : const uint32_t eventId, const uint32_t subeventId, std::function<void(void*)> func, const bool isNeedClear)
269 : {
270 4 : const std::lock_guard<std::mutex> locker(g_funcMapMutex);
271 4 : std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>& subMap = g_funcMap[eventId];
272 4 : const auto it = subMap.insert({subeventId, {func, isNeedClear}});
273 4 : if (!it.second) {
274 1 : AICPUE_LOGE(
275 : "register event call function failed, repulicate register callback "
276 : "function by eventId[%u] subeventId[%u]",
277 : eventId, subeventId);
278 1 : return AICPU_ERROR_FAILED;
279 : }
280 3 : return AICPU_ERROR_NONE;
281 4 : }
282 :
283 4 : status_t DoEventCallback(const uint32_t eventId, const uint32_t subeventId, void* const param)
284 : {
285 4 : const std::lock_guard<std::mutex> locker(g_funcMapMutex);
286 4 : const auto iter = g_funcMap.find(eventId);
287 4 : if (iter == g_funcMap.end()) {
288 2 : AICPUE_RUN_LOGW(
289 : "do event callback function failed, cannot find callback function by "
290 : "eventId[%u] subeventId[%u]",
291 : eventId, subeventId);
292 2 : return AICPU_ERROR_FAILED;
293 : }
294 :
295 2 : std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>& subMap = iter->second;
296 2 : const auto subIter = subMap.find(subeventId);
297 2 : if (subIter == subMap.end()) {
298 1 : AICPUE_RUN_LOGW(
299 : "do event callback function failed, cannot find callback function by "
300 : "eventId[%u] subeventId[%u]",
301 : eventId, subeventId);
302 1 : return AICPU_ERROR_FAILED;
303 : }
304 1 : ((subIter->second).first)(param);
305 : // erase func after call
306 1 : if ((subIter->second).second) {
307 1 : (void)subMap.erase(subIter);
308 : }
309 1 : return AICPU_ERROR_NONE;
310 4 : }
311 :
312 3 : status_t UnRegisterCallback(const uint32_t eventId, const uint32_t subeventId)
313 : {
314 3 : const std::lock_guard<std::mutex> locker(g_funcMapMutex);
315 3 : const auto iter = g_funcMap.find(eventId);
316 3 : if (iter == g_funcMap.end()) {
317 1 : AICPUE_RUN_LOGW(
318 : "skip unregister event callback function, cannot find callback function by eventId[%u] "
319 : "subeventId[%u]",
320 : eventId, subeventId);
321 1 : return AICPU_ERROR_NONE;
322 : }
323 :
324 2 : std::map<uint32_t, std::pair<std::function<void(void*)>, bool>>& subMap = iter->second;
325 2 : const auto subIter = subMap.find(subeventId);
326 2 : if (subIter == subMap.end()) {
327 1 : AICPUE_RUN_LOGW(
328 : "skip unregister event callback function, cannot find callback function by eventId[%u] "
329 : "subeventId[%u]",
330 : eventId, subeventId);
331 1 : return AICPU_ERROR_NONE;
332 : }
333 1 : (void)subMap.erase(subIter);
334 1 : return AICPU_ERROR_NONE;
335 3 : }
336 :
337 : using AicpuStreamDvpp = struct {
338 : uint8_t* dvppBuff;
339 : uint64_t dvppBuffLen;
340 : int32_t channelId;
341 : };
342 :
343 : static pthread_rwlock_t g_streamAndChannelMapLock[AICPU_DVPP_CHL_BUTT] = {
344 : PTHREAD_RWLOCK_INITIALIZER, 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(
373 : const AicpuDvppChlType chlType, const uint32_t streamId, 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() { return g_uniqueVfId; }
503 :
504 30 : void SetUniqueVfId(const uint32_t uniqueVfId) { g_uniqueVfId = uniqueVfId; }
505 :
506 15 : void SetCustAicpuSdFlag(const bool isCustAicpuSdFlag) { g_isCustAicpuSd = isCustAicpuSdFlag; }
507 :
508 136 : bool IsCustAicpuSd() { return g_isCustAicpuSd; }
509 : } // namespace aicpu
510 :
511 4 : aicpu::status_t SetThreadCtxInfo(aicpu::CtxType type, const std::string& key, const std::string& value)
512 : {
513 4 : if (key.empty()) {
514 2 : AICPUE_LOGE("Set thread context failed, context type[%d], key is empty", static_cast<int32_t>(type));
515 2 : return aicpu::AICPU_ERROR_FAILED;
516 : }
517 :
518 2 : auto& ctx = GetThreadCtx(type, g_threadIndex);
519 : try {
520 2 : ctx[key] = value;
521 0 : } catch (std::exception& aicpuExp) {
522 0 : AICPUE_LOGE("Set thread context failed, context type[%d], %s", static_cast<int32_t>(type), aicpuExp.what());
523 0 : return aicpu::AICPU_ERROR_FAILED;
524 0 : }
525 2 : return aicpu::AICPU_ERROR_NONE;
526 : }
527 :
528 3 : aicpu::status_t GetThreadCtxInfo(aicpu::CtxType type, const std::string& key, std::string& value)
529 : {
530 3 : if (key.empty()) {
531 1 : AICPUE_LOGE("Get thread context failed, context type[%d], key is empty", static_cast<int32_t>(type));
532 1 : return aicpu::AICPU_ERROR_FAILED;
533 : }
534 :
535 2 : auto& ctx = GetThreadCtx(type, g_threadIndex);
536 2 : const auto iter = ctx.find(key);
537 2 : if (iter != ctx.end()) {
538 1 : value = iter->second;
539 1 : return aicpu::AICPU_ERROR_NONE;
540 : }
541 1 : AICPUE_LOGE(
542 : "Get thread context failed, context type[%d], no such key[%s]", static_cast<int32_t>(type), key.c_str());
543 1 : return aicpu::AICPU_ERROR_FAILED;
544 : }
545 :
546 4 : aicpu::status_t RemoveThreadCtxInfo(aicpu::CtxType type, const std::string& key)
547 : {
548 4 : auto& ctx = GetThreadCtx(type, g_threadIndex);
549 4 : const auto iter = ctx.find(key);
550 4 : if (iter != ctx.end()) {
551 2 : (void)ctx.erase(iter);
552 2 : return aicpu::AICPU_ERROR_NONE;
553 : }
554 2 : AICPUE_LOGE(
555 : "Remove thread context failed, context type[%d], no such key[%s]", static_cast<int32_t>(type), key.c_str());
556 2 : return aicpu::AICPU_ERROR_FAILED;
557 : }
558 :
559 1 : uint32_t AicpuGetBlockIdx() { return g_blockIdx; }
560 :
561 1 : uint32_t AicpuGetBlockNum() { return g_blockNum; }
562 :
563 1 : uint64_t AicpuGetTaskId() { return g_streamAndTaskId.taskId; }
564 :
565 1 : uint32_t AicpuGetStreamId() { return g_streamAndTaskId.streamId; }
|