• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2025 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 FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_EFFICIENCY_RESOURCES_INCLUDE_EFFICIENCY_RES_MGR_H
17 #define FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_EFFICIENCY_RESOURCES_INCLUDE_EFFICIENCY_RES_MGR_H
18 
19 #include <memory>
20 #include <list>
21 #include <unordered_map>
22 
23 #include "ipc_skeleton.h"
24 #include "singleton.h"
25 #include "event_runner.h"
26 #include "event_handler.h"
27 #include "event_info.h"
28 #include "running_process_info.h"
29 #include "app_mgr_client.h"
30 
31 #include "efficiency_resource_info.h"
32 #include "ibackground_task_subscriber.h"
33 #include "resource_callback_info.h"
34 #include "bundle_manager_helper.h"
35 #include "resource_application_record.h"
36 #include "resources_subscriber_mgr.h"
37 #include "app_state_observer.h"
38 #include "data_storage_helper.h"
39 #include "report_hisysevent_data.h"
40 
41 namespace OHOS {
42 namespace BackgroundTaskMgr {
43 
44 enum class EfficiencyResourceEventTriggerType: uint32_t {
45     EFFICIENCY_RESOURCE_APPLY,
46     EFFICIENCY_RESOURCE_RESET,
47 };
48 
49 enum class CancelReason: uint32_t {
50     DEFAULT,
51     DUMPER,
52     APPLY_INTERFACE,
53     RESET_INTERFACE,
54     APP_DIED,
55     APP_DIDE_TRIGGER_PROCESS_DIED,
56     PROCESS_DIED,
57 };
58 
59 class DataStorageHelper;
60 class BgEfficiencyResourcesMgr : public DelayedSingleton<BgEfficiencyResourcesMgr>,
61                             public std::enable_shared_from_this<BgEfficiencyResourcesMgr> {
62 using ResourceRecordMap = std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>>;
63 using ResourceRecordPair = std::pair<const int32_t, std::shared_ptr<
64     OHOS::BackgroundTaskMgr::ResourceApplicationRecord>>;
65 public:
66     ErrCode ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
67     bool Init(const std::shared_ptr<AppExecFwk::EventRunner>& runner);
68     void InitNecessaryState();
69     void Clear();
70     ErrCode ApplyEfficiencyResources(const sptr<EfficiencyResourceInfo> &resourceInfo);
71     ErrCode ResetAllEfficiencyResources();
72     ErrCode GetAllEfficiencyResources(std::vector<std::shared_ptr<EfficiencyResourceInfo>> &resourceInfoList);
73     ErrCode GetEfficiencyResourcesInfos(std::vector<std::shared_ptr<ResourceCallbackInfo>> &appList,
74         std::vector<std::shared_ptr<ResourceCallbackInfo>> &procList);
75     ErrCode AddSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
76     ErrCode RemoveSubscriber(const sptr<IBackgroundTaskSubscriber> &subscriber);
77     ErrCode RemoveProcessRecord(int32_t uid, int32_t pid, const std::string &bundleName);
78     ErrCode RemoveAppRecord(int32_t uid, const std::string &bundleName, bool resetAll);
79     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
80     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId);
81 
82 private:
83     void ApplyEfficiencyResourcesInner(std::shared_ptr<ResourceCallbackInfo> callbackInfo,
84         const sptr<EfficiencyResourceInfo> &resourceInfo);
85     void UpdateResourcesEndtime(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo,
86         std::shared_ptr<ResourceApplicationRecord> &record, const sptr<EfficiencyResourceInfo> &resourceInfo);
87     void ResetEfficiencyResourcesInner(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo,
88         bool isProcess, CancelReason cancelType = CancelReason::DEFAULT);
89     ErrCode ResetAllEfficiencyResourcesInner(const std::shared_ptr<ResourceCallbackInfo> &callbackInfo,
90         bool isProcess);
91     void RemoveRelativeProcessRecord(int32_t uid, uint32_t resourceNumber);
92     ErrCode ShellDumpInner(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
93     void DumpAllApplicationInfo(std::vector<std::string> &dumpInfo);
94     void DumpResetAllResource(const std::vector<std::string> &dumpOption);
95     void DumpResetResource(const std::vector<std::string> &dumpOption, bool cleanApp, bool cleanAll);
96     void DumpApplicationInfoMap(std::unordered_map<int32_t,
97         std::shared_ptr<ResourceApplicationRecord>> &infoMap, std::vector<std::string> &dumpInfo,
98         std::stringstream &stream, const char *headInfo);
99     void ResetTimeOutResource(int32_t mapKey, bool isProcess);
100     bool RemoveTargetResourceRecord(std::unordered_map<int32_t,
101         std::shared_ptr<ResourceApplicationRecord>> &infoMap, int32_t mapKey,
102         uint32_t cleanResource, EfficiencyResourcesEventType type, CancelReason cancelType);
103     bool GetBundleNamesForUid(int32_t uid, std::string &bundleName);
104     bool IsCallingInfoLegal(int32_t uid, int32_t pid, std::string &bundleName);
105     void EraseRecordIf(ResourceRecordMap &infoMap, const std::function<bool(ResourceRecordPair)> &fun);
106     void RecoverDelayedTask(bool isProcess, ResourceRecordMap& infoMap);
107     void HandlePersistenceData();
108     void CheckPersistenceData(const std::vector<AppExecFwk::RunningProcessInfo> &allProcesses);
109     void RemoveListRecord(std::list<PersistTime> &resourceUnitList, uint32_t eraseBit);
110     void GetEfficiencyResourcesInfosInner(const ResourceRecordMap &infoMap,
111         std::vector<std::shared_ptr<ResourceCallbackInfo>> &list);
112     void GetAllEfficiencyResourcesInner(const ResourceRecordMap &infoMap,
113         std::vector<std::shared_ptr<EfficiencyResourceInfo>> &resourceInfoList, const int32_t uid, const int32_t pid,
114         const bool isProcess);
115     uint32_t GetExemptedResourceType(uint32_t resourceNumber, const int32_t uid, const std::string &bundleName);
116     std::vector<int32_t> QueryRunningResourcesApply(const int32_t uid, const std::string &bundleName);
117     bool CheckAlivedApp(int32_t uid);
118     int32_t GetUserIdByUid(int32_t uid);
119     void ApplyResourceForPkgAndProc(int32_t uid, int32_t pid, const std::string &bundleName,
120         const sptr<EfficiencyResourceInfo> &resourceInfo);
121     void SendResourceApplyTask(int32_t uid, int32_t pid, const std::string &bundleName,
122         const sptr<EfficiencyResourceInfo> &resourceInfo);
123     bool IsServiceExtensionType(const pid_t pid);
124     void LoadResourceQuotaMgrLib();
125     bool CheckOrUpdateCpuApplyQuota(int32_t uid, const std::string &bundleName,
126         const sptr<EfficiencyResourceInfo> &resourceInfo);
127     void UpdateQuotaIfCpuReset(EfficiencyResourcesEventType type, int32_t uid, uint32_t resourceNumber);
128     void DumpSetCpuQuota(const std::vector<std::string> &dumpOption);
129     void DumpResetCpuQuotaUsage(const std::vector<std::string> &dumpOption);
130     void DumpGetCpuQuota(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
131     void ReportHisysEvent(EfficiencyResourceEventTriggerType operationType,
132         const sptr<EfficiencyResourceInfo> &resourceInfo,
133         const std::shared_ptr<ResourceCallbackInfo> &callbackInfo,
134         EfficiencyResourcesEventType type);
135 
136 private:
137     std::atomic<bool> isSysReady_ {false};
138     std::mutex sysAbilityLock_;
139     std::shared_ptr<AppExecFwk::EventHandler> handler_ {nullptr};
140     std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>> appResourceApplyMap_ {};
141     std::unordered_map<int32_t, std::shared_ptr<ResourceApplicationRecord>> procResourceApplyMap_ {};
142     std::shared_ptr<AppStateObserver> appStateObserver_ {nullptr};
143     std::shared_ptr<ResourcesSubscriberMgr> subscriberMgr_ {nullptr};
144     std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_ {nullptr};
145     uint32_t dependsReady_ = 0;
146     void *resourceQuotaMgrHandle_ {nullptr};
147     EfficiencyResourceApplyReportHisysEvent applyEventData_;
148     EfficiencyResourceResetReportHisysEvent resetEventData_;
149 
150     DECLARE_DELAYED_SINGLETON(BgEfficiencyResourcesMgr);
151 };
152 }  // namespace BackgroundTaskMgr
153 }  // namespace OHOS
154 #endif  // FOUNDATION_RESOURCESCHEDULE_BACKGROUND_TASK_MGR_SERVICES_EFFICIENCY_RESOURCES_INCLUDE_EFFICIENCY_RES_MGR_H