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 
19 namespace OHOS {
20 namespace ResourceSchedule {
21 namespace CgroupSetting {
SetThreadSchedPolicy(int tid,int policy)22 int SetThreadSchedPolicy(int tid, int policy)
23 {
24     if (tid < 0) {
25         return -1;
26     }
27     if (tid == 0) {
28         tid = gettid();
29     }
30     SchedPolicy schedPolicy = SchedPolicy(policy);
31     return CgroupAction::GetInstance().SetThreadSchedPolicy(tid, schedPolicy) ? 0 : -1;
32 }
33 
SetThreadGroupSchedPolicy(int pid,int policy)34 int SetThreadGroupSchedPolicy(int pid, int policy)
35 {
36     if (pid < 0) {
37         return -1;
38     }
39     if (pid == 0) {
40         pid = getpid();
41     }
42     SchedPolicy schedPolicy = SchedPolicy(policy);
43     return CgroupAction::GetInstance().SetThreadGroupSchedPolicy(pid, schedPolicy) ? 0 : -1;
44 }
45 
GetThreadSchedPolicy(int tid,SchedPolicy * policy)46 int GetThreadSchedPolicy(int tid, SchedPolicy* policy)
47 {
48     if (tid < 0) {
49         return -1;
50     }
51     if (tid == 0) {
52         tid = gettid();
53     }
54     return CgroupAction::GetInstance().GetSchedPolicy(tid, policy);
55 }
56 
GetSchedPolicyByName(const std::string & name,SchedPolicy * policy)57 int GetSchedPolicyByName(const std::string& name, SchedPolicy* policy)
58 {
59     return CgroupAction::GetInstance().GetSchedPolicyByName(name, policy);
60 }
61 
GetSchedPolicyShortenedName(SchedPolicy policy)62 const char* GetSchedPolicyShortenedName(SchedPolicy policy)
63 {
64     return CgroupAction::GetInstance().GetSchedPolicyAbbrName(policy);
65 }
66 
GetSchedPolicyFullName(SchedPolicy policy)67 const char* GetSchedPolicyFullName(SchedPolicy policy)
68 {
69     return CgroupAction::GetInstance().GetSchedPolicyFullName(policy);
70 }
71 
AddSchedPolicyDeclaration(SchedPolicy policy,const std::string & fullName,const std::string & abbrName)72 void AddSchedPolicyDeclaration(SchedPolicy policy, const std::string& fullName, const std::string& abbrName)
73 {
74     return CgroupAction::GetInstance().AddSchedPolicyDeclaration(policy, fullName, abbrName);
75 }
76 } // namespace CgroupSetting
77 } // namespace ResourceSchedule
78 } // namespace OHOS
79