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