Merge "binderClearBufTest: remove unnecessary flush" am: 253d914cc4
Original change: https://android-review.googlesource.com/c/platform/frameworks/native/+/1552568
MUST ONLY BE SUBMITTED BY AUTOMERGER
Change-Id: I40f595a237831db28b1058468a8760077a4728ca
diff --git a/libs/binder/LazyServiceRegistrar.cpp b/libs/binder/LazyServiceRegistrar.cpp
index ff1fbec..68ea8a6 100644
--- a/libs/binder/LazyServiceRegistrar.cpp
+++ b/libs/binder/LazyServiceRegistrar.cpp
@@ -30,20 +30,15 @@
using AidlServiceManager = android::os::IServiceManager;
-class ClientCounterCallback : public ::android::os::BnClientCallback {
+class ClientCounterCallbackImpl : public ::android::os::BnClientCallback {
public:
- ClientCounterCallback() : mNumConnectedServices(0), mForcePersist(false) {}
+ ClientCounterCallbackImpl() : mNumConnectedServices(0), mForcePersist(false) {}
bool registerService(const sp<IBinder>& service, const std::string& name,
bool allowIsolated, int dumpFlags);
-
- /**
- * Set a flag to prevent services from automatically shutting down
- */
void forcePersist(bool persist);
- void setActiveServicesCountCallback(const std::function<bool(int)>&
- activeServicesCountCallback);
+ void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
bool tryUnregister();
@@ -53,22 +48,6 @@
Status onClients(const sp<IBinder>& service, bool clients) override;
private:
- struct Service {
- sp<IBinder> service;
- bool allowIsolated;
- int dumpFlags;
-
- // whether, based on onClients calls, we know we have a client for this
- // service or not
- bool clients = false;
- bool registered = true;
- };
-
- /**
- * Looks up a service guaranteed to be registered (service from onClients).
- */
- std::map<std::string, Service>::iterator assertRegisteredService(const sp<IBinder>& service);
-
/**
* Unregisters all services that we can. If we can't unregister all, re-register other
* services.
@@ -83,19 +62,55 @@
*/
void maybeTryShutdown();
- // count of services with clients
+ /*
+ * Counter of the number of services that currently have at least one client.
+ */
size_t mNumConnectedServices;
- // map of registered names and services
+ // previous value passed to the active services callback
+ std::optional<bool> mPreviousHasClients;
+
+ struct Service {
+ sp<IBinder> service;
+ bool allowIsolated;
+ int dumpFlags;
+
+ bool registered = true;
+ };
+ /**
+ * Map of registered names and services
+ */
std::map<std::string, Service> mRegisteredServices;
bool mForcePersist;
- // Callback used to report the number of services with clients
- std::function<bool(int)> mActiveServicesCountCallback;
+ // Callback used to report if there are services with clients
+ std::function<bool(bool)> mActiveServicesCallback;
};
-bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
+class ClientCounterCallback {
+public:
+ ClientCounterCallback();
+
+ bool registerService(const sp<IBinder>& service, const std::string& name,
+ bool allowIsolated, int dumpFlags);
+
+ /**
+ * Set a flag to prevent services from automatically shutting down
+ */
+ void forcePersist(bool persist);
+
+ void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
+
+ bool tryUnregister();
+
+ void reRegister();
+
+private:
+ sp<ClientCounterCallbackImpl> mImpl;
+};
+
+bool ClientCounterCallbackImpl::registerService(const sp<IBinder>& service, const std::string& name,
bool allowIsolated, int dumpFlags) {
auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
@@ -115,37 +130,21 @@
}
// Only add this when a service is added for the first time, as it is not removed
- mRegisteredServices[name] = {
- .service = service,
- .allowIsolated = allowIsolated,
- .dumpFlags = dumpFlags
- };
+ mRegisteredServices[name] = {service, allowIsolated, dumpFlags};
}
return true;
}
-std::map<std::string, ClientCounterCallback::Service>::iterator ClientCounterCallback::assertRegisteredService(const sp<IBinder>& service) {
- LOG_ALWAYS_FATAL_IF(service == nullptr, "Got onClients callback for null service");
- for (auto it = mRegisteredServices.begin(); it != mRegisteredServices.end(); ++it) {
- auto const& [name, registered] = *it;
- (void) name;
- if (registered.service != service) continue;
- return it;
- }
- LOG_ALWAYS_FATAL("Got callback on service which we did not register: %s", String8(service->getInterfaceDescriptor()).c_str());
- __builtin_unreachable();
-}
-
-void ClientCounterCallback::forcePersist(bool persist) {
+void ClientCounterCallbackImpl::forcePersist(bool persist) {
mForcePersist = persist;
- if (!mForcePersist && mNumConnectedServices == 0) {
+ if (!mForcePersist) {
// Attempt a shutdown in case the number of clients hit 0 while the flag was on
maybeTryShutdown();
}
}
-bool ClientCounterCallback::tryUnregister() {
+bool ClientCounterCallbackImpl::tryUnregister() {
auto manager = interface_cast<AidlServiceManager>(asBinder(defaultServiceManager()));
for (auto& [name, entry] : mRegisteredServices) {
@@ -161,7 +160,7 @@
return true;
}
-void ClientCounterCallback::reRegister() {
+void ClientCounterCallbackImpl::reRegister() {
for (auto& [name, entry] : mRegisteredServices) {
// re-register entry if not already registered
if (entry.registered) {
@@ -178,15 +177,19 @@
}
}
-void ClientCounterCallback::maybeTryShutdown() {
+void ClientCounterCallbackImpl::maybeTryShutdown() {
if (mForcePersist) {
ALOGI("Shutdown prevented by forcePersist override flag.");
return;
}
bool handledInCallback = false;
- if (mActiveServicesCountCallback != nullptr) {
- handledInCallback = mActiveServicesCountCallback(mNumConnectedServices);
+ if (mActiveServicesCallback != nullptr) {
+ bool hasClients = mNumConnectedServices != 0;
+ if (hasClients != mPreviousHasClients) {
+ handledInCallback = mActiveServicesCallback(hasClients);
+ mPreviousHasClients = hasClients;
+ }
}
// If there is no callback defined or the callback did not handle this
@@ -201,46 +204,61 @@
* onClients is oneway, so no need to worry about multi-threading. Note that this means multiple
* invocations could occur on different threads however.
*/
-Status ClientCounterCallback::onClients(const sp<IBinder>& service, bool clients) {
- auto & [name, registered] = *assertRegisteredService(service);
- if (registered.clients == clients) {
- LOG_ALWAYS_FATAL("Process already thought %s had clients: %d but servicemanager has "
- "notified has clients: %d", name.c_str(), registered.clients, clients);
- }
- registered.clients = clients;
-
- // update cache count of clients
- {
- size_t numWithClients = 0;
- for (const auto& [name, registered] : mRegisteredServices) {
- (void) name;
- if (registered.clients) numWithClients++;
- }
- mNumConnectedServices = numWithClients;
+Status ClientCounterCallbackImpl::onClients(const sp<IBinder>& service, bool clients) {
+ if (clients) {
+ mNumConnectedServices++;
+ } else {
+ mNumConnectedServices--;
}
ALOGI("Process has %zu (of %zu available) client(s) in use after notification %s has clients: %d",
- mNumConnectedServices, mRegisteredServices.size(), name.c_str(), clients);
+ mNumConnectedServices, mRegisteredServices.size(),
+ String8(service->getInterfaceDescriptor()).string(), clients);
maybeTryShutdown();
-
return Status::ok();
}
-void ClientCounterCallback::tryShutdown() {
- ALOGI("Trying to shut down the service. No clients in use for any service in process.");
+ void ClientCounterCallbackImpl::tryShutdown() {
+ ALOGI("Trying to shut down the service. No clients in use for any service in process.");
if (tryUnregister()) {
- ALOGI("Unregistered all clients and exiting");
- exit(EXIT_SUCCESS);
- }
+ ALOGI("Unregistered all clients and exiting");
+ exit(EXIT_SUCCESS);
+ }
reRegister();
}
-void ClientCounterCallback::setActiveServicesCountCallback(const std::function<bool(int)>&
- activeServicesCountCallback) {
- mActiveServicesCountCallback = activeServicesCountCallback;
+void ClientCounterCallbackImpl::setActiveServicesCallback(const std::function<bool(bool)>&
+ activeServicesCallback) {
+ mActiveServicesCallback = activeServicesCallback;
+}
+
+ClientCounterCallback::ClientCounterCallback() {
+ mImpl = new ClientCounterCallbackImpl();
+}
+
+bool ClientCounterCallback::registerService(const sp<IBinder>& service, const std::string& name,
+ bool allowIsolated, int dumpFlags) {
+ return mImpl->registerService(service, name, allowIsolated, dumpFlags);
+}
+
+void ClientCounterCallback::forcePersist(bool persist) {
+ mImpl->forcePersist(persist);
+}
+
+void ClientCounterCallback::setActiveServicesCallback(const std::function<bool(bool)>&
+ activeServicesCallback) {
+ mImpl->setActiveServicesCallback(activeServicesCallback);
+}
+
+bool ClientCounterCallback::tryUnregister() {
+ return mImpl->tryUnregister();
+}
+
+void ClientCounterCallback::reRegister() {
+ mImpl->reRegister();
}
} // namespace internal
@@ -266,9 +284,9 @@
mClientCC->forcePersist(persist);
}
-void LazyServiceRegistrar::setActiveServicesCountCallback(const std::function<bool(int)>&
- activeServicesCountCallback) {
- mClientCC->setActiveServicesCountCallback(activeServicesCountCallback);
+void LazyServiceRegistrar::setActiveServicesCallback(const std::function<bool(bool)>&
+ activeServicesCallback) {
+ mClientCC->setActiveServicesCallback(activeServicesCallback);
}
bool LazyServiceRegistrar::tryUnregister() {
diff --git a/libs/binder/include/binder/LazyServiceRegistrar.h b/libs/binder/include/binder/LazyServiceRegistrar.h
index 73893c7..9659732 100644
--- a/libs/binder/include/binder/LazyServiceRegistrar.h
+++ b/libs/binder/include/binder/LazyServiceRegistrar.h
@@ -56,10 +56,10 @@
void forcePersist(bool persist);
/**
- * Set a callback that is executed when the total number of services with
- * clients changes.
- * The callback takes an argument, which is the number of registered
- * lazy services for this process which have clients.
+ * Set a callback that is invoked when the active service count (i.e. services with clients)
+ * registered with this process drops to zero (or becomes nonzero).
+ * The callback takes a boolean argument, which is 'true' if there is
+ * at least one service with clients.
*
* Callback return value:
* - false: Default behavior for lazy services (shut down the process if there
@@ -73,8 +73,7 @@
*
* This method should be called before 'registerService' to avoid races.
*/
- void setActiveServicesCountCallback(const std::function<bool(int)>&
- activeServicesCountCallback);
+ void setActiveServicesCallback(const std::function<bool(bool)>& activeServicesCallback);
/**
* Try to unregister all services previously registered with 'registerService'.