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 "../pub_inc/trace.h"
12 : #include <set>
13 : #include "env_config_v2.h"
14 :
15 : namespace Hccl {
16 :
17 253 : Trace::Trace() {}
18 :
19 0 : bool Trace::isClosingChar(const char& c) const
20 : {
21 : static const std::set<char> closingChars = {
22 : ' ', ')', ']', '}', ';', ',', '.', '!', '?',
23 0 : };
24 0 : return closingChars.find(c) != closingChars.end();
25 : }
26 :
27 41 : HcclResult Trace::Init(std::string& logInfo)
28 : {
29 41 : traceHandle = TraceCreate(logInfo.c_str()); // Handle只申请不释放,由atrace组件释放
30 41 : if (traceHandle == TRACE_INVALID_HANDLE) {
31 0 : HCCL_RUN_INFO("Trace::Init traceHandle is TRACE_INVALID_HANDLE");
32 : }
33 41 : return HcclResult::HCCL_SUCCESS;
34 : }
35 :
36 30 : void Trace::Save(std::string& buffer)
37 : {
38 : // 对传入的buffersize进行判断。
39 30 : if (EnvConfig::GetInstance().GetLogConfig().GetEntryLogEnable()) {
40 84 : HCCL_RUN_INFO("%s", buffer.c_str());
41 28 : return;
42 : }
43 :
44 2 : u32 len = DEFAULT_ATRACE_MSG_SIZE - 1;
45 2 : u32 pos = 0;
46 2 : u32 totLen = 0;
47 2 : u32 submitLen = 0;
48 2 : u32 submitLenBak = 0;
49 2 : totLen = buffer.length();
50 2 : bool ret = true;
51 2 : if (traceHandle == TRACE_INVALID_HANDLE) {
52 6 : HCCL_WARNING("Tracve::Save Info = %s", buffer.c_str());
53 2 : return;
54 : }
55 0 : char* startPos = static_cast<char*>(const_cast<char*>(buffer.c_str()));
56 0 : while (pos < totLen) {
57 0 : submitLen = (pos + len <= totLen) ? len : (totLen - pos);
58 0 : submitLenBak = submitLen;
59 : // 非尾行截断处理
60 0 : if (pos + len < totLen) {
61 0 : while (submitLen > 0) {
62 0 : if (isClosingChar(startPos[submitLen - 1])) {
63 0 : break;
64 : }
65 0 : submitLen--;
66 : }
67 0 : if (submitLen == 0) {
68 0 : submitLen = submitLenBak;
69 : }
70 : }
71 :
72 : // 截断成功/尾行直接提交
73 0 : ret = ret && TraceSubmit(traceHandle, startPos, submitLen);
74 0 : if (!ret) {
75 0 : HCCL_WARNING("trace submit failed, ret[%d], traceInfo = [%s]", ret, startPos);
76 : }
77 0 : startPos += submitLen;
78 0 : pos += submitLen;
79 : }
80 0 : if (!ret) {
81 0 : HCCL_WARNING("trace submit failed, ret[%d], traceInfo = [%s]", ret, startPos);
82 : }
83 : }
84 :
85 253 : Trace::~Trace() { TraceDestroy(traceHandle); }
86 :
87 : } // namespace Hccl
|