• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 const std::string BOOT_ANIMATION_FINISHED_EVENT = "bootevent.bootanimation.finished";
40 constexpr int32_t WAIT_FOR_BOOT_ANIMATION_S = 14;
41 constexpr int32_t WAIT_TIME_FOR_UNSCHEDULE_MS = 100;
42 constexpr int32_t WAIT_COUNT_FOR_UNSCHEDULE = 5;
SetThreadQosLevel(void)43 void SetThreadQosLevel(void)
44 {
45     std::unordered_map<std::string, std::string> payload;
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)53 static void SetThreadQosLevelWithTid(int32_t pid, int32_t tid)
54 {
55     int32_t ret = WaitParameter(BOOT_ANIMATION_FINISHED_EVENT.c_str(), "true", WAIT_FOR_BOOT_ANIMATION_S);
56     if (ret != 0) {
57         AUDIO_ERR_LOG("wait for boot animation failed or timeout, ret = %{public}d", ret);
58     }
59     UnscheduleThreadInServer(static_cast<uint32_t>(pid), static_cast<uint32_t>(tid));
60     std::unordered_map<std::string, std::string> payload;
61     payload["pid"] = std::to_string(pid);
62     OHOS::ConcurrentTask::ConcurrentTaskClient::GetInstance().RequestAuth(payload);
63     int32_t retryCount = 0;
64     while (retryCount < WAIT_COUNT_FOR_UNSCHEDULE) {
65         ret = OHOS::QOS::SetQosForOtherThread(OHOS::QOS::QosLevel::QOS_USER_INTERACTIVE, tid);
66         if (ret != 0) {
67             AUDIO_WARNING_LOG("set qos for thread %{public}d failed, current retry count %{public}d, ret = %{public}d",
68                 tid, retryCount, ret);
69             retryCount++;
70             std::this_thread::sleep_for(std::chrono::milliseconds(WAIT_TIME_FOR_UNSCHEDULE_MS));
71         } else {
72             AUDIO_INFO_LOG("set qos for thread %{public}d success", tid);
73             return;
74         }
75     }
76     AUDIO_ERR_LOG("set qos for thread %{public}d finally failed", tid);
77 }
78 
SetThreadQosLevelAsync(void)79 void SetThreadQosLevelAsync(void)
80 {
81     int32_t tid = gettid();
82     int32_t pid = getpid();
83     std::thread setThreadQosLevelThread = std::thread([=] { SetThreadQosLevelWithTid(pid, tid); });
84     setThreadQosLevelThread.detach();
85 }
86 
ReSetThreadQosLevel(void)87 void ReSetThreadQosLevel(void)
88 {
89     OHOS::QOS::ResetThreadQos();
90 }
91 #else
92 void SetThreadQosLevel(void) {};
93 void SetThreadQosLevelAsync(void) {};
94 void ReSetThreadQosLevel(void) {};
95 #endif
96 
97 
98 #ifdef __cplusplus
99 }
100 #endif
101