Merge "Fix TODOs made during migration to libbinder_ndk" into rvc-dev

This commit is contained in:
Ruchir Rastogi
2020-03-09 17:54:19 +00:00
committed by Android (Google) Code Review
7 changed files with 82 additions and 51 deletions

View File

@@ -930,8 +930,6 @@ Status StatsService::informOnePackage(const string& app, int32_t uid, int64_t ve
ENFORCE_UID(AID_SYSTEM); ENFORCE_UID(AID_SYSTEM);
VLOG("StatsService::informOnePackage was called"); VLOG("StatsService::informOnePackage was called");
// TODO(b/149254662): This is gross. We should consider changing statsd
// internals to use std::string.
String16 utf16App = String16(app.c_str()); String16 utf16App = String16(app.c_str());
String16 utf16VersionString = String16(versionString.c_str()); String16 utf16VersionString = String16(versionString.c_str());
String16 utf16Installer = String16(installer.c_str()); String16 utf16Installer = String16(installer.c_str());

View File

@@ -38,8 +38,6 @@ AlarmMonitor::~AlarmMonitor() {}
void AlarmMonitor::setStatsCompanionService( void AlarmMonitor::setStatsCompanionService(
shared_ptr<IStatsCompanionService> statsCompanionService) { shared_ptr<IStatsCompanionService> statsCompanionService) {
std::lock_guard<std::mutex> lock(mLock); std::lock_guard<std::mutex> lock(mLock);
// TODO(b/149254662): determine if tmpForLock is needed now that we have moved
// from sp to shared_ptr
shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService; shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
mStatsCompanionService = statsCompanionService; mStatsCompanionService = statsCompanionService;
if (statsCompanionService == nullptr) { if (statsCompanionService == nullptr) {

View File

@@ -54,20 +54,22 @@ struct ConfigReceiverDeathCookie {
shared_ptr<IPendingIntentRef> mPir; shared_ptr<IPendingIntentRef> mPir;
}; };
static void configReceiverDied(void* cookie) { void ConfigManager::configReceiverDied(void* cookie) {
auto cookie_ = static_cast<ConfigReceiverDeathCookie*>(cookie); auto cookie_ = static_cast<ConfigReceiverDeathCookie*>(cookie);
sp<ConfigManager> configManager = cookie_->mConfigManager; sp<ConfigManager>& thiz = cookie_->mConfigManager;
ConfigKey configKey = cookie_->mConfigKey; ConfigKey& configKey = cookie_->mConfigKey;
shared_ptr<IPendingIntentRef> pir = cookie_->mPir; shared_ptr<IPendingIntentRef>& pir = cookie_->mPir;
// TODO(b/149254662): Fix threading. This currently fails if a new // Erase the mapping from the config key to the config receiver (pir) if the
// pir gets set between the get and the remove. // mapping still exists.
if (configManager->GetConfigReceiver(configKey) == pir) { lock_guard<mutex> lock(thiz->mMutex);
configManager->RemoveConfigReceiver(configKey); auto it = thiz->mConfigReceivers.find(configKey);
if (it != thiz->mConfigReceivers.end() && it->second == pir) {
thiz->mConfigReceivers.erase(configKey);
} }
// The death recipient corresponding to this specific pir can never
// be triggered again, so free up resources. // The death recipient corresponding to this specific pir can never be
// TODO(b/149254662): Investigate other options to manage the memory. // triggered again, so free up resources.
delete cookie_; delete cookie_;
} }
@@ -83,26 +85,29 @@ struct ActiveConfigChangedReceiverDeathCookie {
shared_ptr<IPendingIntentRef> mPir; shared_ptr<IPendingIntentRef> mPir;
}; };
static void activeConfigChangedReceiverDied(void* cookie) { void ConfigManager::activeConfigChangedReceiverDied(void* cookie) {
auto cookie_ = static_cast<ActiveConfigChangedReceiverDeathCookie*>(cookie); auto cookie_ = static_cast<ActiveConfigChangedReceiverDeathCookie*>(cookie);
sp<ConfigManager> configManager = cookie_->mConfigManager; sp<ConfigManager>& thiz = cookie_->mConfigManager;
int uid = cookie_->mUid; int uid = cookie_->mUid;
shared_ptr<IPendingIntentRef> pir = cookie_->mPir; shared_ptr<IPendingIntentRef>& pir = cookie_->mPir;
// TODO(b/149254662): Fix threading. This currently fails if a new // Erase the mapping from the config key to the active config changed
// pir gets set between the get and the remove. // receiver (pir) if the mapping still exists.
if (configManager->GetActiveConfigsChangedReceiver(uid) == pir) { lock_guard<mutex> lock(thiz->mMutex);
configManager->RemoveActiveConfigsChangedReceiver(uid); auto it = thiz->mActiveConfigsChangedReceivers.find(uid);
if (it != thiz->mActiveConfigsChangedReceivers.end() && it->second == pir) {
thiz->mActiveConfigsChangedReceivers.erase(uid);
} }
// The death recipient corresponding to this specific pir can never // The death recipient corresponding to this specific pir can never
// be triggered again, so free up resources. // be triggered again, so free up resources.
delete cookie_; delete cookie_;
} }
ConfigManager::ConfigManager(): ConfigManager::ConfigManager() :
mConfigReceiverDeathRecipient(AIBinder_DeathRecipient_new(configReceiverDied)), mConfigReceiverDeathRecipient(AIBinder_DeathRecipient_new(configReceiverDied)),
mActiveConfigChangedReceiverDeathRecipient( mActiveConfigChangedReceiverDeathRecipient(
AIBinder_DeathRecipient_new(activeConfigChangedReceiverDied)) { AIBinder_DeathRecipient_new(activeConfigChangedReceiverDied)) {
} }
ConfigManager::~ConfigManager() { ConfigManager::~ConfigManager() {
@@ -189,8 +194,10 @@ void ConfigManager::RemoveConfigReceiver(const ConfigKey& key) {
void ConfigManager::SetActiveConfigsChangedReceiver(const int uid, void ConfigManager::SetActiveConfigsChangedReceiver(const int uid,
const shared_ptr<IPendingIntentRef>& pir) { const shared_ptr<IPendingIntentRef>& pir) {
lock_guard<mutex> lock(mMutex); {
mActiveConfigsChangedReceivers[uid] = pir; lock_guard<mutex> lock(mMutex);
mActiveConfigsChangedReceivers[uid] = pir;
}
AIBinder_linkToDeath(pir->asBinder().get(), mActiveConfigChangedReceiverDeathRecipient.get(), AIBinder_linkToDeath(pir->asBinder().get(), mActiveConfigChangedReceiverDeathRecipient.get(),
new ActiveConfigChangedReceiverDeathCookie(this, uid, pir)); new ActiveConfigChangedReceiverDeathCookie(this, uid, pir));
} }

View File

@@ -161,6 +161,19 @@ private:
// IPendingIntentRef dies. // IPendingIntentRef dies.
::ndk::ScopedAIBinder_DeathRecipient mConfigReceiverDeathRecipient; ::ndk::ScopedAIBinder_DeathRecipient mConfigReceiverDeathRecipient;
::ndk::ScopedAIBinder_DeathRecipient mActiveConfigChangedReceiverDeathRecipient; ::ndk::ScopedAIBinder_DeathRecipient mActiveConfigChangedReceiverDeathRecipient;
/**
* Death recipient callback that is called when a config receiver dies.
* The cookie is a pointer to a ConfigReceiverDeathCookie.
*/
static void configReceiverDied(void* cookie);
/**
* Death recipient callback that is called when an active config changed
* receiver dies. The cookie is a pointer to an
* ActiveConfigChangedReceiverDeathCookie.
*/
static void activeConfigChangedReceiverDied(void* cookie);
}; };
} // namespace statsd } // namespace statsd

View File

@@ -85,12 +85,9 @@ void StatsPullerManager::updateAlarmLocked() {
return; return;
} }
// TODO(b/149254662): Why are we creating a copy here? This is different // TODO(b/151045771): do not hold a lock while making a binder call
// from the other places where we create a copy because we don't reassign if (mStatsCompanionService != nullptr) {
// mStatsCompanionService so a destructor can't implicitly be called... mStatsCompanionService->setPullingAlarm(mNextPullTimeNs / 1000000);
shared_ptr<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService;
if (statsCompanionServiceCopy != nullptr) {
statsCompanionServiceCopy->setPullingAlarm(mNextPullTimeNs / 1000000);
} else { } else {
VLOG("StatsCompanionService not available. Alarm not set."); VLOG("StatsCompanionService not available. Alarm not set.");
} }
@@ -99,8 +96,6 @@ void StatsPullerManager::updateAlarmLocked() {
void StatsPullerManager::SetStatsCompanionService( void StatsPullerManager::SetStatsCompanionService(
shared_ptr<IStatsCompanionService> statsCompanionService) { shared_ptr<IStatsCompanionService> statsCompanionService) {
// TODO(b/149254662): Why are we using AutoMutex instead of lock_guard?
// Additionally, do we need the temporary shared_ptr to prevent deadlocks?
AutoMutex _l(mLock); AutoMutex _l(mLock);
shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService; shared_ptr<IStatsCompanionService> tmpForLock = mStatsCompanionService;
mStatsCompanionService = statsCompanionService; mStatsCompanionService = statsCompanionService;

View File

@@ -39,33 +39,47 @@ struct BroadcastSubscriberDeathCookie {
shared_ptr<IPendingIntentRef> mPir; shared_ptr<IPendingIntentRef> mPir;
}; };
static void broadcastSubscriberDied(void* cookie) { void SubscriberReporter::broadcastSubscriberDied(void* cookie) {
BroadcastSubscriberDeathCookie* cookie_ = (BroadcastSubscriberDeathCookie*)cookie; auto cookie_ = static_cast<BroadcastSubscriberDeathCookie*>(cookie);
ConfigKey configKey = cookie_->mConfigKey; ConfigKey& configKey = cookie_->mConfigKey;
int64_t subscriberId = cookie_->mSubscriberId; int64_t subscriberId = cookie_->mSubscriberId;
shared_ptr<IPendingIntentRef> pir = cookie_->mPir; shared_ptr<IPendingIntentRef>& pir = cookie_->mPir;
// TODO(b/149254662): Fix threading. This currently fails if a new pir gets SubscriberReporter& thiz = getInstance();
// set between the get and the unset.
if (SubscriberReporter::getInstance().getBroadcastSubscriber(configKey, subscriberId) == pir) { // Erase the mapping from a (config_key, subscriberId) to a pir if the
SubscriberReporter::getInstance().unsetBroadcastSubscriber(configKey, subscriberId); // mapping exists.
lock_guard<mutex> lock(thiz.mLock);
auto subscriberMapIt = thiz.mIntentMap.find(configKey);
if (subscriberMapIt != thiz.mIntentMap.end()) {
auto subscriberMap = subscriberMapIt->second;
auto pirIt = subscriberMap.find(subscriberId);
if (pirIt != subscriberMap.end() && pirIt->second == pir) {
subscriberMap.erase(subscriberId);
if (subscriberMap.empty()) {
thiz.mIntentMap.erase(configKey);
}
}
} }
// The death recipient corresponding to this specific pir can never be // The death recipient corresponding to this specific pir can never be
// triggered again, so free up resources. // triggered again, so free up resources.
delete cookie_; delete cookie_;
} }
static ::ndk::ScopedAIBinder_DeathRecipient sBroadcastSubscriberDeathRecipient( SubscriberReporter::SubscriberReporter() :
AIBinder_DeathRecipient_new(broadcastSubscriberDied)); mBroadcastSubscriberDeathRecipient(AIBinder_DeathRecipient_new(broadcastSubscriberDied)) {
}
void SubscriberReporter::setBroadcastSubscriber(const ConfigKey& configKey, void SubscriberReporter::setBroadcastSubscriber(const ConfigKey& configKey,
int64_t subscriberId, int64_t subscriberId,
const shared_ptr<IPendingIntentRef>& pir) { const shared_ptr<IPendingIntentRef>& pir) {
VLOG("SubscriberReporter::setBroadcastSubscriber called."); VLOG("SubscriberReporter::setBroadcastSubscriber called.");
lock_guard<mutex> lock(mLock); {
mIntentMap[configKey][subscriberId] = pir; lock_guard<mutex> lock(mLock);
// TODO(b/149254662): Is it ok to call linkToDeath while holding a lock? mIntentMap[configKey][subscriberId] = pir;
AIBinder_linkToDeath(pir->asBinder().get(), sBroadcastSubscriberDeathRecipient.get(), }
AIBinder_linkToDeath(pir->asBinder().get(), mBroadcastSubscriberDeathRecipient.get(),
new BroadcastSubscriberDeathCookie(configKey, subscriberId, pir)); new BroadcastSubscriberDeathCookie(configKey, subscriberId, pir));
} }
@@ -103,8 +117,6 @@ void SubscriberReporter::alertBroadcastSubscriber(const ConfigKey& configKey,
} }
int64_t subscriberId = subscription.broadcast_subscriber_details().subscriber_id(); int64_t subscriberId = subscription.broadcast_subscriber_details().subscriber_id();
// TODO(b/149254662): Is there a way to convert a RepeatedPtrField into a
// vector without copying?
vector<string> cookies; vector<string> cookies;
cookies.reserve(subscription.broadcast_subscriber_details().cookie_size()); cookies.reserve(subscription.broadcast_subscriber_details().cookie_size());
for (auto& cookie : subscription.broadcast_subscriber_details().cookie()) { for (auto& cookie : subscription.broadcast_subscriber_details().cookie()) {

View File

@@ -78,7 +78,7 @@ public:
int64_t subscriberId); int64_t subscriberId);
private: private:
SubscriberReporter() {}; SubscriberReporter();
mutable mutex mLock; mutable mutex mLock;
@@ -94,6 +94,14 @@ private:
const Subscription& subscription, const Subscription& subscription,
const vector<string>& cookies, const vector<string>& cookies,
const MetricDimensionKey& dimKey) const; const MetricDimensionKey& dimKey) const;
::ndk::ScopedAIBinder_DeathRecipient mBroadcastSubscriberDeathRecipient;
/**
* Death recipient callback that is called when a broadcast subscriber dies.
* The cookie is a pointer to a BroadcastSubscriberDeathCookie.
*/
static void broadcastSubscriberDied(void* cookie);
}; };
} // namespace statsd } // namespace statsd