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 CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H 17 #define CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H 18 19 #include <unordered_set> 20 #include <list> 21 #include <mutex> 22 #include <unordered_map> 23 #include <vector> 24 #include <atomic> 25 #include "json/json.h" 26 #include "concurrent_task_type.h" 27 #include "qos_policy.h" 28 29 namespace OHOS { 30 namespace ConcurrentTask { 31 class ForegroundAppRecord; 32 33 class TaskController { 34 public: 35 static TaskController& GetInstance(); 36 TaskController() = default; 37 virtual ~TaskController() = default; 38 void ReportData(uint32_t resType, int64_t value, const Json::Value& payload); 39 void QueryInterval(int queryItem, IntervalReply& queryRs); 40 void QueryDeadline(int queryItem, DeadlineReply& ddlReply, const Json::Value& payload); 41 void RequestAuth(const Json::Value& payload); 42 void Init(); 43 void Release(); 44 int CreateNewRtgGrp(int prioType, int rtNum); 45 46 private: 47 void TypeMapInit(); 48 void QosApplyInit(); 49 int TryCreateSystemGroup(); 50 void TryCreateHardwareGroup(); 51 void TryCreateRsGroup(); 52 void QueryUi(pid_t uid, IntervalReply& queryRs); 53 void QueryRender(pid_t uid, IntervalReply& queryRs); 54 void QueryRenderService(pid_t uid, pid_t pid, IntervalReply& queryRs); 55 void QueryHardware(pid_t uid, pid_t pid, IntervalReply& queryRs); 56 void QueryExecutorStart(pid_t uid, pid_t pid, IntervalReply& queryRs); 57 void QueryHwc(pid_t uid, IntervalReply& queryRs); 58 int GetRequestType(std::string strRequstType); 59 void DealSystemRequest(int requestType, const Json::Value& payload); 60 void NewForeground(int uid, int pid); 61 void NewBackground(int uid, int pid); 62 void NewAppStart(int uid, int pid); 63 void AppKilled(int uid, int pid); 64 void ContinuousTaskProcess(int uid, int pid, int status); 65 void FocusStatusProcess(int uid, int pid, int status); 66 int AuthSystemProcess(int pid); 67 bool ModifySystemRate(const Json::Value& payload); 68 void SetAppRate(const Json::Value& payload); 69 int FindRateFromInfo(int uiTid, const Json::Value& payload); 70 void SetRenderServiceRate(const Json::Value& payload); 71 bool CheckJsonValid(const Json::Value& payload); 72 void SetFrameRate(int rtgId, int rate); 73 std::list<ForegroundAppRecord>::iterator GetRecordOfPid(int pid); 74 void PrintInfo(); 75 bool ParsePayload(const Json::Value& payload, int& uid, int& pid); 76 std::string GetProcessNameByToken(); 77 void ModifyGameState(const Json::Value& payload); 78 int GetGamePid(const std::string &gameMsg) const; 79 GameStatus GetGameScene(const std::string &gameMsg) const; 80 void NewForegroundAppRecord(int pid, int uiTid, bool ddlEnabled); 81 82 std::mutex appInfoLock_; 83 std::mutex rateInfoLock_; 84 std::mutex executorStartLock_; 85 std::list<ForegroundAppRecord> foregroundApp_ = {}; 86 std::unordered_map<std::string, int> msgType_ = {}; 87 QosPolicy qosPolicy_; 88 std::vector<int> authApps_; 89 int renderServiceGrpId_ = -1; 90 int hardwareGrpId_ = -1; 91 int rsTid_ = -1; 92 int hardwareTid_ = -1; 93 int systemRate_ = 0; 94 bool rtgEnabled_ = false; 95 bool rsAuthed_ = false; 96 std::atomic<int> curGamePid_ = -1; 97 int executorNum_ = 0; 98 99 const std::string RENDER_SERVICE_PROCESS_NAME = "render_service"; 100 const std::string RESOURCE_SCHEDULE_PROCESS_NAME = "resource_schedule_service"; 101 const std::string MEDIA_SERVICE_PROCESS_NAME = "media_service"; 102 }; 103 104 class ForegroundAppRecord { 105 public: 106 explicit ForegroundAppRecord(int pid, int uiTid, bool createGrp = true); 107 ~ForegroundAppRecord(); 108 109 void AddKeyThread(int tid, int prio = PRIO_NORMAL); 110 bool BeginScene(); 111 bool EndScene(); 112 int GetPid() const; 113 int GetGrpId() const; 114 int GetRate() const; 115 void SetRate(int appRate); 116 int GetUiTid() const; 117 void SetUiTid(int uiTid); 118 void SetGrpId(int grpId); 119 bool IsValid(); 120 void PrintKeyThreads(); 121 122 private: 123 int pid_ = 0; 124 int grpId_ = 0; 125 int rate_ = 0; 126 int uiTid_ = 0; 127 std::unordered_set<int> keyThreads_; 128 }; 129 } // namespace ConcurrentTask 130 } // namespace OHOS 131 132 #endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H 133