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 23 : TraStatus TraceServiceInit(int32_t devId)
24 : {
25 23 : TraStatus ret = AdxRegisterService(
26 : (int32_t)HDC_SERVICE_TYPE_BBOX, COMPONENT_TRACE, TraceDeviceInit, TraceDeviceProcess, TraceDeviceExit);
27 23 : if (ret != TRACE_SUCCESS) {
28 2 : ADIAG_ERR("register component function error, ret = %d.", ret);
29 2 : return TRACE_FAILURE;
30 : }
31 21 : int32_t mode = (devId == -1) ? 0 : 1; // 0 denotes pf , 1 denotes vf
32 21 : ServerInitInfo serverInfo = {(int32_t)HDC_SERVICE_TYPE_BBOX, mode, devId};
33 21 : ret = AdxServiceStartup(serverInfo);
34 21 : if (ret != TRACE_SUCCESS) {
35 1 : ADIAG_ERR("startup component server error, ret = %d.", ret);
36 1 : return TRACE_FAILURE;
37 : }
38 20 : 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 11 : STATIC void TraceServerSendNodeToClient(SessionNode* sessionNode, int8_t listFlag)
55 : {
56 : TraStatus ret;
57 11 : TraceNode* node = TraceTsPopNode(sessionNode);
58 13 : while (node != NULL) {
59 2 : ret = TraceAdxSendMsg(sessionNode->handle, node->data, node->dataLen);
60 2 : if (ret != TRACE_SUCCESS) {
61 2 : ADIAG_ERR("send msg failed, pid = %d, data length = %u bytes.", sessionNode->pid, node->dataLen);
62 : }
63 2 : 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 2 : ADIAG_DBG("send msg successfully, pid = %d, dataLen = %u bytes.", sessionNode->pid, node->dataLen);
71 2 : XFreeTraceNode(&node);
72 2 : node = TraceTsPopNode(sessionNode);
73 : }
74 11 : }
75 :
76 : /**
77 : * @brief thread to send node info to client.
78 : * @param [in] arg: NULL
79 : * @return NULL
80 : */
81 17 : STATIC void* TraceServerSendThread(void* arg)
82 : {
83 : (void)arg;
84 17 : ADIAG_RUN_INF("trace server send thread start.");
85 17 : if (TraceSetThreadName("TraceServerSend") != TRACE_SUCCESS) {
86 3 : ADIAG_WAR("can not set thread name(TraceServerSend) but continue.");
87 : }
88 40 : while (g_traceSendThreadState) {
89 23 : if (!TraceIsDeletedSessionNodeListNull()) {
90 1 : TraceServerHandleDeletedSessionNode(TraceServerSendNodeToClient);
91 : }
92 23 : if (!TraceIsSessionNodeListNull()) {
93 10 : TraceServerHandleSessionNode(TraceServerSendNodeToClient);
94 : }
95 23 : usleep(SESSION_TIME_INTERVAL * TIME_ONE_THOUSAND_MS);
96 : }
97 17 : return NULL;
98 : }
99 :
100 22 : TraStatus TraceServerCreateSendThread(void)
101 : {
102 : TraceUserBlock thread;
103 22 : thread.procFunc = TraceServerSendThread;
104 22 : thread.pulArg = NULL;
105 22 : TraceThreadAttr attr = {0, 0, 0, 0, 0, 0, TRACE_THREAD_STACK_SIZE};
106 22 : TraceThread tid = 0;
107 22 : g_traceSendThreadState = true;
108 22 : 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 19 : g_traceSendThread = tid;
113 19 : ADIAG_RUN_INF("create trace server send thread successfully.");
114 19 : return TRACE_SUCCESS;
115 : }
116 :
117 32 : void TraceServerDestroySendThread(void)
118 : {
119 32 : if (!g_traceSendThreadState) {
120 11 : return;
121 : }
122 21 : g_traceSendThreadState = false;
123 21 : if (g_traceSendThread != 0) {
124 19 : (void)TraceJoinTask(&g_traceSendThread);
125 : }
126 21 : ADIAG_RUN_INF("destroy trace server send thread successfully.");
127 : }
|