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_server_core.h"
12 : #include "ascend_hal.h"
13 : #include "adiag_print.h"
14 : #include "trace_session_mgr.h"
15 : #include "ktrace_ts.h"
16 : #include "trace_send_mgr.h"
17 : #include "trace_server_socket.h"
18 :
19 : STATIC int32_t g_devId = -1;
20 :
21 20 : void TraceServerSetDevId(int32_t devId) { g_devId = devId; }
22 :
23 29 : STATIC INLINE int32_t TraceServerGetDevId(void) { return g_devId; }
24 :
25 20 : TraStatus TraceServerMgrProcess(void)
26 : {
27 20 : TraStatus ret = TraceServerSessionInit();
28 20 : if (ret != TRACE_SUCCESS) {
29 0 : ADIAG_ERR("trace server session init failed.");
30 0 : return TRACE_FAILURE;
31 : }
32 :
33 20 : ret = TraceServiceInit(g_devId);
34 20 : if (ret != TRACE_SUCCESS) {
35 1 : ADIAG_ERR("trace server init failed.");
36 1 : return TRACE_FAILURE;
37 : }
38 19 : return TRACE_SUCCESS;
39 : }
40 :
41 30 : void TraceServerMgrExit(void) { TraceServerSessionExit(); }
42 :
43 12 : STATIC TraStatus GetDevNumIDs(uint32_t* deviceNum, uint32_t* deviceIdArray, uint32_t idArraySize)
44 : {
45 12 : ADIAG_CHK_EXPR_ACTION(deviceNum == NULL, return TRACE_FAILURE, "input device number pointer is null.");
46 12 : ADIAG_CHK_EXPR_ACTION(deviceIdArray == NULL, return TRACE_FAILURE, "input device id array pointer is null.");
47 :
48 12 : drvError_t drvErr = halGetDevNumEx(0, deviceNum);
49 12 : if ((drvErr != 0) || (*deviceNum > idArraySize)) {
50 1 : ADIAG_ERR(
51 : "get device num failed, result=%d, device_number=%u, device id array size=%u.", (int32_t)drvErr, *deviceNum,
52 : idArraySize);
53 1 : return TRACE_FAILURE;
54 : }
55 11 : drvErr = halGetDevIDsEx(0, deviceIdArray, idArraySize);
56 11 : if (drvErr != 0) {
57 1 : ADIAG_ERR("get device id array failed, result=%d.", (int32_t)drvErr);
58 1 : return TRACE_FAILURE;
59 : }
60 10 : return TRACE_SUCCESS;
61 : }
62 :
63 12 : STATIC TraStatus TraceServerPfProcess(void)
64 : {
65 12 : uint32_t deviceIdArray[MAX_DEV_NUM] = {0U}; // device-side device id array
66 12 : uint32_t devNum = 0;
67 12 : TraStatus ret = GetDevNumIDs(&devNum, deviceIdArray, MAX_DEV_NUM);
68 12 : if (ret != TRACE_SUCCESS) {
69 2 : return TRACE_FAILURE;
70 : }
71 10 : ret = KtraceTsCreateThread(devNum, deviceIdArray);
72 10 : if (ret != TRACE_SUCCESS) {
73 1 : ADIAG_ERR("create trace ts thread failed.");
74 1 : return TRACE_FAILURE;
75 : }
76 9 : ret = TraceServerCreateSocketRecv(-1);
77 9 : if (ret != TRACE_SUCCESS) {
78 1 : ADIAG_ERR("create socket receive failed.");
79 1 : return TRACE_FAILURE;
80 : }
81 :
82 8 : return TRACE_SUCCESS;
83 : }
84 :
85 4 : STATIC TraStatus TraceServerVfProcess(uint32_t devId)
86 : {
87 4 : uint32_t deviceIdArray[1] = {devId};
88 4 : TraStatus ret = KtraceTsCreateThread(1U, deviceIdArray);
89 4 : if (ret != TRACE_SUCCESS) {
90 1 : ADIAG_ERR("create trace ts thread failed.");
91 1 : return TRACE_FAILURE;
92 : }
93 3 : ret = TraceServerCreateSocketRecv((int32_t)devId);
94 3 : if (ret != TRACE_SUCCESS) {
95 1 : ADIAG_ERR("create socket receive failed.");
96 1 : return TRACE_FAILURE;
97 : }
98 2 : return TRACE_SUCCESS;
99 : }
100 :
101 17 : TraStatus TraceServerRecvProcess(void)
102 : {
103 17 : int32_t devId = TraceServerGetDevId();
104 17 : if (devId == -1) {
105 12 : return TraceServerPfProcess();
106 5 : } else if ((devId >= MIN_VFID_NUM) && (devId <= MAX_VFID_NUM)) {
107 4 : return TraceServerVfProcess((uint32_t)devId);
108 : } else {
109 1 : ADIAG_ERR("invalid device id(%d).", devId);
110 1 : return TRACE_FAILURE;
111 : }
112 : }
113 :
114 30 : void TraceServerRecvExit(void)
115 : {
116 30 : TraceServerDestroySocketRecv();
117 30 : KtraceTsDestroyThread();
118 30 : }
119 :
120 19 : TraStatus TraceServerSendProcess(void) { return TraceServerCreateSendThread(); }
121 :
122 30 : void TraceServerSendExit(void) { TraceServerDestroySendThread(); }
|