arena_test.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. #include "src/core/lib/resource_quota/arena.h"
  19. #include <inttypes.h>
  20. #include <string.h>
  21. #include "absl/strings/str_format.h"
  22. #include "absl/strings/str_join.h"
  23. #include <grpc/support/alloc.h>
  24. #include <grpc/support/log.h>
  25. #include <grpc/support/string_util.h>
  26. #include <grpc/support/sync.h>
  27. #include "src/core/lib/gpr/string.h"
  28. #include "src/core/lib/gpr/useful.h"
  29. #include "src/core/lib/gprpp/thd.h"
  30. #include "src/core/lib/resource_quota/resource_quota.h"
  31. #include "test/core/util/test_config.h"
  32. using grpc_core::Arena;
  33. static auto* g_memory_allocator = new grpc_core::MemoryAllocator(
  34. grpc_core::ResourceQuota::Default()->memory_quota()->CreateMemoryAllocator(
  35. "test"));
  36. static void test_noop(void) { Arena::Create(1, g_memory_allocator)->Destroy(); }
  37. static void test(const char* name, size_t init_size, const size_t* allocs,
  38. size_t nallocs) {
  39. std::vector<std::string> parts;
  40. parts.push_back(
  41. absl::StrFormat("test '%s': %" PRIdPTR " <- {", name, init_size));
  42. for (size_t i = 0; i < nallocs; i++) {
  43. parts.push_back(absl::StrFormat("%" PRIdPTR ",", allocs[i]));
  44. }
  45. parts.push_back("}");
  46. std::string s = absl::StrJoin(parts, "");
  47. gpr_log(GPR_INFO, "%s", s.c_str());
  48. Arena* a = Arena::Create(init_size, g_memory_allocator);
  49. void** ps = static_cast<void**>(gpr_zalloc(sizeof(*ps) * nallocs));
  50. for (size_t i = 0; i < nallocs; i++) {
  51. ps[i] = a->Alloc(allocs[i]);
  52. // ensure the returned address is aligned
  53. GPR_ASSERT(((intptr_t)ps[i] & 0xf) == 0);
  54. // ensure no duplicate results
  55. for (size_t j = 0; j < i; j++) {
  56. GPR_ASSERT(ps[i] != ps[j]);
  57. }
  58. // ensure writable
  59. memset(ps[i], 1, allocs[i]);
  60. }
  61. a->Destroy();
  62. gpr_free(ps);
  63. }
  64. #define TEST(name, init_size, ...) \
  65. static const size_t allocs_##name[] = {__VA_ARGS__}; \
  66. test(#name, init_size, allocs_##name, GPR_ARRAY_SIZE(allocs_##name))
  67. #define CONCURRENT_TEST_THREADS 10
  68. size_t concurrent_test_iterations() {
  69. if (sizeof(void*) < 8) return 1000;
  70. return 100000;
  71. }
  72. typedef struct {
  73. gpr_event ev_start;
  74. Arena* arena;
  75. } concurrent_test_args;
  76. static void concurrent_test_body(void* arg) {
  77. concurrent_test_args* a = static_cast<concurrent_test_args*>(arg);
  78. gpr_event_wait(&a->ev_start, gpr_inf_future(GPR_CLOCK_REALTIME));
  79. for (size_t i = 0; i < concurrent_test_iterations(); i++) {
  80. *static_cast<char*>(a->arena->Alloc(1)) = static_cast<char>(i);
  81. }
  82. }
  83. static void concurrent_test(void) {
  84. gpr_log(GPR_DEBUG, "concurrent_test");
  85. concurrent_test_args args;
  86. gpr_event_init(&args.ev_start);
  87. args.arena = Arena::Create(1024, g_memory_allocator);
  88. grpc_core::Thread thds[CONCURRENT_TEST_THREADS];
  89. for (int i = 0; i < CONCURRENT_TEST_THREADS; i++) {
  90. thds[i] =
  91. grpc_core::Thread("grpc_concurrent_test", concurrent_test_body, &args);
  92. thds[i].Start();
  93. }
  94. gpr_event_set(&args.ev_start, reinterpret_cast<void*>(1));
  95. for (auto& th : thds) {
  96. th.Join();
  97. }
  98. args.arena->Destroy();
  99. }
  100. int main(int argc, char* argv[]) {
  101. grpc::testing::TestEnvironment env(argc, argv);
  102. test_noop();
  103. TEST(0_1, 0, 1);
  104. TEST(1_1, 1, 1);
  105. TEST(1_2, 1, 2);
  106. TEST(1_3, 1, 3);
  107. TEST(1_inc, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
  108. TEST(6_123, 6, 1, 2, 3);
  109. concurrent_test();
  110. return 0;
  111. }