1# Resource Schedule Service 2 3- [Resource Schedule Service](#resource-schedule-service) 4 - [Introduction<a name="section11660541593"></a>](#introduction) 5 - [Directory Structure<a name="section161941989596"></a>](#directory-structure) 6 - [How to write a plugin<a name="section1312121216216"></a>](#how-to-write-a-plugin) 7 - [Available APIs<a name="section114564657874"></a>](#available-apis) 8 - [Usage Guidelines<a name="section129654513264"></a>](#usage-guidelines) 9 - [Restrictions on Using Transient Tasks<a name="section1551164914237"></a>](#restrictions-on-using-transient-tasks) 10 - [Cgroup Schedule Policy](#cgroup-schedule-policy) 11 - [Basic Policy](#basic-policy) 12 - [Policy Configuration](#policy-configuration) 13 - [Restrictions](#restrictions) 14 - [SocPerf](#socperf) 15 - [Interfaces](#interfaces) 16 - [Configs](#configs) 17 - [Repositories Involved<a name="section1371113476307"></a>](#repositories-involved) 18 19## Introduction<a name="section11660541593"></a> 20 21In the resourceschedule subsystem, it provides the awareness and distribution of system events, such as application start, exit, screen on and off, etc. 22If you need to obtain system events and perform related resource schedule, you can choose to join the resource schedule service in the form of a plugin. 23 24In resource schedule subsystem, the module of cgroup schedule decides the group schedule policy of each process by analyzing application state, window status and background task status. And with proper configuration, it can bind each process to certain cgroup node. These schedule policies can be used to gain better performance. And this module forward different kinds of events to plugin manager and then be distributed to subscribers. 25 26Resource_schedule_service is an engine that receives events, decides schedule policies and executes schedule mechanisms. It's architecture is shown as follows: 27  28It consists of the following important parts: 291. Event manager, which includes the function of using external interfaces to sense system events, as well as the function of using listening forms to sense system events. 302. Application intelligent grouping, which receives the event of application life cycle change and decides the priority of application grouping, is the fundamental basis for global resource schedule. 313. Plugin manager, which is responsible for loading the resource schedule plugin corresponding to the product, receiving system and application events, and distributing events to the plugin according to the plugin subscription. 324. Soc_perf service, which receives frequency modulation events from the related plugin, arbitration the frequency value, and finally uses the kernel interface to set the CPU frequency. 33 34Resource_schedule_service policy is mainly used to extend and schedule the system's global resources with related plugins. Plugins run in the form of dynamic-link, and different users can choose different plugin combinations. At present, the existing plugins include: intelligent perception schedule plugin, device's status management plugin, and soc_perf plugin.The soc_perf plugin is implemented in resource_schedule_service code repository, while the other two plugins are implemented in other code repositories. However, system events are used as the basis for setting schedule policies to the system kernel. 35 36## Directory Structure<a name="section161941989596"></a> 37 38``` 39</foundation/resourceschedule/resource_schedule_service> 40├── cgroup_sched 41| ├── common 42| │ └── include # common header files 43| ├── framework 44| │ ├── process_group # utils to set cgroup by pid 45| │ ├── sched_controller # cgroup schedule 46| │ └── utils # adaption interface to forwarding data 47| │ ├── include 48| │ │ └── ressched_utils.h # event report header files 49| │ └── ressched_utils.cpp # event report interface 50| ├── interfaces # Init/Denit/Query interface for rss 51| │ └── innerkits # Interface APIs 52| └── profiles # config files 53└── ressched 54| ├── common # Common header file 55| ├── interfaces 56| │ └── innerkits # Interface APIs 57| │ └── ressched_client # Report data in process 58| ├── plugins # Plugin code 59| ├── profile # Plugin switch xml and plugin private xml 60| ├── sa_profile # System ability xml 61| ├── sched_controller # event receive 62| | ├── common_event # event receive common interface 63| | └── observer # callback event receive 64| | ├── audio_observer # audio framework event callback 65| | ├── camera_observer # camera event callback 66| | └── telephony_observer # telephony event callback 67| └── services 68| ├── resschedmgr 69| │ ├── pluginbase # Plugin struct definition 70| │ └── resschedfwk # Resource schedule framework 71| └── resschedservice # Resource schedule service 72└── soc_perf 73 ├── configs # socperf config file 74 ├── include 75 | ├── client # socperf client header files 76 | ├── server # socperf server header files 77 | └── server # socperf core code header files 78 ├── src 79 | ├── client # socperf Client interfaces 80 | ├── server # socperf Server codes 81 | └── server # core code, arbitrate and take effects 82 └── sa_profile # System ability xml 83 84``` 85## How to write a plugin<a name="section1312121216216"></a> 86 87### Available APIs<a name="section114564657874"></a> 88 89| API | Description | 90|-------------------------------------------------------------------------------|----------------------------------| 91| function OnPluginInit(std::string& libName): bool; | plugin init | 92| function OnPluginDisable(): void; | plugin disable | 93| function OnDispatchResource(const std::shared_ptr\<ResData\>& data):void; | dispatch resource event | 94 95### Usage Guidelines<a name="section129654513264"></a> 96 97When the plugin is initialized, specify the events that need to be registered for monitoring. When these events occur, the framework will be distributed to each plugin in turn, 98 99At this point, the plugin needs to quickly process message reception for resource schedule (if time-consuming tasks need to be processed by another thread), and the processing is completed, return. 100 101#### Restrictions on Using Transient Tasks<a name="section1551164914237"></a> 102 1031. The plugin can be implemented with C/C++. 104 1052. The event processing of the plugin must be completed quickly. If it exceeds 1ms, warning will be printed. 106If it exceeds 10ms, the framework thinks the plugin is abnormal and reports an error. 107 108## Cgroup Schedule Policy 109 110### Basic Policy 111 112| Scene | Schedule policy | 113|----------|-------| 114| Currently focused oreground application and processes with same uid | top_app | 115| Foreground visible processes, include unfocused window process in split mode, float window process, foundation process | foreground | 116| system level daemon processes. Assign system schedule policy to them when started, | system | 117| Background application and processes with same uid | background | 118| kernel processes, most native processes and others have not mensioned | root | 119 120### Policy Configuration 121 122Each schedule policy is configured in a json file, and is bound to different cgroup. 123``` 124 "Cgroups": [ 125 { 126 "controller": "cpu", 127 "path": "/dev/cpuctl", 128 "sched_policy": { 129 "sp_default": "", 130 "sp_background": "background", 131 "sp_foreground": "foreground", 132 "sp_system_background": "system-background", 133 "sp_top_app": "top-app" 134 } 135 }, 136 { 137 "controller": "cpuset", 138 "path": "/dev/cpuset", 139 "sched_policy": { 140 "sp_default": "", 141 "sp_background": "background", 142 "sp_foreground": "foreground", 143 "sp_system_background": "system-background", 144 "sp_top_app": "top-app" 145 } 146 } 147 ] 148``` 149 150### Restrictions 151 152Configuration file: cgroup_action_config.json 153Every schedule policy defined should be configured. 154 155Introduction to each config item: 156 157| Item | Description | 158|--------|--------| 159|controller|cgroup controller: cpuset, cpuctl, blkio etc.| 160|path|Absolute path of current cgroup| 161|sched_policy|Binding between each schedule policy and cgroup| 162|sp_xx|Different kinds of schedule policy| 163 164## SocPerf 165 166### Interfaces 167 168Supported SocPerf interfaces description 169 170| Interface | Description | 171|----------|-------| 172| PerfRequest(int32_t cmdId, const std::string& msg) | Used for Performace boost freq | 173| PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg) | Used for Performace boost freq and support ON/OFF | 174| PowerLimitBoost(bool onOffTag, const std::string& msg) | Used for Power limit freq which cannot be boosted | 175| ThermalLimitBoost(bool onOffTag, const std::string& msg) | Used for Thermal limit freq which cannot be boosted | 176| LimitRequest(int32_t clientId, const std::vector<int32_t>& tags, const std::vector<int64_t>& configs, const std::string& msg) | Used for Power or Thermal limit freq and multiple freq items can be set together | 177 178All interfaces are based on the key parameter cmdID, cmdID connects scenes and configs, which is used to boost freq or limit freq. 179Interface with parameter onOffTag means it support ON/OFF event. Normally, these interfaces are used for long-term event, 180which needs user to turn on or turn off manually. 181Parameter msg is used for extra information, like client's pid and tid. 182 183### Configs 184 185Config files description 186 187| File | Description | 188|----------|-------| 189| socperf_resource_config.xml | Define resource which can be modify,such as CPU/GPU/DDR/NPU | 190| socperf_boost_config.xml | Config file used for Performace boost | 191 192All xml files are different for particular products. 193For specific product, all resources which could be modify are defined in socperf_resource_config.xml. Each resource has its own resID. 194The cmdID in the socperf_boost_config.xml/socperf_resource_config.xml/socperf_thermal_config.xml must be different. 195 196## callback event receive 197Now support audio frameWork event、 telephony event、camera event callback 198 199audio frameWork contains renserState change、 ringMode change、 volumeKey change 200 201telphony change 202 203camera change 204 205## Repositories Involved<a name="section1371113476307"></a> 206- [windowmanager](https://gitee.com/openharmony/windowmanager) 207- [communication_ipc](https://gitee.com/openharmony/communication_ipc) 208- [hiviewdfx_hilog](https://gitee.com/openharmony/hiviewdfx_hilog) 209