• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 "legacy/softbus_hisysevt_common.h"
22 #include "legacy/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_TRIGGER_RANGE_FOR_MSDP, "TriggerRangeForMsdp"},
119     {SERVER_STOP_RANGE_FOR_MSDP, "StopRangeForMsdp"},
120     {SERVER_SYNC_TRUSTED_RELATION, "SyncTrustedRelationShip"},
121     {SERVER_RIPPLE_STATS, "RippleStats"},
122     {SERVER_CTRL_LNN_BLE_HB, "CtrlLNNBleHb"},
123     {SERVER_SET_DISPLAY_NAME, "SetDisplayName"},
124 };
125 
126 typedef struct {
127     SoftBusMutex lock;
128     uint32_t failCnt;
129     uint32_t successCnt;
130     float successRate;
131 } OpenSessionCntStruct;
132 
133 typedef struct {
134     SoftBusMutex lock;
135     uint32_t maxTimeCost;
136     uint32_t minTimeCost;
137     uint32_t aveTimeCost;
138     uint32_t timesIn500ms;
139     uint32_t timesIn500and1s;
140     uint32_t timesIn1and2s;
141     uint32_t timesOn2s;
142 } OpenSessionTimeStruct;
143 
144 typedef struct {
145     SoftBusMutex lock;
146     int32_t linkType;
147     int64_t totalTime;
148     int32_t totalCnt;
149     int32_t successTotalCnt;
150     int64_t failTotalTime;
151     int32_t failTotalCnt;
152     char packageVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
153     char softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
154     int32_t count1;
155     int32_t count2;
156     int32_t count3;
157     int32_t count4;
158     int32_t count5;
159     char callerPackageName[SOFTBUS_HISYSEVT_PARAM_LEN];
160 } OpenSessionKpiStruct;
161 
162 typedef struct {
163     ListNode node;
164     char appName[SOFTBUS_HISYSEVT_PARAM_LEN];
165     char softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
166     char packageVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
167     int32_t cnt; //Api count
168     ListNode apiCntList;
169 } CalledApiInfoStruct;
170 
171 typedef struct {
172     ListNode node;
173     char apiName[SOFTBUS_HISYSEVT_PARAM_LEN];
174     int32_t calledtotalCnt;
175 } CalledApiCntStruct;
176 
177 static OpenSessionCntStruct g_openSessionCnt;
178 static OpenSessionTimeStruct g_openSessionTime;
179 static OpenSessionKpiStruct g_openSessionKpi;
180 static SoftBusList *g_calledApiInfoList = NULL;
181 static SoftBusList *g_calledApiCntlist = NULL;
182 
183 #define TIME_THOUSANDS_FACTOR (1000)
184 
GetSoftbusRecordTimeMillis(void)185 int64_t GetSoftbusRecordTimeMillis(void)
186 {
187     SoftBusSysTime t;
188     t.sec = 0;
189     t.usec = 0;
190     SoftBusGetTime(&t);
191     int64_t when = t.sec * TIME_THOUSANDS_FACTOR + (t.usec / TIME_THOUSANDS_FACTOR);
192     return when;
193 }
194 
ReleaseCalledApiInfoList(void)195 static void ReleaseCalledApiInfoList(void)
196 {
197     if (g_calledApiInfoList == NULL) {
198         COMM_LOGE(COMM_EVENT, "list NULL");
199         return;
200     }
201     if (SoftBusMutexLock(&g_calledApiInfoList->lock) != SOFTBUS_OK) {
202         COMM_LOGE(COMM_EVENT, "ReleaseCalledApiInfoList lock failed");
203         return;
204     }
205     CalledApiInfoStruct *item = NULL;
206     CalledApiInfoStruct *nextItem = NULL;
207     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
208         ListDelete(&item->node);
209         SoftBusFree(item);
210         g_calledApiInfoList->cnt--;
211     }
212     ListInit(&g_calledApiInfoList->list);
213     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
214 }
215 
ReleaseCalledApiCntList(void)216 static void ReleaseCalledApiCntList(void)
217 {
218     if (g_calledApiCntlist == NULL) {
219         COMM_LOGE(COMM_EVENT, "list NULL");
220         return;
221     }
222     if (SoftBusMutexLock(&g_calledApiCntlist->lock) != SOFTBUS_OK) {
223         COMM_LOGE(COMM_EVENT, "ReleaseCalledApiCntList lock failed");
224         return;
225     }
226     CalledApiCntStruct *item = NULL;
227     CalledApiCntStruct *nextItem = NULL;
228     LIST_FOR_EACH_ENTRY_SAFE(item, nextItem, &g_calledApiCntlist->list, CalledApiCntStruct, node) {
229         ListDelete(&item->node);
230         SoftBusFree(item);
231         g_calledApiCntlist->cnt--;
232     }
233     ListInit(&g_calledApiCntlist->list);
234     (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
235 }
236 
GetNewApiCnt(char * apiName)237 static CalledApiCntStruct *GetNewApiCnt(char *apiName)
238 {
239     CalledApiCntStruct *apiCnt = (CalledApiCntStruct *)SoftBusMalloc(sizeof(CalledApiCntStruct));
240     if (apiCnt == NULL) {
241         COMM_LOGE(COMM_EVENT, "GetNewApiCnt malloc failed");
242         return NULL;
243     }
244     if (strcpy_s(apiCnt->apiName, SOFTBUS_HISYSEVT_PARAM_LEN, apiName) != EOK) {
245         COMM_LOGE(COMM_EVENT, "GetNewApiCnt strcpy failed");
246         SoftBusFree(apiCnt);
247         return NULL;
248     }
249     ListInit(&apiCnt->node);
250     apiCnt->calledtotalCnt = API_CALLED_DEFAULT;
251     return apiCnt;
252 }
253 
GetNewApiInfo(const char * appName,char * apiName)254 static CalledApiInfoStruct *GetNewApiInfo(const char *appName, char *apiName)
255 {
256     CalledApiInfoStruct *apiInfo = (CalledApiInfoStruct *)SoftBusMalloc(sizeof(CalledApiInfoStruct));
257     if (apiInfo == NULL) {
258         COMM_LOGE(COMM_EVENT, "GetNewApiInfo malloc failed");
259         return NULL;
260     }
261     if (strcpy_s(apiInfo->appName, SOFTBUS_HISYSEVT_PARAM_LEN, appName) != EOK ||
262         strcpy_s(apiInfo->softbusVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_softbusVersion) != EOK ||
263         strcpy_s(apiInfo->packageVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_pkgVersion) != EOK) {
264         COMM_LOGE(COMM_EVENT, "GetNewApiInfo strcpy failed");
265         SoftBusFree(apiInfo);
266         return NULL;
267     }
268     ListInit(&apiInfo->node);
269     ListInit(&apiInfo->apiCntList);
270     CalledApiCntStruct *apiCnt = GetNewApiCnt(apiName);
271     if (apiCnt == NULL) {
272         COMM_LOGE(COMM_EVENT, "GetNewApiCnt return NULL");
273         SoftBusFree(apiInfo);
274         return NULL;
275     }
276     ListAdd(&apiInfo->apiCntList, &apiCnt->node);
277     apiInfo->cnt = API_TYPE_DEFAULT;
278     return apiInfo;
279 }
280 
GetApiNameByCode(uint32_t code)281 static char *GetApiNameByCode(uint32_t code)
282 {
283     for (uint32_t i = 0; i < sizeof(g_apiNameIdMapTbl) / sizeof(ApiNameIdMap); i++) {
284         if (g_apiNameIdMapTbl[i].code == code) {
285             return g_apiNameIdMapTbl[i].apiName;
286         }
287     }
288     return NULL;
289 }
290 
AddInfoNodeToList(bool isAppDiff,const char * appName,char * apiName)291 static void AddInfoNodeToList(bool isAppDiff, const char *appName, char *apiName)
292 {
293 #define MAX_PKG_NAME_CNT 200
294     CalledApiInfoStruct *apiInfoNode = NULL;
295     if (isAppDiff) {
296         if (g_calledApiInfoList->cnt > MAX_PKG_NAME_CNT) {
297             COMM_LOGE(COMM_EVENT, "the number %{public}u of callers exceeds the limit", g_calledApiInfoList->cnt);
298             return;
299         }
300         apiInfoNode = GetNewApiInfo(appName, apiName);
301         if (apiInfoNode == NULL) {
302             COMM_LOGE(COMM_EVENT, "GetNewApiInfo fail");
303             return;
304         }
305         ListAdd(&g_calledApiInfoList->list, &apiInfoNode->node);
306         g_calledApiInfoList->cnt++;
307         COMM_LOGD(COMM_EVENT, "GetNewApiInfo success");
308     }
309 }
310 
SoftbusRecordCalledApiInfo(const char * appName,uint32_t code)311 void SoftbusRecordCalledApiInfo(const char *appName, uint32_t code)
312 {
313     COMM_CHECK_AND_RETURN_LOGE(appName != NULL, COMM_EVENT, "app name is null");
314     COMM_CHECK_AND_RETURN_LOGE(g_calledApiInfoList != NULL, COMM_EVENT, "g_calledApiInfoList is null");
315     COMM_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(&g_calledApiInfoList->lock) == SOFTBUS_OK,
316         COMM_EVENT, "SoftbusRecordCalledApiInfo lock fail");
317     char *apiName = GetApiNameByCode(code);
318     if (apiName == NULL) {
319         (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
320         COMM_LOGE(COMM_EVENT, "GetApiNameByCode fail");
321         return;
322     }
323 
324     CalledApiInfoStruct *apiInfoNode = NULL;
325     CalledApiCntStruct *apiCntNode = NULL;
326     bool isAppDiff = true;
327     bool isApiDiff = true;
328     LIST_FOR_EACH_ENTRY(apiInfoNode, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
329         if (strcmp(apiInfoNode->appName, appName) == 0) {
330             isAppDiff = false;
331             LIST_FOR_EACH_ENTRY(apiCntNode, &apiInfoNode->apiCntList, CalledApiCntStruct, node) {
332                 if (strcmp(apiCntNode->apiName, apiName) == 0) {
333                     isApiDiff = false;
334                     apiCntNode->calledtotalCnt++;
335                     COMM_LOGD(COMM_EVENT, "cmpare apiName success");
336                     break;
337                 }
338             }
339         }
340     }
341     AddInfoNodeToList(isAppDiff, appName, apiName);
342     if ((isAppDiff == false) && (isApiDiff == true)) {
343         apiInfoNode = NULL;
344         LIST_FOR_EACH_ENTRY(apiInfoNode, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
345             if (strcmp(apiInfoNode->appName, appName) == 0) {
346                 apiCntNode = GetNewApiCnt(apiName);
347                 if (apiCntNode == NULL) {
348                     COMM_LOGE(COMM_EVENT, "GetNewApiCnt fail");
349                     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
350                     return;
351                 }
352                 ListAdd(&apiInfoNode->apiCntList, &apiCntNode->node);
353                 apiInfoNode->cnt++;
354                 COMM_LOGD(COMM_EVENT, "GetNewApiCnt success");
355             }
356         }
357     }
358     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
359 }
360 
SoftbusRecordCalledApiCnt(uint32_t code)361 void SoftbusRecordCalledApiCnt(uint32_t code)
362 {
363     if (g_calledApiCntlist == NULL) {
364         COMM_LOGE(COMM_EVENT, "g_calledApiCntlist is null");
365         return;
366     }
367     if (SoftBusMutexLock(&g_calledApiCntlist->lock) != SOFTBUS_OK) {
368         COMM_LOGE(COMM_EVENT, "SoftbusRecordCalledApiCnt lock fail");
369         return;
370     }
371     char *apiName = GetApiNameByCode(code);
372     if (apiName == NULL) {
373         (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
374         return;
375     }
376 
377     CalledApiCntStruct *apiCntNode = NULL;
378     bool isDiff = true;
379     LIST_FOR_EACH_ENTRY(apiCntNode, &g_calledApiCntlist->list, CalledApiCntStruct, node) {
380         if (strcmp(apiCntNode->apiName, apiName) == 0) {
381             isDiff = false;
382             apiCntNode->calledtotalCnt++;
383             break;
384         }
385     }
386     if (isDiff == true) {
387         apiCntNode = GetNewApiCnt(apiName);
388         if (apiCntNode == NULL) {
389             COMM_LOGE(COMM_EVENT, "GetNewApiCnt fail");
390             (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
391             return;
392         }
393         ListAdd(&g_calledApiCntlist->list, &apiCntNode->node);
394         g_calledApiCntlist->cnt++;
395     }
396     (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
397 }
398 
SoftbusRecordOpenSessionKpi(const char * pkgName,int32_t linkType,SoftBusOpenSessionStatus isSucc,int64_t time)399 void SoftbusRecordOpenSessionKpi(const char *pkgName, int32_t linkType, SoftBusOpenSessionStatus isSucc, int64_t time)
400 {
401     COMM_CHECK_AND_RETURN_LOGE(pkgName != NULL, COMM_EVENT, "pkg name is null");
402     COMM_CHECK_AND_RETURN_LOGE(SoftBusMutexLock(&g_openSessionKpi.lock) == SOFTBUS_OK, COMM_EVENT, "lock fail");
403     g_openSessionKpi.linkType = linkType;
404 
405     g_openSessionKpi.failTotalCnt += (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC);
406     g_openSessionKpi.successTotalCnt += (isSucc == SOFTBUS_EVT_OPEN_SESSION_SUCC);
407     g_openSessionKpi.totalCnt = g_openSessionKpi.failTotalCnt + g_openSessionKpi.successTotalCnt;
408 
409     (void)strcpy_s(g_openSessionKpi.softbusVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_softbusVersion);
410     (void)strcpy_s(g_openSessionKpi.packageVersion, SOFTBUS_HISYSEVT_PARAM_LEN, g_pkgVersion);
411     (void)strcpy_s(g_openSessionKpi.callerPackageName, SOFTBUS_HISYSEVT_PARAM_LEN, pkgName);
412     g_openSessionKpi.totalTime = time;
413     if (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC) {
414         g_openSessionKpi.failTotalTime = time;
415     }
416 
417     if (time > TIME_COST_1S && time <= TIME_COST_2S) {
418         g_openSessionKpi.count1++;
419     } else if (time > TIME_COST_2S && time <= TIME_COST_4S) {
420         g_openSessionKpi.count2++;
421     } else if (time > TIME_COST_4S && time <= TIME_COST_7S) {
422         g_openSessionKpi.count3++;
423     } else if (time > TIME_COST_7S && time <= TIME_COST_11S) {
424         g_openSessionKpi.count4++;
425     } else if (time > TIME_COST_11S) {
426         g_openSessionKpi.count5++;
427     }
428     (void)SoftBusMutexUnlock(&g_openSessionKpi.lock);
429 }
430 
SoftbusRecordOpenSession(SoftBusOpenSessionStatus isSucc,uint32_t time)431 void SoftbusRecordOpenSession(SoftBusOpenSessionStatus isSucc, uint32_t time)
432 {
433     if (SoftBusMutexLock(&g_openSessionCnt.lock) != SOFTBUS_OK) {
434         return;
435     }
436 
437     g_openSessionCnt.failCnt += (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC);
438     g_openSessionCnt.successCnt += (isSucc == SOFTBUS_EVT_OPEN_SESSION_SUCC);
439     uint32_t totalCnt = g_openSessionCnt.failCnt + g_openSessionCnt.successCnt;
440     if (totalCnt != 0) {
441         g_openSessionCnt.successRate = (float)(g_openSessionCnt.successCnt) / (float)(totalCnt);
442     }
443 
444     (void)SoftBusMutexUnlock(&g_openSessionCnt.lock);
445 
446     if (isSucc != SOFTBUS_EVT_OPEN_SESSION_SUCC) {
447         return;
448     }
449 
450     if (SoftBusMutexLock(&g_openSessionTime.lock) != SOFTBUS_OK) {
451         return;
452     }
453 
454     if (time > g_openSessionTime.maxTimeCost) {
455         g_openSessionTime.maxTimeCost = time;
456     } else if (time < g_openSessionTime.minTimeCost) {
457         g_openSessionTime.minTimeCost = time;
458     }
459 
460     if (g_openSessionCnt.successCnt != 0) {
461         uint64_t totalTimeCost = (uint64_t)(g_openSessionTime.aveTimeCost) * (g_openSessionCnt.successCnt - 1) + time;
462         g_openSessionTime.aveTimeCost = (uint32_t)(totalTimeCost / g_openSessionCnt.successCnt);
463     }
464 
465     if (time < TIME_COST_500MS) {
466         g_openSessionTime.timesIn500ms++;
467     } else if (time < TIME_COST_1S) {
468         g_openSessionTime.timesIn500and1s++;
469     } else if (time < TIME_COST_2S) {
470         g_openSessionTime.timesIn1and2s++;
471     } else {
472         g_openSessionTime.timesOn2s++;
473     }
474 
475     (void)SoftBusMutexUnlock(&g_openSessionTime.lock);
476 }
477 
ClearOpenSessionKpi(void)478 static inline void ClearOpenSessionKpi(void)
479 {
480     memset_s(&g_openSessionKpi.linkType, sizeof(OpenSessionKpiStruct) - sizeof(SoftBusMutex),
481         0, sizeof(OpenSessionKpiStruct) - sizeof(SoftBusMutex));
482 }
483 
ClearOpenSessionCnt(void)484 static inline void ClearOpenSessionCnt(void)
485 {
486     memset_s(&g_openSessionCnt.failCnt, sizeof(OpenSessionCntStruct) - sizeof(SoftBusMutex),
487         0, sizeof(OpenSessionCntStruct) - sizeof(SoftBusMutex));
488 }
489 
ClearOpenSessionTime(void)490 static inline void ClearOpenSessionTime(void)
491 {
492     memset_s(&g_openSessionTime.maxTimeCost, sizeof(OpenSessionTimeStruct) - sizeof(SoftBusMutex),
493         0, sizeof(OpenSessionTimeStruct) - sizeof(SoftBusMutex));
494 }
495 
InitOpenSessionEvtMutexLock(void)496 static inline int32_t InitOpenSessionEvtMutexLock(void)
497 {
498     SoftBusMutexAttr mutexAttr = {SOFTBUS_MUTEX_RECURSIVE};
499     if (SoftBusMutexInit(&g_openSessionCnt.lock, &mutexAttr) != SOFTBUS_OK ||
500         SoftBusMutexInit(&g_openSessionTime.lock, &mutexAttr) != SOFTBUS_OK ||
501         SoftBusMutexInit(&g_openSessionKpi.lock, &mutexAttr) != SOFTBUS_OK) {
502         return SOFTBUS_DFX_INIT_FAILED;
503     }
504     return SOFTBUS_OK;
505 }
506 
CreateCalledApiInfoMsg(SoftBusEvtReportMsg * msg,CalledApiCntStruct * apiCntItem,char * appName,char * softbusVersion,char * packageVersion)507 static void CreateCalledApiInfoMsg(SoftBusEvtReportMsg* msg, CalledApiCntStruct *apiCntItem,
508     char *appName, char *softbusVersion, char *packageVersion)
509 {
510     // event
511     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_CALLED_API_INFO);
512     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
513     msg->paramNum = SOFTBUS_EVT_PARAM_FIVE;
514     // param 0
515     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
516     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_APP_NAME);
517     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
518     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, appName);
519     // param 1
520     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
521     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SOFTBUS_VERSION);
522     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
523     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, softbusVersion);
524     // param 2
525     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
526     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_PACKAGE_VERSION);
527     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
528     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, packageVersion);
529     // param 3
530     param = &msg->paramArray[SOFTBUS_EVT_PARAM_THREE];
531     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_API_NAME);
532     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
533     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, apiCntItem->apiName);
534     // param 4
535     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FOUR];
536     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_CNT);
537     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
538     param->paramValue.i32v = apiCntItem->calledtotalCnt;
539 }
540 
CreateCalledApiCntMsg(SoftBusEvtReportMsg * msg,CalledApiCntStruct * apiCntItem)541 static void CreateCalledApiCntMsg(SoftBusEvtReportMsg* msg, CalledApiCntStruct *apiCntItem)
542 {
543     // event
544     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_CALLED_API_CNT);
545     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
546     msg->paramNum = SOFTBUS_EVT_PARAM_TWO;
547     // param 0
548     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
549     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_API_NAME);
550     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
551     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, apiCntItem->apiName);
552     // param 1
553     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
554     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_CNT);
555     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
556     param->paramValue.i32v = apiCntItem->calledtotalCnt;
557 }
558 
CreateOpenSessionKpiMsg(SoftBusEvtReportMsg * msg)559 static void CreateOpenSessionKpiMsg(SoftBusEvtReportMsg* msg)
560 {
561     if (SoftBusMutexLock(&g_openSessionKpi.lock) != SOFTBUS_OK) {
562         return;
563     }
564     // event
565     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_TRANSPORT_KPI);
566     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
567     msg->paramNum = SOFTBUS_EVT_PARAM_THIRTEEN;
568     // param 0
569     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
570     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_LINK_TYPE);
571     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
572     param->paramValue.i32v = g_openSessionKpi.linkType;
573     // param 1
574     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
575     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_TIME);
576     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT64;
577     param->paramValue.i64v = g_openSessionKpi.totalTime;
578     // param 2
579     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
580     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TOTAL_CNT);
581     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
582     param->paramValue.i32v = g_openSessionKpi.totalCnt;
583     // param 3
584     param = &msg->paramArray[SOFTBUS_EVT_PARAM_THREE];
585     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_FAIL_TOTAL_TIME);
586     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT64;
587     param->paramValue.i64v = g_openSessionKpi.failTotalTime;
588     // param 4
589     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FOUR];
590     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_FAIL_TOTAL_CNT);
591     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
592     param->paramValue.i32v = g_openSessionKpi.failTotalCnt;
593     // param 5
594     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FIVE];
595     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_PACKAGE_VERSION);
596     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
597     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, g_openSessionKpi.packageVersion);
598     // param 6
599     param = &msg->paramArray[SOFTBUS_EVT_PARAM_SIX];
600     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SOFTBUS_VERSION);
601     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
602     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, g_openSessionKpi.softbusVersion);
603     // param 7
604     param = &msg->paramArray[SOFTBUS_EVT_PARAM_SEVEN];
605     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_1S);
606     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
607     param->paramValue.i32v = g_openSessionKpi.count1;
608     // param 8
609     param = &msg->paramArray[SOFTBUS_EVT_PARAM_EIGHT];
610     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_2S);
611     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
612     param->paramValue.i32v = g_openSessionKpi.count2;
613     // param 9
614     param = &msg->paramArray[SOFTBUS_EVT_PARAM_NINE];
615     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_4S);
616     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
617     param->paramValue.i32v = g_openSessionKpi.count3;
618     // param 10
619     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TEN];
620     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_7S);
621     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
622     param->paramValue.i32v = g_openSessionKpi.count4;
623     // param 11
624     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ELEVEN];
625     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_OPEN_TIMES_ABOVE_11S);
626     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
627     param->paramValue.i32v = g_openSessionKpi.count5;
628     // param 12
629     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWELVE];
630     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_CALLER_PACKAGE);
631     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
632     (void)strcpy_s(param->paramValue.str, SOFTBUS_HISYSEVT_PARAM_LEN, g_openSessionKpi.callerPackageName);
633     ClearOpenSessionKpi();
634     (void)SoftBusMutexUnlock(&g_openSessionKpi.lock);
635 }
636 
CreateOpenSessionCntMsg(SoftBusEvtReportMsg * msg)637 static void CreateOpenSessionCntMsg(SoftBusEvtReportMsg* msg)
638 {
639     if (SoftBusMutexLock(&g_openSessionCnt.lock) != SOFTBUS_OK) {
640         COMM_LOGE(COMM_EVENT, "CreateOpenSessionCntMsg lock fail");
641         return;
642     }
643 
644     // event
645     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_TRANS_OPEN_SESSION_CNT);
646     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
647     msg->paramNum = SOFTBUS_EVT_PARAM_THREE;
648 
649     // param 0
650     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
651     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SUCCESS_CNT);
652     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
653     param->paramValue.u32v = g_openSessionCnt.successCnt;
654 
655     // param 1
656     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
657     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_FAIL_CNT);
658     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
659     param->paramValue.u32v = (g_openSessionCnt.failCnt);
660 
661     // param 2
662     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
663     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_SUCCESS_RATE);
664     param->paramType = SOFTBUS_EVT_PARAMTYPE_FLOAT;
665     param->paramValue.f = g_openSessionCnt.successRate;
666 
667     ClearOpenSessionCnt();
668 
669     (void)SoftBusMutexUnlock(&g_openSessionCnt.lock);
670 }
671 
SoftbusReportCalledAPIEvt(void)672 static int32_t SoftbusReportCalledAPIEvt(void)
673 {
674     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_FIVE);
675     if (msg == NULL) {
676         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
677         return SOFTBUS_MALLOC_ERR;
678     }
679     if (g_calledApiInfoList == NULL) {
680         COMM_LOGE(COMM_EVENT, "g_calledApiInfoList is null");
681         SoftbusFreeEvtReportMsg(msg);
682         return SOFTBUS_NO_INIT;
683     }
684     if (SoftBusMutexLock(&g_calledApiInfoList->lock) != SOFTBUS_OK) {
685         COMM_LOGE(COMM_EVENT, "SoftbusReportCalledAPIEvt lock fail");
686         SoftbusFreeEvtReportMsg(msg);
687         return SOFTBUS_LOCK_ERR;
688     }
689     char appName[SOFTBUS_HISYSEVT_PARAM_LEN];
690     char softbusVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
691     char packageVersion[SOFTBUS_HISYSEVT_PARAM_LEN];
692     CalledApiInfoStruct *apiInfoItem = NULL;
693     CalledApiCntStruct *apiCntItem = NULL;
694     int32_t ret = SOFTBUS_OK;
695     LIST_FOR_EACH_ENTRY(apiInfoItem, &g_calledApiInfoList->list, CalledApiInfoStruct, node) {
696         (void)strcpy_s(appName, SOFTBUS_HISYSEVT_NAME_LEN, apiInfoItem->appName);
697         (void)strcpy_s(softbusVersion, SOFTBUS_HISYSEVT_NAME_LEN, apiInfoItem->softbusVersion);
698         (void)strcpy_s(packageVersion, SOFTBUS_HISYSEVT_NAME_LEN, apiInfoItem->packageVersion);
699         LIST_FOR_EACH_ENTRY(apiCntItem, &apiInfoItem->apiCntList, CalledApiCntStruct, node) {
700             CreateCalledApiInfoMsg(msg, apiCntItem, appName, softbusVersion, packageVersion);
701             ret = SoftbusWriteHisEvt(msg);
702             if (ret != SOFTBUS_OK) {
703                 SoftbusFreeEvtReportMsg(msg);
704                 (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
705                 ReleaseCalledApiInfoList();
706                 return ret;
707             }
708         }
709     }
710     SoftbusFreeEvtReportMsg(msg);
711     (void)SoftBusMutexUnlock(&g_calledApiInfoList->lock);
712     ReleaseCalledApiInfoList();
713     return SOFTBUS_OK;
714 }
715 
SoftbusReportCalledAPICntEvt(void)716 static int32_t SoftbusReportCalledAPICntEvt(void)
717 {
718     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_TWO);
719     if (msg == NULL) {
720         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
721         return SOFTBUS_MALLOC_ERR;
722     }
723     if (g_calledApiCntlist == NULL) {
724         COMM_LOGE(COMM_EVENT, "g_calledApiCntlist is null");
725         SoftbusFreeEvtReportMsg(msg);
726         return SOFTBUS_NO_INIT;
727     }
728     if (SoftBusMutexLock(&g_calledApiCntlist->lock) != SOFTBUS_OK) {
729         SoftbusFreeEvtReportMsg(msg);
730         return SOFTBUS_LOCK_ERR;
731     }
732     CalledApiCntStruct *apiCntItem = NULL;
733     int32_t ret = SOFTBUS_OK;
734     LIST_FOR_EACH_ENTRY(apiCntItem, &g_calledApiCntlist->list, CalledApiCntStruct, node) {
735         CreateCalledApiCntMsg(msg, apiCntItem);
736         ret = SoftbusWriteHisEvt(msg);
737         if (ret != SOFTBUS_OK) {
738             SoftbusFreeEvtReportMsg(msg);
739             (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
740             ReleaseCalledApiCntList();
741             return ret;
742         }
743     }
744     SoftbusFreeEvtReportMsg(msg);
745     (void)SoftBusMutexUnlock(&g_calledApiCntlist->lock);
746     ReleaseCalledApiCntList();
747     return SOFTBUS_OK;
748 }
749 
SoftbusReportOpenSessionKpiEvt(void)750 static int32_t SoftbusReportOpenSessionKpiEvt(void)
751 {
752     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_THIRTEEN);
753     if (msg == NULL) {
754         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
755         return SOFTBUS_MALLOC_ERR;
756     }
757     CreateOpenSessionKpiMsg(msg);
758     int ret = SoftbusWriteHisEvt(msg);
759     SoftbusFreeEvtReportMsg(msg);
760     return ret;
761 }
762 
SoftbusReportOpenSessionCntEvt(void)763 static int32_t SoftbusReportOpenSessionCntEvt(void)
764 {
765     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_THREE);
766     if (msg == NULL) {
767         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
768         return SOFTBUS_MALLOC_ERR;
769     }
770     CreateOpenSessionCntMsg(msg);
771     int ret = SoftbusWriteHisEvt(msg);
772     SoftbusFreeEvtReportMsg(msg);
773 
774     return ret;
775 }
776 
CreateOpenSessionTimeMsg(SoftBusEvtReportMsg * msg)777 static void CreateOpenSessionTimeMsg(SoftBusEvtReportMsg* msg)
778 {
779     if (SoftBusMutexLock(&g_openSessionTime.lock) != SOFTBUS_OK) {
780         COMM_LOGE(COMM_EVENT, "CreateOpenSessionTimeMsg lock fail");
781         return;
782     }
783 
784     // event
785     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, STATISTIC_EVT_TRANS_OPEN_SESSION_TIME_COST);
786     msg->evtType = SOFTBUS_EVT_TYPE_STATISTIC;
787     msg->paramNum = SOFTBUS_EVT_PARAM_SEVEN;
788 
789     // param 0
790     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
791     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_MAX_TIME_COST);
792     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
793     param->paramValue.u32v = g_openSessionTime.maxTimeCost;
794 
795     // param 1
796     param = &msg->paramArray[SOFTBUS_EVT_PARAM_ONE];
797     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_MIN_TIME_COST);
798     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
799     param->paramValue.u32v = g_openSessionTime.minTimeCost;
800 
801     // param 2
802     param = &msg->paramArray[SOFTBUS_EVT_PARAM_TWO];
803     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_AVE_TIME_COST);
804     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
805     param->paramValue.u32v = g_openSessionTime.aveTimeCost;
806 
807     // param 3
808     param = &msg->paramArray[SOFTBUS_EVT_PARAM_THREE];
809     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_UNDER_500MS);
810     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
811     param->paramValue.u32v = g_openSessionTime.timesIn500ms;
812 
813     // param 4
814     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FOUR];
815     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_BETWEEN_500MS_1S);
816     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
817     param->paramValue.u32v = g_openSessionTime.timesIn500and1s;
818 
819     // param 5
820     param = &msg->paramArray[SOFTBUS_EVT_PARAM_FIVE];
821     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_BETWEEN_1S_2S);
822     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
823     param->paramValue.u32v = g_openSessionTime.timesIn1and2s;
824 
825     // param 6
826     param = &msg->paramArray[SOFTBUS_EVT_PARAM_SIX];
827     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_TIMES_ABOVE_2S);
828     param->paramType = SOFTBUS_EVT_PARAMTYPE_UINT32;
829     param->paramValue.u32v = g_openSessionTime.timesOn2s;
830 
831     ClearOpenSessionTime();
832 
833     (void)SoftBusMutexUnlock(&g_openSessionTime.lock);
834 }
835 
SoftbusReportOpenSessionTimeEvt(void)836 static int32_t SoftbusReportOpenSessionTimeEvt(void)
837 {
838     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_SEVEN);
839     if (msg == NULL) {
840         COMM_LOGE(COMM_EVENT, "SoftbusCreateEvtReportMsg fail");
841         return SOFTBUS_MALLOC_ERR;
842     }
843     CreateOpenSessionTimeMsg(msg);
844     int ret = SoftbusWriteHisEvt(msg);
845     SoftbusFreeEvtReportMsg(msg);
846 
847     return ret;
848 }
849 
CreateTransErrMsg(SoftBusEvtReportMsg * msg,int32_t errcode)850 static inline void CreateTransErrMsg(SoftBusEvtReportMsg* msg, int32_t errcode)
851 {
852     // event
853     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, FAULT_EVT_TRANS_FAULT);
854     msg->evtType = SOFTBUS_EVT_TYPE_FAULT;
855     msg->paramNum = SOFTBUS_EVT_PARAM_ONE;
856 
857     // param 0
858     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
859     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_ERRCODE);
860     param->paramType = SOFTBUS_EVT_PARAMTYPE_INT32;
861     param->paramValue.i32v = errcode;
862 }
863 
CreateTransInfoMsg(SoftBusEvtReportMsg * msg,const char * infoMsg)864 static inline void CreateTransInfoMsg(SoftBusEvtReportMsg* msg, const char *infoMsg)
865 {
866     // event
867     (void)strcpy_s(msg->evtName, SOFTBUS_HISYSEVT_NAME_LEN, BEHAVIOR_EVT_TRANS_INFO);
868     msg->evtType = SOFTBUS_EVT_TYPE_BEHAVIOR;
869     msg->paramNum = SOFTBUS_EVT_PARAM_ONE;
870 
871     // param 0
872     SoftBusEvtParam* param = &msg->paramArray[SOFTBUS_EVT_PARAM_ZERO];
873     (void)strcpy_s(param->paramName, SOFTBUS_HISYSEVT_NAME_LEN, TRANS_PARAM_INFOMSG);
874     param->paramType = SOFTBUS_EVT_PARAMTYPE_STRING;
875     (void)strcpy_s(param->paramValue.str, sizeof(param->paramValue.str), infoMsg);
876 }
877 
SoftbusReportTransErrorEvt(int32_t errcode)878 void SoftbusReportTransErrorEvt(int32_t errcode)
879 {
880     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_ONE);
881     if (msg == NULL) {
882         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
883         return;
884     }
885     errcode = GetErrorCodeEx(errcode);
886     CreateTransErrMsg(msg, errcode);
887     int ret = SoftbusWriteHisEvt(msg);
888     SoftbusFreeEvtReportMsg(msg);
889 
890     if (ret != SOFTBUS_OK) {
891         COMM_LOGE(COMM_EVENT, "Sys Evt Witre FAIL! errCode=%{public}d", errcode);
892     }
893 }
894 
SoftbusReportTransInfoEvt(const char * infoMsg)895 void SoftbusReportTransInfoEvt(const char *infoMsg)
896 {
897     if (infoMsg == NULL) {
898         COMM_LOGE(COMM_EVENT, "infoMsg is null");
899         return;
900     }
901     SoftBusEvtReportMsg* msg = SoftbusCreateEvtReportMsg(SOFTBUS_EVT_PARAM_ONE);
902     if (msg == NULL) {
903         COMM_LOGE(COMM_EVENT, "Alloc EvtReport Msg Fail!");
904         return;
905     }
906     CreateTransInfoMsg(msg, infoMsg);
907     int ret = SoftbusWriteHisEvt(msg);
908     SoftbusFreeEvtReportMsg(msg);
909 
910     if (ret != SOFTBUS_OK) {
911         COMM_LOGE(COMM_EVENT, "Sys Evt Witre FAIL! errMsg=%{public}s", infoMsg);
912     }
913 }
914 
DeinitOpenSessionEvtMutexLock(void)915 static void DeinitOpenSessionEvtMutexLock(void)
916 {
917     SoftBusMutexDestroy(&g_openSessionCnt.lock);
918     SoftBusMutexDestroy(&g_openSessionTime.lock);
919     SoftBusMutexDestroy(&g_openSessionKpi.lock);
920 }
921 
InitTransStatisticSysEvt(void)922 int32_t InitTransStatisticSysEvt(void)
923 {
924     if (InitOpenSessionEvtMutexLock() != SOFTBUS_OK) {
925         COMM_LOGE(COMM_EVENT, "Trans Statistic Evt Lock Init Fail!");
926         DeinitOpenSessionEvtMutexLock();
927         return SOFTBUS_DFX_INIT_FAILED;
928     }
929 
930     g_calledApiInfoList = CreateSoftBusList();
931     if (g_calledApiInfoList == NULL) {
932         COMM_LOGE(COMM_EVENT, "Create g_calledApiInfoList failed.");
933         return SOFTBUS_CREATE_LIST_ERR;
934     }
935     g_calledApiCntlist = CreateSoftBusList();
936     if (g_calledApiCntlist == NULL) {
937         COMM_LOGE(COMM_EVENT, "Create g_calledApiCntlist failed.");
938         return SOFTBUS_CREATE_LIST_ERR;
939     }
940     ClearOpenSessionCnt();
941     ClearOpenSessionKpi();
942     ClearOpenSessionTime();
943 
944     SetStatisticEvtReportFunc(SOFTBUS_STATISTIC_EVT_TRANS_OPEN_SESSION_CNT, SoftbusReportOpenSessionCntEvt);
945     SetStatisticEvtReportFunc(SOFTBUS_STATISTIC_EVT_TRANS_OPEN_SESSION_KPI, SoftbusReportOpenSessionKpiEvt);
946     SetStatisticEvtReportFunc(TRANSPORT_API_CALLED_INFO_STATISTIC_EVENT, SoftbusReportCalledAPIEvt);
947     SetStatisticEvtReportFunc(TRANSPORT_API_CALLED_CNT_STATISTIC_EVENT, SoftbusReportCalledAPICntEvt);
948     SetStatisticEvtReportFunc(SOFTBUS_STATISTIC_EVT_TRANS_OPEN_SESSION_TIME_COST, SoftbusReportOpenSessionTimeEvt);
949     return SOFTBUS_OK;
950 }
951 
DeinitTransStatisticSysEvt(void)952 void DeinitTransStatisticSysEvt(void)
953 {
954     if (g_calledApiInfoList == NULL || g_calledApiCntlist == NULL) {
955         COMM_LOGE(COMM_EVENT, "g_calledApiInfoList or g_calledApiCntlist is NULL");
956         return;
957     }
958     DestroySoftBusList(g_calledApiInfoList);
959     DestroySoftBusList(g_calledApiCntlist);
960     g_calledApiInfoList = NULL;
961     g_calledApiCntlist = NULL;
962 }
963