• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <perfmgr/HintManager.h>
21 #include <utils/Looper.h>
22 #include <utils/Thread.h>
23 
24 #include <array>
25 #include <unordered_map>
26 
27 #include "AdpfTypes.h"
28 #include "AppDescriptorTrace.h"
29 #include "PowerSessionManager.h"
30 #include "SessionRecords.h"
31 
32 namespace aidl {
33 namespace google {
34 namespace hardware {
35 namespace power {
36 namespace impl {
37 namespace pixel {
38 
39 using aidl::android::hardware::power::BnPowerHintSession;
40 using ::android::Message;
41 using ::android::MessageHandler;
42 using ::android::sp;
43 using std::chrono::milliseconds;
44 using std::chrono::nanoseconds;
45 using std::chrono::steady_clock;
46 using std::chrono::time_point;
47 
48 // The Power Hint Session is responsible for providing an
49 // interface for creating, updating, and closing power hints
50 // for a Session. Each sesion that is mapped to multiple
51 // threads (or task ids).
52 template <class HintManagerT = ::android::perfmgr::HintManager,
53           class PowerSessionManagerT = PowerSessionManager<>>
54 class PowerHintSession : public BnPowerHintSession, public Immobile {
55   public:
56     explicit PowerHintSession(int32_t tgid, int32_t uid, const std::vector<int32_t> &threadIds,
57                               int64_t durationNanos, SessionTag tag);
58     ~PowerHintSession();
59     ndk::ScopedAStatus close() override;
60     ndk::ScopedAStatus pause() override;
61     ndk::ScopedAStatus resume() override;
62     ndk::ScopedAStatus updateTargetWorkDuration(int64_t targetDurationNanos) override;
63     ndk::ScopedAStatus reportActualWorkDuration(
64             const std::vector<WorkDuration> &actualDurations) override;
65     ndk::ScopedAStatus sendHint(SessionHint hint) override;
66     ndk::ScopedAStatus setMode(SessionMode mode, bool enabled) override;
67     ndk::ScopedAStatus setThreads(const std::vector<int32_t> &threadIds) override;
68     ndk::ScopedAStatus getSessionConfig(SessionConfig *_aidl_return) override;
69 
70     void dumpToStream(std::ostream &stream);
71     SessionTag getSessionTag() const;
72 
73   private:
74     // In practice this lock should almost never get contested, but it's necessary for FMQ
75     std::mutex mPowerHintSessionLock;
76     bool isTimeout() REQUIRES(mPowerHintSessionLock);
77     // Is hint session for a user application
78     bool isAppSession() REQUIRES(mPowerHintSessionLock);
79     void tryToSendPowerHint(std::string hint) REQUIRES(mPowerHintSessionLock);
80     void updatePidControlVariable(int pidControlVariable, bool updateVote = true)
81             REQUIRES(mPowerHintSessionLock);
82     int64_t convertWorkDurationToBoostByPid(const std::vector<WorkDuration> &actualDurations)
83             REQUIRES(mPowerHintSessionLock);
84     bool updateHeuristicBoost() REQUIRES(mPowerHintSessionLock);
85 
86     // Data
87     PowerSessionManagerT *mPSManager;
88     const int64_t mSessionId = 0;
89     const std::string mIdString;
90     std::shared_ptr<AppHintDesc> mDescriptor GUARDED_BY(mPowerHintSessionLock);
91 
92     // Trace strings, this is thread safe since only assigned during construction
93     std::shared_ptr<AppDescriptorTrace> mAppDescriptorTrace;
94     time_point<steady_clock> mLastUpdatedTime GUARDED_BY(mPowerHintSessionLock);
95     bool mSessionClosed GUARDED_BY(mPowerHintSessionLock) = false;
96     // Are cpu load change related hints are supported
97     std::unordered_map<std::string, std::optional<bool>> mSupportedHints
98             GUARDED_BY(mPowerHintSessionLock);
99     // Use the value of the last enum in enum_range +1 as array size
GUARDED_BY(mPowerHintSessionLock)100     std::array<bool, enum_size<SessionMode>()> mModes GUARDED_BY(mPowerHintSessionLock){};
101     // Tag labeling what kind of session this is
102     const SessionTag mTag;
103     std::unique_ptr<SessionRecords> mSessionRecords GUARDED_BY(mPowerHintSessionLock) = nullptr;
GUARDED_BY(mPowerHintSessionLock)104     bool mHeuristicBoostActive GUARDED_BY(mPowerHintSessionLock){false};
105 };
106 
107 }  // namespace pixel
108 }  // namespace impl
109 }  // namespace power
110 }  // namespace hardware
111 }  // namespace google
112 }  // namespace aidl
113