1e41f4b71Sopenharmony_ci# QoS Development
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## **Scenario**
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ciIn 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.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ciYou can customize the attributes for priority-based task scheduling based on the QoS feature.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci## Basic Concepts
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci### QoS
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ciIn 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
14e41f4b71Sopenharmony_ci. 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.
15e41f4b71Sopenharmony_ci
16e41f4b71Sopenharmony_ci### QoS Level
17e41f4b71Sopenharmony_ciCurrently, OpenHarmony provides six QoS levels in ascending order based on the degree of system-user interaction.
18e41f4b71Sopenharmony_ci
19e41f4b71Sopenharmony_ci| QoS Level                                                      | Application Scenario                                                        | Load                                                        |
20e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
21e41f4b71Sopenharmony_ci| QOS_BACKGROUND | Background tasks invisible to users, such as data synchronization and backup.| It takes several minutes or hours to complete the task.|
22e41f4b71Sopenharmony_ci| 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.|
23e41f4b71Sopenharmony_ci| QOS_DEFAULT | Default level.| It takes a few seconds to complete the task.|
24e41f4b71Sopenharmony_ci| QOS_USER_INITIATED | Tasks triggered by users with observable progress, for example, opening a file.| The task is completed in seconds.|
25e41f4b71Sopenharmony_ci| QOS_DEADLINE_REQUEST | Tasks that require an immediate response, such as page loading.| The task is done immediately.|
26e41f4b71Sopenharmony_ci| QOS_USER_INTERACTIVE	 | User interaction tasks (UI thread, interface refreshing, and animation).| The task is instant.|
27e41f4b71Sopenharmony_ci
28e41f4b71Sopenharmony_ciThe QoS level is specified by **QoS_level**, which is an enum defined as follows:
29e41f4b71Sopenharmony_ci### QoS_Level Declaration
30e41f4b71Sopenharmony_ci```{.c}
31e41f4b71Sopenharmony_citypedef enum QoS_Level {
32e41f4b71Sopenharmony_ci    /**
33e41f4b71Sopenharmony_ci     * QoS level for background tasks, such as data synchronization.
34e41f4b71Sopenharmony_ci     */
35e41f4b71Sopenharmony_ci    QOS_BACKGROUND,
36e41f4b71Sopenharmony_ci    /**
37e41f4b71Sopenharmony_ci     * QoS level for tasks that do not require immediate response, such as download.
38e41f4b71Sopenharmony_ci     */
39e41f4b71Sopenharmony_ci    QOS_UTILITY,
40e41f4b71Sopenharmony_ci    /**
41e41f4b71Sopenharmony_ci     * Default QoS level.
42e41f4b71Sopenharmony_ci     */
43e41f4b71Sopenharmony_ci    QOS_DEFAULT,
44e41f4b71Sopenharmony_ci    /**
45e41f4b71Sopenharmony_ci     * QoS level for tasks triggered by users with observable progress, such as opening a file.
46e41f4b71Sopenharmony_ci     */
47e41f4b71Sopenharmony_ci    QOS_USER_INITIATED,
48e41f4b71Sopenharmony_ci    /**
49e41f4b71Sopenharmony_ci     * QoS level for tasks that require immediate response, such as page loading.
50e41f4b71Sopenharmony_ci     */
51e41f4b71Sopenharmony_ci    QOS_DEADLINE_REQUEST,
52e41f4b71Sopenharmony_ci    /**
53e41f4b71Sopenharmony_ci     * QoS level for user interaction tasks, such as animation drawing.
54e41f4b71Sopenharmony_ci     */
55e41f4b71Sopenharmony_ci    QOS_USER_INTERACTIVE,
56e41f4b71Sopenharmony_ci} QoS_Level;
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci```
59e41f4b71Sopenharmony_ci
60e41f4b71Sopenharmony_ci## Effect
61e41f4b71Sopenharmony_ciA task with a higher QoS level is allocated more CPU time than a task with a lower QoS level.
62e41f4b71Sopenharmony_ci
63e41f4b71Sopenharmony_ciThe following shows how proper QoS accelerates application execution.
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci### Optimization of Thread Execution by QoS
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci#### Before Using QoS
68e41f4b71Sopenharmony_ci![qosfigure1.png](./figures/qosfigure1.png)
69e41f4b71Sopenharmony_ciThread 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.
70e41f4b71Sopenharmony_ci
71e41f4b71Sopenharmony_ci1. 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.
72e41f4b71Sopenharmony_ci
73e41f4b71Sopenharmony_ci2. Thread 1 also has a low priority and waits for a long period of time after being woken up.
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci3. Thread 1 has a low priority and is always preempted by other threads for a long period of time during running.
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci#### After using QoS
78e41f4b71Sopenharmony_ci![qosfigure2.png](./figures/qosfigure2.png)
79e41f4b71Sopenharmony_ci
80e41f4b71Sopenharmony_ciThe figure above illustrates the thread execution after QoS levels are set for thread 1 and thread 2.
81e41f4b71Sopenharmony_ci
82e41f4b71Sopenharmony_ci1. The ratio of thread 2 running time increases, which decreases the wait time of thread 1.
83e41f4b71Sopenharmony_ci
84e41f4b71Sopenharmony_ci2. After thread 1 is woken up by thread 2, the wait time decreases.
85e41f4b71Sopenharmony_ci
86e41f4b71Sopenharmony_ci3. The ratio of thread 1 running time increases, and the preemption proportion decreases.
87e41f4b71Sopenharmony_ci
88e41f4b71Sopenharmony_ci### Optimization of the RN Framework by QoS
89e41f4b71Sopenharmony_ciAs 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.
90e41f4b71Sopenharmony_ci
91e41f4b71Sopenharmony_ci| Scenario     | Test Environment| Hermes Engine Time| RN Common Time| Common Instruction Execution Time for RN Framework + ArkUI Native Rendering| Native Rendering Period| Total|
92e41f4b71Sopenharmony_ci| ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- |
93e41f4b71Sopenharmony_ci| benchmark<br>1500view      | Without QoS      | 173.1 ms       | 24.3 ms       | 33.1 ms       | 4.03 ms       | 270.8 ms       |
94e41f4b71Sopenharmony_ci| benchmark<br>1500view   | With QoS       | 144.8 ms        | 23.4 ms        | 33.7 ms        | 34.8 ms       | 236.6 ms       |
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci## Available APIs
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci| API                                                      | Description                                                        | Parameter                                                        | Return Value                                                        |
99e41f4b71Sopenharmony_ci| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ |
100e41f4b71Sopenharmony_ci| OH_QoS_SetThreadQoS(QoS_Level level) | Sets the QoS level for this task.| QoS_Level level | **0** or **–1**|
101e41f4b71Sopenharmony_ci| OH_QoS_ResetThreadQoS() | Removes the QoS level for this task.| N/A| **0** or **–1**|
102e41f4b71Sopenharmony_ci| OH_QoS_GetThreadQoS(QoS_Level *level) | Obtains the QoS level of this task.| QoS_Level *level | **0** or **–1**|
103e41f4b71Sopenharmony_ci
104e41f4b71Sopenharmony_ci### Constraints
105e41f4b71Sopenharmony_ci* The QoS APIs can be used only for the current task.
106e41f4b71Sopenharmony_ci
107e41f4b71Sopenharmony_ci## Function Description
108e41f4b71Sopenharmony_ci
109e41f4b71Sopenharmony_ci### OH_QoS_SetThreadQoS
110e41f4b71Sopenharmony_ci
111e41f4b71Sopenharmony_ci#### Function
112e41f4b71Sopenharmony_ci```{.c}
113e41f4b71Sopenharmony_ciint OH_QoS_SetThreadQoS(QoS_Level level);
114e41f4b71Sopenharmony_ci```
115e41f4b71Sopenharmony_ci
116e41f4b71Sopenharmony_ci#### Parameters
117e41f4b71Sopenharmony_ciQoS_Level level
118e41f4b71Sopenharmony_ci* QoS level to set.
119e41f4b71Sopenharmony_ci
120e41f4b71Sopenharmony_ci#### Return Value
121e41f4b71Sopenharmony_ci* Returns **0** if the operation is successful; returns **-1** otherwise.
122e41f4b71Sopenharmony_ci
123e41f4b71Sopenharmony_ci#### Description
124e41f4b71Sopenharmony_ciSets the QoS level for this task.
125e41f4b71Sopenharmony_ci
126e41f4b71Sopenharmony_ci#### Example
127e41f4b71Sopenharmony_ci```
128e41f4b71Sopenharmony_ci#include <stdio.h>
129e41f4b71Sopenharmony_ci#include "qos/qos.h"
130e41f4b71Sopenharmony_ci
131e41f4b71Sopenharmony_ciint main()
132e41f4b71Sopenharmony_ci{
133e41f4b71Sopenharmony_ci    // Set the QoS level of this task to QOS_USER_INITIATED.
134e41f4b71Sopenharmony_ci    int ret = OH_QoS_SetThreadQoS(QoS_Level::QOS_USER_INITIATED);
135e41f4b71Sopenharmony_ci    
136e41f4b71Sopenharmony_ci    if (!ret) { // If ret is 0, the operation is successful.
137e41f4b71Sopenharmony_ci        printf("set QoS Success.");
138e41f4b71Sopenharmony_ci    } else { // If ret is not 0, the operation fails.
139e41f4b71Sopenharmony_ci        printf("set QoS failed.");
140e41f4b71Sopenharmony_ci    }
141e41f4b71Sopenharmony_ci
142e41f4b71Sopenharmony_ci    return 0;
143e41f4b71Sopenharmony_ci}
144e41f4b71Sopenharmony_ci```
145e41f4b71Sopenharmony_ci
146e41f4b71Sopenharmony_ci### OH_QoS_ResetThreadQoS
147e41f4b71Sopenharmony_ci
148e41f4b71Sopenharmony_ci#### Function
149e41f4b71Sopenharmony_ci```{.c}
150e41f4b71Sopenharmony_ciint OH_QoS_ResetThreadQoS();
151e41f4b71Sopenharmony_ci```
152e41f4b71Sopenharmony_ci
153e41f4b71Sopenharmony_ci#### Parameters
154e41f4b71Sopenharmony_ci* N/A.
155e41f4b71Sopenharmony_ci
156e41f4b71Sopenharmony_ci#### Return Value
157e41f4b71Sopenharmony_ci* Returns **0** if the operation is successful; returns **-1** otherwise.
158e41f4b71Sopenharmony_ci
159e41f4b71Sopenharmony_ci#### Description
160e41f4b71Sopenharmony_ciRemoves the QoS level of this task.
161e41f4b71Sopenharmony_ci
162e41f4b71Sopenharmony_ci#### Example
163e41f4b71Sopenharmony_ci```
164e41f4b71Sopenharmony_ci#include <stdio.h>
165e41f4b71Sopenharmony_ci#include "qos/qos.h"
166e41f4b71Sopenharmony_ci
167e41f4b71Sopenharmony_ciint main()
168e41f4b71Sopenharmony_ci{
169e41f4b71Sopenharmony_ci    // Removes the QoS level of this task.
170e41f4b71Sopenharmony_ci    int ret = OH_QoS_ResetThreadQoS();
171e41f4b71Sopenharmony_ci    
172e41f4b71Sopenharmony_ci    if (!ret) { // If ret is 0, the operation is successful.
173e41f4b71Sopenharmony_ci        printf("reset QoS Success.");
174e41f4b71Sopenharmony_ci    } else { // If ret is not 0, the operation fails.
175e41f4b71Sopenharmony_ci        printf("reset QoS failed.");
176e41f4b71Sopenharmony_ci    }
177e41f4b71Sopenharmony_ci
178e41f4b71Sopenharmony_ci    return 0;
179e41f4b71Sopenharmony_ci}
180e41f4b71Sopenharmony_ci```
181e41f4b71Sopenharmony_ci
182e41f4b71Sopenharmony_ci### OH_QoS_GetThreadQoS
183e41f4b71Sopenharmony_ci
184e41f4b71Sopenharmony_ci#### Function
185e41f4b71Sopenharmony_ci```{.c}
186e41f4b71Sopenharmony_ciint OH_QoS_GetThreadQoS(QoS_Level *level);
187e41f4b71Sopenharmony_ci```
188e41f4b71Sopenharmony_ci
189e41f4b71Sopenharmony_ci#### Parameters
190e41f4b71Sopenharmony_ciQoS_Level *level
191e41f4b71Sopenharmony_ci* QoS level set for a task.
192e41f4b71Sopenharmony_ci
193e41f4b71Sopenharmony_ci#### Return Value
194e41f4b71Sopenharmony_ci* Returns **0** if the operation is successful; returns **-1** otherwise.
195e41f4b71Sopenharmony_ci
196e41f4b71Sopenharmony_ci#### Description
197e41f4b71Sopenharmony_ciObtains the latest QoS level of this task. If no QoS level is set, **-1** is returned.
198e41f4b71Sopenharmony_ci
199e41f4b71Sopenharmony_ci#### Example
200e41f4b71Sopenharmony_ci```
201e41f4b71Sopenharmony_ci#include <stdio.h>
202e41f4b71Sopenharmony_ci#include "qos/qos.h"
203e41f4b71Sopenharmony_ci
204e41f4b71Sopenharmony_ciint main()
205e41f4b71Sopenharmony_ci{
206e41f4b71Sopenharmony_ci    // Obtain the QoS level of this task.
207e41f4b71Sopenharmony_ci    QoS_Level level = QoS_Level::QOS_DEFAULT;
208e41f4b71Sopenharmony_ci    int ret = OH_QoS_GetThreadQoS(&level);
209e41f4b71Sopenharmony_ci
210e41f4b71Sopenharmony_ci    if (!ret) { // If ret is 0, the operation is successful.
211e41f4b71Sopenharmony_ci        printf("get QoS level %d Success.", level);
212e41f4b71Sopenharmony_ci    } else { // If ret is not 0, the operation fails.
213e41f4b71Sopenharmony_ci        printf("get QoS level failed.");
214e41f4b71Sopenharmony_ci    }
215e41f4b71Sopenharmony_ci
216e41f4b71Sopenharmony_ci    return 0;
217e41f4b71Sopenharmony_ci}
218e41f4b71Sopenharmony_ci```
219e41f4b71Sopenharmony_ci
220e41f4b71Sopenharmony_ci## How to Develop
221e41f4b71Sopenharmony_ciThe following walks you through on how to query and modify the QoS level of a task using Node-API interfaces.
222e41f4b71Sopenharmony_ci
223e41f4b71Sopenharmony_ci**Adding the Dynamic Link Library**
224e41f4b71Sopenharmony_ci
225e41f4b71Sopenharmony_ciAdd the following library to **CMakeLists.txt**.
226e41f4b71Sopenharmony_ci```txt
227e41f4b71Sopenharmony_cilibqos.so
228e41f4b71Sopenharmony_ci```
229e41f4b71Sopenharmony_ci
230e41f4b71Sopenharmony_ci#### Example
231e41f4b71Sopenharmony_ci```txt
232e41f4b71Sopenharmony_ci# the minimum version of CMake.
233e41f4b71Sopenharmony_cicmake_minimum_required(VERSION 3.4.1)
234e41f4b71Sopenharmony_ciproject(qos)
235e41f4b71Sopenharmony_ci
236e41f4b71Sopenharmony_ciset(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR})
237e41f4b71Sopenharmony_ci
238e41f4b71Sopenharmony_ciinclude_directories(${NATIVERENDER_ROOT_PATH}
239e41f4b71Sopenharmony_ci                    ${NATIVERENDER_ROOT_PATH}/include)
240e41f4b71Sopenharmony_ci
241e41f4b71Sopenharmony_ciadd_library(entry SHARED hello.cpp)
242e41f4b71Sopenharmony_citarget_link_libraries(entry PUBLIC libqos.so)
243e41f4b71Sopenharmony_ci```
244e41f4b71Sopenharmony_ci
245e41f4b71Sopenharmony_ci**Including the Header File**
246e41f4b71Sopenharmony_ci```c
247e41f4b71Sopenharmony_ci#include "qos/qos.h"
248e41f4b71Sopenharmony_ci```
249e41f4b71Sopenharmony_ci**Calling QoS APIs**
250e41f4b71Sopenharmony_ci
251e41f4b71Sopenharmony_ciUse **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.	
252