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
17 #define LOG_TAG "SchedulingPolicyService"
18 //#define LOG_NDEBUG 0
19
20 #include <binder/IServiceManager.h>
21 #include <cutils/properties.h>
22 #include <utils/Mutex.h>
23 #include "ISchedulingPolicyService.h"
24 #include "mediautils/SchedulingPolicyService.h"
25
26 namespace android {
27
28 static sp<ISchedulingPolicyService> sSchedulingPolicyService;
29 static const String16 _scheduling_policy("scheduling_policy");
30 static Mutex sMutex;
31
requestPriority(pid_t pid,pid_t tid,int32_t prio,bool isForApp,bool asynchronous)32 int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous)
33 {
34 // FIXME merge duplicated code related to service lookup, caching, and error recovery
35 int ret;
36 for (;;) {
37 sMutex.lock();
38 sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
39 sMutex.unlock();
40 if (sps == 0) {
41 sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
42 if (binder == 0) {
43 sleep(1);
44 continue;
45 }
46 sps = interface_cast<ISchedulingPolicyService>(binder);
47 sMutex.lock();
48 sSchedulingPolicyService = sps;
49 sMutex.unlock();
50 }
51 ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous);
52 if (ret != DEAD_OBJECT) {
53 break;
54 }
55 ALOGW("SchedulingPolicyService died");
56 sMutex.lock();
57 sSchedulingPolicyService.clear();
58 sMutex.unlock();
59 }
60 return ret;
61 }
62
requestCpusetBoost(bool enable,const sp<IBinder> & client)63 int requestCpusetBoost(bool enable, const sp<IBinder> &client)
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
requestSpatializerPriority(pid_t pid,pid_t tid)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
111 } // namespace android
112