Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 426b0f5a authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "BufferPool: limit number of idle buffers in caches" into mainline-prod

parents ab5299e6 ea5baf2b
Loading
Loading
Loading
Loading
+9 −4
Original line number Diff line number Diff line
@@ -39,6 +39,8 @@ namespace {

    static constexpr size_t kMinAllocBytesForEviction = 1024*1024*15;
    static constexpr size_t kMinBufferCountForEviction = 25;
    static constexpr size_t kMaxUnusedBufferCount = 64;
    static constexpr size_t kUnusedBufferCountTarget = kMaxUnusedBufferCount - 16;

    static constexpr nsecs_t kEvictGranularityNs = 1000000000; // 1 sec
    static constexpr nsecs_t kEvictDurationNs = 5000000000; // 5 secs
@@ -724,9 +726,11 @@ ResultStatus Accessor::Impl::BufferPool::addNewBuffer(
}

void Accessor::Impl::BufferPool::cleanUp(bool clearCache) {
    if (clearCache || mTimestampUs > mLastCleanUpUs + kCleanUpDurationUs) {
    if (clearCache || mTimestampUs > mLastCleanUpUs + kCleanUpDurationUs ||
            mStats.buffersNotInUse() > kMaxUnusedBufferCount) {
        mLastCleanUpUs = mTimestampUs;
        if (mTimestampUs > mLastLogUs + kLogDurationUs) {
        if (mTimestampUs > mLastLogUs + kLogDurationUs ||
                mStats.buffersNotInUse() > kMaxUnusedBufferCount) {
            mLastLogUs = mTimestampUs;
            ALOGD("bufferpool2 %p : %zu(%zu size) total buffers - "
                  "%zu(%zu size) used buffers - %zu/%zu (recycle/alloc) - "
@@ -737,8 +741,9 @@ void Accessor::Impl::BufferPool::cleanUp(bool clearCache) {
                  mStats.mTotalFetches, mStats.mTotalTransfers);
        }
        for (auto freeIt = mFreeBuffers.begin(); freeIt != mFreeBuffers.end();) {
            if (!clearCache && (mStats.mSizeCached < kMinAllocBytesForEviction
                    || mBuffers.size() < kMinBufferCountForEviction)) {
            if (!clearCache && mStats.buffersNotInUse() <= kUnusedBufferCountTarget &&
                    (mStats.mSizeCached < kMinAllocBytesForEviction ||
                     mBuffers.size() < kMinBufferCountForEviction)) {
                break;
            }
            auto it = mBuffers.find(*freeIt);
+6 −0
Original line number Diff line number Diff line
@@ -193,6 +193,12 @@ private:
                : mSizeCached(0), mBuffersCached(0), mSizeInUse(0), mBuffersInUse(0),
                  mTotalAllocations(0), mTotalRecycles(0), mTotalTransfers(0), mTotalFetches(0) {}

            /// # of currently unused buffers
            size_t buffersNotInUse() const {
                ALOG_ASSERT(mBuffersCached >= mBuffersInUse);
                return mBuffersCached - mBuffersInUse;
            }

            /// A new buffer is allocated on an allocation request.
            void onBufferAllocated(size_t allocSize) {
                mSizeCached += allocSize;
+10 −2
Original line number Diff line number Diff line
@@ -32,6 +32,8 @@ namespace implementation {
static constexpr int64_t kReceiveTimeoutUs = 1000000; // 100ms
static constexpr int kPostMaxRetry = 3;
static constexpr int kCacheTtlUs = 1000000; // TODO: tune
static constexpr size_t kMaxCachedBufferCount = 64;
static constexpr size_t kCachedBufferCountTarget = kMaxCachedBufferCount - 16;

class BufferPoolClient::Impl
        : public std::enable_shared_from_this<BufferPoolClient::Impl> {
@@ -136,6 +138,10 @@ private:
            --mActive;
            mLastChangeUs = getTimestampNow();
        }

        int cachedBufferCount() const {
            return mBuffers.size() - mActive;
        }
    } mCache;

    // FMQ - release notifier
@@ -668,10 +674,12 @@ bool BufferPoolClient::Impl::syncReleased(uint32_t messageId) {
// should have mCache.mLock
void BufferPoolClient::Impl::evictCaches(bool clearCache) {
    int64_t now = getTimestampNow();
    if (now >= mLastEvictCacheUs + kCacheTtlUs || clearCache) {
    if (now >= mLastEvictCacheUs + kCacheTtlUs ||
            clearCache || mCache.cachedBufferCount() > kMaxCachedBufferCount) {
        size_t evicted = 0;
        for (auto it = mCache.mBuffers.begin(); it != mCache.mBuffers.end();) {
            if (!it->second->hasCache() && (it->second->expire() || clearCache)) {
            if (!it->second->hasCache() && (it->second->expire() ||
                        clearCache || mCache.cachedBufferCount() > kCachedBufferCountTarget)) {
                it = mCache.mBuffers.erase(it);
                ++evicted;
            } else {