Coverage for /opt/cloud/slavespace/usr1/096471637100f3de0fcfc01072822a80/ut/src/asys/analyze/asys_analyze.py: 38%

360 statements  

« prev     ^ index     » next       coverage.py v7.14.1, created at 2026-08-21 15:37 +0800

1#!/usr/bin/env python3 

2# -*- coding: utf-8 -*- 

3# ---------------------------------------------------------------------------- 

4# Copyright (c) 2025 Huawei Technologies Co., Ltd. 

5# 

6# Licensed under the Apache License, Version 2.0 (the "License"); 

7# you may not use this file except in compliance with the License. 

8# You may obtain a copy of the License at 

9# 

10# http://www.apache.org/licenses/LICENSE-2.0 

11# 

12# Unless required by applicable law or agreed to in writing, software 

13# distributed under the License is distributed on an "AS IS" BASIS, 

14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

15# See the License for the specific language governing permissions and 

16# limitations under the License. 

17# ---------------------------------------------------------------------------- 

18 

19import os 

20import stat 

21import time 

22import sys 

23import struct 

24 

25from analyze.coredump_analyze import CoreDump 

26from common import log_error, log_warning, log_info, log_debug 

27from common import FileOperate as f 

28from common.cmd_run import check_command, real_time_output 

29from common.task_common import get_target_cnt 

30from common.const import DSMI_UB_PORT_NUM, DL_PORT_RX_VL_NUM, STATS_ITEM_NUM, UBQOS_MAX_SL_NUM 

31from common.const import UB_ENTIRE_STATUS_MAP, UB_PORT_STATUS_MAP, BALANCE_ALGORITHM_MAP 

32from collect.coretrace import ParseCoreTrace 

33from collect.trace import ParseTrace 

34from collect.stackcore import ParseStackCore 

35from collect import AsysCollect 

36from params import ParamDict 

37 

38ub_file_names = ["ubnl_dfx_statistic", "ubnl_dfx_ssu_schedule", "ubnl_dfx_config_item", 

39 "ubmem_daw", "ubtpl_acl_src", "sl_to_vl"] 

40 

41 

42class AsysAnalyze: 

43 def __init__(self): 

44 self.file = self.get_param_arg('file') 

45 self.path = self.get_param_arg('path') 

46 self.exe_file = self.get_param_arg("exe_file") 

47 self.core_file = self.get_param_arg("core_file") 

48 self.symbol = self.get_param_arg('symbol') 

49 self.symbol_path = self.get_param_arg('symbol_path') 

50 self.output = ParamDict().asys_output_timestamp_dir 

51 self.run_mode = self.get_param_arg('run_mode') 

52 self.device_id = ParamDict().get_arg('device_id', 0) 

53 

54 def clean_output(self): 

55 f.remove_dir(self.output) 

56 

57 @staticmethod 

58 def get_param_arg(mode): 

59 if mode == "symbol": 

60 return ParamDict().get_arg(mode) 

61 return ParamDict().get_arg(mode) if ParamDict().get_arg(mode) else None 

62 

63 @staticmethod 

64 def _convert_ub_port_status(bin_file_path, txt_file_path): 

65 fmt = f'I{DSMI_UB_PORT_NUM}I' 

66 try: 

67 with open(bin_file_path, 'rb') as bin_f: 

68 bin_data = bin_f.read() 

69 

70 expected_size = struct.calcsize(fmt) 

71 if len(bin_data) < expected_size: 

72 raise ValueError(f"Binary expected {expected_size} bytes, actual {len(bin_data)} bytes") 

73 

74 unpacked_data = struct.unpack(fmt, bin_data[:expected_size]) 

75 link_status = unpacked_data[0] 

76 port_status = unpacked_data[1: 1 + DSMI_UB_PORT_NUM] 

77 

78 with open(txt_file_path, 'w', encoding='utf-8') as txt_f: 

79 txt_f.write("=== DSMI UB Port Status Data ===\n\n") 

80 txt_f.write("1. Overall UB Link Status:\n") 

81 ub_link_desc = UB_ENTIRE_STATUS_MAP.get(link_status, f"Unknown status (Value: {link_status})") 

82 txt_f.write(f" Status Value: {link_status} -> {ub_link_desc}\n\n") 

