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 bool isDeleteLess = false;
64 while (curNum > config.first && delTime <= delEndTime) {
65 auto delNum = CleanDbByTime(type, delTime);
66 if (delNum < 0) { // db is corrupt
67 HIVIEW_LOGW("failed to clean db, err=%{public}d", delNum);
68 DeleteDb(type);
69 return;
70 }
71 curNum = SysEventDao::GetNum(type);
72 if (curNum < 0) {
73 HIVIEW_LOGW("failed to get num from db, err=%{public}d", curNum);
74 DeleteDb(type);
75 return;
76 }
77 if (delNum < DELETE_LIMIT) { // change to the next time interval
78 saveHour -= isDeleteLess ? 1 : (config.second * delPct); // change from 10% to 1h
79 const int delBdyHour = 24;
80 if (saveHour < delBdyHour && !isDeleteLess) {
81 saveHour = delBdyHour;
82 isDeleteLess = true;
83 }
84 delTime = delEndTime - saveHour * MILLISECS_PER_HOUR;
85 }
86 HIVIEW_LOGD("cleaning db: curNum=%{public}d, delTime=%{public}" PRId64 ", delEndTime=%{public}" PRId64,
87 curNum, delTime, delEndTime);
88 }
89 HIVIEW_LOGI("end to clean db, curNum=%{public}d", curNum);
90 }
91
CleanDbByTime(StoreType type,int64_t time)92 int SysEventDbCleaner::CleanDbByTime(StoreType type, int64_t time)
93 {
94 auto sysEventQuery = SysEventDao::BuildQuery(type);
95 sysEventQuery->Where(EventCol::TS, EventStore::Op::LE, time);
96 return SysEventDao::Delete(sysEventQuery, DELETE_LIMIT);
97 }
98
DeleteDb(StoreType type)99 void SysEventDbCleaner::DeleteDb(StoreType type)
100 {
101 if (SysEventDao::DeleteDB(type) != 0) {
102 HIVIEW_LOGE("failed to delete db file, type=%{public}d", type);
103 }
104 }
105 } // namespace HiviewDFX
106 } // namespace OHOS