• 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_LONG_PARAM = "--database";
40 constexpr const char *DUMP_SHORT_PARAM = "-d";
41 }
42 
43 const std::string DBDfxAdapter::EVENT_CODE = "ERROR_CODE";
44 const std::string DBDfxAdapter::APP_ID = "APP_ID";
45 const std::string DBDfxAdapter::USER_ID = "USER_ID";
46 const std::string DBDfxAdapter::STORE_ID = "STORE_ID";
47 const std::string DBDfxAdapter::SQLITE_EXECUTE = "SQLITE_EXECUTE";
48 const std::string DBDfxAdapter::SYNC_ACTION = "SYNC_ACTION";
49 const std::string DBDfxAdapter::EVENT_OPEN_DATABASE_FAILED = "OPEN_DATABASE_FAILED";
50 
Dump(int fd,const std::vector<std::u16string> & args)51 void DBDfxAdapter::Dump(int fd, const std::vector<std::u16string> &args)
52 {
53     if (!args.empty()) {
54         const std::u16string longParam =
55             std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(DUMP_LONG_PARAM);
56         const std::u16string shortParam =
57             std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(DUMP_SHORT_PARAM);
58         auto find = std::any_of(args.begin(), args.end(), [&longParam, &shortParam](const std::u16string &arg) {
59             return arg == longParam || arg == shortParam;
60         });
61         if (!find) {
62             return;
63         }
64     }
65     DBDumpHelper::Dump(fd, "DistributedDB Dump Message Info:\n\n");
66     DBDumpHelper::Dump(fd, "DistributedDB Database Basic Message Info:\n");
67     KvDBManager::GetInstance()->Dump(fd);
68     RelationalStoreInstance::GetInstance()->Dump(fd);
69     DBDumpHelper::Dump(fd, "DistributedDB Common Message Info:\n");
70     RuntimeContext::GetInstance()->DumpCommonInfo(fd);
71     DBDumpHelper::Dump(fd, "\tlast error msg = %s\n", SQLiteUtils::GetLastErrorMsg().c_str());
72 }
73 
74 #ifdef USE_DFX_ABILITY
ReportFault(const ReportTask & reportTask)75 void DBDfxAdapter::ReportFault(const ReportTask &reportTask)
76 {
77     RuntimeContext::GetInstance()->ScheduleTask([=]() {
78         // call hievent here
79         HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR,
80             reportTask.eventName,
81             OHOS::HiviewDFX::HiSysEvent::EventType::FAULT,
82             APP_ID, reportTask.appId,
83             STORE_ID, reportTask.storeId,
84             EVENT_CODE, std::to_string(reportTask.errCode));
85     });
86 }
87 
StartTrace(const std::string & action)88 void DBDfxAdapter::StartTrace(const std::string &action)
89 {
90     ::StartTrace(HITRACE_LABEL, action);
91 }
92 
FinishTrace()93 void DBDfxAdapter::FinishTrace()
94 {
95     ::FinishTrace(HITRACE_LABEL);
96 }
97 
StartTraceSQL()98 void DBDfxAdapter::StartTraceSQL()
99 {
100 #ifdef TRACE_SQLITE_EXECUTE
101     ::StartTrace(HITRACE_LABEL, SQLITE_EXECUTE);
102 #endif
103 }
104 
FinishTraceSQL()105 void DBDfxAdapter::FinishTraceSQL()
106 {
107 #ifdef TRACE_SQLITE_EXECUTE
108     ::FinishTrace(HITRACE_LABEL);
109 #endif
110 }
111 
StartAsyncTrace(const std::string & action,int32_t taskId)112 void DBDfxAdapter::StartAsyncTrace(const std::string &action, int32_t taskId)
113 {
114     // call hitrace here
115     // need include bytrace.h
116     ::StartAsyncTrace(HITRACE_LABEL, action, taskId);
117 }
118 
FinishAsyncTrace(const std::string & action,int32_t taskId)119 void DBDfxAdapter::FinishAsyncTrace(const std::string &action, int32_t taskId)
120 {
121     // call hitrace here
122     ::FinishAsyncTrace(HITRACE_LABEL, action, taskId);
123 }
124 
125 #else
ReportFault(const ReportTask & reportTask)126 void DBDfxAdapter::ReportFault(const ReportTask &reportTask)
127 {
128     (void) reportTask;
129 }
130 
StartTrace(const std::string & action)131 void DBDfxAdapter::StartTrace(const std::string &action)
132 {
133     (void) action;
134 }
135 
FinishTrace()136 void DBDfxAdapter::FinishTrace()
137 {
138 }
139 
StartAsyncTrace(const std::string & action,int32_t taskId)140 void DBDfxAdapter::StartAsyncTrace(const std::string &action, int32_t taskId)
141 {
142     (void) action;
143     (void) taskId;
144 }
145 
FinishAsyncTrace(const std::string & action,int32_t taskId)146 void DBDfxAdapter::FinishAsyncTrace(const std::string &action, int32_t taskId)
147 {
148     (void) action;
149     (void) taskId;
150 }
151 
StartTraceSQL()152 void DBDfxAdapter::StartTraceSQL()
153 {
154 }
155 
FinishTraceSQL()156 void DBDfxAdapter::FinishTraceSQL()
157 {
158 }
159 #endif
160 } // namespace DistributedDB