1 /*
2 * Copyright (c) 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 "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 int64_t AgingRequest::totalDataBytesThreshold = AgingConstants::DEFAULT_AGING_DATA_SIZE_THRESHOLD;
27 int64_t AgingRequest::oneDayTimeMs = AgingConstants::ONE_DAYS_MS;
AgingRequest()28 AgingRequest::AgingRequest()
29 {
30 InitAgingPolicySystemParameters();
31 }
32
InitAgingDatasizeThreshold()33 void AgingRequest::InitAgingDatasizeThreshold()
34 {
35 char szDatasizeThreshold[AgingConstants::THRESHOLD_VAL_LEN] = {0};
36 int32_t ret = GetParameter(AgingConstants::SYSTEM_PARAM_DATA_SIZE_THRESHOLD.c_str(), "", szDatasizeThreshold,
37 AgingConstants::THRESHOLD_VAL_LEN);
38 APP_LOGD("ret is %{public}d, szDatasizeThreshold is %{public}d", ret, atoi(szDatasizeThreshold));
39 if (ret <= 0) {
40 APP_LOGD("GetParameter failed");
41 return;
42 }
43 if (strcmp(szDatasizeThreshold, "") != 0) {
44 totalDataBytesThreshold = atoi(szDatasizeThreshold);
45 APP_LOGD("AgingRequest init aging data size threshold success");
46 }
47 }
48
InitAgingOneDayTimeMs()49 void AgingRequest::InitAgingOneDayTimeMs()
50 {
51 char szOneDayTimeMs[AgingConstants::THRESHOLD_VAL_LEN] = {0};
52 int32_t ret = GetParameter(AgingConstants::SYSTEM_PARAM_RECENILY_USED_THRESHOLD.c_str(), "", szOneDayTimeMs,
53 AgingConstants::THRESHOLD_VAL_LEN);
54 APP_LOGD("ret is %{public}d, szOneDayTimeMs is %{public}d", ret, atoi(szOneDayTimeMs));
55 if (ret <= 0) {
56 APP_LOGD("GetParameter failed");
57 return;
58 }
59 if (strcmp(szOneDayTimeMs, "") != 0) {
60 oneDayTimeMs = atoi(szOneDayTimeMs);
61 APP_LOGD("AgingRequest init aging one day time ms success");
62 }
63 }
64
InitAgingPolicySystemParameters()65 void AgingRequest::InitAgingPolicySystemParameters()
66 {
67 InitAgingDatasizeThreshold();
68 InitAgingOneDayTimeMs();
69 }
70
IsReachStartAgingThreshold() const71 bool AgingRequest::IsReachStartAgingThreshold() const
72 {
73 APP_LOGD("tatalDataBytes: %{public}" PRId64 ", totalDataBytesThreshold: %{public}" PRId64, tatalDataBytes,
74 totalDataBytesThreshold);
75 return tatalDataBytes > totalDataBytesThreshold;
76 }
77
IsReachEndAgingThreshold() const78 bool AgingRequest::IsReachEndAgingThreshold() const
79 {
80 APP_LOGD("tatalDataBytes: %{public}" PRId64 ", totalDataBytesThreshold: %{public}" PRId64, tatalDataBytes,
81 totalDataBytesThreshold);
82 return tatalDataBytes < (int64_t)(totalDataBytesThreshold * AgingConstants::AGING_SIZE_RATIO);
83 }
84
AddAgingBundle(AgingBundleInfo & bundleInfo)85 void AgingRequest::AddAgingBundle(AgingBundleInfo &bundleInfo)
86 {
87 agingBundles.emplace_back(bundleInfo);
88 }
89
RequestReset()90 void AgingRequest::RequestReset()
91 {
92 agingBundles.clear();
93 tatalDataBytes = 0;
94 InitAgingPolicySystemParameters();
95 }
96 } // namespace AppExecFwk
97 } // namespace OHOS