diff --git a/libstats/pull/Android.bp b/libstats/pull/Android.bp index de5903317..827e992dc 100644 --- a/libstats/pull/Android.bp +++ b/libstats/pull/Android.bp @@ -19,9 +19,6 @@ // ========================================================== cc_library_shared { name: "libstatspull", - aidl: { - include_dirs: ["frameworks/base/core/java"], - }, srcs: [ "stats_pull_atom_callback.cpp", ], @@ -31,11 +28,9 @@ cc_library_shared { ], export_include_dirs: ["include"], shared_libs: [ - //TODO: use libbinder_ndk. Remove libservices. - "libbinder", + "libbinder_ndk", "libstatssocket", - "libservices", - "statsd-aidl-cpp", + "statsd-aidl-ndk_platform", ], static_libs: [ "liblog", diff --git a/libstats/pull/stats_pull_atom_callback.cpp b/libstats/pull/stats_pull_atom_callback.cpp index 9c497b8ea..27e9d29e2 100644 --- a/libstats/pull/stats_pull_atom_callback.cpp +++ b/libstats/pull/stats_pull_atom_callback.cpp @@ -15,18 +15,26 @@ */ #include +#include #include #include #include -#include -#include -#include -#include -#include +#include +#include +#include +#include +#include +#include +#include -#include +using Status = ::ndk::ScopedAStatus; +using aidl::android::os::BnPullAtomCallback; +using aidl::android::os::IPullAtomResultReceiver; +using aidl::android::os::IStatsd; +using aidl::android::util::StatsEventParcel; +using ::ndk::SharedRefBase; struct AStatsEventList { std::vector data; @@ -74,7 +82,7 @@ void AStatsManager_PullAtomMetadata_setAdditiveFields(AStatsManager_PullAtomMeta metadata->additive_fields.assign(additive_fields, additive_fields + num_fields); } -class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback { +class StatsPullAtomCallbackInternal : public BnPullAtomCallback { public: StatsPullAtomCallbackInternal(const AStatsManager_PullAtomCallback callback, void* cookie, const int64_t coolDownNs, const int64_t timeoutNs, @@ -85,21 +93,19 @@ class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback { mTimeoutNs(timeoutNs), mAdditiveFields(additiveFields) {} - ::android::binder::Status onPullAtom( - int32_t atomTag, - const ::android::sp<::android::os::IPullAtomResultReceiver>& resultReceiver) override { + Status onPullAtom(int32_t atomTag, + const std::shared_ptr& resultReceiver) override { AStatsEventList statsEventList; - statsEventList.data.clear(); int successInt = mCallback(atomTag, &statsEventList, mCookie); bool success = successInt == AStatsManager_PULL_SUCCESS; // Convert stats_events into StatsEventParcels. - std::vector parcels; + std::vector parcels; for (int i = 0; i < statsEventList.data.size(); i++) { size_t size; uint8_t* buffer = AStatsEvent_getBuffer(statsEventList.data[i], &size); - android::util::StatsEventParcel p; + StatsEventParcel p; // vector.assign() creates a copy, but this is inevitable unless // stats_event.h/c uses a vector as opposed to a buffer. p.buffer.assign(buffer, buffer + size); @@ -110,7 +116,7 @@ class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback { for (int i = 0; i < statsEventList.data.size(); i++) { AStatsEvent_release(statsEventList.data[i]); } - return android::binder::Status::ok(); + return Status::ok(); } const int64_t& getCoolDownNs() const { return mCoolDownNs; } @@ -126,60 +132,55 @@ class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback { }; static std::mutex pullAtomMutex; -static android::sp sStatsd = nullptr; +static std::shared_ptr sStatsd = nullptr; -static std::map> mPullers; -static android::sp getStatsService(); +static std::map> mPullers; +static std::shared_ptr getStatsService(); -class StatsDeathRecipient : public android::IBinder::DeathRecipient { - public: - StatsDeathRecipient() = default; - ~StatsDeathRecipient() override = default; - - // android::IBinder::DeathRecipient override: - void binderDied(const android::wp& /* who */) override { - { - std::lock_guard lock(pullAtomMutex); - sStatsd = nullptr; - } - android::sp statsService = getStatsService(); - if (statsService == nullptr) { - return; - } - - std::map> pullersCopy; - { - std::lock_guard lock(pullAtomMutex); - pullersCopy = mPullers; - } - for (auto it : pullersCopy) { - statsService->registerNativePullAtomCallback(it.first, it.second->getCoolDownNs(), - it.second->getTimeoutNs(), - it.second->getAdditiveFields(), it.second); - } +static void binderDied(void* /*cookie*/) { + { + std::lock_guard lock(pullAtomMutex); + sStatsd = nullptr; } -}; -static android::sp statsDeathRecipient = new StatsDeathRecipient(); + std::shared_ptr statsService = getStatsService(); + if (statsService == nullptr) { + return; + } -static android::sp getStatsService() { + // Since we do not want to make an IPC with the lock held, we first create a + // copy of the data with the lock held before iterating through the map. + std::map> pullersCopy; + { + std::lock_guard lock(pullAtomMutex); + pullersCopy = mPullers; + } + for (const auto& it : pullersCopy) { + statsService->registerNativePullAtomCallback(it.first, it.second->getCoolDownNs(), + it.second->getTimeoutNs(), + it.second->getAdditiveFields(), it.second); + } +} + +static ::ndk::ScopedAIBinder_DeathRecipient sDeathRecipient( + AIBinder_DeathRecipient_new(binderDied)); + +static std::shared_ptr getStatsService() { std::lock_guard lock(pullAtomMutex); if (!sStatsd) { - // Fetch statsd. - const android::sp binder = - android::defaultServiceManager()->getService(android::String16("stats")); - if (!binder) { - return nullptr; + // Fetch statsd + ::ndk::SpAIBinder binder(AServiceManager_getService("stats")); + sStatsd = IStatsd::fromBinder(binder); + if (sStatsd) { + AIBinder_linkToDeath(binder.get(), sDeathRecipient.get(), /*cookie=*/nullptr); } - binder->linkToDeath(statsDeathRecipient); - sStatsd = android::interface_cast(binder); } return sStatsd; } void registerStatsPullAtomCallbackBlocking(int32_t atomTag, - android::sp cb) { - const android::sp statsService = getStatsService(); + std::shared_ptr cb) { + const std::shared_ptr statsService = getStatsService(); if (statsService == nullptr) { // Statsd not available return; @@ -190,7 +191,7 @@ void registerStatsPullAtomCallbackBlocking(int32_t atomTag, } void unregisterStatsPullAtomCallbackBlocking(int32_t atomTag) { - const android::sp statsService = getStatsService(); + const std::shared_ptr statsService = getStatsService(); if (statsService == nullptr) { // Statsd not available return; @@ -211,8 +212,9 @@ void AStatsManager_registerPullAtomCallback(int32_t atom_tag, additiveFields = metadata->additive_fields; } - android::sp callbackBinder = new StatsPullAtomCallbackInternal( - callback, cookie, coolDownNs, timeoutNs, additiveFields); + std::shared_ptr callbackBinder = + SharedRefBase::make(callback, cookie, coolDownNs, + timeoutNs, additiveFields); { std::lock_guard lg(pullAtomMutex);