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 "dltrace_function.h"
12 : #include "hccl_dl.h"
13 : #include "log.h"
14 :
15 : namespace hccl {
16 :
17 1868 : DlTraceFunction &DlTraceFunction::GetInstance()
18 : {
19 1868 : static DlTraceFunction hcclDlTraceFunction;
20 1868 : return hcclDlTraceFunction;
21 : }
22 :
23 10 : DlTraceFunction::DlTraceFunction() : handle_(nullptr)
24 : {
25 10 : }
26 :
27 10 : DlTraceFunction::~DlTraceFunction()
28 : {
29 10 : if (handle_ != nullptr) {
30 9 : (void)HcclDlclose(handle_);
31 9 : handle_ = nullptr;
32 : }
33 10 : }
34 :
35 0 : HcclResult DlTraceFunction::DlUTraceFunctionInterInit() {
36 0 : dlUtraceDestroy = (void(*)(TraHandle handle))HcclDlsym(handle_, "UtraceDestroy");
37 0 : CHK_SMART_PTR_NULL(dlUtraceDestroy);
38 0 : dlUtraceSubmit = (HcclResult(*)(TraHandle handle, const void *buffer, u32 bufSize))HcclDlsym(handle_,
39 0 : "UtraceSubmit");
40 0 : CHK_SMART_PTR_NULL(dlUtraceSubmit);
41 0 : dlUtraceCreateWithAttr = (TraHandle(*)(int tracerType, const char *objName, const TraceAttr *attr))HcclDlsym(
42 0 : handle_, "UtraceCreateWithAttr");
43 0 : CHK_SMART_PTR_NULL(dlUtraceCreateWithAttr);
44 0 : dlUtraceSetGlobalAttr = (TraStatus(*)(const TraceGlobalAttr *attr))HcclDlsym(
45 0 : handle_, "UtraceSetGlobalAttr");
46 0 : CHK_SMART_PTR_NULL(dlUtraceSetGlobalAttr);
47 0 : dlUtraceSave = (TraStatus(*)(TracerType tracerType, bool syncFlag))HcclDlsym(handle_, "UtraceSave");
48 0 : CHK_SMART_PTR_NULL(dlUtraceSave);
49 0 : return HCCL_SUCCESS;
50 : }
51 :
52 10 : HcclResult DlTraceFunction::DlATraceFunctionInterInit() {
53 10 : dlAtraceDestroy = (void(*)(TraHandle handle))HcclDlsym(handle_, "AtraceDestroy");
54 10 : CHK_SMART_PTR_NULL(dlAtraceDestroy);
55 10 : dlAtraceSubmit = (HcclResult(*)(TraHandle handle, const void *buffer, u32 bufSize))HcclDlsym(handle_,
56 10 : "AtraceSubmit");
57 10 : CHK_SMART_PTR_NULL(dlAtraceSubmit);
58 10 : dlAtraceCreateWithAttr = (TraHandle(*)(int tracerType, const char *objName, const TraceAttr *attr))HcclDlsym(
59 10 : handle_, "AtraceCreateWithAttr");
60 10 : CHK_SMART_PTR_NULL(dlAtraceCreateWithAttr);
61 10 : dlAtraceSetGlobalAttr = (TraStatus(*)(const TraceGlobalAttr *attr))HcclDlsym(
62 10 : handle_, "AtraceSetGlobalAttr");
63 10 : dlAtraceSave = (TraStatus(*)(TracerType tracerType, bool syncFlag))HcclDlsym(handle_, "AtraceSave");
64 10 : CHK_SMART_PTR_NULL(dlAtraceSave);
65 10 : return HCCL_SUCCESS;
66 : }
67 :
68 628 : HcclResult DlTraceFunction::DlTraceFunctionInit()
69 : {
70 : HcclResult ret;
71 628 : std::lock_guard<std::mutex> lock(handleMutex_);
72 628 : if (handle_ == nullptr) {
73 11 : handle_ = HcclDlopen("libascend_trace.so", RTLD_NOW);
74 11 : const char* errMsg_atrace = dlerror();
75 11 : if (handle_ == nullptr) {
76 0 : HCCL_WARNING("dlopen [%s] failed, %s", "libascend_trace.so",\
77 : (errMsg_atrace == nullptr) ? "please check the file exist or permission denied." : errMsg_atrace);
78 :
79 0 : handle_ = HcclDlopen("libutrace.so", RTLD_NOW);
80 0 : const char* errMsg_utrace = dlerror();
81 0 : CHK_PRT_RET(handle_ == nullptr, HCCL_ERROR("dlopen [%s] failed, %s", "libutrace.so",\
82 : (errMsg_utrace == nullptr) ? "please check the file exist or permission denied." : errMsg_utrace),\
83 : HCCL_E_OPEN_FILE_FAILURE);
84 0 : ret = DlUTraceFunctionInterInit();
85 0 : if (ret != HCCL_SUCCESS) {
86 0 : HCCL_ERROR("[DlTraceFunctionInit] DlUTraceFunctionInterInit fail, ret[%d], destroy handle.", ret);
87 0 : (void)HcclDlclose(handle_);
88 0 : handle_ = nullptr;
89 0 : return ret;
90 : }
91 : } else {
92 11 : ret = DlATraceFunctionInterInit();
93 11 : if (ret != HCCL_SUCCESS) {
94 1 : HCCL_ERROR("[DlTraceFunctionInit] DlATraceFunctionInterInit fail, ret[%d], destroy handle.", ret);
95 1 : (void)HcclDlclose(handle_);
96 1 : handle_ = nullptr;
97 1 : return ret;
98 : }
99 : }
100 : }
101 627 : return HCCL_SUCCESS;
102 628 : }
103 : }
|