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 14. 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. 15 16### QoS Level 17Currently, OpenHarmony provides six QoS levels in ascending order based on the degree of system-user interaction. 18 19| QoS Level | Application Scenario | Load | 20| ------------------------------------------------------------ | ------------------------------------------------------------ | ------------------------------------------------------------ | 21| QOS_BACKGROUND | Background tasks invisible to users, such as data synchronization and backup.| It takes several minutes or hours to complete the task.| 22| 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.| 23| QOS_DEFAULT | Default level.| It takes a few seconds to complete the task.| 24| QOS_USER_INITIATED | Tasks triggered by users with observable progress, for example, opening a file.| The task is completed in seconds.| 25| QOS_DEADLINE_REQUEST | Tasks that require an immediate response, such as page loading.| The task is done immediately.| 26| QOS_USER_INTERACTIVE | User interaction tasks (UI thread, interface refreshing, and animation).| The task is instant.| 27 28The QoS level is specified by **QoS_level**, which is an enum defined as follows: 29### QoS_Level Declaration 30```{.c} 31typedef enum QoS_Level { 32 /** 33 * QoS level for background tasks, such as data synchronization. 34 */ 35 QOS_BACKGROUND, 36 /** 37 * QoS level for tasks that do not require immediate response, such as download. 38 */ 39 QOS_UTILITY, 40 /** 41 * Default QoS level. 42 */ 43 QOS_DEFAULT, 44 /** 45 * QoS level for tasks triggered by users with observable progress, such as opening a file. 46 */ 47 QOS_USER_INITIATED, 48 /** 49 * QoS level for tasks that require immediate response, such as page loading. 50 */ 51 QOS_DEADLINE_REQUEST, 52 /** 53 * QoS level for user interaction tasks, such as animation drawing. 54 */ 55 QOS_USER_INTERACTIVE, 56} QoS_Level; 57 58``` 59 60## Effect 61A task with a higher QoS level is allocated more CPU time than a task with a lower QoS level. 62 63The following shows how proper QoS accelerates application execution. 64 65### Optimization of Thread Execution by QoS 66 67#### Before Using QoS 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 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| Hermes Engine Time| RN Common Time| Common Instruction Execution Time for RN Framework + ArkUI Native Rendering| Native Rendering Period| Total| 92| ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | ----------- | 93| benchmark<br>1500view | Without QoS | 173.1 ms | 24.3 ms | 33.1 ms | 4.03 ms | 270.8 ms | 94| benchmark<br>1500view | With QoS | 144.8 ms | 23.4 ms | 33.7 ms | 34.8 ms | 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 for 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. 125 126#### Example 127``` 128#include <stdio.h> 129#include "qos/qos.h" 130 131int main() 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. 161 162#### Example 163``` 164#include <stdio.h> 165#include "qos/qos.h" 166 167int main() 168{ 169 // Removes 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* QoS level set for a 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. 198 199#### Example 200``` 201#include <stdio.h> 202#include "qos/qos.h" 203 204int main() 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 223**Adding the Dynamic Link Library** 224 225Add the following library to **CMakeLists.txt**. 226```txt 227libqos.so 228``` 229 230#### Example 231```txt 232# the minimum version of CMake. 233cmake_minimum_required(VERSION 3.4.1) 234project(qos) 235 236set(NATIVERENDER_ROOT_PATH ${CMAKE_CURRENT_SOURCE_DIR}) 237 238include_directories(${NATIVERENDER_ROOT_PATH} 239 ${NATIVERENDER_ROOT_PATH}/include) 240 241add_library(entry SHARED hello.cpp) 242target_link_libraries(entry PUBLIC libqos.so) 243``` 244 245**Including the Header File** 246```c 247#include "qos/qos.h" 248``` 249**Calling QoS APIs** 250 251Use **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