binshard.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "test/jemalloc_test.h"
  2. /* Config -- "narenas:1,bin_shards:1-160:16|129-512:4|256-256:8" */
  3. #define NTHREADS 16
  4. #define REMOTE_NALLOC 256
  5. static void *
  6. thd_producer(void *varg) {
  7. void **mem = varg;
  8. unsigned arena, i;
  9. size_t sz;
  10. sz = sizeof(arena);
  11. /* Remote arena. */
  12. expect_d_eq(mallctl("arenas.create", (void *)&arena, &sz, NULL, 0), 0,
  13. "Unexpected mallctl() failure");
  14. for (i = 0; i < REMOTE_NALLOC / 2; i++) {
  15. mem[i] = mallocx(1, MALLOCX_TCACHE_NONE | MALLOCX_ARENA(arena));
  16. }
  17. /* Remote bin. */
  18. for (; i < REMOTE_NALLOC; i++) {
  19. mem[i] = mallocx(1, MALLOCX_TCACHE_NONE | MALLOCX_ARENA(0));
  20. }
  21. return NULL;
  22. }
  23. TEST_BEGIN(test_producer_consumer) {
  24. thd_t thds[NTHREADS];
  25. void *mem[NTHREADS][REMOTE_NALLOC];
  26. unsigned i;
  27. /* Create producer threads to allocate. */
  28. for (i = 0; i < NTHREADS; i++) {
  29. thd_create(&thds[i], thd_producer, mem[i]);
  30. }
  31. for (i = 0; i < NTHREADS; i++) {
  32. thd_join(thds[i], NULL);
  33. }
  34. /* Remote deallocation by the current thread. */
  35. for (i = 0; i < NTHREADS; i++) {
  36. for (unsigned j = 0; j < REMOTE_NALLOC; j++) {
  37. expect_ptr_not_null(mem[i][j],
  38. "Unexpected remote allocation failure");
  39. dallocx(mem[i][j], 0);
  40. }
  41. }
  42. }
  43. TEST_END
  44. static void *
  45. thd_start(void *varg) {
  46. void *ptr, *ptr2;
  47. edata_t *edata;
  48. unsigned shard1, shard2;
  49. tsdn_t *tsdn = tsdn_fetch();
  50. /* Try triggering allocations from sharded bins. */
  51. for (unsigned i = 0; i < 1024; i++) {
  52. ptr = mallocx(1, MALLOCX_TCACHE_NONE);
  53. ptr2 = mallocx(129, MALLOCX_TCACHE_NONE);
  54. edata = emap_edata_lookup(tsdn, &arena_emap_global, ptr);
  55. shard1 = edata_binshard_get(edata);
  56. dallocx(ptr, 0);
  57. expect_u_lt(shard1, 16, "Unexpected bin shard used");
  58. edata = emap_edata_lookup(tsdn, &arena_emap_global, ptr2);
  59. shard2 = edata_binshard_get(edata);
  60. dallocx(ptr2, 0);
  61. expect_u_lt(shard2, 4, "Unexpected bin shard used");
  62. if (shard1 > 0 || shard2 > 0) {
  63. /* Triggered sharded bin usage. */
  64. return (void *)(uintptr_t)shard1;
  65. }
  66. }
  67. return NULL;
  68. }
  69. TEST_BEGIN(test_bin_shard_mt) {
  70. test_skip_if(have_percpu_arena &&
  71. PERCPU_ARENA_ENABLED(opt_percpu_arena));
  72. thd_t thds[NTHREADS];
  73. unsigned i;
  74. for (i = 0; i < NTHREADS; i++) {
  75. thd_create(&thds[i], thd_start, NULL);
  76. }
  77. bool sharded = false;
  78. for (i = 0; i < NTHREADS; i++) {
  79. void *ret;
  80. thd_join(thds[i], &ret);
  81. if (ret != NULL) {
  82. sharded = true;
  83. }
  84. }
  85. expect_b_eq(sharded, true, "Did not find sharded bins");
  86. }
  87. TEST_END
  88. TEST_BEGIN(test_bin_shard) {
  89. unsigned nbins, i;
  90. size_t mib[4], mib2[4];
  91. size_t miblen, miblen2, len;
  92. len = sizeof(nbins);
  93. expect_d_eq(mallctl("arenas.nbins", (void *)&nbins, &len, NULL, 0), 0,
  94. "Unexpected mallctl() failure");
  95. miblen = 4;
  96. expect_d_eq(mallctlnametomib("arenas.bin.0.nshards", mib, &miblen), 0,
  97. "Unexpected mallctlnametomib() failure");
  98. miblen2 = 4;
  99. expect_d_eq(mallctlnametomib("arenas.bin.0.size", mib2, &miblen2), 0,
  100. "Unexpected mallctlnametomib() failure");
  101. for (i = 0; i < nbins; i++) {
  102. uint32_t nshards;
  103. size_t size, sz1, sz2;
  104. mib[2] = i;
  105. sz1 = sizeof(nshards);
  106. expect_d_eq(mallctlbymib(mib, miblen, (void *)&nshards, &sz1,
  107. NULL, 0), 0, "Unexpected mallctlbymib() failure");
  108. mib2[2] = i;
  109. sz2 = sizeof(size);
  110. expect_d_eq(mallctlbymib(mib2, miblen2, (void *)&size, &sz2,
  111. NULL, 0), 0, "Unexpected mallctlbymib() failure");
  112. if (size >= 1 && size <= 128) {
  113. expect_u_eq(nshards, 16, "Unexpected nshards");
  114. } else if (size == 256) {
  115. expect_u_eq(nshards, 8, "Unexpected nshards");
  116. } else if (size > 128 && size <= 512) {
  117. expect_u_eq(nshards, 4, "Unexpected nshards");
  118. } else {
  119. expect_u_eq(nshards, 1, "Unexpected nshards");
  120. }
  121. }
  122. }
  123. TEST_END
  124. int
  125. main(void) {
  126. return test_no_reentrancy(
  127. test_bin_shard,
  128. test_bin_shard_mt,
  129. test_producer_consumer);
  130. }