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