Mark Salyzyn | 819c58a | 2013-11-22 12:39:43 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013-2014 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 <fcntl.h> |
Mark Salyzyn | cef098e | 2014-01-09 09:02:55 -0800 | [diff] [blame] | 18 | #include <signal.h> |
Mark Salyzyn | 819c58a | 2013-11-22 12:39:43 -0800 | [diff] [blame] | 19 | #include <gtest/gtest.h> |
| 20 | #include <log/log.h> |
| 21 | #include <log/logger.h> |
| 22 | #include <log/log_read.h> |
| 23 | |
Mark Salyzyn | cef098e | 2014-01-09 09:02:55 -0800 | [diff] [blame] | 24 | // enhanced version of LOG_FAILURE_RETRY to add support for EAGAIN and |
| 25 | // non-syscall libs. Since we are only using this in the emergency of |
| 26 | // a signal to stuff a terminating code into the logs, we will spin rather |
| 27 | // than try a usleep. |
| 28 | #define LOG_FAILURE_RETRY(exp) ({ \ |
| 29 | typeof (exp) _rc; \ |
| 30 | do { \ |
| 31 | _rc = (exp); \ |
| 32 | } while (((_rc == -1) \ |
| 33 | && ((errno == EINTR) \ |
| 34 | || (errno == EAGAIN))) \ |
| 35 | || (_rc == -EINTR) \ |
| 36 | || (_rc == -EAGAIN)); \ |
| 37 | _rc; }) |
| 38 | |
Mark Salyzyn | 819c58a | 2013-11-22 12:39:43 -0800 | [diff] [blame] | 39 | TEST(liblog, __android_log_buf_print) { |
| 40 | ASSERT_LT(0, __android_log_buf_print(LOG_ID_RADIO, ANDROID_LOG_INFO, |
| 41 | "TEST__android_log_buf_print", |
| 42 | "radio")); |
| 43 | usleep(1000); |
| 44 | ASSERT_LT(0, __android_log_buf_print(LOG_ID_SYSTEM, ANDROID_LOG_INFO, |
| 45 | "TEST__android_log_buf_print", |
| 46 | "system")); |
| 47 | usleep(1000); |
| 48 | ASSERT_LT(0, __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, |
| 49 | "TEST__android_log_buf_print", |
| 50 | "main")); |
| 51 | usleep(1000); |
| 52 | } |
| 53 | |
| 54 | TEST(liblog, __android_log_buf_write) { |
| 55 | ASSERT_LT(0, __android_log_buf_write(LOG_ID_RADIO, ANDROID_LOG_INFO, |
| 56 | "TEST__android_log_buf_write", |
| 57 | "radio")); |
| 58 | usleep(1000); |
| 59 | ASSERT_LT(0, __android_log_buf_write(LOG_ID_SYSTEM, ANDROID_LOG_INFO, |
| 60 | "TEST__android_log_buf_write", |
| 61 | "system")); |
| 62 | usleep(1000); |
| 63 | ASSERT_LT(0, __android_log_buf_write(LOG_ID_MAIN, ANDROID_LOG_INFO, |
| 64 | "TEST__android_log_buf_write", |
| 65 | "main")); |
| 66 | usleep(1000); |
| 67 | } |
| 68 | |
| 69 | TEST(liblog, __android_log_btwrite) { |
| 70 | int intBuf = 0xDEADBEEF; |
| 71 | ASSERT_LT(0, __android_log_btwrite(0, |
| 72 | EVENT_TYPE_INT, |
| 73 | &intBuf, sizeof(intBuf))); |
| 74 | long long longBuf = 0xDEADBEEFA55A5AA5; |
| 75 | ASSERT_LT(0, __android_log_btwrite(0, |
| 76 | EVENT_TYPE_LONG, |
| 77 | &longBuf, sizeof(longBuf))); |
| 78 | usleep(1000); |
| 79 | char Buf[] = "\20\0\0\0DeAdBeEfA55a5aA5"; |
| 80 | ASSERT_LT(0, __android_log_btwrite(0, |
| 81 | EVENT_TYPE_STRING, |
| 82 | Buf, sizeof(Buf) - 1)); |
| 83 | usleep(1000); |
| 84 | } |
| 85 | |
| 86 | static void* ConcurrentPrintFn(void *arg) { |
| 87 | int ret = __android_log_buf_print(LOG_ID_MAIN, ANDROID_LOG_INFO, |
| 88 | "TEST__android_log_print", "Concurrent %d", |
| 89 | reinterpret_cast<int>(arg)); |
| 90 | return reinterpret_cast<void*>(ret); |
| 91 | } |
| 92 | |
| 93 | #define NUM_CONCURRENT 64 |
| 94 | #define _concurrent_name(a,n) a##__concurrent##n |
| 95 | #define concurrent_name(a,n) _concurrent_name(a,n) |
| 96 | |
| 97 | TEST(liblog, concurrent_name(__android_log_buf_print, NUM_CONCURRENT)) { |
| 98 | pthread_t t[NUM_CONCURRENT]; |
| 99 | int i; |
| 100 | for (i=0; i < NUM_CONCURRENT; i++) { |
| 101 | ASSERT_EQ(0, pthread_create(&t[i], NULL, |
| 102 | ConcurrentPrintFn, |
| 103 | reinterpret_cast<void *>(i))); |
| 104 | } |
| 105 | int ret = 0; |
| 106 | for (i=0; i < NUM_CONCURRENT; i++) { |
| 107 | void* result; |
| 108 | ASSERT_EQ(0, pthread_join(t[i], &result)); |
| 109 | if ((0 == ret) && (0 != reinterpret_cast<int>(result))) { |
| 110 | ret = reinterpret_cast<int>(result); |
| 111 | } |
| 112 | } |
| 113 | ASSERT_LT(0, ret); |
| 114 | } |
| 115 | |
| 116 | TEST(liblog, __android_log_btwrite__android_logger_list_read) { |
| 117 | struct logger_list *logger_list; |
| 118 | |
| 119 | pid_t pid = getpid(); |
| 120 | |
| 121 | ASSERT_EQ(0, NULL == (logger_list = android_logger_list_open( |
| 122 | LOG_ID_EVENTS, O_RDONLY | O_NDELAY, 1000, pid))); |
| 123 | |
| 124 | log_time ts(CLOCK_MONOTONIC); |
| 125 | |
| 126 | ASSERT_LT(0, __android_log_btwrite(0, EVENT_TYPE_LONG, &ts, sizeof(ts))); |
| 127 | usleep(1000000); |
| 128 | |
| 129 | int count = 0; |
| 130 | |
| 131 | for (;;) { |
| 132 | log_msg log_msg; |
| 133 | if (android_logger_list_read(logger_list, &log_msg) <= 0) { |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | ASSERT_EQ(log_msg.entry.pid, pid); |
| 138 | |
| 139 | if ((log_msg.entry.len != (4 + 1 + 8)) |
| 140 | || (log_msg.id() != LOG_ID_EVENTS)) { |
| 141 | continue; |
| 142 | } |
| 143 | |
| 144 | char *eventData = log_msg.msg(); |
| 145 | |
| 146 | if (eventData[4] != EVENT_TYPE_LONG) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | log_time tx(eventData + 4 + 1); |
| 151 | if (ts == tx) { |
| 152 | ++count; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | ASSERT_EQ(1, count); |
| 157 | |
| 158 | android_logger_list_close(logger_list); |
| 159 | } |
| 160 | |
Mark Salyzyn | cef098e | 2014-01-09 09:02:55 -0800 | [diff] [blame] | 161 | static unsigned signaled; |
| 162 | log_time signal_time; |
| 163 | |
| 164 | static void caught_blocking(int signum) |
| 165 | { |
| 166 | unsigned long long v = 0xDEADBEEFA55A0000ULL; |
| 167 | |
| 168 | v += getpid() & 0xFFFF; |
| 169 | |
| 170 | ++signaled; |
| 171 | if ((signal_time.tv_sec == 0) && (signal_time.tv_nsec == 0)) { |
| 172 | clock_gettime(CLOCK_MONOTONIC, &signal_time); |
| 173 | signal_time.tv_sec += 2; |
| 174 | } |
| 175 | |
| 176 | LOG_FAILURE_RETRY(__android_log_btwrite(0, EVENT_TYPE_LONG, &v, sizeof(v))); |
| 177 | } |
| 178 | |
| 179 | // Fill in current process user and system time in 10ms increments |
| 180 | static void get_ticks(unsigned long long *uticks, unsigned long long *sticks) |
| 181 | { |
| 182 | *uticks = *sticks = 0; |
| 183 | |
| 184 | pid_t pid = getpid(); |
| 185 | |
| 186 | char buffer[512]; |
| 187 | snprintf(buffer, sizeof(buffer), "/proc/%u/stat", pid); |
| 188 | |
| 189 | FILE *fp = fopen(buffer, "r"); |
| 190 | if (!fp) { |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | char *cp = fgets(buffer, sizeof(buffer), fp); |
| 195 | fclose(fp); |
| 196 | if (!cp) { |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | pid_t d; |
| 201 | char s[sizeof(buffer)]; |
| 202 | char c; |
| 203 | long long ll; |
| 204 | unsigned long long ull; |
| 205 | |
| 206 | if (15 != sscanf(buffer, |
| 207 | "%d %s %c %lld %lld %lld %lld %lld %llu %llu %llu %llu %llu %llu %llu ", |
| 208 | &d, s, &c, &ll, &ll, &ll, &ll, &ll, &ull, &ull, &ull, &ull, &ull, |
| 209 | uticks, sticks)) { |
| 210 | *uticks = *sticks = 0; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | TEST(liblog, android_logger_list_read__cpu) { |
| 215 | struct logger_list *logger_list; |
| 216 | unsigned long long v = 0xDEADBEEFA55A0000ULL; |
| 217 | |
| 218 | pid_t pid = getpid(); |
| 219 | |
| 220 | v += pid & 0xFFFF; |
| 221 | |
| 222 | ASSERT_EQ(0, NULL == (logger_list = android_logger_list_open( |
| 223 | LOG_ID_EVENTS, O_RDONLY, 1000, pid))); |
| 224 | |
| 225 | int count = 0; |
| 226 | |
| 227 | int signals = 0; |
| 228 | |
| 229 | unsigned long long uticks_start; |
| 230 | unsigned long long sticks_start; |
| 231 | get_ticks(&uticks_start, &sticks_start); |
| 232 | |
| 233 | const unsigned alarm_time = 10; |
| 234 | |
| 235 | memset(&signal_time, 0, sizeof(signal_time)); |
| 236 | |
| 237 | signal(SIGALRM, caught_blocking); |
| 238 | alarm(alarm_time); |
| 239 | |
| 240 | signaled = 0; |
| 241 | |
| 242 | do { |
| 243 | log_msg log_msg; |
| 244 | if (android_logger_list_read(logger_list, &log_msg) <= 0) { |
| 245 | break; |
| 246 | } |
| 247 | |
| 248 | alarm(alarm_time); |
| 249 | |
| 250 | ++count; |
| 251 | |
| 252 | ASSERT_EQ(log_msg.entry.pid, pid); |
| 253 | |
| 254 | if ((log_msg.entry.len != (4 + 1 + 8)) |
| 255 | || (log_msg.id() != LOG_ID_EVENTS)) { |
| 256 | continue; |
| 257 | } |
| 258 | |
| 259 | char *eventData = log_msg.msg(); |
| 260 | |
| 261 | if (eventData[4] != EVENT_TYPE_LONG) { |
| 262 | continue; |
| 263 | } |
| 264 | |
| 265 | unsigned long long l = eventData[4 + 1 + 0] & 0xFF; |
| 266 | l |= (unsigned long long) (eventData[4 + 1 + 1] & 0xFF) << 8; |
| 267 | l |= (unsigned long long) (eventData[4 + 1 + 2] & 0xFF) << 16; |
| 268 | l |= (unsigned long long) (eventData[4 + 1 + 3] & 0xFF) << 24; |
| 269 | l |= (unsigned long long) (eventData[4 + 1 + 4] & 0xFF) << 32; |
| 270 | l |= (unsigned long long) (eventData[4 + 1 + 5] & 0xFF) << 40; |
| 271 | l |= (unsigned long long) (eventData[4 + 1 + 6] & 0xFF) << 48; |
| 272 | l |= (unsigned long long) (eventData[4 + 1 + 7] & 0xFF) << 56; |
| 273 | |
| 274 | if (l == v) { |
| 275 | ++signals; |
| 276 | break; |
| 277 | } |
| 278 | } while (!signaled || ({log_time t(CLOCK_MONOTONIC); t < signal_time;})); |
| 279 | alarm(0); |
| 280 | signal(SIGALRM, SIG_DFL); |
| 281 | |
| 282 | ASSERT_LT(1, count); |
| 283 | |
| 284 | ASSERT_EQ(1, signals); |
| 285 | |
| 286 | android_logger_list_close(logger_list); |
| 287 | |
| 288 | unsigned long long uticks_end; |
| 289 | unsigned long long sticks_end; |
| 290 | get_ticks(&uticks_end, &sticks_end); |
| 291 | |
| 292 | // Less than 1% in either user or system time, or both |
| 293 | const unsigned long long one_percent_ticks = alarm_time; |
| 294 | unsigned long long user_ticks = uticks_end - uticks_start; |
| 295 | unsigned long long system_ticks = sticks_end - sticks_start; |
| 296 | ASSERT_GT(one_percent_ticks, user_ticks); |
| 297 | ASSERT_GT(one_percent_ticks, system_ticks); |
| 298 | ASSERT_GT(one_percent_ticks, user_ticks + system_ticks); |
| 299 | } |
| 300 | |
Mark Salyzyn | 819c58a | 2013-11-22 12:39:43 -0800 | [diff] [blame] | 301 | TEST(liblog, android_logger_get_) { |
| 302 | struct logger_list * logger_list = android_logger_list_alloc(O_WRONLY, 0, 0); |
| 303 | |
| 304 | for(int i = LOG_ID_MIN; i < LOG_ID_MAX; ++i) { |
| 305 | log_id_t id = static_cast<log_id_t>(i); |
| 306 | const char *name = android_log_id_to_name(id); |
| 307 | if (id != android_name_to_log_id(name)) { |
| 308 | continue; |
| 309 | } |
| 310 | struct logger * logger; |
| 311 | ASSERT_EQ(0, NULL == (logger = android_logger_open(logger_list, id))); |
| 312 | ASSERT_EQ(id, android_logger_get_id(logger)); |
| 313 | ASSERT_LT(0, android_logger_get_log_size(logger)); |
| 314 | ASSERT_LT(0, android_logger_get_log_readable_size(logger)); |
| 315 | ASSERT_LT(0, android_logger_get_log_version(logger)); |
| 316 | } |
| 317 | |
| 318 | android_logger_list_close(logger_list); |
| 319 | } |