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