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 #pragma once 18 19 #include "PowerStats.h" 20 #include "PowerStatsCollector.h" 21 22 #include <android-base/thread_annotations.h> 23 #include <audio_utils/CommandThread.h> 24 #include <memory> 25 #include <set> 26 27 namespace android::media::psh_utils { 28 29 /** 30 * PowerClientStats accumulates power measurements based on start and stop events. 31 * 32 * The start and stop events must eventually be matched, but several start events 33 * in a row only results in the power counted once. 34 */ 35 class PowerClientStats { 36 public: 37 // A command thread is used for tokens to dispatch start and stop sequentially 38 // with less overhead to the caller. 39 static audio_utils::CommandThread& getCommandThread(); 40 41 /** 42 * Creates an UID based power stat tracker. 43 * 44 * @param uid uid of app 45 * @param additional string to be printed out. 46 */ 47 PowerClientStats(uid_t uid, const std::string& additional); 48 49 /** 50 * Starts power tracking. 51 */ 52 void start(int64_t actualNs) EXCLUDES(mMutex); 53 54 /** 55 * Stops power tracking (saves the difference) - must be paired with start(). 56 */ 57 void stop(int64_t actualNs) EXCLUDES(mMutex); 58 59 /** 60 * Adds a pid to the App for string printing. 61 */ 62 void addPid(pid_t pid) EXCLUDES(mMutex); 63 64 /** 65 * Removes the pid from the App for string printing. 66 */ 67 size_t removePid(pid_t pid) EXCLUDES(mMutex); 68 69 /** 70 * Returns the string info. 71 * @param stats if true returns the stats. 72 * @return stat string. 73 */ 74 std::string toString(bool stats = false, const std::string& prefix = {}) 75 const EXCLUDES(mMutex); 76 77 private: 78 // Snapshots are taken no more often than 500ms. 79 static constexpr int64_t kStatTimeToleranceNs = 500'000'000; 80 81 mutable std::mutex mMutex; 82 const uid_t mUid; 83 const std::string mName; 84 const std::string mAdditional; 85 std::set<pid_t> mPids GUARDED_BY(mMutex); // pids sharing same uid 86 int64_t mTokenCount GUARDED_BY(mMutex) = 0; 87 int64_t mStartNs GUARDED_BY(mMutex) = 0; 88 std::shared_ptr<const PowerStats> mStartStats GUARDED_BY(mMutex); 89 90 // Cumulative time while active: sum of deltas of (stop - start). 91 int64_t mCumulativeNs GUARDED_BY(mMutex) = 0; 92 // Cumulative stats while active: sum of deltas of (stop - start), 93 // where snapshots are quantized to ~500ms accuracy. 94 std::shared_ptr<PowerStats> mCumulativeStats GUARDED_BY(mMutex) = 95 std::make_shared<PowerStats>(); 96 }; 97 98 } // namespace android::media::psh_utils 99