blob: e273efe1b821f2b163a022e29e64a82fbc260491 [file] [log] [blame]
Tom Cherry6bc6f492020-05-21 12:13:20 -07001/*
2 * Copyright (C) 2020 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 "LogBufferTest.h"
18
19class ChattyLogBufferTest : public LogBufferTest {};
20
21TEST_P(ChattyLogBufferTest, deduplication_simple) {
22 auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
23 bool regex = false) -> LogMessage {
24 logger_entry entry = {
25 .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
26 std::string message;
27 message.push_back(ANDROID_LOG_INFO);
28 message.append(tag);
29 message.push_back('\0');
30 message.append(msg);
31 message.push_back('\0');
32 return {entry, message, regex};
33 };
34
35 // clang-format off
36 std::vector<LogMessage> log_messages = {
37 make_message(0, "test_tag", "duplicate"),
38 make_message(1, "test_tag", "duplicate"),
39 make_message(2, "test_tag", "not_same"),
40 make_message(3, "test_tag", "duplicate"),
41 make_message(4, "test_tag", "duplicate"),
42 make_message(5, "test_tag", "not_same"),
43 make_message(6, "test_tag", "duplicate"),
44 make_message(7, "test_tag", "duplicate"),
45 make_message(8, "test_tag", "duplicate"),
46 make_message(9, "test_tag", "not_same"),
47 make_message(10, "test_tag", "duplicate"),
48 make_message(11, "test_tag", "duplicate"),
49 make_message(12, "test_tag", "duplicate"),
50 make_message(13, "test_tag", "duplicate"),
51 make_message(14, "test_tag", "duplicate"),
52 make_message(15, "test_tag", "duplicate"),
53 make_message(16, "test_tag", "not_same"),
54 make_message(100, "test_tag", "duplicate"),
55 make_message(200, "test_tag", "duplicate"),
56 make_message(300, "test_tag", "duplicate"),
57 };
58 // clang-format on
59 FixupMessages(&log_messages);
60 LogMessages(log_messages);
61
62 std::vector<LogMessage> read_log_messages;
Tom Cherryc5c9eba2020-10-06 10:22:35 -070063 {
64 auto lock = std::lock_guard{logd_lock};
65 std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
66
67 std::unique_ptr<FlushToState> flush_to_state =
68 log_buffer_->CreateFlushToState(1, kLogMaskAll);
69 EXPECT_TRUE(log_buffer_->FlushTo(test_writer.get(), *flush_to_state, nullptr));
70 }
Tom Cherry6bc6f492020-05-21 12:13:20 -070071
72 std::vector<LogMessage> expected_log_messages = {
73 make_message(0, "test_tag", "duplicate"),
74 make_message(1, "test_tag", "duplicate"),
75 make_message(2, "test_tag", "not_same"),
76 make_message(3, "test_tag", "duplicate"),
77 make_message(4, "test_tag", "duplicate"),
78 make_message(5, "test_tag", "not_same"),
79 // 3 duplicate logs together print the first, a 1 count chatty message, then the last.
80 make_message(6, "test_tag", "duplicate"),
Tom Cherryd444ab42020-05-28 12:38:21 -070081 make_message(7, "chatty", "uid=0\\([^\\)]+\\) [^ ]+ identical 1 line", true),
Tom Cherry6bc6f492020-05-21 12:13:20 -070082 make_message(8, "test_tag", "duplicate"),
83 make_message(9, "test_tag", "not_same"),
84 // 6 duplicate logs together print the first, a 4 count chatty message, then the last.
85 make_message(10, "test_tag", "duplicate"),
Tom Cherryd444ab42020-05-28 12:38:21 -070086 make_message(14, "chatty", "uid=0\\([^\\)]+\\) [^ ]+ identical 4 lines", true),
Tom Cherry6bc6f492020-05-21 12:13:20 -070087 make_message(15, "test_tag", "duplicate"),
88 make_message(16, "test_tag", "not_same"),
89 // duplicate logs > 1 minute apart are not deduplicated.
90 make_message(100, "test_tag", "duplicate"),
91 make_message(200, "test_tag", "duplicate"),
92 make_message(300, "test_tag", "duplicate"),
93 };
94 FixupMessages(&expected_log_messages);
95 CompareLogMessages(expected_log_messages, read_log_messages);
96};
97
98TEST_P(ChattyLogBufferTest, deduplication_overflow) {
99 auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
100 bool regex = false) -> LogMessage {
101 logger_entry entry = {
102 .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
103 std::string message;
104 message.push_back(ANDROID_LOG_INFO);
105 message.append(tag);
106 message.push_back('\0');
107 message.append(msg);
108 message.push_back('\0');
109 return {entry, message, regex};
110 };
111
112 uint32_t sec = 0;
113 std::vector<LogMessage> log_messages = {
114 make_message(sec++, "test_tag", "normal"),
115 };
116 size_t expired_per_chatty_message = std::numeric_limits<uint16_t>::max();
117 for (size_t i = 0; i < expired_per_chatty_message + 3; ++i) {
118 log_messages.emplace_back(make_message(sec++, "test_tag", "duplicate"));
119 }
120 log_messages.emplace_back(make_message(sec++, "test_tag", "normal"));
121 FixupMessages(&log_messages);
122 LogMessages(log_messages);
123
124 std::vector<LogMessage> read_log_messages;
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700125 {
126 auto lock = std::lock_guard{logd_lock};
127 std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
128 std::unique_ptr<FlushToState> flush_to_state =
129 log_buffer_->CreateFlushToState(1, kLogMaskAll);
130 EXPECT_TRUE(log_buffer_->FlushTo(test_writer.get(), *flush_to_state, nullptr));
131 }
Tom Cherry6bc6f492020-05-21 12:13:20 -0700132
133 std::vector<LogMessage> expected_log_messages = {
134 make_message(0, "test_tag", "normal"),
135 make_message(1, "test_tag", "duplicate"),
136 make_message(expired_per_chatty_message + 1, "chatty",
Tom Cherryd444ab42020-05-28 12:38:21 -0700137 "uid=0\\([^\\)]+\\) [^ ]+ identical 65535 lines", true),
Tom Cherry6bc6f492020-05-21 12:13:20 -0700138 make_message(expired_per_chatty_message + 2, "chatty",
Tom Cherryd444ab42020-05-28 12:38:21 -0700139 "uid=0\\([^\\)]+\\) [^ ]+ identical 1 line", true),
Tom Cherry6bc6f492020-05-21 12:13:20 -0700140 make_message(expired_per_chatty_message + 3, "test_tag", "duplicate"),
141 make_message(expired_per_chatty_message + 4, "test_tag", "normal"),
142 };
143 FixupMessages(&expected_log_messages);
144 CompareLogMessages(expected_log_messages, read_log_messages);
145}
146
147TEST_P(ChattyLogBufferTest, deduplication_liblog) {
148 auto make_message = [&](uint32_t sec, int32_t tag, int32_t count) -> LogMessage {
149 logger_entry entry = {
150 .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_EVENTS, .uid = 0};
151 android_log_event_int_t liblog_event = {
152 .header.tag = tag, .payload.type = EVENT_TYPE_INT, .payload.data = count};
153 return {entry, std::string(reinterpret_cast<char*>(&liblog_event), sizeof(liblog_event)),
154 false};
155 };
156
157 // LIBLOG_LOG_TAG
158 std::vector<LogMessage> log_messages = {
159 make_message(0, 1234, 1),
160 make_message(1, LIBLOG_LOG_TAG, 3),
161 make_message(2, 1234, 2),
162 make_message(3, LIBLOG_LOG_TAG, 3),
163 make_message(4, LIBLOG_LOG_TAG, 4),
164 make_message(5, 1234, 223),
165 make_message(6, LIBLOG_LOG_TAG, 2),
166 make_message(7, LIBLOG_LOG_TAG, 3),
167 make_message(8, LIBLOG_LOG_TAG, 4),
168 make_message(9, 1234, 227),
169 make_message(10, LIBLOG_LOG_TAG, 1),
170 make_message(11, LIBLOG_LOG_TAG, 3),
171 make_message(12, LIBLOG_LOG_TAG, 2),
172 make_message(13, LIBLOG_LOG_TAG, 3),
173 make_message(14, LIBLOG_LOG_TAG, 5),
174 make_message(15, 1234, 227),
175 make_message(16, LIBLOG_LOG_TAG, 2),
176 make_message(17, LIBLOG_LOG_TAG, std::numeric_limits<int32_t>::max()),
177 make_message(18, LIBLOG_LOG_TAG, 3),
178 make_message(19, LIBLOG_LOG_TAG, 5),
179 make_message(20, 1234, 227),
180 };
181 FixupMessages(&log_messages);
182 LogMessages(log_messages);
183
184 std::vector<LogMessage> read_log_messages;
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700185 {
186 auto lock = std::lock_guard{logd_lock};
187 std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, nullptr));
188 std::unique_ptr<FlushToState> flush_to_state =
189 log_buffer_->CreateFlushToState(1, kLogMaskAll);
190 EXPECT_TRUE(log_buffer_->FlushTo(test_writer.get(), *flush_to_state, nullptr));
191 }
Tom Cherry6bc6f492020-05-21 12:13:20 -0700192
193 std::vector<LogMessage> expected_log_messages = {
194 make_message(0, 1234, 1),
195 make_message(1, LIBLOG_LOG_TAG, 3),
196 make_message(2, 1234, 2),
197 make_message(3, LIBLOG_LOG_TAG, 3),
198 make_message(4, LIBLOG_LOG_TAG, 4),
199 make_message(5, 1234, 223),
200 // More than 2 liblog events (3 here), sum their value into the third message.
201 make_message(6, LIBLOG_LOG_TAG, 2),
202 make_message(8, LIBLOG_LOG_TAG, 7),
203 make_message(9, 1234, 227),
204 // More than 2 liblog events (5 here), sum their value into the third message.
205 make_message(10, LIBLOG_LOG_TAG, 1),
206 make_message(14, LIBLOG_LOG_TAG, 13),
207 make_message(15, 1234, 227),
208 // int32_t max is the max for a chatty message, beyond that we must use new messages.
209 make_message(16, LIBLOG_LOG_TAG, 2),
210 make_message(17, LIBLOG_LOG_TAG, std::numeric_limits<int32_t>::max()),
211 make_message(19, LIBLOG_LOG_TAG, 8),
212 make_message(20, 1234, 227),
213 };
214 FixupMessages(&expected_log_messages);
215 CompareLogMessages(expected_log_messages, read_log_messages);
216};
217
Tom Cherryca4b25d2020-05-28 20:02:42 -0700218TEST_P(ChattyLogBufferTest, no_leading_chatty_simple) {
219 auto make_message = [&](uint32_t sec, int32_t pid, uint32_t uid, uint32_t lid, const char* tag,
220 const char* msg, bool regex = false) -> LogMessage {
221 logger_entry entry = {.pid = pid, .tid = 1, .sec = sec, .nsec = 1, .lid = lid, .uid = uid};
222 std::string message;
223 message.push_back(ANDROID_LOG_INFO);
224 message.append(tag);
225 message.push_back('\0');
226 message.append(msg);
227 message.push_back('\0');
228 return {entry, message, regex};
229 };
230
231 // clang-format off
232 std::vector<LogMessage> log_messages = {
233 make_message(1, 1, 1, LOG_ID_MAIN, "test_tag", "duplicate1"),
234 make_message(2, 2, 2, LOG_ID_SYSTEM, "test_tag", "duplicate2"),
235 make_message(3, 2, 2, LOG_ID_SYSTEM, "test_tag", "duplicate2"),
236 make_message(4, 2, 2, LOG_ID_SYSTEM, "test_tag", "duplicate2"),
237 make_message(6, 2, 2, LOG_ID_SYSTEM, "test_tag", "not duplicate2"),
238 make_message(7, 1, 1, LOG_ID_MAIN, "test_tag", "duplicate1"),
239 make_message(8, 1, 1, LOG_ID_MAIN, "test_tag", "duplicate1"),
240 make_message(9, 1, 1, LOG_ID_MAIN, "test_tag", "duplicate1"),
241 make_message(10, 1, 1, LOG_ID_MAIN, "test_tag", "not duplicate1"),
242 };
243 // clang-format on
244 FixupMessages(&log_messages);
245 LogMessages(log_messages);
246
247 // After logging log_messages, the below is what should be in the buffer:
248 // PID=1, LOG_ID_MAIN duplicate1
249 // [1] PID=2, LOG_ID_SYSTEM duplicate2
250 // PID=2, LOG_ID_SYSTEM chatty drop
251 // PID=2, LOG_ID_SYSTEM duplicate2
252 // PID=2, LOG_ID_SYSTEM not duplicate2
253 // [2] PID=1, LOG_ID_MAIN chatty drop
254 // [3] PID=1, LOG_ID_MAIN duplicate1
255 // PID=1, LOG_ID_MAIN not duplicate1
256
257 // We then read from the 2nd sequence number, starting from log message [1], but filtering out
258 // everything but PID=1, which results in us starting with log message [2], which is a chatty
259 // drop. Code prior to this test case would erroneously print it. The intended behavior that
260 // this test checks prints logs starting from log message [3].
261
262 // clang-format off
263 std::vector<LogMessage> expected_log_messages = {
264 make_message(9, 1, 1, LOG_ID_MAIN, "test_tag", "duplicate1"),
265 make_message(10, 1, 1, LOG_ID_MAIN, "test_tag", "not duplicate1"),
266 };
267 FixupMessages(&expected_log_messages);
268 // clang-format on
269
270 std::vector<LogMessage> read_log_messages;
271 bool released = false;
272 {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700273 auto lock = std::lock_guard{logd_lock};
Tom Cherryca4b25d2020-05-28 20:02:42 -0700274 std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released));
275 std::unique_ptr<LogReaderThread> log_reader(
276 new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true,
277 0, ~0, 1, {}, 2, {}));
278 reader_list_.reader_threads().emplace_back(std::move(log_reader));
279 }
280
281 while (!released) {
282 usleep(5000);
283 }
284
285 CompareLogMessages(expected_log_messages, read_log_messages);
286}
287
288TEST_P(ChattyLogBufferTest, no_leading_chatty_tail) {
289 auto make_message = [&](uint32_t sec, const char* tag, const char* msg,
290 bool regex = false) -> LogMessage {
291 logger_entry entry = {
292 .pid = 1, .tid = 1, .sec = sec, .nsec = 1, .lid = LOG_ID_MAIN, .uid = 0};
293 std::string message;
294 message.push_back(ANDROID_LOG_INFO);
295 message.append(tag);
296 message.push_back('\0');
297 message.append(msg);
298 message.push_back('\0');
299 return {entry, message, regex};
300 };
301
302 // clang-format off
303 std::vector<LogMessage> log_messages = {
304 make_message(1, "test_tag", "duplicate"),
305 make_message(2, "test_tag", "duplicate"),
306 make_message(3, "test_tag", "duplicate"),
307 make_message(4, "test_tag", "not_duplicate"),
308 };
309 // clang-format on
310 FixupMessages(&log_messages);
311 LogMessages(log_messages);
312
313 // After logging log_messages, the below is what should be in the buffer:
314 // "duplicate"
315 // chatty
316 // "duplicate"
317 // "not duplicate"
318
319 // We then read the tail 3 messages expecting there to not be a chatty message, meaning that we
320 // should only see the last two messages.
321
322 // clang-format off
323 std::vector<LogMessage> expected_log_messages = {
324 make_message(3, "test_tag", "duplicate"),
325 make_message(4, "test_tag", "not_duplicate"),
326 };
327 FixupMessages(&expected_log_messages);
328 // clang-format on
329
330 std::vector<LogMessage> read_log_messages;
331 bool released = false;
332 {
Tom Cherryc5c9eba2020-10-06 10:22:35 -0700333 auto lock = std::lock_guard{logd_lock};
Tom Cherryca4b25d2020-05-28 20:02:42 -0700334 std::unique_ptr<LogWriter> test_writer(new TestWriter(&read_log_messages, &released));
335 std::unique_ptr<LogReaderThread> log_reader(
336 new LogReaderThread(log_buffer_.get(), &reader_list_, std::move(test_writer), true,
337 3, ~0, 0, {}, 1, {}));
338 reader_list_.reader_threads().emplace_back(std::move(log_reader));
339 }
340
341 while (!released) {
342 usleep(5000);
343 }
344
345 CompareLogMessages(expected_log_messages, read_log_messages);
346}
347
Tom Cherryd444ab42020-05-28 12:38:21 -0700348INSTANTIATE_TEST_CASE_P(ChattyLogBufferTests, ChattyLogBufferTest, testing::Values("chatty"));