Added IPCThreadState::blockUntilThreadAvailable() method.

Will be used by the system_server watchdog to monitor the
availability of binder threads in the process to handle
incoming IPC requests.

Bug: 19297165
Change-Id: I39175f3869ad14da5620fddb47f454e6e4ee2b25
This commit is contained in:
Wale Ogunwale 2015-04-13 16:16:10 -07:00
parent e88e366963
commit 376b822728
4 changed files with 54 additions and 13 deletions

View File

@ -84,6 +84,10 @@ public:
// the background. // the background.
static void disableBackgroundScheduling(bool disable); static void disableBackgroundScheduling(bool disable);
// Call blocks until the number of executing binder threads is less than
// the maximum number of binder threads threads allowed for this process.
void blockUntilThreadAvailable();
private: private:
IPCThreadState(); IPCThreadState();
~IPCThreadState(); ~IPCThreadState();

View File

@ -24,6 +24,8 @@
#include <utils/threads.h> #include <utils/threads.h>
#include <pthread.h>
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
namespace android { namespace android {
@ -82,6 +84,14 @@ private:
int mDriverFD; int mDriverFD;
void* mVMStart; void* mVMStart;
// Protects thread count variable below.
pthread_mutex_t mThreadCountLock;
pthread_cond_t mThreadCountDecrement;
// Number of binder threads current executing a command.
size_t mExecutingThreadsCount;
// Maximum number for binder threads allowed for this process.
size_t mMaxThreads;
mutable Mutex mLock; // protects everything below. mutable Mutex mLock; // protects everything below.
Vector<handle_entry>mHandleToObject; Vector<handle_entry>mHandleToObject;

View File

@ -399,6 +399,17 @@ void IPCThreadState::flushCommands()
talkWithDriver(false); talkWithDriver(false);
} }
void IPCThreadState::blockUntilThreadAvailable()
{
pthread_mutex_lock(&mProcess->mThreadCountLock);
while (mProcess->mExecutingThreadsCount >= mProcess->mMaxThreads) {
ALOGW("Waiting for thread to be free. mExecutingThreadsCount=%i mMaxThreads=%i\n",
mProcess->mExecutingThreadsCount, mProcess->mMaxThreads);
pthread_cond_wait(&mProcess->mThreadCountDecrement, &mProcess->mThreadCountLock);
}
pthread_mutex_unlock(&mProcess->mThreadCountLock);
}
status_t IPCThreadState::getAndExecuteCommand() status_t IPCThreadState::getAndExecuteCommand()
{ {
status_t result; status_t result;
@ -414,8 +425,17 @@ status_t IPCThreadState::getAndExecuteCommand()
<< getReturnString(cmd) << endl; << getReturnString(cmd) << endl;
} }
pthread_mutex_lock(&mProcess->mThreadCountLock);
mProcess->mExecutingThreadsCount++;
pthread_mutex_unlock(&mProcess->mThreadCountLock);
result = executeCommand(cmd); result = executeCommand(cmd);
pthread_mutex_lock(&mProcess->mThreadCountLock);
mProcess->mExecutingThreadsCount--;
pthread_cond_broadcast(&mProcess->mThreadCountDecrement);
pthread_mutex_unlock(&mProcess->mThreadCountLock);
// After executing the command, ensure that the thread is returned to the // After executing the command, ensure that the thread is returned to the
// foreground cgroup before rejoining the pool. The driver takes care of // foreground cgroup before rejoining the pool. The driver takes care of
// restoring the priority, but doesn't do anything with cgroups so we // restoring the priority, but doesn't do anything with cgroups so we

View File

@ -42,6 +42,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2)) #define BINDER_VM_SIZE ((1*1024*1024) - (4096 *2))
#define DEFAULT_MAX_BINDER_THREADS 15
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -294,7 +295,9 @@ void ProcessState::spawnPooledThread(bool isMain)
status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) { status_t ProcessState::setThreadPoolMaxThreadCount(size_t maxThreads) {
status_t result = NO_ERROR; status_t result = NO_ERROR;
if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) == -1) { if (ioctl(mDriverFD, BINDER_SET_MAX_THREADS, &maxThreads) != -1) {
mMaxThreads = maxThreads;
} else {
result = -errno; result = -errno;
ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result)); ALOGE("Binder ioctl to set max threads failed: %s", strerror(-result));
} }
@ -322,7 +325,7 @@ static int open_driver()
close(fd); close(fd);
fd = -1; fd = -1;
} }
size_t maxThreads = 15; size_t maxThreads = DEFAULT_MAX_BINDER_THREADS;
result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads); result = ioctl(fd, BINDER_SET_MAX_THREADS, &maxThreads);
if (result == -1) { if (result == -1) {
ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno)); ALOGE("Binder ioctl to set max threads failed: %s", strerror(errno));
@ -336,6 +339,10 @@ static int open_driver()
ProcessState::ProcessState() ProcessState::ProcessState()
: mDriverFD(open_driver()) : mDriverFD(open_driver())
, mVMStart(MAP_FAILED) , mVMStart(MAP_FAILED)
, mThreadCountLock(PTHREAD_MUTEX_INITIALIZER)
, mThreadCountDecrement(PTHREAD_COND_INITIALIZER)
, mExecutingThreadsCount(0)
, mMaxThreads(DEFAULT_MAX_BINDER_THREADS)
, mManagesContexts(false) , mManagesContexts(false)
, mBinderContextCheckFunc(NULL) , mBinderContextCheckFunc(NULL)
, mBinderContextUserData(NULL) , mBinderContextUserData(NULL)