File: SerializedLogBufferTest.cpp

package info (click to toggle)
android-platform-tools 34.0.5-12
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 150,900 kB
  • sloc: cpp: 805,786; java: 293,500; ansic: 128,288; xml: 127,491; python: 41,481; sh: 14,245; javascript: 9,665; cs: 3,846; asm: 2,049; makefile: 1,917; yacc: 440; awk: 368; ruby: 183; sql: 140; perl: 88; lex: 67
file content (398 lines) | stat: -rw-r--r-- 14,430 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/*
 * Copyright (C) 2021 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "SerializedLogBuffer.h"

#include <gtest/gtest.h>
#include <log/log.h>

#include "LogReaderList.h"
#include "LogSize.h"
#include "LogStatistics.h"
#include "LogTags.h"
#include "SerializedLogChunk.h"
#include "SerializedLogEntry.h"

// b/188858988 - Previously, if clearing UID logs erased the back-most chunk, then a chunk that has
// previously been closed for writing will be the back-most chunk in the list. Subsequent calls to
// SerializedLogBuffer::Log() will call SerializedLogChunk::FinishWriting(), which triggered a
// CHECK().
TEST(SerializedLogBuffer, uid_prune_deletes_last_chunk) {
    LogReaderList reader_list;
    LogTags tags;
    LogStatistics stats(false, true);
    SerializedLogBuffer log_buffer(&reader_list, &tags, &stats);

    log_buffer.SetSize(LOG_ID_MAIN, kLogBufferMinSize);
    log_buffer.Clear(LOG_ID_MAIN, 0);

    const uid_t kNonClearUid = 123;
    const std::string kDontClearMessage = "this message is not cleared";
    log_buffer.Log(LOG_ID_MAIN, log_time(0, 1), kNonClearUid, 1, 1, kDontClearMessage.data(),
                   kDontClearMessage.size());

    // Fill at least one chunk with a message with the UID that we'll clear
    const uid_t kClearUid = 555;
    const std::string kClearMessage(1024, 'c');
    size_t size_written = 0;
    while (size_written < kLogBufferMinSize / 2) {
        log_buffer.Log(LOG_ID_MAIN, log_time(1, size_written), kClearUid, 1, 1,
                       kClearMessage.data(), kClearMessage.size());
        size_written += kClearMessage.size();
    }

    log_buffer.Clear(LOG_ID_MAIN, kClearUid);

    // This previously would trigger a CHECK() in SerializedLogChunk::FinishWriting().
    log_buffer.Log(LOG_ID_MAIN, log_time(0, 2), kNonClearUid, 1, 1, kDontClearMessage.data(),
                   kDontClearMessage.size());
}

struct TestEntry {
    uint32_t uid;
    uint32_t pid;
    uint32_t tid;
    uint64_t sequence;
    log_time realtime;
    std::string msg;
};

SerializedLogChunk CreateChunk(size_t max_size, const std::vector<TestEntry>& entries,
                               bool finish_writing) {
    SerializedLogChunk chunk(max_size / SerializedLogBuffer::kChunkSizeDivisor);

    for (const auto& entry : entries) {
        if (!chunk.CanLog(sizeof(SerializedLogEntry) + entry.msg.size())) {
            EXPECT_TRUE(false) << "Test set up failure, entries don't fit in chunks";
            return chunk;
        }
        chunk.Log(entry.sequence, entry.realtime, entry.uid, entry.pid, entry.tid, entry.msg.data(),
                  entry.msg.size());
    }
    if (finish_writing) {
        chunk.FinishWriting();
    }
    return chunk;
}

void VerifyChunks(const std::list<SerializedLogChunk>& expected,
                  const std::list<SerializedLogChunk>& chunks) {
    int chunk = 0;
    auto expected_it = expected.begin();
    auto it = chunks.begin();
    for (; expected_it != expected.end() && it != chunks.end(); ++expected_it, ++it, ++chunk) {
        EXPECT_EQ(expected_it->reader_ref_count_, it->reader_ref_count_) << "chunk #" << chunk;
        EXPECT_EQ(expected_it->writer_active_, it->writer_active_) << "chunk #" << chunk;
        EXPECT_EQ(expected_it->highest_sequence_number_, it->highest_sequence_number_)
                << "chunk #" << chunk;
        EXPECT_EQ(expected_it->readers_, it->readers_) << "chunk #" << chunk;

        ASSERT_EQ(expected_it->contents_.size(), it->contents_.size()) << "chunk #" << chunk;
        ASSERT_EQ(expected_it->write_offset_, it->write_offset_) << "chunk #" << chunk;
        if (expected_it->contents_.size() > 0) {
            for (int i = 0; i < it->write_offset_; ++i) {
                EXPECT_EQ(expected_it->contents_.data()[i], it->contents_.data()[i])
                        << "chunk #" << chunk;
            }
        }

        ASSERT_EQ(expected_it->compressed_log_.size(), it->compressed_log_.size())
                << "chunk #" << chunk;
        if (expected_it->compressed_log_.size() > 0) {
            for (size_t i = 0; i < it->compressed_log_.size(); ++i) {
                EXPECT_EQ(expected_it->compressed_log_.data()[i], it->compressed_log_.data()[i])
                        << "chunk #" << chunk;
            }
        }
    }
    EXPECT_EQ(expected.end(), expected_it);
    EXPECT_EQ(chunks.end(), it);
}

// If no blocks are present before ClearLogsByUid() then no blocks should be output.
TEST(SerializedLogBuffer, uid_prune_no_blocks) {
    const uid_t kClearUid = 555;
    const size_t kMaxSize = kLogBufferMinSize;

    std::list<SerializedLogChunk> chunks;
    std::list<SerializedLogChunk> expected_chunks;

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
}

// If no UIDs to be cleared are found, then the _same exact_ block is returned.
TEST(SerializedLogBuffer, uid_prune_one_block_no_uid) {
    const uid_t kNonClearUid = 123;
    const uid_t kClearUid = 555;
    const size_t kMaxSize = kLogBufferMinSize;

    std::vector<TestEntry> entries = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 1,
             .realtime = log_time(0, 1),
             .msg = "some message"},
    };

    std::list<SerializedLogChunk> chunks;
    chunks.emplace_back(CreateChunk(kMaxSize, entries, false));
    void* original_chunk_address = reinterpret_cast<void*>(&chunks.front());

    std::list<SerializedLogChunk> expected_chunks;
    expected_chunks.push_back(CreateChunk(kMaxSize, entries, false));

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
    void* after_clear_chunk_address = reinterpret_cast<void*>(&chunks.front());
    EXPECT_EQ(original_chunk_address, after_clear_chunk_address);

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
    after_clear_chunk_address = reinterpret_cast<void*>(&chunks.front());
    EXPECT_EQ(original_chunk_address, after_clear_chunk_address);
}

std::vector<TestEntry> FilterEntries(const std::vector<TestEntry>& entries, uid_t uid_to_remove) {
    std::vector<TestEntry> filtered_entries;
    for (const auto& entry : entries) {
        if (entry.uid == uid_to_remove) {
            continue;
        }
        filtered_entries.emplace_back(entry);
    }
    return filtered_entries;
}

TEST(SerializedLogBuffer, uid_prune_one_block_some_uid) {
    const uid_t kNonClearUid = 123;
    const uid_t kClearUid = 555;
    const size_t kMaxSize = kLogBufferMinSize;

    std::list<SerializedLogChunk> chunks;
    std::vector<TestEntry> entries = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 1,
             .realtime = log_time(0, 1),
             .msg = "some message"},
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 2,
             .realtime = log_time(0, 1),
             .msg = "some cleared message"},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries, false));

    std::list<SerializedLogChunk> expected_chunks;
    expected_chunks.emplace_back(CreateChunk(kMaxSize, FilterEntries(entries, kClearUid), false));

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
}

TEST(SerializedLogBuffer, uid_prune_one_block_all_uid) {
    const uid_t kClearUid = 555;
    const size_t kMaxSize = kLogBufferMinSize;

    std::list<SerializedLogChunk> chunks;
    std::vector<TestEntry> entries = {
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 1,
             .realtime = log_time(0, 1),
             .msg = "some message"},
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 2,
             .realtime = log_time(0, 1),
             .msg = "some cleared message"},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries, false));

    std::list<SerializedLogChunk> expected_chunks;
    expected_chunks.emplace_back(CreateChunk(kMaxSize, {}, false));

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
}

TEST(SerializedLogBuffer, uid_prune_first_middle_last_chunks) {
    const uid_t kNonClearUid = 123;
    const uid_t kClearUid = 555;
    const std::string kMsg = "constant log message";
    const size_t kMaxSize =
            (sizeof(SerializedLogEntry) + kMsg.size()) * SerializedLogBuffer::kChunkSizeDivisor;

    std::list<SerializedLogChunk> chunks;
    std::vector<TestEntry> entries0 = {
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 1,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries0, true));
    std::vector<TestEntry> entries1 = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 2,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries1, true));
    std::vector<TestEntry> entries2 = {
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 3,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries2, true));
    std::vector<TestEntry> entries3 = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 4,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries3, true));
    std::vector<TestEntry> entries4 = {
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 5,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries4, false));

    std::list<SerializedLogChunk> expected_chunks;
    expected_chunks.emplace_back(CreateChunk(kMaxSize, entries1, true));
    expected_chunks.emplace_back(CreateChunk(kMaxSize, entries3, false));

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
}

TEST(SerializedLogBuffer, uid_prune_coalesce) {
    const uid_t kNonClearUid = 123;
    const uid_t kClearUid = 555;
    const std::string kMsg = "constant log message";
    const size_t kMaxSize =
            (sizeof(SerializedLogEntry) + kMsg.size()) * SerializedLogBuffer::kChunkSizeDivisor * 2;

    std::list<SerializedLogChunk> chunks;
    std::vector<TestEntry> entries0 = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 1,
             .realtime = log_time(0, 1),
             .msg = kMsg},
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 2,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries0, true));
    std::vector<TestEntry> entries1 = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 3,
             .realtime = log_time(0, 1),
             .msg = kMsg},
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 4,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries1, true));
    std::vector<TestEntry> entries2 = {
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 5,
             .realtime = log_time(0, 1),
             .msg = kMsg},
            {.uid = kClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 6,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries2, true));
    std::vector<TestEntry> entries3 = {
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 7,
             .realtime = log_time(0, 1),
             .msg = kMsg},
            {.uid = kNonClearUid,
             .pid = 10,
             .tid = 10,
             .sequence = 8,
             .realtime = log_time(0, 1),
             .msg = kMsg},
    };
    chunks.emplace_back(CreateChunk(kMaxSize, entries3, false));

    std::list<SerializedLogChunk> expected_chunks;
    expected_chunks.emplace_back(CreateChunk(kMaxSize, entries0, true));

    std::vector<TestEntry> expected_entries_1;
    expected_entries_1.emplace_back(entries1[0]);
    expected_entries_1.emplace_back(entries3[0]);
    expected_chunks.emplace_back(CreateChunk(kMaxSize, expected_entries_1, true));

    std::vector<TestEntry> expected_entries_2;
    expected_entries_2.emplace_back(entries3[1]);
    expected_chunks.emplace_back(CreateChunk(kMaxSize, expected_entries_2, false));

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);

    ClearLogsByUid(chunks, kClearUid, kMaxSize, LOG_ID_MAIN, nullptr);
    VerifyChunks(expected_chunks, chunks);
}