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 18 : void TraceServerSetDevId(int32_t devId)
22 : {
23 18 : g_devId = devId;
24 18 : }
25 :
26 27 : STATIC INLINE int32_t TraceServerGetDevId(void)
27 : {
28 27 : return g_devId;
29 : }
30 :
31 18 : TraStatus TraceServerMgrProcess(void)
32 : {
33 18 : TraStatus ret = TraceServerSessionInit();
34 18 : if (ret != TRACE_SUCCESS) {
35 0 : ADIAG_ERR("trace server session init failed.");
36 0 : return TRACE_FAILURE;
37 : }
38 :
39 18 : ret = TraceServiceInit(g_devId);
40 18 : if (ret != TRACE_SUCCESS) {
41 1 : ADIAG_ERR("trace server init failed.");
42 1 : return TRACE_FAILURE;
43 : }
44 17 : return TRACE_SUCCESS;
45 : }
46 :
47 28 : void TraceServerMgrExit(void)
48 : {
49 28 : TraceServerSessionExit();
50 28 : }
51 :
52 10 : STATIC TraStatus GetDevNumIDs(uint32_t *deviceNum, uint32_t *deviceIdArray)
53 : {
54 10 : int32_t devNum = 0;
55 10 : int32_t devId[MAX_DEV_NUM] = { 0 };
56 10 : int32_t ret = log_get_device_id(devId, &devNum, MAX_DEV_NUM);
57 10 : if ((ret != 0) || (devNum > MAX_DEV_NUM) || (devNum < 0)) {
58 2 : ADIAG_ERR("get device id failed, result=%d, device_number=%d.", ret, devNum);
59 2 : return TRACE_FAILURE;
60 : }
61 8 : *deviceNum = (uint32_t)devNum;
62 8 : int32_t idx = 0;
63 16 : for (; idx < devNum; idx++) {
64 8 : if ((devId[idx] >= 0) && (devId[idx] < MAX_DEV_NUM)) {
65 8 : deviceIdArray[idx] = (uint32_t)devId[idx];
66 : }
67 : }
68 8 : return TRACE_SUCCESS;
69 : }
70 :
71 10 : STATIC TraStatus TraceServerPfProcess(void)
72 : {
73 10 : uint32_t deviceIdArray[MAX_DEV_NUM] = { 0U }; // device-side device id array
74 10 : uint32_t devNum = 0;
75 10 : TraStatus ret = GetDevNumIDs(&devNum, deviceIdArray);
76 10 : if (ret != TRACE_SUCCESS) {
77 2 : return TRACE_FAILURE;
78 : }
79 8 : ret = KtraceTsCreateThread(devNum, deviceIdArray);
80 8 : if (ret != TRACE_SUCCESS) {
81 1 : ADIAG_ERR("create trace ts thread failed.");
82 1 : return TRACE_FAILURE;
83 : }
84 7 : ret = TraceServerCreateSocketRecv(-1);
85 7 : if (ret != TRACE_SUCCESS) {
86 1 : ADIAG_ERR("create socket receive failed.");
87 1 : return TRACE_FAILURE;
88 : }
89 :
90 6 : return TRACE_SUCCESS;
91 : }
92 :
93 4 : STATIC TraStatus TraceServerVfProcess(uint32_t devId)
94 : {
95 4 : uint32_t deviceIdArray[1] = { devId };
96 4 : TraStatus ret = KtraceTsCreateThread(1U, deviceIdArray);
97 4 : if (ret != TRACE_SUCCESS) {
98 1 : ADIAG_ERR("create trace ts thread failed.");
99 1 : return TRACE_FAILURE;
100 : }
101 3 : ret = TraceServerCreateSocketRecv((int32_t)devId);
102 3 : if (ret != TRACE_SUCCESS) {
103 1 : ADIAG_ERR("create socket receive failed.");
104 1 : return TRACE_FAILURE;
105 : }
106 2 : return TRACE_SUCCESS;
107 : }
108 :
109 15 : TraStatus TraceServerRecvProcess(void)
110 : {
111 15 : int32_t devId = TraceServerGetDevId();
112 15 : if (devId == -1) {
113 10 : return TraceServerPfProcess();
114 5 : } else if ((devId >= MIN_VFID_NUM) && (devId <= MAX_VFID_NUM)) {
115 4 : return TraceServerVfProcess((uint32_t)devId);
116 : } else {
117 1 : ADIAG_ERR("invalid device id(%d).", devId);
118 1 : return TRACE_FAILURE;
119 : }
120 : }
121 :
122 28 : void TraceServerRecvExit(void)
123 : {
124 28 : TraceServerDestroySocketRecv();
125 28 : KtraceTsDestroyThread();
126 28 : }
127 :
128 17 : TraStatus TraceServerSendProcess(void)
129 : {
130 17 : return TraceServerCreateSendThread();
131 : }
132 :
133 28 : void TraceServerSendExit(void)
134 : {
135 28 : TraceServerDestroySendThread();
136 28 : }
|