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 #include "sched_policy.h"
16 #include <unistd.h> // for gettid, getpid
17 #include "cgroup_action.h" // for CgroupAction
18 #include "parameters.h" // for GetParameter
19 #include "res_exe_type.h" // for ResExeType
20
21 namespace OHOS {
22 namespace ResourceSchedule {
23 namespace CgroupSetting {
24 namespace {
25 const bool IS_NEED_TO_SET_POLICY_BY_EXECUTOR = system::GetParameter("ohos.boot.kernel", "").size() == 0;
26 }
27
SetThreadSchedPolicy(int tid,int policy)28 int SetThreadSchedPolicy(int tid, int policy)
29 {
30 if (tid < 0) {
31 return -1;
32 }
33 if (tid == 0) {
34 tid = gettid();
35 }
36 SchedPolicy schedPolicy = SchedPolicy(policy);
37 int ret = CgroupAction::GetInstance().SetThreadSchedPolicy(tid, schedPolicy) ? 0 : -1;
38 if (IS_NEED_TO_SET_POLICY_BY_EXECUTOR) {
39 ret = CgroupAction::GetInstance().SetSchedPolicyByExecutor(tid, policy,
40 ResourceSchedule::ResExeType::RES_TYPE_SET_THREAD_SCHED_POLICY_SYNC_EVENT);
41 }
42 return ret;
43 }
44
SetThreadGroupSchedPolicy(int pid,int policy)45 int SetThreadGroupSchedPolicy(int pid, int policy)
46 {
47 if (pid < 0) {
48 return -1;
49 }
50 if (pid == 0) {
51 pid = getpid();
52 }
53 SchedPolicy schedPolicy = SchedPolicy(policy);
54 int ret = CgroupAction::GetInstance().SetThreadGroupSchedPolicy(pid, schedPolicy) ? 0 : -1;
55 if (IS_NEED_TO_SET_POLICY_BY_EXECUTOR) {
56 ret = CgroupAction::GetInstance().SetSchedPolicyByExecutor(pid, policy,
57 ResourceSchedule::ResExeType::RES_TYPE_SET_THREAD_GROUP_SCHED_POLICY_SYNC_EVENT);
58 }
59 return ret;
60 }
61
GetThreadSchedPolicy(int tid,SchedPolicy * policy)62 int GetThreadSchedPolicy(int tid, SchedPolicy* policy)
63 {
64 if (tid < 0) {
65 return -1;
66 }
67 if (tid == 0) {
68 tid = gettid();
69 }
70 return CgroupAction::GetInstance().GetSchedPolicy(tid, policy);
71 }
72
GetSchedPolicyByName(const std::string & name,SchedPolicy * policy)73 int GetSchedPolicyByName(const std::string& name, SchedPolicy* policy)
74 {
75 return CgroupAction::GetInstance().GetSchedPolicyByName(name, policy);
76 }
77
GetSchedPolicyShortenedName(SchedPolicy policy)78 const char* GetSchedPolicyShortenedName(SchedPolicy policy)
79 {
80 return CgroupAction::GetInstance().GetSchedPolicyAbbrName(policy);
81 }
82
GetSchedPolicyFullName(SchedPolicy policy)83 const char* GetSchedPolicyFullName(SchedPolicy policy)
84 {
85 return CgroupAction::GetInstance().GetSchedPolicyFullName(policy);
86 }
87
AddSchedPolicyDeclaration(SchedPolicy policy,const std::string & fullName,const std::string & abbrName)88 void AddSchedPolicyDeclaration(SchedPolicy policy, const std::string& fullName, const std::string& abbrName)
89 {
90 return CgroupAction::GetInstance().AddSchedPolicyDeclaration(policy, fullName, abbrName);
91 }
92 } // namespace CgroupSetting
93 } // namespace ResourceSchedule
94 } // namespace OHOS
95