Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 1 | /* |
| 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 Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 17 | #define LOG_TAG "SchedulingPolicyService" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 20 | #include <binder/IServiceManager.h> |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 21 | #include <cutils/properties.h> |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 22 | #include <utils/Mutex.h> |
| 23 | #include "ISchedulingPolicyService.h" |
Eino-Ville Talvala | f99498e | 2015-09-25 16:52:55 -0700 | [diff] [blame] | 24 | #include "mediautils/SchedulingPolicyService.h" |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | static sp<ISchedulingPolicyService> sSchedulingPolicyService; |
| 29 | static const String16 _scheduling_policy("scheduling_policy"); |
| 30 | static Mutex sMutex; |
| 31 | |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 32 | int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous) |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 33 | { |
| 34 | // FIXME merge duplicated code related to service lookup, caching, and error recovery |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 35 | int ret; |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 36 | for (;;) { |
| 37 | sMutex.lock(); |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 38 | sp<ISchedulingPolicyService> sps = sSchedulingPolicyService; |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 39 | sMutex.unlock(); |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 40 | if (sps == 0) { |
| 41 | sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy); |
| 42 | if (binder == 0) { |
| 43 | sleep(1); |
| 44 | continue; |
| 45 | } |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 46 | sps = interface_cast<ISchedulingPolicyService>(binder); |
| 47 | sMutex.lock(); |
| 48 | sSchedulingPolicyService = sps; |
| 49 | sMutex.unlock(); |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 50 | } |
Mikhail Naganov | 83f0427 | 2017-02-07 10:45:09 -0800 | [diff] [blame] | 51 | ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous); |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 52 | if (ret != DEAD_OBJECT) { |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 53 | break; |
| 54 | } |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 55 | ALOGW("SchedulingPolicyService died"); |
| 56 | sMutex.lock(); |
| 57 | sSchedulingPolicyService.clear(); |
| 58 | sMutex.unlock(); |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 59 | } |
Eric Laurent | f59a4b3 | 2013-07-02 11:15:41 -0700 | [diff] [blame] | 60 | return ret; |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Chong Zhang | fdd512a | 2019-11-22 11:03:14 -0800 | [diff] [blame] | 63 | int requestCpusetBoost(bool enable, const sp<IBinder> &client) |
Chong Zhang | 79d2b28 | 2018-04-17 14:14:31 -0700 | [diff] [blame] | 64 | { |
| 65 | int ret; |
| 66 | sMutex.lock(); |
| 67 | sp<ISchedulingPolicyService> sps = sSchedulingPolicyService; |
| 68 | sMutex.unlock(); |
| 69 | if (sps == 0) { |
| 70 | sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy); |
| 71 | if (binder == 0) { |
| 72 | return DEAD_OBJECT; |
| 73 | } |
| 74 | sps = interface_cast<ISchedulingPolicyService>(binder); |
| 75 | sMutex.lock(); |
| 76 | sSchedulingPolicyService = sps; |
| 77 | sMutex.unlock(); |
| 78 | } |
| 79 | ret = sps->requestCpusetBoost(enable, client); |
| 80 | if (ret != DEAD_OBJECT) { |
| 81 | return ret; |
| 82 | } |
| 83 | ALOGW("SchedulingPolicyService died"); |
| 84 | sMutex.lock(); |
| 85 | sSchedulingPolicyService.clear(); |
| 86 | sMutex.unlock(); |
| 87 | return ret; |
| 88 | } |
| 89 | |
Andy Hung | 41ccf7f | 2022-12-14 14:25:49 -0800 | [diff] [blame] | 90 | int requestSpatializerPriority(pid_t pid, pid_t tid) { |
| 91 | if (pid == -1 || tid == -1) return BAD_VALUE; |
| 92 | |
| 93 | // update priority to RT if specified. |
| 94 | constexpr int32_t kRTPriorityMin = 1; |
| 95 | constexpr int32_t kRTPriorityMax = 3; |
| 96 | const int32_t priorityBoost = |
| 97 | property_get_int32("audio.spatializer.priority", kRTPriorityMin); |
| 98 | if (priorityBoost >= kRTPriorityMin && priorityBoost <= kRTPriorityMax) { |
| 99 | const status_t status = requestPriority( |
| 100 | pid, tid, priorityBoost, false /* isForApp */, true /*asynchronous*/); |
| 101 | if (status != OK) { |
| 102 | ALOGW("%s: Cannot request spatializer priority boost %d, status:%d", |
| 103 | __func__, priorityBoost, status); |
| 104 | return status < 0 ? status : UNKNOWN_ERROR; |
| 105 | } |
| 106 | return priorityBoost; |
| 107 | } |
| 108 | return 0; // no boost requested |
| 109 | } |
| 110 | |
Glenn Kasten | 1dc28b7 | 2012-04-24 10:01:03 -0700 | [diff] [blame] | 111 | } // namespace android |