• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #ifndef FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_CGROUP_MANAGER_H
17 #define FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_CGROUP_MANAGER_H
18 
19 #include <memory>
20 #include <functional>
21 #include <shared_mutex>
22 
23 #include "event_handler.h"
24 #include "file_descriptor_listener.h"
25 #include "nocopyable.h"
26 #include "singleton.h"
27 #include "unique_fd.h"
28 
29 namespace OHOS {
30 namespace AppExecFwk {
31 class CgroupManager : public FileDescriptorListener, public std::enable_shared_from_this<CgroupManager> {
32 
33     DECLARE_DELAYED_SINGLETON(CgroupManager)
34 public:
35     enum SchedPolicy {
36         SCHED_POLICY_DEFAULT = 0,
37         SCHED_POLICY_BACKGROUND,
38         SCHED_POLICY_FREEZED,
39 
40         SCHED_POLICY_MAX
41     };
42 
43     enum LowMemoryLevel {
44         LOW_MEMORY_LEVEL_LOW = 0,
45         LOW_MEMORY_LEVEL_MEDIUM,
46         LOW_MEMORY_LEVEL_CRITICAL,
47 
48         LOW_MEMORY_LEVEL_MAX
49     };
50 
51     enum SchedPolicyCpu {
52         SCHED_POLICY_CPU_DEFAULT = 0,
53         SCHED_POLICY_CPU_BACKGROUND,
54 
55         SCHED_POLICY_CPU_MAX
56     };
57 
58     enum SchedPolicyFreezer {
59         SCHED_POLICY_FREEZER_FROZEN = 0,
60         SCHED_POLICY_FREEZER_THAWED,
61 
62         SCHED_POLICY_FREEZER_MAX
63     };
64 
65     std::function<void(CgroupManager::LowMemoryLevel)> LowMemoryAlert;
66 
67 public:
68     virtual bool Init();
69     virtual bool InitCheck();
70     virtual bool IsInited() const;
71     virtual bool SetThreadSchedPolicy(int tid, SchedPolicy schedPolicy);
72     virtual bool SetProcessSchedPolicy(int pid, SchedPolicy schedPolicy);
73     virtual void OnReadable(int32_t fd) override;
74 
75 private:
76     std::shared_ptr<EventHandler> eventHandler_;
77     int cpusetTasksFds_[SCHED_POLICY_CPU_MAX] = {-1};
78     int cpuctlTasksFds_[SCHED_POLICY_CPU_MAX] = {-1};
79     int freezerTasksFds_[SCHED_POLICY_FREEZER_MAX] = {-1};
80     int memoryEventControlFd_ = -1;
81     int memoryEventFds_[LOW_MEMORY_LEVEL_MAX] = {-1};
82     int memoryPressureFds_[LOW_MEMORY_LEVEL_MAX] = {-1};
83 
84     bool RegisterLowMemoryMonitor(const int memoryEventFds[LOW_MEMORY_LEVEL_MAX],
85         const int memoryPressureFds[LOW_MEMORY_LEVEL_MAX], const int memoryEventControlFd, const LowMemoryLevel level,
86         const std::shared_ptr<EventHandler> &eventHandler);
87     bool InitCpusetTasksFds(UniqueFd cpusetTasksFds[SCHED_POLICY_CPU_MAX]);
88     bool InitCpuctlTasksFds(UniqueFd cpuctlTasksFds[SCHED_POLICY_CPU_MAX]);
89     bool InitFreezerTasksFds(UniqueFd freezerTasksFds[SCHED_POLICY_FREEZER_MAX]);
90     bool InitMemoryEventControlFd(UniqueFd &memoryEventControlFd);
91     bool InitMemoryEventFds(UniqueFd memoryEventFds[LOW_MEMORY_LEVEL_MAX]);
92     bool InitMemoryPressureFds(UniqueFd memoryPressureFds[LOW_MEMORY_LEVEL_MAX]);
93     bool SetCpusetSubsystem(const int tid, const SchedPolicy schedPolicy);
94     bool SetCpuctlSubsystem(const int tid, const SchedPolicy schedPolicy);
95     bool SetFreezerSubsystem(const int tid, const SchedPolicyFreezer state);
96 };
97 }  // namespace AppExecFwk
98 }  // namespace OHOS
99 
100 #endif  // FOUNDATION_APPEXECFWK_SERVICES_APPMGR_INCLUDE_CGROUP_MANAGER_H
101