Loading media/psh_utils/HealthStatsProvider.cpp +2 −14 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <aidl/android/hardware/health/IHealth.h> #include <android-base/logging.h> #include <android/binder_manager.h> #include <psh_utils/ServiceSingleton.h> using ::aidl::android::hardware::health::HealthInfo; using ::aidl::android::hardware::health::IHealth; Loading @@ -25,19 +26,7 @@ using ::aidl::android::hardware::health::IHealth; namespace android::media::psh_utils { static auto getHealthService() { [[clang::no_destroy]] static constinit std::mutex m; [[clang::no_destroy]] static constinit std::shared_ptr<IHealth> healthService; std::lock_guard l(m); if (healthService) { return healthService; } const auto serviceName = std::string(IHealth::descriptor).append("/default"); healthService = IHealth::fromBinder( ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str()))); return healthService; return getServiceSingleton<IHealth>(); } status_t HealthStatsDataProvider::fill(PowerStats* stat) const { Loading @@ -45,7 +34,6 @@ status_t HealthStatsDataProvider::fill(PowerStats* stat) const { HealthStats& stats = stat->health_stats; auto healthService = getHealthService(); if (healthService == nullptr) { LOG(ERROR) << "unable to get health AIDL service"; return NO_INIT; } HealthInfo healthInfo; Loading media/psh_utils/PowerStatsCollector.cpp +1 −4 Original line number Diff line number Diff line Loading @@ -80,10 +80,7 @@ int PowerStatsCollector::fill(PowerStats* stats) const { } for (const auto& provider : mPowerStatsProviders) { if (provider->fill(stats) != 0) { LOG(ERROR) << __func__ << ": a data provider failed"; continue; } (void) provider->fill(stats); // on error, we continue to proceed. } // boot time follows wall clock time, but starts from boot. Loading media/psh_utils/PowerStatsProvider.cpp +3 −18 Original line number Diff line number Diff line Loading @@ -15,37 +15,23 @@ */ #include "PowerStatsProvider.h" #include <aidl/android/hardware/power/stats/IPowerStats.h> #include <android-base/logging.h> #include <android/binder_manager.h> #include <psh_utils/ServiceSingleton.h> #include <unordered_map> #include <aidl/android/hardware/power/stats/IPowerStats.h> using ::aidl::android::hardware::power::stats::IPowerStats; namespace android::media::psh_utils { static auto getPowerStatsService() { [[clang::no_destroy]] static constinit std::mutex m; [[clang::no_destroy]] static constinit std::shared_ptr<IPowerStats> powerStats; std::lock_guard l(m); if (powerStats) { return powerStats; } const auto serviceName = std::string(IPowerStats::descriptor) .append("/default"); powerStats = IPowerStats::fromBinder( ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str()))); return powerStats; return getServiceSingleton<IPowerStats>(); } status_t RailEnergyDataProvider::fill(PowerStats *stat) const { if (stat == nullptr) return BAD_VALUE; auto powerStatsService = getPowerStatsService(); if (powerStatsService == nullptr) { LOG(ERROR) << "unable to get power.stats AIDL service"; return NO_INIT; } Loading Loading @@ -91,7 +77,6 @@ status_t PowerEntityResidencyDataProvider::fill(PowerStats* stat) const { if (stat == nullptr) return BAD_VALUE; auto powerStatsService = getPowerStatsService(); if (powerStatsService == nullptr) { LOG(ERROR) << "unable to get power.stats AIDL service"; return NO_INIT; } Loading media/psh_utils/PowerStatsProvider.h +0 −1 Original line number Diff line number Diff line Loading @@ -17,7 +17,6 @@ #pragma once #include <psh_utils/PowerStatsCollector.h> #include <iostream> namespace android::media::psh_utils { Loading media/psh_utils/include/psh_utils/ServiceSingleton.h 0 → 100644 +67 −0 Original line number Diff line number Diff line /* * Copyright (C) 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <android/binder_auto_utils.h> #include <android/binder_manager.h> #include <android-base/thread_annotations.h> #include <mutex> #include <utils/Log.h> #include <utils/Timers.h> namespace android::media::psh_utils { struct DefaultServiceTraits { static constexpr int64_t kThresholdRetryNs = 1'000'000'000; static constexpr int64_t kMaxRetries = 5; static constexpr const char* kServiceVersion = "/default"; static constexpr bool kShowLog = true; }; template<typename Service, typename ServiceTraits = DefaultServiceTraits> std::shared_ptr<Service> getServiceSingleton() { [[clang::no_destroy]] static constinit std::mutex m; [[clang::no_destroy]] static constinit std::shared_ptr<Service> service GUARDED_BY(m); static int64_t nextTryNs GUARDED_BY(m) = 0; static int64_t tries GUARDED_BY(m) = 0; std::lock_guard l(m); if (service || tries > ServiceTraits::kMaxRetries // try too many times || systemTime(SYSTEM_TIME_BOOTTIME) < nextTryNs) { // try too frequently. return service; } const auto serviceName = std::string(Service::descriptor) .append(ServiceTraits::kServiceVersion); service = Service::fromBinder( ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str()))); if (!service) { // If failed, set a time limit before retry. // No need to log an error, it is already done. nextTryNs = systemTime(SYSTEM_TIME_BOOTTIME) + ServiceTraits::kThresholdRetryNs; ALOGV_IF(ServiceTraits::kShowLog, "service:%s retries:%lld of %lld nextTryNs:%lld", Service::descriptor, (long long)tries, (long long)kMaxRetries, (long long)nextTryNs); ++tries; } return service; } } // namespace android::media::psh_utils Loading
media/psh_utils/HealthStatsProvider.cpp +2 −14 Original line number Diff line number Diff line Loading @@ -18,6 +18,7 @@ #include <aidl/android/hardware/health/IHealth.h> #include <android-base/logging.h> #include <android/binder_manager.h> #include <psh_utils/ServiceSingleton.h> using ::aidl::android::hardware::health::HealthInfo; using ::aidl::android::hardware::health::IHealth; Loading @@ -25,19 +26,7 @@ using ::aidl::android::hardware::health::IHealth; namespace android::media::psh_utils { static auto getHealthService() { [[clang::no_destroy]] static constinit std::mutex m; [[clang::no_destroy]] static constinit std::shared_ptr<IHealth> healthService; std::lock_guard l(m); if (healthService) { return healthService; } const auto serviceName = std::string(IHealth::descriptor).append("/default"); healthService = IHealth::fromBinder( ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str()))); return healthService; return getServiceSingleton<IHealth>(); } status_t HealthStatsDataProvider::fill(PowerStats* stat) const { Loading @@ -45,7 +34,6 @@ status_t HealthStatsDataProvider::fill(PowerStats* stat) const { HealthStats& stats = stat->health_stats; auto healthService = getHealthService(); if (healthService == nullptr) { LOG(ERROR) << "unable to get health AIDL service"; return NO_INIT; } HealthInfo healthInfo; Loading
media/psh_utils/PowerStatsCollector.cpp +1 −4 Original line number Diff line number Diff line Loading @@ -80,10 +80,7 @@ int PowerStatsCollector::fill(PowerStats* stats) const { } for (const auto& provider : mPowerStatsProviders) { if (provider->fill(stats) != 0) { LOG(ERROR) << __func__ << ": a data provider failed"; continue; } (void) provider->fill(stats); // on error, we continue to proceed. } // boot time follows wall clock time, but starts from boot. Loading
media/psh_utils/PowerStatsProvider.cpp +3 −18 Original line number Diff line number Diff line Loading @@ -15,37 +15,23 @@ */ #include "PowerStatsProvider.h" #include <aidl/android/hardware/power/stats/IPowerStats.h> #include <android-base/logging.h> #include <android/binder_manager.h> #include <psh_utils/ServiceSingleton.h> #include <unordered_map> #include <aidl/android/hardware/power/stats/IPowerStats.h> using ::aidl::android::hardware::power::stats::IPowerStats; namespace android::media::psh_utils { static auto getPowerStatsService() { [[clang::no_destroy]] static constinit std::mutex m; [[clang::no_destroy]] static constinit std::shared_ptr<IPowerStats> powerStats; std::lock_guard l(m); if (powerStats) { return powerStats; } const auto serviceName = std::string(IPowerStats::descriptor) .append("/default"); powerStats = IPowerStats::fromBinder( ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str()))); return powerStats; return getServiceSingleton<IPowerStats>(); } status_t RailEnergyDataProvider::fill(PowerStats *stat) const { if (stat == nullptr) return BAD_VALUE; auto powerStatsService = getPowerStatsService(); if (powerStatsService == nullptr) { LOG(ERROR) << "unable to get power.stats AIDL service"; return NO_INIT; } Loading Loading @@ -91,7 +77,6 @@ status_t PowerEntityResidencyDataProvider::fill(PowerStats* stat) const { if (stat == nullptr) return BAD_VALUE; auto powerStatsService = getPowerStatsService(); if (powerStatsService == nullptr) { LOG(ERROR) << "unable to get power.stats AIDL service"; return NO_INIT; } Loading
media/psh_utils/PowerStatsProvider.h +0 −1 Original line number Diff line number Diff line Loading @@ -17,7 +17,6 @@ #pragma once #include <psh_utils/PowerStatsCollector.h> #include <iostream> namespace android::media::psh_utils { Loading
media/psh_utils/include/psh_utils/ServiceSingleton.h 0 → 100644 +67 −0 Original line number Diff line number Diff line /* * Copyright (C) 2024 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include <android/binder_auto_utils.h> #include <android/binder_manager.h> #include <android-base/thread_annotations.h> #include <mutex> #include <utils/Log.h> #include <utils/Timers.h> namespace android::media::psh_utils { struct DefaultServiceTraits { static constexpr int64_t kThresholdRetryNs = 1'000'000'000; static constexpr int64_t kMaxRetries = 5; static constexpr const char* kServiceVersion = "/default"; static constexpr bool kShowLog = true; }; template<typename Service, typename ServiceTraits = DefaultServiceTraits> std::shared_ptr<Service> getServiceSingleton() { [[clang::no_destroy]] static constinit std::mutex m; [[clang::no_destroy]] static constinit std::shared_ptr<Service> service GUARDED_BY(m); static int64_t nextTryNs GUARDED_BY(m) = 0; static int64_t tries GUARDED_BY(m) = 0; std::lock_guard l(m); if (service || tries > ServiceTraits::kMaxRetries // try too many times || systemTime(SYSTEM_TIME_BOOTTIME) < nextTryNs) { // try too frequently. return service; } const auto serviceName = std::string(Service::descriptor) .append(ServiceTraits::kServiceVersion); service = Service::fromBinder( ::ndk::SpAIBinder(AServiceManager_checkService(serviceName.c_str()))); if (!service) { // If failed, set a time limit before retry. // No need to log an error, it is already done. nextTryNs = systemTime(SYSTEM_TIME_BOOTTIME) + ServiceTraits::kThresholdRetryNs; ALOGV_IF(ServiceTraits::kShowLog, "service:%s retries:%lld of %lld nextTryNs:%lld", Service::descriptor, (long long)tries, (long long)kMaxRetries, (long long)nextTryNs); ++tries; } return service; } } // namespace android::media::psh_utils