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_CGROUP_ACTION_H_
17 #define CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_CGROUP_ACTION_H_
18 
19 #include <functional>         // for less
20 #include <mutex>              // for mutex
21 #include <iosfwd>             // for string
22 #include <map>                // for map
23 #include <string>             // for basic_string
24 #include <vector>             // for vector
25 #include "sched_policy.h"     // for SchedPolicy
26 #include "nlohmann/json.hpp"
27 
28 namespace OHOS {
29 namespace ResourceSchedule {
30 namespace CgroupSetting {
31 class CgroupAction {
32 public:
33     static CgroupAction& GetInstance();
34 
35     void AddSchedPolicyDeclaration(SchedPolicy policy, const std::string& fullName, const std::string& abbrName);
36     std::vector<SchedPolicy> GetSchedPolicyList();
37     const char* GetSchedPolicyFullName(SchedPolicy policy);
38     const char* GetSchedPolicyAbbrName(SchedPolicy policy);
39 
40     bool SetThreadSchedPolicy(int tid, SchedPolicy policy);
41     bool SetThreadGroupSchedPolicy(int tid, SchedPolicy policy);
42     int GetSchedPolicy(int tid, SchedPolicy* policy);
43     int GetSchedPolicyByName(const std::string& name, SchedPolicy* policy);
44 
45 private:
46     CgroupAction();
47     ~CgroupAction() = default;
48 
49     CgroupAction(const CgroupAction&) = delete;
50     CgroupAction& operator=(const CgroupAction &) = delete;
51     CgroupAction(CgroupAction&&) = delete;
52     CgroupAction& operator=(CgroupAction&&) = delete;
53 
54     std::mutex mutex_; // for sched policy maps
55     bool allowToAdd_ = true;
56     std::map<SchedPolicy, std::string> fullNames_;
57     std::map<SchedPolicy, std::string> abbrNames_;
58 
59     bool LoadConfigFile();
60     bool IsEnabled();
61     bool IsSchedPolicyValid(SchedPolicy policy);
62     static bool ParseConfigFileToJsonObj(nlohmann::json& jsonObjRoot);
63 };
64 } // namespace CgroupSetting
65 } // namespace ResourceSchedule
66 } // namespace OHOS
67 #endif // CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_CGROUP_ACTION_H_
68