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 "trace_driver_api.h"
12 : #include "ascend_hal.h"
13 : #include "adiag_print.h"
14 : #include "adiag_utils.h"
15 : #include "trace_load_library.h"
16 :
17 : #define DRIVER_FUNCTION_NUM 2U
18 : #define DRIVER_LIBRARY_NAME "libascend_hal.so"
19 :
20 : STATIC ArgPtr g_libHandle = NULL;
21 : STATIC SymbolInfo g_drvFuncInfo[DRIVER_FUNCTION_NUM] = {
22 : {"drvGetPlatformInfo", NULL},
23 : {"drvGetDevNum", NULL},
24 : };
25 :
26 2082 : TraStatus TraceDriverInit(void)
27 : {
28 2082 : g_libHandle = TraceOpenLibrary(DRIVER_LIBRARY_NAME);
29 2082 : if (g_libHandle == NULL) {
30 : // on the general server (pure cpu) scene the driver library is absent, this is expected
31 : // and should not report an error, otherwise it pollutes the disk log on a normal startup.
32 0 : ADIAG_WAR(
33 : "dlopen library %s failed, treat as pure cpu env, strerr=%s.", DRIVER_LIBRARY_NAME,
34 : strerror(AdiagGetErrorCode()));
35 0 : return TRACE_FAILURE;
36 : }
37 :
38 2082 : TraStatus ret = TraceLoadFunc(g_libHandle, g_drvFuncInfo, DRIVER_FUNCTION_NUM);
39 2082 : ADIAG_CHK_EXPR_ACTION(ret != TRACE_SUCCESS, return TRACE_FAILURE, "load function symbol failed, ret=%d.", ret);
40 :
41 2082 : return TRACE_SUCCESS;
42 : }
43 :
44 2072 : void TraceDriverExit(void)
45 : {
46 2072 : if (g_libHandle == NULL) {
47 0 : return;
48 : }
49 2072 : if (TraceCloseLibrary(g_libHandle) != TRACE_SUCCESS) {
50 22 : ADIAG_WAR("can not dlclose library %s, strerr=%s.", DRIVER_LIBRARY_NAME, strerror(AdiagGetErrorCode()));
51 : }
52 : }
53 :
54 : typedef drvError_t (*DrvGetPlatformInfo)(uint32_t*);
55 2082 : TraStatus TraceDrvGetPlatformInfo(uint32_t* info)
56 : {
57 2082 : DrvGetPlatformInfo func = (DrvGetPlatformInfo)g_drvFuncInfo[0].handle;
58 2082 : ADIAG_CHK_EXPR_ACTION(func == NULL, return TRACE_FAILURE, "can not find function\"%s\".", g_drvFuncInfo[0].symbol);
59 :
60 2082 : drvError_t drvErr = func(info);
61 2082 : ADIAG_CHK_EXPR_ACTION(
62 : drvErr != DRV_ERROR_NONE, return TRACE_FAILURE, "get platform info failed, drvErr=%d.", (int32_t)drvErr);
63 :
64 2082 : return TRACE_SUCCESS;
65 : }
66 :
67 : typedef drvError_t (*DrvGetDevNum)(uint32_t*);
68 2082 : TraStatus TraceDrvGetDevNum(uint32_t* num)
69 : {
70 2082 : DrvGetDevNum func = (DrvGetDevNum)g_drvFuncInfo[1].handle;
71 2082 : ADIAG_CHK_EXPR_ACTION(func == NULL, return TRACE_FAILURE, "can not find function\"%s\".", g_drvFuncInfo[1].symbol);
72 :
73 2082 : drvError_t drvErr = func(num);
74 :
75 2082 : if (drvErr == DRV_ERROR_NOT_SUPPORT) {
76 22 : return TRACE_FAILURE;
77 : }
78 2060 : ADIAG_CHK_EXPR_ACTION(
79 : drvErr != DRV_ERROR_NONE, return TRACE_FAILURE, "get device num failed, drvErr=%d.", (int32_t)drvErr);
80 :
81 2060 : return TRACE_SUCCESS;
82 : }
|