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 using AssociatedStartType = ResourceSchedule::ResType::AssociatedStartType;
GetInstance()31 ResSchedUtil &ResSchedUtil::GetInstance()
32 {
33 static ResSchedUtil instance;
34 return instance;
35 }
36
convertType(int64_t resSchedType)37 int64_t ResSchedUtil::convertType(int64_t resSchedType)
38 {
39 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
40 if (resSchedType == RES_TYPE_SCB_START_ABILITY) {
41 return static_cast<int64_t>(AssociatedStartType::SCB_START_ABILITY);
42 } else if (resSchedType == RES_TYPE_EXTENSION_START_ABILITY) {
43 return static_cast<int64_t>(AssociatedStartType::EXTENSION_START_ABILITY);
44 } else if (resSchedType == RES_TYPE_MISSION_LIST_START_ABILITY) {
45 return static_cast<int64_t>(AssociatedStartType::MISSION_LIST_START_ABILITY);
46 }
47 #endif
48 TAG_LOGE(AAFwkTag::DEFAULT, "sched invalid");
49 return -1;
50 }
51
ReportAbilityStartInfoToRSS(const AbilityInfo & abilityInfo,int32_t pid,bool isColdStart)52 void ResSchedUtil::ReportAbilityStartInfoToRSS(const AbilityInfo &abilityInfo, int32_t pid, bool isColdStart)
53 {
54 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
55 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_APP_ABILITY_START;
56 std::unordered_map<std::string, std::string> eventParams {
57 { "name", "ability_start" },
58 { "uid", std::to_string(abilityInfo.applicationInfo.uid) },
59 { "bundleName", abilityInfo.applicationInfo.bundleName },
60 { "abilityName", abilityInfo.name },
61 { "pid", std::to_string(pid) }
62 };
63 TAG_LOGD(AAFwkTag::DEFAULT, "call");
64 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, isColdStart ? 1 : 0, eventParams);
65 #endif
66 }
67
ReportAbilityAssociatedStartInfoToRSS(const AbilityInfo & abilityInfo,int64_t resSchedType,int32_t callerUid,int32_t callerPid)68 void ResSchedUtil::ReportAbilityAssociatedStartInfoToRSS(
69 const AbilityInfo &abilityInfo, int64_t resSchedType, int32_t callerUid, int32_t callerPid)
70 {
71 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
72 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_APP_ASSOCIATED_START;
73 std::unordered_map<std::string, std::string> eventParams {
74 { "name", "associated_start" },
75 { "caller_uid", std::to_string(callerUid) },
76 { "caller_pid", std::to_string(callerPid) },
77 { "callee_uid", std::to_string(abilityInfo.applicationInfo.uid) },
78 { "callee_bundle_name", abilityInfo.applicationInfo.bundleName }
79 };
80 int64_t type = convertType(resSchedType);
81 TAG_LOGD(AAFwkTag::DEFAULT, "call");
82 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
83 #endif
84 }
85
GetThawReasonByAbilityType(const AbilityInfo & abilityInfo)86 std::string ResSchedUtil::GetThawReasonByAbilityType(const AbilityInfo &abilityInfo)
87 {
88 std::string reason;
89 if (abilityInfo.type == AppExecFwk::AbilityType::PAGE) {
90 reason = "THAW_BY_START_PAGE_ABILITY";
91 } else if (abilityInfo.type == AppExecFwk::AbilityType::EXTENSION &&
92 abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::SERVICE) {
93 reason = "THAW_BY_START_SERVICE_EXTENSION";
94 } else if (abilityInfo.type == AppExecFwk::AbilityType::EXTENSION &&
95 AAFwk::UIExtensionUtils::IsUIExtension(abilityInfo.extensionAbilityType)) {
96 reason = "THAW_BY_START_UI_EXTENSION";
97 } else {
98 reason = "THAW_BY_START_NOT_PAGE_ABILITY";
99 }
100 return reason;
101 }
102
NeedReportByPidWhenConnect(const AbilityInfo & abilityInfo)103 bool ResSchedUtil::NeedReportByPidWhenConnect(const AbilityInfo &abilityInfo)
104 {
105 return abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::REMOTE_NOTIFICATION ||
106 abilityInfo.extensionAbilityType == AppExecFwk::ExtensionAbilityType::BACKUP;
107 }
108
ReportEventToRSS(const int32_t uid,const std::string & bundleName,const std::string & reason,const int32_t pid,const int32_t callerPid)109 void ResSchedUtil::ReportEventToRSS(const int32_t uid, const std::string &bundleName, const std::string &reason,
110 const int32_t pid, const int32_t callerPid)
111 {
112 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
113 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_THAW_ONE_APP;
114 nlohmann::json payload;
115 payload.emplace("uid", uid);
116 payload.emplace("pid", pid);
117 payload.emplace("bundleName", bundleName);
118 payload.emplace("reason", reason);
119 payload.emplace("callerPid", callerPid);
120 nlohmann::json reply;
121 TAG_LOGD(AAFwkTag::DEFAULT, "call");
122 ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
123 #endif
124 }
125
GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> & frozenPids)126 void ResSchedUtil::GetAllFrozenPidsFromRSS(std::unordered_set<int32_t> &frozenPids)
127 {
128 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
129 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_ALL_SUSPEND_STATE;
130 nlohmann::json payload;
131 nlohmann::json reply;
132 TAG_LOGD(AAFwkTag::DEFAULT, "call");
133 int32_t ret = ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
134 if (ret != 0 || !reply.contains("allSuspendState") || !reply["allSuspendState"].is_array()) {
135 TAG_LOGE(AAFwkTag::DEFAULT, "ReportSyncEvent fail");
136 return;
137 }
138
139 for (nlohmann::json &appObj : reply["allSuspendState"]) {
140 // Here can get uid if needed
141 if (!appObj.contains("pidsState") || !appObj["pidsState"].is_array()) {
142 continue;
143 }
144
145 for (nlohmann::json &pidObj : appObj["pidsState"]) {
146 if (!pidObj.contains("pid") || !pidObj["pid"].is_number() ||
147 !pidObj.contains("isFrozen") || !pidObj["isFrozen"].is_boolean()) {
148 break;
149 }
150 int32_t pid = pidObj["pid"].get<int32_t>();
151 bool isFrozen = pidObj["isFrozen"].get<bool>();
152 if (isFrozen) {
153 frozenPids.insert(pid);
154 }
155 }
156 }
157 if (frozenPids.empty()) {
158 TAG_LOGW(AAFwkTag::DEFAULT, "Get frozen pids empty");
159 }
160 #endif
161 }
162
CheckShouldForceKillProcess(int32_t pid)163 bool ResSchedUtil::CheckShouldForceKillProcess(int32_t pid)
164 {
165 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
166 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_SHOULD_FORCE_KILL_PROCESS;
167 nlohmann::json payload;
168 nlohmann::json reply;
169 payload.emplace("pid", pid);
170 ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
171 if (!reply.contains("ShouldForceKillProcess") || !reply["ShouldForceKillProcess"].is_number_integer()) {
172 return true;
173 }
174 return reply["ShouldForceKillProcess"].get<int32_t>() == 1;
175 #else
176 return true;
177 #endif
178 }
179
ReportLoadingEventToRss(LoadingStage stage,int32_t pid,int32_t uid,int64_t timeDuration,int64_t abilityRecordId)180 void ResSchedUtil::ReportLoadingEventToRss(LoadingStage stage, int32_t pid, int32_t uid,
181 int64_t timeDuration, int64_t abilityRecordId)
182 {
183 #ifdef RESOURCE_SCHEDULE_SERVICE_ENABLE
184 uint32_t resType = ResourceSchedule::ResType::RES_TYPE_KEY_PERF_SCENE;
185 std::unordered_map<std::string, std::string> eventParams {
186 { "extType", "10015"},
187 { "pid", std::to_string(pid) },
188 { "uid", std::to_string(uid) },
189 };
190 if (timeDuration > 0) { // millisecond
191 eventParams.emplace("timeoutDuration", std::to_string(timeDuration));
192 }
193 if (abilityRecordId > 0) {
194 eventParams.emplace("abilityRecordId", std::to_string(abilityRecordId));
195 }
196
197 int64_t type = static_cast<int64_t>(stage);
198 TAG_LOGD(AAFwkTag::DEFAULT, "call");
199 ResourceSchedule::ResSchedClient::GetInstance().ReportData(resType, type, eventParams);
200 #endif
201 }
202
GetNWebPreloadSet() const203 std::unordered_set<std::string> ResSchedUtil::GetNWebPreloadSet() const
204 {
205 uint32_t resType = ResourceSchedule::ResType::SYNC_RES_TYPE_GET_NWEB_PRELOAD_SET;
206 nlohmann::json payload;
207 nlohmann::json reply;
208 ResourceSchedule::ResSchedClient::GetInstance().ReportSyncEvent(resType, 0, payload, reply);
209 if (!reply.contains("NWebPreloadSet")) {
210 TAG_LOGW(AAFwkTag::DEFAULT, "does not get preload process set");
211 return {};
212 }
213 auto jsonObj = reply["NWebPreloadSet"];
214 return { jsonObj.begin(), jsonObj.end() };
215 }
216 } // namespace AAFwk
217 } // namespace OHOS