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 <psh_utils/PowerClientStats.h> |
| 18 | #include <mediautils/ServiceUtilities.h> |
| 19 | |
| 20 | namespace android::media::psh_utils { |
| 21 | |
| 22 | /* static */ |
| 23 | audio_utils::CommandThread& PowerClientStats::getCommandThread() { |
| 24 | [[clang::no_destroy]] static audio_utils::CommandThread ct; |
| 25 | return ct; |
| 26 | } |
| 27 | |
| 28 | PowerClientStats::PowerClientStats(uid_t uid, const std::string& additional) |
| 29 | : mUid(uid), mAdditional(additional) {} |
| 30 | |
| 31 | void PowerClientStats::start(int64_t actualNs) { |
| 32 | std::lock_guard l(mMutex); |
| 33 | ++mTokenCount; |
| 34 | if (mStartNs == 0) mStartNs = actualNs; |
| 35 | if (mStartStats) return; |
| 36 | mStartStats = PowerStatsCollector::getCollector().getStats(kStatTimeToleranceNs); |
| 37 | } |
| 38 | |
| 39 | void PowerClientStats::stop(int64_t actualNs) { |
| 40 | std::lock_guard l(mMutex); |
| 41 | if (--mTokenCount > 0) return; |
| 42 | if (mStartNs != 0) mDeltaNs += actualNs - mStartNs; |
| 43 | mStartNs = 0; |
| 44 | if (!mStartStats) return; |
| 45 | const auto stopStats = PowerStatsCollector::getCollector().getStats(kStatTimeToleranceNs); |
| 46 | if (stopStats && stopStats != mStartStats) { |
| 47 | *mDeltaStats += *stopStats - *mStartStats; |
| 48 | } |
| 49 | mStartStats.reset(); |
| 50 | } |
| 51 | |
| 52 | void PowerClientStats::addPid(pid_t pid) { |
| 53 | std::lock_guard l(mMutex); |
| 54 | mPids.emplace(pid); |
| 55 | } |
| 56 | |
| 57 | size_t PowerClientStats::removePid(pid_t pid) { |
| 58 | std::lock_guard l(mMutex); |
| 59 | mPids.erase(pid); |
| 60 | return mPids.size(); |
| 61 | } |
| 62 | |
| 63 | std::string PowerClientStats::toString(bool stats, const std::string& prefix) const { |
| 64 | std::lock_guard l(mMutex); |
| 65 | |
| 66 | // Adjust delta time and stats if currently running. |
| 67 | auto deltaStats = mDeltaStats; |
| 68 | auto deltaNs = mDeltaNs; |
| 69 | if (mStartNs) deltaNs += systemTime(SYSTEM_TIME_BOOTTIME) - mStartNs; |
| 70 | if (mStartStats) { |
| 71 | const auto stopStats = PowerStatsCollector::getCollector().getStats(kStatTimeToleranceNs); |
| 72 | if (stopStats && stopStats != mStartStats) { |
| 73 | auto newStats = std::make_shared<PowerStats>(*deltaStats); |
| 74 | *newStats += *stopStats - *mStartStats; |
| 75 | deltaStats = newStats; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | std::string result(prefix); |
| 80 | result.append("uid: ") |
| 81 | .append(std::to_string(mUid)) |
| 82 | .append(" ").append(mediautils::UidInfo::getInfo(mUid)->package) |
| 83 | .append(" streams: ").append(std::to_string(mTokenCount)) |
| 84 | .append(" seconds: ").append(std::to_string(deltaNs * 1e-9)); |
| 85 | result.append(" {"); |
| 86 | for (auto pid : mPids) { |
| 87 | result.append(" ").append(std::to_string(pid)); |
| 88 | } |
| 89 | result.append(" }"); |
| 90 | if (!mAdditional.empty()) { |
| 91 | result.append("\n").append(prefix).append(mAdditional); |
| 92 | } |
| 93 | if (stats) { |
| 94 | std::string prefix2(prefix); |
| 95 | prefix2.append(" "); |
| 96 | result.append("\n").append(deltaStats->normalizedEnergy(prefix2)); |
| 97 | } |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | } // namespace android::media::psh_utils |