Merge "Add tracking of parcel memory allocations." into lmp-mr1-dev

This commit is contained in:
Dianne Hackborn 2014-11-12 00:24:28 +00:00 committed by Android (Google) Code Review
commit ca69e8f2d0
4 changed files with 67 additions and 4 deletions

View File

@ -205,7 +205,11 @@ public:
// Explicitly close all file descriptors in the parcel. // Explicitly close all file descriptors in the parcel.
void closeFileDescriptors(); void closeFileDescriptors();
// Debugging: get metrics on current allocations.
static size_t getGlobalAllocSize();
static size_t getGlobalAllocCount();
private: private:
typedef void (*release_func)(Parcel* parcel, typedef void (*release_func)(Parcel* parcel,
const uint8_t* data, size_t dataSize, const uint8_t* data, size_t dataSize,

View File

@ -34,7 +34,12 @@ extern Vector<int32_t> gTextBuffers;
extern Mutex gProcessMutex; extern Mutex gProcessMutex;
extern sp<ProcessState> gProcess; extern sp<ProcessState> gProcess;
// For ServiceManager.cpp // For Parcel.cpp
extern Mutex gParcelGlobalAllocSizeLock;
extern size_t gParcelGlobalAllocSize;
extern size_t gParcelGlobalAllocCount;
// For IServiceManager.cpp
extern Mutex gDefaultServiceManagerLock; extern Mutex gDefaultServiceManagerLock;
extern sp<IServiceManager> gDefaultServiceManager; extern sp<IServiceManager> gDefaultServiceManager;
extern sp<IPermissionController> gPermissionController; extern sp<IPermissionController> gPermissionController;

View File

@ -35,6 +35,7 @@
#include <cutils/ashmem.h> #include <cutils/ashmem.h>
#include <private/binder/binder_module.h> #include <private/binder/binder_module.h>
#include <private/binder/Static.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
@ -48,6 +49,8 @@
#define LOG_REFS(...) #define LOG_REFS(...)
//#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__) //#define LOG_REFS(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
#define LOG_ALLOC(...)
//#define LOG_ALLOC(...) ALOG(LOG_DEBUG, "Parcel", __VA_ARGS__)
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@ -291,12 +294,24 @@ status_t unflatten_binder(const sp<ProcessState>& proc,
Parcel::Parcel() Parcel::Parcel()
{ {
LOG_ALLOC("Parcel %p: constructing", this);
initState(); initState();
} }
Parcel::~Parcel() Parcel::~Parcel()
{ {
freeDataNoInit(); freeDataNoInit();
LOG_ALLOC("Parcel %p: destroyed", this);
}
size_t Parcel::getGlobalAllocSize() {
AutoMutex _l(gParcelGlobalAllocSizeLock);
return gParcelGlobalAllocSize;
}
size_t Parcel::getGlobalAllocCount() {
AutoMutex _l(gParcelGlobalAllocSizeLock);
return gParcelGlobalAllocCount;
} }
const uint8_t* Parcel::data() const const uint8_t* Parcel::data() const
@ -1488,11 +1503,20 @@ void Parcel::freeData()
void Parcel::freeDataNoInit() void Parcel::freeDataNoInit()
{ {
if (mOwner) { if (mOwner) {
LOG_ALLOC("Parcel %p: freeing other owner data", this);
//ALOGI("Freeing data ref of %p (pid=%d)", this, getpid()); //ALOGI("Freeing data ref of %p (pid=%d)", this, getpid());
mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
} else { } else {
LOG_ALLOC("Parcel %p: freeing allocated data", this);
releaseObjects(); releaseObjects();
if (mData) free(mData); if (mData) {
LOG_ALLOC("Parcel %p: freeing with %zu capacity", this, mDataCapacity);
gParcelGlobalAllocSizeLock.lock();
gParcelGlobalAllocSize -= mDataCapacity;
gParcelGlobalAllocCount--;
gParcelGlobalAllocSizeLock.unlock();
free(mData);
}
if (mObjects) free(mObjects); if (mObjects) free(mObjects);
} }
} }
@ -1521,6 +1545,11 @@ status_t Parcel::restartWrite(size_t desired)
releaseObjects(); releaseObjects();
if (data) { if (data) {
LOG_ALLOC("Parcel %p: restart from %zu to %zu capacity", this, mDataCapacity, desired);
gParcelGlobalAllocSizeLock.lock();
gParcelGlobalAllocSize += desired;
gParcelGlobalAllocSize -= mDataCapacity;
gParcelGlobalAllocSizeLock.unlock();
mData = data; mData = data;
mDataCapacity = desired; mDataCapacity = desired;
} }
@ -1600,6 +1629,12 @@ status_t Parcel::continueWrite(size_t desired)
mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie); mOwner(this, mData, mDataSize, mObjects, mObjectsSize, mOwnerCookie);
mOwner = NULL; mOwner = NULL;
LOG_ALLOC("Parcel %p: taking ownership of %zu capacity", this, desired);
gParcelGlobalAllocSizeLock.lock();
gParcelGlobalAllocSize += desired;
gParcelGlobalAllocCount++;
gParcelGlobalAllocSizeLock.unlock();
mData = data; mData = data;
mObjects = objects; mObjects = objects;
mDataSize = (mDataSize < desired) ? mDataSize : desired; mDataSize = (mDataSize < desired) ? mDataSize : desired;
@ -1634,6 +1669,12 @@ status_t Parcel::continueWrite(size_t desired)
if (desired > mDataCapacity) { if (desired > mDataCapacity) {
uint8_t* data = (uint8_t*)realloc(mData, desired); uint8_t* data = (uint8_t*)realloc(mData, desired);
if (data) { if (data) {
LOG_ALLOC("Parcel %p: continue from %zu to %zu capacity", this, mDataCapacity,
desired);
gParcelGlobalAllocSizeLock.lock();
gParcelGlobalAllocSize += desired;
gParcelGlobalAllocSize -= mDataCapacity;
gParcelGlobalAllocSizeLock.unlock();
mData = data; mData = data;
mDataCapacity = desired; mDataCapacity = desired;
} else if (desired > mDataCapacity) { } else if (desired > mDataCapacity) {
@ -1664,6 +1705,12 @@ status_t Parcel::continueWrite(size_t desired)
ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired); ALOGE("continueWrite: %zu/%p/%zu/%zu", mDataCapacity, mObjects, mObjectsCapacity, desired);
} }
LOG_ALLOC("Parcel %p: allocating with %zu capacity", this, desired);
gParcelGlobalAllocSizeLock.lock();
gParcelGlobalAllocSize += desired;
gParcelGlobalAllocCount++;
gParcelGlobalAllocSizeLock.unlock();
mData = data; mData = data;
mDataSize = mDataPos = 0; mDataSize = mDataPos = 0;
ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize); ALOGV("continueWrite Setting data size of %p to %zu", this, mDataSize);
@ -1676,6 +1723,7 @@ status_t Parcel::continueWrite(size_t desired)
void Parcel::initState() void Parcel::initState()
{ {
LOG_ALLOC("Parcel %p: initState", this);
mError = NO_ERROR; mError = NO_ERROR;
mData = 0; mData = 0;
mDataSize = 0; mDataSize = 0;

View File

@ -90,7 +90,13 @@ public:
static LibBinderIPCtStatics gIPCStatics; static LibBinderIPCtStatics gIPCStatics;
// ------------ ServiceManager.cpp // ------------ Parcel.cpp
Mutex gParcelGlobalAllocSizeLock;
size_t gParcelGlobalAllocSize = 0;
size_t gParcelGlobalAllocCount = 0;
// ------------ IServiceManager.cpp
Mutex gDefaultServiceManagerLock; Mutex gDefaultServiceManagerLock;
sp<IServiceManager> gDefaultServiceManager; sp<IServiceManager> gDefaultServiceManager;