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, int appType);
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     bool IsVideoApp(int pid);
75     int ParseAppType(const Json::Value& payload);
76     int AuthSystemProcess(int pid);
77     bool ConfigReaderInit();
78     bool ModifySystemRate(const Json::Value& payload);
79     void SetAppRate(const Json::Value& payload);
80     int FindRateFromInfo(int uiTid, const Json::Value& payload);
81     void SetRenderServiceRate(const Json::Value& payload);
82     void SetAppAndRenderServicRate(int appRate, int rsRate);
83     bool CheckJsonValid(const Json::Value& payload);
84     void SetFrameRate(int rtgId, int rate);
85     std::list<ForegroundAppRecord>::iterator GetRecordOfPid(int pid);
86     void PrintInfo();
87     bool ParsePayload(const Json::Value& payload, int& uid, int& pid, std::string& bundleName);
88     std::string GetProcessNameByToken();
89     void ModifyGameState(const Json::Value& payload);
90     int GetGamePid(const std::string &gameMsg) const;
91     GameStatus GetGameScene(const std::string &gameMsg) const;
92     void NewForegroundAppRecord(int pid, int uiTid, bool ddlEnabled);
93 
94     std::mutex appInfoLock_;
95     std::mutex rateInfoLock_;
96     std::mutex executorStartLock_;
97     std::mutex ddlPowerModeLock_;
98     std::mutex configReaderMutex_;
99     std::list<ForegroundAppRecord> foregroundApp_ = {};
100     std::list<int> rsThreads_ = {};
101     std::unordered_map<std::string, int> msgType_ = {};
102     QosPolicy qosPolicy_;
103     std::vector<int> authApps_;
104     int renderServiceMainGrpId_ = -1;
105     int renderServiceRenderGrpId_ = -1;
106     int renderServiceMainTid_ = -1;
107     int renderServiceRenderTid_ = -1;
108     int hardwareGrpId_ = -1;
109     int hardwareTid_ = -1;
110     int systemRate_ = 0;
111     int uniAppRate_ = 0;
112     bool rtgEnabled_ = false;
113     bool configEnable_ = false;
114     int authedRSPid_ = 0;
115     bool ddlSceneSchedSwitch_ = false;
116     bool ddlPowerModeEnable_ = false;
117     bool isVideoApp_ = false;
118     std::atomic<int> curGamePid_ = -1;
119     int executorNum_ = 0;
120     std::map<int, std::string> appBundleName;
121     std::unique_ptr<ConfigReader> configReader_ = nullptr;
122     std::unordered_map<std::string, int> appTypeCache_;
123 
124     const std::string RENDER_SERVICE_PROCESS_NAME = "render_service";
125     const std::string RESOURCE_SCHEDULE_PROCESS_NAME = "resource_schedule_service";
126     const std::string GAME_ACCELERATE_SCHED_PROCESS_NAME = "game_accelerate_schedule";
127 };
128 
129 class ForegroundAppRecord {
130 public:
131     explicit ForegroundAppRecord(int pid, int uiTid, bool createGrp = true);
132     ~ForegroundAppRecord();
133 
134     void AddKeyThread(int tid, int prio = PRIO_NORMAL);
135     bool BeginScene();
136     bool EndScene();
137     int GetPid() const;
138     int GetGrpId() const;
139     int GetRate() const;
140     void SetRate(int appRate);
141     int GetUiTid() const;
142     void SetUiTid(int uiTid);
143     void SetGrpId(int grpId);
144     bool IsValid();
145     void PrintKeyThreads();
146 
147 private:
148     int pid_ = 0;
149     int grpId_ = 0;
150     int rate_ = 0;
151     int uiTid_ = 0;
152     std::unordered_set<int> keyThreads_;
153 };
154 } // namespace ConcurrentTask
155 } // namespace OHOS
156 
157 #endif // CONCURRENT_TASK_SERVICES_CONCURRENTSEVICE_INCLUDE_CONCURRENT_TASK_CONTROLLER_H
158