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 : #ifndef TSD_BASIC_COMPONENT_MESSAGE_BUILDER_HDC_MESSAGE_BUILDER_H
11 : #define TSD_BASIC_COMPONENT_MESSAGE_BUILDER_HDC_MESSAGE_BUILDER_H
12 :
13 : #include <cstdint>
14 : #include <string>
15 : #include <utility>
16 : #include <vector>
17 :
18 : #include "driver/ascend_hal_base.h" // process_sign
19 : #include "proto/tsd_message.pb.h"
20 : #include "tsd/status.h"
21 : #include "tsd/tsd_client.h" // SchedMode
22 :
23 : namespace tsd {
24 : // Pure value-type snapshot of the inputs required to assemble an HDC message.
25 : // Constructed at the call site (e.g. inside ProcessModeManager::Construct*Msg)
26 : // and consumed in the same call frame; no references, no lifetime coupling.
27 : // This struct is the single migration anchor for moving HDC message assembly
28 : // out of ProcessModeManager. Every field a message type may need is pre-embedded
29 : // here so that:
30 : // 1. ProcessModeManager fills the common, manager-derived fields exactly once
31 : // (see ProcessModeManager::BuildBaseMessageContext), avoiding the duplicated
32 : // "copy member -> message" code that exists per message type today.
33 : // 2. A new BuildXxx in HdcMessageBuilder only reads the fields it cares about;
34 : // unused fields stay value-initialized and are ignored.
35 : // Enum-typed values are stored as the raw integer already cast from the caller's
36 : // enum so this header does not pull in tsdclient-internal enum definitions.
37 : struct MessageContext {
38 : // ---- Device / process identity (common, manager-derived) ----
39 : uint32_t logicDeviceId = 0U;
40 : uint32_t rankSize = 0U;
41 : process_sign procSign = {};
42 :
43 : // ---- Profiling ----
44 : uint32_t profilingMode = 0U;
45 :
46 : // ---- Log levels ----
47 : std::string logLevel;
48 : std::string ccecpuLogLevel;
49 : std::string aicpuLogLevel;
50 :
51 : // ---- Package host check codes ----
52 : uint32_t aicpuKernelCheckCode = 0U;
53 : uint32_t aicpuExtendKernelCheckCode = 0U;
54 : uint32_t ascendcppCheckCode = 0U;
55 :
56 : // ---- AICPU / scheduler ----
57 : uint32_t aicpuDeviceMode = 0U;
58 : SchedMode aicpuSchedMode = {};
59 :
60 : // ---- Queue schedule (QS) ----
61 : std::string qsInitGroupName;
62 : uint64_t schedPolicy = 0UL;
63 :
64 : // ---- Start flags ----
65 : bool startHccp = false;
66 : bool startCp = false;
67 : bool startQs = false;
68 :
69 : // ---- Paths / misc flags ----
70 : std::string ascendInstallPath;
71 : bool waitFlag = false;
72 : bool asan = false;
73 :
74 : // ---- Generic package operation (per-call) ----
75 : uint32_t msgType = 0U; // raw HDCMessage::MsgType for generic builders
76 : uint32_t checkCode = 0U;
77 : uint32_t packageType = 0U;
78 : uint32_t packageWorkerType = 0U;
79 : uint32_t packageMaxProcessTime = 0U;
80 : bool beforeSendPkg = false;
81 : std::string packageName;
82 : std::string hashCode;
83 :
84 : // ---- Check-package query (per-call, TSD_CHECK_PACKAGE) ----
85 : uint32_t extendpkgCheckCode = 0U;
86 :
87 : // ---- Normal check-code with plugin version (per-call, TSD_GET_DEVICE_PACKAGE_CHECKCODE_NORMAL) ----
88 : // Optional: when version is non-empty, a host_plugin_versions entry is appended.
89 : struct {
90 : std::string version;
91 : std::string timestamp;
92 7 : bool Empty() const { return version.empty() && timestamp.empty(); }
93 : } hostPluginVersion;
94 :
95 : // ---- Capability query (per-call) ----
96 : // Raw TsdCapabilityType (cast from the caller's enum) used to derive the
97 : // HDCMessage::MsgType for capability-query messages.
98 : int32_t capabilityType = 0;
99 :
100 : // ---- File operations (per-call) ----
101 : std::string omfileName;
102 : std::string removeFilePath;
103 :
104 : // ---- Sub-process control (per-call) ----
105 : uint32_t closeSubProcPid = 0U;
106 :
107 : // ---- Sub-process status / close lists (per-call) ----
108 : // Parallel arrays describing the sub-process entries carried by
109 : // TSD_GET_SUB_PROC_STATUS / TSD_CLOSE_SUB_PROC_LIST. subProcTypeList may be
110 : // empty (e.g. GetSubProcStatus carries only pids).
111 : std::vector<uint32_t> subProcPidList;
112 : std::vector<uint32_t> subProcTypeList;
113 :
114 : // ---- Common open sub-process (per-call, TSD_OPEN_SUB_PROC) ----
115 : uint32_t subProcOpenType = 0U; // helper_sub_proc.process_type
116 : bool hasSubProcFilePath = false;
117 : std::string subProcFilePath;
118 : bool withSubProcLogLevel = false; // include log_level (HCCP case)
119 : std::vector<std::pair<std::string, std::string>> subProcEnvList; // (env_name, env_value)
120 : std::vector<std::string> subProcExtParamList;
121 : };
122 :
123 : // Pure HDC message assembler.
124 : // Responsibility scope: given an immutable MessageContext + per-call inputs,
125 : // populate the HDCMessage proto. Methods are static, side-effect free w.r.t.
126 : // any owning object (only logging and env-lookup happen internally).
127 : // Sending the message (devCommClient_->Send, WaitRsp, ...) is NOT part of
128 : // this class and remains in ProcessModeManager.
129 : class HdcMessageBuilder {
130 : public:
131 : HdcMessageBuilder() = delete;
132 : ~HdcMessageBuilder() = delete;
133 : HdcMessageBuilder(const HdcMessageBuilder&) = delete;
134 : HdcMessageBuilder(HdcMessageBuilder&&) = delete;
135 : HdcMessageBuilder& operator=(const HdcMessageBuilder&) = delete;
136 : HdcMessageBuilder& operator=(HdcMessageBuilder&&) = delete;
137 :
138 : // Build TSD_START_PROC_MSG.
139 : // Byte-equivalent to the legacy ProcessModeManager::ConstructOpenMsg.
140 : static TSD_StatusT BuildOpen(HDCMessage& msg, const MessageContext& ctx);
141 :
142 : // Build TSD_CLOSE_PROC_MSG.
143 : // Byte-equivalent to the legacy ProcessModeManager::ConstructCloseMsg.
144 : static TSD_StatusT BuildClose(HDCMessage& msg, const MessageContext& ctx);
145 :
146 : // Build TSD_UPDATE_PROIFILING_MSG.
147 : // Reads ctx.profilingMode / rankSize / logicDeviceId / procSign.
148 : static TSD_StatusT BuildUpdateProfiling(HDCMessage& msg, const MessageContext& ctx);
149 :
150 : // Build TSD_OM_PKG_DECOMPRESS_STATUS.
151 : // Reads ctx.omfileName / logicDeviceId / procSign.
152 : static TSD_StatusT BuildOmFileDecompress(HDCMessage& msg, const MessageContext& ctx);
153 :
154 : // Build a generic package check-code message. The concrete proto type is
155 : // carried by ctx.msgType (raw HDCMessage::MsgType), and the body uses
156 : // ctx.checkCode / beforeSendPkg / logicDeviceId / procSign.
157 : static TSD_StatusT BuildPackageCheckCode(HDCMessage& msg, const MessageContext& ctx);
158 :
159 : // Build a capability-query message.
160 : // Reads ctx.capabilityType (raw TsdCapabilityType) to derive the proto
161 : // message type, plus ctx.logicDeviceId / ctx.procSign.
162 : static TSD_StatusT BuildCapability(HDCMessage& msg, const MessageContext& ctx);
163 :
164 : // Build TSD_CLOSE_SUB_PROC.
165 : // Reads ctx.closeSubProcPid / logicDeviceId / procSign.
166 : static TSD_StatusT BuildCloseSubProc(HDCMessage& msg, const MessageContext& ctx);
167 :
168 : // Build TSD_REMOVE_FILE.
169 : // Reads ctx.removeFilePath / logicDeviceId / procSign.
170 : static TSD_StatusT BuildRemoveFile(HDCMessage& msg, const MessageContext& ctx);
171 :
172 : // Build TSD_GET_DEVICE_CANN_HS_CHECKCODE.
173 : // Reads ctx.packageMaxProcessTime / packageWorkerType / packageType /
174 : // packageName / hashCode / logicDeviceId.
175 : static TSD_StatusT BuildCannHsCheckCode(HDCMessage& msg, const MessageContext& ctx);
176 :
177 : // Build TSD_GET_SUB_PROC_STATUS.
178 : // Reads ctx.subProcPidList (+ optional ctx.subProcTypeList) / logicDeviceId /
179 : // procSign. Handles both GetSubProcStatus (pids only) and GetSubProcListStatus
180 : // (pids + types).
181 : static TSD_StatusT BuildGetSubProcStatus(HDCMessage& msg, const MessageContext& ctx);
182 :
183 : // Build TSD_CLOSE_SUB_PROC_LIST.
184 : // Reads ctx.subProcPidList / subProcTypeList / logicDeviceId / procSign.
185 : static TSD_StatusT BuildCloseSubProcList(HDCMessage& msg, const MessageContext& ctx);
186 :
187 : // Build TSD_OPEN_SUB_PROC (common open).
188 : // Reads ctx.subProcOpenType / subProcFilePath / subProcEnvList /
189 : // subProcExtParamList / ascendInstallPath / withSubProcLogLevel / logLevel /
190 : // logicDeviceId / procSign.
191 : static TSD_StatusT BuildCommonOpen(HDCMessage& msg, const MessageContext& ctx);
192 :
193 : // Build TSD_CHECK_PACKAGE_RETRY.
194 : // Reads ctx.checkCode / packageType / logicDeviceId / procSign / waitFlag.
195 : static TSD_StatusT BuildCheckPackageRetry(HDCMessage& msg, const MessageContext& ctx);
196 :
197 : // Build TSD_CHECK_PACKAGE.
198 : // Reads ctx.checkCode / extendpkgCheckCode / ascendcppCheckCode / asan /
199 : // logicDeviceId / procSign.
200 : static TSD_StatusT BuildCheckPackage(HDCMessage& msg, const MessageContext& ctx);
201 :
202 : // Build TSD_UPDATE_PACKAGE_PROCESS_CONFIG.
203 : // Reads ctx.logicDeviceId / procSign. The caller is responsible for
204 : // filling package config fields via PackageProcessConfig::ConstructPkgConfigMsg
205 : // after this builder sets the header.
206 : static TSD_StatusT BuildUpdatePackageConfig(HDCMessage& msg, const MessageContext& ctx);
207 :
208 : // Build TSD_GET_DEVICE_PACKAGE_CHECKCODE_NORMAL.
209 : // Reads ctx.packageName / hashCode / packageWorkerType / packageMaxProcessTime /
210 : // packageType / logicDeviceId. Optionally appends a host_plugin_versions entry
211 : // when ctx.hostPluginVersion is non-empty.
212 : static TSD_StatusT BuildNormalCheckCode(HDCMessage& msg, const MessageContext& ctx);
213 : };
214 : } // namespace tsd
215 : #endif // TSD_BASIC_COMPONENT_MESSAGE_BUILDER_HDC_MESSAGE_BUILDER_H
|