From 99e69716215cd0665379bc90d708f2ea8689831d Mon Sep 17 00:00:00 2001 From: Ruben Brunk Date: Tue, 26 May 2015 17:25:07 -0700 Subject: Track camera and flashlight usage in battery stats. Bug: 15986092 Change-Id: I9dc6828332e4091fd93bf2d82839e8e3862a2fc2 --- services/camera/libcameraservice/Android.mk | 1 + services/camera/libcameraservice/CameraService.cpp | 96 +++++++++++++-- services/camera/libcameraservice/CameraService.h | 26 +++- .../camera/libcameraservice/utils/ClientManager.h | 133 +++++++++++++++------ 4 files changed, 205 insertions(+), 51 deletions(-) (limited to 'services/camera') diff --git a/services/camera/libcameraservice/Android.mk b/services/camera/libcameraservice/Android.mk index 9c60911..cbead32 100644 --- a/services/camera/libcameraservice/Android.mk +++ b/services/camera/libcameraservice/Android.mk @@ -62,6 +62,7 @@ LOCAL_SHARED_LIBRARIES:= \ libbinder \ libcutils \ libmedia \ + libmediautils \ libcamera_client \ libgui \ libhardware \ diff --git a/services/camera/libcameraservice/CameraService.cpp b/services/camera/libcameraservice/CameraService.cpp index fc9a332..527e80b 100644 --- a/services/camera/libcameraservice/CameraService.cpp +++ b/services/camera/libcameraservice/CameraService.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -139,6 +140,11 @@ void CameraService::onFirstRef() BnCameraService::onFirstRef(); + // Update battery life tracking if service is restarting + BatteryNotifier& notifier(BatteryNotifier::getInstance()); + notifier.noteResetCamera(); + notifier.noteResetFlashlight(); + camera_module_t *rawModule; int err = hw_get_module(CAMERA_HARDWARE_MODULE_ID, (const hw_module_t **)&rawModule); @@ -323,12 +329,39 @@ void CameraService::onTorchStatusChangedLocked(const String8& cameraId, res = setTorchStatusLocked(cameraId, newStatus); if (res) { - ALOGE("%s: Failed to set the torch status", __FUNCTION__, - (uint32_t)newStatus); + ALOGE("%s: Failed to set the torch status", __FUNCTION__, (uint32_t)newStatus); return; } { + // Update battery life logging for flashlight + Mutex::Autolock al(mTorchClientMapMutex); + auto iter = mTorchUidMap.find(cameraId); + if (iter != mTorchUidMap.end()) { + int oldUid = iter->second.second; + int newUid = iter->second.first; + BatteryNotifier& notifier(BatteryNotifier::getInstance()); + if (oldUid != newUid) { + // If the UID has changed, log the status and update current UID in mTorchUidMap + if (status == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) { + notifier.noteFlashlightOff(cameraId, oldUid); + } + if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) { + notifier.noteFlashlightOn(cameraId, newUid); + } + iter->second.second = newUid; + } else { + // If the UID has not changed, log the status + if (newStatus == ICameraServiceListener::TORCH_STATUS_AVAILABLE_ON) { + notifier.noteFlashlightOn(cameraId, oldUid); + } else { + notifier.noteFlashlightOff(cameraId, oldUid); + } + } + } + } + + { Mutex::Autolock lock(mStatusListenerLock); for (auto& i : mListenerList) { i->onTorchStatusChanged(newStatus, String16{cameraId}); @@ -1137,12 +1170,13 @@ status_t CameraService::connectDevice( status_t CameraService::setTorchMode(const String16& cameraId, bool enabled, const sp& clientBinder) { - if (enabled && clientBinder == NULL) { + if (enabled && clientBinder == nullptr) { ALOGE("%s: torch client binder is NULL", __FUNCTION__); return -EINVAL; } String8 id = String8(cameraId.string()); + int uid = getCallingUid(); // verify id is valid. auto state = getCameraState(id); @@ -1181,7 +1215,21 @@ status_t CameraService::setTorchMode(const String16& cameraId, bool enabled, } } + { + // Update UID map - this is used in the torch status changed callbacks, so must be done + // before setTorchMode + Mutex::Autolock al(mTorchClientMapMutex); + if (mTorchUidMap.find(id) == mTorchUidMap.end()) { + mTorchUidMap[id].first = uid; + mTorchUidMap[id].second = uid; + } else { + // Set the pending UID + mTorchUidMap[id].first = uid; + } + } + status_t res = mFlashlight->setTorchMode(id, enabled); + if (res) { ALOGE("%s: setting torch mode of camera %s to %d failed. %s (%d)", __FUNCTION__, id.string(), enabled, strerror(-res), res); @@ -1192,19 +1240,17 @@ status_t CameraService::setTorchMode(const String16& cameraId, bool enabled, // update the link to client's death Mutex::Autolock al(mTorchClientMapMutex); ssize_t index = mTorchClientMap.indexOfKey(id); + BatteryNotifier& notifier(BatteryNotifier::getInstance()); if (enabled) { if (index == NAME_NOT_FOUND) { mTorchClientMap.add(id, clientBinder); } else { - const sp oldBinder = mTorchClientMap.valueAt(index); - oldBinder->unlinkToDeath(this); - + mTorchClientMap.valueAt(index)->unlinkToDeath(this); mTorchClientMap.replaceValueAt(index, clientBinder); } clientBinder->linkToDeath(this); } else if (index != NAME_NOT_FOUND) { - sp oldBinder = mTorchClientMap.valueAt(index); - oldBinder->unlinkToDeath(this); + mTorchClientMap.valueAt(index)->unlinkToDeath(this); } } @@ -1226,8 +1272,7 @@ void CameraService::notifySystemEvent(int32_t eventId, const int32_t* args, size } } -status_t CameraService::addListener( - const sp& listener) { +status_t CameraService::addListener(const sp& listener) { ALOGV("%s: Add listener %p", __FUNCTION__, listener.get()); if (listener == 0) { @@ -1948,9 +1993,40 @@ String8 CameraService::CameraState::getId() const { } // ---------------------------------------------------------------------------- +// ClientEventListener +// ---------------------------------------------------------------------------- + +void CameraService::ClientEventListener::onClientAdded( + const resource_policy::ClientDescriptor>& descriptor) { + auto basicClient = descriptor.getValue(); + if (basicClient.get() != nullptr) { + BatteryNotifier& notifier(BatteryNotifier::getInstance()); + notifier.noteStartCamera(descriptor.getKey(), + static_cast(basicClient->getClientUid())); + } +} + +void CameraService::ClientEventListener::onClientRemoved( + const resource_policy::ClientDescriptor>& descriptor) { + auto basicClient = descriptor.getValue(); + if (basicClient.get() != nullptr) { + BatteryNotifier& notifier(BatteryNotifier::getInstance()); + notifier.noteStopCamera(descriptor.getKey(), + static_cast(basicClient->getClientUid())); + } +} + + +// ---------------------------------------------------------------------------- // CameraClientManager // ---------------------------------------------------------------------------- +CameraService::CameraClientManager::CameraClientManager() { + setListener(std::make_shared()); +} + CameraService::CameraClientManager::~CameraClientManager() {} sp CameraService::CameraClientManager::getCameraClient( diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h index 9b7163a..2e4743f 100644 --- a/services/camera/libcameraservice/CameraService.h +++ b/services/camera/libcameraservice/CameraService.h @@ -46,6 +46,7 @@ #include #include #include +#include namespace android { @@ -327,6 +328,20 @@ public: }; // class Client + /** + * A listener class that implements the LISTENER interface for use with a ClientManager, and + * implements the following methods: + * void onClientRemoved(const ClientDescriptor& descriptor); + * void onClientAdded(const ClientDescriptor& descriptor); + */ + class ClientEventListener { + public: + void onClientAdded(const resource_policy::ClientDescriptor>& descriptor); + void onClientRemoved(const resource_policy::ClientDescriptor>& descriptor); + }; // class ClientEventListener + typedef std::shared_ptr>> DescriptorPtr; @@ -338,9 +353,10 @@ public: * This class manages the eviction behavior for the camera clients. See the parent class * implementation in utils/ClientManager for the specifics of this behavior. */ - class CameraClientManager : - public resource_policy::ClientManager> { + class CameraClientManager : public resource_policy::ClientManager, ClientEventListener> { public: + CameraClientManager(); virtual ~CameraClientManager(); /** @@ -624,13 +640,15 @@ private: sp mFlashlight; // guard mTorchStatusMap Mutex mTorchStatusMutex; - // guard mTorchClientMap + // guard mTorchClientMap, mTorchUidMap Mutex mTorchClientMapMutex; // camera id -> torch status KeyedVector mTorchStatusMap; // camera id -> torch client binder // only store the last client that turns on each camera's torch mode - KeyedVector > mTorchClientMap; + KeyedVector> mTorchClientMap; + // camera id -> [incoming uid, current uid] pair + std::map> mTorchUidMap; // check and handle if torch client's process has died void handleTorchClientBinderDied(const wp &who); diff --git a/services/camera/libcameraservice/utils/ClientManager.h b/services/camera/libcameraservice/utils/ClientManager.h index 5afb7a3..7ae58d5 100644 --- a/services/camera/libcameraservice/utils/ClientManager.h +++ b/services/camera/libcameraservice/utils/ClientManager.h @@ -172,6 +172,26 @@ void ClientDescriptor::setPriority(int32_t priority) { // -------------------------------------------------------------------------------- /** + * A default class implementing the LISTENER interface used by ClientManager. + */ +template +class DefaultEventListener { +public: + void onClientAdded(const ClientDescriptor& descriptor); + void onClientRemoved(const ClientDescriptor& descriptor); +}; + +template +void DefaultEventListener::onClientAdded( + const ClientDescriptor& /*descriptor*/) {} + +template +void DefaultEventListener::onClientRemoved( + const ClientDescriptor& /*descriptor*/) {} + +// -------------------------------------------------------------------------------- + +/** * The ClientManager class wraps an LRU-ordered list of active clients and implements eviction * behavior for handling shared resource access. * @@ -189,7 +209,7 @@ void ClientDescriptor::setPriority(int32_t priority) { * incoming descriptor has the highest priority. Otherwise, the incoming descriptor is * removed instead. */ -template +template> class ClientManager { public: // The default maximum "cost" allowed before evicting @@ -275,6 +295,24 @@ public: status_t waitUntilRemoved(const std::shared_ptr> client, nsecs_t timeout) const; + /** + * Set the current listener for client add/remove events. + * + * The listener instance must inherit from the LISTENER class and implement the following + * methods: + * void onClientRemoved(const ClientDescriptor& descriptor); + * void onClientAdded(const ClientDescriptor& descriptor); + * + * These callback methods will be called with the ClientManager's lock held, and should + * not call any further ClientManager methods. + * + * The onClientRemoved method will be called when the client has been removed or evicted + * from the ClientManager that this event listener has been added to. The onClientAdded + * method will be called when the client has been added to the ClientManager that this + * event listener has been added to. + */ + void setListener(const std::shared_ptr& listener); + protected: ~ClientManager(); @@ -300,36 +338,38 @@ private: int32_t mMaxCost; // LRU ordered, most recent at end std::vector>> mClients; + std::shared_ptr mListener; }; // class ClientManager -template -ClientManager::ClientManager() : +template +ClientManager::ClientManager() : ClientManager(DEFAULT_MAX_COST) {} -template -ClientManager::ClientManager(int32_t totalCost) : mMaxCost(totalCost) {} +template +ClientManager::ClientManager(int32_t totalCost) : mMaxCost(totalCost) {} -template -ClientManager::~ClientManager() {} +template +ClientManager::~ClientManager() {} -template -std::vector>> ClientManager::wouldEvict( +template +std::vector>> +ClientManager::wouldEvict( const std::shared_ptr>& client) const { Mutex::Autolock lock(mLock); return wouldEvictLocked(client); } -template +template std::vector>> -ClientManager::getIncompatibleClients( +ClientManager::getIncompatibleClients( const std::shared_ptr>& client) const { Mutex::Autolock lock(mLock); return wouldEvictLocked(client, /*returnIncompatibleClients*/true); } -template +template std::vector>> -ClientManager::wouldEvictLocked( +ClientManager::wouldEvictLocked( const std::shared_ptr>& client, bool returnIncompatibleClients) const { @@ -420,8 +460,9 @@ ClientManager::wouldEvictLocked( } -template -std::vector>> ClientManager::addAndEvict( +template +std::vector>> +ClientManager::addAndEvict( const std::shared_ptr>& client) { Mutex::Autolock lock(mLock); auto evicted = wouldEvictLocked(client); @@ -433,6 +474,9 @@ std::vector>> ClientManageronClientRemoved(**iter); + // Remove evicted clients from list mClients.erase(std::remove_if(mClients.begin(), mClients.end(), [&iter] (std::shared_ptr>& curClientPtr) { @@ -444,21 +488,22 @@ std::vector>> ClientManageronClientAdded(*client); mClients.push_back(client); mRemovedCondition.broadcast(); return evicted; } -template +template std::vector>> -ClientManager::getAll() const { +ClientManager::getAll() const { Mutex::Autolock lock(mLock); return mClients; } -template -std::vector ClientManager::getAllKeys() const { +template +std::vector ClientManager::getAllKeys() const { Mutex::Autolock lock(mLock); std::vector keys(mClients.size()); for (const auto& i : mClients) { @@ -467,8 +512,8 @@ std::vector ClientManager::getAllKeys() const { return keys; } -template -std::vector ClientManager::getAllOwners() const { +template +std::vector ClientManager::getAllOwners() const { Mutex::Autolock lock(mLock); std::set owners; for (const auto& i : mClients) { @@ -477,8 +522,8 @@ std::vector ClientManager::getAllOwners() const { return std::vector(owners.begin(), owners.end()); } -template -void ClientManager::updatePriorities( +template +void ClientManager::updatePriorities( const std::map& ownerPriorityList) { Mutex::Autolock lock(mLock); for (auto& i : mClients) { @@ -489,8 +534,8 @@ void ClientManager::updatePriorities( } } -template -std::shared_ptr> ClientManager::get( +template +std::shared_ptr> ClientManager::get( const KEY& key) const { Mutex::Autolock lock(mLock); for (const auto& i : mClients) { @@ -499,23 +544,30 @@ std::shared_ptr> ClientManager::get( return std::shared_ptr>(nullptr); } -template -void ClientManager::removeAll() { +template +void ClientManager::removeAll() { Mutex::Autolock lock(mLock); + if (mListener != nullptr) { + for (const auto& i : mClients) { + mListener->onClientRemoved(*i); + } + } mClients.clear(); mRemovedCondition.broadcast(); } -template -std::shared_ptr> ClientManager::remove(const KEY& key) { +template +std::shared_ptr> ClientManager::remove( + const KEY& key) { Mutex::Autolock lock(mLock); std::shared_ptr> ret; // Remove evicted clients from list mClients.erase(std::remove_if(mClients.begin(), mClients.end(), - [&key, &ret] (std::shared_ptr>& curClientPtr) { + [this, &key, &ret] (std::shared_ptr>& curClientPtr) { if (curClientPtr->getKey() == key) { + if (mListener != nullptr) mListener->onClientRemoved(*curClientPtr); ret = curClientPtr; return true; } @@ -526,8 +578,8 @@ std::shared_ptr> ClientManager::remove( return ret; } -template -status_t ClientManager::waitUntilRemoved( +template +status_t ClientManager::waitUntilRemoved( const std::shared_ptr> client, nsecs_t timeout) const { status_t ret = NO_ERROR; @@ -558,14 +610,21 @@ status_t ClientManager::waitUntilRemoved( return ret; } -template -void ClientManager::remove( +template +void ClientManager::setListener(const std::shared_ptr& listener) { + Mutex::Autolock lock(mLock); + mListener = listener; +} + +template +void ClientManager::remove( const std::shared_ptr>& value) { Mutex::Autolock lock(mLock); // Remove evicted clients from list mClients.erase(std::remove_if(mClients.begin(), mClients.end(), - [&value] (std::shared_ptr>& curClientPtr) { + [this, &value] (std::shared_ptr>& curClientPtr) { if (curClientPtr == value) { + if (mListener != nullptr) mListener->onClientRemoved(*curClientPtr); return true; } return false; @@ -573,8 +632,8 @@ void ClientManager::remove( mRemovedCondition.broadcast(); } -template -int64_t ClientManager::getCurrentCostLocked() const { +template +int64_t ClientManager::getCurrentCostLocked() const { int64_t totalCost = 0; for (const auto& x : mClients) { totalCost += x->getCost(); -- cgit v1.1