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 "dlprof_func_v2.h"
12 : #include "log.h"
13 : #include "exception_util.h"
14 :
15 : namespace Hccl {
16 17 : DlProfFunc& DlProfFunc::GetInstance()
17 : {
18 17 : static DlProfFunc hcclDlProfFunction;
19 17 : return hcclDlProfFunction;
20 : }
21 :
22 0 : bool DlProfFunc::isStubMode() { return false; }
23 :
24 1 : DlProfFunc::DlProfFunc()
25 : {
26 1 : if (isStubMode()) {
27 1 : DlProfFunctionStubInit();
28 : } else {
29 0 : DlProfFunctionInit();
30 : }
31 1 : }
32 :
33 1 : DlProfFunc::~DlProfFunc()
34 : {
35 1 : if (handle_ != nullptr) {
36 0 : (void)dlclose(handle_);
37 0 : handle_ = nullptr;
38 : }
39 1 : }
40 :
41 17 : static uint64_t HcclMsprofSysCycleTimeStub()
42 : {
43 51 : HCCL_WARNING("Entry HcclMsprofSysCycleTimeStub");
44 17 : return 0;
45 : }
46 :
47 1 : void DlProfFunc::DlProfFunctionStubInit() { dlMsprofSysCycleTime = (uint64_t(*)(void))HcclMsprofSysCycleTimeStub; }
48 :
49 0 : HcclResult DlProfFunc::DlProfFunctionInterInit()
50 : {
51 0 : CHECK_NULLPTR(handle_, "[DlProfFunc::DlProfFunctionInterInit] handle_ is nullptr!");
52 0 : dlMsprofSysCycleTime = (uint64_t(*)(void))dlsym(handle_, "MsprofSysCycleTime");
53 0 : CHK_PTR_NULL(dlMsprofSysCycleTime);
54 :
55 0 : return HCCL_SUCCESS;
56 : }
57 :
58 0 : HcclResult DlProfFunc::DlProfFunctionInit()
59 : {
60 0 : if (initializedFlag_) {
61 0 : return HCCL_SUCCESS;
62 : }
63 :
64 0 : std::lock_guard<std::mutex> lock(handleMutex_);
65 0 : if (handle_ == nullptr) {
66 0 : handle_ = dlopen("libprofapi.so", RTLD_NOW);
67 : }
68 0 : if (handle_ != nullptr) {
69 0 : CHK_RET(DlProfFunctionInterInit());
70 : }
71 0 : initializedFlag_ = true;
72 0 : return HCCL_SUCCESS;
73 0 : }
74 : } // namespace Hccl
|