83 txt_f.write("2. Status of Each UB Port (Total 36 ports):\n") 

84 txt_f.write(" Port No | Status Val | Status Description\n") 

85 txt_f.write(" --------|------------|-------------------\n") 

86 for port_idx in range(DSMI_UB_PORT_NUM): 

87 status_val = port_status[port_idx] 

88 status_desc = UB_PORT_STATUS_MAP.get(status_val, f"Unknown status (Value: {status_val})") 

89 txt_f.write(f" {port_idx:7d} | {status_val:10d} | {status_desc}\n") 

90 

91 log_info(f"Conversion successful! {bin_file_path} has been converted to text file {txt_file_path}") 

92 

93 except FileNotFoundError: 

94 log_warning(f"Error: {bin_file_path} not found") 

95 except (struct.error, ValueError) as e: 

96 log_error(f"Parse error: port_status {e}") 

97 except Exception as e: 

98 log_error(f"Unexpected error: port_status {e}") 

99 

100 @staticmethod 

101 def _convert_ub_port_perf_test(bin_file_path, txt_file_path): 

102 fmt = f"4I{DL_PORT_RX_VL_NUM}I{DL_PORT_RX_VL_NUM}I28I4I{DL_PORT_RX_VL_NUM}I{DL_PORT_RX_VL_NUM}I28I" 

103 expected_size = struct.calcsize(fmt) 

104 

105 try: 

106 with open(bin_file_path, 'rb') as bin_f: 

107 d = struct.unpack(fmt, bin_f.read()) 

108 if len(d) * 4 < expected_size: 

109 raise ValueError(f"Binary expected {expected_size} bytes, actual {len(d)*4} bytes") 

110 

111 p, s = 0, DL_PORT_RX_VL_NUM 

112 rx_cnt = (d[p + 1] << 32) | d[p] 

113 p += 2 

114 rx_max = (d[p + 1] << 32) | d[p] 

115 p += 2 

116 rx_vl_cnt, rx_vl_max = d[p: p + s], d[p + s: p + 2 * s] 

117 p += 2 * s 

118 p += 28 # skip rsv1 

119 tx_cnt = (d[p + 1] << 32) | d[p] 

120 p += 2 

121 tx_max = (d[p + 1] << 32) | d[p] 

122 p += 2 

123 tx_vl_cnt, tx_vl_max = d[p: p + s], d[p + s: p + 2 * s] 

124 

125 with open(txt_file_path, 'w', encoding='utf-8') as txt_f: 

126 txt_f.write("=== MAMI Port Performance Test Counter ===\n") 

127 txt_f.write(f"RX Total: 0x{rx_cnt:016X} ({rx_cnt}) | RX Max: 0x{rx_max:016X} ({rx_max})\n") 

128 txt_f.write(f"TX Total: 0x{tx_cnt:016X} ({tx_cnt}) | TX Max: 0x{tx_max:016X} ({tx_max})\n\n") 

129 txt_f.write(f"{'VL':<4}|{'RX Cnt':<20}|{'RX Max':<20}|{'TX Cnt':<20}|{'TX Max':<20}\n{'-'*90}\n") 

130 for i in range(s): 

131 txt_f.write(f"{i:<4}|{rx_vl_cnt[i]:<20}|{rx_vl_max[i]:<20}|" 

132 f"{tx_vl_cnt[i]:<20}|{tx_vl_max[i]:<20}\n") 

133 log_info(f"Conversion successful! {bin_file_path} has been converted to text file {txt_file_path}") 

134 

135 except FileNotFoundError: 

136 log_warning(f"Error: {bin_file_path} not found") 

137 except (struct.error, ValueError) as e: 

138 log_error(f"Parse error: port_perf_test {e}") 

139 except Exception as e: 

140 log_error(f"Unexpected error: port_perf_test {e}") 

141 

142 @staticmethod 

143 def _convert_ub_ubnl_dfx(bin_file_path, txt_file_path, dfx_type): 

144 item_fmt = "64BQ" 

145 struct_fmt = f"I{item_fmt * STATS_ITEM_NUM}" 

146 expected_size = struct.calcsize(struct_fmt) 

147 try: 

148 with open(bin_file_path, "rb") as bin_f: 

149 bin_data = bin_f.read() 

150 

151 if len(bin_data) < expected_size: 

