From 5e1b7e1b0a23a47ab76ed022284fd40b67ebe503 Mon Sep 17 00:00:00 2001 From: Parth Sane Date: Fri, 29 Nov 2024 10:40:41 +0000 Subject: Remove static list in libbinder for caching This adds a new flag to identify Lazy services. This flag is set when addService is called from LazyServiceRegistrar. This flag is then used by libbinder in a client. When getService is called, libbinder decides if the binder should be cached or not using this flag. Doc: go/libbinder-cache-v2 Flag: RELEASE_LIBBINDER_REMOVE_STATIC_LIST Test: atest binderCacheUnitTest Bug: 333854840 Change-Id: I5fff0140f635dddb4f5a6d618f4e9abace6feb54 --- libs/binder/BackendUnifiedServiceManager.cpp | 51 +++++++++++++++++++++------- 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'libs/binder/BackendUnifiedServiceManager.cpp') diff --git a/libs/binder/BackendUnifiedServiceManager.cpp b/libs/binder/BackendUnifiedServiceManager.cpp index 123dfd2c05..03d687a9cc 100644 --- a/libs/binder/BackendUnifiedServiceManager.cpp +++ b/libs/binder/BackendUnifiedServiceManager.cpp @@ -16,6 +16,7 @@ #include "BackendUnifiedServiceManager.h" #include +#include #include #if defined(__BIONIC__) && !defined(__ANDROID_VNDK__) @@ -36,6 +37,12 @@ constexpr bool kUseCacheInAddService = true; constexpr bool kUseCacheInAddService = false; #endif +#ifdef LIBBINDER_REMOVE_CACHE_STATIC_LIST +constexpr bool kRemoveStaticList = true; +#else +constexpr bool kRemoveStaticList = false; +#endif + using AidlServiceManager = android::os::IServiceManager; using android::os::IAccessor; using binder::Status; @@ -110,6 +117,13 @@ static const char* kStaticCachableList[] = { // go/keep-sorted end }; +os::ServiceWithMetadata createServiceWithMetadata(const sp& service, bool isLazyService) { + os::ServiceWithMetadata out = os::ServiceWithMetadata(); + out.service = service; + out.isLazyService = isLazyService; + return out; +} + bool BinderCacheWithInvalidation::isClientSideCachingEnabled(const std::string& serviceName) { sp self = ProcessState::selfOrNull(); if (!self || self->getThreadPoolMaxTotalThreadCount() <= 0) { @@ -132,15 +146,21 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, return Status::ok(); } - if (service.getTag() == os::Service::Tag::binder) { - return updateCache(serviceName, service.get()); + if (service.getTag() == os::Service::Tag::serviceWithMetadata) { + auto serviceWithMetadata = service.get(); + return updateCache(serviceName, serviceWithMetadata.service, + serviceWithMetadata.isLazyService); } return Status::ok(); } Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, - const sp& binder) { + const sp& binder, bool isServiceLazy) { std::string traceStr; + // Don't cache if service is lazy + if (kRemoveStaticList && isServiceLazy) { + return Status::ok(); + } if (atrace_is_tag_enabled(ATRACE_TAG_AIDL)) { traceStr = "BinderCacheWithInvalidation::updateCache : " + serviceName; } @@ -153,7 +173,9 @@ Status BackendUnifiedServiceManager::updateCache(const std::string& serviceName, binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, "BinderCacheWithInvalidation::updateCache failed: " "isBinderAlive_false"); - } else if (mCacheForGetService->isClientSideCachingEnabled(serviceName)) { + } + // If we reach here with kRemoveStaticList=true then we know service isn't lazy + else if (kRemoveStaticList || mCacheForGetService->isClientSideCachingEnabled(serviceName)) { binder::ScopedTrace aidlTrace(ATRACE_TAG_AIDL, "BinderCacheWithInvalidation::updateCache successful"); return mCacheForGetService->setItem(serviceName, binder); @@ -173,7 +195,7 @@ bool BackendUnifiedServiceManager::returnIfCached(const std::string& serviceName sp item = mCacheForGetService->getItem(serviceName); // TODO(b/363177618): Enable caching for binders which are always null. if (item != nullptr && item->isBinderAlive()) { - *_out = os::Service::make(item); + *_out = createServiceWithMetadata(item, false); return true; } return false; @@ -188,7 +210,7 @@ Status BackendUnifiedServiceManager::getService(const ::std::string& name, sp* _aidl_return) { os::Service service; Status status = getService2(name, &service); - *_aidl_return = service.get(); + *_aidl_return = service.get().service; return status; } @@ -227,14 +249,16 @@ Status BackendUnifiedServiceManager::checkService(const ::std::string& name, os: Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, const os::Service& in, os::Service* _out) { switch (in.getTag()) { - case os::Service::Tag::binder: { - if (in.get() == nullptr) { + case os::Service::Tag::serviceWithMetadata: { + auto serviceWithMetadata = in.get(); + if (serviceWithMetadata.service == nullptr) { // failed to find a service. Check to see if we have any local // injected Accessors for this service. os::Service accessor; Status status = getInjectedAccessor(name, &accessor); if (!status.isOk()) { - *_out = os::Service::make(nullptr); + *_out = os::Service::make( + createServiceWithMetadata(nullptr, false)); return status; } if (accessor.getTag() == os::Service::Tag::accessor && @@ -255,7 +279,8 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, sp accessor = interface_cast(accessorBinder); if (accessor == nullptr) { ALOGE("Service#accessor doesn't have accessor. VM is maybe starting..."); - *_out = os::Service::make(nullptr); + *_out = os::Service::make( + createServiceWithMetadata(nullptr, false)); return Status::ok(); } auto request = [=] { @@ -276,7 +301,8 @@ Status BackendUnifiedServiceManager::toBinderService(const ::std::string& name, return Status::fromStatusT(status); } session->setSessionSpecificRoot(accessorBinder); - *_out = os::Service::make(session->getRootObject()); + *_out = os::Service::make( + createServiceWithMetadata(session->getRootObject(), false)); return Status::ok(); } default: { @@ -291,7 +317,8 @@ Status BackendUnifiedServiceManager::addService(const ::std::string& name, Status status = mTheRealServiceManager->addService(name, service, allowIsolated, dumpPriority); // mEnableAddServiceCache is true by default. if (kUseCacheInAddService && mEnableAddServiceCache && status.isOk()) { - return updateCache(name, service); + return updateCache(name, service, + dumpPriority & android::os::IServiceManager::FLAG_IS_LAZY_SERVICE); } return status; } -- cgit v1.2.3-59-g8ed1b