blob: 62c3f81571875169fdfd26cc9a6a641008c4c0ba [file] [log] [blame]
Glenn Kasten1dc28b72012-04-24 10:01:03 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Eric Laurentf59a4b32013-07-02 11:15:41 -070017#define LOG_TAG "SchedulingPolicyService"
18//#define LOG_NDEBUG 0
19
Andy Hung8e8a4b92023-11-15 18:36:21 -080020#include <audio_utils/threads.h>
Glenn Kasten1dc28b72012-04-24 10:01:03 -070021#include <binder/IServiceManager.h>
Andy Hung8e8a4b92023-11-15 18:36:21 -080022#include <cutils/android_filesystem_config.h>
Andy Hung41ccf7f2022-12-14 14:25:49 -080023#include <cutils/properties.h>
Glenn Kasten1dc28b72012-04-24 10:01:03 -070024#include <utils/Mutex.h>
25#include "ISchedulingPolicyService.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070026#include "mediautils/SchedulingPolicyService.h"
Glenn Kasten1dc28b72012-04-24 10:01:03 -070027
28namespace android {
29
30static sp<ISchedulingPolicyService> sSchedulingPolicyService;
31static const String16 _scheduling_policy("scheduling_policy");
32static Mutex sMutex;
33
Mikhail Naganov83f04272017-02-07 10:45:09 -080034int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous)
Glenn Kasten1dc28b72012-04-24 10:01:03 -070035{
Andy Hung8e8a4b92023-11-15 18:36:21 -080036 // audioserver thread priority boosted internally to reduce binder latency and boot time.
37 if (!isForApp && pid == getpid() && getuid() == AID_AUDIOSERVER && prio >= 1 && prio <= 3) {
38 const status_t status = audio_utils::set_thread_priority(
39 tid, audio_utils::rtprio_to_unified_priority(prio));
40 if (status == NO_ERROR) return NO_ERROR;
41 ALOGD("%s: set priority %d, status:%d needs to fallback to SchedulingPolicyService",
42 __func__, prio, status);
43 }
44
Glenn Kasten1dc28b72012-04-24 10:01:03 -070045 // FIXME merge duplicated code related to service lookup, caching, and error recovery
Eric Laurentf59a4b32013-07-02 11:15:41 -070046 int ret;
Glenn Kasten1dc28b72012-04-24 10:01:03 -070047 for (;;) {
48 sMutex.lock();
Eric Laurentf59a4b32013-07-02 11:15:41 -070049 sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
Glenn Kasten1dc28b72012-04-24 10:01:03 -070050 sMutex.unlock();
Eric Laurentf59a4b32013-07-02 11:15:41 -070051 if (sps == 0) {
52 sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
53 if (binder == 0) {
54 sleep(1);
55 continue;
56 }
Glenn Kasten1dc28b72012-04-24 10:01:03 -070057 sps = interface_cast<ISchedulingPolicyService>(binder);
58 sMutex.lock();
59 sSchedulingPolicyService = sps;
60 sMutex.unlock();
Eric Laurentf59a4b32013-07-02 11:15:41 -070061 }
Mikhail Naganov83f04272017-02-07 10:45:09 -080062 ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous);
Eric Laurentf59a4b32013-07-02 11:15:41 -070063 if (ret != DEAD_OBJECT) {
Glenn Kasten1dc28b72012-04-24 10:01:03 -070064 break;
65 }
Eric Laurentf59a4b32013-07-02 11:15:41 -070066 ALOGW("SchedulingPolicyService died");
67 sMutex.lock();
68 sSchedulingPolicyService.clear();
69 sMutex.unlock();
Glenn Kasten1dc28b72012-04-24 10:01:03 -070070 }
Eric Laurentf59a4b32013-07-02 11:15:41 -070071 return ret;
Glenn Kasten1dc28b72012-04-24 10:01:03 -070072}
73
Chong Zhangfdd512a2019-11-22 11:03:14 -080074int requestCpusetBoost(bool enable, const sp<IBinder> &client)
Chong Zhang79d2b282018-04-17 14:14:31 -070075{
76 int ret;
77 sMutex.lock();
78 sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
79 sMutex.unlock();
80 if (sps == 0) {
81 sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
82 if (binder == 0) {
83 return DEAD_OBJECT;
84 }
85 sps = interface_cast<ISchedulingPolicyService>(binder);
86 sMutex.lock();
87 sSchedulingPolicyService = sps;
88 sMutex.unlock();
89 }
90 ret = sps->requestCpusetBoost(enable, client);
91 if (ret != DEAD_OBJECT) {
92 return ret;
93 }
94 ALOGW("SchedulingPolicyService died");
95 sMutex.lock();
96 sSchedulingPolicyService.clear();
97 sMutex.unlock();
98 return ret;
99}
100
Andy Hung41ccf7f2022-12-14 14:25:49 -0800101int requestSpatializerPriority(pid_t pid, pid_t tid) {
102 if (pid == -1 || tid == -1) return BAD_VALUE;
103
104 // update priority to RT if specified.
105 constexpr int32_t kRTPriorityMin = 1;
106 constexpr int32_t kRTPriorityMax = 3;
107 const int32_t priorityBoost =
108 property_get_int32("audio.spatializer.priority", kRTPriorityMin);
109 if (priorityBoost >= kRTPriorityMin && priorityBoost <= kRTPriorityMax) {
110 const status_t status = requestPriority(
111 pid, tid, priorityBoost, false /* isForApp */, true /*asynchronous*/);
112 if (status != OK) {
113 ALOGW("%s: Cannot request spatializer priority boost %d, status:%d",
114 __func__, priorityBoost, status);
115 return status < 0 ? status : UNKNOWN_ERROR;
116 }
117 return priorityBoost;
118 }
119 return 0; // no boost requested
120}
121
Glenn Kasten1dc28b72012-04-24 10:01:03 -0700122} // namespace android