152 raise ValueError(f"Binary expected {expected_size} bytes, actual {len(bin_data)} bytes") 

153 

154 unpacked = struct.unpack(struct_fmt, bin_data[:expected_size]) 

155 ptr = 0 

156 count = unpacked[ptr] 

157 ptr += 1 

158 

159 stats_items = [] 

160 for _ in range(count): 

161 # 提取64个char的原始字节 

162 name_raw = bytes(unpacked[ptr: ptr + 64]) 

163 ptr += 64 

164 value = unpacked[ptr] 

165 ptr += 1 

166 

167 name = name_raw.split(b'\x00')[0].decode("utf-8", errors="replace") 

168 name = name.strip() or "Unnamed_Stat" 

169 stats_items.append((name, value)) 

170 

171 with open(txt_file_path, "w", encoding="utf-8") as txt_f: 

172 txt_f.write(f"=== MAMI {dfx_type} Data (UBNL DFX) ===\n") 

173 txt_f.write(f"Reported Count (from struct): {count}\n") 

174 txt_f.write("-" * 90 + "\n") 

175 txt_f.write(f"{'Index':<6} | {'Stats Name':<40} | {'64-bit Value':<20}\n") 

176 txt_f.write(f"{'------':<6} | {'----------------------------------------':<40} | " 

177 f"{'--------------------':<20}\n") 

178 

179 for idx, (name, value) in enumerate(stats_items, 1): 

180 txt_f.write(f"{idx:<6} | {name:<40} | {value:<20}\n") 

181 

182 log_info(f"Conversion successful! {bin_file_path} has been converted to text file {txt_file_path}") 

183 

184 except FileNotFoundError: 

185 log_warning(f"Error: {bin_file_path} not found") 

186 except (struct.error, ValueError) as e: 

187 log_error(f"Parse error: ubnl_dfx {dfx_type} {e}") 

188 except Exception as e: 

189 log_error(f"Unexpected error: ubnl_dfx {dfx_type} {e}") 

190 

191 @staticmethod 

192 def _convert_ub_ubmem_daw(bin_file_path, txt_file_path): 

193 fmt = '4B' 

194 try: 

195 with open(bin_file_path, 'rb') as bin_f: 

196 bin_data = bin_f.read() 

197 

198 expected_size = struct.calcsize(fmt) 

199 if len(bin_data) < expected_size: 

200 raise ValueError(f"Binary expected {expected_size} bytes, actual {len(bin_data)} bytes") 

201 

202 unpacked_data = struct.unpack(fmt, bin_data[:expected_size]) 

203 

204 template_id = unpacked_data[0] 

205 balance_algorithm = unpacked_data[1] 

206 balance_start_bit = unpacked_data[2] 

207 reserved = unpacked_data[3] 

208 

209 algorithm_desc = BALANCE_ALGORITHM_MAP.get(balance_algorithm, 

210 f"Unknown algorithm (value: {balance_algorithm})") 

211 

212 with open(txt_file_path, 'w', encoding='utf-8') as txt_f: 

213 txt_f.write("=== MAMI Dynamic Address Window (DAW) Table Properties ===\n\n") 

214 txt_f.write("DAW Table Configuration:\n") 

215 txt_f.write("--------------------------------------------------------\n") 

216 txt_f.write(f"Template ID: {template_id} (Defined by BIOS, used by control plane)\n") 

217 txt_f.write(f"Balance Algorithm: {balance_algorithm} -> {algorithm_desc}\n") 

218 txt_f.write(f"Balance Start Bit: {balance_start_bit} (Lowest address bit for hash)\n") 

219 txt_f.write(f"Reserved Field: {reserved} (For struct alignment)\n") 

220 

221 log_info(f"Conversion successful! {bin_file_path} has been converted to text file {txt_file_path}") 

222 

223 except FileNotFoundError: 

224 log_warning(f"Error: {bin_file_path} not found") 

225 except (struct.error, ValueError) as e: 

226 log_error(f"Parse error: ubmem_daw {e}") 

227 except Exception as e: 

228 log_error(f"Unexpected error: ubmem_daw {e}") 

229 

230 @staticmethod 

231 def _convert_ub_ubtpl_acl_src(bin_file_path, txt_file_path): 

232 head_fmt = "HHI8B" 

