• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "bundle_priority_info.h"
17 #include "memmgr_log.h"
18 
19 namespace OHOS {
20 namespace Memory {
21 namespace {
22 const std::string TAG = "BundlePriorityInfo";
23 } // namespace
24 
BundlePriorityInfo(const std::string & name,int bundleUid)25 BundlePriorityInfo::BundlePriorityInfo(const std::string &name, int bundleUid):name_(name), uid_(bundleUid)
26 {
27     this->priority_ = RECLAIM_PRIORITY_UNKNOWN;
28     this->accountId_ = bundleUid / USER_ID_SHIFT;
29     this->state_ = BundleState::STATE_DEFAULT;
30 }
31 
BundlePriorityInfo(const std::string & name,int bundleUid,int priority)32 BundlePriorityInfo::BundlePriorityInfo(const std::string &name, int bundleUid, int priority):name_(name),
33     uid_(bundleUid), priority_(priority)
34 {
35     this->accountId_ = bundleUid / USER_ID_SHIFT;
36     this->state_ = BundleState::STATE_DEFAULT;
37 }
38 
BundlePriorityInfo(const std::string & name,int bundleUid,int priority,int accountId,BundleState state)39 BundlePriorityInfo::BundlePriorityInfo(const std::string &name, int bundleUid, int priority, int accountId,
40     BundleState state) : name_(name), uid_(bundleUid), priority_(priority), accountId_(accountId), state_(state)
41 {
42 }
43 
BundlePriorityInfo(const BundlePriorityInfo & copyBundle)44 BundlePriorityInfo::BundlePriorityInfo(const BundlePriorityInfo &copyBundle) : name_(copyBundle.name_),
45     uid_(copyBundle.uid_), priority_(copyBundle.priority_), accountId_(copyBundle.accountId_), state_(copyBundle.state_)
46 {
47     for (auto itrProcess = copyBundle.procs_.begin(); itrProcess != copyBundle.procs_.end(); itrProcess++) {
48         ProcessPriorityInfo processInfo = itrProcess->second;
49         ProcessPriorityInfo tmpProcess(processInfo);
50 
51         this->procs_.insert(std::make_pair(tmpProcess.pid_, tmpProcess));
52     }
53 }
54 
HasProc(pid_t pid)55 bool BundlePriorityInfo::HasProc(pid_t pid)
56 {
57     if (procs_.count(pid) == 0) {
58         return false;
59     }
60     return true;
61 }
62 
AddProc(ProcessPriorityInfo & newProcess)63 void BundlePriorityInfo::AddProc(ProcessPriorityInfo &newProcess)
64 {
65     procs_.insert(std::make_pair(newProcess.pid_, newProcess));
66 }
67 
RemoveProcByPid(pid_t pid)68 void BundlePriorityInfo::RemoveProcByPid(pid_t pid)
69 {
70     procs_.erase(pid);
71 }
72 
GetProcsCount()73 int BundlePriorityInfo::GetProcsCount()
74 {
75     return procs_.size();
76 }
77 
FindProcByPid(pid_t pid)78 ProcessPriorityInfo& BundlePriorityInfo::FindProcByPid(pid_t pid)
79 {
80     return procs_.at(pid);
81 }
82 
GetMinProcPriority()83 int BundlePriorityInfo::GetMinProcPriority()
84 {
85     int min_priority = RECLAIM_PRIORITY_UNKNOWN;
86     for (auto i = procs_.begin(); i != procs_.end(); ++i) {
87         if (i->second.priority_ < min_priority) {
88             min_priority = i->second.priority_;
89         }
90     }
91     return min_priority;
92 }
93 
SetPriority(int targetPriority)94 void BundlePriorityInfo::SetPriority(int targetPriority)
95 {
96     priority_ = targetPriority;
97 }
98 
UpdatePriority()99 void BundlePriorityInfo::UpdatePriority()
100 {
101     int targetPriority = GetMinProcPriority();
102     if (targetPriority >= RECLAIM_PRIORITY_MAX) {
103         targetPriority = RECLAIM_PRIORITY_MAX;
104     } else if (targetPriority <= RECLAIM_PRIORITY_MIN) {
105         targetPriority = RECLAIM_PRIORITY_MIN;
106     }
107     SetPriority(targetPriority);
108     HILOGI("bundleName=%{public}s, priority=%{public}d", name_.c_str(), priority_);
109 }
110 
GetState()111 BundleState BundlePriorityInfo::GetState()
112 {
113     return state_;
114 }
115 
SetState(BundleState state)116 void BundlePriorityInfo::SetState(BundleState state)
117 {
118     state_ = state;
119 }
120 
IncreaseProcsPriority(int delta)121 void BundlePriorityInfo::IncreaseProcsPriority(int delta)
122 {
123     for (auto i = procs_.begin(); i != procs_.end(); ++i) {
124         int oldPriority = i->second.priority_;
125         i->second.SetPriority(oldPriority + delta);
126         int newPriority = i->second.priority_;
127         HILOGI("pid = %{public}d, oldPriority = %{public}d, newPriority = %{public}d",
128                i->second.pid_, oldPriority, newPriority);
129     }
130 }
131 } // namespace Memory
132 } // namespace OHOS
133