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 std::lock_guard<std::mutex> lock(mutex_);
41 AgingUtil::SortAgingBundles(agingBundles_);
42 return agingBundles_.size();
43 }
44
InitAgingDatasizeThreshold()45 void AgingRequest::InitAgingDatasizeThreshold()
46 {
47 char szDatasizeThreshold[AgingConstants::THRESHOLD_VAL_LEN] = {0};
48 int32_t ret = GetParameter(SYSTEM_PARAM_DATA_SIZE_THRESHOLD.c_str(), "", szDatasizeThreshold,
49 AgingConstants::THRESHOLD_VAL_LEN);
50 APP_LOGD("ret is %{public}d, szDatasizeThreshold is %{public}d", ret, atoi(szDatasizeThreshold));
51 if (ret <= 0) {
52 APP_LOGD("GetParameter failed");
53 return;
54 }
55
56 if (strcmp(szDatasizeThreshold, "") != 0) {
57 totalDataBytesThreshold_ = atoi(szDatasizeThreshold);
58 APP_LOGD("AgingRequest init aging data size threshold success");
59 }
60 }
61
InitAgingOneDayTimeMs()62 void AgingRequest::InitAgingOneDayTimeMs()
63 {
64 char szOneDayTimeMs[AgingConstants::THRESHOLD_VAL_LEN] = {0};
65 int32_t ret = GetParameter(SYSTEM_PARAM_RECENILY_USED_THRESHOLD.c_str(), "", szOneDayTimeMs,
66 AgingConstants::THRESHOLD_VAL_LEN);
67 APP_LOGD("ret is %{public}d, szOneDayTimeMs is %{public}d", ret, atoi(szOneDayTimeMs));
68 if (ret <= 0) {
69 APP_LOGD("GetParameter failed");
70 return;
71 }
72
73 if (strcmp(szOneDayTimeMs, "") != 0) {
74 oneDayTimeMs_ = atoi(szOneDayTimeMs);
75 APP_LOGD("AgingRequest init aging one day time ms success");
76 }
77 }
78
InitAgingPolicySystemParameters()79 void AgingRequest::InitAgingPolicySystemParameters()
80 {
81 InitAgingDatasizeThreshold();
82 InitAgingOneDayTimeMs();
83 }
84
IsReachStartAgingThreshold() const85 bool AgingRequest::IsReachStartAgingThreshold() const
86 {
87 APP_LOGD("totalDataBytes: %{public}" PRId64 ", totalDataBytesThreshold: %{public}" PRId64,
88 totalDataBytes_, totalDataBytesThreshold_);
89 return totalDataBytes_ > totalDataBytesThreshold_;
90 }
91
IsReachEndAgingThreshold() const92 bool AgingRequest::IsReachEndAgingThreshold() const
93 {
94 APP_LOGD("totalDataBytes: %{public}" PRId64 ", totalDataBytesThreshold: %{public}" PRId64,
95 totalDataBytes_, totalDataBytesThreshold_);
96 return totalDataBytes_ < (int64_t)(totalDataBytesThreshold_ * AgingConstants::AGING_SIZE_RATIO);
97 }
98
AddAgingBundle(AgingBundleInfo & bundleInfo)99 void AgingRequest::AddAgingBundle(AgingBundleInfo &bundleInfo)
100 {
101 std::lock_guard<std::mutex> lock(mutex_);
102 agingBundles_.emplace_back(bundleInfo);
103 }
104
ResetRequest()105 void AgingRequest::ResetRequest()
106 {
107 std::lock_guard<std::mutex> lock(mutex_);
108 agingBundles_.clear();
109 agingCleanType_ = AgingCleanType::CLEAN_CACHE;
110 totalDataBytes_ = 0;
111 }
112
Dump()113 void AgingRequest::Dump()
114 {
115 std::lock_guard<std::mutex> lock(mutex_);
116 for (const auto &agingBundle : agingBundles_) {
117 APP_LOGD("bundle: %{public}s, lastTimeUsed: %{public}" PRId64 ", startCount: %{public}d",
118 agingBundle.GetBundleName().c_str(), agingBundle.GetRecentlyUsedTime(), agingBundle.GetStartCount());
119 }
120 }
121 } // namespace AppExecFwk
122 } // namespace OHOS