Andy Hung | 88a7afe | 2024-08-12 20:00:46 -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 | |
| 17 | #include "AudioToken.h" |
| 18 | #define LOG_TAG "AudioPowerManager" |
| 19 | #include <com_android_media_audioserver.h> |
| 20 | #include <cutils/properties.h> |
| 21 | #include <utils/Log.h> |
| 22 | #include <psh_utils/AudioPowerManager.h> |
| 23 | |
| 24 | namespace android::media::psh_utils { |
| 25 | |
| 26 | /* static */ |
| 27 | AudioPowerManager& AudioPowerManager::getAudioPowerManager() { |
| 28 | [[clang::no_destroy]] static AudioPowerManager apm; |
| 29 | return apm; |
| 30 | } |
| 31 | |
| 32 | std::unique_ptr<Token> AudioPowerManager::startClient(pid_t pid, uid_t uid, |
| 33 | const std::string& additional) { |
| 34 | std::shared_ptr<PowerClientStats> powerClientStats; |
| 35 | std::lock_guard l(mMutex); |
| 36 | if (mPowerClientStats.count(uid) == 0) { |
| 37 | const auto it = mHistoricalClients.find(uid); |
| 38 | if (it == mHistoricalClients.end()) { |
| 39 | powerClientStats = std::make_shared<PowerClientStats>(uid, additional); |
| 40 | } else { |
| 41 | powerClientStats = it->second; |
| 42 | mHistoricalClients.erase(it); |
| 43 | } |
| 44 | mPowerClientStats[uid] = powerClientStats; |
| 45 | } else { |
| 46 | powerClientStats = mPowerClientStats[uid]; |
| 47 | } |
| 48 | powerClientStats->addPid(pid); |
| 49 | mPidToUid[pid] = uid; |
| 50 | std::unique_ptr<Token> token = |
| 51 | std::make_unique<AudioClientToken>(powerClientStats, pid, uid, additional); |
| 52 | mOutstandingTokens.emplace(token.get()); |
| 53 | return token; |
| 54 | } |
| 55 | |
| 56 | std::unique_ptr<Token> AudioPowerManager::startTrack(uid_t uid, const std::string& additional) { |
| 57 | std::lock_guard l(mMutex); |
| 58 | if (mPowerClientStats.count(uid) == 0) { |
| 59 | ALOGW("%s: Cannot find uid: %d", __func__, uid); |
| 60 | return {}; |
| 61 | } |
| 62 | auto powerClientStats = mPowerClientStats[uid]; |
| 63 | std::unique_ptr<Token> token = |
| 64 | std::make_unique<AudioTrackToken>(powerClientStats, additional); |
| 65 | mOutstandingTokens.emplace(token.get()); |
| 66 | return token; |
| 67 | } |
| 68 | |
| 69 | std::unique_ptr<Token> AudioPowerManager::startThread( |
| 70 | pid_t pid, const std::string& wakeLockName, |
| 71 | WakeFlag wakeFlag, const std::string& additional) { |
| 72 | std::lock_guard l(mMutex); |
| 73 | std::unique_ptr<Token> token = |
| 74 | std::make_unique<AudioThreadToken>(pid, wakeLockName, wakeFlag, additional); |
| 75 | mOutstandingTokens.emplace(token.get()); |
| 76 | return token; |
| 77 | } |
| 78 | |
| 79 | std::string AudioPowerManager::toString() const { |
| 80 | const std::string prefix(" "); |
| 81 | std::string result; |
| 82 | std::lock_guard l(mMutex); |
| 83 | result.append("Power Tokens:\n"); |
| 84 | std::vector<std::string> tokenInfo; |
| 85 | for (const auto& token: mOutstandingTokens) { |
| 86 | tokenInfo.emplace_back(token->toString()); |
| 87 | } |
| 88 | std::sort(tokenInfo.begin(), tokenInfo.end()); |
| 89 | for (const auto& info: tokenInfo) { |
| 90 | result.append(prefix).append(info).append("\n"); |
| 91 | } |
| 92 | result.append("Power Clients:\n"); |
| 93 | for (const auto& [uid, powerClientStats]: mPowerClientStats) { |
| 94 | result.append(powerClientStats->toString(true, prefix)); |
| 95 | } |
| 96 | result.append("Power Client History:\n"); |
| 97 | for (const auto& [power, powerClientStats]: mHistoricalClients) { |
| 98 | result.append(powerClientStats->toString(true, prefix)); |
| 99 | } |
| 100 | return result; |
| 101 | } |
| 102 | |
| 103 | void AudioPowerManager::stopClient(pid_t pid) { |
| 104 | std::lock_guard l(mMutex); |
| 105 | const auto pidit = mPidToUid.find(pid); |
| 106 | if (pidit == mPidToUid.end()) return; |
| 107 | const uid_t uid = pidit->second; |
| 108 | const auto it = mPowerClientStats.find(uid); |
| 109 | if (it == mPowerClientStats.end()) return; |
| 110 | |
| 111 | auto powerClientStats = it->second; |
| 112 | size_t count = powerClientStats->removePid(pid); |
| 113 | if (count == 0) { |
| 114 | mHistoricalClients[uid] = powerClientStats; |
| 115 | mPowerClientStats.erase(it); |
| 116 | if (mHistoricalClients.size() > kHistory) { |
| 117 | mHistoricalClients.erase(mHistoricalClients.begin()); // remove oldest. |
| 118 | } |
| 119 | } |
| 120 | mPidToUid.erase(pid); |
| 121 | } |
| 122 | |
| 123 | void AudioPowerManager::clear_token_ptr(Token* token) { |
| 124 | if (token != nullptr) { |
| 125 | std::lock_guard l(mMutex); |
| 126 | (void)mOutstandingTokens.erase(token); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /* static */ |
| 131 | bool AudioPowerManager::enabled() { |
| 132 | static const bool enabled = com::android::media::audioserver::power_stats() |
| 133 | && property_get_bool("persist.audio.power_stats.enabled", false); |
| 134 | return enabled; |
| 135 | } |
| 136 | |
| 137 | } // namespace android::media::psh_utils |