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