Andy Hung | 125ff52 | 2024-06-28 09:51:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2024 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | #include <android-base/logging.h> |
| 17 | #include <psh_utils/PowerStatsCollector.h> |
| 18 | #include "PowerStatsProvider.h" |
| 19 | #include <utils/Timers.h> |
| 20 | |
| 21 | namespace android::media::psh_utils { |
| 22 | |
| 23 | PowerStatsCollector::PowerStatsCollector() { |
| 24 | addProvider(std::make_unique<PowerEntityResidencyDataProvider>()); |
| 25 | addProvider(std::make_unique<RailEnergyDataProvider>()); |
Andy Hung | 44acd22 | 2024-07-16 20:32:44 -0700 | [diff] [blame] | 26 | addProvider(std::make_unique<HealthStatsDataProvider>()); |
Andy Hung | 125ff52 | 2024-06-28 09:51:30 -0700 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | /* static */ |
| 30 | PowerStatsCollector& PowerStatsCollector::getCollector() { |
| 31 | [[clang::no_destroy]] static PowerStatsCollector psc; |
| 32 | return psc; |
| 33 | } |
| 34 | |
Andy Hung | 1eb078c | 2024-08-13 16:14:00 -0700 | [diff] [blame] | 35 | std::shared_ptr<const PowerStats> PowerStatsCollector::getStats(int64_t toleranceNs) { |
| 36 | // Check if there is a cached PowerStats result available. |
| 37 | // As toleranceNs may be different between callers, it may be that some callers |
| 38 | // are blocked on mMutexExclusiveFill for a new stats result, while other callers |
| 39 | // may find the current cached result acceptable (within toleranceNs). |
| 40 | if (toleranceNs > 0) { |
| 41 | auto result = checkLastStats(toleranceNs); |
| 42 | if (result) return result; |
| 43 | } |
| 44 | |
| 45 | // Take the mMutexExclusiveFill to ensure only one thread is filling. |
| 46 | std::lock_guard lg1(mMutexExclusiveFill); |
| 47 | // As obtaining a new PowerStats snapshot might take some time, |
| 48 | // check again to see if another waiting thread filled the cached result for us. |
| 49 | if (toleranceNs > 0) { |
| 50 | auto result = checkLastStats(toleranceNs); |
| 51 | if (result) return result; |
| 52 | } |
Andy Hung | 125ff52 | 2024-06-28 09:51:30 -0700 | [diff] [blame] | 53 | auto result = std::make_shared<PowerStats>(); |
| 54 | (void)fill(result.get()); |
Andy Hung | 1eb078c | 2024-08-13 16:14:00 -0700 | [diff] [blame] | 55 | std::lock_guard lg2(mMutex); |
| 56 | mLastFetchNs = systemTime(SYSTEM_TIME_BOOTTIME); |
| 57 | mLastFetchStats = result; |
Andy Hung | 125ff52 | 2024-06-28 09:51:30 -0700 | [diff] [blame] | 58 | return result; |
| 59 | } |
| 60 | |
Andy Hung | 1eb078c | 2024-08-13 16:14:00 -0700 | [diff] [blame] | 61 | std::shared_ptr<const PowerStats> PowerStatsCollector::checkLastStats(int64_t toleranceNs) const { |
| 62 | if (toleranceNs > 0) { |
| 63 | // see if we can return an old result. |
| 64 | std::lock_guard lg(mMutex); |
| 65 | if (mLastFetchStats && systemTime(SYSTEM_TIME_BOOTTIME) - mLastFetchNs < toleranceNs) { |
| 66 | return mLastFetchStats; |
| 67 | } |
| 68 | } |
| 69 | return {}; |
| 70 | } |
| 71 | |
Andy Hung | 125ff52 | 2024-06-28 09:51:30 -0700 | [diff] [blame] | 72 | void PowerStatsCollector::addProvider(std::unique_ptr<PowerStatsProvider>&& powerStatsProvider) { |
| 73 | mPowerStatsProviders.emplace_back(std::move(powerStatsProvider)); |
| 74 | } |
| 75 | |
| 76 | int PowerStatsCollector::fill(PowerStats* stats) const { |
| 77 | if (!stats) { |
| 78 | LOG(ERROR) << __func__ << ": bad args; stat is null"; |
| 79 | return 1; |
| 80 | } |
| 81 | |
| 82 | for (const auto& provider : mPowerStatsProviders) { |
| 83 | if (provider->fill(stats) != 0) { |
| 84 | LOG(ERROR) << __func__ << ": a data provider failed"; |
| 85 | continue; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | // boot time follows wall clock time, but starts from boot. |
| 90 | stats->metadata.start_time_since_boot_ms = systemTime(SYSTEM_TIME_BOOTTIME) / 1'000'000; |
| 91 | |
| 92 | // wall clock time |
| 93 | stats->metadata.start_time_epoch_ms = systemTime(SYSTEM_TIME_REALTIME) / 1'000'000; |
| 94 | |
| 95 | // monotonic time follows boot time, but does not include any time suspended. |
| 96 | stats->metadata.start_time_monotonic_ms = systemTime() / 1'000'000; |
| 97 | return 0; |
| 98 | } |
| 99 | |
| 100 | } // namespace android::media::psh_utils |