1 /*
2  * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * miscservices under the License is miscservices on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "comm_log.h"
16 #include "securec.h"
17 #include "softbus_error_code.h"
18 #include "softbus_adapter_thread.h"
19 #include "softbus_adapter_timer.h"
20 #include "softbus_common.h"
21 #include "softbus_hisysevt_common.h"
22 #include "softbus_hisysevt_transreporter.h"
23 #include "softbus_utils.h"
24 #include "softbus_adapter_mem.h"
25 #include "softbus_server_ipc_interface_code.h"
26 
27 #define STATISTIC_EVT_TRANSPORT_KPI "TRANSPORT_KPI"
28 #define STATISTIC_EVT_CALLED_API_INFO "CALLED_API_INFO"
29 #define STATISTIC_EVT_CALLED_API_CNT "CALLED_API_CNT"
30 #define STATISTIC_EVT_TRANS_OPEN_SESSION_CNT "TRANS_OPEN_SESSION_CNT"
31 #define STATISTIC_EVT_TRANS_OPEN_SESSION_TIME_COST "TRANS_OPEN_SESSION_TIME_COST"
32 
33 #define FAULT_EVT_TRANS_FAULT "TRANS_FAULT"
34 #define BEHAVIOR_EVT_TRANS_INFO "TRANS_INFO"
35 
36 #define TRANS_PARAM_LINK_TYPE "LINK_TYPE"
37 
38 #define TRANS_PARAM_APP_NAME "APP_NAME"
39 #define TRANS_PARAM_API_NAME "API_NAME"
40 
41 #define TRANS_PARAM_TOTAL_CNT "TOTAL_COUNT"
42 #define TRANS_PARAM_FAIL_TOTAL_CNT "FAIL_TOTAL_COUNT"
43 #define TRANS_PARAM_SUCCESS_CNT "SUCCESS_CNT"
44 #define TRANS_PARAM_FAIL_CNT "FAIL_CNT"
45 #define TRANS_PARAM_SUCCESS_RATE "SUCCESS_RATE"
46 
47 #define TRANS_PARAM_TOTAL_TIME "TOTAL_TIME"
48 #define TRANS_PARAM_FAIL_TOTAL_TIME "FAIL_TOTAL_TIME"
49 #define TRANS_PARAM_MAX_TIME_COST "MAX_TIME_COST"
50 #define TRANS_PARAM_MIN_TIME_COST "MIN_TIME_COST"
51 #define TRANS_PARAM_AVE_TIME_COST "AVE_TIME_COST"
52 #define TRANS_PARAM_TIMES_UNDER_500MS "TIMES_UNDER_500MS"
53 #define TRANS_PARAM_TIMES_BETWEEN_500MS_1S "TIMES_BETWEEN_500MS_1S"
54 #define TRANS_PARAM_TIMES_BETWEEN_1S_2S "TIMES_BETWEEN_1S_2S"
55 #define TRANS_PARAM_TIMES_ABOVE_2S "TIMES_ABOVE_2S"
56 #define TRANS_OPEN_TIMES_ABOVE_1S "COUNT1"
57 #define TRANS_OPEN_TIMES_ABOVE_2S "COUNT2"
58 #define TRANS_OPEN_TIMES_ABOVE_4S "COUNT3"
59 #define TRANS_OPEN_TIMES_ABOVE_7S "COUNT4"
60 #define TRANS_OPEN_TIMES_ABOVE_11S "COUNT5"
61 
62 #define TRANS_PARAM_PACKAGE_VERSION "PACKAGE_VERSION"
63 #define TRANS_PARAM_SOFTBUS_VERSION "SOFT_BUS_VERSION"
64 #define TRANS_PARAM_CALLER_PACKAGE "CALLER_PACKAGE_NAME"
65 
66 #define TRANS_PARAM_ERRCODE "ERROR_CODE"
67 #define TRANS_PARAM_INFOMSG "INFO_MSG"
68 
69 #define TIME_COST_500MS (500)
70 #define TIME_COST_1S (1000)
71 #define TIME_COST_2S (2000)
72 #define TIME_COST_4S (4000)
73 #define TIME_COST_7S (7000)
74 #define TIME_COST_11S (11000)
75 #define API_TYPE_DEFAULT (1)
76 #define API_CALLED_DEFAULT (1)
77 
78 static char g_softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN] = "softbusVersion1";
79 static char g_pkgVersion[SOFTBUS_HISYSEVT_PARAM_LEN] = "packageVersion1";
80 typedef struct {
81     uint32_t code;
82     char *apiName;
83 }ApiNameIdMap;
84 static ApiNameIdMap g_apiNameIdMapTbl[] = {
85     {MANAGE_REGISTER_SERVICE, "SoftbusRegisterService"},
86     {SERVER_CREATE_SESSION_SERVER, "CreateSessionServer"},
87     {SERVER_REMOVE_SESSION_SERVER, "RemoveSessionServer"},
88     {SERVER_OPEN_SESSION, "OpenSession"},
89     {SERVER_OPEN_AUTH_SESSION, "OpenAuthSession"},
90     {SERVER_NOTIFY_AUTH_SUCCESS, "NotifyAuthSuccess"},
91     {SERVER_CLOSE_CHANNEL, "CloseChannel"},
92     {SERVER_SESSION_SENDMSG, "SendMessage"},
93     {SERVER_JOIN_LNN, "JoinLNN"},
94     {SERVER_JOIN_METANODE, "JoinMetaNode"},
95     {SERVER_LEAVE_LNN, "LeaveLNN"},
96     {SERVER_LEAVE_METANODE, "LeaveMetaNode"},
97     {SERVER_GET_ALL_ONLINE_NODE_INFO, "GetAllOnlineNodeInfo"},
98     {SERVER_GET_LOCAL_DEVICE_INFO, "GetLocalDeviceInfo"},
99     {SERVER_GET_NODE_KEY_INFO, "GetNodeKeyInfo"},
100     {SERVER_SET_NODE_DATA_CHANGE_FLAG, "SetNodeDataChangeFlag"},
101     {SERVER_REG_DATA_LEVEL_CHANGE_CB, "RegDataChangeLevelCb"},
102     {SERVER_UNREG_DATA_LEVEL_CHANGE_CB, "UnregDataChangeLevelCb"},
103     {SERVER_SET_DATA_LEVEL, "SetDataLevel"},
104     {SERVER_START_TIME_SYNC, "StartTimeSync"},
105     {SERVER_STOP_TIME_SYNC, "StopTimeSync"},
106     {SERVER_QOS_REPORT, "QosReport"},
107     {SERVER_STREAM_STATS, "StreamStats"},
108     {SERVER_GRANT_PERMISSION, "GrantPermission"},
109     {SERVER_REMOVE_PERMISSION, "RemovePermission"},
110     {SERVER_PUBLISH_LNN, "PublishLNN"},
111     {SERVER_STOP_PUBLISH_LNN, "StopPublishLNN"},
112     {SERVER_REFRESH_LNN, "RefreshLNN"},
113     {SERVER_STOP_REFRESH_LNN, "StopRefreshLNN"},
114     {SERVER_ACTIVE_META_NODE, "ActiveMetaNode"},
115     {SERVER_DEACTIVE_META_NODE, "DeactiveMetaNode"},
116     {SERVER_GET_ALL_META_NODE_INFO, "GetAllMetaNodeInfo"},
117     {SERVER_SHIFT_LNN_GEAR, "ShiftLNNGear"},
118     {SERVER_RIPPLE_STATS, "RippleStats"},
119     {SERVER_CTRL_LNN_BLE_HB, "CtrlLNNBleHb"},
120 };
121 
122 typedef struct {
123     SoftBusMutex lock;
124     uint32_t failCnt;
125     uint32_t successCnt;
126     float successRate;
127 } OpenSessionCntStruct;
128 
129 typedef struct {
130     SoftBusMutex lock;
131     uint32_t maxTimeCost;
132     uint32_t minTimeCost;
133     uint32_t aveTimeCost;
134     uint32_t timesIn500ms;
135     uint32_t timesIn500and1s;
136     uint32_t timesIn1and2s;
137     uint32_t timesOn2s;
138 } OpenSessionTimeStruct;
139 
140 typedef struct {
141     SoftBusMutex lock;
142     int32_t linkType;
143     int64_t totalTime;
144     int32_t totalCnt;
145     int32_t successTotalCnt;
146     int64_t failTotalTime;
147     int32_t failTotalCnt;
148     char packageVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
149     char softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
150     int32_t count1;
151     int32_t count2;
152     int32_t count3;
153     int32_t count4;
154     int32_t count5;
155     char callerPackageName[SOFTBUS_HISYSEVT_PARAM_LEN];
156 } OpenSessionKpiStruct;
157 
158 typedef struct {
159     ListNode node;
160     char appName[SOFTBUS_HISYSEVT_PARAM_LEN];
161     char softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
162     char packageVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
163     int32_t cnt; //Api count
164     ListNode apiCntList;
165 } CalledApiInfoStruct;
166 
167 typedef struct {
168     ListNode node;
169     char apiName[SOFTBUS_HISYSEVT_PARAM_LEN];
170     int32_t calledtotalCnt;
171 } CalledApiCntStruct;
172 
173 static OpenSessionCntStruct g_openSessionCnt;
174 static OpenSessionTimeStruct g_openSessionTime;
175 static OpenSessionKpiStruct g_openSessionKpi;
176 static SoftBusList *g_calledApiInfoList = NULL;
177 static SoftBusList *g_calledApiCntlist = NULL;
178 
179 #define TIME_THOUSANDS_FACTOR (1000)
180 
GetSoftbusRecordTimeMillis(void)181 int64_t GetSoftbusRecordTimeMillis(void)
182 {
183     SoftBusSysTime t;
184     t.sec = 0;
185     t.usec = 0;
186     SoftBusGetTime(&t);
187     int64_t when = t.sec * TIME_THOUSANDS_FACTOR + (t.usec / TIME_THOUSANDS_FACTOR);
188     return when;
189 }
190 
ReleaseCalledApiInfoList(void)191 static void ReleaseCalledApiInfoList(void)
192 {
193     if (g_calledApiInfoList == NULL) {
194         COMM_LOGE(COMM_EVENT, "list NULL");
195         return;
196     }
197     if (SoftBusMutexLock(&g_calledApiInfoList->lock) != SOFTBUS_OK) {
198         COMM_LOGE(COMM_EVENT, "ReleaseCalledApiInfoList lock failed");
199         return;
200     }
201     CalledApiInfoStruct *item = NULL;
202     CalledApiInfoStruct *nextItem = NULL;
203     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
204         ListDelete(&item->node);
205         SoftBusFree(item);
206         g_calledApiInfoList->cnt--;
207     }
208     ListInit(&g_calledApiInfoList->list);
209     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
210 }
211 
ReleaseCalledApiCntList(void)212 static void ReleaseCalledApiCntList(void)
213 {
214     if (g_calledApiCntlist == NULL) {
215         COMM_LOGE(COMM_EVENT, "list NULL");
216         return;
217     }
218     if (SoftBusMutexLock(&g_calledApiCntlist->lock) != SOFTBUS_OK) {
219         COMM_LOGE(COMM_EVENT, "ReleaseCalledApiCntList lock failed");
220         return;
221     }
222     CalledApiCntStruct *item = NULL;
223     CalledApiCntStruct *nextItem = NULL;
224     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_calledApiCntlist->list, CalledApiCntStruct, node) {
225         ListDelete(&item->node);
226         SoftBusFree(item);
227         g_calledApiCntlist->cnt--;
228     }
229     ListInit(&g_calledApiCntlist->list);
230     (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
231 }
232 
GetNewApiCnt(char * apiName)233 static CalledApiCntStruct *GetNewApiCnt(char *apiName)
234 {
235     CalledApiCntStruct *apiCnt = (CalledApiCntStruct *)SoftBusMalloc(sizeof(CalledApiCntStruct));
236     if (apiCnt == NULL) {
237         COMM_LOGE(COMM_EVENT, "GetNewApiCnt malloc failed");
238         return NULL;
239     }
240     if (strcpy_s(apiCnt->apiName, SOFTBUS_HISYSEVT_PARAM_LEN, apiName) != EOK) {
241         COMM_LOGE(COMM_EVENT, "GetNewApiCnt strcpy failed");
242         SoftBusFree(apiCnt);
243         return NULL;
244     }
245     ListInit(&apiCnt->node);
246     apiCnt->calledtotalCnt = API_CALLED_DEFAULT;
247     return apiCnt;
248 }
249 
GetNewApiInfo(const char * appName,char * apiName)250 static CalledApiInfoStruct *GetNewApiInfo(const char *appName, char *apiName)
251 {
252     CalledApiInfoStruct *apiInfo = (CalledApiInfoStruct *)SoftBusMalloc(sizeof(CalledApiInfoStruct));
253     if (apiInfo == NULL) {
254         COMM_LOGE(COMM_EVENT, "GetNewApiInfo malloc failed");
255         return NULL;
256     }
257     if (strcpy_s(apiInfo->appName, SOFTBUS_HISYSEVT_PARAM_LEN, appName) != EOK ||
258         strcpy_s(apiInfo->softbusVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_softbusVersion) != EOK ||
259         strcpy_s(apiInfo->packageVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_pkgVersion) != EOK) {
260         COMM_LOGE(COMM_EVENT, "GetNewApiInfo strcpy failed");
261         SoftBusFree(apiInfo);
262         return NULL;
263     }
264     ListInit(&apiInfo->node);
265     ListInit(&apiInfo->apiCntList);
266     CalledApiCntStruct *apiCnt = GetNewApiCnt(apiName);
267     if (apiCnt == NULL) {
268         COMM_LOGE(COMM_EVENT, "GetNewApiCnt return NULL");
269         SoftBusFree(apiInfo);
270         return NULL;
271     }
272     ListAdd(&apiInfo->apiCntList, &apiCnt->node);
273     apiInfo->cnt = API_TYPE_DEFAULT;
274     return apiInfo;
275 }
276 
GetApiNameByCode(uint32_t code)277 static char *GetApiNameByCode(uint32_t code)
278 {
279     for (uint32_t i = 0; i < sizeof(g_apiNameIdMapTbl) / sizeof(ApiNameIdMap); i++) {
280         if (g_apiNameIdMapTbl[i].code == code) {
281             return g_apiNameIdMapTbl[i].apiName;
282         }
283     }
284     return NULL;
285 }
286 
AddInfoNodeToList(bool isAppDiff,const char * appName,char * apiName)287 static void AddInfoNodeToList(bool isAppDiff, const char *appName, char *apiName)
288 {
289     CalledApiInfoStruct *apiInfoNode = NULL;
290     if (isAppDiff) {
291         apiInfoNode = GetNewApiInfo(appName, apiName);
292         if (apiInfoNode == NULL) {
293             COMM_LOGE(COMM_EVENT, "GetNewApiInfo fail");
294             return;
295         }
296         ListAdd(&g_calledApiInfoList->list, &apiInfoNode->node);
297         g_calledApiInfoList->cnt++;
298         COMM_LOGD(COMM_EVENT, "GetNewApiInfo success");
299     }
300 }
301 
SoftbusRecordCalledApiInfo(const char * appName,uint32_t code)302 void SoftbusRecordCalledApiInfo(const char *appName, uint32_t code)
303 {
304     if (g_calledApiInfoList == NULL) {
305         COMM_LOGE(COMM_EVENT, "g_calledApiInfoList is null");
306         return;
307     }
308     if (SoftBusMutexLock(&g_calledApiInfoList->lock) != SOFTBUS_OK) {
309         COMM_LOGE(COMM_EVENT, "SoftbusRecordCalledApiInfo lock fail");
310         return;
311     }
312     char *apiName = GetApiNameByCode(code);
313     if (apiName == NULL) {
314         (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
315         COMM_LOGE(COMM_EVENT, "GetApiNameByCode fail");
316         return;
317     }
318 
319     CalledApiInfoStruct *apiInfoNode = NULL;
320     CalledApiCntStruct *apiCntNode = NULL;
321     bool isAppDiff = true;
322     bool isApiDiff = true;
323     LIST_FOR_EACH_ENTRY(apiInfoNode, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
324         if (strcmp(apiInfoNode->appName, appName) == 0) {
325             isAppDiff = false;
326             LIST_FOR_EACH_ENTRY(apiCntNode, &apiInfoNode->apiCntList, CalledApiCntStruct, node) {
327                 if (strcmp(apiCntNode->apiName, apiName) == 0) {
328                     isApiDiff = false;
329                     apiCntNode->calledtotalCnt++;
330                     COMM_LOGD(COMM_EVENT, "cmpare apiName success");
331                     break;
332                 }
333             }
334         }
335     }
336     AddInfoNodeToList(isAppDiff, appName, apiName);
337     if ((isAppDiff == false) && (isApiDiff == true)) {
338         apiInfoNode = NULL;
339         LIST_FOR_EACH_ENTRY(apiInfoNode, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
340             if (strcmp(apiInfoNode->appName, appName) == 0) {
341                 apiCntNode = GetNewApiCnt(apiName);
342                 if (apiCntNode == NULL) {
343                     COMM_LOGE(COMM_EVENT, "GetNewApiCnt fail");
344                     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
345                     return;
346                 }
347                 ListAdd(&apiInfoNode->apiCntList, &apiCntNode->node);
348                 apiInfoNode->cnt++;
349                 COMM_LOGD(COMM_EVENT, "GetNewApiCnt success");
350             }
351         }
352     }
353     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
354 }
355 
SoftbusRecordCalledApiCnt(uint32_t code)356 void SoftbusRecordCalledApiCnt(uint32_t code)
357 {
358     if (g_calledApiCntlist == NULL) {
359         COMM_LOGE(COMM_EVENT, "g_calledApiCntlist is null");
360         return;
361     }
362     if (SoftBusMutexLock(&g_calledApiCntlist->lock) != SOFTBUS_OK) {
363         COMM_LOGE(COMM_EVENT, "SoftbusRecordCalledApiCnt lock fail");
364         return;
365     }
366     char *apiName = GetApiNameByCode(code);
367     if (apiName == NULL) {
368         (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
369         return;
370     }
371 
372     CalledApiCntStruct *apiCntNode = NULL;
373     bool isDiff = true;
374     LIST_FOR_EACH_ENTRY(apiCntNode, &g_calledApiCntlist->list, CalledApiCntStruct, node) {
375         if (strcmp(apiCntNode->apiName, apiName) == 0) {
376             isDiff = false;
377             apiCntNode->calledtotalCnt++;
378             break;
379         }
380     }
381     if (isDiff == true) {
382         apiCntNode = GetNewApiCnt(apiName);
383         if (apiCntNode == NULL) {
384             COMM_LOGE(COMM_EVENT, "GetNewApiCnt fail");
385             (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
386             return;
387         }
388         ListAdd(&g_calledApiCntlist->list, &apiCntNode->node);
389         g_calledApiCntlist->cnt++;
390     }
391     (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
392 }
393 
SoftbusRecordOpenSessionKpi(const char * pkgName,int32_t linkType,SoftBusOpenSessionStatus isSucc,int64_t time)394 void SoftbusRecordOpenSessionKpi(const char *pkgName, int32_t linkType, SoftBusOpenSessionStatus isSucc, int64_t time)
395 {
396     if (SoftBusMutexLock(&g_openSessionKpi.lock) != SOFTBUS_OK) {
397         return;
398     }
399     g_openSessionKpi.linkType = linkType;
400 
401     g_openSessionKpi.failTotalCnt += (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC);
402     g_openSessionKpi.successTotalCnt += (isSucc == SOFTBUS_EVT_OPEN_SESSION_SUCC);
403     g_openSessionKpi.totalCnt = g_openSessionKpi.failTotalCnt + g_openSessionKpi.successTotalCnt;
404 
405     (void)strcpy_s(g_openSessionKpi.softbusVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_softbusVersion);
406     (void)strcpy_s(g_openSessionKpi.packageVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_pkgVersion);
407     (void)strcpy_s(g_openSessionKpi.callerPackageName, SOFTBUS_HISYSEVT_PARAM_LEN, pkgName);
408     g_openSessionKpi.totalTime = time;
409     if (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC) {
410         g_openSessionKpi.failTotalTime = time;
411     }
412 
413     if (time > TIME_COST_1S && time <= TIME_COST_2S) {
414         g_openSessionKpi.count1++;
415     } else if (time > TIME_COST_2S && time <= TIME_COST_4S) {
416         g_openSessionKpi.count2++;
417     } else if (time > TIME_COST_4S && time <= TIME_COST_7S) {
418         g_openSessionKpi.count3++;
419     } else if (time > TIME_COST_7S && time <= TIME_COST_11S) {
420         g_openSessionKpi.count4++;
421     } else if (time > TIME_COST_11S) {
422         g_openSessionKpi.count5++;
423     }
424     (void)SoftBusMutexUnlock(&g_openSessionKpi.lock);
425 }
426 
SoftbusRecordOpenSession(SoftBusOpenSessionStatus isSucc,uint32_t time)427 void SoftbusRecordOpenSession(SoftBusOpenSessionStatus isSucc, uint32_t time)
428 {
429     if (SoftBusMutexLock(&g_openSessionCnt.lock) != SOFTBUS_OK) {
430         return;
431     }
432 
433     g_openSessionCnt.failCnt += (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC);
434     g_openSessionCnt.successCnt += (isSucc == SOFTBUS_EVT_OPEN_SESSION_SUCC);
435     uint32_t totalCnt = g_openSessionCnt.failCnt + g_openSessionCnt.successCnt;
436     if (totalCnt != 0) {
437         g_openSessionCnt.successRate = (float)(g_openSessionCnt.successCnt) / (float)(totalCnt);
438     }
439 
440     (void)SoftBusMutexUnlock(&g_openSessionCnt.lock);
441 
442     if (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC) {
443         return;
444     }
445 
446     if (SoftBusMutexLock(&g_openSessionTime.lock) != SOFTBUS_OK) {
447         return;
448     }
449 
450     if (time > g_openSessionTime.maxTimeCost) {
451         g_openSessionTime.maxTimeCost = time;
452     } else if (time < g_openSessionTime.minTimeCost) {
453         g_openSessionTime.minTimeCost = time;
454     }
455 
456     if (g_openSessionCnt.successCnt != 0) {
457         uint64_t totalTimeCost = (g_openSessionTime.aveTimeCost) * (g_openSessionCnt.successCnt - 1) + time;
458         g_openSessionTime.aveTimeCost = (uint32_t)(totalTimeCost / g_openSessionCnt.successCnt);
459     }
460 
461     if (time < TIME_COST_500MS) {
462         g_openSessionTime.timesIn500ms++;
463     } else if (time < TIME_COST_1S) {
464         g_openSessionTime.timesIn500and1s++;
465     } else if (time < TIME_COST_2S) {
466         g_openSessionTime.timesIn1and2s++;
467     } else {
468         g_openSessionTime.timesOn2s++;
469     }
470 
471     (void)SoftBusMutexUnlock(&g_openSessionTime.lock);
472 }
473 
ClearOpenSessionKpi(void)474 static inline void ClearOpenSessionKpi(void)
475 {
476     memset_s(&g_openSessionKpi.linkType, sizeof(OpenSessionKpiStruct) - sizeof(SoftBusMutex),
477         0, sizeof(OpenSessionKpiStruct) - sizeof(SoftBusMutex));
478 }
479 
ClearOpenSessionCnt(void)480 static inline void ClearOpenSessionCnt(void)
481 {
482     memset_s(&g_openSessionCnt.failCnt, sizeof(OpenSessionCntStruct) - sizeof(SoftBusMutex),
483         0, sizeof(OpenSessionCntStruct) - sizeof(SoftBusMutex));
484 }
485 
ClearOpenSessionTime(void)486 static inline void ClearOpenSessionTime(void)
487 {
488     memset_s(&g_openSessionTime.maxTimeCost, sizeof(OpenSessionTimeStruct) - sizeof(SoftBusMutex),
489         0, sizeof(OpenSessionTimeStruct) - sizeof(SoftBusMutex));
490 }
491 
InitOpenSessionEvtMutexLock(void)492 static inline int32_t InitOpenSessionEvtMutexLock(void)
493 {
494     SoftBusMutexAttr mutexAttr = {SOFTBUS_MUTEX_RECURSIVE};
495     if (SoftBusMutexInit(&g_openSessionCnt.lock, &mutexAttr) != SOFTBUS_OK ||
496         SoftBusMutexInit(&g_openSessionTime.lock, &mutexAttr) != SOFTBUS_OK ||
497         SoftBusMutexInit(&g_openSessionKpi.lock, &mutexAttr) != SOFTBUS_OK) {
498         return SOFTBUS_DFX_INIT_FAILED;
499     }
500     return SOFTBUS_OK;
501 }
502 
CreateCalledApiInfoMsg(SoftBusEvtReportMsg * msg,CalledApiCntStruct * apiCntItem,char * appName,char * softbusVersion,char * packageVersion)503 static void CreateCalledApiInfoMsg(SoftBusEvtReportMsg* msg, CalledApiCntStruct *apiCntItem,
504     char *appName, char *softbusVersion, char *packageVersion)
505 {
506     // event
507     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_CALLED_API_INFO);
508     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
509     msg->paramNum = SOFTBUS_EVT_PARAM_FIVE;
510     // param 0
511     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
512     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_APP_NAME);
513     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
514     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, appName);
515     // param 1
516     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
517     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SOFTBUS_VERSION);
518     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
519     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, softbusVersion);
520     // param 2
521     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
522     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_PACKAGE_VERSION);
523     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
524     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, packageVersion);
525     // param 3
526     param = &msg->paramArray[SOFTBUS_EVT_PARAM_THREE];
527     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_API_NAME);
528     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
529     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, apiCntItem->apiName);
530     // param 4
531     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FOUR];
532     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_CNT);
533     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
534     param->paramValue.i32v = apiCntItem->calledtotalCnt;
535 }
536 
CreateCalledApiCntMsg(SoftBusEvtReportMsg * msg,CalledApiCntStruct * apiCntItem)537 static void CreateCalledApiCntMsg(SoftBusEvtReportMsg* msg, CalledApiCntStruct *apiCntItem)
538 {
539     // event
540     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_CALLED_API_CNT);
541     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
542     msg->paramNum = SOFTBUS_EVT_PARAM_TWO;
543     // param 0
544     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
545     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_API_NAME);
546     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
547     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, apiCntItem->apiName);
548     // param 1
549     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
550     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_CNT);
551     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
552     param->paramValue.i32v = apiCntItem->calledtotalCnt;
553 }
554 
CreateOpenSessionKpiMsg(SoftBusEvtReportMsg * msg)555 static void CreateOpenSessionKpiMsg(SoftBusEvtReportMsg* msg)
556 {
557     if (SoftBusMutexLock(&g_openSessionKpi.lock) != SOFTBUS_OK) {
558         return;
559     }
560     // event
561     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_TRANSPORT_KPI);
562     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
563     msg->paramNum = SOFTBUS_EVT_PARAM_THIRTEEN;
564     // param 0
565     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
566     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_LINK_TYPE);
567     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
568     param->paramValue.i32v = g_openSessionKpi.linkType;
569     // param 1
570     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
571     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_TIME);
572     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT64;
573     param->paramValue.i64v = g_openSessionKpi.totalTime;
574     // param 2
575     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
576     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_CNT);
577     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
578     param->paramValue.i32v = g_openSessionKpi.totalCnt;
579     // param 3
580     param = &msg->paramArray[SOFTBUS_EVT_PARAM_THREE];
581     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_FAIL_TOTAL_TIME);
582     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT64;
583     param->paramValue.i64v = g_openSessionKpi.failTotalTime;
584     // param 4
585     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FOUR];
586     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_FAIL_TOTAL_CNT);
587     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
588     param->paramValue.i32v = g_openSessionKpi.failTotalCnt;
589     // param 5
590     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FIVE];
591     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_PACKAGE_VERSION);
592     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
593     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, g_openSessionKpi.packageVersion);
594     // param 6
595     param = &msg->paramArray[SOFTBUS_EVT_PARAM_SIX];
596     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SOFTBUS_VERSION);
597     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
598     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, g_openSessionKpi.softbusVersion);
599     // param 7
600     param = &msg->paramArray[SOFTBUS_EVT_PARAM_SEVEN];
601     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_1S);
602     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
603     param->paramValue.i32v = g_openSessionKpi.count1;
604     // param 8
605     param = &msg->paramArray[SOFTBUS_EVT_PARAM_EIGHT];
606     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_2S);
607     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
608     param->paramValue.i32v = g_openSessionKpi.count2;
609     // param 9
610     param = &msg->paramArray[SOFTBUS_EVT_PARAM_NINE];
611     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_4S);
612     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
613     param->paramValue.i32v = g_openSessionKpi.count3;
614     // param 10
615     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TEN];
616     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_7S);
617     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
618     param->paramValue.i32v = g_openSessionKpi.count4;
619     // param 11
620     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ELEVEN];
621     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_11S);
622     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
623     param->paramValue.i32v = g_openSessionKpi.count5;
624     // param 12
625     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWELVE];
626     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_CALLER_PACKAGE);
627     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
628     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, g_openSessionKpi.callerPackageName);
629     ClearOpenSessionKpi();
630     (void)SoftBusMutexUnlock(&g_openSessionKpi.lock);
631 }
632 
CreateOpenSessionCntMsg(SoftBusEvtReportMsg * msg)633 static void CreateOpenSessionCntMsg(SoftBusEvtReportMsg* msg)
634 {
635     if (SoftBusMutexLock(&g_openSessionCnt.lock) != SOFTBUS_OK) {
636         COMM_LOGE(COMM_EVENT, "CreateOpenSessionCntMsg lock fail");
637         return;
638     }
639 
640     // event
641     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_TRANS_OPEN_SESSION_CNT);
642     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
643     msg->paramNum = SOFTBUS_EVT_PARAM_THREE;
644 
645     // param 0
646     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
647     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SUCCESS_CNT);
648     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
649     param->paramValue.u32v = g_openSessionCnt.successCnt;
650 
651     // param 1
652     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
653     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_FAIL_CNT);
654     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
655     param->paramValue.u32v = (g_openSessionCnt.failCnt);
656 
657     // param 2
658     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
659     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SUCCESS_RATE);
660     param->paramType = SOFTBUS_EVT_PARAMTYPE_FLOAT;
661     param->paramValue.f = g_openSessionCnt.successRate;
662 
663     ClearOpenSessionCnt();
664 
665     (void)SoftBusMutexUnlock(&g_openSessionCnt.lock);
666 }
667 
SoftbusReportCalledAPIEvt(void)668 static int32_t SoftbusReportCalledAPIEvt(void)
669 {
670     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_FIVE);
671     if (msg == NULL) {
672         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
673         return SOFTBUS_MALLOC_ERR;
674     }
675     if (g_calledApiInfoList == NULL) {
676         COMM_LOGE(COMM_EVENT, "g_calledApiInfoList is null");
677         SoftbusFreeEvtReportMsg(msg);
678         return SOFTBUS_NO_INIT;
679     }
680     if (SoftBusMutexLock(&g_calledApiInfoList->lock) != SOFTBUS_OK) {
681         COMM_LOGE(COMM_EVENT, "SoftbusReportCalledAPIEvt lock fail");
682         SoftbusFreeEvtReportMsg(msg);
683         return SOFTBUS_LOCK_ERR;
684     }
685     char appName[SOFTBUS_HISYSEVT_PARAM_LEN];
686     char softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
687     char packageVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
688     CalledApiInfoStruct *apiInfoItem = NULL;
689     CalledApiCntStruct *apiCntItem = NULL;
690     int32_t ret = SOFTBUS_OK;
691     LIST_FOR_EACH_ENTRY(apiInfoItem, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
692         (void)strcpy_s(appName, SOFTBUS_HISYSEVT_NAME_LEN, apiInfoItem->appName);
693         (void)strcpy_s(softbusVersion, SOFTBUS_HISYSEVT_NAME_LEN, apiInfoItem->softbusVersion);
694         (void)strcpy_s(packageVersion, SOFTBUS_HISYSEVT_NAME_LEN, apiInfoItem->packageVersion);
695         LIST_FOR_EACH_ENTRY(apiCntItem, &apiInfoItem->apiCntList, CalledApiCntStruct, node) {
696             CreateCalledApiInfoMsg(msg, apiCntItem, appName, softbusVersion, packageVersion);
697             ret = SoftbusWriteHisEvt(msg);
698             if (ret != SOFTBUS_OK) {
699                 SoftbusFreeEvtReportMsg(msg);
700                 (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
701                 ReleaseCalledApiInfoList();
702                 return ret;
703             }
704         }
705     }
706     SoftbusFreeEvtReportMsg(msg);
707     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
708     ReleaseCalledApiInfoList();
709     return SOFTBUS_OK;
710 }
711 
SoftbusReportCalledAPICntEvt(void)712 static int32_t SoftbusReportCalledAPICntEvt(void)
713 {
714     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_TWO);
715     if (msg == NULL) {
716         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
717         return SOFTBUS_MALLOC_ERR;
718     }
719     if (g_calledApiCntlist == NULL) {
720         COMM_LOGE(COMM_EVENT, "g_calledApiCntlist is null");
721         SoftbusFreeEvtReportMsg(msg);
722         return SOFTBUS_NO_INIT;
723     }
724     if (SoftBusMutexLock(&g_calledApiCntlist->lock) != SOFTBUS_OK) {
725         SoftbusFreeEvtReportMsg(msg);
726         return SOFTBUS_LOCK_ERR;
727     }
728     CalledApiCntStruct *apiCntItem = NULL;
729     int32_t ret = SOFTBUS_OK;
730     LIST_FOR_EACH_ENTRY(apiCntItem, &g_calledApiCntlist->list, CalledApiCntStruct, node) {
731         CreateCalledApiCntMsg(msg, apiCntItem);
732         ret = SoftbusWriteHisEvt(msg);
733         if (ret != SOFTBUS_OK) {
734             SoftbusFreeEvtReportMsg(msg);
735             (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
736             ReleaseCalledApiCntList();
737             return ret;
738         }
739     }
740     SoftbusFreeEvtReportMsg(msg);
741     (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
742     ReleaseCalledApiCntList();
743     return SOFTBUS_OK;
744 }
745 
SoftbusReportOpenSessionKpiEvt(void)746 static int32_t SoftbusReportOpenSessionKpiEvt(void)
747 {
748     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_THIRTEEN);
749     if (msg == NULL) {
750         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
751         return SOFTBUS_MALLOC_ERR;
752     }
753     CreateOpenSessionKpiMsg(msg);
754     int ret = SoftbusWriteHisEvt(msg);
755     SoftbusFreeEvtReportMsg(msg);
756     return ret;
757 }
758 
SoftbusReportOpenSessionCntEvt(void)759 static int32_t SoftbusReportOpenSessionCntEvt(void)
760 {
761     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_THREE);
762     if (msg == NULL) {
763         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
764         return SOFTBUS_MALLOC_ERR;
765     }
766     CreateOpenSessionCntMsg(msg);
767     int ret = SoftbusWriteHisEvt(msg);
768     SoftbusFreeEvtReportMsg(msg);
769 
770     return ret;
771 }
772 
CreateOpenSessionTimeMsg(SoftBusEvtReportMsg * msg)773 static void CreateOpenSessionTimeMsg(SoftBusEvtReportMsg* msg)
774 {
775     if (SoftBusMutexLock(&g_openSessionTime.lock) != SOFTBUS_OK) {
776         COMM_LOGE(COMM_EVENT, "CreateOpenSessionTimeMsg lock fail");
777         return;
778     }
779 
780     // event
781     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_TRANS_OPEN_SESSION_TIME_COST);
782     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
783     msg->paramNum = SOFTBUS_EVT_PARAM_SEVEN;
784 
785     // param 0
786     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
787     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_MAX_TIME_COST);
788     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
789     param->paramValue.u32v = g_openSessionTime.maxTimeCost;
790 
791     // param 1
792     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
793     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_MIN_TIME_COST);
794     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
795     param->paramValue.u32v = g_openSessionTime.minTimeCost;
796 
797     // param 2
798     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
799     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_AVE_TIME_COST);
800     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
801     param->paramValue.u32v = g_openSessionTime.aveTimeCost;
802 
803     // param 3
804     param = &msg->paramArray[SOFTBUS_EVT_PARAM_THREE];
805     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_UNDER_500MS);
806     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
807     param->paramValue.u32v = g_openSessionTime.timesIn500ms;
808 
809     // param 4
810     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FOUR];
811     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_BETWEEN_500MS_1S);
812     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
813     param->paramValue.u32v = g_openSessionTime.timesIn500and1s;
814 
815     // param 5
816     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FIVE];
817     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_BETWEEN_1S_2S);
818     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
819     param->paramValue.u32v = g_openSessionTime.timesIn1and2s;
820 
821     // param 6
822     param = &msg->paramArray[SOFTBUS_EVT_PARAM_SIX];
823     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_ABOVE_2S);
824     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
825     param->paramValue.u32v = g_openSessionTime.timesOn2s;
826 
827     ClearOpenSessionTime();
828 
829     (void)SoftBusMutexUnlock(&g_openSessionTime.lock);
830 }
831 
SoftbusReportOpenSessionTimeEvt(void)832 static int32_t SoftbusReportOpenSessionTimeEvt(void)
833 {
834     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_SEVEN);
835     if (msg == NULL) {
836         COMM_LOGE(COMM_EVENT, "SoftbusCreateEvtReportMsg fail");
837         return SOFTBUS_MALLOC_ERR;
838     }
839     CreateOpenSessionTimeMsg(msg);
840     int ret = SoftbusWriteHisEvt(msg);
841     SoftbusFreeEvtReportMsg(msg);
842 
843     return ret;
844 }
845 
CreateTransErrMsg(SoftBusEvtReportMsg * msg,int32_t errcode)846 static inline void CreateTransErrMsg(SoftBusEvtReportMsg* msg, int32_t errcode)
847 {
848     // event
849     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, FAULT_EVT_TRANS_FAULT);
850     msg->evtType = SOFTBUS_EVT_TYPE_FAULT;
851     msg->paramNum = SOFTBUS_EVT_PARAM_ONE;
852 
853     // param 0
854     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
855     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_ERRCODE);
856     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
857     param->paramValue.i32v = errcode;
858 }
859 
CreateTransInfoMsg(SoftBusEvtReportMsg * msg,const char * infoMsg)860 static inline void CreateTransInfoMsg(SoftBusEvtReportMsg* msg, const char *infoMsg)
861 {
862     // event
863     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, BEHAVIOR_EVT_TRANS_INFO);
864     msg->evtType = SOFTBUS_EVT_TYPE_BEHAVIOR;
865     msg->paramNum = SOFTBUS_EVT_PARAM_ONE;
866 
867     // param 0
868     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
869     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_INFOMSG);
870     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
871     (void)strcpy_s(param->paramValue.str, sizeof(param->paramValue.str), infoMsg);
872 }
873 
SoftbusReportTransErrorEvt(int32_t errcode)874 void SoftbusReportTransErrorEvt(int32_t errcode)
875 {
876     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_ONE);
877     if (msg == NULL) {
878         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
879         return;
880     }
881     errcode = GetErrorCodeEx(errcode);
882     CreateTransErrMsg(msg, errcode);
883     int ret = SoftbusWriteHisEvt(msg);
884     SoftbusFreeEvtReportMsg(msg);
885 
886     if (ret != SOFTBUS_OK) {
887         COMM_LOGE(COMM_EVENT, "Sys Evt Witre FAIL! errCode=%{public}d", errcode);
888     }
889 }
890 
SoftbusReportTransInfoEvt(const char * infoMsg)891 void SoftbusReportTransInfoEvt(const char *infoMsg)
892 {
893     if (infoMsg == NULL) {
894         COMM_LOGE(COMM_EVENT, "infoMsg is null");
895         return;
896     }
897     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_ONE);
898     if (msg == NULL) {
899         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
900         return;
901     }
902     CreateTransInfoMsg(msg, infoMsg);
903     int ret = SoftbusWriteHisEvt(msg);
904     SoftbusFreeEvtReportMsg(msg);
905 
906     if (ret != SOFTBUS_OK) {
907         COMM_LOGE(COMM_EVENT, "Sys Evt Witre FAIL! errMsg=%{public}s", infoMsg);
908     }
909 }
910 
InitTransStatisticSysEvt(void)911 int32_t InitTransStatisticSysEvt(void)
912 {
913     if (InitOpenSessionEvtMutexLock() != SOFTBUS_OK) {
914         COMM_LOGE(COMM_EVENT, "Trans Statistic Evt Lock Init Fail!");
915         return SOFTBUS_DFX_INIT_FAILED;
916     }
917 
918     g_calledApiInfoList = CreateSoftBusList();
919     g_calledApiCntlist = CreateSoftBusList();
920     ClearOpenSessionCnt();
921     ClearOpenSessionKpi();
922     ClearOpenSessionTime();
923 
924     SetStatisticEvtReportFunc(SOFTBUS_STATISTIC_EVT_TRANS_OPEN_SESSION_CNT, SoftbusReportOpenSessionCntEvt);
925     SetStatisticEvtReportFunc(SOFTBUS_STATISTIC_EVT_TRANS_OPEN_SESSION_KPI, SoftbusReportOpenSessionKpiEvt);
926     SetStatisticEvtReportFunc(TRANSPORT_API_CALLED_INFO_STATISTIC_EVENT, SoftbusReportCalledAPIEvt);
927     SetStatisticEvtReportFunc(TRANSPORT_API_CALLED_CNT_STATISTIC_EVENT, SoftbusReportCalledAPICntEvt);
928     SetStatisticEvtReportFunc(SOFTBUS_STATISTIC_EVT_TRANS_OPEN_SESSION_TIME_COST, SoftbusReportOpenSessionTimeEvt);
929     return SOFTBUS_OK;
930 }
931 
DeinitTransStatisticSysEvt(void)932 void DeinitTransStatisticSysEvt(void)
933 {
934     if (g_calledApiInfoList == NULL || g_calledApiCntlist == NULL) {
935         COMM_LOGE(COMM_EVENT, "g_calledApiInfoList or g_calledApiCntlist is NULL");
936         return;
937     }
938     DestroySoftBusList(g_calledApiInfoList);
939     DestroySoftBusList(g_calledApiCntlist);
940     g_calledApiInfoList = NULL;
941     g_calledApiCntlist = NULL;
942 }
943