233 head_size = struct.calcsize(head_fmt) 

234 eid_fmt = "B3B4I" 

235 struct_fmt = f"IH2B{eid_fmt}IBI12B" 

236 struct_size = struct.calcsize(struct_fmt) 

237 try: 

238 with open(bin_file_path, 'rb') as bin_f: 

239 bin_data = bin_f.read() 

240 if len(bin_data) < head_size: 

241 raise ValueError(f"Binary too short! expected ≥{head_size}B, act {len(bin_data)}B") 

242 

243 # 解析固定头部+位段 

244 hdr = struct.unpack(head_fmt, bin_data[:head_size]) 

245 num, flag_rsv, end_idx = hdr[0], hdr[1], hdr[2] 

246 more_flag, rsv = flag_rsv & 0x01, (flag_rsv >> 1) & 0x7FFF 

247 body = bin_data[head_size:] 

248 

249 expected_size = num * struct_size + head_size 

250 if len(bin_data) < expected_size: 

251 raise ValueError(f"Binary expected {expected_size} bytes, actual {len(bin_data)} bytes") 

252 

253 acl_list = [] 

254 for i in range(num): 

255 acl = struct.unpack(struct_fmt, body[i * struct_size: (i + 1) * struct_size]) 

256 # 解析mamiEid:compressedFlag(acl[4]) + union(acl[8:12]) 

257 eid_flag = acl[4] 

258 if eid_flag == 0: # 128位非压缩EID,4个uint32_t拼接 

259 eid_hex = ''.join([f"{x:08X}" for x in acl[8:12]]).upper() 

260 else: # 20位压缩EID,提取低20位 

261 eid_20bit = acl[8] & 0x000FFFFF 

262 eid_hex = f"{eid_20bit:05X}".upper() 

263 # 提取aclGrpId低24位,整理核心字段 

264 acl_grp_id = acl[14] & 0x00FFFFFF 

265 acl_list.append((acl[0], acl[1], eid_flag, eid_hex, acl[12], acl[13], acl_grp_id)) 

266 

267 with open(txt_file_path, 'w', encoding='utf-8') as txt_f: 

268 txt_f.write("=== UBTPL Source ACL Config (Support 128/20bit EID) ===\n") 

269 txt_f.write(f"Return Count: {num} | More Flag: {more_flag} (0=No/1=Yes)\n") 

270 txt_f.write(f"End Index: 0x{end_idx:08X} ({end_idx})\n") 

271 txt_f.write(f"{'-'*130}\n") 

272 txt_f.write(f"{'Idx':<4}|{'PlaneId':<10}|{'UEIdx':<8}|{'EidFlag':<8}|{'EID':<32}|" 

273 f"{'TransType':<10}|{'AclType':<8}|{'AclGrpId':<10}\n") 

274 txt_f.write(f"{'-'*4}|{'-'*10}|{'-'*8}|{'-'*8}|{'-'*32}|{'-'*10}|{'-'*8}|{'-'*10}\n") 

275 for idx, (p, u, c, e, t, a, g) in enumerate(acl_list): 

276 txt_f.write(f"{idx:<4}|{p:<10}|{u:<8}|{c:<8}|{e:<32}|{t:<10}|{a:<8}|{g:<10}\n") 

277 

278 log_info(f"Conversion successful! {bin_file_path} has been converted to text file {txt_file_path}") 

279 

280 except FileNotFoundError: 

281 log_warning(f"Error: {bin_file_path} not found") 

282 except (struct.error, ValueError) as e: 

283 log_error(f"Parse error: ubtpl_acl_src {e}") 

284 except Exception as e: 

285 log_error(f"Unexpected error: ubtpl_acl_src {e}") 

286 

287 @staticmethod 

288 def _convert_ub_sl_to_vl(bin_file_path, txt_file_path): 

289 item_fmt = "2H" 

290 struct_fmt = f"2I{UBQOS_MAX_SL_NUM * 2}H" 

291 expected_size = struct.calcsize(struct_fmt) 

292 

293 try: 

294 with open(bin_file_path, 'rb') as bin_f: 

295 bin_data = bin_f.read() 

296 if len(bin_data) < expected_size: 

297 raise ValueError(f"Binary expected {expected_size} bytes, actual {len(bin_data)} bytes") 

