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