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 "pkg_delay_suspend_info.h"
17 #include "bgtask_config.h"
18 #include "transient_task_log.h"
19 #include "time_provider.h"
20 #include "bgtaskmgr_inner_errors.h"
21 #include "errors.h"
22
23 #include <sstream>
24
25 using namespace std;
26
27 namespace OHOS {
28 namespace BackgroundTaskMgr {
29 namespace {
30 constexpr int32_t MAX_REQUEST_ID = 3;
31 constexpr int32_t MIN_ALLOW_QUOTA_TIME = 16 * MSEC_PER_SEC; // 16s
32 constexpr int32_t WATCHDOG_DELAY_TIME = 6 * MSEC_PER_SEC;
33 }
34
IsAllowRequest()35 ErrCode PkgDelaySuspendInfo::IsAllowRequest()
36 {
37 if (requestList_.size() >= MAX_REQUEST_ID) {
38 return ERR_BGTASK_EXCEEDS_THRESHOLD;
39 }
40
41 UpdateQuota();
42 if (quota_ >= MIN_ALLOW_QUOTA_TIME) {
43 return ERR_OK;
44 }
45
46 return ERR_BGTASK_TIME_INSUFFICIENT;
47 }
48
AddRequest(const shared_ptr<DelaySuspendInfoEx> & delayInfo,const int32_t delayTime,const bool needSetTime)49 void PkgDelaySuspendInfo::AddRequest(const shared_ptr<DelaySuspendInfoEx>& delayInfo,
50 const int32_t delayTime, const bool needSetTime)
51 {
52 if (needSetTime) {
53 int32_t exempted_quota = DelayedSingleton<BgtaskConfig>::GetInstance()->GetTransientTaskExemptedQuato();
54 BGTASK_LOGD("pkgname: %{public}s, requestId: %{public}d exempted_quota %{public}d", pkg_.c_str(),
55 delayInfo->GetRequestId(), exempted_quota);
56 delayInfo->SetActualDelayTime(exempted_quota + WATCHDOG_DELAY_TIME);
57 } else {
58 delayInfo->SetActualDelayTime((quota_ < delayTime) ? quota_ : delayTime);
59 }
60 requestList_.push_back(delayInfo);
61 }
62
RemoveRequest(const int32_t requestId)63 void PkgDelaySuspendInfo::RemoveRequest(const int32_t requestId)
64 {
65 for (auto iter = requestList_.begin(); iter != requestList_.end(); iter++) {
66 if (!(*iter)->IsSameRequestId(requestId)) {
67 continue;
68 }
69 StopAccounting(requestId);
70 requestList_.erase(iter);
71 if (requestList_.empty()) {
72 UpdateQuota();
73 isCounting_ = false;
74 }
75 break;
76 }
77 }
78
GetRemainDelayTime(const int32_t requestId)79 int32_t PkgDelaySuspendInfo::GetRemainDelayTime(const int32_t requestId)
80 {
81 for (auto &info : requestList_) {
82 if (info->IsSameRequestId(requestId)) {
83 return info->GetRemainDelayTime();
84 }
85 }
86 return 0;
87 }
88
StartAccounting(const int32_t requestId)89 void PkgDelaySuspendInfo::StartAccounting(const int32_t requestId)
90 {
91 for (auto &info : requestList_) {
92 if ((requestId != -1) && !info->IsSameRequestId(requestId)) {
93 continue;
94 }
95 if (!isCounting_) {
96 UpdateQuota();
97 isCounting_ = true;
98 }
99 if (info->GetBaseTime() == 0) {
100 info->StartAccounting();
101 timerManager_->AddTimer(info->GetRequestId(), info->GetAdvanceCallbackTime());
102 BGTASK_LOGD("StartAccounting pkgname: %{public}s, requestId: %{public}d, pid: %{public}d",
103 pkg_.c_str(), info->GetRequestId(), info->GetPid());
104 }
105 }
106 }
107
StopAccounting(const int32_t requestId)108 void PkgDelaySuspendInfo::StopAccounting(const int32_t requestId)
109 {
110 for (auto &info : requestList_) {
111 if (!info->IsSameRequestId(requestId) || (info->GetBaseTime() == 0)) {
112 continue;
113 }
114 info->StopAccounting();
115 timerManager_->RemoveTimer(info->GetRequestId());
116 BGTASK_LOGD("StopAccounting pkgname: %{public}s, requestId: %{public}d, pid: %{public}d",
117 pkg_.c_str(), info->GetRequestId(), info->GetPid());
118 }
119 }
120
StopAccountingAll()121 void PkgDelaySuspendInfo::StopAccountingAll()
122 {
123 BGTASK_LOGD("StopAccountingAll %{public}s", pkg_.c_str());
124 for (auto &info : requestList_) {
125 if (info->GetBaseTime() == 0) {
126 continue;
127 }
128 info->StopAccounting();
129 timerManager_->RemoveTimer(info->GetRequestId());
130 BGTASK_LOGD("StopAccountingAll pkgname: %{public}s, requestId: %{public}d, pid: %{public}d",
131 pkg_.c_str(), info->GetRequestId(), info->GetPid());
132 }
133 UpdateQuota();
134 isCounting_ = false;
135 }
136
UpdateQuota(bool reset)137 void PkgDelaySuspendInfo::UpdateQuota(bool reset)
138 {
139 spendTime_ = isCounting_ ? GetModifiedTime() : 0;
140 quota_ -= spendTime_;
141 if (quota_ < 0) {
142 quota_ = 0;
143 }
144 baseTime_ = static_cast<int32_t>(TimeProvider::GetCurrentTime());
145 if (reset) {
146 quota_ = INIT_QUOTA;
147 }
148 BGTASK_LOGD("%{public}s Lastest quota: %{public}d, spendTime: %{public}d, isCounting: %{public}d",
149 pkg_.c_str(), quota_, spendTime_, isCounting_);
150 }
151
GetModifiedTime()152 int32_t PkgDelaySuspendInfo::GetModifiedTime()
153 {
154 bool isExemptedApp = DelayedSingleton<BgtaskConfig>::GetInstance()->
155 IsTransientTaskExemptedQuatoApp(pkg_);
156 int32_t exempted_quota = 0;
157 if (isExemptedApp) {
158 exempted_quota = DelayedSingleton<BgtaskConfig>::GetInstance()->GetTransientTaskExemptedQuato();
159 }
160 BGTASK_LOGD("bundleName:%{public}s exempted: %{public}d exempted_quota:%{public}d",
161 pkg_.c_str(), isExemptedApp, exempted_quota);
162 int32_t time = static_cast<int32_t>(TimeProvider::GetCurrentTime()) - baseTime_ - exempted_quota;
163 return (time < 0) ? 0 : time;
164 }
165
166 } // namespace BackgroundTaskMgr
167 } // namespace OHOS
168