summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteve Paik2017-12-21 14:44:46 -0600
committerSteve Paik2018-01-08 15:35:35 -0600
commitd5dc8074755d9a4e653e0128b2ad474a6b9bb81f (patch)
tree740a5e10923077ba0f663674edaf6c58f4bf3870 /libsuspend
parent93d344d98cd02d66c3aac8067718be828ea283f9 (diff)
downloadplatform-system-core-d5dc8074755d9a4e653e0128b2ad474a6b9bb81f.tar.gz
platform-system-core-d5dc8074755d9a4e653e0128b2ad474a6b9bb81f.tar.xz
platform-system-core-d5dc8074755d9a4e653e0128b2ad474a6b9bb81f.zip
Fix logging in libsuspend
ALOGV --> LOG(VERBOSE) ALOGE --> PLOG(ERROR) Also cpp-ify file handling Bug: 70669809 Test: Logs work Change-Id: Idbaf3dccb495cdcd11f411c70784d19d1faa13a5
Diffstat (limited to 'libsuspend')
-rw-r--r--libsuspend/Android.bp1
-rw-r--r--libsuspend/autosuspend_wakeup_count.cpp103
2 files changed, 46 insertions, 58 deletions
diff --git a/libsuspend/Android.bp b/libsuspend/Android.bp
index fa06dc45f..b3e36c2bd 100644
--- a/libsuspend/Android.bp
+++ b/libsuspend/Android.bp
@@ -14,6 +14,7 @@ cc_library {
14 export_include_dirs: ["include"], 14 export_include_dirs: ["include"],
15 local_include_dirs: ["include"], 15 local_include_dirs: ["include"],
16 shared_libs: [ 16 shared_libs: [
17 "libbase",
17 "liblog", 18 "liblog",
18 "libcutils", 19 "libcutils",
19 ], 20 ],
diff --git a/libsuspend/autosuspend_wakeup_count.cpp b/libsuspend/autosuspend_wakeup_count.cpp
index 30f842738..cfca76521 100644
--- a/libsuspend/autosuspend_wakeup_count.cpp
+++ b/libsuspend/autosuspend_wakeup_count.cpp
@@ -17,7 +17,6 @@
17#define LOG_TAG "libsuspend" 17#define LOG_TAG "libsuspend"
18//#define LOG_NDEBUG 0 18//#define LOG_NDEBUG 0
19 19
20#include <errno.h>
21#include <fcntl.h> 20#include <fcntl.h>
22#include <pthread.h> 21#include <pthread.h>
23#include <semaphore.h> 22#include <semaphore.h>
@@ -29,22 +28,29 @@
29#include <sys/types.h> 28#include <sys/types.h>
30#include <unistd.h> 29#include <unistd.h>
31 30
32#include <log/log.h> 31#include <android-base/file.h>
32#include <android-base/logging.h>
33#include <android-base/strings.h>
33 34
34#include "autosuspend_ops.h" 35#include "autosuspend_ops.h"
35 36
36#define SYS_POWER_STATE "/sys/power/state"
37#define SYS_POWER_WAKEUP_COUNT "/sys/power/wakeup_count"
38
39#define BASE_SLEEP_TIME 100000 37#define BASE_SLEEP_TIME 100000
38#define MAX_SLEEP_TIME 60000000
40 39
41static int state_fd; 40static int state_fd;
42static int wakeup_count_fd; 41static int wakeup_count_fd;
42
43using android::base::ReadFdToString;
44using android::base::Trim;
45using android::base::WriteStringToFd;
46
43static pthread_t suspend_thread; 47static pthread_t suspend_thread;
44static sem_t suspend_lockout; 48static sem_t suspend_lockout;
45static const char* sleep_state = "mem"; 49static const char* sleep_state = "mem";
46static void (*wakeup_func)(bool success) = NULL; 50static void (*wakeup_func)(bool success) = NULL;
47static int sleep_time = BASE_SLEEP_TIME; 51static int sleep_time = BASE_SLEEP_TIME;
52static constexpr char sys_power_state[] = "/sys/power/state";
53static constexpr char sys_power_wakeup_count[] = "/sys/power/wakeup_count";
48 54
49static void update_sleep_time(bool success) { 55static void update_sleep_time(bool success) {
50 if (success) { 56 if (success) {
@@ -52,13 +58,10 @@ static void update_sleep_time(bool success) {
52 return; 58 return;
53 } 59 }
54 // double sleep time after each failure up to one minute 60 // double sleep time after each failure up to one minute
55 sleep_time = MIN(sleep_time * 2, 60000000); 61 sleep_time = MIN(sleep_time * 2, MAX_SLEEP_TIME);
56} 62}
57 63
58static void* suspend_thread_func(void* arg __attribute__((unused))) { 64static void* suspend_thread_func(void* arg __attribute__((unused))) {
59 char buf[80];
60 char wakeup_count[20];
61 int wakeup_count_len;
62 int ret; 65 int ret;
63 bool success = true; 66 bool success = true;
64 67
@@ -66,95 +69,84 @@ static void* suspend_thread_func(void* arg __attribute__((unused))) {
66 update_sleep_time(success); 69 update_sleep_time(success);
67 usleep(sleep_time); 70 usleep(sleep_time);
68 success = false; 71 success = false;
69 ALOGV("%s: read wakeup_count", __func__); 72 LOG(VERBOSE) << "read wakeup_count";
70 lseek(wakeup_count_fd, 0, SEEK_SET); 73 lseek(wakeup_count_fd, 0, SEEK_SET);
71 wakeup_count_len = 74 std::string wakeup_count;
72 TEMP_FAILURE_RETRY(read(wakeup_count_fd, wakeup_count, sizeof(wakeup_count))); 75 if (!ReadFdToString(wakeup_count_fd, &wakeup_count)) {
73 if (wakeup_count_len < 0) { 76 PLOG(ERROR) << "error reading from " << sys_power_wakeup_count;
74 strerror_r(errno, buf, sizeof(buf));
75 ALOGE("Error reading from %s: %s", SYS_POWER_WAKEUP_COUNT, buf);
76 wakeup_count_len = 0;
77 continue; 77 continue;
78 } 78 }
79 if (!wakeup_count_len) { 79
80 ALOGE("Empty wakeup count"); 80 wakeup_count = Trim(wakeup_count);
81 if (wakeup_count.empty()) {
82 LOG(ERROR) << "empty wakeup count";
81 continue; 83 continue;
82 } 84 }
83 85
84 ALOGV("%s: wait", __func__); 86 LOG(VERBOSE) << "wait";
85 ret = sem_wait(&suspend_lockout); 87 ret = sem_wait(&suspend_lockout);
86 if (ret < 0) { 88 if (ret < 0) {
87 strerror_r(errno, buf, sizeof(buf)); 89 PLOG(ERROR) << "error waiting on semaphore";
88 ALOGE("Error waiting on semaphore: %s", buf);
89 continue; 90 continue;
90 } 91 }
91 92
92 ALOGV("%s: write %*s to wakeup_count", __func__, wakeup_count_len, wakeup_count); 93 LOG(VERBOSE) << "write " << wakeup_count << " to wakeup_count";
93 ret = TEMP_FAILURE_RETRY(write(wakeup_count_fd, wakeup_count, wakeup_count_len)); 94 if (WriteStringToFd(wakeup_count, wakeup_count_fd)) {
94 if (ret < 0) { 95 LOG(VERBOSE) << "write " << sleep_state << " to " << sys_power_state;
95 strerror_r(errno, buf, sizeof(buf)); 96 success = WriteStringToFd(sleep_state, state_fd);
96 ALOGE("Error writing to %s: %s", SYS_POWER_WAKEUP_COUNT, buf); 97
97 } else {
98 ALOGV("%s: write %s to %s", __func__, sleep_state, SYS_POWER_STATE);
99 ret = TEMP_FAILURE_RETRY(write(state_fd, sleep_state, strlen(sleep_state)));
100 if (ret >= 0) {
101 success = true;
102 }
103 void (*func)(bool success) = wakeup_func; 98 void (*func)(bool success) = wakeup_func;
104 if (func != NULL) { 99 if (func != NULL) {
105 (*func)(success); 100 (*func)(success);
106 } 101 }
102 } else {
103 PLOG(ERROR) << "error writing to " << sys_power_wakeup_count;
107 } 104 }
108 105
109 ALOGV("%s: release sem", __func__); 106 LOG(VERBOSE) << "release sem";
110 ret = sem_post(&suspend_lockout); 107 ret = sem_post(&suspend_lockout);
111 if (ret < 0) { 108 if (ret < 0) {
112 strerror_r(errno, buf, sizeof(buf)); 109 PLOG(ERROR) << "error releasing semaphore";
113 ALOGE("Error releasing semaphore: %s", buf);
114 } 110 }
115 } 111 }
116 return NULL; 112 return NULL;
117} 113}
118 114
119static int autosuspend_wakeup_count_enable(void) { 115static int autosuspend_wakeup_count_enable(void) {
120 char buf[80];
121 int ret; 116 int ret;
122 117
123 ALOGV("autosuspend_wakeup_count_enable"); 118 LOG(VERBOSE) << "autosuspend_wakeup_count_enable";
124 119
125 ret = sem_post(&suspend_lockout); 120 ret = sem_post(&suspend_lockout);
126 121
127 if (ret < 0) { 122 if (ret < 0) {
128 strerror_r(errno, buf, sizeof(buf)); 123 PLOG(ERROR) << "error changing semaphore";
129 ALOGE("Error changing semaphore: %s", buf);
130 } 124 }
131 125
132 ALOGV("autosuspend_wakeup_count_enable done"); 126 LOG(VERBOSE) << "autosuspend_wakeup_count_enable done";
133 127
134 return ret; 128 return ret;
135} 129}
136 130
137static int autosuspend_wakeup_count_disable(void) { 131static int autosuspend_wakeup_count_disable(void) {
138 char buf[80];
139 int ret; 132 int ret;
140 133
141 ALOGV("autosuspend_wakeup_count_disable"); 134 LOG(VERBOSE) << "autosuspend_wakeup_count_disable";
142 135
143 ret = sem_wait(&suspend_lockout); 136 ret = sem_wait(&suspend_lockout);
144 137
145 if (ret < 0) { 138 if (ret < 0) {
146 strerror_r(errno, buf, sizeof(buf)); 139 PLOG(ERROR) << "error changing semaphore";
147 ALOGE("Error changing semaphore: %s", buf);
148 } 140 }
149 141
150 ALOGV("autosuspend_wakeup_count_disable done"); 142 LOG(VERBOSE) << "autosuspend_wakeup_count_disable done";
151 143
152 return ret; 144 return ret;
153} 145}
154 146
155static void autosuspend_set_wakeup_callback(void (*func)(bool success)) { 147static void autosuspend_set_wakeup_callback(void (*func)(bool success)) {
156 if (wakeup_func != NULL) { 148 if (wakeup_func != NULL) {
157 ALOGE("Duplicate wakeup callback applied, keeping original"); 149 LOG(ERROR) << "duplicate wakeup callback applied, keeping original";
158 return; 150 return;
159 } 151 }
160 wakeup_func = func; 152 wakeup_func = func;
@@ -168,36 +160,31 @@ struct autosuspend_ops autosuspend_wakeup_count_ops = {
168 160
169struct autosuspend_ops* autosuspend_wakeup_count_init(void) { 161struct autosuspend_ops* autosuspend_wakeup_count_init(void) {
170 int ret; 162 int ret;
171 char buf[80];
172 163
173 state_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_STATE, O_RDWR)); 164 state_fd = TEMP_FAILURE_RETRY(open(sys_power_state, O_RDWR));
174 if (state_fd < 0) { 165 if (state_fd < 0) {
175 strerror_r(errno, buf, sizeof(buf)); 166 PLOG(ERROR) << "error opening " << sys_power_state;
176 ALOGE("Error opening %s: %s", SYS_POWER_STATE, buf);
177 goto err_open_state; 167 goto err_open_state;
178 } 168 }
179 169
180 wakeup_count_fd = TEMP_FAILURE_RETRY(open(SYS_POWER_WAKEUP_COUNT, O_RDWR)); 170 wakeup_count_fd = TEMP_FAILURE_RETRY(open(sys_power_wakeup_count, O_RDWR));
181 if (wakeup_count_fd < 0) { 171 if (wakeup_count_fd < 0) {
182 strerror_r(errno, buf, sizeof(buf)); 172 PLOG(ERROR) << "error opening " << sys_power_wakeup_count;
183 ALOGE("Error opening %s: %s", SYS_POWER_WAKEUP_COUNT, buf);
184 goto err_open_wakeup_count; 173 goto err_open_wakeup_count;
185 } 174 }
186 175
187 ret = sem_init(&suspend_lockout, 0, 0); 176 ret = sem_init(&suspend_lockout, 0, 0);
188 if (ret < 0) { 177 if (ret < 0) {
189 strerror_r(errno, buf, sizeof(buf)); 178 PLOG(ERROR) << "error creating semaphore";
190 ALOGE("Error creating semaphore: %s", buf);
191 goto err_sem_init; 179 goto err_sem_init;
192 } 180 }
193 ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL); 181 ret = pthread_create(&suspend_thread, NULL, suspend_thread_func, NULL);
194 if (ret) { 182 if (ret) {
195 strerror_r(ret, buf, sizeof(buf)); 183 LOG(ERROR) << "error creating thread: " << strerror(ret);
196 ALOGE("Error creating thread: %s", buf);
197 goto err_pthread_create; 184 goto err_pthread_create;
198 } 185 }
199 186
200 ALOGI("Selected wakeup count"); 187 LOG(INFO) << "selected wakeup count";
201 return &autosuspend_wakeup_count_ops; 188 return &autosuspend_wakeup_count_ops;
202 189
203err_pthread_create: 190err_pthread_create: