blob: 69106a19a18dc8062ea72030825df9bf3341471a [file] [log] [blame]
The Android Open Source Project5738f832012-12-12 16:00:35 -08001/******************************************************************************
2 *
3 * Copyright (C) 2012 Broadcom Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19/************************************************************************************
20 *
21 * Filename: bt_utils.c
22 *
23 * Description: Miscellaneous helper functions
24 *
25 *
26 ***********************************************************************************/
27
Chris Mantonf8027002015-03-12 09:22:48 -070028#define LOG_TAG "bt_utils"
Sharvil Nanavati44802762014-12-23 23:08:58 -080029
Marie Janssen49a86702015-07-08 11:48:57 -070030#include "bt_utils.h"
31
32#include <errno.h>
33#include <pthread.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <sys/resource.h>
37#include <unistd.h>
38
39#include <utils/ThreadDefs.h>
40#include <cutils/sched_policy.h>
41
Arman Uguray499282b2015-05-29 15:10:45 -070042// TODO(armansito): cutils/properties.h is only being used to pull-in runtime
43// settings on Android. Remove this conditional include once we have a generic
44// way to obtain system properties.
45#if !defined(OS_GENERIC)
The Android Open Source Project5738f832012-12-12 16:00:35 -080046#include <cutils/properties.h>
Arman Uguray499282b2015-05-29 15:10:45 -070047#endif // !defined(OS_GENERIC)
48
Chris Manton83e2c342014-09-29 21:37:44 -070049#include "bt_types.h"
Sharvil Nanavati95b74f22015-03-12 15:55:21 -070050#include "btcore/include/module.h"
Scott James Remnant47d68ee2015-04-02 15:22:29 -070051#include "osi/include/compat.h"
Sharvil Nanavati44802762014-12-23 23:08:58 -080052#include "osi/include/log.h"
The Android Open Source Project5738f832012-12-12 16:00:35 -080053
54/*******************************************************************************
55** Type definitions for callback functions
56********************************************************************************/
57static pthread_once_t g_DoSchedulingGroupOnce[TASK_HIGH_MAX];
58static BOOLEAN g_DoSchedulingGroup[TASK_HIGH_MAX];
59static pthread_mutex_t gIdxLock;
60static int g_TaskIdx;
Zhihai Xu4128e362014-01-08 11:45:17 -080061static int g_TaskIDs[TASK_HIGH_MAX];
62#define INVALID_TASK_ID (-1)
The Android Open Source Project5738f832012-12-12 16:00:35 -080063
Zach Johnson96363ff2014-09-24 17:27:25 -070064static future_t *init(void) {
65 int i;
66 pthread_mutexattr_t lock_attr;
The Android Open Source Project5738f832012-12-12 16:00:35 -080067
Zach Johnson96363ff2014-09-24 17:27:25 -070068 for(i = 0; i < TASK_HIGH_MAX; i++) {
69 g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
70 g_DoSchedulingGroup[i] = TRUE;
71 g_TaskIDs[i] = INVALID_TASK_ID;
72 }
73
74 pthread_mutexattr_init(&lock_attr);
75 pthread_mutex_init(&gIdxLock, &lock_attr);
76 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -080077}
78
Zach Johnson96363ff2014-09-24 17:27:25 -070079static future_t *clean_up(void) {
80 pthread_mutex_destroy(&gIdxLock);
81 return NULL;
The Android Open Source Project5738f832012-12-12 16:00:35 -080082}
83
Ian Coolidge1f81b642015-04-21 16:25:08 -070084EXPORT_SYMBOL const module_t bt_utils_module = {
Zach Johnson96363ff2014-09-24 17:27:25 -070085 .name = BT_UTILS_MODULE,
86 .init = init,
87 .start_up = NULL,
88 .shut_down = NULL,
89 .clean_up = clean_up,
90 .dependencies = {
91 NULL
92 }
93};
94
Arman Uguray499282b2015-05-29 15:10:45 -070095// TODO(armansito): Remove this conditional code once there is a generic way
96// to obtain system properties. System properties are only available on
97// Android. Don't do the following check if this is a generic build.
98#if !defined(OS_GENERIC)
The Android Open Source Project5738f832012-12-12 16:00:35 -080099/*****************************************************************************
100**
101** Function check_do_scheduling_group
102**
103** Description check if it is ok to change schedule group
104**
105** Returns void
106**
107*******************************************************************************/
108static void check_do_scheduling_group(void) {
109 char buf[PROPERTY_VALUE_MAX];
110 int len = property_get("debug.sys.noschedgroups", buf, "");
111 if (len > 0) {
112 int temp;
113 if (sscanf(buf, "%d", &temp) == 1) {
114 g_DoSchedulingGroup[g_TaskIdx] = temp == 0;
115 }
116 }
117}
Arman Uguray499282b2015-05-29 15:10:45 -0700118#endif // !defined(OS_GENERIC)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800119
120/*****************************************************************************
121**
122** Function raise_priority_a2dp
123**
124** Description Raise task priority for A2DP streaming
125**
126** Returns void
127**
128*******************************************************************************/
129void raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task) {
130 int rc = 0;
131 int tid = gettid();
Mattias Agren3eae42f2014-10-02 09:43:04 +0200132 int priority = ANDROID_PRIORITY_AUDIO;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800133
134 pthread_mutex_lock(&gIdxLock);
135 g_TaskIdx = high_task;
136
Arman Uguray499282b2015-05-29 15:10:45 -0700137 // TODO(armansito): Remove this conditional check once we find a solution
138 // for system/core on non-Android platforms.
139#if defined(OS_GENERIC)
140 rc = -1;
141#else // !defined(OS_GENERIC)
The Android Open Source Project5738f832012-12-12 16:00:35 -0800142 pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx], check_do_scheduling_group);
143 if (g_DoSchedulingGroup[g_TaskIdx]) {
144 // set_sched_policy does not support tid == 0
Andre Eisenbachf13db8a2014-07-11 16:57:24 -0700145 rc = set_sched_policy(tid, SP_AUDIO_SYS);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800146 }
Arman Uguray499282b2015-05-29 15:10:45 -0700147#endif // defined(OS_GENERIC)
148
Zhihai Xu4128e362014-01-08 11:45:17 -0800149 g_TaskIDs[high_task] = tid;
The Android Open Source Project5738f832012-12-12 16:00:35 -0800150 pthread_mutex_unlock(&gIdxLock);
151
152 if (rc) {
Marie Janssendb554582015-06-26 14:53:46 -0700153 LOG_WARN(LOG_TAG, "failed to change sched policy, tid %d, err: %d", tid, errno);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800154 }
155
Mattias Agren3eae42f2014-10-02 09:43:04 +0200156 // always use urgent priority for HCI worker thread until we can adjust
157 // its prio individually. All other threads can be dynamically adjusted voa
158 // adjust_priority_a2dp()
159
160 if (high_task == TASK_HIGH_HCI_WORKER)
161 priority = ANDROID_PRIORITY_URGENT_AUDIO;
162
163 if (setpriority(PRIO_PROCESS, tid, priority) < 0) {
Marie Janssendb554582015-06-26 14:53:46 -0700164 LOG_WARN(LOG_TAG, "failed to change priority tid: %d to %d", tid, priority);
The Android Open Source Project5738f832012-12-12 16:00:35 -0800165 }
166}
167
Zhihai Xu4128e362014-01-08 11:45:17 -0800168/*****************************************************************************
169**
170** Function adjust_priority_a2dp
171**
172** Description increase the a2dp consumer task priority temporarily when start
173** audio playing, to avoid overflow the audio packet queue, restore
174** the a2dp consumer task priority when stop audio playing.
175**
176** Returns void
177**
178*******************************************************************************/
179void adjust_priority_a2dp(int start) {
180 int priority = start ? ANDROID_PRIORITY_URGENT_AUDIO : ANDROID_PRIORITY_AUDIO;
181 int tid;
182 int i;
183
Mattias Agren3eae42f2014-10-02 09:43:04 +0200184 for (i = 0; i < TASK_HIGH_MAX; i++)
Zhihai Xu4128e362014-01-08 11:45:17 -0800185 {
186 tid = g_TaskIDs[i];
187 if (tid != INVALID_TASK_ID)
188 {
189 if (setpriority(PRIO_PROCESS, tid, priority) < 0)
190 {
Marie Janssendb554582015-06-26 14:53:46 -0700191 LOG_WARN(LOG_TAG, "failed to change priority tid: %d to %d", tid, priority);
Zhihai Xu4128e362014-01-08 11:45:17 -0800192 }
193 }
194 }
195}