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 <iosfwd> // for string 21 #include <map> // for map 22 #include <string> // for basic_string 23 #include <vector> // for vector 24 #include "ffrt.h" 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 SetSchedPolicyByExecutor(int tid, int policy, uint32_t resType); 43 int GetSchedPolicy(int tid, SchedPolicy* policy); 44 int GetSchedPolicyByName(const std::string& name, SchedPolicy* policy); 45 46 private: 47 CgroupAction(); 48 ~CgroupAction() = default; 49 50 CgroupAction(const CgroupAction&) = delete; 51 CgroupAction& operator=(const CgroupAction &) = delete; 52 CgroupAction(CgroupAction&&) = delete; 53 CgroupAction& operator=(CgroupAction&&) = delete; 54 55 ffrt::mutex mutex_; // for sched policy maps 56 bool allowToAdd_ = true; 57 std::map<SchedPolicy, std::string> fullNames_; 58 std::map<SchedPolicy, std::string> abbrNames_; 59 60 bool LoadConfigFile(); 61 bool IsEnabled(); 62 bool IsSchedPolicyValid(SchedPolicy policy); 63 static bool ParseConfigFileToJsonObj(nlohmann::json& jsonObjRoot); 64 }; 65 } // namespace CgroupSetting 66 } // namespace ResourceSchedule 67 } // namespace OHOS 68 #endif // CGROUP_SCHED_FRAMEWORK_PROCESS_GROUP_INCLUDE_CGROUP_ACTION_H_ 69