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 :
11 : #include "aicpusd_interface_process.h"
12 : #include <securec.h>
13 : #include <unistd.h>
14 : #include "aicpusd_info.h"
15 : #include "ascend_hal.h"
16 : #include "aicpusd_event_manager.h"
17 : #include "aicpusd_event_process.h"
18 : #include "aicpusd_threads_process.h"
19 : #include "dump_task.h"
20 : #include "aicpu_context.h"
21 : #include "aicpusd_common.h"
22 : #include "aicpusd_context.h"
23 : #include "aicpusd_util.h"
24 : #include "aicpu_sched/common/aicpu_task_struct.h"
25 : #include "aicpusd_resource_manager.h"
26 : #include "aicpusd_drv_manager.h"
27 : #include "aicpusd_model_execute.h"
28 : #include "aicpu_engine.h"
29 : #include "aicpusd_profiler.h"
30 : #include "aicpusd_monitor.h"
31 : #include "aicpusd_worker.h"
32 : #include "aicpusd_cust_so_manager.h"
33 : #include "aicpusd_sub_module_interface.h"
34 :
35 : namespace {
36 : const std::map<EVENT_ID, SCHEDULE_PRIORITY> eventPriority = {
37 : {EVENT_ID::EVENT_TS_CTRL_MSG, SCHEDULE_PRIORITY::PRIORITY_LEVEL0}
38 : };
39 :
40 : // first array index
41 : constexpr size_t FIRST_INDEX = 0LU;
42 : }
43 : namespace AicpuSchedule {
44 : /**
45 : * @ingroup AicpuScheduleCore
46 : * @brief it is used to construct a object of AicpuScheduleCore.
47 : */
48 1 : AicpuScheduleInterface::AicpuScheduleInterface()
49 1 : : profilingMode_(PROFILING_CLOSE),
50 1 : noThreadFlag_(true),
51 1 : initFlag_(false),
52 1 : eventBitMap_(0U),
53 1 : runMode_(aicpu::AicpuRunMode::THREAD_MODE) {}
54 :
55 : /**
56 : * @ingroup AicpuScheduleCore
57 : * @brief it is used to destructor a object of AicpuScheduleCore.
58 : */
59 1 : AicpuScheduleInterface::~AicpuScheduleInterface() {}
60 :
61 22 : AicpuScheduleInterface &AicpuScheduleInterface::GetInstance()
62 : {
63 22 : static AicpuScheduleInterface instance;
64 22 : return instance;
65 : }
66 :
67 4 : int32_t AicpuScheduleInterface::BindHostPid(const std::string &pidSign, const uint32_t vfId) const
68 : {
69 4 : const AicpuPlat mode = (runMode_ == aicpu::AicpuRunMode::PROCESS_PCIE_MODE)
70 4 : ? AicpuPlat::AICPU_ONLINE_PLAT : AicpuPlat::AICPU_OFFLINE_PLAT;
71 4 : int32_t ret = 0;
72 4 : ret = AicpuDrvManager::GetInstance().BindHostPid(pidSign, static_cast<int32_t>(mode),
73 : vfId, DEVDRV_PROCESS_CP1);
74 4 : if (runMode_ == aicpu::AicpuRunMode::THREAD_MODE) {
75 4 : int32_t getRet = 0;
76 4 : getRet = AicpuDrvManager::GetInstance().BindHostPid(pidSign, static_cast<int32_t>(mode),
77 : vfId, DEVDRV_PROCESS_CP2);
78 4 : aicpusd_warn("BindHostPid DEVDRV_PROCESS_CP2 ret[%d]", getRet);
79 : }
80 4 : return ret;
81 : }
82 :
83 : /**
84 : * @ingroup AicpuScheduleInterface
85 : * @brief it use to initialize aicpu schedule.
86 : * @param [in] deviceId
87 : * @param [in] hostPid : the process id of host
88 : * @param [in] pidSign : the signature of pid ,it is used in drv.
89 : * @param [in] profilingMode
90 : * @param [in] vfId : vf id
91 : * @param [in] isOnline: true-process mode; false-thread mode
92 : * @return AICPU_SCHEDULE_OK: success, other: error code
93 : */
94 11 : int32_t AicpuScheduleInterface::InitAICPUScheduler(const std::vector<uint32_t> &deviceVec,
95 : const pid_t hostPid,
96 : const std::string &pidSign,
97 : const uint32_t profilingMode,
98 : const uint32_t vfId,
99 : const bool isOnline,
100 : const std::string &hostProcName)
101 : {
102 11 : const std::unique_lock<std::mutex> lockForInit(mutexForInit_);
103 11 : if (initFlag_) {
104 2 : aicpusd_warn("Aicpu schedule is already init.");
105 2 : return AICPU_SCHEDULE_OK;
106 : }
107 9 : noThreadFlag_ = false;
108 9 : if (deviceVec.empty()) {
109 1 : aicpusd_err("device vector is empty");
110 1 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
111 : }
112 : // get aicpu run mode
113 8 : const int32_t getRet = GetCurrentRunMode(isOnline);
114 8 : if (getRet != AICPU_SCHEDULE_OK) {
115 1 : aicpusd_err("getRet is not ok:%u", getRet);
116 1 : return getRet;
117 : }
118 7 : if (AicpuDrvManager::GetInstance().InitDrvMgr(deviceVec, hostPid, vfId,
119 7 : true, runMode_, hostProcName) != AICPU_SCHEDULE_OK) {
120 1 : aicpusd_err("Failed to init aicpu drv manager");
121 1 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
122 : }
123 :
124 6 : DeployContext deployCtx = DeployContext::DEVICE;
125 6 : const StatusCode status = GetAicpuDeployContext(deployCtx);
126 6 : if (status != AICPU_SCHEDULE_OK) {
127 1 : aicpusd_err("Get current deploy ctx failed.");
128 1 : return status;
129 : }
130 5 : const bool cpuMode = GetCpuMode();
131 5 : if ((deployCtx == DeployContext::DEVICE) && (!cpuMode)) {
132 5 : const int32_t bindPidRet = BindHostPid(pidSign, vfId);
133 5 : if (bindPidRet != AICPU_SCHEDULE_OK) {
134 1 : aicpusd_err("check bind pid sign failed, ret[%d].", bindPidRet);
135 1 : return bindPidRet;
136 : }
137 4 : aicpusd_info("Bind pid sign success");
138 : }
139 4 : const uint32_t groupId = CP_DEFAULT_GROUP_ID;
140 :
141 4 : int32_t ret = AicpuDrvManager::GetInstance().InitDrvSchedModule(groupId, eventPriority);
142 4 : if (ret != DRV_ERROR_NONE) {
143 1 : aicpusd_err("Failed to init the drv schedule module, ret[%d].", ret);
144 1 : return AICPU_SCHEDULE_ERROR_DRV_ERR;
145 : }
146 : const auto retCp =
147 3 : ComputeProcess::GetInstance().Start(deviceVec, hostPid, pidSign, profilingMode, vfId, runMode_);
148 3 : if (retCp != AICPU_SCHEDULE_OK) {
149 1 : aicpusd_err("Compute process start failed, ret[%d].", retCp);
150 1 : ComputeProcess::GetInstance().Stop();
151 1 : return AICPU_SCHEDULE_ERROR_INIT_CP_FAILED;
152 : }
153 :
154 2 : aicpusd_info("Successfully started compute process, deviceId[%u], hostPid[%d], runMode[%u]",
155 : deviceVec[FIRST_INDEX], hostPid, runMode_);
156 2 : initFlag_ = true;
157 2 : return AICPU_SCHEDULE_OK;
158 11 : }
159 :
160 9 : int32_t AicpuScheduleInterface::GetCurrentRunMode(const bool isOnline)
161 : {
162 9 : if (!isOnline) {
163 7 : runMode_ = aicpu::AicpuRunMode::THREAD_MODE;
164 7 : aicpusd_run_info("Current aicpu mode is offline (call by api).");
165 7 : return AICPU_SCHEDULE_OK;
166 : }
167 2 : aicpusd_err("Datadump only support thread mode.");
168 2 : return AICPU_SCHEDULE_ERROR_INIT_CP_FAILED;
169 : }
170 :
171 2 : int32_t AicpuScheduleInterface::DettachDeviceInDestroy(const std::vector<uint32_t> &deviceVec) const
172 : {
173 3 : for (size_t i = 0LU; i < deviceVec.size(); i++) {
174 2 : const drvError_t ret = halEschedDettachDevice(deviceVec[i]);
175 2 : if (ret != DRV_ERROR_NONE) {
176 1 : aicpusd_err("Failed to detach device[%u], result[%d].", deviceVec[i], ret);
177 1 : return ret;
178 : }
179 1 : aicpusd_info("Successfully detached AICPU scheduler, device[%d].", deviceVec[i]);
180 : }
181 1 : return AICPU_SCHEDULE_OK;
182 : }
183 : }
|