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 ![img](figures/resource_schedule_service_architecture.png)
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    ├── common                        # Common header file
74    ├── interfaces                    #
75    │   └── inner_api                 # Interface APIs
76    │       └── socperf_client        # SocPerf Client interfaces
77    ├── profile                       # SocPerf config file
78    ├── sa_profile                    # System ability xml
79    ├── services
80        ├── core                      # SocPerf core code, arbitrate and take effects
81        └── server                    # SocPerf server codes
82
83```
84## How to write a plugin<a name="section1312121216216"></a>
85
86### Available APIs<a name="section114564657874"></a>
87
88| API                                                                           | Description                      |
89|-------------------------------------------------------------------------------|----------------------------------|
90| function OnPluginInit(std::string& libName): bool;                            | plugin init                      |
91| function OnPluginDisable(): void;                                             | plugin disable                   |
92| function OnDispatchResource(const std::shared_ptr\<ResData\>& data):void;       | dispatch resource event          |
93
94### Usage Guidelines<a name="section129654513264"></a>
95
96When 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,
97
98At 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.
99
100#### Restrictions on Using Transient Tasks<a name="section1551164914237"></a>
101
1021. The plugin can be implemented with C/C++.
103
1042. The event processing of the plugin must be completed quickly. If it exceeds 1ms, warning will be printed.
105If it exceeds 10ms, the framework thinks the plugin is abnormal and reports an error.
106
107## Cgroup Schedule Policy
108
109### Basic Policy
110
111| Scene  | Schedule policy  |
112|----------|-------|
113| Currently focused oreground application and processes with same uid  | top_app  |
114| Foreground visible processes, include unfocused window process in split mode, float window process, foundation process  | foreground  |
115| system level daemon processes. Assign system schedule policy to them when started,   | system  |
116| Background application and processes with same uid | background |
117| kernel processes, most native processes and others have not mensioned | root |
118
119### Policy Configuration
120
121Each schedule policy is configured in a json file, and is bound to different cgroup.
122```
123  "Cgroups": [
124    {
125      "controller": "cpu",
126      "path": "/dev/cpuctl",
127      "sched_policy": {
128        "sp_default": "",
129        "sp_background": "background",
130        "sp_foreground": "foreground",
131        "sp_system_background": "system-background",
132        "sp_top_app": "top-app"
133      }
134    },
135    {
136      "controller": "cpuset",
137      "path": "/dev/cpuset",
138      "sched_policy": {
139        "sp_default": "",
140        "sp_background": "background",
141        "sp_foreground": "foreground",
142        "sp_system_background": "system-background",
143        "sp_top_app": "top-app"
144      }
145    }
146  ]
147```
148
149### Restrictions
150
151Configuration file: cgroup_action_config.json
152Every schedule policy defined should be configured.
153
154Introduction to each config item:
155
156| Item | Description |
157|--------|--------|
158|controller|cgroup controller: cpuset, cpuctl, blkio etc.|
159|path|Absolute path of current cgroup|
160|sched_policy|Binding between each schedule policy and cgroup|
161|sp_xx|Different kinds of schedule policy|
162
163## SocPerf
164
165### Interfaces
166
167Supported SocPerf interfaces description
168
169| Interface  | Description  |
170|----------|-------|
171| PerfRequest(int32_t cmdId, const std::string& msg) | Used for Performace boost freq |
172| PerfRequestEx(int32_t cmdId, bool onOffTag, const std::string& msg) | Used for Performace boost freq and support ON/OFF |
173| PowerLimitBoost(bool onOffTag, const std::string& msg) | Used for Power limit freq which cannot be boosted |
174| ThermalLimitBoost(bool onOffTag, const std::string& msg) | Used for Thermal limit freq which cannot be boosted |
175| 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 |
176
177All interfaces are based on the key parameter cmdID, cmdID connects scenes and configs, which is used to boost freq or limit freq.
178Interface with parameter onOffTag means it support ON/OFF event. Normally, these interfaces are used for long-term event,
179which needs user to turn on or turn off manually.
180Parameter msg is used for extra information, like client's pid and tid.
181
182### Configs
183
184Config files description
185
186| File  | Description  |
187|----------|-------|
188| socperf_resource_config.xml | Define resource which can be modify,such as CPU/GPU/DDR/NPU |
189| socperf_boost_config.xml | Config file used for Performace boost |
190
191All xml files are different for particular products.
192For specific product, all resources which could be modify are defined in socperf_resource_config.xml. Each resource has its own resID.
193The cmdID in the socperf_boost_config.xml/socperf_resource_config.xml/socperf_thermal_config.xml must be different.
194
195## callback event receive
196Now support audio frameWork event、 telephony event、camera event callback
197
198audio frameWork contains renserState change、 ringMode change、 volumeKey change
199
200telphony change
201
202camera change
203
204## Repositories Involved<a name="section1371113476307"></a>
205- [windowmanager](https://gitee.com/openharmony/windowmanager)
206- [communication_ipc](https://gitee.com/openharmony/communication_ipc)
207- [hiviewdfx_hilog](https://gitee.com/openharmony/hiviewdfx_hilog)
208