1 /* 2 * Copyright (c) 2022-2024 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 RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_RES_SCHED_MGR_H 17 #define RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_RES_SCHED_MGR_H 18 19 #include <sys/types.h> 20 #include <string> 21 #include <unordered_set> 22 #include "event_handler.h" 23 #include "kill_process.h" 24 #include "single_instance.h" 25 #include "nlohmann/json.hpp" 26 27 namespace OHOS { 28 namespace ResourceSchedule { 29 class ResSchedMgr { 30 DECLARE_SINGLE_INSTANCE(ResSchedMgr); 31 32 public: 33 /** 34 * Init resource schedule manager. 35 */ 36 void Init(); 37 38 /** 39 * Stop resource schedule manager. 40 */ 41 void Stop(); 42 43 /** 44 * Report data from other modules, will report resource data async. 45 * 46 * @param resType Resource type 47 * @param value bit64 content. 48 * @param payload Extra content. 49 */ 50 void ReportData(uint32_t resType, int64_t value = 0, const nlohmann::json& payload = nullptr); 51 52 /** 53 * Report data inner, will report resource data async. 54 * 55 * @param resType Resource type. 56 * @param value bit64 content. 57 * @param payload Extra content. 58 */ 59 void ReportDataInner(uint32_t resType, int64_t value = 0, const nlohmann::json& payload = nullptr); 60 61 /** 62 * Kill process by pid. 63 * 64 * @param payload process message 65 */ 66 int32_t KillProcessByClient(const nlohmann::json& payload = nullptr); 67 68 /** 69 * Init resource_schedule_executor plugin manager. 70 * 71 * @param isProcessInit is process init 72 */ 73 void InitExecutorPlugin(bool isProcessInit = false); 74 75 /** 76 * Init foreground app. 77 * 78 */ 79 void InitForegroundAppInfo(); 80 81 /** 82 * process app state change. 83 * 84 * @param state the app's state 85 * @param pid the app's pid 86 */ 87 void OnApplicationStateChange(int32_t state, int32_t pid); 88 89 /** 90 * ReportAppStateInProcess. 91 * 92 * @param state the app's state 93 * @param pid the app's pid 94 */ 95 void ReportAppStateInProcess(int32_t state, int32_t pid); 96 97 /** 98 * ReportProcessStateInProcess. 99 * 100 * @param state the Process's state 101 * @param pid the Process's pid 102 */ 103 void ReportProcessStateInProcess(int32_t state, int32_t pid); 104 105 /** 106 * judge app is foreground. 107 * 108 * @param pid the app's pid 109 * @return true if the app is foreground 110 */ 111 bool IsForegroundApp(int32_t pid); 112 113 /** 114 * Get allow sceneboard report ext resTypes. 115 * 116 * @return the set of allow sceneboard report extension resTypes. 117 */ 118 std::unordered_set<uint32_t>& GetAllowSCBReportResExt(); 119 120 /** 121 * Get allow all sa report extension resTypes. 122 * 123 * @return the set of allow all sa report extension resTypes. 124 */ 125 std::unordered_set<uint32_t>& GetAllowAllSAReportResExt(); 126 127 /** 128 * Get allow some sa report extension resTypes. 129 * 130 * @return the set of allow some sa report extension resTypes. 131 */ 132 std::unordered_map<uint32_t, std::unordered_set<int32_t>>& GetAllowSomeSAReportResExt(); 133 134 /** 135 * Get allow all app report extension resTypes. 136 * 137 * @return the set of allow all app report extension resTypes. 138 */ 139 std::unordered_set<uint32_t>& GetAllowAllAppReportResExt(); 140 141 /** 142 * Get allow foreground app report extension resTypes. 143 * 144 * @return the set of allow foreground app report extension resTypes. 145 */ 146 std::unordered_set<uint32_t>& GetAllowFgAppReportResExt(); 147 148 /** 149 * Set allow sceneboard report ext resTypes. 150 * 151 * @param allowSCBReportResExt the set of allow sceneboard report extension resTypes. 152 */ 153 void SetAllowSCBReportResExt(const std::unordered_set<uint32_t>& allowSCBReportResExt); 154 155 /** 156 * Set allow all sa report extension resTypes. 157 * 158 * @param allowAllSAReportResExt the set of allow all sa report extension resTypes. 159 */ 160 void SetAllowAllSAReportResExt(const std::unordered_set<uint32_t>& allowAllSAReportResExt); 161 162 /** 163 * Set allow some sa report extension resTypes. 164 * 165 * @param allowSomeSAReportResExt the set of allow some sa report extension resTypes. 166 */ 167 void SetAllowSomeSAReportResExt(const std::unordered_map<uint32_t, std::unordered_set<int32_t>>& 168 allowSomeSAReportResExt); 169 170 /** 171 * Set allow all app report extension resTypes. 172 * 173 * @param allowAllAppReportResExt the set of allow all app report extension resTypes. 174 */ 175 void SetAllowAllAppReportResExt(const std::unordered_set<uint32_t>& allowAllAppReportResExt); 176 177 /** 178 * Set allow foreground app report extension resTypes. 179 * 180 * @param allowFgAppReportResExt the set of allow foreground app report extension resTypes. 181 */ 182 void SetAllowFgAppReportResExt(const std::unordered_set<uint32_t>& allowFgAppReportResExt); 183 private: 184 std::shared_ptr<KillProcess> killProcess_ = nullptr; 185 std::mutex foregroundPidsMutex_; 186 std::unordered_set<int32_t> foregroundPids_; 187 std::unordered_set<uint32_t> allowSCBReportResExt_; 188 std::unordered_set<uint32_t> allowAllSAReportResExt_; 189 std::unordered_map<uint32_t, std::unordered_set<int32_t>> allowSomeSAReportResExt_; 190 std::unordered_set<uint32_t> allowAllAppReportResExt_; 191 std::unordered_set<uint32_t> allowFgAppReportResExt_; 192 }; 193 } // namespace ResourceSchedule 194 } // namespace OHOS 195 196 #endif // RESSCHED_SERVICES_RESSCHEDMGR_RESSCHEDFWK_INCLUDE_RES_SCHED_MGR_H 197