| 1 |
/* |
| 2 |
* |
| 3 |
* Copyright 2017 gRPC authors. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "src/core/lib/resource_quota/arena.h" |
| 20 |
|
| 21 |
#include <inttypes.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include <algorithm> |
| 25 |
#include <ostream> |
| 26 |
#include <string> |
| 27 |
#include <vector> |
| 28 |
|
| 29 |
#include "absl/strings/str_join.h" |
| 30 |
#include "gtest/gtest.h" |
| 31 |
|
| 32 |
#include <grpc/grpc.h> |
| 33 |
#include <grpc/support/sync.h> |
| 34 |
#include <grpc/support/time.h> |
| 35 |
|
| 36 |
#include "src/core/lib/gprpp/ref_counted_ptr.h" |
| 37 |
#include "src/core/lib/gprpp/thd.h" |
| 38 |
#include "src/core/lib/iomgr/exec_ctx.h" |
| 39 |
#include "src/core/lib/resource_quota/resource_quota.h" |
| 40 |
#include "test/core/util/test_config.h" |
| 41 |
|
| 42 |
namespace grpc_core { |
| 43 |
|
| 44 |
namespace { |
| 45 |
auto* g_memory_allocator = new MemoryAllocator( |
| 46 |
ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator("test")); |
| 47 |
} |
| 48 |
|
| 49 |
TEST(ArenaTest, NoOp) { |
| 50 |
ExecCtx exec_ctx; |
| 51 |
Arena::Create(1, g_memory_allocator)->Destroy(); |
| 52 |
} |
| 53 |
|
| 54 |
TEST(ArenaTest, ManagedNew) { |
| 55 |
ExecCtx exec_ctx; |
| 56 |
Arena* arena = Arena::Create(1, g_memory_allocator); |
| 57 |
for (int i = 0; i < 100; i++) { |
| 58 |
arena->ManagedNew<std::unique_ptr<int>>(std::make_unique<int>(i)); |
| 59 |
} |
| 60 |
arena->Destroy(); |
| 61 |
} |
| 62 |
|
| 63 |
struct AllocShape { |
| 64 |
size_t initial_size; |
| 65 |
std::vector<size_t> allocs; |
| 66 |
}; |
| 67 |
|
| 68 |
std::ostream& operator<<(std::ostream& out, const AllocShape& shape) { |
| 69 |
out << "AllocShape{initial_size=" << shape.initial_size |
| 70 |
<< ", allocs=" << absl::StrJoin(shape.allocs, ",") << "}"; |
| 71 |
return out; |
| 72 |
} |
| 73 |
|
| 74 |
class AllocTest : public ::testing::TestWithParam<AllocShape> {}; |
| 75 |
|
| 76 |
TEST_P(AllocTest, Works) { |
| 77 |
ExecCtx exec_ctx; |
| 78 |
Arena* a = Arena::Create(GetParam().initial_size, g_memory_allocator); |
| 79 |
std::vector<void*> allocated; |
| 80 |
for (auto alloc : GetParam().allocs) { |
| 81 |
void* p = a->Alloc(alloc); |
| 82 |
// ensure the returned address is aligned |
| 83 |
EXPECT_EQ(((intptr_t)p & 0xf), 0); |
| 84 |
// ensure no duplicate results |
| 85 |
for (auto other_p : allocated) { |
| 86 |
EXPECT_NE(p, other_p); |
| 87 |
} |
| 88 |
// ensure writable |
| 89 |
memset(p, 1, alloc); |
| 90 |
allocated.push_back(p); |
| 91 |
} |
| 92 |
a->Destroy(); |
| 93 |
} |
| 94 |
|
| 95 |
INSTANTIATE_TEST_SUITE_P( |
| 96 |
AllocTests, AllocTest, |
| 97 |
::testing::Values(AllocShape{0, {1}}, AllocShape{1, {1}}, |
| 98 |
AllocShape{1, {2}}, AllocShape{1, {3}}, |
| 99 |
AllocShape{1, {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}}, |
| 100 |
AllocShape{6, {1, 2, 3}})); |
| 101 |
|
| 102 |
#define CONCURRENT_TEST_THREADS 10 |
| 103 |
|
| 104 |
size_t concurrent_test_iterations() { |
| 105 |
if (sizeof(void*) < 8) return 1000; |
| 106 |
return 100000; |
| 107 |
} |
| 108 |
|
| 109 |
typedef struct { |
| 110 |
gpr_event ev_start; |
| 111 |
Arena* arena; |
| 112 |
} concurrent_test_args; |
| 113 |
|
| 114 |
TEST(ArenaTest, ConcurrentAlloc) { |
| 115 |
concurrent_test_args args; |
| 116 |
gpr_event_init(&args.ev_start); |
| 117 |
args.arena = Arena::Create(1024, g_memory_allocator); |
| 118 |
|
| 119 |
Thread thds[CONCURRENT_TEST_THREADS]; |
| 120 |
|
| 121 |
for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) { |
| 122 |
thds[i] = Thread( |
| 123 |
"grpc_concurrent_test", |
| 124 |
[](void* arg) { |
| 125 |
concurrent_test_args* a = static_cast<concurrent_test_args*>(arg); |
| 126 |
gpr_event_wait(&a->ev_start, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 127 |
for (size_t i = 0; i < concurrent_test_iterations(); i++) { |
| 128 |
*static_cast<char*>(a->arena->Alloc(1)) = static_cast<char>(i); |
| 129 |
} |
| 130 |
}, |
| 131 |
&args); |
| 132 |
thds[i].Start(); |
| 133 |
} |
| 134 |
|
| 135 |
gpr_event_set(&args.ev_start, reinterpret_cast<void*>(1)); |
| 136 |
|
| 137 |
for (auto& th : thds) { |
| 138 |
th.Join(); |
| 139 |
} |
| 140 |
|
| 141 |
args.arena->Destroy(); |
| 142 |
} |
| 143 |
|
| 144 |
TEST(ArenaTest, ConcurrentManagedNew) { |
| 145 |
concurrent_test_args args; |
| 146 |
gpr_event_init(&args.ev_start); |
| 147 |
args.arena = Arena::Create(1024, g_memory_allocator); |
| 148 |
|
| 149 |
Thread thds[CONCURRENT_TEST_THREADS]; |
| 150 |
|
| 151 |
for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) { |
| 152 |
thds[i] = Thread( |
| 153 |
"grpc_concurrent_test", |
| 154 |
[](void* arg) { |
| 155 |
concurrent_test_args* a = static_cast<concurrent_test_args*>(arg); |
| 156 |
gpr_event_wait(&a->ev_start, gpr_inf_future(GPR_CLOCK_REALTIME)); |
| 157 |
for (size_t i = 0; i < concurrent_test_iterations(); i++) { |
| 158 |
a->arena->ManagedNew<std::unique_ptr<int>>( |
| 159 |
std::make_unique<int>(static_cast<int>(i))); |
| 160 |
} |
| 161 |
}, |
| 162 |
&args); |
| 163 |
thds[i].Start(); |
| 164 |
} |
| 165 |
|
| 166 |
gpr_event_set(&args.ev_start, reinterpret_cast<void*>(1)); |
| 167 |
|
| 168 |
for (auto& th : thds) { |
| 169 |
th.Join(); |
| 170 |
} |
| 171 |
|
| 172 |
args.arena->Destroy(); |
| 173 |
} |
| 174 |
|
| 175 |
TEST(ArenaTest, PooledObjectsArePooled) { |
| 176 |
struct TestObj { |
| 177 |
char a[100]; |
| 178 |
}; |
| 179 |
|
| 180 |
auto arena = MakeScopedArena(1024, g_memory_allocator); |
| 181 |
auto obj = arena->MakePooled<TestObj>(); |
| 182 |
void* p = obj.get(); |
| 183 |
obj.reset(); |
| 184 |
obj = arena->MakePooled<TestObj>(); |
| 185 |
EXPECT_EQ(p, obj.get()); |
| 186 |
} |
| 187 |
|
| 188 |
TEST(ArenaTest, CreateManyObjects) { |
| 189 |
struct TestObj { |
| 190 |
char a[100]; |
| 191 |
}; |
| 192 |
auto arena = MakeScopedArena(1024, g_memory_allocator); |
| 193 |
std::vector<Arena::PoolPtr<TestObj>> objs; |
| 194 |
objs.reserve(1000); |
| 195 |
for (int i = 0; i < 1000; i++) { |
| 196 |
objs.emplace_back(arena->MakePooled<TestObj>()); |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
} // namespace grpc_core |
| 201 |
|
| 202 |
int main(int argc, char* argv[]) { |
| 203 |
grpc::testing::TestEnvironment give_me_a_name(&argc, argv); |
| 204 |
::testing::InitGoogleTest(&argc, argv); |
| 205 |
return RUN_ALL_TESTS(); |
| 206 |
} |