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