• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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  * distributed under the License is distributed 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 
16 #define LOG_TAG "HiViewAdapter"
17 
18 #include "hiview_adapter.h"
19 
20 #include <thread>
21 #include <unistd.h>
22 
23 #include "log_print.h"
24 #include "utils/anonymous.h"
25 #include "utils/time_utils.h"
26 namespace OHOS {
27 namespace DistributedDataDfx {
28 using namespace DistributedKv;
29 using Anonymous = DistributedData::Anonymous;
30 namespace {
31 constexpr const char *DATAMGR_DOMAIN = "DISTDATAMGR";
32 // fault key
33 constexpr const char *FAULT_TYPE = "FAULT_TYPE";
34 constexpr const char *MODULE_NAME = "MODULE_NAME";
35 constexpr const char *INTERFACE_NAME = "INTERFACE_NAME";
36 constexpr const char *ERROR_TYPE = "ERROR_TYPE";
37 constexpr const char *SYNC_ERROR_INFO = "SYNC_ERROR_INFO";
38 // Database statistic
39 constexpr const char *USER_ID = "ANONYMOUS_UID";
40 constexpr const char *APP_ID = "APP_ID";
41 constexpr const char *STORE_ID = "STORE_ID";
42 constexpr const char *DB_SIZE = "DB_SIZE";
43 // interface visit statistic
44 constexpr const char *TIMES = "TIMES";
45 constexpr const char *DEVICE_ID = "ANONYMOUS_DID";
46 constexpr const char *SEND_SIZE = "SEND_SIZE";
47 constexpr const char *RECEIVED_SIZE = "RECEIVED_SIZE";
48 constexpr const char *AVERAGE_TIMES = "AVERAGE_TIME";
49 constexpr const char *WORST_TIMES = "WORST_TIME";
50 constexpr const char *INTERFACES = "INTERFACES";
51 constexpr const char *TAG = "TAG";
52 constexpr const char *POWERSTATS = "PowerStats";
53 // behaviour key
54 constexpr const char *BEHAVIOUR_INFO = "BEHAVIOUR_INFO";
55 constexpr const char *CHANNEL = "CHANNEL";
56 constexpr const char *DATA_SIZE = "DATA_SIZE";
57 constexpr const char *DATA_TYPE = "DATA_TYPE";
58 constexpr const char *OPERATION = "OPERATION";
59 constexpr const char *RESULT = "RESULT";
60 
61 const std::map<int, std::string> EVENT_COVERT_TABLE = {
62     { DfxCodeConstant::SERVICE_FAULT, "SERVICE_FAULT" },
63     { DfxCodeConstant::RUNTIME_FAULT, "RUNTIME_FAULT" },
64     { DfxCodeConstant::DATABASE_FAULT, "DATABASE_FAULT" },
65     { DfxCodeConstant::COMMUNICATION_FAULT, "COMMUNICATION_FAULT" },
66     { DfxCodeConstant::DATABASE_STATISTIC, "DATABASE_STATISTIC}" },
67     { DfxCodeConstant::VISIT_STATISTIC, "VISIT_STATISTIC" },
68     { DfxCodeConstant::TRAFFIC_STATISTIC, "VISIT_STATISTIC" },
69     { DfxCodeConstant::DATABASE_PERFORMANCE_STATISTIC, "DATABASE_PERFORMANCE_STATISTIC" },
70     { DfxCodeConstant::API_PERFORMANCE_STATISTIC, "API_PERFORMANCE_STATISTIC" },
71     { DfxCodeConstant::API_PERFORMANCE_INTERFACE, "API_PERFORMANCE_STATISTIC" },
72     { DfxCodeConstant::DATABASE_SYNC_FAILED, "DATABASE_SYNC_FAILED" },
73     { DfxCodeConstant::DATABASE_CORRUPTED_FAILED, "DATABASE_CORRUPTED_FAILED" },
74     { DfxCodeConstant::DATABASE_REKEY_FAILED, "DATABASE_REKEY_FAILED" },
75     { DfxCodeConstant::DATABASE_BEHAVIOUR, "DATABASE_BEHAVIOUR" },
76     { DfxCodeConstant::UDMF_DATA_BEHAVIOR, "UDMF_DATA_BEHAVIOR" },
77     { DfxCodeConstant::ARKDATA_CLOUD_SYNC_FAULT, "ARKDATA_CLOUD_SYNC_FAULT" },
78 };
79 }
80 std::mutex HiViewAdapter::visitMutex_;
81 std::map<std::string, StatisticWrap<VisitStat>> HiViewAdapter::visitStat_;
82 
83 std::mutex HiViewAdapter::trafficMutex_;
84 std::map<std::string, StatisticWrap<TrafficStat>> HiViewAdapter::trafficStat_;
85 
86 std::mutex HiViewAdapter::dbMutex_;
87 std::map<std::string, StatisticWrap<DbStat>> HiViewAdapter::dbStat_;
88 
89 std::mutex HiViewAdapter::apiPerformanceMutex_;
90 std::map<std::string, StatisticWrap<ApiPerformanceStat>> HiViewAdapter::apiPerformanceStat_;
91 
92 bool HiViewAdapter::running_ = false;
93 std::mutex HiViewAdapter::runMutex_;
94 
ReportFault(int dfxCode,const FaultMsg & msg,std::shared_ptr<ExecutorPool> executors)95 void HiViewAdapter::ReportFault(int dfxCode, const FaultMsg &msg, std::shared_ptr<ExecutorPool> executors)
96 {
97     if (executors == nullptr) {
98         ZLOGW("executors is nullptr!");
99         return;
100     }
101     ExecutorPool::Task task([dfxCode, msg]() {
102         struct HiSysEventParam params[] = {
103             { .name = { *FAULT_TYPE },
104                 .t = HISYSEVENT_INT32,
105                 .v = { .i32 = static_cast<int32_t>(msg.faultType) },
106                 .arraySize = 0 },
107             { .name = { *MODULE_NAME },
108                 .t = HISYSEVENT_STRING,
109                 .v = { .s = const_cast<char *>(msg.moduleName.c_str()) },
110                 .arraySize = 0 },
111             { .name = { *INTERFACE_NAME },
112                 .t = HISYSEVENT_STRING,
113                 .v = { .s = const_cast<char *>(msg.interfaceName.c_str()) },
114                 .arraySize = 0 },
115             { .name = { *ERROR_TYPE },
116                 .t = HISYSEVENT_INT32,
117                 .v = { .i32 = static_cast<int32_t>(msg.errorType) },
118                 .arraySize = 0 },
119         };
120         OH_HiSysEvent_Write(
121             DATAMGR_DOMAIN,
122             CoverEventID(dfxCode).c_str(),
123             HISYSEVENT_FAULT,
124             params,
125             sizeof(params) / sizeof(params[0])
126         );
127     });
128     executors->Execute(std::move(task));
129 }
130 
ReportDBFault(int dfxCode,const DBFaultMsg & msg,std::shared_ptr<ExecutorPool> executors)131 void HiViewAdapter::ReportDBFault(int dfxCode, const DBFaultMsg &msg, std::shared_ptr<ExecutorPool> executors)
132 {
133     if (executors == nullptr) {
134         ZLOGW("executors is nullptr!");
135         return;
136     }
137     ExecutorPool::Task task([dfxCode, msg]() {
138         std::string storeId = Anonymous::Change(msg.storeId);
139         struct HiSysEventParam params[] = {
140             { .name = { *APP_ID },
141                 .t = HISYSEVENT_STRING,
142                 .v = { .s = const_cast<char *>(msg.appId.c_str()) },
143                 .arraySize = 0 },
144             { .name = { *STORE_ID },
145                 .t = HISYSEVENT_STRING,
146                 .v = { .s = const_cast<char *>(storeId.c_str()) },
147                 .arraySize = 0 },
148             { .name = { *MODULE_NAME },
149                 .t = HISYSEVENT_STRING,
150                 .v = { .s = const_cast<char *>(msg.moduleName.c_str()) },
151                 .arraySize = 0 },
152             { .name = { *ERROR_TYPE },
153                 .t = HISYSEVENT_INT32,
154                 .v = { .i32 = static_cast<int32_t>(msg.errorType) },
155                 .arraySize = 0 },
156         };
157         OH_HiSysEvent_Write(
158             DATAMGR_DOMAIN,
159             CoverEventID(dfxCode).c_str(),
160             HISYSEVENT_FAULT,
161             params,
162             sizeof(params) / sizeof(params[0])
163         );
164     });
165     executors->Execute(std::move(task));
166 }
167 
ReportCommFault(int dfxCode,const CommFaultMsg & msg,std::shared_ptr<ExecutorPool> executors)168 void HiViewAdapter::ReportCommFault(int dfxCode, const CommFaultMsg &msg, std::shared_ptr<ExecutorPool> executors)
169 {
170     if (executors == nullptr) {
171         ZLOGW("executors is nullptr!");
172         return;
173     }
174     ExecutorPool ::Task task([dfxCode, msg]() {
175         std::string message;
176         std::string storeId = Anonymous::Change(msg.storeId);
177         for (size_t i = 0; i < msg.deviceId.size(); i++) {
178             message.append("No: ").append(std::to_string(i))
179                 .append(" sync to device: ").append(msg.deviceId[i])
180                 .append(" has error, errCode:").append(std::to_string(msg.errorCode[i])).append(". ");
181         }
182         struct HiSysEventParam params[] = {
183             { .name = { *USER_ID },
184                 .t = HISYSEVENT_STRING,
185                 .v = { .s = const_cast<char *>(msg.userId.c_str()) },
186                 .arraySize = 0 },
187             { .name = { *APP_ID },
188                 .t = HISYSEVENT_STRING,
189                 .v = { .s = const_cast<char *>(msg.appId.c_str()) },
190                 .arraySize = 0 },
191             { .name = { *STORE_ID },
192                 .t = HISYSEVENT_STRING,
193                 .v = { .s = const_cast<char *>(storeId.c_str()) },
194                 .arraySize = 0 },
195             { .name = { *SYNC_ERROR_INFO },
196                 .t = HISYSEVENT_STRING,
197                 .v = { .s = const_cast<char *>(message.c_str()) },
198                 .arraySize = 0 },
199         };
200         OH_HiSysEvent_Write(
201             DATAMGR_DOMAIN,
202             CoverEventID(dfxCode).c_str(),
203             HISYSEVENT_FAULT,
204             params,
205             sizeof(params) / sizeof(params[0])
206         );
207     });
208     executors->Execute(std::move(task));
209 }
210 
ReportArkDataFault(int dfxCode,const ArkDataFaultMsg & msg,std::shared_ptr<ExecutorPool> executors)211 void HiViewAdapter::ReportArkDataFault(int dfxCode, const ArkDataFaultMsg &msg, std::shared_ptr<ExecutorPool> executors)
212 {
213     if (executors == nullptr) {
214         ZLOGW("executors is nullptr!");
215         return;
216     }
217     ExecutorPool::Task task([dfxCode, msg]() {
218         std::string occurTime = DistributedData::TimeUtils::GetCurSysTimeWithMs();
219         std::string bundleName = msg.bundleName;
220         std::string moduleName = msg.moduleName;
221         std::string storeName = msg.storeName;
222         std::string businessType = msg.businessType;
223         std::string appendix = msg.appendixMsg;
224         std::string faultType = msg.faultType;
225         struct HiSysEventParam params[] = {
226             { .name = { "FAULT_TIME" }, .t = HISYSEVENT_STRING, .v = { .s = occurTime.data() }, .arraySize = 0 },
227             { .name = { "FAULT_TYPE" }, .t = HISYSEVENT_STRING, .v = { .s = faultType.data() }, .arraySize = 0 },
228             { .name = { "BUNDLE_NAME" }, .t = HISYSEVENT_STRING, .v = { .s = bundleName.data() }, .arraySize = 0 },
229             { .name = { "MODULE_NAME" }, .t = HISYSEVENT_STRING, .v = { .s = moduleName.data() }, .arraySize = 0 },
230             { .name = { "STORE_NAME" }, .t = HISYSEVENT_STRING, .v = { .s = storeName.data() }, .arraySize = 0 },
231             { .name = { "BUSINESS_TYPE" }, .t = HISYSEVENT_STRING, .v = { .s = businessType.data() }, .arraySize = 0 },
232             { .name = { "ERROR_CODE" }, .t = HISYSEVENT_INT32, .v = { .i32 = msg.errorType }, .arraySize = 0 },
233             { .name = { "APPENDIX" }, .t = HISYSEVENT_STRING, .v = { .s = appendix.data() }, .arraySize = 0 },
234         };
235         OH_HiSysEvent_Write(DATAMGR_DOMAIN, CoverEventID(dfxCode).c_str(), HISYSEVENT_FAULT, params,
236             sizeof(params) / sizeof(params[0]));
237     });
238     executors->Execute(std::move(task));
239 }
240 
ReportBehaviour(int dfxCode,const BehaviourMsg & msg,std::shared_ptr<ExecutorPool> executors)241 void HiViewAdapter::ReportBehaviour(int dfxCode, const BehaviourMsg &msg, std::shared_ptr<ExecutorPool> executors)
242 {
243     if (executors == nullptr) {
244         ZLOGW("executors is nullptr!");
245         return;
246     }
247     ExecutorPool::Task task([dfxCode, msg]() {
248         std::string message;
249         std::string storeId = Anonymous::Change(msg.storeId);
250         message.append("Behaviour type : ").append(std::to_string(static_cast<int>(msg.behaviourType)))
251             .append(" behaviour info : ").append(msg.extensionInfo);
252         struct HiSysEventParam params[] = {
253             { .name = { *USER_ID },
254                 .t = HISYSEVENT_STRING,
255                 .v = { .s = const_cast<char *>(msg.userId.c_str()) },
256                 .arraySize = 0 },
257             { .name = { *APP_ID },
258                 .t = HISYSEVENT_STRING,
259                 .v = { .s = const_cast<char *>(msg.appId.c_str()) },
260                 .arraySize = 0 },
261             { .name = { *STORE_ID },
262                 .t = HISYSEVENT_STRING,
263                 .v = { .s = const_cast<char *>(storeId.c_str()) },
264                 .arraySize = 0 },
265             { .name = { *BEHAVIOUR_INFO },
266                 .t = HISYSEVENT_STRING,
267                 .v = { .s = const_cast<char *>(message.c_str()) },
268                 .arraySize = 0 },
269         };
270         OH_HiSysEvent_Write(
271             DATAMGR_DOMAIN,
272             CoverEventID(dfxCode).c_str(),
273             HISYSEVENT_BEHAVIOR,
274             params,
275             sizeof(params) / sizeof(params[0])
276         );
277     });
278     executors->Execute(std::move(task));
279 }
280 
ReportDatabaseStatistic(int dfxCode,const DbStat & stat,std::shared_ptr<ExecutorPool> executors)281 void HiViewAdapter::ReportDatabaseStatistic(int dfxCode, const DbStat &stat, std::shared_ptr<ExecutorPool> executors)
282 {
283     if (executors == nullptr) {
284         ZLOGW("executors is nullptr!");
285         return;
286     }
287     ExecutorPool::Task task([dfxCode, stat]() {
288         std::lock_guard<std::mutex> lock(dbMutex_);
289         if (!dbStat_.count(stat.GetKey())) {
290             dbStat_.insert({stat.GetKey(), {stat, 0, dfxCode}});
291         }
292     });
293     executors->Execute(std::move(task));
294     StartTimerThread(executors);
295 }
296 
ReportDbSize(const StatisticWrap<DbStat> & stat)297 void HiViewAdapter::ReportDbSize(const StatisticWrap<DbStat> &stat)
298 {
299     uint64_t dbSize;
300     if (!stat.val.delegate->GetKvStoreDiskSize(stat.val.storeId, dbSize)) {
301         return;
302     }
303 
304     ValueHash vh;
305     std::string userId;
306     if (!vh.CalcValueHash(stat.val.userId, userId)) {
307         return;
308     }
309     std::string storeId = Anonymous::Change(stat.val.storeId);
310     struct HiSysEventParam params[] = {
311         { .name = { *USER_ID },
312             .t = HISYSEVENT_STRING,
313             .v = { .s = const_cast<char *>(userId.c_str()) },
314             .arraySize = 0 },
315         { .name = { *APP_ID },
316             .t = HISYSEVENT_STRING,
317             .v = { .s = const_cast<char *>(stat.val.appId.c_str()) },
318             .arraySize = 0 },
319         { .name = { *STORE_ID },
320             .t = HISYSEVENT_STRING,
321             .v = { .s = const_cast<char *>(storeId.c_str()) },
322             .arraySize = 0 },
323         { .name = { *DB_SIZE }, .t = HISYSEVENT_UINT64, .v = { .ui64 = dbSize }, .arraySize = 0 },
324     };
325     OH_HiSysEvent_Write(
326         DATAMGR_DOMAIN,
327         CoverEventID(stat.code).c_str(),
328         HISYSEVENT_STATISTIC,
329         params,
330         sizeof(params) / sizeof(params[0])
331     );
332 }
333 
InvokeDbSize()334 void HiViewAdapter::InvokeDbSize()
335 {
336     std::lock_guard<std::mutex> lock(dbMutex_);
337     for (auto const &kv : dbStat_) {
338         if (kv.second.val.delegate == nullptr) {
339             continue;
340         }
341         // device coordinate for single version database
342         if (!kv.second.val.storeId.empty()) {
343             ReportDbSize(kv.second);
344             continue;
345         }
346         // below codes for multiple version database
347         std::vector<StoreInfo> storeInfos;
348         kv.second.val.delegate->GetKvStoreKeys(storeInfos);
349         if (storeInfos.empty()) {
350             continue;
351         }
352         for (auto const &storeInfo : storeInfos) {
353             ReportDbSize({{storeInfo.userId, storeInfo.appId, storeInfo.storeId, 0,
354                 kv.second.val.delegate}, 0, kv.second.code});
355         }
356     }
357     dbStat_.clear();
358 }
359 
ReportTrafficStatistic(int dfxCode,const TrafficStat & stat,std::shared_ptr<ExecutorPool> executors)360 void HiViewAdapter::ReportTrafficStatistic(int dfxCode, const TrafficStat &stat,
361     std::shared_ptr<ExecutorPool> executors)
362 {
363     if (executors == nullptr) {
364         ZLOGW("executors is nullptr!");
365         return;
366     }
367     ExecutorPool::Task task([dfxCode, stat]() {
368         std::lock_guard<std::mutex> lock(trafficMutex_);
369         auto it = trafficStat_.find(stat.GetKey());
370         if (it != trafficStat_.end()) {
371             it->second.val.receivedSize += stat.receivedSize;
372             it->second.val.sendSize += stat.sendSize;
373         } else {
374             trafficStat_.insert({stat.GetKey(), {stat, 0, dfxCode}});
375         }
376     });
377     executors->Execute(std::move(task));
378     StartTimerThread(executors);
379 }
380 
InvokeTraffic()381 void HiViewAdapter::InvokeTraffic()
382 {
383     std::lock_guard<std::mutex> lock(trafficMutex_);
384     ValueHash vh;
385     for (auto const &kv : trafficStat_) {
386         std::string deviceId;
387         if (!vh.CalcValueHash(kv.second.val.deviceId, deviceId)) {
388             continue;
389         }
390         std::string devId  = Anonymous::Change(deviceId);
391         struct HiSysEventParam params[] = {
392             { .name = { *TAG }, .t = HISYSEVENT_STRING, .v = { .s = const_cast<char *>(POWERSTATS) }, .arraySize = 0 },
393             { .name = { *APP_ID },
394                 .t = HISYSEVENT_STRING,
395                 .v = { .s = const_cast<char *>(kv.second.val.appId.c_str()) },
396                 .arraySize = 0 },
397             { .name = { *DEVICE_ID },
398                 .t = HISYSEVENT_STRING,
399                 .v = { .s = const_cast<char *>(devId.c_str()) },
400                 .arraySize = 0 },
401             { .name = { *SEND_SIZE },
402                 .t = HISYSEVENT_INT64,
403                 .v = { .i64 = static_cast<int64_t>(kv.second.val.sendSize) },
404                 .arraySize = 0 },
405             { .name = { *RECEIVED_SIZE },
406                 .t = HISYSEVENT_INT64,
407                 .v = { .i64 = static_cast<int64_t>(kv.second.val.receivedSize) },
408                 .arraySize = 0 },
409         };
410         OH_HiSysEvent_Write(
411             DATAMGR_DOMAIN,
412             CoverEventID(kv.second.code).c_str(),
413             HISYSEVENT_STATISTIC,
414             params,
415             sizeof(params) / sizeof(params[0])
416         );
417     }
418     trafficStat_.clear();
419 }
420 
ReportVisitStatistic(int dfxCode,const VisitStat & stat,std::shared_ptr<ExecutorPool> executors)421 void HiViewAdapter::ReportVisitStatistic(int dfxCode, const VisitStat &stat, std::shared_ptr<ExecutorPool> executors)
422 {
423     if (executors == nullptr) {
424         ZLOGW("executors is nullptr!");
425         return;
426     }
427     ExecutorPool::Task task([dfxCode, stat]() {
428         std::lock_guard<std::mutex> lock(visitMutex_);
429         auto it = visitStat_.find(stat.GetKey());
430         if (it == visitStat_.end()) {
431             visitStat_.insert({stat.GetKey(), {stat, 1, dfxCode}});
432         } else {
433             it->second.times++;
434         }
435     });
436     executors->Execute(std::move(task));
437     StartTimerThread(executors);
438 }
439 
InvokeVisit()440 void HiViewAdapter::InvokeVisit()
441 {
442     std::lock_guard<std::mutex> lock(visitMutex_);
443     for (auto const &kv : visitStat_) {
444         struct HiSysEventParam params[] = {
445             { .name = { *TAG },
446                 .t = HISYSEVENT_STRING,
447                 .v = { .s = const_cast<char *>(POWERSTATS) },
448                 .arraySize = 0 },
449             { .name = { *APP_ID },
450                 .t = HISYSEVENT_STRING,
451                 .v = { .s = const_cast<char *>(kv.second.val.appId.c_str()) },
452                 .arraySize = 0 },
453             { .name = { *INTERFACE_NAME },
454                 .t = HISYSEVENT_STRING,
455                 .v = { .s = const_cast<char *>(kv.second.val.interfaceName.c_str()) },
456                 .arraySize = 0 },
457             { .name = { *BEHAVIOUR_INFO },
458                 .t = HISYSEVENT_INT64,
459                 .v = { .i64 = static_cast<int64_t>(kv.second.times) },
460                 .arraySize = 0 },
461         };
462         OH_HiSysEvent_Write(
463             DATAMGR_DOMAIN,
464             CoverEventID(kv.second.code).c_str(),
465             HISYSEVENT_STATISTIC,
466             params,
467             sizeof(params) / sizeof(params[0])
468         );
469     }
470     visitStat_.clear();
471 }
472 
ReportApiPerformanceStatistic(int dfxCode,const ApiPerformanceStat & stat,std::shared_ptr<ExecutorPool> executors)473 void HiViewAdapter::ReportApiPerformanceStatistic(int dfxCode, const ApiPerformanceStat &stat,
474     std::shared_ptr<ExecutorPool> executors)
475 {
476     if (executors == nullptr) {
477         ZLOGW("executors is nullptr!");
478         return;
479     }
480     ExecutorPool::Task task([dfxCode, stat]() {
481         std::lock_guard<std::mutex> lock(apiPerformanceMutex_);
482         auto it = apiPerformanceStat_.find(stat.GetKey());
483         if (it == apiPerformanceStat_.end()) {
484             apiPerformanceStat_.insert({stat.GetKey(), {stat, 1, dfxCode}}); // the init value of times is 1
485         } else {
486             it->second.times++;
487             it->second.val.costTime = stat.costTime;
488             if (it->second.times > 0) {
489                 it->second.val.averageTime =
490                     (it->second.val.averageTime * static_cast<uint64_t>(it->second.times - 1) + stat.costTime)
491                     / it->second.times;
492             }
493             if (stat.costTime > it->second.val.worstTime) {
494                 it->second.val.worstTime = stat.costTime;
495             }
496         }
497     });
498 
499     executors->Execute(std::move(task));
500     StartTimerThread(executors);
501 }
502 
ReportUdmfBehaviour(int dfxCode,const UdmfBehaviourMsg & msg,std::shared_ptr<ExecutorPool> executors)503 void HiViewAdapter::ReportUdmfBehaviour(
504     int dfxCode, const UdmfBehaviourMsg &msg, std::shared_ptr<ExecutorPool> executors)
505 {
506     if (executors == nullptr) {
507         ZLOGI("report udmf behavior failed");
508         return;
509     }
510     ExecutorPool::Task task([dfxCode, msg]() {
511         struct HiSysEventParam params[] = {
512             { .name = { *APP_ID },
513                 .t = HISYSEVENT_STRING,
514                 .v = { .s = const_cast<char *>(msg.appId.c_str()) },
515                 .arraySize = 0 },
516             { .name = { *CHANNEL },
517                 .t = HISYSEVENT_STRING,
518                 .v = { .s = const_cast<char *>(msg.channel.c_str()) },
519                 .arraySize = 0 },
520             { .name = { *DATA_SIZE },
521                 .t = HISYSEVENT_INT64,
522                 .v = { .i64 = msg.dataSize },
523                 .arraySize = 0 },
524             { .name = { *DATA_TYPE },
525                 .t = HISYSEVENT_STRING,
526                 .v = { .s = const_cast<char *>(msg.dataType.c_str()) },
527                 .arraySize = 0 },
528             { .name = { *OPERATION },
529                 .t = HISYSEVENT_STRING,
530                 .v = { .s = const_cast<char *>(msg.operation.c_str()) },
531                 .arraySize = 0 },
532             { .name = { *RESULT },
533                 .t = HISYSEVENT_STRING,
534                 .v = { .s = const_cast<char *>(msg.result.c_str()) },
535                 .arraySize = 0 },
536         };
537         OH_HiSysEvent_Write(
538             DATAMGR_DOMAIN,
539             CoverEventID(dfxCode).c_str(),
540             HISYSEVENT_BEHAVIOR,
541             params,
542             sizeof(params) / sizeof(params[0])
543         );
544     });
545     executors->Execute(std::move(task));
546 }
547 
InvokeApiPerformance()548 void HiViewAdapter::InvokeApiPerformance()
549 {
550     std::string message;
551     message.append("[");
552     std::lock_guard<std::mutex> lock(apiPerformanceMutex_);
553     for (auto const &kv : apiPerformanceStat_) {
554         message.append("{\"CODE\":\"").append(std::to_string(kv.second.code)).append("\",")
555             .append("\"").append(INTERFACE_NAME).append("\":\"").append(kv.second.val.interfaceName).append("\",")
556             .append("\"").append(TIMES).append("\":").append(std::to_string(kv.second.times)).append(",")
557             .append("\"").append(AVERAGE_TIMES).append("\":").append(std::to_string(kv.second.val.averageTime))
558             .append(",").append("\"").append(WORST_TIMES).append("\":").append(std::to_string(kv.second.val.worstTime))
559             .append("}");
560     }
561     message.append("]");
562     struct HiSysEventParam params[] = {
563         { .name = { *INTERFACES },
564             .t = HISYSEVENT_STRING,
565             .v = { .s = const_cast<char *>(message.c_str()) },
566             .arraySize = 0 },
567     };
568     OH_HiSysEvent_Write(
569         DATAMGR_DOMAIN,
570         CoverEventID(DfxCodeConstant::API_PERFORMANCE_STATISTIC).c_str(),
571         HISYSEVENT_STATISTIC,
572         params,
573         sizeof(params) / sizeof(params[0])
574     );
575     apiPerformanceStat_.clear();
576     ZLOGI("DdsTrace interface: clean");
577 }
578 
StartTimerThread(std::shared_ptr<ExecutorPool> executors)579 void HiViewAdapter::StartTimerThread(std::shared_ptr<ExecutorPool> executors)
580 {
581     if (running_) {
582         return;
583     }
584     std::lock_guard<std::mutex> lock(runMutex_);
585     if (running_) {
586         return;
587     }
588     running_ = true;
589     auto interval = std::chrono::seconds(WAIT_TIME);
590     auto fun = []() {
591         time_t current = time(nullptr);
592         tm localTime = { 0 };
593         tm *result = localtime_r(&current, &localTime);
594         if ((result != nullptr) && (localTime.tm_hour == DAILY_REPORT_TIME)) {
595             InvokeDbSize();
596             InvokeApiPerformance();
597         }
598         InvokeTraffic();
599         InvokeVisit();
600     };
601     executors->Schedule(fun, interval);
602 }
603 
CoverEventID(int dfxCode)604 std::string HiViewAdapter::CoverEventID(int dfxCode)
605 {
606     std::string sysEventID;
607     auto operatorIter = EVENT_COVERT_TABLE.find(dfxCode);
608     if (operatorIter != EVENT_COVERT_TABLE.end()) {
609         sysEventID = operatorIter->second;
610     }
611     return sysEventID;
612 }
613 } // namespace DistributedDataDfx
614 } // namespace OHOS