1 /*
2 * Copyright (c) 2021-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
16 #include "sys_event_db_cleaner.h"
17
18 #include <cinttypes>
19 #include <map>
20
21 #include "logger.h"
22 #include "time_util.h"
23
24 namespace OHOS {
25 namespace HiviewDFX {
26 DEFINE_LOG_TAG("HiView-SysEventDbCleaner");
27 using EventStore::EventCol;
28 using EventStore::SysEventDao;
29 using EventStore::StoreType;
30 namespace {
31 const std::map<StoreType, std::pair<int, int>> CLEAN_CONFIG_MAP = {
32 { StoreType::BEHAVIOR, { 300000, 7 * 24 } }, // behavior event, maxNum: 300k, maxTime: 7 days
33 { StoreType::FAULT, { 20000, 30 * 24 } }, // fault event, maxNum: 20k, maxTime: 30 days
34 { StoreType::STATISTIC, { 200000, 30 * 24 } }, // statistic event, maxNum: 200k, maxTime: 30 days
35 { StoreType::SECURITY, { 30000, 90 * 24} }, // security event, maxNum: 30k, maxTime: 90 days
36 };
37 const int64_t MILLISECS_PER_HOUR = TimeUtil::SECONDS_PER_HOUR * TimeUtil::SEC_TO_MILLISEC;
38 const int64_t DELETE_LIMIT = 10000;
39 }
40
TryToClean()41 void SysEventDbCleaner::TryToClean()
42 {
43 for (auto cleanConfig : CLEAN_CONFIG_MAP) {
44 TryToCleanDb(cleanConfig.first, cleanConfig.second);
45 }
46 }
47
TryToCleanDb(StoreType type,const std::pair<int,int> & config)48 void SysEventDbCleaner::TryToCleanDb(StoreType type, const std::pair<int, int>& config)
49 {
50 int curNum = SysEventDao::GetNum(type);
51 HIVIEW_LOGI("try to clean db: type=%{public}d, curNum=%{public}d, maxNum=%{public}d, maxTime=%{public}d",
52 type, curNum, config.first, config.second);
53
54 // delete events if the num of events exceeds 10%
55 const double delPct = 0.1;
56 if (curNum <= (config.first + config.first * delPct)) {
57 return;
58 }
59
60 int64_t delEndTime = static_cast<int64_t>(TimeUtil::GetMilliseconds());
61 int saveHour = config.second;
62 int64_t delTime = delEndTime > saveHour * MILLISECS_PER_HOUR ?
63 (delEndTime - saveHour * MILLISECS_PER_HOUR) : delEndTime;
64 bool isDeleteLess = false;
65 while (curNum > config.first && delTime <= delEndTime) {
66 auto delNum = CleanDbByTime(type, delTime);
67 if (delNum < 0) { // db is corrupt
68 HIVIEW_LOGW("failed to clean db, err=%{public}d", delNum);
69 DeleteDb(type);
70 return;
71 }
72 curNum = SysEventDao::GetNum(type);
73 if (curNum < 0) {
74 HIVIEW_LOGW("failed to get num from db, err=%{public}d", curNum);
75 DeleteDb(type);
76 return;
77 }
78 if (delNum < DELETE_LIMIT) { // change to the next time interval
79 saveHour -= isDeleteLess ? 1 : (config.second * delPct); // change from 10% to 1h
80 const int delBdyHour = 24;
81 if (saveHour < delBdyHour && !isDeleteLess) {
82 saveHour = delBdyHour;
83 isDeleteLess = true;
84 }
85 delTime = delEndTime > saveHour * MILLISECS_PER_HOUR ?
86 (delEndTime - saveHour * MILLISECS_PER_HOUR) : delEndTime;
87 }
88 HIVIEW_LOGD("cleaning db: curNum=%{public}d, delTime=%{public}" PRId64 ", delEndTime=%{public}" PRId64,
89 curNum, delTime, delEndTime);
90 }
91 HIVIEW_LOGI("end to clean db, curNum=%{public}d", curNum);
92 }
93
CleanDbByTime(StoreType type,int64_t time)94 int SysEventDbCleaner::CleanDbByTime(StoreType type, int64_t time)
95 {
96 auto sysEventQuery = SysEventDao::BuildQuery(type);
97 sysEventQuery->Where(EventCol::TS, EventStore::Op::LE, time);
98 return SysEventDao::Delete(sysEventQuery, DELETE_LIMIT);
99 }
100
DeleteDb(StoreType type)101 void SysEventDbCleaner::DeleteDb(StoreType type)
102 {
103 if (SysEventDao::DeleteDB(type) != 0) {
104 HIVIEW_LOGE("failed to delete db file, type=%{public}d", type);
105 }
106 }
107 } // namespace HiviewDFX
108 } // namespace OHOS