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 <iostream>
17 #include <ctime>
18 #include <cstring>
19 #include "sched_policy.h"
20
21 using namespace OHOS::ResourceSchedule::CgroupSetting;
22 constexpr int ARGUMENT_NUMBER_TWO = 2;
23 constexpr int ARGUMENT_NUMBER_THREE = 3;
24
CovertSchedPolicyStringToEnumType(char * policyStr,SchedPolicy * policy)25 static bool CovertSchedPolicyStringToEnumType(char* policyStr, SchedPolicy* policy)
26 {
27 for (int i = SP_DEFAULT; i < SP_CNT; i++) {
28 SchedPolicy policyItem = static_cast<SchedPolicy>(i);
29 const char* stringItem = GetSchedPolicyFullName(policyItem);
30 if (!strcmp(stringItem, "error")) {
31 continue;
32 }
33 if (!strcmp(policyStr, stringItem)) {
34 *policy = policyItem;
35 return true;
36 }
37 }
38 return false;
39 }
40
SetThreadSchedPolicyTest(int argc,char * argv[])41 static void SetThreadSchedPolicyTest(int argc, char *argv[])
42 {
43 if (argc == ARGUMENT_NUMBER_THREE) {
44 int tid = std::stoi(argv[1]);
45 char* policyStr = argv[2];
46 SchedPolicy policy;
47 if (!CovertSchedPolicyStringToEnumType(policyStr, &policy)) {
48 return;
49 }
50 if (SetThreadSchedPolicy(tid, policy) == 0) {
51 std::cout << "Set thread tid = << "<< tid << " SolicyPolicy = "<< policy << "ok." << std::endl;
52 }
53 }
54 }
55
SetThreadGroupSchedPolicyTest(int argc,char * argv[])56 static void SetThreadGroupSchedPolicyTest(int argc, char *argv[])
57 {
58 if (argc == ARGUMENT_NUMBER_THREE) {
59 int pid = std::stoi(argv[1]);
60 char* policyStr = argv[2];
61 SchedPolicy policy;
62 if (!CovertSchedPolicyStringToEnumType(policyStr, &policy)) {
63 return;
64 }
65 if (SetThreadGroupSchedPolicy(pid, policy) == 0) {
66 std::cout << "Set thread group pid = << "<< pid << " SolicyPolicy = "<< policy << "ok." << std::endl;
67 }
68 }
69 }
70
GetThreadSchedPolicyTest(int argc,char * argv[])71 static void GetThreadSchedPolicyTest(int argc, char *argv[])
72 {
73 if (argc == ARGUMENT_NUMBER_TWO) {
74 int tid = std::stoi(argv[1]);
75 SchedPolicy policy;
76 if (GetThreadSchedPolicy(tid, &policy) == 0) {
77 std::cout << "Get the SolicyPolicy of thread tid = " << tid << " is " << policy << "\n";
78 }
79 }
80 }
81
GetSchedPolicyShortenedNameTest(int argc,char * argv[])82 static void GetSchedPolicyShortenedNameTest(int argc, char *argv[])
83 {
84 if (argc == ARGUMENT_NUMBER_TWO) {
85 int policyNum = std::stoi(argv[1]);
86 if (policyNum < SP_DEFAULT || policyNum > SP_MAX) {
87 return;
88 }
89 SchedPolicy policy = static_cast<SchedPolicy>(policyNum);
90 const char* policyStr = GetSchedPolicyShortenedName(policy);
91 std::cout << "Get SchedPolicy shortened name of policy num = " << policyNum << " is:" << policyStr << std::endl;
92 }
93 }
94
GetSchedPolicyFullNameTest(int argc,char * argv[])95 static void GetSchedPolicyFullNameTest(int argc, char *argv[])
96 {
97 if (argc == ARGUMENT_NUMBER_TWO) {
98 int policyNum = std::stoi(argv[1]);
99 if (policyNum < SP_DEFAULT || policyNum > SP_MAX) {
100 return;
101 }
102 SchedPolicy policy = static_cast<SchedPolicy>(policyNum);
103 const char* policyStr = GetSchedPolicyShortenedName(policy);
104 std::cout << "Get SchedPolicy full name of policy num = " << policyNum << " is:" << policyStr << std::endl;
105 }
106 }
107
main(int argc,char * argv[])108 int main(int argc, char *argv[])
109 {
110 if (argc > ARGUMENT_NUMBER_TWO) {
111 char* operation = argv[1];
112 if (!strcmp(operation, "SetThreadSchedPolicyTest")) {
113 SetThreadSchedPolicyTest(argc, argv);
114 } else if (!strcmp(operation, "SetThreadGroupSchedPolicyTest")) {
115 SetThreadGroupSchedPolicyTest(argc, argv);
116 } else if (!strcmp(operation, "GetThreadSchedPolicyTest")) {
117 GetThreadSchedPolicyTest(argc, argv);
118 } else if (!strcmp(operation, "GetSchedPolicyShortenedNameTest")) {
119 GetSchedPolicyShortenedNameTest(argc, argv);
120 } else if (!strcmp(operation, "GetSchedPolicyFullNameTest")) {
121 GetSchedPolicyFullNameTest(argc, argv);
122 }
123 }
124 return 0;
125 }
126