blob: 65f65a44649942b6677bb5643a8d28640092302b [file] [log] [blame]
Andy Hung88a7afe2024-08-12 20:00:46 -07001/*
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
20namespace android::media::psh_utils {
21
22/* static */
23audio_utils::CommandThread& PowerClientStats::getCommandThread() {
24 [[clang::no_destroy]] static audio_utils::CommandThread ct;
25 return ct;
26}
27
28PowerClientStats::PowerClientStats(uid_t uid, const std::string& additional)
29 : mUid(uid), mAdditional(additional) {}
30
31void 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
39void PowerClientStats::stop(int64_t actualNs) {
40 std::lock_guard l(mMutex);
41 if (--mTokenCount > 0) return;
Andy Hung8cc27ea2024-09-18 17:17:37 -070042 if (mStartNs != 0) mCumulativeNs += actualNs - mStartNs;
Andy Hung88a7afe2024-08-12 20:00:46 -070043 mStartNs = 0;
44 if (!mStartStats) return;
45 const auto stopStats = PowerStatsCollector::getCollector().getStats(kStatTimeToleranceNs);
46 if (stopStats && stopStats != mStartStats) {
Andy Hung8cc27ea2024-09-18 17:17:37 -070047 *mCumulativeStats += *stopStats - *mStartStats;
Andy Hung88a7afe2024-08-12 20:00:46 -070048 }
49 mStartStats.reset();
50}
51
52void PowerClientStats::addPid(pid_t pid) {
53 std::lock_guard l(mMutex);
54 mPids.emplace(pid);
55}
56
57size_t PowerClientStats::removePid(pid_t pid) {
58 std::lock_guard l(mMutex);
59 mPids.erase(pid);
60 return mPids.size();
61}
62
63std::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.
Andy Hung8cc27ea2024-09-18 17:17:37 -070067 auto cumulativeStats = mCumulativeStats;
68 auto cumulativeNs = mCumulativeNs;
69 if (mStartNs) cumulativeNs += systemTime(SYSTEM_TIME_BOOTTIME) - mStartNs;
Andy Hung88a7afe2024-08-12 20:00:46 -070070 if (mStartStats) {
71 const auto stopStats = PowerStatsCollector::getCollector().getStats(kStatTimeToleranceNs);
72 if (stopStats && stopStats != mStartStats) {
Andy Hung8cc27ea2024-09-18 17:17:37 -070073 auto newStats = std::make_shared<PowerStats>(*cumulativeStats);
Andy Hung88a7afe2024-08-12 20:00:46 -070074 *newStats += *stopStats - *mStartStats;
Andy Hung8cc27ea2024-09-18 17:17:37 -070075 cumulativeStats = newStats;
Andy Hung88a7afe2024-08-12 20:00:46 -070076 }
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))
Andy Hung8cc27ea2024-09-18 17:17:37 -070084 .append(" seconds: ").append(std::to_string(cumulativeNs * 1e-9));
Andy Hung88a7afe2024-08-12 20:00:46 -070085 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(" ");
Andy Hung8cc27ea2024-09-18 17:17:37 -070096 result.append("\n").append(cumulativeStats->normalizedEnergy(prefix2));
Andy Hung88a7afe2024-08-12 20:00:46 -070097 }
98 return result;
99}
100
101} // namespace android::media::psh_utils