autosuspend_wakeup_count.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (C) 2012 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. #define LOG_TAG "libsuspend"
  17. //#define LOG_NDEBUG 0
  18. #include <fcntl.h>
  19. #include <pthread.h>
  20. #include <semaphore.h>
  21. #include <stdbool.h>
  22. #include <stddef.h>
  23. #include <string.h>
  24. #include <sys/param.h>
  25. #include <sys/stat.h>
  26. #include <sys/types.h>
  27. #include <unistd.h>
  28. #include <android-base/file.h>
  29. #include <android-base/logging.h>
  30. #include <android-base/strings.h>
  31. #include "autosuspend_ops.h"
  32. #define BASE_SLEEP_TIME 100000
  33. #define MAX_SLEEP_TIME 60000000
  34. static int state_fd = -1;
  35. static int wakeup_count_fd;
  36. using android::base::ReadFdToString;
  37. using android::base::Trim;
  38. using android::base::WriteStringToFd;
  39. static pthread_t suspend_thread;
  40. static sem_t suspend_lockout;
  41. static constexpr char sleep_state[] = "mem";
  42. static void (*wakeup_func)(bool success) = NULL;
  43. static int sleep_time = BASE_SLEEP_TIME;
  44. static constexpr char sys_power_state[] = "/sys/power/state";
  45. static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count";
  46. static bool autosuspend_is_init = false;
  47. static void update_sleep_time(bool success) {
  48. if (success) {
  49. sleep_time = BASE_SLEEP_TIME;
  50. return;
  51. }
  52. // double sleep time after each failure up to one minute
  53. sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME);
  54. }
  55. static void* suspend_thread_func(void* arg __attribute__((unused))) {
  56. bool success = true;
  57. while (true) {
  58. update_sleep_time(success);
  59. usleep(sleep_time);
  60. success = false;
  61. LOG(VERBOSE) << "read wakeup_count";
  62. lseek(wakeup_count_fd, 0, SEEK_SET);
  63. std::string wakeup_count;
  64. if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) {
  65. PLOG(ERROR) << "error reading from " << sys_power_wakeup_count;
  66. continue;
  67. }
  68. wakeup_count = Trim(wakeup_count);
  69. if (wakeup_count.empty()) {
  70. LOG(ERROR) << "empty wakeup count";
  71. continue;
  72. }
  73. LOG(VERBOSE) << "wait";
  74. int ret = sem_wait(&suspend_lockout);
  75. if (ret < 0) {
  76. PLOG(ERROR) << "error waiting on semaphore";
  77. continue;
  78. }
  79. LOG(VERBOSE) << "write " << wakeup_count << " to wakeup_count";
  80. if (WriteStringToFd(wakeup_count, wakeup_count_fd)) {
  81. LOG(VERBOSE) << "write " << sleep_state << " to " << sys_power_state;
  82. success = WriteStringToFd(sleep_state, state_fd);
  83. void (*func)(bool success) = wakeup_func;
  84. if (func != NULL) {
  85. (*func)(success);
  86. }
  87. } else {
  88. PLOG(ERROR) << "error writing to " << sys_power_wakeup_count;
  89. }
  90. LOG(VERBOSE) << "release sem";
  91. ret = sem_post(&suspend_lockout);
  92. if (ret < 0) {
  93. PLOG(ERROR) << "error releasing semaphore";
  94. }
  95. }
  96. return NULL;
  97. }
  98. static int init_state_fd(void) {
  99. if (state_fd >= 0) {
  100. return 0;
  101. }
  102. int fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_CLOEXEC | O_RDWR));
  103. if (fd < 0) {
  104. PLOG(ERROR) << "error opening " << sys_power_state;
  105. return -1;
  106. }
  107. state_fd = fd;
  108. LOG(INFO) << "init_state_fd success";
  109. return 0;
  110. }
  111. static int autosuspend_init(void) {
  112. if (autosuspend_is_init) {
  113. return 0;
  114. }
  115. int ret = init_state_fd();
  116. if (ret < 0) {
  117. return -1;
  118. }
  119. wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_CLOEXEC | O_RDWR));
  120. if (wakeup_count_fd < 0) {
  121. PLOG(ERROR) << "error opening " << sys_power_wakeup_count;
  122. goto err_open_wakeup_count;
  123. }
  124. ret = sem_init(&suspend_lockout, 0, 0);
  125. if (ret < 0) {
  126. PLOG(ERROR) << "error creating suspend_lockout semaphore";
  127. goto err_sem_init;
  128. }
  129. ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
  130. if (ret) {
  131. LOG(ERROR) << "error creating thread: " << strerror(ret);
  132. goto err_pthread_create;
  133. }
  134. LOG(VERBOSE) << "autosuspend_init success";
  135. autosuspend_is_init = true;
  136. return 0;
  137. err_pthread_create:
  138. sem_destroy(&suspend_lockout);
  139. err_sem_init:
  140. close(wakeup_count_fd);
  141. err_open_wakeup_count:
  142. return -1;
  143. }
  144. static int autosuspend_wakeup_count_enable(void) {
  145. LOG(VERBOSE) << "autosuspend_wakeup_count_enable";
  146. int ret = autosuspend_init();
  147. if (ret < 0) {
  148. LOG(ERROR) << "autosuspend_init failed";
  149. return ret;
  150. }
  151. ret = sem_post(&suspend_lockout);
  152. if (ret < 0) {
  153. PLOG(ERROR) << "error changing semaphore";
  154. }
  155. LOG(VERBOSE) << "autosuspend_wakeup_count_enable done";
  156. return ret;
  157. }
  158. static int autosuspend_wakeup_count_disable(void) {
  159. LOG(VERBOSE) << "autosuspend_wakeup_count_disable";
  160. if (!autosuspend_is_init) {
  161. return 0; // always successful if no thread is running yet
  162. }
  163. int ret = sem_wait(&suspend_lockout);
  164. if (ret < 0) {
  165. PLOG(ERROR) << "error changing semaphore";
  166. }
  167. LOG(VERBOSE) << "autosuspend_wakeup_count_disable done";
  168. return ret;
  169. }
  170. static int force_suspend(int timeout_ms) {
  171. LOG(VERBOSE) << "force_suspend called with timeout: " << timeout_ms;
  172. int ret = init_state_fd();
  173. if (ret < 0) {
  174. return ret;
  175. }
  176. return WriteStringToFd(sleep_state, state_fd) ? 0 : -1;
  177. }
  178. static void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
  179. if (wakeup_func != NULL) {
  180. LOG(ERROR) << "duplicate wakeup callback applied, keeping original";
  181. return;
  182. }
  183. wakeup_func = func;
  184. }
  185. struct autosuspend_ops autosuspend_wakeup_count_ops = {
  186. .enable = autosuspend_wakeup_count_enable,
  187. .disable = autosuspend_wakeup_count_disable,
  188. .force_suspend = force_suspend,
  189. .set_wakeup_callback = autosuspend_set_wakeup_callback,
  190. };
  191. struct autosuspend_ops* autosuspend_wakeup_count_init(void) {
  192. return &autosuspend_wakeup_count_ops;
  193. }