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 <string>
17 #include <sstream>
18 #include "memmgr_log.h"
19 #include "kernel_interface.h"
20 #include "oom_score_adj_utils.h"
21 
22 namespace OHOS {
23 namespace Memory {
24 namespace {
25 const std::string TAG = "ReclaimPriorityManager";
26 }
27 
WriteOomScoreAdjToKernel(std::shared_ptr<BundlePriorityInfo> bundle)28 bool OomScoreAdjUtils::WriteOomScoreAdjToKernel(std::shared_ptr<BundlePriorityInfo> bundle)
29 {
30     HILOGD("called");
31     if (bundle == nullptr) {
32         return false;
33     }
34     for (auto i = bundle->procs_.begin(); i != bundle->procs_.end(); ++i) {
35         int priority = i->second.priority_;
36         pid_t pid = i->second.pid_;
37         std::stringstream ss;
38         ss << "/proc/" << pid << "/oom_score_adj";
39         std::string path = ss.str();
40         std::string content = std::to_string(priority);
41         HILOGD("prepare to echo %{public}s > %{public}s", content.c_str(), path.c_str());
42         KernelInterface::GetInstance().EchoToPath(path.c_str(), content.c_str());
43     }
44     return true;
45 }
46 
WriteOomScoreAdjToKernel(pid_t pid,int priority)47 bool OomScoreAdjUtils::WriteOomScoreAdjToKernel(pid_t pid, int priority)
48 {
49     HILOGD("called");
50     std::stringstream ss;
51     ss << "/proc/" << pid << "/oom_score_adj";
52     std::string path = ss.str();
53     std::string content = std::to_string(priority);
54     HILOGD("prepare to echo %{public}s > %{public}s", content.c_str(), path.c_str());
55     KernelInterface::GetInstance().EchoToPath(path.c_str(), content.c_str());
56     return true;
57 }
58 } // namespace Memory
59 } // namespace OHOS
60