system/hardware/interfaces
Révision | fe11412c1508b3010c54fb6f34e3120179209289 (tree) |
---|---|
l'heure | 2019-03-26 09:14:18 |
Auteur | Tri Vo <trong@goog...> |
Commiter | android-build-merger |
Ability to choose either suspend counter or /sys/power/wake_lock. am: a833d47822 am: 85ad4a9a10
am: f7791b77c5
Change-Id: I23fa71e80b2ad2c6812407fa3adfe62e26fbc749
@@ -42,6 +42,10 @@ namespace suspend { | ||
42 | 42 | namespace V1_0 { |
43 | 43 | |
44 | 44 | static const char kSleepState[] = "mem"; |
45 | +// TODO(b/128923994): we only need /sys/power/wake_[un]lock to export debugging info via | |
46 | +// /sys/kernel/debug/wakeup_sources. | |
47 | +static constexpr char kSysPowerWakeLock[] = "/sys/power/wake_lock"; | |
48 | +static constexpr char kSysPowerWakeUnlock[] = "/sys/power/wake_unlock"; | |
45 | 49 | |
46 | 50 | // This function assumes that data in fd is small enough that it can be read in one go. |
47 | 51 | // We use this function instead of the ones available in libbase because it doesn't block |
@@ -67,9 +71,9 @@ TimestampType getEpochTimeNow() { | ||
67 | 71 | return std::chrono::duration_cast<std::chrono::microseconds>(timeSinceEpoch).count(); |
68 | 72 | } |
69 | 73 | |
70 | -WakeLock::WakeLock(SystemSuspend* systemSuspend, const WakeLockIdType& id) | |
71 | - : mReleased(), mSystemSuspend(systemSuspend), mId(id) { | |
72 | - mSystemSuspend->incSuspendCounter(); | |
74 | +WakeLock::WakeLock(SystemSuspend* systemSuspend, const WakeLockIdType& id, const string& name) | |
75 | + : mReleased(), mSystemSuspend(systemSuspend), mId(id), mName(name) { | |
76 | + mSystemSuspend->incSuspendCounter(mName); | |
73 | 77 | } |
74 | 78 | |
75 | 79 | WakeLock::~WakeLock() { |
@@ -83,22 +87,37 @@ Return<void> WakeLock::release() { | ||
83 | 87 | |
84 | 88 | void WakeLock::releaseOnce() { |
85 | 89 | std::call_once(mReleased, [this]() { |
86 | - mSystemSuspend->decSuspendCounter(); | |
90 | + mSystemSuspend->decSuspendCounter(mName); | |
87 | 91 | mSystemSuspend->deleteWakeLockStatsEntry(mId); |
88 | 92 | }); |
89 | 93 | } |
90 | 94 | |
91 | 95 | SystemSuspend::SystemSuspend(unique_fd wakeupCountFd, unique_fd stateFd, size_t maxStatsEntries, |
92 | 96 | std::chrono::milliseconds baseSleepTime, |
93 | - const sp<SuspendControlService>& controlService) | |
97 | + const sp<SuspendControlService>& controlService, | |
98 | + bool useSuspendCounter) | |
94 | 99 | : mSuspendCounter(0), |
95 | 100 | mWakeupCountFd(std::move(wakeupCountFd)), |
96 | 101 | mStateFd(std::move(stateFd)), |
97 | 102 | mMaxStatsEntries(maxStatsEntries), |
98 | 103 | mBaseSleepTime(baseSleepTime), |
99 | 104 | mSleepTime(baseSleepTime), |
100 | - mControlService(controlService) { | |
105 | + mControlService(controlService), | |
106 | + mUseSuspendCounter(useSuspendCounter), | |
107 | + mWakeLockFd(-1), | |
108 | + mWakeUnlockFd(-1) { | |
101 | 109 | mControlService->setSuspendService(this); |
110 | + | |
111 | + if (!mUseSuspendCounter) { | |
112 | + mWakeLockFd.reset(TEMP_FAILURE_RETRY(open(kSysPowerWakeLock, O_CLOEXEC | O_RDWR))); | |
113 | + if (mWakeLockFd < 0) { | |
114 | + PLOG(ERROR) << "error opening " << kSysPowerWakeLock; | |
115 | + } | |
116 | + mWakeUnlockFd.reset(TEMP_FAILURE_RETRY(open(kSysPowerWakeUnlock, O_CLOEXEC | O_RDWR))); | |
117 | + if (mWakeUnlockFd < 0) { | |
118 | + PLOG(ERROR) << "error opening " << kSysPowerWakeUnlock; | |
119 | + } | |
120 | + } | |
102 | 121 | } |
103 | 122 | |
104 | 123 | bool SystemSuspend::enableAutosuspend() { |
@@ -133,7 +152,7 @@ Return<sp<IWakeLock>> SystemSuspend::acquireWakeLock(WakeLockType /* type */, | ||
133 | 152 | const hidl_string& name) { |
134 | 153 | auto pid = getCallingPid(); |
135 | 154 | auto wlId = getWakeLockId(pid, name); |
136 | - IWakeLock* wl = new WakeLock{this, wlId}; | |
155 | + IWakeLock* wl = new WakeLock{this, wlId, name}; | |
137 | 156 | { |
138 | 157 | auto l = std::lock_guard(mStatsLock); |
139 | 158 |
@@ -174,15 +193,27 @@ Return<void> SystemSuspend::debug(const hidl_handle& handle, | ||
174 | 193 | return Void(); |
175 | 194 | } |
176 | 195 | |
177 | -void SystemSuspend::incSuspendCounter() { | |
196 | +void SystemSuspend::incSuspendCounter(const string& name) { | |
178 | 197 | auto l = std::lock_guard(mCounterLock); |
179 | - mSuspendCounter++; | |
198 | + if (mUseSuspendCounter) { | |
199 | + mSuspendCounter++; | |
200 | + } else { | |
201 | + if (!WriteStringToFd(name, mWakeLockFd)) { | |
202 | + PLOG(ERROR) << "error writing " << name << " to " << kSysPowerWakeLock; | |
203 | + } | |
204 | + } | |
180 | 205 | } |
181 | 206 | |
182 | -void SystemSuspend::decSuspendCounter() { | |
207 | +void SystemSuspend::decSuspendCounter(const string& name) { | |
183 | 208 | auto l = std::lock_guard(mCounterLock); |
184 | - if (--mSuspendCounter == 0) { | |
185 | - mCounterCondVar.notify_one(); | |
209 | + if (mUseSuspendCounter) { | |
210 | + if (--mSuspendCounter == 0) { | |
211 | + mCounterCondVar.notify_one(); | |
212 | + } | |
213 | + } else { | |
214 | + if (!WriteStringToFd(name, mWakeUnlockFd)) { | |
215 | + PLOG(ERROR) << "error writing " << name << " to " << kSysPowerWakeUnlock; | |
216 | + } | |
186 | 217 | } |
187 | 218 | } |
188 | 219 |
@@ -53,7 +53,7 @@ TimestampType getEpochTimeNow(); | ||
53 | 53 | |
54 | 54 | class WakeLock : public IWakeLock { |
55 | 55 | public: |
56 | - WakeLock(SystemSuspend* systemSuspend, const WakeLockIdType& id); | |
56 | + WakeLock(SystemSuspend* systemSuspend, const WakeLockIdType& id, const std::string& name); | |
57 | 57 | ~WakeLock(); |
58 | 58 | |
59 | 59 | Return<void> release(); |
@@ -64,17 +64,18 @@ class WakeLock : public IWakeLock { | ||
64 | 64 | |
65 | 65 | SystemSuspend* mSystemSuspend; |
66 | 66 | WakeLockIdType mId; |
67 | + std::string mName; | |
67 | 68 | }; |
68 | 69 | |
69 | 70 | class SystemSuspend : public ISystemSuspend { |
70 | 71 | public: |
71 | 72 | SystemSuspend(unique_fd wakeupCountFd, unique_fd stateFd, size_t maxStatsEntries, |
72 | 73 | std::chrono::milliseconds baseSleepTime, |
73 | - const sp<SuspendControlService>& controlService); | |
74 | + const sp<SuspendControlService>& controlService, bool useSuspendCounter = true); | |
74 | 75 | Return<sp<IWakeLock>> acquireWakeLock(WakeLockType type, const hidl_string& name) override; |
75 | 76 | Return<void> debug(const hidl_handle& handle, const hidl_vec<hidl_string>& options) override; |
76 | - void incSuspendCounter(); | |
77 | - void decSuspendCounter(); | |
77 | + void incSuspendCounter(const std::string& name); | |
78 | + void decSuspendCounter(const std::string& name); | |
78 | 79 | void deleteWakeLockStatsEntry(WakeLockIdType id); |
79 | 80 | bool enableAutosuspend(); |
80 | 81 | bool forceSuspend(); |
@@ -107,6 +108,13 @@ class SystemSuspend : public ISystemSuspend { | ||
107 | 108 | void updateSleepTime(bool success); |
108 | 109 | |
109 | 110 | sp<SuspendControlService> mControlService; |
111 | + | |
112 | + // If true, use mSuspendCounter to keep track of native wake locks. Otherwise, rely on | |
113 | + // /sys/power/wake_lock interface to block suspend. | |
114 | + // TODO(b/128923994): remove dependency on /sys/power/wake_lock interface. | |
115 | + bool mUseSuspendCounter; | |
116 | + unique_fd mWakeLockFd; | |
117 | + unique_fd mWakeUnlockFd; | |
110 | 118 | }; |
111 | 119 | |
112 | 120 | } // namespace V1_0 |
@@ -1,4 +1,5 @@ | ||
1 | 1 | service system_suspend /system/bin/hw/android.system.suspend@1.0-service |
2 | 2 | class hal |
3 | 3 | user system |
4 | - group system | |
4 | + group system wakelock | |
5 | + capabilities BLOCK_SUSPEND |
@@ -81,7 +81,7 @@ int main() { | ||
81 | 81 | |
82 | 82 | sp<SystemSuspend> suspend = |
83 | 83 | new SystemSuspend(std::move(wakeupCountFd), std::move(stateFd), 100 /* maxStatsEntries */, |
84 | - 100ms /* baseSleepTime */, suspendControl); | |
84 | + 100ms /* baseSleepTime */, suspendControl, false /* mUseSuspendCounter*/); | |
85 | 85 | status_t status = suspend->registerAsService(); |
86 | 86 | if (android::OK != status) { |
87 | 87 | LOG(FATAL) << "Unable to register system-suspend service: " << status; |