Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 1 | /* |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 2 | * Copyright (C) 2020 The Android Open Source Project |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 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 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 17 | #include "LogBufferTest.h" |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 18 | |
| 19 | #include <unistd.h> |
| 20 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 21 | #include <limits> |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 22 | #include <memory> |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 23 | #include <regex> |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/stringprintf.h> |
| 27 | #include <android-base/strings.h> |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 28 | |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 29 | #include "LogReaderThread.h" |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 30 | #include "LogWriter.h" |
| 31 | |
| 32 | using android::base::Join; |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 33 | using android::base::Split; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 34 | using android::base::StringPrintf; |
| 35 | |
Tom Cherry | a515197 | 2020-05-15 11:39:58 -0700 | [diff] [blame] | 36 | #ifndef __ANDROID__ |
| 37 | unsigned long __android_logger_get_buffer_size(log_id_t) { |
| 38 | return 1024 * 1024; |
| 39 | } |
| 40 | |
| 41 | bool __android_logger_valid_buffer_size(unsigned long) { |
| 42 | return true; |
| 43 | } |
| 44 | #endif |
| 45 | |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 46 | void android::prdebug(const char* fmt, ...) { |
| 47 | va_list ap; |
| 48 | va_start(ap, fmt); |
| 49 | vfprintf(stderr, fmt, ap); |
| 50 | fprintf(stderr, "\n"); |
| 51 | va_end(ap); |
| 52 | } |
| 53 | |
| 54 | char* android::uidToName(uid_t) { |
| 55 | return nullptr; |
| 56 | } |
| 57 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 58 | static std::vector<std::string> CompareLoggerEntries(const logger_entry& expected, |
| 59 | const logger_entry& result, bool ignore_len) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 60 | std::vector<std::string> errors; |
| 61 | if (!ignore_len && expected.len != result.len) { |
| 62 | errors.emplace_back( |
| 63 | StringPrintf("len: expected %" PRIu16 " vs %" PRIu16, expected.len, result.len)); |
| 64 | } |
| 65 | if (expected.hdr_size != result.hdr_size) { |
| 66 | errors.emplace_back(StringPrintf("hdr_size: %" PRIu16 " vs %" PRIu16, expected.hdr_size, |
| 67 | result.hdr_size)); |
| 68 | } |
| 69 | if (expected.pid != result.pid) { |
| 70 | errors.emplace_back( |
| 71 | StringPrintf("pid: expected %" PRIi32 " vs %" PRIi32, expected.pid, result.pid)); |
| 72 | } |
| 73 | if (expected.tid != result.tid) { |
| 74 | errors.emplace_back( |
| 75 | StringPrintf("tid: expected %" PRIu32 " vs %" PRIu32, expected.tid, result.tid)); |
| 76 | } |
| 77 | if (expected.sec != result.sec) { |
| 78 | errors.emplace_back( |
| 79 | StringPrintf("sec: expected %" PRIu32 " vs %" PRIu32, expected.sec, result.sec)); |
| 80 | } |
| 81 | if (expected.nsec != result.nsec) { |
| 82 | errors.emplace_back( |
| 83 | StringPrintf("nsec: expected %" PRIu32 " vs %" PRIu32, expected.nsec, result.nsec)); |
| 84 | } |
| 85 | if (expected.lid != result.lid) { |
| 86 | errors.emplace_back( |
| 87 | StringPrintf("lid: expected %" PRIu32 " vs %" PRIu32, expected.lid, result.lid)); |
| 88 | } |
| 89 | if (expected.uid != result.uid) { |
| 90 | errors.emplace_back( |
| 91 | StringPrintf("uid: expected %" PRIu32 " vs %" PRIu32, expected.uid, result.uid)); |
| 92 | } |
| 93 | return errors; |
| 94 | } |
| 95 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 96 | static std::string MakePrintable(std::string in) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 97 | if (in.size() > 80) { |
| 98 | in = in.substr(0, 80) + "..."; |
| 99 | } |
| 100 | std::string result; |
| 101 | for (const char c : in) { |
| 102 | if (isprint(c)) { |
| 103 | result.push_back(c); |
| 104 | } else { |
| 105 | result.append(StringPrintf("\\%02x", static_cast<int>(c) & 0xFF)); |
| 106 | } |
| 107 | } |
| 108 | return result; |
| 109 | } |
| 110 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 111 | static std::string CompareMessages(const std::string& expected, const std::string& result) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 112 | if (expected == result) { |
| 113 | return {}; |
| 114 | } |
| 115 | size_t diff_index = 0; |
| 116 | for (; diff_index < std::min(expected.size(), result.size()); ++diff_index) { |
| 117 | if (expected[diff_index] != result[diff_index]) { |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | if (diff_index < 10) { |
| 123 | auto expected_short = MakePrintable(expected); |
| 124 | auto result_short = MakePrintable(result); |
| 125 | return StringPrintf("msg: expected '%s' vs '%s'", expected_short.c_str(), |
| 126 | result_short.c_str()); |
| 127 | } |
| 128 | |
| 129 | auto expected_short = MakePrintable(expected.substr(diff_index)); |
| 130 | auto result_short = MakePrintable(result.substr(diff_index)); |
| 131 | return StringPrintf("msg: index %zu: expected '%s' vs '%s'", diff_index, expected_short.c_str(), |
| 132 | result_short.c_str()); |
| 133 | } |
| 134 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 135 | static std::string CompareRegexMessages(const std::string& expected, const std::string& result) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 136 | auto expected_pieces = Split(expected, std::string("\0", 1)); |
| 137 | auto result_pieces = Split(result, std::string("\0", 1)); |
| 138 | |
| 139 | if (expected_pieces.size() != 3 || result_pieces.size() != 3) { |
| 140 | return StringPrintf( |
| 141 | "msg: should have 3 null delimited strings found %d in expected, %d in result: " |
| 142 | "'%s' vs '%s'", |
| 143 | static_cast<int>(expected_pieces.size()), static_cast<int>(result_pieces.size()), |
| 144 | MakePrintable(expected).c_str(), MakePrintable(result).c_str()); |
| 145 | } |
| 146 | if (expected_pieces[0] != result_pieces[0]) { |
| 147 | return StringPrintf("msg: tag/priority mismatch expected '%s' vs '%s'", |
| 148 | MakePrintable(expected_pieces[0]).c_str(), |
| 149 | MakePrintable(result_pieces[0]).c_str()); |
| 150 | } |
| 151 | std::regex expected_tag_regex(expected_pieces[1]); |
| 152 | if (!std::regex_search(result_pieces[1], expected_tag_regex)) { |
| 153 | return StringPrintf("msg: message regex mismatch expected '%s' vs '%s'", |
| 154 | MakePrintable(expected_pieces[1]).c_str(), |
| 155 | MakePrintable(result_pieces[1]).c_str()); |
| 156 | } |
| 157 | if (expected_pieces[2] != result_pieces[2]) { |
| 158 | return StringPrintf("msg: nothing expected after final null character '%s' vs '%s'", |
| 159 | MakePrintable(expected_pieces[2]).c_str(), |
| 160 | MakePrintable(result_pieces[2]).c_str()); |
| 161 | } |
| 162 | return {}; |
| 163 | } |
| 164 | |
| 165 | void CompareLogMessages(const std::vector<LogMessage>& expected, |
| 166 | const std::vector<LogMessage>& result) { |
| 167 | EXPECT_EQ(expected.size(), result.size()); |
| 168 | size_t end = std::min(expected.size(), result.size()); |
| 169 | size_t num_errors = 0; |
| 170 | for (size_t i = 0; i < end; ++i) { |
| 171 | auto errors = |
| 172 | CompareLoggerEntries(expected[i].entry, result[i].entry, expected[i].regex_compare); |
| 173 | auto msg_error = expected[i].regex_compare |
| 174 | ? CompareRegexMessages(expected[i].message, result[i].message) |
| 175 | : CompareMessages(expected[i].message, result[i].message); |
| 176 | if (!msg_error.empty()) { |
| 177 | errors.emplace_back(msg_error); |
| 178 | } |
| 179 | if (!errors.empty()) { |
| 180 | GTEST_LOG_(ERROR) << "Mismatch log message " << i << "\n" << Join(errors, "\n"); |
| 181 | ++num_errors; |
| 182 | } |
| 183 | } |
| 184 | EXPECT_EQ(0U, num_errors); |
| 185 | } |
| 186 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 187 | void FixupMessages(std::vector<LogMessage>* messages) { |
| 188 | for (auto& [entry, message, _] : *messages) { |
| 189 | entry.hdr_size = sizeof(logger_entry); |
| 190 | entry.len = message.size(); |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 191 | } |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 192 | } |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 193 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 194 | TEST_P(LogBufferTest, smoke) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 195 | std::vector<LogMessage> log_messages = { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 196 | {{ |
| 197 | .pid = 1, |
| 198 | .tid = 1, |
| 199 | .sec = 1234, |
| 200 | .nsec = 323001, |
| 201 | .lid = LOG_ID_MAIN, |
| 202 | .uid = 0, |
| 203 | }, |
| 204 | "smoke test"}, |
| 205 | }; |
| 206 | FixupMessages(&log_messages); |
| 207 | LogMessages(log_messages); |
| 208 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 209 | std::vector<LogMessage> read_log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 210 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr)); |
Tom Cherry | 855c7c8 | 2020-05-28 12:38:21 -0700 | [diff] [blame] | 211 | std::unique_ptr<FlushToState> flush_to_state = log_buffer_->CreateFlushToState(1, kLogMaskAll); |
| 212 | EXPECT_TRUE(log_buffer_->FlushTo(test_writer.get(), *flush_to_state, nullptr)); |
| 213 | EXPECT_EQ(2ULL, flush_to_state->start()); |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 214 | CompareLogMessages(log_messages, read_log_messages); |
| 215 | } |
| 216 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 217 | TEST_P(LogBufferTest, smoke_with_reader_thread) { |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 218 | std::vector<LogMessage> log_messages = { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 219 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0}, |
| 220 | "first"}, |
| 221 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0}, |
| 222 | "second"}, |
| 223 | {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_KERNEL, .uid = 0}, |
| 224 | "third"}, |
| 225 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20004, .lid = LOG_ID_MAIN, .uid = 0}, |
| 226 | "fourth"}, |
| 227 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20005, .lid = LOG_ID_RADIO, .uid = 0}, |
| 228 | "fifth"}, |
| 229 | {{.pid = 2, .tid = 2, .sec = 10000, .nsec = 20006, .lid = LOG_ID_RADIO, .uid = 0}, |
| 230 | "sixth"}, |
| 231 | {{.pid = 3, .tid = 2, .sec = 10000, .nsec = 20007, .lid = LOG_ID_RADIO, .uid = 0}, |
| 232 | "seventh"}, |
| 233 | {{.pid = 4, .tid = 2, .sec = 10000, .nsec = 20008, .lid = LOG_ID_MAIN, .uid = 0}, |
| 234 | "eighth"}, |
| 235 | {{.pid = 5, .tid = 2, .sec = 10000, .nsec = 20009, .lid = LOG_ID_CRASH, .uid = 0}, |
| 236 | "nineth"}, |
| 237 | {{.pid = 6, .tid = 2, .sec = 10000, .nsec = 20011, .lid = LOG_ID_MAIN, .uid = 0}, |
| 238 | "tenth"}, |
| 239 | }; |
| 240 | FixupMessages(&log_messages); |
| 241 | LogMessages(log_messages); |
| 242 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 243 | std::vector<LogMessage> read_log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 244 | bool released = false; |
| 245 | |
| 246 | { |
| 247 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 248 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 249 | std::unique_ptr<LogReaderThread> log_reader( |
| 250 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true, |
| 251 | 0, ~0, 0, {}, 1, {})); |
| 252 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 253 | } |
| 254 | |
| 255 | while (!released) { |
| 256 | usleep(5000); |
| 257 | } |
| 258 | { |
| 259 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 260 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 261 | } |
| 262 | CompareLogMessages(log_messages, read_log_messages); |
| 263 | } |
| 264 | |
| 265 | // Generate random messages, set the 'sec' parameter explicit though, to be able to track the |
| 266 | // expected order of messages. |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 267 | LogMessage GenerateRandomLogMessage(uint32_t sec) { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 268 | auto rand_uint32 = [](int max) -> uint32_t { return rand() % max; }; |
| 269 | logger_entry entry = { |
| 270 | .hdr_size = sizeof(logger_entry), |
| 271 | .pid = rand() % 5000, |
| 272 | .tid = rand_uint32(5000), |
| 273 | .sec = sec, |
| 274 | .nsec = rand_uint32(NS_PER_SEC), |
| 275 | .lid = rand_uint32(LOG_ID_STATS), |
| 276 | .uid = rand_uint32(100000), |
| 277 | }; |
| 278 | |
| 279 | // See comment in ChattyLogBuffer::Log() for why this is disallowed. |
| 280 | if (entry.nsec % 1000 == 0) { |
| 281 | ++entry.nsec; |
| 282 | } |
| 283 | |
| 284 | if (entry.lid == LOG_ID_EVENTS) { |
| 285 | entry.lid = LOG_ID_KERNEL; |
| 286 | } |
| 287 | |
| 288 | std::string message; |
| 289 | char priority = ANDROID_LOG_INFO + rand() % 2; |
| 290 | message.push_back(priority); |
| 291 | |
| 292 | int tag_length = 2 + rand() % 10; |
| 293 | for (int i = 0; i < tag_length; ++i) { |
| 294 | message.push_back('a' + rand() % 26); |
| 295 | } |
| 296 | message.push_back('\0'); |
| 297 | |
| 298 | int msg_length = 2 + rand() % 1000; |
| 299 | for (int i = 0; i < msg_length; ++i) { |
| 300 | message.push_back('a' + rand() % 26); |
| 301 | } |
| 302 | message.push_back('\0'); |
| 303 | |
| 304 | entry.len = message.size(); |
| 305 | |
| 306 | return {entry, message}; |
| 307 | } |
| 308 | |
Tom Cherry | e18346d | 2020-05-21 12:13:20 -0700 | [diff] [blame] | 309 | TEST_P(LogBufferTest, random_messages) { |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 310 | srand(1); |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 311 | std::vector<LogMessage> log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 312 | for (size_t i = 0; i < 1000; ++i) { |
| 313 | log_messages.emplace_back(GenerateRandomLogMessage(i)); |
| 314 | } |
| 315 | LogMessages(log_messages); |
| 316 | |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 317 | std::vector<LogMessage> read_log_messages; |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 318 | bool released = false; |
| 319 | |
| 320 | { |
| 321 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 322 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 323 | std::unique_ptr<LogReaderThread> log_reader( |
| 324 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true, |
| 325 | 0, ~0, 0, {}, 1, {})); |
| 326 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 327 | } |
| 328 | |
| 329 | while (!released) { |
| 330 | usleep(5000); |
| 331 | } |
| 332 | { |
| 333 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 334 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 335 | } |
| 336 | CompareLogMessages(log_messages, read_log_messages); |
| 337 | } |
Tom Cherry | b398a7c | 2020-05-20 12:09:22 -0700 | [diff] [blame] | 338 | |
Tom Cherry | 90e9ce0 | 2020-06-01 13:43:50 -0700 | [diff] [blame] | 339 | TEST_P(LogBufferTest, read_last_sequence) { |
| 340 | std::vector<LogMessage> log_messages = { |
| 341 | {{.pid = 1, .tid = 2, .sec = 10000, .nsec = 20001, .lid = LOG_ID_MAIN, .uid = 0}, |
| 342 | "first"}, |
| 343 | {{.pid = 10, .tid = 2, .sec = 10000, .nsec = 20002, .lid = LOG_ID_MAIN, .uid = 0}, |
| 344 | "second"}, |
| 345 | {{.pid = 100, .tid = 2, .sec = 10000, .nsec = 20003, .lid = LOG_ID_MAIN, .uid = 0}, |
| 346 | "third"}, |
| 347 | }; |
| 348 | FixupMessages(&log_messages); |
| 349 | LogMessages(log_messages); |
| 350 | |
| 351 | std::vector<LogMessage> read_log_messages; |
| 352 | bool released = false; |
| 353 | |
| 354 | { |
| 355 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 356 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released)); |
| 357 | std::unique_ptr<LogReaderThread> log_reader( |
| 358 | new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true, |
| 359 | 0, ~0, 0, {}, 3, {})); |
| 360 | reader_list_.reader_threads().emplace_back(std::move(log_reader)); |
| 361 | } |
| 362 | |
| 363 | while (!released) { |
| 364 | usleep(5000); |
| 365 | } |
| 366 | { |
| 367 | auto lock = std::unique_lock{reader_list_.reader_threads_lock()}; |
| 368 | EXPECT_EQ(0U, reader_list_.reader_threads().size()); |
| 369 | } |
| 370 | std::vector<LogMessage> expected_log_messages = {log_messages.back()}; |
| 371 | CompareLogMessages(expected_log_messages, read_log_messages); |
| 372 | } |
| 373 | |
Tom Cherry | 8f61346 | 2020-05-12 12:46:43 -0700 | [diff] [blame] | 374 | INSTANTIATE_TEST_CASE_P(LogBufferTests, LogBufferTest, testing::Values("chatty", "simple")); |