1 /*
2 * Copyright (c) 2022-2023 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 "aging/aging_request.h"
17
18 #include <cinttypes>
19
20 #include "aging/aging_constants.h"
21 #include "app_log_wrapper.h"
22 #include "parameter.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
26 namespace {
27 const std::string SYSTEM_PARAM_DATA_SIZE_THRESHOLD = "persist.sys.bms.aging.policy.data.size.threshold";
28 const std::string SYSTEM_PARAM_RECENILY_USED_THRESHOLD = "persist.sys.bms.aging.policy.recently.used.threshold";
29 }
30 int64_t AgingRequest::totalDataBytesThreshold_ = AgingConstants::DEFAULT_AGING_DATA_SIZE_THRESHOLD;
31 int64_t AgingRequest::oneDayTimeMs_ = AgingConstants::ONE_DAYS_MS;
32
AgingRequest()33 AgingRequest::AgingRequest()
34 {
35 InitAgingPolicySystemParameters();
36 }
37
SortAgingBundles()38 size_t AgingRequest::SortAgingBundles()
39 {
40 AgingUtil::SortAgingBundles(agingBundles_);
41 return agingBundles_.size();
42 }
43
InitAgingDatasizeThreshold()44 void AgingRequest::InitAgingDatasizeThreshold()
45 {
46 char szDatasizeThreshold[AgingConstants::THRESHOLD_VAL_LEN] = {0};
47 int32_t ret = GetParameter(SYSTEM_PARAM_DATA_SIZE_THRESHOLD.c_str(), "", szDatasizeThreshold,
48 AgingConstants::THRESHOLD_VAL_LEN);
49 APP_LOGD("ret is %{public}d, szDatasizeThreshold is %{public}d", ret, atoi(szDatasizeThreshold));
50 if (ret <= 0) {
51 APP_LOGD("GetParameter failed");
52 return;
53 }
54
55 if (strcmp(szDatasizeThreshold, "") != 0) {
56 totalDataBytesThreshold_ = atoi(szDatasizeThreshold);
57 APP_LOGD("AgingRequest init aging data size threshold success");
58 }
59 }
InitAgingOneDayTimeMs()60 void AgingRequest::InitAgingOneDayTimeMs()
61 {
62 char szOneDayTimeMs[AgingConstants::THRESHOLD_VAL_LEN] = {0};
63 int32_t ret = GetParameter(SYSTEM_PARAM_RECENILY_USED_THRESHOLD.c_str(), "", szOneDayTimeMs,
64 AgingConstants::THRESHOLD_VAL_LEN);
65 APP_LOGD("ret is %{public}d, szOneDayTimeMs is %{public}d", ret, atoi(szOneDayTimeMs));
66 if (ret <= 0) {
67 APP_LOGD("GetParameter failed");
68 return;
69 }
70
71 if (strcmp(szOneDayTimeMs, "") != 0) {
72 oneDayTimeMs_ = atoi(szOneDayTimeMs);
73 APP_LOGD("AgingRequest init aging one day time ms success");
74 }
75 }
76
InitAgingPolicySystemParameters()77 void AgingRequest::InitAgingPolicySystemParameters()
78 {
79 InitAgingDatasizeThreshold();
80 InitAgingOneDayTimeMs();
81 }
82
IsReachStartAgingThreshold() const83 bool AgingRequest::IsReachStartAgingThreshold() const
84 {
85 APP_LOGD("tatalDataBytes: %{public}" PRId64 ", totalDataBytesThreshold: %{public}" PRId64,
86 tatalDataBytes_, totalDataBytesThreshold_);
87 return tatalDataBytes_ > totalDataBytesThreshold_;
88 }
89
IsReachEndAgingThreshold() const90 bool AgingRequest::IsReachEndAgingThreshold() const
91 {
92 APP_LOGD("tatalDataBytes: %{public}" PRId64 ", totalDataBytesThreshold: %{public}" PRId64,
93 tatalDataBytes_, totalDataBytesThreshold_);
94 return tatalDataBytes_ < (int64_t)(totalDataBytesThreshold_ * AgingConstants::AGING_SIZE_RATIO);
95 }
96
AddAgingBundle(AgingBundleInfo & bundleInfo)97 void AgingRequest::AddAgingBundle(AgingBundleInfo &bundleInfo)
98 {
99 agingBundles_.emplace_back(bundleInfo);
100 }
101
ResetRequest()102 void AgingRequest::ResetRequest()
103 {
104 agingBundles_.clear();
105 agingCleanType_ = AgingCleanType::CLEAN_CACHE;
106 tatalDataBytes_ = 0;
107 }
108
Dump()109 void AgingRequest::Dump()
110 {
111 for (const auto &agingBundle : agingBundles_) {
112 APP_LOGD("bundle: %{public}s, lastTimeUsed: %{public}" PRId64 ", startCount: %{public}d",
113 agingBundle.GetBundleName().c_str(), agingBundle.GetRecentlyUsedTime(), agingBundle.GetStartCount());
114 }
115 }
116 } // namespace AppExecFwk
117 } // namespace OHOS