298 

299 # 解包数据并提取核心字段 

300 d = struct.unpack(struct_fmt, bin_data[:expected_size]) 

301 plane_id, num = d[0], d[1] 

302 # 校验有效配置数范围,非法值自动修正 

303 num = max(0, min(num, UBQOS_MAX_SL_NUM)) 

304 # 提取SL-VL配置,按索引分组 

305 sl2vl = [(d[2 + 2 * i], d[3 + 2 * i]) for i in range(UBQOS_MAX_SL_NUM)] 

306 

307 with open(txt_file_path, 'w', encoding='utf-8') as txt_f: 

308 txt_f.write("=== UBQOS SL to VL Mapping Configuration ===\n") 

309 txt_f.write(f"Plane ID: {plane_id} | Valid Config Num: {num} (Max: {UBQOS_MAX_SL_NUM})\n") 

310 txt_f.write(f"Struct Total Size: {expected_size} bytes\n{'-'*60}\n") 

311 txt_f.write(f"{'Idx':<6}|{'SL(0-15)':<10}|{'VL(0-15)':<10}|{'Status':<10}\n") 

312 txt_f.write(f"{'-'*6}|{'-'*10}|{'-'*10}|{'-'*10}\n") 

313 for i, (sl, vl) in enumerate(sl2vl): 

314 status = "Valid" if i < num else "Reserved" 

315 txt_f.write(f"{i:<6}|{sl:<10}|{vl:<10}|{status:<10}\n") 

316 log_info(f"Conversion successful! {bin_file_path} has been converted to text file {txt_file_path}") 

317 

318 except FileNotFoundError: 

319 log_warning(f"Error: {bin_file_path} not found") 

320 except (struct.error, ValueError) as e: 

321 log_error(f"Parse error: sl_to_vl {e}") 

322 except Exception as e: 

323 log_error(f"Unexpected error: sl_to_vl {e}") 

324 

325 def write_res_file(self, file_name, file_content): 

326 try: 

327 flags = os.O_WRONLY | os.O_CREAT 

328 modes = stat.S_IWUSR | stat.S_IRUSR 

329 with os.fdopen(os.open(f"{self.output}/{file_name}", flags, modes), 'w') as fw: 

330 fw.write(file_content) 

331 except Exception as e: 

332 log_error(e) 

333 

334 def run(self): 

335 if f.check_exists(self.path) and f.check_exists(self.output): 

336 if os.path.relpath(self.path, self.output).endswith(".."): 

337 self.clean_output() 

338 log_error('The output directory cannot be the same as the "path" directory or its subdirectories.') 

339 return False 

340 mode_function = { 

341 "trace": self.__atrace_analyze, 

342 "stackcore": self.__atrace_analyze, 

343 "coretrace": self.__atrace_analyze, 

344 "coredump": self.__core_dump_analyze, 

345 "aicore_error": self.__aicore_error_analyze, 

346 "ub": self.__ub_analyze 

347 } 

348 func = mode_function.get(self.run_mode) 

349 return func() 

350 

351 def __copy_dir(self): 

352 if self.run_mode == "trace": 

353 return f.copy_dir(self.path, self.output) 

354 # stackcore, coretrace 

355 for root, _, files in os.walk(self.path): 

356 for file in files: 

357 if self.run_mode in {"stackcore", "coretrace"} and not file.startswith(self.run_mode): 

358 continue 

359 root_path = os.path.relpath(root, self.path) 

360 if not f.copy_file_to_dir(os.path.join(root, file), os.path.join(self.output, root_path)): 

361 return False 

362 return True 

363 

364 def __atrace_analyze(self): 

365 """ 

366 parse the trace file. If the file exists, parse the file. If the directory exists, parse the directory. 

367 """ 

368 if self.run_mode == "trace": 

369 parse_struct = ParseTrace(True) 

370 elif self.run_mode == "stackcore": 

371 parse_struct = ParseStackCore(self.symbol_path, self.file) 

372 if not self.symbol_path: 

373 log_warning("'--symbol_path' is not set, the default path will be used to analyze.") 

374 elif self.run_mode == "coretrace": 

375 parse_struct = ParseCoreTrace(self.symbol_path, self.file) 

376 if not self.symbol_path: 

