Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 17 | #define LOG_TAG "HealthLoop" |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 18 | #define KLOG_LEVEL 6 |
| 19 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 20 | #include <health/HealthLoop.h> |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 21 | |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <libgen.h> |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | #include <sys/epoll.h> |
| 28 | #include <sys/timerfd.h> |
| 29 | #include <unistd.h> |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 30 | |
| 31 | #include <android-base/logging.h> |
| 32 | #include <batteryservice/BatteryService.h> |
| 33 | #include <cutils/klog.h> |
| 34 | #include <cutils/uevent.h> |
| 35 | #include <healthd/healthd.h> |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 36 | #include <utils/Errors.h> |
| 37 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 38 | #include <health/utils.h> |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 39 | |
| 40 | using namespace android; |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 41 | using namespace std::chrono_literals; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 42 | |
| 43 | #define POWER_SUPPLY_SUBSYSTEM "power_supply" |
| 44 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace hardware { |
| 47 | namespace health { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 48 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 49 | HealthLoop::HealthLoop() { |
| 50 | InitHealthdConfig(&healthd_config_); |
| 51 | awake_poll_interval_ = -1; |
| 52 | wakealarm_wake_interval_ = healthd_config_.periodic_chores_interval_fast; |
| 53 | } |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 54 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 55 | HealthLoop::~HealthLoop() { |
| 56 | LOG(FATAL) << "HealthLoop cannot be destroyed"; |
| 57 | } |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 58 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 59 | int HealthLoop::RegisterEvent(int fd, BoundFunction func, EventWakeup wakeup) { |
| 60 | CHECK(!reject_event_register_); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 61 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 62 | auto* event_handler = |
| 63 | event_handlers_ |
| 64 | .emplace_back(std::make_unique<EventHandler>(EventHandler{this, fd, func})) |
| 65 | .get(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 66 | |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 67 | struct epoll_event ev; |
| 68 | |
| 69 | ev.events = EPOLLIN; |
| 70 | |
| 71 | if (wakeup == EVENT_WAKEUP_FD) ev.events |= EPOLLWAKEUP; |
| 72 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 73 | ev.data.ptr = reinterpret_cast<void*>(event_handler); |
| 74 | |
| 75 | if (epoll_ctl(epollfd_, EPOLL_CTL_ADD, fd, &ev) == -1) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 76 | KLOG_ERROR(LOG_TAG, "epoll_ctl failed; errno=%d\n", errno); |
| 77 | return -1; |
| 78 | } |
| 79 | |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 80 | return 0; |
| 81 | } |
| 82 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 83 | void HealthLoop::WakeAlarmSetInterval(int interval) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 84 | struct itimerspec itval; |
| 85 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 86 | if (wakealarm_fd_ == -1) return; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 87 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 88 | wakealarm_wake_interval_ = interval; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 89 | |
| 90 | if (interval == -1) interval = 0; |
| 91 | |
| 92 | itval.it_interval.tv_sec = interval; |
| 93 | itval.it_interval.tv_nsec = 0; |
| 94 | itval.it_value.tv_sec = interval; |
| 95 | itval.it_value.tv_nsec = 0; |
| 96 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 97 | if (timerfd_settime(wakealarm_fd_, 0, &itval, NULL) == -1) |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 98 | KLOG_ERROR(LOG_TAG, "wakealarm_set_interval: timerfd_settime failed\n"); |
| 99 | } |
| 100 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 101 | void HealthLoop::AdjustWakealarmPeriods(bool charger_online) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 102 | // Fast wake interval when on charger (watch for overheat); |
| 103 | // slow wake interval when on battery (watch for drained battery). |
| 104 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 105 | int new_wake_interval = charger_online ? healthd_config_.periodic_chores_interval_fast |
| 106 | : healthd_config_.periodic_chores_interval_slow; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 107 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 108 | if (new_wake_interval != wakealarm_wake_interval_) WakeAlarmSetInterval(new_wake_interval); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 109 | |
| 110 | // During awake periods poll at fast rate. If wake alarm is set at fast |
| 111 | // rate then just use the alarm; if wake alarm is set at slow rate then |
| 112 | // poll at fast rate while awake and let alarm wake up at slow rate when |
| 113 | // asleep. |
| 114 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 115 | if (healthd_config_.periodic_chores_interval_fast == -1) |
| 116 | awake_poll_interval_ = -1; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 117 | else |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 118 | awake_poll_interval_ = new_wake_interval == healthd_config_.periodic_chores_interval_fast |
| 119 | ? -1 |
| 120 | : healthd_config_.periodic_chores_interval_fast * 1000; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 123 | void HealthLoop::PeriodicChores() { |
| 124 | ScheduleBatteryUpdate(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 127 | // TODO(b/140330870): Use BPF instead. |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 128 | #define UEVENT_MSG_LEN 2048 |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 129 | void HealthLoop::UeventEvent(uint32_t /*epevents*/) { |
| 130 | // No need to lock because uevent_fd_ is guaranteed to be initialized. |
| 131 | |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 132 | char msg[UEVENT_MSG_LEN + 2]; |
| 133 | char* cp; |
| 134 | int n; |
| 135 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 136 | n = uevent_kernel_multicast_recv(uevent_fd_, msg, UEVENT_MSG_LEN); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 137 | if (n <= 0) return; |
| 138 | if (n >= UEVENT_MSG_LEN) /* overflow -- discard */ |
| 139 | return; |
| 140 | |
| 141 | msg[n] = '\0'; |
| 142 | msg[n + 1] = '\0'; |
| 143 | cp = msg; |
| 144 | |
| 145 | while (*cp) { |
| 146 | if (!strcmp(cp, "SUBSYSTEM=" POWER_SUPPLY_SUBSYSTEM)) { |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 147 | ScheduleBatteryUpdate(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | |
| 151 | /* advance to after the next \0 */ |
| 152 | while (*cp++) |
| 153 | ; |
| 154 | } |
| 155 | } |
| 156 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 157 | void HealthLoop::UeventInit(void) { |
| 158 | uevent_fd_.reset(uevent_open_socket(64 * 1024, true)); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 159 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 160 | if (uevent_fd_ < 0) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 161 | KLOG_ERROR(LOG_TAG, "uevent_init: uevent_open_socket failed\n"); |
| 162 | return; |
| 163 | } |
| 164 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 165 | fcntl(uevent_fd_, F_SETFL, O_NONBLOCK); |
| 166 | if (RegisterEvent(uevent_fd_, &HealthLoop::UeventEvent, EVENT_WAKEUP_FD)) |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 167 | KLOG_ERROR(LOG_TAG, "register for uevent events failed\n"); |
| 168 | } |
| 169 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 170 | void HealthLoop::WakeAlarmEvent(uint32_t /*epevents*/) { |
| 171 | // No need to lock because wakealarm_fd_ is guaranteed to be initialized. |
| 172 | |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 173 | unsigned long long wakeups; |
| 174 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 175 | if (read(wakealarm_fd_, &wakeups, sizeof(wakeups)) == -1) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 176 | KLOG_ERROR(LOG_TAG, "wakealarm_event: read wakealarm fd failed\n"); |
| 177 | return; |
| 178 | } |
| 179 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 180 | PeriodicChores(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 183 | void HealthLoop::WakeAlarmInit(void) { |
| 184 | wakealarm_fd_.reset(timerfd_create(CLOCK_BOOTTIME_ALARM, TFD_NONBLOCK)); |
| 185 | if (wakealarm_fd_ == -1) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 186 | KLOG_ERROR(LOG_TAG, "wakealarm_init: timerfd_create failed\n"); |
| 187 | return; |
| 188 | } |
| 189 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 190 | if (RegisterEvent(wakealarm_fd_, &HealthLoop::WakeAlarmEvent, EVENT_WAKEUP_FD)) |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 191 | KLOG_ERROR(LOG_TAG, "Registration of wakealarm event failed\n"); |
| 192 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 193 | WakeAlarmSetInterval(healthd_config_.periodic_chores_interval_fast); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 194 | } |
| 195 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 196 | void HealthLoop::MainLoop(void) { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 197 | int nevents = 0; |
| 198 | while (1) { |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 199 | reject_event_register_ = true; |
| 200 | size_t eventct = event_handlers_.size(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 201 | struct epoll_event events[eventct]; |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 202 | int timeout = awake_poll_interval_; |
| 203 | |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 204 | int mode_timeout; |
| 205 | |
| 206 | /* Don't wait for first timer timeout to run periodic chores */ |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 207 | if (!nevents) PeriodicChores(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 208 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 209 | Heartbeat(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 210 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 211 | mode_timeout = PrepareToWait(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 212 | if (timeout < 0 || (mode_timeout > 0 && mode_timeout < timeout)) timeout = mode_timeout; |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 213 | nevents = epoll_wait(epollfd_, events, eventct, timeout); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 214 | if (nevents == -1) { |
| 215 | if (errno == EINTR) continue; |
| 216 | KLOG_ERROR(LOG_TAG, "healthd_mainloop: epoll_wait failed\n"); |
| 217 | break; |
| 218 | } |
| 219 | |
| 220 | for (int n = 0; n < nevents; ++n) { |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 221 | if (events[n].data.ptr) { |
| 222 | auto* event_handler = reinterpret_cast<EventHandler*>(events[n].data.ptr); |
| 223 | event_handler->func(event_handler->object, events[n].events); |
| 224 | } |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
| 228 | return; |
| 229 | } |
| 230 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 231 | int HealthLoop::InitInternal() { |
| 232 | epollfd_.reset(epoll_create1(EPOLL_CLOEXEC)); |
| 233 | if (epollfd_ == -1) { |
Nick Kralevich | 8c038f2 | 2018-12-15 11:36:47 -0800 | [diff] [blame] | 234 | KLOG_ERROR(LOG_TAG, "epoll_create1 failed; errno=%d\n", errno); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 235 | return -1; |
| 236 | } |
| 237 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 238 | // Call subclass's init for any additional init steps. |
| 239 | // Note that healthd_config_ is initialized before wakealarm_fd_; see |
| 240 | // AdjustUeventWakealarmPeriods(). |
| 241 | Init(&healthd_config_); |
| 242 | |
| 243 | WakeAlarmInit(); |
| 244 | UeventInit(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 245 | |
| 246 | return 0; |
| 247 | } |
| 248 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 249 | int HealthLoop::StartLoop() { |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 250 | int ret; |
| 251 | |
| 252 | klog_set_level(KLOG_LEVEL); |
| 253 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 254 | ret = InitInternal(); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 255 | if (ret) { |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 256 | KLOG_ERROR(LOG_TAG, "Initialization failed, exiting\n"); |
| 257 | return 2; |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 260 | MainLoop(); |
| 261 | KLOG_ERROR(LOG_TAG, "Main loop terminated, exiting\n"); |
Hridya Valsaraju | d3e3d72 | 2017-12-11 17:31:00 -0800 | [diff] [blame] | 262 | return 3; |
| 263 | } |
Yifan Hong | 50c9e25 | 2019-10-03 16:26:13 -0700 | [diff] [blame] | 264 | |
| 265 | } // namespace health |
| 266 | } // namespace hardware |
| 267 | } // namespace android |