arena_test.cc
Go to the documentation of this file.
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 
20 
21 #include <inttypes.h>
22 #include <string.h>
23 
24 #include "absl/strings/str_format.h"
25 #include "absl/strings/str_join.h"
26 
27 #include <grpc/support/alloc.h>
28 #include <grpc/support/log.h>
30 #include <grpc/support/sync.h>
31 
34 #include "src/core/lib/gprpp/thd.h"
37 
38 using grpc_core::Arena;
39 
41  grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
42  "test"));
43 
44 static void test_noop(void) { Arena::Create(1, g_memory_allocator)->Destroy(); }
45 
46 static void test(const char* name, size_t init_size, const size_t* allocs,
47  size_t nallocs) {
48  std::vector<std::string> parts;
49  parts.push_back(
50  absl::StrFormat("test '%s': %" PRIdPTR " <- {", name, init_size));
51  for (size_t i = 0; i < nallocs; i++) {
52  parts.push_back(absl::StrFormat("%" PRIdPTR ",", allocs[i]));
53  }
54  parts.push_back("}");
55  std::string s = absl::StrJoin(parts, "");
56  gpr_log(GPR_INFO, "%s", s.c_str());
57 
58  Arena* a = Arena::Create(init_size, g_memory_allocator);
59  void** ps = static_cast<void**>(gpr_zalloc(sizeof(*ps) * nallocs));
60  for (size_t i = 0; i < nallocs; i++) {
61  ps[i] = a->Alloc(allocs[i]);
62  // ensure the returned address is aligned
63  GPR_ASSERT(((intptr_t)ps[i] & 0xf) == 0);
64  // ensure no duplicate results
65  for (size_t j = 0; j < i; j++) {
66  GPR_ASSERT(ps[i] != ps[j]);
67  }
68  // ensure writable
69  memset(ps[i], 1, allocs[i]);
70  }
71  a->Destroy();
72  gpr_free(ps);
73 }
74 
75 #define TEST(name, init_size, ...) \
76  static const size_t allocs_##name[] = {__VA_ARGS__}; \
77  test(#name, init_size, allocs_##name, GPR_ARRAY_SIZE(allocs_##name))
78 
79 #define CONCURRENT_TEST_THREADS 10
80 
82  if (sizeof(void*) < 8) return 1000;
83  return 100000;
84 }
85 
86 typedef struct {
90 
91 static void concurrent_test_body(void* arg) {
94  for (size_t i = 0; i < concurrent_test_iterations(); i++) {
95  *static_cast<char*>(a->arena->Alloc(1)) = static_cast<char>(i);
96  }
97 }
98 
99 static void concurrent_test(void) {
100  gpr_log(GPR_DEBUG, "concurrent_test");
101 
103  gpr_event_init(&args.ev_start);
104  args.arena = Arena::Create(1024, g_memory_allocator);
105 
107 
108  for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) {
109  thds[i] =
110  grpc_core::Thread("grpc_concurrent_test", concurrent_test_body, &args);
111  thds[i].Start();
112  }
113 
114  gpr_event_set(&args.ev_start, reinterpret_cast<void*>(1));
115 
116  for (auto& th : thds) {
117  th.Join();
118  }
119 
120  args.arena->Destroy();
121 }
122 
123 int main(int argc, char* argv[]) {
124  grpc::testing::TestEnvironment env(&argc, argv);
125 
126  test_noop();
127  TEST(0_1, 0, 1);
128  TEST(1_1, 1, 1);
129  TEST(1_2, 1, 2);
130  TEST(1_3, 1, 3);
131  TEST(1_inc, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
132  TEST(6_123, 6, 1, 2, 3);
133  concurrent_test();
134 
135  return 0;
136 }
concurrent_test_args::ev_start
gpr_event ev_start
Definition: arena_test.cc:87
GPR_INFO
#define GPR_INFO
Definition: include/grpc/impl/codegen/log.h:56
Arena
struct Arena Arena
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/arena.h:189
log.h
generate.env
env
Definition: generate.py:37
memset
return memset(p, 0, total)
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_event_engine::experimental::MemoryAllocator
Definition: memory_allocator.h:35
string.h
gpr_event_set
GPRAPI void gpr_event_set(gpr_event *ev, void *value)
Definition: sync.cc:59
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
arena.h
gpr_inf_future
GPRAPI gpr_timespec gpr_inf_future(gpr_clock_type type)
Definition: src/core/lib/gpr/time.cc:55
test
static void test(const char *name, size_t init_size, const size_t *allocs, size_t nallocs)
Definition: arena_test.cc:46
setup.name
name
Definition: setup.py:542
g_memory_allocator
static auto * g_memory_allocator
Definition: arena_test.cc:40
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
concurrent_test_args
Definition: arena_test.cc:86
grpc_core::Arena
Definition: src/core/lib/resource_quota/arena.h:45
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
string_util.h
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
GPR_ASSERT
#define GPR_ASSERT(x)
Definition: include/grpc/impl/codegen/log.h:94
absl::StrJoin
std::string StrJoin(Iterator start, Iterator end, absl::string_view sep, Formatter &&fmt)
Definition: abseil-cpp/absl/strings/str_join.h:239
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
gpr_log
GPRAPI void gpr_log(const char *file, int line, gpr_log_severity severity, const char *format,...) GPR_PRINT_FORMAT_CHECK(4
concurrent_test_args::arena
Arena * arena
Definition: arena_test.cc:88
arg
Definition: cmdline.cc:40
intptr_t
_W64 signed int intptr_t
Definition: stdint-msvc2008.h:118
grpc_core::ResourceQuota::Default
static ResourceQuotaRefPtr Default()
Definition: resource_quota.cc:27
gpr_event_init
GPRAPI void gpr_event_init(gpr_event *ev)
Definition: sync.cc:54
resource_quota.h
grpc_core::Thread::Start
void Start()
Definition: thd.h:125
gpr_event_wait
GPRAPI void * gpr_event_wait(gpr_event *ev, gpr_timespec abs_deadline)
Definition: sync.cc:73
test_config.h
gpr_event
Definition: impl/codegen/sync_generic.h:31
concurrent_test
static void concurrent_test(void)
Definition: arena_test.cc:99
alloc.h
grpc::testing::TestEnvironment
Definition: test/core/util/test_config.h:54
thd.h
arg
struct arg arg
concurrent_test_iterations
size_t concurrent_test_iterations()
Definition: arena_test.cc:81
grpc_core::Thread
Definition: thd.h:43
test_noop
static void test_noop(void)
Definition: arena_test.cc:44
GPR_DEBUG
#define GPR_DEBUG
Definition: include/grpc/impl/codegen/log.h:55
main
int main(int argc, char *argv[])
Definition: arena_test.cc:123
GPR_CLOCK_REALTIME
@ GPR_CLOCK_REALTIME
Definition: gpr_types.h:39
sync.h
concurrent_test_body
static void concurrent_test_body(void *arg)
Definition: arena_test.cc:91
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
CONCURRENT_TEST_THREADS
#define CONCURRENT_TEST_THREADS
Definition: arena_test.cc:79


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:42