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