1 /*
2 * Copyright (c) 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 #include "res_sched_util.h"
17
18 #include <string>
19
20 #include "ability_info.h"
21 #include "ui_extension_utils.h"
22 #include "hilog_tag_wrapper.h"
23 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
24 #include "res_sched_client.h"
25 #include "res_type.h"
26 #endif
27
28 namespace OHOS {
29 namespace AAFwk {
30 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
31 using AssociatedStartType = ResourceSchedule::ResType::AssociatedStartType;
32 static constexpr int PERF_NUM = 10202;
33 #endif
GetInstance()34 ResSchedUtil &ResSchedUtil::GetInstance()
35 {
36 static ResSchedUtil instance;
37 return instance;
38 }
39
convertType(int64_t resSchedType)40 int64_t ResSchedUtil::convertType(int64_t resSchedType)
41 {
42 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
43 if (resSchedType == RES_TYPE_SCB_START_ABILITY) {
44 return static_cast<int64_t>(AssociatedStartType::SCB_START_ABILITY);
45 } else if (resSchedType == RES_TYPE_EXTENSION_START_ABILITY) {
46 return static_cast<int64_t>(AssociatedStartType::EXTENSION_START_ABILITY);
47 } else if (resSchedType == RES_TYPE_MISSION_LIST_START_ABILITY) {
48 return static_cast<int64_t>(AssociatedStartType::MISSION_LIST_START_ABILITY);
49 }
50 #endif
51 TAG_LOGE(AAFwkTag::DEFAULT, "sched invalid");
52 return -1;
53 }
54
ReportSubHealtyPerfInfoToRSS()55 void ResSchedUtil::ReportSubHealtyPerfInfoToRSS()
56 {
57 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
58 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_ANCO_CUST;
59 std::unordered_map<std::string, std::string> eventParams{{"name", "soc_perf"}};
60 TAG_LOGI(AAFwkTag::DEFAULT, "soc_perf start");
61 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, PERF_NUM, eventParams);
62 #endif
63 }
64
ReportAbilityStartInfoToRSS(const AbilityInfo & abilityInfo,int32_t pid,bool isColdStart,bool supportWarmSmartGC)65 void ResSchedUtil::ReportAbilityStartInfoToRSS(const AbilityInfo &abilityInfo, int32_t pid, bool isColdStart,
66 bool supportWarmSmartGC)
67 {
68 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
69 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_APP_ABILITY_START;
70 std::unordered_map<std::string, std::string> eventParams {
71 { "name", "ability_start" },
72 { "uid", std::to_string(abilityInfo.applicationInfo.uid) },
73 { "bundleName", abilityInfo.applicationInfo.bundleName },
74 { "abilityName", abilityInfo.name },
75 { "pid", std::to_string(pid) },
76 { "supportWarmSmartGC", supportWarmSmartGC ? "1": "0" }
77 };
78 TAG_LOGD(AAFwkTag::DEFAULT, "call");
79 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, isColdStart ? 1 : 0, eventParams);
80 #endif
81 }
82
ReportAbilityAssociatedStartInfoToRSS(const AbilityInfo & abilityInfo,int64_t resSchedType,int32_t callerUid,int32_t callerPid)83 void ResSchedUtil::ReportAbilityAssociatedStartInfoToRSS(
84 const AbilityInfo &abilityInfo, int64_t resSchedType, int32_t callerUid, int32_t callerPid)
85 {
86 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
87 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_APP_ASSOCIATED_START;
88 std::unordered_map<std::string, std::string> eventParams {
89 { "name", "associated_start" },
90 { "caller_uid", std::to_string(callerUid) },
91 { "caller_pid", std::to_string(callerPid) },
92 { "callee_uid", std::to_string(abilityInfo.applicationInfo.uid) },
93 { "callee_bundle_name", abilityInfo.applicationInfo.bundleName }
94 };
95 int64_t type = convertType(resSchedType);
96 TAG_LOGD(AAFwkTag::DEFAULT, "call");
97 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
98 #endif
99 }
100
ReportPreloadApplicationToRSS(const std::shared_ptr<AbilityInfo> & abilityInfo,int32_t preloadMode)101 void ResSchedUtil::ReportPreloadApplicationToRSS(const std::shared_ptr<AbilityInfo>& abilityInfo, int32_t preloadMode)
102 {
103 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
104 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_PRELOAD_APPLICATION;
105 std::unordered_map<std::string, std::string> eventParams {
106 { "name", "preload_application" },
107 { "uid", std::to_string(abilityInfo->applicationInfo.uid) },
108 { "bundleName", abilityInfo->applicationInfo.bundleName },
109 { "preloadMode", std::to_string(preloadMode) }
110 };
111 TAG_LOGD(AAFwkTag::DEFAULT, "call");
112 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, preloadMode, eventParams);
113 #endif
114 }
115
GetThawReasonByAbilityType(const AbilityInfo & abilityInfo)116 std::string ResSchedUtil::GetThawReasonByAbilityType(const AbilityInfo &abilityInfo)
117 {
118 std::string reason;
119 if (abilityInfo.type == AppExecFwk::AbilityType::PAGE) {
120 reason = "THAW_BY_START_PAGE_ABILITY";
121 } else if (abilityInfo.type == AppExecFwk::AbilityType::EXTENSION &&
122 abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE) {
123 reason = "THAW_BY_START_SERVICE_EXTENSION";
124 } else if (abilityInfo.type == AppExecFwk::AbilityType::EXTENSION &&
125 AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType)) {
126 reason = "THAW_BY_START_UI_EXTENSION";
127 } else {
128 reason = "THAW_BY_START_NOT_PAGE_ABILITY";
129 }
130 return reason;
131 }
132
ReportAbilityIntentExemptionInfoToRSS(int32_t callerUid,int32_t callerPid)133 void ResSchedUtil::ReportAbilityIntentExemptionInfoToRSS(int32_t callerUid, int32_t callerPid)
134 {
135 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
136 std::unordered_map<std::string, std::string> eventParams {
137 {"uid", std::to_string(callerUid)},
138 {"duration", std::to_string(INTENT_EXEMPTION_DURATION)}
139 };
140
141 TAG_LOGD(AAFwkTag::DEFAULT, "report Intent ExemptionInfo Uid:%{public}d", callerUid);
142 ResourceSchedule::ResSchedClient::GetInstance().ReportData(
143 ResourceSchedule::ResType::RES_TYPE_INTENT_CTRL_APP, 0, eventParams);
144 #endif
145 }
146
ReportEventToRSS(const int32_t uid,const std::string & bundleName,const std::string & reason,const int32_t pid,const int32_t callerPid)147 void ResSchedUtil::ReportEventToRSS(const int32_t uid, const std::string &bundleName, const std::string &reason,
148 const int32_t pid, const int32_t callerPid)
149 {
150 TAG_LOGD(AAFwkTag::DEFAULT, "ReportEventToRSS---%{public}d_%{public}s_%{public}d callerPid=%{public}d",
151 uid, bundleName.c_str(), pid, callerPid);
152 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
153 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_THAW_ONE_APP;
154 nlohmann::json payload;
155 payload.emplace("uid", uid);
156 payload.emplace("pid", pid);
157 payload.emplace("bundleName", bundleName);
158 payload.emplace("reason", reason);
159 payload.emplace("callerPid", callerPid);
160 nlohmann::json reply;
161 TAG_LOGD(AAFwkTag::DEFAULT, "call");
162 ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
163 #endif
164 }
165
GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> & frozenPids)166 void ResSchedUtil::GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> &frozenPids)
167 {
168 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
169 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_ALL_SUSPEND_STATE;
170 nlohmann::json payload;
171 nlohmann::json reply;
172 TAG_LOGD(AAFwkTag::DEFAULT, "call");
173 int32_t ret = ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
174 if (ret != 0 || !reply.contains("allSuspendState") || !reply["allSuspendState"].is_array()) {
175 TAG_LOGE(AAFwkTag::DEFAULT, "ReportSyncEvent fail");
176 return;
177 }
178
179 for (nlohmann::json &appObj : reply["allSuspendState"]) {
180 // Here can get uid if needed
181 if (!appObj.contains("pidsState") || !appObj["pidsState"].is_array()) {
182 continue;
183 }
184
185 for (nlohmann::json &pidObj : appObj["pidsState"]) {
186 if (!pidObj.contains("pid") || !pidObj["pid"].is_number() ||
187 !pidObj.contains("isFrozen") || !pidObj["isFrozen"].is_boolean()) {
188 break;
189 }
190 int32_t pid = pidObj["pid"].get<int32_t>();
191 bool isFrozen = pidObj["isFrozen"].get<bool>();
192 if (isFrozen) {
193 frozenPids.insert(pid);
194 }
195 }
196 }
197 if (frozenPids.empty()) {
198 TAG_LOGW(AAFwkTag::DEFAULT, "empty frozen pids");
199 }
200 #endif
201 }
202
CheckShouldForceKillProcess(int32_t pid,const std::string & bundleName)203 bool ResSchedUtil::CheckShouldForceKillProcess(int32_t pid, const std::string& bundleName)
204 {
205 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
206 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_SHOULD_FORCE_KILL_PROCESS;
207 nlohmann::json payload;
208 nlohmann::json reply;
209 payload.emplace("pid", pid);
210 ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
211 if (!reply.contains("ShouldForceKillProcess") || !reply["ShouldForceKillProcess"].is_number_integer()) {
212 return true;
213 }
214 return reply["ShouldForceKillProcess"].get<int32_t>() == 1;
215 #else
216 return true;
217 #endif
218 }
219
ReportLoadingEventToRss(LoadingStage stage,int32_t pid,int32_t uid,int64_t timeDuration,int64_t abilityRecordId)220 void ResSchedUtil::ReportLoadingEventToRss(LoadingStage stage, int32_t pid, int32_t uid,
221 int64_t timeDuration, int64_t abilityRecordId)
222 {
223 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
224 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_KEY_PERF_SCENE;
225 std::unordered_map<std::string, std::string> eventParams {
226 { "extType", "10015"},
227 { "pid", std::to_string(pid) },
228 { "uid", std::to_string(uid) },
229 };
230 if (timeDuration > 0) { // millisecond
231 eventParams.emplace("timeoutDuration", std::to_string(timeDuration));
232 }
233 if (abilityRecordId > 0) {
234 eventParams.emplace("abilityRecordId", std::to_string(abilityRecordId));
235 }
236
237 int64_t type = static_cast<int64_t>(stage);
238 TAG_LOGD(AAFwkTag::DEFAULT, "call");
239 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
240 #endif
241 }
242
GetNWebPreloadSet() const243 std::unordered_set<std::string> ResSchedUtil::GetNWebPreloadSet() const
244 {
245 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
246 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_NWEB_PRELOAD_SET;
247 nlohmann::json payload;
248 nlohmann::json reply;
249 ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
250 if (!reply.contains("NWebPreloadSet")) {
251 TAG_LOGW(AAFwkTag::DEFAULT, "does not get preload process set");
252 return {};
253 }
254 auto jsonObj = reply["NWebPreloadSet"];
255 return { jsonObj.begin(), jsonObj.end() };
256 #else
257 return {};
258 #endif
259 }
260 } // namespace AAFwk
261 } // namespace OHOS
262