• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# QoS Development
2
3## **Scenario**
4
5In a multi-processor or multi-tasking OS, resources such as CPUs and memory are shared among processes or tasks. Proper scheduling ensures fair distribution of resources, fast system response, and optimal resource utilization. Prioritizing tasks of applications based on their importance can help the system better schedule tasks. This topic describes how to use the quality-of-service (QoS) feature and related APIs to adjust the running time of tasks in the OpenHarmony system.
6
7You can customize the attributes for priority-based task scheduling based on the QoS feature.
8
9## Basic Concepts
10
11### QoS
12
13In OpenHarmony, the QoS feature allows critical tasks to receive necessary resources to meet performance requirements. You can prioritize tasks with different QoS levels based on their importance. The system then arranges the running time and sequence of each task based on their QoS level. For example, when multiple tasks need to be executed in the system, the tasks with less interaction with users, such as the background download tasks, can be executed later than the tasks perceived by users, such as animation drawing.
14
15### QoS Level
16The system provides six QoS levels in ascending order based on the degree of system-user interaction.
17
18| QoS Level                                                      | Application Scenario                                                        | Load                                                        |
19| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
20| QOS_BACKGROUND | Background tasks invisible to users, such as data synchronization and backup.| It takes several minutes or hours to complete the task.|
21| QOS_UTILITY | Tasks that do not require immediate response, such as data download and import.| It takes several seconds or minutes to complete the task.|
22| QOS_DEFAULT | Default level.| It takes a few seconds to complete the task.|
23| QOS_USER_INITIATED | Tasks triggered by users with observable progress, for example, opening a file.| The task is completed in seconds.|
24| QOS_DEADLINE_REQUEST | Tasks that require an immediate response, such as page loading.| The task is done immediately.|
25| QOS_USER_INTERACTIVE	 | User interaction tasks (UI thread, interface refreshing, and animation).| The task is instant.|
26
27The QoS level is specified by **QoS_Level**, which is an enum defined as follows:
28### QoS_Level Declaration
29```{.c}
30typedef enum QoS_Level {
31    /**
32     * QoS level for background tasks, such as data synchronization.
33     */
34    QOS_BACKGROUND,
35    /**
36     * QoS level for tasks that do not require immediate response, such as download.
37     */
38    QOS_UTILITY,
39    /**
40     * Default QoS level.
41     */
42    QOS_DEFAULT,
43    /**
44     * QoS level for tasks triggered by users with observable progress, such as opening a file.
45     */
46    QOS_USER_INITIATED,
47    /**
48     * QoS level for tasks that require immediate response, such as page loading.
49     */
50    QOS_DEADLINE_REQUEST,
51    /**
52     * QoS level for user interaction tasks, such as animation drawing.
53     */
54    QOS_USER_INTERACTIVE,
55} QoS_Level;
56
57```
58
59## Effect
60A task with a higher QoS level is allocated more CPU time than a task with a lower QoS level.
61
62The following shows how proper QoS accelerates application execution.
63
64### Optimization of Thread Execution by QoS
65
66#### Before Using QoS
67![qosfigure1.png](./figures/qosfigure1.png)
68
69Thread 1 and thread 2 are two key threads of an application. During the running of thread 1, thread 2 is triggered. Then, thread 1 will be blocked until thread 2 is executed. Before the QoS levels of the two threads are marked, thread 3 and thread 4 take precedence over these two threads. The figure above illustrates the execution of thread 1 and thread 2 before QoS is used.
70
711. Thread 1 waits to be woken up by thread 2. However, thread 2 has a low priority and is always preempted for a long time. As a result, thread 1 sleeps for a long time.
72
732. Thread 1 also has a low priority and waits for a long period of time after being woken up.
74
753. Thread 1 has a low priority and is always preempted by other threads for a long period of time during running.
76
77#### After Using QoS
78![qosfigure2.png](./figures/qosfigure2.png)
79
80The figure above illustrates the thread execution after QoS levels are set for thread 1 and thread 2.
81
821. The ratio of thread 2 running time increases, which decreases the wait time of thread 1.
83
842. After thread 1 is woken up by thread 2, the wait time decreases.
85
863. The ratio of thread 1 running time increases, and the preemption proportion decreases.
87
88### Optimization of the RN Framework by QoS
89As indicated by the following table, the performance of the open-source benchmark test is improved by about 13% after the QoS levels are set for key threads in the RN framework.
90
91| Scenario     | Test Environment| Total Rendering Time|
92| ----------- | ----------- | ----------- |
93| benchmark<br>1500view      | Without QoS      | 270.8 ms       |
94| benchmark<br>1500view   | With QoS       | 236.6 ms       |
95
96## Available APIs
97
98| API                                                      | Description                                                        | Parameter                                                        | Return Value                                                        |
99| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
100| OH_QoS_SetThreadQoS(QoS_Level level) | Sets the QoS level for this task.| QoS_Level level | **0** or **–1**|
101| OH_QoS_ResetThreadQoS() | Removes the QoS level of this task.| N/A| **0** or **–1**|
102| OH_QoS_GetThreadQoS(QoS_Level *level) | Obtains the QoS level of this task.| QoS_Level *level | **0** or **–1**|
103
104### Constraints
105* The QoS APIs can be used only for the current task.
106
107## Function Description
108
109### OH_QoS_SetThreadQoS
110
111#### Function
112```{.c}
113int OH_QoS_SetThreadQoS(QoS_Level level);
114```
115
116#### Parameters
117QoS_Level level
118* QoS level to set.
119
120#### Return Value
121* Returns **0** if the operation is successful; returns **-1** otherwise.
122
123#### Description
124Sets the QoS level for this task. You can mark the task with different QoS levels according to its importance, thereby achieving different scheduling priorities. For details, see [QoS Practice](https://developer.huawei.com/consumer/en/doc/best-practices/bpta-thread-priority-setting).
125
126#### Example
127```
128#include <stdio.h>
129#include "qos/qos.h"
130
131int func()
132{
133    // Set the QoS level of this task to QOS_USER_INITIATED.
134    int ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INITIATED);
135
136    if (!ret) { // If ret is 0, the operation is successful.
137        printf("set QoS Success.");
138    } else { // If ret is not 0, the operation fails.
139        printf("set QoS failed.");
140    }
141
142    return 0;
143}
144```
145
146### OH_QoS_ResetThreadQoS
147
148#### Function
149```{.c}
150int OH_QoS_ResetThreadQoS();
151```
152
153#### Parameters
154* N/A.
155
156#### Return Value
157* Returns **0** if the operation is successful; returns **-1** otherwise.
158
159#### Description
160Removes the QoS level of this task. For details, see [QoS Practice](https://developer.huawei.com/consumer/en/doc/best-practices/bpta-thread-priority-setting).
161
162#### Example
163```
164#include <stdio.h>
165#include "qos/qos.h"
166
167int func()
168{
169    // Remove the QoS level of this task.
170    int ret = OH_QoS_ResetThreadQoS();
171
172    if (!ret) { // If ret is 0, the operation is successful.
173        printf("reset QoS Success.");
174    } else { // If ret is not 0, the operation fails.
175        printf("reset QoS failed.");
176    }
177
178    return 0;
179}
180```
181
182### OH_QoS_GetThreadQoS
183
184#### Function
185```{.c}
186int OH_QoS_GetThreadQoS(QoS_Level *level);
187```
188
189#### Parameters
190QoS_Level *level
191* Pointer to the QoS level of the task.
192
193#### Return Value
194* Returns **0** if the operation is successful; returns **-1** otherwise.
195
196#### Description
197Obtains the latest QoS level of this task. If no QoS level is set, **-1** is returned. For details, see [QoS Practice](https://developer.huawei.com/consumer/en/doc/best-practices/bpta-thread-priority-setting).
198
199#### Example
200```
201#include <stdio.h>
202#include "qos/qos.h"
203
204int func()
205{
206    // Obtain the QoS level of this task.
207    QoS_Level level = QoS_Level::QOS_DEFAULT;
208    int ret = OH_QoS_GetThreadQoS(&level);
209
210    if (!ret) { // If ret is 0, the operation is successful.
211        printf("get QoS level %d Success.", level);
212    } else { // If ret is not 0, the operation fails.
213        printf("get QoS level failed.");
214    }
215
216    return 0;
217}
218```
219
220## How to Develop
221The following walks you through on how to query and modify the QoS level of a task using Node-API interfaces.
222
2231. Add the dynamic link library.
224The QoS feature depends on the **libqos.so** library, which needs to be added to the compilation environment of the target application.
225
226**Example**
227
228If you use DevEco Studio to create a template NDK project, the **CMakeLists.txt** script is generated by default. Add the dependent dynamic link library to the script. The following is an example:
229
230```txt
231# the minimum version of CMake.
232cmake_minimum_required(VERSION 3.4.1)
233project(qos)
234
235set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
236
237include_directories(${NATIVERENDER_ROOT_PATH}
238                    ${NATIVERENDER_ROOT_PATH}/include)
239
240add_library(entry SHARED hello.cpp)
241
242# You can directly reference libqos.so because it is in the NDK of the link addressing path.
243target_link_libraries(entry PUBLIC libqos.so)
244```
245
2462. Include the header file.
247
248In the source code that uses the QoS feature, include the related header file.
249
250```c
251#include "qos/qos.h"
252```
253
2543. Call QoS APIs.
255
256Use **OHQoSSetThreadQoS()** to set the QoS level for a task, use **OHQoSGetThreadQoS()** to obtain the QoS level set, and use **OHQoSResetThreadQoS()** to reset the QoS level to default.
257