377 log_warning("'--symbol_path' is not set, the default path will be used to analyze.") 

378 else: 

379 return False 

380 

381 if self.file: 

382 f.copy_file_to_dir(self.file, self.output) 

383 log_info(f"Copy source file {self.file} into {self.output}") 

384 return parse_struct.start_parse_file(os.path.join(self.output, os.path.basename(self.file))) 

385 elif self.path: 

386 self.path = os.path.abspath(self.path) 

387 self.output = os.path.join(self.output, self.path.split(os.sep)[-1]) 

388 copy_res = self.__copy_dir() 

389 if not copy_res: 

390 return False 

391 count = get_target_cnt(self.output) 

392 return parse_struct.run(self.output, count=count) 

393 else: 

394 log_error("Analyze requires either the --file or --path argument") 

395 return False 

396 

397 def __core_dump_analyze(self): 

398 stack_txt = "[process]\n" 

399 if not check_command("gdb"): 

400 log_error('Gdb does not exist, install gdb before using it.') 

401 return False 

402 if not self.exe_file: 

403 log_error("The --exe_file parameter must exist for analyze coredump.") 

404 return False 

405 if not self.core_file: 

406 log_error("The --core_file parameter must exist for analyze coredump.") 

407 return False 

408 core_dump = CoreDump(self.exe_file, self.core_file, self.symbol, self.output) 

409 stack_txt, pid = core_dump.start_gdb(stack_txt) 

410 if pid == 0: 

411 return False 

412 file_name = f"stackcore_{os.path.basename(self.exe_file)}_{pid}_{int(round(time.time() * 1000))}.txt" 

413 self.write_res_file(file_name, stack_txt) 

414 return True 

415 

416 def __aicore_error_analyze(self): 

417 output_path = os.path.dirname(self.output) 

418 msaicerr_path = ParamDict().tools_path.parents[1].joinpath("msaicerr", "msaicerr.py") 

419 log_debug(f"Start load msaicerr tools path: {msaicerr_path}") 

420 if not os.path.exists(msaicerr_path): 

421 log_error('The path of the msaicerr tool cannot be found, please install the whole package.') 

422 return False 

423 if self.path: 

424 log_debug(f"msaicerr analyze path {self.path}") 

425 cmd = f"{sys.executable} {msaicerr_path} -p {self.path} -dev {self.device_id} -out {output_path}" 

426 else: 

427 asys_collector = AsysCollect() 

428 task_res = AsysCollect().run() 

429 log_debug(f"Asys collect path {asys_collector.output_root_path} res {task_res}") 

430 if not task_res: 

431 log_error(f"Asys collect log failed") 

432 return False 

433 cmd = (f"{sys.executable} {msaicerr_path} -p {asys_collector.output_root_path} -dev {self.device_id}" 

434 f" -out {output_path}") 

435 log_debug(f"Start run: {cmd}") 

436 res = real_time_output(cmd) 

437 self.clean_output() 

438 return res 

439 

440 def __ub_analyze(self): 

441 if self.path: 

442 self.path = os.path.abspath(self.path) 

443 else: 

444 log_error("Please enter the path to the UB data collection file.") 

445 return True 

446 for ub_file_name in ub_file_names: 

447 func_name = "_convert_ub_" + ub_file_name 

448 bin_file_name = ub_file_name + ".bin" 

449 txt_file_name = ub_file_name + ".txt" 

450 bin_file_path = os.path.join(self.path, bin_file_name) 

451 txt_file_path = os.path.join(self.output, txt_file_name) 

452 func = getattr(self, func_name, None) 

453 if func: 

454 func(bin_file_path, txt_file_path) 

455 return True 

456 

457 def _convert_ub_ubnl_dfx_statistic(self, bin_file_path, txt_file_path): 

458 self._convert_ub_ubnl_dfx(bin_file_path, txt_file_path, "Statistic") 

459 

460 def _convert_ub_ubnl_dfx_ssu_schedule(self, bin_file_path, txt_file_path): 

461 self._convert_ub_ubnl_dfx(bin_file_path, txt_file_path, "Ssu Schedule") 

462 

463 def _convert_ub_ubnl_dfx_config_item(self, bin_file_path, txt_file_path): 

464 self._convert_ub_ubnl_dfx(bin_file_path, txt_file_path, "Config Item")