pa.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "test/jemalloc_test.h"
  2. #include "jemalloc/internal/pa.h"
  3. static void *
  4. alloc_hook(extent_hooks_t *extent_hooks, void *new_addr, size_t size,
  5. size_t alignment, bool *zero, bool *commit, unsigned arena_ind) {
  6. void *ret = pages_map(new_addr, size, alignment, commit);
  7. return ret;
  8. }
  9. static bool
  10. merge_hook(extent_hooks_t *extent_hooks, void *addr_a, size_t size_a,
  11. void *addr_b, size_t size_b, bool committed, unsigned arena_ind) {
  12. return !maps_coalesce;
  13. }
  14. static bool
  15. split_hook(extent_hooks_t *extent_hooks, void *addr, size_t size,
  16. size_t size_a, size_t size_b, bool committed, unsigned arena_ind) {
  17. return !maps_coalesce;
  18. }
  19. static void
  20. init_test_extent_hooks(extent_hooks_t *hooks) {
  21. /*
  22. * The default hooks are mostly fine for testing. A few of them,
  23. * though, access globals (alloc for dss setting in an arena, split and
  24. * merge touch the global emap to find head state. The first of these
  25. * can be fixed by keeping that state with the hooks, where it logically
  26. * belongs. The second, though, we can only fix when we use the extent
  27. * hook API.
  28. */
  29. memcpy(hooks, &ehooks_default_extent_hooks, sizeof(extent_hooks_t));
  30. hooks->alloc = &alloc_hook;
  31. hooks->merge = &merge_hook;
  32. hooks->split = &split_hook;
  33. }
  34. typedef struct test_data_s test_data_t;
  35. struct test_data_s {
  36. pa_shard_t shard;
  37. pa_central_t central;
  38. base_t *base;
  39. emap_t emap;
  40. pa_shard_stats_t stats;
  41. malloc_mutex_t stats_mtx;
  42. extent_hooks_t hooks;
  43. };
  44. test_data_t *init_test_data(ssize_t dirty_decay_ms, ssize_t muzzy_decay_ms) {
  45. test_data_t *test_data = calloc(1, sizeof(test_data_t));
  46. assert_ptr_not_null(test_data, "");
  47. init_test_extent_hooks(&test_data->hooks);
  48. base_t *base = base_new(TSDN_NULL, /* ind */ 1, &test_data->hooks,
  49. /* metadata_use_hooks */ true);
  50. assert_ptr_not_null(base, "");
  51. test_data->base = base;
  52. bool err = emap_init(&test_data->emap, test_data->base,
  53. /* zeroed */ true);
  54. assert_false(err, "");
  55. nstime_t time;
  56. nstime_init(&time, 0);
  57. err = pa_central_init(&test_data->central, base, opt_hpa,
  58. &hpa_hooks_default);
  59. assert_false(err, "");
  60. const size_t pa_oversize_threshold = 8 * 1024 * 1024;
  61. err = pa_shard_init(TSDN_NULL, &test_data->shard, &test_data->central,
  62. &test_data->emap, test_data->base, /* ind */ 1, &test_data->stats,
  63. &test_data->stats_mtx, &time, pa_oversize_threshold, dirty_decay_ms,
  64. muzzy_decay_ms);
  65. assert_false(err, "");
  66. return test_data;
  67. }
  68. void destroy_test_data(test_data_t *data) {
  69. base_delete(TSDN_NULL, data->base);
  70. free(data);
  71. }
  72. static void *
  73. do_alloc_free_purge(void *arg) {
  74. test_data_t *test_data = (test_data_t *)arg;
  75. for (int i = 0; i < 10 * 1000; i++) {
  76. bool deferred_work_generated = false;
  77. edata_t *edata = pa_alloc(TSDN_NULL, &test_data->shard, PAGE,
  78. PAGE, /* slab */ false, /* szind */ 0, /* zero */ false,
  79. /* guarded */ false, &deferred_work_generated);
  80. assert_ptr_not_null(edata, "");
  81. pa_dalloc(TSDN_NULL, &test_data->shard, edata,
  82. &deferred_work_generated);
  83. malloc_mutex_lock(TSDN_NULL,
  84. &test_data->shard.pac.decay_dirty.mtx);
  85. pac_decay_all(TSDN_NULL, &test_data->shard.pac,
  86. &test_data->shard.pac.decay_dirty,
  87. &test_data->shard.pac.stats->decay_dirty,
  88. &test_data->shard.pac.ecache_dirty, true);
  89. malloc_mutex_unlock(TSDN_NULL,
  90. &test_data->shard.pac.decay_dirty.mtx);
  91. }
  92. return NULL;
  93. }
  94. TEST_BEGIN(test_alloc_free_purge_thds) {
  95. test_data_t *test_data = init_test_data(0, 0);
  96. thd_t thds[4];
  97. for (int i = 0; i < 4; i++) {
  98. thd_create(&thds[i], do_alloc_free_purge, test_data);
  99. }
  100. for (int i = 0; i < 4; i++) {
  101. thd_join(thds[i], NULL);
  102. }
  103. }
  104. TEST_END
  105. int
  106. main(void) {
  107. return test(
  108. test_alloc_free_purge_thds);
  109. }