1 /*
2 * Copyright (c) 2025 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 "trace_db_callback.h"
16
17 #include "hiview_logger.h"
18 #include "sql_util.h"
19
20 namespace OHOS {
21 namespace HiviewDFX {
22 namespace {
23 DEFINE_LOG_TAG("TraceDbStoreCallback");
24
25 // Table trace_flow_control column name
26 const std::string FLOW_TABLE_NAME = "trace_flow_control";
27 const std::string TABLE_NAME = "trace_flow_control";
28 const std::string COLUMN_SYSTEM_TIME = "system_time";
29 const std::string COLUMN_CALLER_NAME = "caller_name";
30 const std::string COLUMN_USED_SIZE = "used_size";
31
32 // Table unified_collection_task column name
33 const std::string TABLE_NAME_TASK = "unified_collection_task";
34 const std::string COLUMN_ID = "id";
35 const std::string COLUMN_TASK_DATE = "task_date";
36 const std::string COLUMN_TASK_TYPE = "task_type";
37 const std::string COLUMN_UID = "uid";
38 const std::string COLUMN_PID = "pid";
39 const std::string COLUMN_BUNDLE_NAME = "bundle_name";
40 const std::string COLUMN_BUNDLE_VERSION = "bundle_version";
41 const std::string COLUMN_START_TIME = "start_time";
42 const std::string COLUMN_FINISH_TIME = "finish_time";
43 const std::string COLUMN_RESOURCE_PATH = "resource_path";
44 const std::string COLUMN_RESOURCE_SIZE = "resource_size";
45 const std::string COLUMN_COST_CPU = "cost_cpu";
46 const std::string COLUMN_STATE = "state";
47
48 // Table trace_behavior_db_helper column name
49 const std::string TABLE_NAME_BEHAVIOR = "trace_behavior_db_helper";
50 const std::string COLUMN_BEHAVIOR_ID = "behavior_id ";
51 const std::string COLUMN_DATE = "task_date";
52 const std::string COLUMN_USED_QUOTA = "used_quota";
53
54 // Table telemetry_flow_control column name
55 const std::string TABLE_TELEMETRY_FLOW_CONTROL = "telemetry_flow_control";
56 const std::string TABLE_TELEMETRY_TIME_CONTROL = "telemetry_time_control";
57 const std::string TABLE_TELEMETRY_CONTROL = "telemetry_control";
58 const std::string COLUMN_MODULE_NAME = "module";
59 const std::string COLUMN_QUOTA = "quota";
60 const std::string COLUMN_THRESHOLD = "threshold";
61 const std::string COLUMN_TELEMTRY_ID = "telemetry_id";
62 const std::string COLUMN_BEGIN_TIME = "begin_time";
63 }
64
OnCreate(NativeRdb::RdbStore & rdbStore)65 int TraceDbStoreCallback::OnCreate(NativeRdb::RdbStore& rdbStore)
66 {
67 HIVIEW_LOGI("create dbStore");
68 if (auto ret = CreateTraceFlowControlTable(rdbStore); ret != NativeRdb::E_OK) {
69 HIVIEW_LOGE("failed to create table trace_flow_control");
70 return ret;
71 }
72 if (auto ret = CreateAppTaskTable(rdbStore); ret != NativeRdb::E_OK) {
73 HIVIEW_LOGE("failed to create table unified_collection_task");
74 return ret;
75 }
76 if (auto ret = CreateTraceBehaviorDbHelperTable(rdbStore); ret != NativeRdb::E_OK) {
77 HIVIEW_LOGE("failed to create table trace_behavior_db_helper");
78 return ret;
79 }
80 if (auto ret = CreateTelemetryControlTable(rdbStore); ret != NativeRdb::E_OK) {
81 HIVIEW_LOGE("failed to create table telemetry_flow_control");
82 return ret;
83 }
84 return NativeRdb::E_OK;
85 }
86
OnUpgrade(NativeRdb::RdbStore & rdbStore,int oldVersion,int newVersion)87 int TraceDbStoreCallback::OnUpgrade(NativeRdb::RdbStore& rdbStore, int oldVersion, int newVersion)
88 {
89 HIVIEW_LOGI("oldVersion=%{public}d, newVersion=%{public}d", oldVersion, newVersion);
90 std::string flowDropSql = SqlUtil::GenerateDropSql(FLOW_TABLE_NAME);
91 if (int ret = rdbStore.ExecuteSql(flowDropSql); ret != NativeRdb::E_OK) {
92 HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", FLOW_TABLE_NAME.c_str(), ret);
93 return -1;
94 }
95 std::string taskSql = SqlUtil::GenerateDropSql(TABLE_NAME_TASK);
96 if (int ret = rdbStore.ExecuteSql(taskSql); ret != NativeRdb::E_OK) {
97 HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", TABLE_NAME_TASK.c_str(), ret);
98 return -1;
99 }
100 std::string behaviorSql = SqlUtil::GenerateDropSql(TABLE_NAME_BEHAVIOR);
101 if (int ret = rdbStore.ExecuteSql(behaviorSql); ret != NativeRdb::E_OK) {
102 HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", TABLE_NAME_BEHAVIOR.c_str(), ret);
103 return -1;
104 }
105 std::string timeSql = SqlUtil::GenerateDropSql(TABLE_TELEMETRY_TIME_CONTROL);
106 if (int ret = rdbStore.ExecuteSql(timeSql); ret != NativeRdb::E_OK) {
107 HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", TABLE_TELEMETRY_TIME_CONTROL.c_str(), ret);
108 return -1;
109 }
110 std::string flowSql = SqlUtil::GenerateDropSql(TABLE_TELEMETRY_FLOW_CONTROL);
111 if (int ret = rdbStore.ExecuteSql(flowSql); ret != NativeRdb::E_OK) {
112 HIVIEW_LOGE("failed to drop table %{public}s, ret=%{public}d", TABLE_TELEMETRY_FLOW_CONTROL.c_str(), ret);
113 return -1;
114 }
115 return OnCreate(rdbStore);
116 }
117
CreateTraceFlowControlTable(NativeRdb::RdbStore & rdbStore)118 int32_t TraceDbStoreCallback::CreateTraceFlowControlTable(NativeRdb::RdbStore& rdbStore)
119 {
120 /**
121 * table: trace_flow_control
122 *
123 * describe: store data that has been used
124 * |-----|-------------|-------------|-----------|
125 * | id | system_time | caller_name | used_size |
126 * |-----|-------------|-------------|-----------|
127 * | INT | VARCHAR | VARCHAR | INT64 |
128 * |-----|-------------|-------------|-----------|
129 */
130 const std::vector<std::pair<std::string, std::string>> fields = {
131 {COLUMN_SYSTEM_TIME, SqlUtil::COLUMN_TYPE_STR},
132 {COLUMN_CALLER_NAME, SqlUtil::COLUMN_TYPE_STR},
133 {COLUMN_USED_SIZE, SqlUtil::COLUMN_TYPE_INT},
134 };
135 HIVIEW_LOGI("create table trace_flow_control table");
136 std::string sql = SqlUtil::GenerateCreateSql(TABLE_NAME, fields);
137 if (rdbStore.ExecuteSql(sql) != NativeRdb::E_OK) {
138 HIVIEW_LOGE("failed to create table, sql=%{public}s", sql.c_str());
139 return -1;
140 }
141 return 0;
142 }
143
CreateAppTaskTable(NativeRdb::RdbStore & rdbStore)144 int32_t TraceDbStoreCallback::CreateAppTaskTable(NativeRdb::RdbStore& rdbStore)
145 {
146 /**
147 * table: unified_collection_task
148 *
149 * describe: store data that app task
150 * |-----|-----------|-----------|-------|-------|-------------|----------------|------------|-------------|
151 * | id | task_date | task_type | uid | pid | bundle_name | bundle_version | start_time | finish_time |
152 * |-----|-----------|-----------|-------|-------|-------------|----------------|------------|-------------|
153 * | INT | INT64 | INT8 | INT32 | INT32 | TEXT | TEXT | INT64 | INT64 |
154 * |-----|-----------|-----------|-------|-------|-------------|----------------|------------|-------------|
155 *
156 * |---------------|---------------|----------|-------|
157 * | resource_path | resource_size | cost_cpu | state |
158 * |---------------|---------------|----------|-------|
159 * | TEXT | INT32 | REAL | INT32 |
160 * |---------------|---------------|----------|-------|
161 */
162 const std::vector<std::pair<std::string, std::string>> fields = {
163 {COLUMN_TASK_DATE, SqlUtil::COLUMN_TYPE_INT},
164 {COLUMN_TASK_TYPE, SqlUtil::COLUMN_TYPE_INT},
165 {COLUMN_UID, SqlUtil::COLUMN_TYPE_INT},
166 {COLUMN_PID, SqlUtil::COLUMN_TYPE_INT},
167 {COLUMN_BUNDLE_NAME, SqlUtil::COLUMN_TYPE_STR},
168 {COLUMN_BUNDLE_VERSION, SqlUtil::COLUMN_TYPE_STR},
169 {COLUMN_START_TIME, SqlUtil::COLUMN_TYPE_INT},
170 {COLUMN_FINISH_TIME, SqlUtil::COLUMN_TYPE_INT},
171 {COLUMN_RESOURCE_PATH, SqlUtil::COLUMN_TYPE_STR},
172 {COLUMN_RESOURCE_SIZE, SqlUtil::COLUMN_TYPE_INT},
173 {COLUMN_COST_CPU, SqlUtil::COLUMN_TYPE_DOU},
174 {COLUMN_STATE, SqlUtil::COLUMN_TYPE_INT},
175 };
176 HIVIEW_LOGI("create table app task=%{public}s", TABLE_NAME_TASK.c_str());
177 std::string sql = SqlUtil::GenerateCreateSql(TABLE_NAME_TASK, fields);
178 if (rdbStore.ExecuteSql(sql) != NativeRdb::E_OK) {
179 HIVIEW_LOGE("failed to create app task table, sql=%{public}s", sql.c_str());
180 return -1;
181 }
182 return 0;
183 }
184
CreateTraceBehaviorDbHelperTable(NativeRdb::RdbStore & rdbStore)185 int32_t TraceDbStoreCallback::CreateTraceBehaviorDbHelperTable(NativeRdb::RdbStore& rdbStore)
186 {
187 /**
188 * table: trace_behavior_db_helper
189 *
190 * describe: store trace behavior quota
191 * |-----|-------------|-----------|------------|
192 * | id | behavior_id | task_date | used_quota |
193 * |-----|-------------|-----------|------------|
194 * | INT | INT32 | TEXT | INT32 |
195 * |-----|-------------|-----------|------------|
196 */
197 const std::vector<std::pair<std::string, std::string>> fields = {
198 {COLUMN_BEHAVIOR_ID, SqlUtil::COLUMN_TYPE_INT},
199 {COLUMN_DATE, SqlUtil::COLUMN_TYPE_INT},
200 {COLUMN_USED_QUOTA, SqlUtil::COLUMN_TYPE_INT},
201 };
202 HIVIEW_LOGI("create table trace_behavior_db_helper table");
203 std::string sql = SqlUtil::GenerateCreateSql(TABLE_NAME_BEHAVIOR, fields);
204 if (rdbStore.ExecuteSql(sql) != NativeRdb::E_OK) {
205 HIVIEW_LOGE("failed to create table, sql=%{public}s", sql.c_str());
206 return -1;
207 }
208 return 0;
209 }
210
CreateTelemetryControlTable(NativeRdb::RdbStore & rdbStore)211 int32_t TraceDbStoreCallback::CreateTelemetryControlTable(NativeRdb::RdbStore &rdbStore)
212 {
213 /**
214 * table: telemetry_flow_control
215 *
216 * describe: store trace behavior quota
217 * |--------------|------- -|-----------|------------|------------|--------------|
218 * | telemetry_id | module | used_size | quota | threshold | begin_time |
219 * |--------------|-- ------|-----------|------------|------------|--------------|
220 * | VARCHAR | VARCHAR | INT32 | INT32 | INT32 | INT64 |
221 * |--------------|----- ---|-----------|------------|------------|--------------|
222 */
223 const std::vector<std::pair<std::string, std::string>> fields = {
224 {COLUMN_TELEMTRY_ID, SqlUtil::COLUMN_TYPE_STR},
225 {COLUMN_MODULE_NAME, SqlUtil::COLUMN_TYPE_STR},
226 {COLUMN_USED_SIZE, SqlUtil::COLUMN_TYPE_INT},
227 {COLUMN_QUOTA, SqlUtil::COLUMN_TYPE_INT},
228 {COLUMN_THRESHOLD, SqlUtil::COLUMN_TYPE_INT},
229 {COLUMN_BEGIN_TIME, SqlUtil::COLUMN_TYPE_INT},
230 };
231 std::string sql = SqlUtil::GenerateCreateSql(TABLE_TELEMETRY_CONTROL, fields);
232 if (rdbStore.ExecuteSql(sql) != NativeRdb::E_OK) {
233 HIVIEW_LOGE("failed to create table, sql=%{public}s", sql.c_str());
234 return -1;
235 }
236 return 0;
237 }
238 }
239 }