1 /* 2 * Copyright 2021 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 <aidl/android/hardware/power/BnPowerHintSession.h> 20 #include <aidl/android/hardware/power/WorkDuration.h> 21 #include <utils/Looper.h> 22 #include <utils/Thread.h> 23 24 #include <mutex> 25 #include <unordered_map> 26 27 namespace aidl { 28 namespace google { 29 namespace hardware { 30 namespace power { 31 namespace impl { 32 namespace pixel { 33 34 using aidl::android::hardware::power::BnPowerHintSession; 35 using aidl::android::hardware::power::WorkDuration; 36 using ::android::Message; 37 using ::android::MessageHandler; 38 using ::android::sp; 39 using std::chrono::milliseconds; 40 using std::chrono::nanoseconds; 41 using std::chrono::steady_clock; 42 using std::chrono::time_point; 43 44 static const int32_t kMaxUclampValue = 1024; 45 struct AppHintDesc { AppHintDescAppHintDesc46 AppHintDesc(int32_t tgid, int32_t uid, std::vector<int> threadIds) 47 : tgid(tgid), 48 uid(uid), 49 threadIds(std::move(threadIds)), 50 duration(0LL), 51 current_min(0), 52 is_active(true), 53 update_count(0), 54 integral_error(0), 55 previous_error(0) {} 56 std::string toString() const; 57 const int32_t tgid; 58 const int32_t uid; 59 const std::vector<int> threadIds; 60 nanoseconds duration; 61 int current_min; 62 // status 63 std::atomic<bool> is_active; 64 // pid 65 uint64_t update_count; 66 int64_t integral_error; 67 int64_t previous_error; 68 }; 69 70 class PowerHintSession : public BnPowerHintSession { 71 public: 72 explicit PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds, 73 int64_t durationNanos, nanoseconds adpfRate); 74 ~PowerHintSession(); 75 ndk::ScopedAStatus close() override; 76 ndk::ScopedAStatus pause() override; 77 ndk::ScopedAStatus resume() override; 78 ndk::ScopedAStatus updateTargetWorkDuration(int64_t targetDurationNanos) override; 79 ndk::ScopedAStatus reportActualWorkDuration( 80 const std::vector<WorkDuration> &actualDurations) override; 81 bool isActive(); 82 bool isStale(); 83 const std::vector<int> &getTidList() const; 84 85 private: 86 class StaleHandler : public MessageHandler { 87 public: StaleHandler(PowerHintSession * session)88 StaleHandler(PowerHintSession *session) 89 : mSession(session), mIsMonitoringStale(false), mLastUpdatedTime(steady_clock::now()) {} 90 void handleMessage(const Message &message) override; 91 void updateStaleTimer(); 92 time_point<steady_clock> getStaleTime(); 93 94 private: 95 PowerHintSession *mSession; 96 std::atomic<bool> mIsMonitoringStale; 97 std::atomic<time_point<steady_clock>> mLastUpdatedTime; 98 std::mutex mStaleLock; 99 }; 100 101 private: 102 void setStale(); 103 void updateUniveralBoostMode(); 104 int setUclamp(int32_t min, int32_t max = kMaxUclampValue); 105 std::string getIdString() const; 106 AppHintDesc *mDescriptor = nullptr; 107 sp<StaleHandler> mStaleHandler; 108 sp<MessageHandler> mPowerManagerHandler; 109 std::mutex mLock; 110 const nanoseconds kAdpfRate; 111 std::atomic<bool> mSessionClosed = false; 112 }; 113 114 } // namespace pixel 115 } // namespace impl 116 } // namespace power 117 } // namespace hardware 118 } // namespace google 119 } // namespace aidl 120