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 <string>
12 : #include "sal_pub.h"
13 : #include "externalinput.h"
14 : #include "adapter_trace.h"
15 : #include "atrace_types.h"
16 : #include "hccl_trace_info.h"
17 : using namespace std;
18 : namespace hccl {
19 : constexpr u32 TRACE_MAX_MSG_SIZE = 111;
20 :
21 524 : HcclTraceInfo::HcclTraceInfo() : index(0), handle(-1) { hcclTraceType_ = HcclTraceType::HostTraceType; }
22 :
23 11 : HcclTraceInfo::HcclTraceInfo(const UtraceAttr& utraceAttr) : utraceAttr_(utraceAttr), index(0), handle(-1)
24 : {
25 11 : hcclTraceType_ = HcclTraceType::DeviceTraceType;
26 11 : }
27 :
28 536 : HcclTraceInfo::~HcclTraceInfo() {}
29 :
30 536 : HcclResult HcclTraceInfo::Init(std::string& logInfo)
31 : {
32 536 : if (hcclTraceType_ == HcclTraceType::DeviceTraceType && !utraceAttr_.utraceStatusFlag) {
33 11 : return HCCL_SUCCESS;
34 : }
35 525 : if (handle != -1) {
36 0 : return HCCL_SUCCESS;
37 : }
38 : /* 申请trace资源信息 */
39 525 : CHK_RET(hrtOpenTrace());
40 525 : if (hrtTraceCreateWithAttr(logInfo.c_str(), handle) != HCCL_SUCCESS) {
41 0 : HCCL_RUN_INFO("[HcclTraceInfo]atrace create handle failed, Save logs to run info");
42 0 : handle = -1;
43 : }
44 525 : if (hcclTraceType_ == HcclTraceType::DeviceTraceType) {
45 0 : TraceGlobalAttr traceGlobalAttr = {};
46 0 : traceGlobalAttr.saveMode = 1; // 1代表 发送到远程并保存
47 0 : traceGlobalAttr.deviceId = utraceAttr_.deviceid;
48 0 : traceGlobalAttr.pid = utraceAttr_.pid;
49 0 : CHK_RET(hrtTraceSetGlobalAttr(&traceGlobalAttr));
50 : }
51 525 : return HCCL_SUCCESS;
52 : }
53 :
54 535 : void HcclTraceInfo::DeInit()
55 : {
56 535 : if (handle != -1
57 312 : && ((hcclTraceType_ == HcclTraceType::DeviceTraceType && utraceAttr_.utraceStatusFlag)
58 312 : || hcclTraceType_ == HcclTraceType::HostTraceType)) {
59 : /* 销毁当前trace句柄 */
60 312 : hrtTraceDestroy(handle);
61 : }
62 535 : }
63 :
64 634 : HcclResult HcclTraceInfo::SaveTraceInfo(std::string& logInfo, AtraceOption option)
65 : {
66 634 : if (hcclTraceType_ == HcclTraceType::DeviceTraceType && !utraceAttr_.utraceStatusFlag) {
67 0 : return HCCL_SUCCESS;
68 : }
69 :
70 634 : if (UNLIKELY(handle == -1)) {
71 0 : HCCL_WARNING("%s", logInfo.c_str());
72 0 : return HCCL_SUCCESS;
73 : }
74 :
75 634 : if (GetExternalInputHcclEnableEntryLog() && hcclTraceType_ == HcclTraceType::HostTraceType) {
76 633 : HCCL_RUN_INFO("%s", logInfo.c_str());
77 633 : return HCCL_SUCCESS;
78 : }
79 1 : std::string outLogInfo = "";
80 1 : if (hcclTraceType_ == HcclTraceType::HostTraceType) {
81 1 : if (option == AtraceOption::Opbasekey) {
82 1 : outLogInfo = to_string(SalGetTid()) + "_" + to_string(index) + "_" + logInfo;
83 : } else {
84 0 : outLogInfo = to_string(SalGetTid()) + "_" + logInfo;
85 : }
86 : } else {
87 0 : outLogInfo = logInfo;
88 : }
89 :
90 : /* 输出Trace信息 */
91 1 : u32 len = TRACE_MAX_MSG_SIZE; // atrace 兼顾性能的单条日志长度
92 1 : u32 pos = 0;
93 1 : u32 totLen = outLogInfo.length();
94 1 : u32 submitLen = 0;
95 1 : const u8* startPos = reinterpret_cast<const u8*>(outLogInfo.c_str());
96 2 : while (pos < totLen) {
97 1 : submitLen = (pos + len <= totLen) ? len : (totLen - pos);
98 1 : CHK_RET(hrtTraceSubmit(handle, startPos, submitLen));
99 1 : startPos += submitLen;
100 1 : pos += submitLen;
101 : }
102 1 : if (option == AtraceOption::Opbasekey && hcclTraceType_ == HcclTraceType::HostTraceType) {
103 1 : index++;
104 : }
105 :
106 1 : return HCCL_SUCCESS;
107 1 : }
108 :
109 0 : HcclResult HcclTraceInfo::SavealgtypeTraceInfo(std::string& alg, const std::string& tag)
110 : {
111 0 : std::string logInfo;
112 0 : if (alg == "") {
113 0 : logInfo = tag + " AlgType is set by user or the server number is one";
114 : } else {
115 0 : logInfo = tag + " AlgType is: " + alg;
116 : }
117 0 : CHK_RET_AND_PRINT_IDE(SaveTraceInfo(logInfo, AtraceOption::Algtype), tag.c_str());
118 0 : return HCCL_SUCCESS;
119 0 : }
120 :
121 0 : HcclResult HcclTraceInfo::Flush()
122 : {
123 0 : if (hcclTraceType_ == HcclTraceType::DeviceTraceType && !utraceAttr_.utraceStatusFlag) {
124 0 : return HCCL_SUCCESS;
125 : }
126 :
127 0 : if (UNLIKELY(handle == -1)) {
128 0 : return HCCL_SUCCESS;
129 : }
130 0 : TracerType tracerType = TracerType::TRACER_TYPE_SCHEDULE;
131 0 : bool syncFlag = true;
132 0 : CHK_RET(hrtTraceSave(tracerType, syncFlag));
133 0 : return HCCL_SUCCESS;
134 : }
135 : } // namespace hccl
|