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::ORG_PKG = "ORG_PKG";
44 const std::string DBDfxAdapter::FUNC = "FUNC";
45 const std::string DBDfxAdapter::BIZ_SCENE = "BIZ_SCENE";
46 const std::string DBDfxAdapter::BIZ_STATE = "BIZ_STATE";
47 const std::string DBDfxAdapter::BIZ_STAGE = "BIZ_STAGE";
48 const std::string DBDfxAdapter::STAGE_RES = "STAGE_RES";
49 const std::string DBDfxAdapter::ERROR_CODE = "ERROR_CODE";
50 const std::string DBDfxAdapter::ORG_PKG_NAME = "distributeddata";
51 const std::string DBDfxAdapter::DISTRIBUTED_DB_BEHAVIOR = "DISTRIBUTED_DB_BEHAVIOR";
52 const std::string DBDfxAdapter::SQLITE_EXECUTE = "SQLITE_EXECUTE";
53 const std::string DBDfxAdapter::SYNC_ACTION = "SYNC_ACTION";
54 const std::string DBDfxAdapter::EVENT_OPEN_DATABASE_FAILED = "OPEN_DATABASE_FAILED";
55
Dump(int fd,const std::vector<std::u16string> & args)56 void DBDfxAdapter::Dump(int fd, const std::vector<std::u16string> &args)
57 {
58 if (!args.empty()) {
59 const std::u16string longParam =
60 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(DUMP_LONG_PARAM);
61 const std::u16string shortParam =
62 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> {}.from_bytes(DUMP_SHORT_PARAM);
63 auto find = std::any_of(args.begin(), args.end(), [&longParam, &shortParam](const std::u16string &arg) {
64 return arg == longParam || arg == shortParam;
65 });
66 if (!find) {
67 return;
68 }
69 }
70 DBDumpHelper::Dump(fd, "DistributedDB Dump Message Info:\n\n");
71 DBDumpHelper::Dump(fd, "DistributedDB Database Basic Message Info:\n");
72 auto kvDBManager = KvDBManager::GetInstance();
73 if (kvDBManager == nullptr) {
74 return;
75 }
76 kvDBManager->Dump(fd);
77 RelationalStoreInstance::GetInstance()->Dump(fd);
78 DBDumpHelper::Dump(fd, "DistributedDB Common Message Info:\n");
79 RuntimeContext::GetInstance()->DumpCommonInfo(fd);
80 DBDumpHelper::Dump(fd, "\tlast error msg = %s\n", SQLiteUtils::GetLastErrorMsg().c_str());
81 }
82
83 #ifdef USE_DFX_ABILITY
ReportBehavior(const ReportTask & reportTask)84 void DBDfxAdapter::ReportBehavior(const ReportTask &reportTask)
85 {
86 RuntimeContext::GetInstance()->ScheduleTask([=]() {
87 int dbDfxErrCode = -(reportTask.errCode - E_BASE) + E_DB_DFX_BASE;
88 struct HiSysEventParam params[] = {
89 {.name = {"ORG_PKG"},
90 .t = HISYSEVENT_STRING,
91 .v = {.s = const_cast<char *>(ORG_PKG_NAME.c_str())},
92 .arraySize = 0},
93 {.name = {"FUNC"},
94 .t = HISYSEVENT_STRING,
95 .v = {.s = const_cast<char *>(reportTask.funcName.c_str())},
96 .arraySize = 0},
97 {.name = {"BIZ_SCENE"},
98 .t = HISYSEVENT_INT32,
99 .v = {.i32 = static_cast<int32_t>(reportTask.scene)},
100 .arraySize = 0},
101 {.name = {"BIZ_STATE"},
102 .t = HISYSEVENT_INT32,
103 .v = {.i32 = static_cast<int32_t>(reportTask.state)},
104 .arraySize = 0},
105 {.name = {"BIZ_STAGE"},
106 .t = HISYSEVENT_INT32,
107 .v = {.i32 = static_cast<int32_t>(reportTask.stage)},
108 .arraySize = 0},
109 {.name = {"STAGE_RES"},
110 .t = HISYSEVENT_INT32,
111 .v = {.i32 = static_cast<int32_t>(reportTask.result)},
112 .arraySize = 0},
113 {.name = {"ERROR_CODE"},
114 .t = HISYSEVENT_INT32,
115 .v = {.i32 = static_cast<int32_t>(dbDfxErrCode)},
116 .arraySize = 0},
117 };
118 // call hievent here
119 OH_HiSysEvent_Write(OHOS::HiviewDFX::HiSysEvent::Domain::DISTRIBUTED_DATAMGR,
120 DISTRIBUTED_DB_BEHAVIOR.c_str(),
121 HISYSEVENT_BEHAVIOR,
122 params,
123 sizeof(params) / sizeof(params[0]));
124 });
125 }
126
StartTrace(const std::string & action)127 void DBDfxAdapter::StartTrace(const std::string &action)
128 {
129 ::StartTrace(HITRACE_LABEL, action);
130 }
131
FinishTrace()132 void DBDfxAdapter::FinishTrace()
133 {
134 ::FinishTrace(HITRACE_LABEL);
135 }
136
StartTracing()137 void DBDfxAdapter::StartTracing()
138 {
139 #ifdef TRACE_SQLITE_EXECUTE
140 ::StartTrace(HITRACE_LABEL, SQLITE_EXECUTE);
141 #endif
142 }
143
FinishTracing()144 void DBDfxAdapter::FinishTracing()
145 {
146 #ifdef TRACE_SQLITE_EXECUTE
147 ::FinishTrace(HITRACE_LABEL);
148 #endif
149 }
150
StartAsyncTrace(const std::string & action,int32_t taskId)151 void DBDfxAdapter::StartAsyncTrace(const std::string &action, int32_t taskId)
152 {
153 // call hitrace here
154 // need include bytrace.h
155 ::StartAsyncTrace(HITRACE_LABEL, action, taskId);
156 }
157
FinishAsyncTrace(const std::string & action,int32_t taskId)158 void DBDfxAdapter::FinishAsyncTrace(const std::string &action, int32_t taskId)
159 {
160 // call hitrace here
161 ::FinishAsyncTrace(HITRACE_LABEL, action, taskId);
162 }
163
164 #else
ReportBehavior(const ReportTask & reportTask)165 void DBDfxAdapter::ReportBehavior(const ReportTask &reportTask)
166 {
167 (void) reportTask;
168 }
169
StartTrace(const std::string & action)170 void DBDfxAdapter::StartTrace(const std::string &action)
171 {
172 (void) action;
173 }
174
FinishTrace()175 void DBDfxAdapter::FinishTrace()
176 {
177 }
178
StartAsyncTrace(const std::string & action,int32_t taskId)179 void DBDfxAdapter::StartAsyncTrace(const std::string &action, int32_t taskId)
180 {
181 (void) action;
182 (void) taskId;
183 }
184
FinishAsyncTrace(const std::string & action,int32_t taskId)185 void DBDfxAdapter::FinishAsyncTrace(const std::string &action, int32_t taskId)
186 {
187 (void) action;
188 (void) taskId;
189 }
190
StartTracing()191 void DBDfxAdapter::StartTracing()
192 {
193 }
194
FinishTracing()195 void DBDfxAdapter::FinishTracing()
196 {
197 }
198 #endif
199 } // namespace DistributedDB