blob: 6e515ffd583ba22c3731cdb05d0d70a150355f93 [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
Glenn Kasten1dc28b72012-04-24 10:01:03 -070020#include <binder/IServiceManager.h>
Andy Hung41ccf7f2022-12-14 14:25:49 -080021#include <cutils/properties.h>
Glenn Kasten1dc28b72012-04-24 10:01:03 -070022#include <utils/Mutex.h>
23#include "ISchedulingPolicyService.h"
Eino-Ville Talvalaf99498e2015-09-25 16:52:55 -070024#include "mediautils/SchedulingPolicyService.h"
Glenn Kasten1dc28b72012-04-24 10:01:03 -070025
26namespace android {
27
28static sp<ISchedulingPolicyService> sSchedulingPolicyService;
29static const String16 _scheduling_policy("scheduling_policy");
30static Mutex sMutex;
31
Mikhail Naganov83f04272017-02-07 10:45:09 -080032int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous)
Glenn Kasten1dc28b72012-04-24 10:01:03 -070033{
34 // FIXME merge duplicated code related to service lookup, caching, and error recovery
Eric Laurentf59a4b32013-07-02 11:15:41 -070035 int ret;
Glenn Kasten1dc28b72012-04-24 10:01:03 -070036 for (;;) {
37 sMutex.lock();
Eric Laurentf59a4b32013-07-02 11:15:41 -070038 sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
Glenn Kasten1dc28b72012-04-24 10:01:03 -070039 sMutex.unlock();
Eric Laurentf59a4b32013-07-02 11:15:41 -070040 if (sps == 0) {
41 sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
42 if (binder == 0) {
43 sleep(1);
44 continue;
45 }
Glenn Kasten1dc28b72012-04-24 10:01:03 -070046 sps = interface_cast<ISchedulingPolicyService>(binder);
47 sMutex.lock();
48 sSchedulingPolicyService = sps;
49 sMutex.unlock();
Eric Laurentf59a4b32013-07-02 11:15:41 -070050 }
Mikhail Naganov83f04272017-02-07 10:45:09 -080051 ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous);
Eric Laurentf59a4b32013-07-02 11:15:41 -070052 if (ret != DEAD_OBJECT) {
Glenn Kasten1dc28b72012-04-24 10:01:03 -070053 break;
54 }
Eric Laurentf59a4b32013-07-02 11:15:41 -070055 ALOGW("SchedulingPolicyService died");
56 sMutex.lock();
57 sSchedulingPolicyService.clear();
58 sMutex.unlock();
Glenn Kasten1dc28b72012-04-24 10:01:03 -070059 }
Eric Laurentf59a4b32013-07-02 11:15:41 -070060 return ret;
Glenn Kasten1dc28b72012-04-24 10:01:03 -070061}
62
Chong Zhangfdd512a2019-11-22 11:03:14 -080063int requestCpusetBoost(bool enable, const sp<IBinder> &client)
Chong Zhang79d2b282018-04-17 14:14:31 -070064{
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 Hung41ccf7f2022-12-14 14:25:49 -080090int 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 Kasten1dc28b72012-04-24 10:01:03 -0700111} // namespace android