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 : #include "trace_send_mgr.h"
11 : #include "trace_server_mgr.h"
12 : #include "trace_system_api.h"
13 : #include "adx_component_api_c.h"
14 : #include "trace_queue.h"
15 : #include "adiag_print.h"
16 : #include "trace_session_mgr.h"
17 : #include "trace_node.h"
18 : #include "trace_adx_api.h"
19 :
20 : STATIC TraceThread g_traceSendThread = 0;
21 : STATIC bool g_traceSendThreadState = false;
22 :
23 21 : TraStatus TraceServiceInit(int32_t devId)
24 : {
25 21 : TraStatus ret = AdxRegisterService((int32_t)HDC_SERVICE_TYPE_BBOX,
26 : COMPONENT_TRACE, TraceDeviceInit, TraceDeviceProcess, TraceDeviceExit);
27 21 : if (ret != TRACE_SUCCESS) {
28 2 : ADIAG_ERR("register component function error, ret = %d.", ret);
29 2 : return TRACE_FAILURE;
30 : }
31 19 : int32_t mode = (devId == -1) ? 0 : 1; // 0 denotes pf , 1 denotes vf
32 19 : ServerInitInfo serverInfo = {(int32_t)HDC_SERVICE_TYPE_BBOX, mode, devId};
33 19 : ret = AdxServiceStartup(serverInfo);
34 19 : if (ret != TRACE_SUCCESS) {
35 1 : ADIAG_ERR("startup component server error, ret = %d.", ret);
36 1 : return TRACE_FAILURE;
37 : }
38 18 : return TRACE_SUCCESS;
39 : }
40 :
41 1 : STATIC TraStatus TraceServerSendEndMsg(const void *handle)
42 : {
43 1 : TraceEndMsg msg = { 0 };
44 1 : msg.msgType = TRACE_END_MSG;
45 1 : TraStatus ret = TraceAdxSendMsg(handle, (const char *)&msg, sizeof(TraceEndMsg));
46 1 : TraStatus retEnd = TraceAdxSendMsg(handle, HDC_END_MSG, strlen(HDC_END_MSG));
47 1 : if ((ret != TRACE_SUCCESS) || (retEnd != TRACE_SUCCESS)) {
48 1 : ADIAG_ERR("send end msg failed, ret = %d, retEnd = %d.", ret, retEnd);
49 1 : return TRACE_FAILURE;
50 : }
51 0 : return TRACE_SUCCESS;
52 : }
53 :
54 13 : STATIC void TraceServerSendNodeToClient(SessionNode *sessionNode, int8_t listFlag)
55 : {
56 : TraStatus ret;
57 13 : TraceNode *node = TraceTsPopNode(sessionNode);
58 19 : while (node != NULL) {
59 6 : ret = TraceAdxSendMsg(sessionNode->handle, node->data, node->dataLen);
60 6 : if (ret != TRACE_SUCCESS) {
61 2 : ADIAG_ERR("send msg failed, pid = %d, data length = %u bytes.", sessionNode->pid, node->dataLen);
62 : }
63 6 : if ((listFlag == DELETED_SESSION_LIST) && (node->flag == ADIAG_INFO_FLAG_END)) {
64 1 : ret = TraceServerSendEndMsg(sessionNode->handle);
65 1 : if (ret != TRACE_SUCCESS) {
66 1 : ADIAG_ERR("send end msg failed, pid = %d.", sessionNode->pid);
67 : }
68 1 : sessionNode->timeout = 0;
69 : }
70 6 : ADIAG_DBG("send msg successfully, pid = %d, dataLen = %u bytes.", sessionNode->pid, node->dataLen);
71 6 : XFreeTraceNode(&node);
72 6 : node = TraceTsPopNode(sessionNode);
73 : }
74 13 : }
75 :
76 : /**
77 : * @brief thread to send node info to client.
78 : * @param [in] arg: NULL
79 : * @return NULL
80 : */
81 15 : STATIC void *TraceServerSendThread(void *arg)
82 : {
83 : (void)arg;
84 15 : ADIAG_RUN_INF("trace server send thread start.");
85 15 : if (TraceSetThreadName("TraceServerSend") != TRACE_SUCCESS) {
86 3 : ADIAG_WAR("can not set thread name(TraceServerSend) but continue.");
87 : }
88 40 : while (g_traceSendThreadState) {
89 25 : if (!TraceIsDeletedSessionNodeListNull()) {
90 1 : TraceServerHandleDeletedSessionNode(TraceServerSendNodeToClient);
91 : }
92 25 : if (!TraceIsSessionNodeListNull()) {
93 12 : TraceServerHandleSessionNode(TraceServerSendNodeToClient);
94 : }
95 25 : usleep(SESSION_TIME_INTERVAL * TIME_ONE_THOUSAND_MS);
96 : }
97 15 : return NULL;
98 : }
99 :
100 20 : TraStatus TraceServerCreateSendThread(void)
101 : {
102 : TraceUserBlock thread;
103 20 : thread.procFunc = TraceServerSendThread;
104 20 : thread.pulArg = NULL;
105 20 : TraceThreadAttr attr = { 0, 0, 0, 0, 0, 0, TRACE_THREAD_STACK_SIZE };
106 20 : TraceThread tid = 0;
107 20 : g_traceSendThreadState = true;
108 20 : if (TraceCreateTaskWithThreadAttr(&tid, &thread, &attr) != TRACE_SUCCESS) {
109 3 : ADIAG_ERR("create trace server send thread failed, strerr=%s.", strerror(AdiagGetErrorCode()));
110 3 : return TRACE_FAILURE;
111 : }
112 17 : g_traceSendThread = tid;
113 17 : ADIAG_RUN_INF("create trace server send thread successfully.");
114 17 : return TRACE_SUCCESS;
115 : }
116 :
117 30 : void TraceServerDestroySendThread(void)
118 : {
119 30 : if (!g_traceSendThreadState) {
120 11 : return;
121 : }
122 19 : g_traceSendThreadState = false;
123 19 : if (g_traceSendThread != 0) {
124 17 : (void)TraceJoinTask(&g_traceSendThread);
125 : }
126 19 : ADIAG_RUN_INF("destroy trace server send thread successfully.");
127 : }
|