1 /*
2 * Copyright (C) 2023 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 "src/trace_processor/util/bump_allocator.h"
18
19 #include <limits>
20 #include <random>
21 #include <vector>
22
23 #include "perfetto/ext/base/utils.h"
24 #include "test/gtest_and_gmock.h"
25
26 namespace perfetto {
27 namespace trace_processor {
28
29 class BumpAllocatorUnittest : public ::testing::Test {
30 public:
31 // Allocates |size| bytes of memory with aligned to |align|, writes |size|
32 // bytes in the region, reads |size| bytes and then frees the memory.
33 //
34 // Very useful to check that none of the internal DCHECKs of the allocator
35 // fire.
AllocateWriteReadAndFree(uint32_t size)36 void AllocateWriteReadAndFree(uint32_t size) {
37 BumpAllocator::AllocId id = allocator_.Alloc(size);
38 uint8_t* ptr = static_cast<uint8_t*>(allocator_.GetPointer(id));
39
40 std::vector<uint8_t> data(size);
41 for (uint32_t i = 0; i < size; ++i) {
42 data[i] = static_cast<uint8_t>(rnd_engine_() &
43 std::numeric_limits<uint8_t>::max());
44 }
45 memcpy(ptr, data.data(), size);
46 ASSERT_EQ(memcmp(ptr, data.data(), size), 0);
47 allocator_.Free(id);
48 }
49
50 protected:
51 std::minstd_rand0 rnd_engine_;
52 BumpAllocator allocator_;
53 };
54
TEST_F(BumpAllocatorUnittest,AllocSmoke)55 TEST_F(BumpAllocatorUnittest, AllocSmoke) {
56 AllocateWriteReadAndFree(8);
57 AllocateWriteReadAndFree(16);
58 AllocateWriteReadAndFree(24);
59 AllocateWriteReadAndFree(64);
60 AllocateWriteReadAndFree(1024);
61 AllocateWriteReadAndFree(BumpAllocator::kChunkSize);
62
63 allocator_.EraseFrontFreeChunks();
64 }
65
TEST_F(BumpAllocatorUnittest,EraseFrontAtAnyTime)66 TEST_F(BumpAllocatorUnittest, EraseFrontAtAnyTime) {
67 BumpAllocator::AllocId id = allocator_.Alloc(8);
68 allocator_.EraseFrontFreeChunks();
69 allocator_.Free(id);
70 allocator_.EraseFrontFreeChunks();
71 }
72
TEST_F(BumpAllocatorUnittest,PastEndOnChunkBoundary)73 TEST_F(BumpAllocatorUnittest, PastEndOnChunkBoundary) {
74 BumpAllocator::AllocId id = allocator_.Alloc(BumpAllocator::kChunkSize);
75 BumpAllocator::AllocId past_end = allocator_.PastTheEndId();
76 ASSERT_GT(past_end, id);
77 ASSERT_EQ(past_end.chunk_index, 1u);
78 ASSERT_EQ(past_end.chunk_offset, 0u);
79 allocator_.Free(id);
80 }
81
TEST_F(BumpAllocatorUnittest,EraseFrontAccounting)82 TEST_F(BumpAllocatorUnittest, EraseFrontAccounting) {
83 AllocateWriteReadAndFree(8);
84 ASSERT_EQ(allocator_.EraseFrontFreeChunks(), 1u);
85 ASSERT_EQ(allocator_.erased_front_chunks_count(), 1u);
86 AllocateWriteReadAndFree(8);
87 ASSERT_EQ(allocator_.EraseFrontFreeChunks(), 1u);
88 ASSERT_EQ(allocator_.erased_front_chunks_count(), 2u);
89 }
90
TEST_F(BumpAllocatorUnittest,EraseFrontFreeChunk)91 TEST_F(BumpAllocatorUnittest, EraseFrontFreeChunk) {
92 AllocateWriteReadAndFree(8);
93 allocator_.EraseFrontFreeChunks();
94
95 auto past_id = allocator_.PastTheEndId();
96 ASSERT_EQ(past_id.chunk_index, 1u);
97 ASSERT_EQ(past_id.chunk_offset, 0u);
98
99 auto id = allocator_.Alloc(8);
100 ASSERT_EQ(id.chunk_index, past_id.chunk_index);
101 ASSERT_EQ(id.chunk_offset, past_id.chunk_offset);
102 allocator_.Free(id);
103 }
104
TEST_F(BumpAllocatorUnittest,StressTest)105 TEST_F(BumpAllocatorUnittest, StressTest) {
106 std::minstd_rand0 rnd_engine;
107 for (int i = 0; i < 1000; i++) {
108 uint32_t size =
109 static_cast<uint32_t>((rnd_engine() * 8) % BumpAllocator::kChunkSize);
110 AllocateWriteReadAndFree(size);
111 }
112 }
113
114 } // namespace trace_processor
115 } // namespace perfetto
116