• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include "db_dfx_adapter.h"
16 
17 #include <codecvt>
18 #include <cstdio>
19 #include <locale>
20 #include <string>
21 
22 #include "log_print.h"
23 #include "db_dump_helper.h"
24 #include "db_errno.h"
25 #include "kvdb_manager.h"
26 #include "relational_store_instance.h"
27 #include "runtime_context.h"
28 #include "sqlite_utils.h"
29 #ifdef USE_DFX_ABILITY
30 #include "hitrace_meter.h"
31 #include "hisysevent.h"
32 #endif
33 
34 namespace DistributedDB {
35 namespace {
36 #ifdef USE_DFX_ABILITY
37 constexpr uint64_t HITRACE_LABEL = HITRACE_TAG_DISTRIBUTEDDATA;
38 #endif
39 constexpr const char *DUMP_PARAM = "dump-distributeddb";
40 }
41 
42 const std::string DBDfxAdapter::EVENT_CODE = "ERROR_CODE";
43 const std::string DBDfxAdapter::APP_ID = "APP_ID";
44 const std::string DBDfxAdapter::USER_ID = "USER_ID";
45 const std::string DBDfxAdapter::STORE_ID = "STORE_ID";
46 const std::string DBDfxAdapter::SQLITE_EXECUTE = "SQLITE_EXECUTE";
47 const std::string DBDfxAdapter::SYNC_ACTION = "SYNC_ACTION";
48 const std::string DBDfxAdapter::EVENT_OPEN_DATABASE_FAILED = "OPEN_DATABASE_FAILED";
49 
Dump(int fd,const std::vector<std::u16string> & args)50 void DBDfxAdapter::Dump(int fd, const std::vector<std::u16string> &args)
51 {
52     if (!args.empty()) {
53         const std::u16string u16DumpParam =
54             std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(DUMP_PARAM);
55         auto find = std::any_of(args.begin(), args.end(), [&u16DumpParam](const std::u16string &arg) {
56             return arg == u16DumpParam;
57         });
58         if (!find) {
59             return;
60         }
61     }
62     DBDumpHelper::Dump(fd, "DistributedDB Dump Message Info:\n\n");
63     DBDumpHelper::Dump(fd, "DistributedDB Database Basic Message Info:\n");
64     KvDBManager::GetInstance()->Dump(fd);
65     RelationalStoreInstance::GetInstance()->Dump(fd);
66     DBDumpHelper::Dump(fd, "DistributedDB Common Message Info:\n");
67     RuntimeContext::GetInstance()->DumpCommonInfo(fd);
68     DBDumpHelper::Dump(fd, "\tlast error msg = %s\n", SQLiteUtils::GetLastErrorMsg().c_str());
69 }
70 
71 #ifdef USE_DFX_ABILITY
ReportFault(const ReportTask & reportTask)72 void DBDfxAdapter::ReportFault(const ReportTask &reportTask)
73 {
74     RuntimeContext::GetInstance()->ScheduleTask([=]() {
75         // call hievent here
76         OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR,
77             reportTask.eventName,
78             OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
79             APP_ID, reportTask.appId,
80             STORE_ID, reportTask.storeId,
81             EVENT_CODE, std::to_string(reportTask.errCode));
82     });
83 }
84 
StartTrace(const std::string & action)85 void DBDfxAdapter::StartTrace(const std::string &action)
86 {
87     ::StartTrace(HITRACE_LABEL, action);
88 }
89 
FinishTrace()90 void DBDfxAdapter::FinishTrace()
91 {
92     ::FinishTrace(HITRACE_LABEL);
93 }
94 
StartTraceSQL()95 void DBDfxAdapter::StartTraceSQL()
96 {
97 #ifdef TRACE_SQLITE_EXECUTE
98     ::StartTrace(HITRACE_LABEL, SQLITE_EXECUTE);
99 #endif
100 }
101 
FinishTraceSQL()102 void DBDfxAdapter::FinishTraceSQL()
103 {
104 #ifdef TRACE_SQLITE_EXECUTE
105     ::FinishTrace(HITRACE_LABEL);
106 #endif
107 }
108 
StartAsyncTrace(const std::string & action,int32_t taskId)109 void DBDfxAdapter::StartAsyncTrace(const std::string &action, int32_t taskId)
110 {
111     // call hitrace here
112     // need include bytrace.h
113     ::StartAsyncTrace(HITRACE_LABEL, action, taskId);
114 }
115 
FinishAsyncTrace(const std::string & action,int32_t taskId)116 void DBDfxAdapter::FinishAsyncTrace(const std::string &action, int32_t taskId)
117 {
118     // call hitrace here
119     ::FinishAsyncTrace(HITRACE_LABEL, action, taskId);
120 }
121 
122 #else
ReportFault(const ReportTask & reportTask)123 void DBDfxAdapter::ReportFault(const ReportTask &reportTask)
124 {
125     (void) reportTask;
126 }
127 
StartTrace(const std::string & action)128 void DBDfxAdapter::StartTrace(const std::string &action)
129 {
130     (void) action;
131 }
132 
FinishTrace()133 void DBDfxAdapter::FinishTrace()
134 {
135 }
136 
StartAsyncTrace(const std::string & action,int32_t taskId)137 void DBDfxAdapter::StartAsyncTrace(const std::string &action, int32_t taskId)
138 {
139     (void) action;
140     (void) taskId;
141 }
142 
FinishAsyncTrace(const std::string & action,int32_t taskId)143 void DBDfxAdapter::FinishAsyncTrace(const std::string &action, int32_t taskId)
144 {
145     (void) action;
146     (void) taskId;
147 }
148 
StartTraceSQL()149 void DBDfxAdapter::StartTraceSQL()
150 {
151 }
152 
FinishTraceSQL()153 void DBDfxAdapter::FinishTraceSQL()
154 {
155 }
156 #endif
157 } // namespace DistributedDB