1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 #ifndef LOG_TAG
16 #define LOG_TAG "AudioQosManager"
17 #endif
18
19 #include "audio_qosmanager.h"
20 #include <unistd.h>
21 #include <cstring>
22 #include <unordered_map>
23
24 #ifdef QOSMANAGER_ENABLE
25 #include <chrono>
26 #include <thread>
27 #include "audio_common_log.h"
28 #include "audio_schedule.h"
29 #include "qos.h"
30 #include "concurrent_task_client.h"
31 #include "parameter.h"
32 #endif
33
34 #ifdef __cplusplus
35 extern "C" {
36 #endif
37
38 #ifdef QOSMANAGER_ENABLE
39
40 constexpr int32_t AUDIO_PROC_QOS_TABLE = 7;
41
SetThreadQosLevel(void)42 void SetThreadQosLevel(void)
43 {
44 std::unordered_map<std::string, std::string> payload;
45 payload["groupId"] = std::to_string(AUDIO_PROC_QOS_TABLE);
46 payload["pid"] = std::to_string(getpid());
47 OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().RequestAuth(payload);
48 int32_t ret = OHOS::QOS::SetThreadQos(OHOS::QOS::QosLevel::QOS_USER_INTERACTIVE);
49 CHECK_AND_RETURN_LOG(ret == 0, "set thread qos failed, ret = %{public}d", ret);
50 AUDIO_INFO_LOG("set thread qos success");
51 }
52
SetThreadQosLevelWithTid(int32_t pid,int32_t tid,int32_t setPriority)53 static void SetThreadQosLevelWithTid(int32_t pid, int32_t tid, int32_t setPriority)
54 {
55 std::unordered_map<std::string, std::string> payload;
56 payload["groupId"] = std::to_string(AUDIO_PROC_QOS_TABLE);
57 payload["pid"] = std::to_string(pid);
58 OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().RequestAuth(payload);
59
60 int32_t ret;
61 if (setPriority == 1) {
62 ret = OHOS::QOS::SetQosForOtherThread(OHOS::QOS::QosLevel::QOS_USER_INTERACTIVE, tid);
63 CHECK_AND_RETURN_LOG(ret == 0, "set thread qos failed, ret = %{public}d", ret);
64 AUDIO_INFO_LOG("set qos %{public}d for thread %{public}d success",
65 OHOS::QOS::QosLevel::QOS_USER_INTERACTIVE, tid);
66 return;
67 }
68
69 ret = OHOS::QOS::SetQosForOtherThread(OHOS::QOS::QosLevel::QOS_KEY_BACKGROUND, tid);
70 CHECK_AND_RETURN_LOG(ret == 0, "set thread qos failed, ret = %{public}d", ret);
71 AUDIO_INFO_LOG("set qos %{public}d for thread %{public}d success", OHOS::QOS::QosLevel::QOS_KEY_BACKGROUND, tid);
72 }
73
SetThreadQosLevelAsync(int32_t setPriority)74 void SetThreadQosLevelAsync(int32_t setPriority)
75 {
76 AUDIO_INFO_LOG("set thread qos level start");
77 int32_t tid = gettid();
78 int32_t pid = getpid();
79 std::thread setThreadQosLevelThread = std::thread([=] { SetThreadQosLevelWithTid(pid, tid, setPriority); });
80 setThreadQosLevelThread.detach();
81 }
82
ResetThreadQosLevel(void)83 void ResetThreadQosLevel(void)
84 {
85 OHOS::QOS::ResetThreadQos();
86 }
87 #else
88 void SetThreadQosLevel(void) {};
89 void SetThreadQosLevelAsync(int32_t setPriority) {};
90 void ResetThreadQosLevel(void) {};
91 #endif
92
93
94 #ifdef __cplusplus
95 }
96 #endif
97