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 "config_reader.h"
28 #include "qos_policy.h"
29 
30 namespace OHOS {
31 namespace ConcurrentTask {
32 class ForegroundAppRecord;
33 
34 class TaskController {
35 public:
36     static TaskController& GetInstance();
37     TaskController() = default;
38     virtual ~TaskController() = default;
39     void ReportData(uint32_t resType, int64_t value, const Json::Value& payload);
40     void QueryInterval(int queryItem, IntervalReply& queryRs);
41     void QueryDeadline(int queryItem, DeadlineReply& ddlReply, const Json::Value& payload);
42     void RequestAuth(const Json::Value& payload);
43     void Init();
44     void Release();
45     int CreateNewRtgGrp(int prioType, int rtNum);
46 
47 private:
48     void TypeMapInit();
49     void QosApplyInit();
50     int TryCreateSystemGroup();
51     void TryCreateHardwareGroup();
52     void TryCreateRsGroup();
53     void TryCreateRSMainGrp();
54     void TryCreateRSRenderGrp();
55     void QueryUi(pid_t uid, IntervalReply& queryRs);
56     void QueryRender(pid_t uid, IntervalReply& queryRs);
57     void QueryRenderService(pid_t uid, IntervalReply& queryRs);
58     void QueryRenderServiceMain(pid_t uid, pid_t pid, IntervalReply& queryRs);
59     void QueryRenderServiceRender(pid_t uid, pid_t pid, IntervalReply& queryRs);
60     void QueryHardware(pid_t uid, pid_t pid, IntervalReply& queryRs);
61     void QueryExecutorStart(pid_t uid, pid_t pid, IntervalReply& queryRs);
62     void QueryHwc(pid_t uid, IntervalReply& queryRs);
63     int GetRequestType(std::string strRequstType);
64     void DealSystemRequest(int requestType, const Json::Value& payload);
65     void NewForeground(int uid, int pid);
66     void NewBackground(int uid, int pid);
67     void NewAppStart(int uid, int pid, const std::string& bundleName);
68     void AppKilled(int uid, int pid);
69     void ContinuousTaskProcess(int uid, int pid, int status);
70     void FocusStatusProcess(int uid, int pid, int status);
71     void InteractionSceneProcess(int status);
72     void DeadlinePerfMode();
73     void DeadlinePowerMode();
74     int AuthSystemProcess(int pid);
75     bool ConfigReaderInit();
76     bool ModifySystemRate(const Json::Value& payload);
77     void SetAppRate(const Json::Value& payload);
78     int FindRateFromInfo(int uiTid, const Json::Value& payload);
79     void SetRenderServiceRate(const Json::Value& payload);
80     void SetAppAndRenderServiceRate(int appRate, int rsRate);
81     bool CheckJsonValid(const Json::Value& payload);
82     void SetFrameRate(int rtgId, int rate);
83     ForegroundAppRecord* GetRecordOfPid(int pid);
84     void PrintInfo();
85     bool ParsePayload(const Json::Value& payload, int& uid, int& pid, std::string& bundleName);
86     std::string GetProcessNameByToken();
87     void ModifyGameState(const Json::Value& payload);
88     int GetGamePid(const std::string &gameMsg) const;
89     GameStatus GetGameScene(const std::string &gameMsg) const;
90     void NewForegroundAppRecord(int pid, int uiTid, bool ddlEnabled);
91 
92     std::mutex appInfoLock_;
93     std::mutex rateInfoLock_;
94     std::mutex executorStartLock_;
95     std::mutex ddlPowerModeLock_;
96     std::mutex configReaderMutex_;
97     std::list<ForegroundAppRecord> foregroundApp_ = {};
98     std::list<int> rsThreads_ = {};
99     std::unordered_map<std::string, int> msgType_ = {};
100     QosPolicy qosPolicy_;
101     std::vector<int> authApps_;
102     int renderServiceMainGrpId_ = -1;
103     int renderServiceRenderGrpId_ = -1;
104     int renderServiceMainTid_ = -1;
105     int renderServiceRenderTid_ = -1;
106     int hardwareGrpId_ = -1;
107     int hardwareTid_ = -1;
108     int systemRate_ = 0;
109     int uniAppRate_ = 0;
110     bool rtgEnabled_ = false;
111     bool configEnable_ = false;
112     int authedRSPid_ = 0;
113     bool ddlSceneSchedSwitch_ = false;
114     bool ddlPowerModeEnable_ = false;
115     std::atomic<int> curGamePid_ = -1;
116     int executorNum_ = 0;
117     std::map<int, std::string> appBundleName;
118     std::unique_ptr<ConfigReader> configReader_ = nullptr;
119 
120     const std::string RENDER_SERVICE_PROCESS_NAME = "render_service";
121     const std::string RESOURCE_SCHEDULE_PROCESS_NAME = "resource_schedule_service";
122     const std::string GAME_ACCELERATE_SCHED_PROCESS_NAME = "game_accelerate_schedule";
123 };
124 
125 class ForegroundAppRecord {
126 public:
127     explicit ForegroundAppRecord(int pid, int uiTid, bool createGrp = true);
128     ~ForegroundAppRecord();
129 
130     void AddKeyThread(int tid, int prio = PRIO_NORMAL);
131     bool BeginScene();
132     bool EndScene();
133     int GetPid() const;
134     int GetGrpId() const;
135     int GetRate() const;
136     void SetRate(int appRate);
137     int GetUiTid() const;
138     void SetUiTid(int uiTid);
139     void SetGrpId(int grpId);
140     bool IsValid();
141     void PrintKeyThreads();
142 
143 private:
144     int pid_ = 0;
145     int grpId_ = 0;
146     int rate_ = 0;
147     int uiTid_ = 0;
148     std::unordered_set<int> keyThreads_;
149 };
150 } // namespace ConcurrentTask
151 } // namespace OHOS
152 
153 #endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H
154