Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 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 "LogBuffer.h" |
| 18 | |
| 19 | #include <unistd.h> |
| 20 | |
| 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <android-base/strings.h> |
| 26 | #include <gtest/gtest.h> |
| 27 | |
| 28 | #include "ChattyLogBuffer.h" |
| 29 | #include "LogReaderList.h" |
| 30 | #include "LogReaderThread.h" |
| 31 | #include "LogStatistics.h" |
| 32 | #include "LogTags.h" |
| 33 | #include "LogWhiteBlackList.h" |
| 34 | #include "LogWriter.h" |
| 35 | |
| 36 | using android::base::Join; |
| 37 | using android::base::StringPrintf; |
| 38 | |
Tom Cherry | a515197 | 2020-05-15 11:39:58 -0700 | [diff] [blame] | 39 | #ifndef __ANDROID__ |
| 40 | unsigned long __android_logger_get_buffer_size(log_id_t) { |
| 41 | return 1024 * 1024; |
| 42 | } |
| 43 | |
| 44 | bool __android_logger_valid_buffer_size(unsigned long) { |
| 45 | return true; |
| 46 | } |
| 47 | #endif |
| 48 | |
Tom Cherry | 43f3f76 | 2020-05-14 22:28:09 -0700 | [diff] [blame] | 49 | void android::prdebug(const char* fmt, ...) { |
| 50 | va_list ap; |
| 51 | va_start(ap, fmt); |
| 52 | vfprintf(stderr, fmt, ap); |
| 53 | fprintf(stderr, "\n"); |
| 54 | va_end(ap); |
| 55 | } |
| 56 | |
| 57 | char* android::uidToName(uid_t) { |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | class TestWriter : public LogWriter { |
| 62 | public: |
| 63 | TestWriter(std::vector<std::pair<logger_entry, std::string>>* msgs, bool* released) |
| 64 | : LogWriter(0, true, true), msgs_(msgs), released_(released) {} |
| 65 | bool Write(const logger_entry& entry, const char* message) override { |
| 66 | msgs_->emplace_back(entry, std::string(message, entry.len)); |
| 67 | return true; |
| 68 | } |
| 69 | |
| 70 | void Release() { |
| 71 | if (released_) *released_ = true; |
| 72 | } |
| 73 | |
| 74 | std::string name() const override { return "test_writer"; } |
| 75 | |
| 76 | private: |
| 77 | std::vector<std::pair<logger_entry, std::string>>* msgs_; |
| 78 | bool* released_; |
| 79 | }; |
| 80 | |
| 81 | class LogBufferTest : public testing::Test { |
| 82 | protected: |
| 83 | void SetUp() override { |
| 84 | log_buffer_.reset(new ChattyLogBuffer(&reader_list_, &tags_, &prune_, &stats_)); |
| 85 | } |
| 86 | |
| 87 | void FixupMessages(std::vector<std::pair<logger_entry, std::string>>* messages) { |
| 88 | for (auto& [entry, message] : *messages) { |
| 89 | entry.hdr_size = sizeof(logger_entry); |
| 90 | entry.len = message.size(); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void LogMessages(const std::vector<std::pair<logger_entry, std::string>>& messages) { |
| 95 | for (auto& [entry, message] : messages) { |
| 96 | log_buffer_->Log(static_cast<log_id_t>(entry.lid), log_time(entry.sec, entry.nsec), |
| 97 | entry.uid, entry.pid, entry.tid, message.c_str(), message.size()); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | std::vector<std::string> CompareLoggerEntries(const logger_entry& expected, |
| 102 | const logger_entry& result) { |
| 103 | std::vector<std::string> errors; |
| 104 | if (expected.len != result.len) { |
| 105 | errors.emplace_back( |
| 106 | StringPrintf("len: %" PRIu16 " vs %" PRIu16, expected.len, result.len)); |
| 107 | } |
| 108 | if (expected.hdr_size != result.hdr_size) { |
| 109 | errors.emplace_back(StringPrintf("hdr_size: %" PRIu16 " vs %" PRIu16, expected.hdr_size, |
| 110 | result.hdr_size)); |
| 111 | } |
| 112 | if (expected.pid != result.pid) { |
| 113 | errors.emplace_back( |
| 114 | StringPrintf("pid: %" PRIi32 " vs %" PRIi32, expected.pid, result.pid)); |
| 115 | } |
| 116 | if (expected.tid != result.tid) { |
| 117 | errors.emplace_back( |
| 118 | StringPrintf("tid: %" PRIu32 " vs %" PRIu32, expected.tid, result.tid)); |
| 119 | } |
| 120 | if (expected.sec != result.sec) { |
| 121 | errors.emplace_back( |
| 122 | StringPrintf("sec: %" PRIu32 " vs %" PRIu32, expected.sec, result.sec)); |
| 123 | } |
| 124 | if (expected.nsec != result.nsec) { |
| 125 | errors.emplace_back( |
| 126 | StringPrintf("nsec: %" PRIu32 " vs %" PRIu32, expected.nsec, result.nsec)); |
| 127 | } |
| 128 | if (expected.lid != result.lid) { |
| 129 | errors.emplace_back( |
| 130 | StringPrintf("lid: %" PRIu32 " vs %" PRIu32, expected.lid, result.lid)); |
| 131 | } |
| 132 | if (expected.uid != result.uid) { |
| 133 | errors.emplace_back( |
| 134 | StringPrintf("uid: %" PRIu32 " vs %" PRIu32, expected.uid, result.uid)); |
| 135 | } |
| 136 | return errors; |
| 137 | } |
| 138 | |
| 139 | std::string CompareMessages(const std::string& expected, const std::string& result) { |
| 140 | if (expected == result) { |
| 141 | return {}; |
| 142 | } |
| 143 | auto shorten_string = [](const std::string& in) { |
| 144 | if (in.size() > 10) { |
| 145 | return in.substr(0, 10) + "..."; |
| 146 | } |
| 147 | return in; |
| 148 | }; |
| 149 | |
| 150 | size_t diff_index = 0; |
| 151 | for (; diff_index < std::min(expected.size(), result.size()); ++diff_index) { |
| 152 | if (expected[diff_index] != result[diff_index]) { |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if (diff_index < 10) { |
| 158 | auto expected_short = shorten_string(expected); |
| 159 | auto result_short = shorten_string(result); |
| 160 | return StringPrintf("msg: %s vs %s", expected_short.c_str(), result_short.c_str()); |
| 161 | } |
| 162 | |
| 163 | auto expected_short = shorten_string(expected.substr(diff_index)); |
| 164 | auto result_short = shorten_string(result.substr(diff_index)); |
| 165 | return StringPrintf("msg: index %zu: %s vs %s", diff_index, expected_short.c_str(), |
| 166 | result_short.c_str()); |
| 167 | } |
| 168 | |
| 169 | void CompareLogMessages(const std::vector<std::pair<logger_entry, std::string>>& expected, |
| 170 | const std::vector<std::pair<logger_entry, std::string>>& result) { |
| 171 | EXPECT_EQ(expected.size(), result.size()); |
| 172 | size_t end = std::min(expected.size(), result.size()); |
| 173 | size_t num_errors = 0; |
| 174 | for (size_t i = 0; i < end; ++i) { |
| 175 | auto errors = CompareLoggerEntries(expected[i].first, result[i].first); |
| 176 | auto msg_error = CompareMessages(expected[i].second, result[i].second); |
| 177 | if (!msg_error.empty()) { |
| 178 | errors.emplace_back(msg_error); |
| 179 | } |
| 180 | if (!errors.empty()) { |
| 181 | GTEST_LOG_(ERROR) << "Mismatch log message " << i << " " << Join(errors, " "); |
| 182 | ++num_errors; |
| 183 | } |
| 184 | } |
| 185 | EXPECT_EQ(0U, num_errors); |
| 186 | } |
| 187 | |
| 188 | LogReaderList reader_list_; |
| 189 | LogTags tags_; |
| 190 | PruneList prune_; |
| 191 | LogStatistics stats_{false}; |
| 192 | std::unique_ptr<LogBuffer> log_buffer_; |
| 193 | }; |
| 194 | |
| 195 | TEST_F(LogBufferTest, smoke) { |
| 196 | std::vector<std::pair<logger_entry, std::string>> log_messages = { |
| 197 | {{ |
| 198 | .pid = 1, |
| 199 | .tid = 1, |
| 200 | .sec = 1234, |
| 201 | .nsec = 323001, |
| 202 | .lid = LOG_ID_MAIN, |
| 203 | .uid = 0, |
| 204 | }, |
| 205 | "smoke test"}, |
| 206 | }; |
| 207 | FixupMessages(&log_messages); |
| 208 | LogMessages(log_messages); |
| 209 | |
| 210 | std::vector<std::pair<logger_entry, std::string>> read_log_messages; |
| 211 | std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr)); |
| 212 | uint64_t flush_result = log_buffer_->FlushTo(test_writer.get(), 1, nullptr, nullptr); |
| 213 | EXPECT_EQ(1ULL, flush_result); |
| 214 | CompareLogMessages(log_messages, read_log_messages); |
| 215 | } |
| 216 | |
| 217 | TEST_F(LogBufferTest, smoke_with_reader_thread) { |
| 218 | std::vector<std::pair<logger_entry, std::string>> log_messages = { |
| 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 | |
| 243 | std::vector<std::pair<logger_entry, std::string>> read_log_messages; |
| 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. |
| 267 | std::pair<logger_entry, std::string> GenerateRandomLogMessage(uint32_t sec) { |
| 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 | |
| 309 | TEST_F(LogBufferTest, random_messages) { |
| 310 | srand(1); |
| 311 | std::vector<std::pair<logger_entry, std::string>> log_messages; |
| 312 | for (size_t i = 0; i < 1000; ++i) { |
| 313 | log_messages.emplace_back(GenerateRandomLogMessage(i)); |
| 314 | } |
| 315 | LogMessages(log_messages); |
| 316 | |
| 317 | std::vector<std::pair<logger_entry, std::string>> read_log_messages; |
| 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 | } |