Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "hdc_message_builder.h"
11 :
12 : #include <string>
13 :
14 : #include "capability_manager.h"
15 : #include "env_internal_api.h"
16 : #include "tsd_log.h"
17 : #include "tsd_util_func.h"
18 :
19 : namespace tsd {
20 : namespace {
21 : // Compute the tsdclient capability bitmap. Side-effect (TSD_INFO log) is
22 : // intentionally preserved so the produced HDCMessage and surrounding log
23 : // output are byte-equivalent to the legacy implementation.
24 26 : uint32_t BuildTsdClientCapabilityLevel()
25 : {
26 26 : uint32_t curSupport = 0U;
27 26 : TSD_BITMAP_SET(curSupport, TSDCLIENT_SUPPORT_NEW_ERRORCODE);
28 26 : TSD_INFO("Set tsdclient capability level, value is [%u].", curSupport);
29 26 : return curSupport;
30 : }
31 :
32 : // Set both the modulo device id (used for routing) and the real device id
33 : // (used for callback verification) carried by every per-device HDC message.
34 173 : void SetDeviceIds(HDCMessage& msg, const MessageContext& ctx)
35 : {
36 173 : msg.set_device_id(ctx.logicDeviceId % PER_OS_CHIP_NUM);
37 : // 传递真实的deviceId在回调的时候做校验使用,替代reqId
38 173 : msg.set_real_device_id(ctx.logicDeviceId);
39 173 : }
40 :
41 : // Populate proc_sign_pid with the process tgid, and optionally the sign string.
42 188 : TSD_StatusT SetProcSignPid(HDCMessage& msg, const MessageContext& ctx, const bool withSign = false)
43 : {
44 188 : ProcessSignPid* const signPid = msg.mutable_proc_sign_pid();
45 188 : if (signPid == nullptr) {
46 0 : TSD_ERROR("signPid is null.");
47 0 : return TSD_INTERNAL_ERROR;
48 : }
49 188 : signPid->set_proc_pid(static_cast<uint32_t>(ctx.procSign.tgid));
50 188 : if (withSign) {
51 224 : signPid->set_proc_sign(std::string(ctx.procSign.sign));
52 : }
53 188 : TSD_RUN_INFO(
54 : "[HdcMessageBuilder] tsd get process sign successfully, procpid[%u]", static_cast<uint32_t>(ctx.procSign.tgid));
55 188 : return TSD_OK;
56 : }
57 :
58 : // Append the sub-process entries described by ctx (pid list + optional type
59 : // list) to the given SubProcStatus repeated field, keeping the type list and
60 : // the status list parallel.
61 20 : void FillSubProcList(
62 : HDCMessage& msg, const MessageContext& ctx, google::protobuf::RepeatedPtrField<SubProcStatus>* statusList)
63 : {
64 55 : for (size_t index = 0U; index < ctx.subProcPidList.size(); index++) {
65 35 : SubProcStatus* const curStatus = statusList->Add();
66 35 : if (index < ctx.subProcTypeList.size()) {
67 23 : msg.add_sub_proc_type_list(ctx.subProcTypeList[index]);
68 : }
69 35 : curStatus->set_sub_proc_pid(ctx.subProcPidList[index]);
70 : }
71 20 : }
72 : } // namespace
73 :
74 26 : TSD_StatusT HdcMessageBuilder::BuildOpen(HDCMessage& hdcMsg, const MessageContext& ctx)
75 : {
76 26 : hdcMsg.set_rank_size(ctx.rankSize);
77 26 : hdcMsg.set_start_hccp(ctx.startHccp);
78 26 : hdcMsg.set_start_cp(ctx.startCp);
79 26 : hdcMsg.set_profiling_mode(ctx.profilingMode);
80 26 : SetDeviceIds(hdcMsg, ctx);
81 26 : LogLevel* const level = hdcMsg.mutable_log_level();
82 26 : if (level == nullptr) {
83 0 : TSD_ERROR("mutable log level error");
84 0 : return TSD_INTERNAL_ERROR;
85 : }
86 26 : level->set_log_level(ctx.logLevel);
87 26 : CcecpuLogLevel* const ccecpuLogLevel = hdcMsg.mutable_ccecpu_log_level();
88 26 : if (ccecpuLogLevel != nullptr) {
89 26 : ccecpuLogLevel->set_ccecpu_log_level(ctx.ccecpuLogLevel);
90 : }
91 26 : AicpuLogLevel* const aicpuLogLevel = hdcMsg.mutable_aicpu_log_level();
92 26 : if (aicpuLogLevel != nullptr) {
93 26 : aicpuLogLevel->set_aicpu_log_level(ctx.aicpuLogLevel);
94 : }
95 26 : const TSD_StatusT signRet = SetProcSignPid(hdcMsg, ctx, true);
96 26 : if (signRet != TSD_OK) {
97 0 : return signRet;
98 : }
99 26 : hdcMsg.set_check_code(ctx.aicpuKernelCheckCode);
100 26 : hdcMsg.set_extendpkg_check_code(ctx.aicpuExtendKernelCheckCode);
101 26 : hdcMsg.set_ascendcpppkg_check_code(ctx.ascendcppCheckCode);
102 26 : hdcMsg.set_type(HDCMessage::TSD_START_PROC_MSG);
103 26 : hdcMsg.set_device_mode(ctx.aicpuDeviceMode);
104 26 : hdcMsg.set_aicpu_sched_mode(static_cast<uint32_t>(ctx.aicpuSchedMode));
105 26 : const uint32_t tsdclientCapabilityLevel = BuildTsdClientCapabilityLevel();
106 26 : hdcMsg.set_tsdclient_capability_level(tsdclientCapabilityLevel);
107 26 : std::string aicpuPath;
108 26 : GetEnvFromMmSys(MM_ENV_ASCEND_AICPU_PATH, "ASCEND_AICPU_PATH", aicpuPath);
109 26 : AscendAicpuPath* const ascendAicpuPath = hdcMsg.mutable_ascend_aicpu_path();
110 26 : if (ascendAicpuPath == nullptr) {
111 0 : TSD_ERROR("mutable ascend aicpu path error");
112 0 : return TSD_INTERNAL_ERROR;
113 : }
114 : ascendAicpuPath->set_ascend_aicpu_path(aicpuPath);
115 26 : return TSD_OK;
116 26 : }
117 :
118 23 : TSD_StatusT HdcMessageBuilder::BuildClose(HDCMessage& msg, const MessageContext& ctx)
119 : {
120 23 : SetDeviceIds(msg, ctx);
121 23 : msg.set_type(HDCMessage::TSD_CLOSE_PROC_MSG);
122 23 : msg.set_rank_size(ctx.rankSize);
123 23 : return SetProcSignPid(msg, ctx);
124 : }
125 :
126 5 : TSD_StatusT HdcMessageBuilder::BuildUpdateProfiling(HDCMessage& msg, const MessageContext& ctx)
127 : {
128 5 : SetDeviceIds(msg, ctx);
129 5 : msg.set_type(HDCMessage::TSD_UPDATE_PROIFILING_MSG);
130 5 : msg.set_profiling_mode(ctx.profilingMode);
131 5 : msg.set_rank_size(ctx.rankSize);
132 5 : return SetProcSignPid(msg, ctx);
133 : }
134 :
135 3 : TSD_StatusT HdcMessageBuilder::BuildOmFileDecompress(HDCMessage& msg, const MessageContext& ctx)
136 : {
137 3 : SetDeviceIds(msg, ctx);
138 3 : msg.set_type(HDCMessage::TSD_OM_PKG_DECOMPRESS_STATUS);
139 3 : msg.set_omfile_name(ctx.omfileName);
140 3 : return SetProcSignPid(msg, ctx);
141 : }
142 :
143 9 : TSD_StatusT HdcMessageBuilder::BuildPackageCheckCode(HDCMessage& msg, const MessageContext& ctx)
144 : {
145 9 : msg.set_real_device_id(ctx.logicDeviceId);
146 9 : msg.set_type(static_cast<HDCMessage::MsgType>(ctx.msgType));
147 9 : msg.set_check_code(ctx.checkCode);
148 9 : msg.set_before_send_pkg(ctx.beforeSendPkg);
149 9 : return SetProcSignPid(msg, ctx, true);
150 : }
151 :
152 65 : TSD_StatusT HdcMessageBuilder::BuildCapability(HDCMessage& msg, const MessageContext& ctx)
153 : {
154 65 : const CapabilitySpec* spec = FindCapabilitySpec(ctx.capabilityType);
155 65 : if (spec != nullptr) {
156 63 : msg.set_type(spec->msgType);
157 : }
158 65 : SetDeviceIds(msg, ctx);
159 65 : return SetProcSignPid(msg, ctx);
160 : }
161 :
162 7 : TSD_StatusT HdcMessageBuilder::BuildCloseSubProc(HDCMessage& msg, const MessageContext& ctx)
163 : {
164 7 : SetDeviceIds(msg, ctx);
165 7 : msg.set_type(HDCMessage::TSD_CLOSE_SUB_PROC);
166 7 : msg.set_close_sub_proc_pid(ctx.closeSubProcPid);
167 7 : return SetProcSignPid(msg, ctx);
168 : }
169 :
170 3 : TSD_StatusT HdcMessageBuilder::BuildRemoveFile(HDCMessage& msg, const MessageContext& ctx)
171 : {
172 3 : SetDeviceIds(msg, ctx);
173 3 : msg.set_type(HDCMessage::TSD_REMOVE_FILE);
174 3 : msg.set_remove_file_path(ctx.removeFilePath);
175 3 : return SetProcSignPid(msg, ctx);
176 : }
177 :
178 8 : TSD_StatusT HdcMessageBuilder::BuildCannHsCheckCode(HDCMessage& msg, const MessageContext& ctx)
179 : {
180 8 : msg.set_real_device_id(ctx.logicDeviceId);
181 8 : msg.set_type(HDCMessage::TSD_GET_DEVICE_CANN_HS_CHECKCODE);
182 8 : msg.set_package_max_process_time(ctx.packageMaxProcessTime);
183 8 : msg.set_package_worker_type(ctx.packageWorkerType);
184 8 : msg.set_package_type(ctx.packageType);
185 8 : SinkPackageHashCodeInfo* const pkgHostInfo = msg.add_package_hash_code_list();
186 8 : if (pkgHostInfo == nullptr) {
187 0 : TSD_ERROR("add package hash code list error");
188 0 : return TSD_INTERNAL_ERROR;
189 : }
190 8 : pkgHostInfo->set_package_name(ctx.packageName);
191 8 : pkgHostInfo->set_hash_code(ctx.hashCode);
192 8 : return TSD_OK;
193 : }
194 :
195 11 : TSD_StatusT HdcMessageBuilder::BuildGetSubProcStatus(HDCMessage& msg, const MessageContext& ctx)
196 : {
197 11 : SetDeviceIds(msg, ctx);
198 11 : msg.set_type(HDCMessage::TSD_GET_SUB_PROC_STATUS);
199 11 : const TSD_StatusT ret = SetProcSignPid(msg, ctx);
200 11 : if (ret != TSD_OK) {
201 0 : return ret;
202 : }
203 11 : FillSubProcList(msg, ctx, msg.mutable_sub_proc_status_list());
204 11 : return TSD_OK;
205 : }
206 :
207 9 : TSD_StatusT HdcMessageBuilder::BuildCloseSubProcList(HDCMessage& msg, const MessageContext& ctx)
208 : {
209 9 : SetDeviceIds(msg, ctx);
210 9 : msg.set_type(HDCMessage::TSD_CLOSE_SUB_PROC_LIST);
211 9 : const TSD_StatusT ret = SetProcSignPid(msg, ctx);
212 9 : if (ret != TSD_OK) {
213 0 : return ret;
214 : }
215 9 : FillSubProcList(msg, ctx, msg.mutable_close_sub_list());
216 9 : return TSD_OK;
217 : }
218 :
219 21 : TSD_StatusT HdcMessageBuilder::BuildCommonOpen(HDCMessage& msg, const MessageContext& ctx)
220 : {
221 21 : HelperSubProcess* const subProcessInfo = msg.mutable_helper_sub_proc();
222 21 : if (subProcessInfo == nullptr) {
223 0 : TSD_ERROR("helper_sub_proc is null.");
224 0 : return TSD_INTERNAL_ERROR;
225 : }
226 21 : subProcessInfo->set_process_type(ctx.subProcOpenType);
227 21 : if (ctx.hasSubProcFilePath) {
228 12 : subProcessInfo->set_file_path(ctx.subProcFilePath);
229 : }
230 32 : for (const std::pair<std::string, std::string>& env : ctx.subProcEnvList) {
231 11 : EnvPara* const evnParam = subProcessInfo->add_env_list();
232 11 : if (evnParam == nullptr) {
233 0 : TSD_ERROR("add env list error");
234 0 : return TSD_INTERNAL_ERROR;
235 : }
236 11 : evnParam->set_env_name(env.first);
237 11 : evnParam->set_env_value(env.second);
238 : }
239 38 : for (const std::string& extParam : ctx.subProcExtParamList) {
240 17 : subProcessInfo->add_ext_param_list(extParam);
241 : }
242 21 : if (!ctx.ascendInstallPath.empty()) {
243 15 : msg.set_ascend_install_path(ctx.ascendInstallPath);
244 : }
245 21 : if (ctx.withSubProcLogLevel) {
246 7 : LogLevel* const level = msg.mutable_log_level();
247 7 : if (level != nullptr) {
248 7 : level->set_log_level(ctx.logLevel);
249 : }
250 : }
251 21 : msg.set_type(HDCMessage::TSD_OPEN_SUB_PROC);
252 21 : SetDeviceIds(msg, ctx);
253 21 : return SetProcSignPid(msg, ctx, true);
254 : }
255 :
256 2 : TSD_StatusT HdcMessageBuilder::BuildCheckPackageRetry(HDCMessage& msg, const MessageContext& ctx)
257 : {
258 2 : msg.set_real_device_id(ctx.logicDeviceId);
259 2 : msg.set_type(HDCMessage::TSD_CHECK_PACKAGE_RETRY);
260 2 : msg.set_check_code(ctx.checkCode);
261 2 : msg.set_package_type(ctx.packageType);
262 2 : msg.set_wait_flag(ctx.waitFlag);
263 2 : return TSD_OK;
264 : }
265 :
266 13 : TSD_StatusT HdcMessageBuilder::BuildCheckPackage(HDCMessage& msg, const MessageContext& ctx)
267 : {
268 13 : msg.set_real_device_id(ctx.logicDeviceId);
269 13 : msg.set_type(HDCMessage::TSD_CHECK_PACKAGE);
270 13 : msg.set_asan(ctx.asan);
271 13 : if (ctx.checkCode != 0U) {
272 3 : msg.set_check_code(ctx.checkCode);
273 : }
274 13 : if (ctx.extendpkgCheckCode != 0U) {
275 3 : msg.set_extendpkg_check_code(ctx.extendpkgCheckCode);
276 : }
277 13 : if (ctx.ascendcppCheckCode != 0U) {
278 3 : msg.set_ascendcpppkg_check_code(ctx.ascendcppCheckCode);
279 : }
280 13 : return TSD_OK;
281 : }
282 :
283 6 : TSD_StatusT HdcMessageBuilder::BuildUpdatePackageConfig(HDCMessage& msg, const MessageContext& ctx)
284 : {
285 6 : msg.set_real_device_id(ctx.logicDeviceId);
286 6 : msg.set_type(HDCMessage::TSD_UPDATE_PACKAGE_PROCESS_CONFIG);
287 6 : return SetProcSignPid(msg, ctx, false);
288 : }
289 :
290 7 : TSD_StatusT HdcMessageBuilder::BuildNormalCheckCode(HDCMessage& msg, const MessageContext& ctx)
291 : {
292 7 : msg.set_real_device_id(ctx.logicDeviceId);
293 7 : msg.set_type(HDCMessage::TSD_GET_DEVICE_PACKAGE_CHECKCODE_NORMAL);
294 7 : SinkPackageHashCodeInfo* const pkgHostInfo = msg.add_package_hash_code_list();
295 7 : if (pkgHostInfo == nullptr) {
296 0 : TSD_ERROR("add package hash code list error");
297 0 : return TSD_INTERNAL_ERROR;
298 : }
299 7 : pkgHostInfo->set_package_name(ctx.packageName);
300 7 : pkgHostInfo->set_hash_code(ctx.hashCode);
301 7 : if (!ctx.hostPluginVersion.Empty()) {
302 3 : PluginPackageVersionInfo* const info = msg.add_host_plugin_versions();
303 3 : if (info == nullptr) {
304 0 : TSD_ERROR("add host plugin versions error");
305 0 : return TSD_INTERNAL_ERROR;
306 : }
307 3 : info->set_package_name(ctx.packageName);
308 3 : info->set_version(ctx.hostPluginVersion.version);
309 3 : info->set_timestamp(ctx.hostPluginVersion.timestamp);
310 : }
311 7 : msg.set_package_worker_type(ctx.packageWorkerType);
312 7 : msg.set_package_max_process_time(ctx.packageMaxProcessTime);
313 7 : msg.set_package_type(ctx.packageType);
314 7 : return TSD_OK;
315 : }
316 : } // namespace tsd
|