• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 <android-base/logging.h>
18 #include <android/binder_manager.h>
19 #include <binder/IPCThreadState.h>
20 #include <binder/IServiceManager.h>
21 #include <binder/ProcessState.h>
22 #include <cutils/native_handle.h>
23 #include <hidl/HidlTransportSupport.h>
24 #include <hwbinder/ProcessState.h>
25 
26 #include <fcntl.h>
27 #include <sys/socket.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 
32 #include <SuspendProperties.sysprop.h>
33 
34 #include "SuspendControlService.h"
35 #include "SystemSuspend.h"
36 #include "SystemSuspendAidl.h"
37 #include "SystemSuspendHidl.h"
38 
39 using aidl::android::system::suspend::SystemSuspendAidl;
40 using android::sp;
41 using android::status_t;
42 using android::String16;
43 using android::base::Socketpair;
44 using android::base::unique_fd;
45 using android::hardware::configureRpcThreadpool;
46 using android::hardware::joinRpcThreadpool;
47 using android::system::suspend::V1_0::ISystemSuspend;
48 using android::system::suspend::V1_0::SleepTimeConfig;
49 using android::system::suspend::V1_0::SuspendControlService;
50 using android::system::suspend::V1_0::SuspendControlServiceInternal;
51 using android::system::suspend::V1_0::SystemSuspend;
52 using android::system::suspend::V1_0::SystemSuspendHidl;
53 using namespace std::chrono_literals;
54 using namespace ::android::sysprop;
55 
56 static constexpr size_t kStatsCapacity = 1000;
57 static constexpr char kSysClassWakeup[] = "/sys/class/wakeup";
58 static constexpr char kSysPowerSuspendStats[] = "/sys/power/suspend_stats";
59 static constexpr char kSysPowerWakeupCount[] = "/sys/power/wakeup_count";
60 static constexpr char kSysPowerState[] = "/sys/power/state";
61 // TODO(b/120445600): Use upstream mechanism for wakeup reasons once available
62 static constexpr char kSysKernelWakeupReasons[] = "/sys/kernel/wakeup_reasons/last_resume_reason";
63 static constexpr char kSysKernelSuspendTime[] = "/sys/kernel/wakeup_reasons/last_suspend_time";
64 
65 static constexpr uint32_t kDefaultMaxSleepTimeMillis = 60000;
66 static constexpr uint32_t kDefaultBaseSleepTimeMillis = 100;
67 static constexpr double kDefaultSleepTimeScaleFactor = 2.0;
68 static constexpr uint32_t kDefaultBackoffThresholdCount = 0;
69 static constexpr uint32_t kDefaultShortSuspendThresholdMillis = 0;
70 static constexpr bool kDefaultFailedSuspendBackoffEnabled = true;
71 static constexpr bool kDefaultShortSuspendBackoffEnabled = false;
72 
main()73 int main() {
74     unique_fd wakeupCountFd{TEMP_FAILURE_RETRY(open(kSysPowerWakeupCount, O_CLOEXEC | O_RDWR))};
75     if (wakeupCountFd < 0) {
76         PLOG(ERROR) << "error opening " << kSysPowerWakeupCount;
77     }
78     unique_fd stateFd{TEMP_FAILURE_RETRY(open(kSysPowerState, O_CLOEXEC | O_RDWR))};
79     if (stateFd < 0) {
80         PLOG(ERROR) << "error opening " << kSysPowerState;
81     }
82     unique_fd kernelWakelockStatsFd{
83         TEMP_FAILURE_RETRY(open(kSysClassWakeup, O_DIRECTORY | O_CLOEXEC | O_RDONLY))};
84     if (kernelWakelockStatsFd < 0) {
85         PLOG(ERROR) << "SystemSuspend: Error opening " << kSysClassWakeup;
86     }
87     unique_fd suspendStatsFd{
88         TEMP_FAILURE_RETRY(open(kSysPowerSuspendStats, O_DIRECTORY | O_CLOEXEC | O_RDONLY))};
89     if (suspendStatsFd < 0) {
90         PLOG(ERROR) << "SystemSuspend: Error opening " << kSysPowerSuspendStats;
91     }
92     unique_fd wakeupReasonsFd{
93         TEMP_FAILURE_RETRY(open(kSysKernelWakeupReasons, O_CLOEXEC | O_RDONLY))};
94     if (wakeupReasonsFd < 0) {
95         PLOG(ERROR) << "SystemSuspend: Error opening " << kSysKernelWakeupReasons;
96     }
97     unique_fd suspendTimeFd{TEMP_FAILURE_RETRY(open(kSysKernelSuspendTime, O_CLOEXEC | O_RDONLY))};
98     if (suspendTimeFd < 0) {
99         PLOG(ERROR) << "SystemSuspend: Error opening " << kSysKernelSuspendTime;
100     }
101 
102     // If either /sys/power/wakeup_count or /sys/power/state fail to open, we construct
103     // SystemSuspend with blocking fds. This way this process will keep running, handle wake lock
104     // requests, collect stats, but won't suspend the device. We want this behavior on devices
105     // (hosts) where system suspend should not be handles by Android platform e.g. ARC++, Android
106     // virtual devices.
107     if (wakeupCountFd < 0 || stateFd < 0) {
108         // This will block all reads/writes to these fds from the suspend thread.
109         Socketpair(SOCK_STREAM, &wakeupCountFd, &stateFd);
110     }
111 
112     SleepTimeConfig sleepTimeConfig = {
113         .baseSleepTime = std::chrono::milliseconds(
114             SuspendProperties::base_sleep_time_millis().value_or(kDefaultBaseSleepTimeMillis)),
115         .maxSleepTime = std::chrono::milliseconds(
116             SuspendProperties::max_sleep_time_millis().value_or(kDefaultMaxSleepTimeMillis)),
117         .sleepTimeScaleFactor =
118             SuspendProperties::sleep_time_scale_factor().value_or(kDefaultSleepTimeScaleFactor),
119         .backoffThreshold =
120             SuspendProperties::backoff_threshold_count().value_or(kDefaultBackoffThresholdCount),
121         .shortSuspendThreshold =
122             std::chrono::milliseconds(SuspendProperties::short_suspend_threshold_millis().value_or(
123                 kDefaultShortSuspendThresholdMillis)),
124         .failedSuspendBackoffEnabled = SuspendProperties::failed_suspend_backoff_enabled().value_or(
125             kDefaultFailedSuspendBackoffEnabled),
126         .shortSuspendBackoffEnabled = SuspendProperties::short_suspend_backoff_enabled().value_or(
127             kDefaultShortSuspendBackoffEnabled),
128     };
129 
130     configureRpcThreadpool(1, true /* callerWillJoin */);
131 
132     sp<SuspendControlService> suspendControl = new SuspendControlService();
133     auto controlStatus =
134         android::defaultServiceManager()->addService(String16("suspend_control"), suspendControl);
135     if (controlStatus != android::OK) {
136         LOG(FATAL) << "Unable to register suspend_control service: " << controlStatus;
137     }
138 
139     sp<SuspendControlServiceInternal> suspendControlInternal = new SuspendControlServiceInternal();
140     controlStatus = android::defaultServiceManager()->addService(
141         String16("suspend_control_internal"), suspendControlInternal);
142     if (controlStatus != android::OK) {
143         LOG(FATAL) << "Unable to register suspend_control_internal service: " << controlStatus;
144     }
145 
146     // Create non-HW binder threadpool for SuspendControlService.
147     sp<android::ProcessState> ps{android::ProcessState::self()};
148     ps->startThreadPool();
149 
150     sp<SystemSuspend> suspend = new SystemSuspend(
151         std::move(wakeupCountFd), std::move(stateFd), std::move(suspendStatsFd), kStatsCapacity,
152         std::move(kernelWakelockStatsFd), std::move(wakeupReasonsFd), std::move(suspendTimeFd),
153         sleepTimeConfig, suspendControl, suspendControlInternal, true /* mUseSuspendCounter*/);
154 
155     std::shared_ptr<SystemSuspendAidl> suspendAidl =
156         ndk::SharedRefBase::make<SystemSuspendAidl>(suspend.get());
157     const std::string suspendAidlInstance =
158         std::string() + SystemSuspendAidl::descriptor + "/default";
159     auto aidlStatus =
160         AServiceManager_addService(suspendAidl->asBinder().get(), suspendAidlInstance.c_str());
161     CHECK_EQ(aidlStatus, STATUS_OK)
162         << "Unable to register system-suspend AIDL service: " << aidlStatus;
163 
164     sp<SystemSuspendHidl> suspendHidl = new SystemSuspendHidl(suspend.get());
165     status_t hidlStatus = suspendHidl->registerAsService();
166     if (android::OK != hidlStatus) {
167         LOG(INFO) << "system-suspend HIDL hal not supported, use the AIDL suspend hal for "
168                      "requesting wakelocks";
169     }
170 
171     joinRpcThreadpool();
172     std::abort(); /* unreachable */
173 }
174