2424
2525namespace android {
2626
27+ #ifdef LIBBINDER_CLIENT_CACHE
28+ constexpr bool kUseCache = true ;
29+ #else
30+ constexpr bool kUseCache = false ;
31+ #endif
32+
2733using AidlServiceManager = android::os::IServiceManager;
2834using IAccessor = android::os::IAccessor;
2935
36+ static const char * kStaticCachableList [] = {
37+ " activity" ,
38+ " android.hardware.thermal.IThermal/default" ,
39+ " android.hardware.power.IPower/default" ,
40+ " android.frameworks.stats.IStats/default" ,
41+ " android.system.suspend.ISystemSuspend/default" ,
42+ " appops" ,
43+ " audio" ,
44+ " batterystats" ,
45+ " carrier_config" ,
46+ " connectivity" ,
47+ " content_capture" ,
48+ " device_policy" ,
49+ " display" ,
50+ " dropbox" ,
51+ " econtroller" ,
52+ " isub" ,
53+ " legacy_permission" ,
54+ " location" ,
55+ " media.extractor" ,
56+ " media.metrics" ,
57+ " media.player" ,
58+ " media.resource_manager" ,
59+ " netd_listener" ,
60+ " netstats" ,
61+ " network_management" ,
62+ " nfc" ,
63+ " package_native" ,
64+ " performance_hint" ,
65+ " permission" ,
66+ " permissionmgr" ,
67+ " permission_checker" ,
68+ " phone" ,
69+ " platform_compat" ,
70+ " power" ,
71+ " role" ,
72+ " sensorservice" ,
73+ " statscompanion" ,
74+ " telephony.registry" ,
75+ " thermalservice" ,
76+ " time_detector" ,
77+ " trust" ,
78+ " uimode" ,
79+ " virtualdevice" ,
80+ " virtualdevice_native" ,
81+ " webviewupdate" ,
82+ };
83+
84+ bool BinderCacheWithInvalidation::isClientSideCachingEnabled (const std::string& serviceName) {
85+ if (ProcessState::self ()->getThreadPoolMaxTotalThreadCount () <= 0 ) {
86+ ALOGW (" Thread Pool max thread count is 0. Cannot cache binder as linkToDeath cannot be "
87+ " implemented. serviceName: %s" ,
88+ serviceName.c_str ());
89+ return false ;
90+ }
91+ for (const char * name : kStaticCachableList ) {
92+ if (name == serviceName) {
93+ return true ;
94+ }
95+ }
96+ return false ;
97+ }
98+
99+ binder::Status BackendUnifiedServiceManager::updateCache (const std::string& serviceName,
100+ const os::Service& service) {
101+ if (!kUseCache ) {
102+ return binder::Status::ok ();
103+ }
104+ if (service.getTag () == os::Service::Tag::binder) {
105+ sp<IBinder> binder = service.get <os::Service::Tag::binder>();
106+ if (binder && mCacheForGetService ->isClientSideCachingEnabled (serviceName) &&
107+ binder->isBinderAlive ()) {
108+ return mCacheForGetService ->setItem (serviceName, binder);
109+ }
110+ }
111+ return binder::Status::ok ();
112+ }
113+
114+ bool BackendUnifiedServiceManager::returnIfCached (const std::string& serviceName,
115+ os::Service* _out) {
116+ if (!kUseCache ) {
117+ return false ;
118+ }
119+ sp<IBinder> item = mCacheForGetService ->getItem (serviceName);
120+ // TODO(b/363177618): Enable caching for binders which are always null.
121+ if (item != nullptr && item->isBinderAlive ()) {
122+ *_out = os::Service::make<os::Service::Tag::binder>(item);
123+ return true ;
124+ }
125+ return false ;
126+ }
127+
30128BackendUnifiedServiceManager::BackendUnifiedServiceManager (const sp<AidlServiceManager>& impl)
31- : mTheRealServiceManager (impl) {}
129+ : mTheRealServiceManager (impl) {
130+ mCacheForGetService = std::make_shared<BinderCacheWithInvalidation>();
131+ }
32132
33133sp<AidlServiceManager> BackendUnifiedServiceManager::getImpl () {
34134 return mTheRealServiceManager ;
@@ -44,20 +144,34 @@ binder::Status BackendUnifiedServiceManager::getService(const ::std::string& nam
44144
45145binder::Status BackendUnifiedServiceManager::getService2 (const ::std::string& name,
46146 os::Service* _out) {
147+ if (returnIfCached (name, _out)) {
148+ return binder::Status::ok ();
149+ }
47150 os::Service service;
48151 binder::Status status = mTheRealServiceManager ->getService2 (name, &service);
152+
49153 if (status.isOk ()) {
50- return toBinderService (name, service, _out);
154+ status = toBinderService (name, service, _out);
155+ if (status.isOk ()) {
156+ return updateCache (name, service);
157+ }
51158 }
52159 return status;
53160}
54161
55162binder::Status BackendUnifiedServiceManager::checkService (const ::std::string& name,
56163 os::Service* _out) {
57164 os::Service service;
165+ if (returnIfCached (name, _out)) {
166+ return binder::Status::ok ();
167+ }
168+
58169 binder::Status status = mTheRealServiceManager ->checkService (name, &service);
59170 if (status.isOk ()) {
60- return toBinderService (name, service, _out);
171+ status = toBinderService (name, service, _out);
172+ if (status.isOk ()) {
173+ return updateCache (name, service);
174+ }
61175 }
62176 return status;
63177}
0 commit comments