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 #ifndef CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_SCHED_POLICY_H_
17 #define CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_SCHED_POLICY_H_
18 
19 #include <cstdint>    // for uint32_t
20 #include <iosfwd>     // for string
21 
22 namespace OHOS {
23 namespace ResourceSchedule {
24 namespace CgroupSetting {
25 using SchedPolicy = uint32_t;
26 
27 /**
28  * Schedule policy define, keep in sync with
29  * (1) cgroup_controller.cpp: bool QuerySchedPolicyFullName(const std::string& name, SchedPolicy& policy);
30  * (2) sched_policy.cpp: const char* GetSchedPolicyShortenedName(SchedPolicy policy);
31  */
32 
33 static constexpr SchedPolicy SP_DEFAULT = 0;
34 static constexpr SchedPolicy SP_BACKGROUND = 1;
35 static constexpr SchedPolicy SP_FOREGROUND = 2;
36 static constexpr SchedPolicy SP_SYSTEM_BACKGROUND = 3;
37 static constexpr SchedPolicy SP_TOP_APP = 4;
38 static constexpr SchedPolicy SP_SYSTEM_DEFAULT = SP_DEFAULT;
39 
40 static constexpr SchedPolicy SP_UPPER_LIMIT = 0xff;
41 
42 /**
43  * Assign all threads in process pid to the cgroup associated with the specified policy.
44  * On platforms which support getpid(), zero pid means current process.
45  * Return value: 0 for success, or -errno for error.
46  *
47  * @param pid process id.
48  * @param policy schedule policy.
49  * @return Return value: 0 for success, or -errno for error.
50  */
51 int SetThreadGroupSchedPolicy(int pid, int policy);
52 
53 /**
54  * Assign thread tid to the cgroup associated with the specified policy.
55  * On platforms which support gettid(), zero tid means current thread.
56  * If the thread is a thread group leader, that is it's gettid() == getpid(),
57  * then the other threads in the same thread group are _not_ affected.
58  * Return value: 0 for success, or -errno for error.
59  *
60  * @param tid thread id.
61  * @param policy variable to accept return value.
62  * @return Return value: 0 for success, or -errno for error.
63  */
64 int SetThreadSchedPolicy(int tid, int policy);
65 
66 /**
67  * Return the policy associated with the cgroup of thread tid via policy pointer.
68  * On platforms which support gettid(), zero tid means current thread.
69  * Return value: 0 for success, or -1 for error and set errno.
70  *
71  * @param tid thread id.
72  * @param policy a policy pointer.
73  * @return Return value: 0 for success, or -errno for error.
74  */
75 int GetThreadSchedPolicy(int tid, SchedPolicy* policy);
76 
77 /**
78  * Return the policy associated with policy name via policy pointer.
79  * Return value: 0 for success, or -1 for error and set errno.
80  *
81  * @param name policy full name.
82  * @param policy a policy pointer.
83  * @return Return value: 0 for success, or -errno for error.
84  */
85 int GetSchedPolicyByName(const std::string& name, SchedPolicy* policy);
86 
87 /**
88  * Return a displayable string corresponding to policy: shortened name.
89  * Return value: NUL-terminated name of unspecified length, nullptr if invalid;
90  * the caller is responsible for displaying the useful part of the string.
91  *
92  * @param policy schedule policy.
93  * @return Return a displayable string corresponding to policy.
94  */
95 const char* GetSchedPolicyShortenedName(SchedPolicy policy);
96 
97 /**
98  * Return a displayable string corresponding to policy: full name.
99  * Return value: NUL-terminated name of unspecified length, nullptr if invalid;
100  * the caller is responsible for displaying the useful part of the string.
101  *
102  * @param policy schedule policy.
103  * @return Return a displayable string corresponding to policy.
104  */
105 const char* GetSchedPolicyFullName(SchedPolicy policy);
106 
107 /**
108  * Add a new kind of sched policy, with unique policy id, full
109  * name and abbreviation name.
110  *
111  * @param policy schedule policy.
112  * @param fullName full name for policy.
113  * @param abbrName abbreviation name for policy.
114  */
115 void AddSchedPolicyDeclaration(SchedPolicy policy, const std::string& fullName, const std::string& abbrName);
116 } // namespace CgroupSetting
117 } // namespace ResourceSchedule
118 } // namespace OHOS
119 
120 #endif // CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_SCHED_POLICY_H_
121