Loading...
tests/magazine_medium_test.c libmalloc-317.140.5 libmalloc-792.80.2
--- libmalloc/libmalloc-317.140.5/tests/magazine_medium_test.c
+++ libmalloc/libmalloc-792.80.2/tests/magazine_medium_test.c
@@ -6,12 +6,19 @@
 //
 
 #include <darwintest.h>
+#include "../src/internal.h"
+
+#if CONFIG_MEDIUM_ALLOCATOR
 
 #include "../src/magazine_medium.c"
 #include "magazine_testing.h"
 
 bool aggressive_madvise_enabled = false;
-T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
+uint64_t magazine_medium_madvise_window_scale_factor = 1;
+malloc_zero_policy_t malloc_zero_policy = MALLOC_ZERO_POLICY_DEFAULT;
+
+T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true), T_META_TAG_VM_PREFERRED,
+		T_META_TAG_NO_ALLOCATOR_OVERRIDE);
 
 static inline void
 medium_test_rack_setup(rack_t *rack)
@@ -67,7 +74,8 @@
 }
 
 
-T_DECL(medium_realloc_madvise_headers, "medium realloc in place maintains madvise headers",
+T_DECL(medium_realloc_madvise_headers,
+		"medium realloc in place maintains madvise headers",
 	   T_META_ENABLED(CONFIG_MEDIUM_ALLOCATOR))
 {
 	struct rack_s rack;
@@ -150,3 +158,86 @@
 
 	assert_block_madvise_headers(last_ptr, block_size + final_block_size, false, true);
 }
+
+T_DECL(madvise_scale_factor, "madvise_scale_factor changes window size",
+	   T_META_ENABLED(CONFIG_MEDIUM_ALLOCATOR))
+{
+	struct rack_s rack;
+	medium_test_rack_setup(&rack);
+
+	magazine_t *mag = NULL;
+	void *ptr = medium_malloc_should_clear(&rack, 1, false);
+	mag = get_magazine(&rack, ptr);
+	free_medium(&rack, ptr, MEDIUM_REGION_FOR_PTR(ptr), 0);
+	ptr = NULL;
+
+	magazine_medium_madvise_window_scale_factor = 1;
+	uint64_t granularity = medium_sliding_madvise_granularity(mag);
+	T_QUIET; T_ASSERT_EQ(granularity, (uint64_t) MEDIUM_MADVISE_MIN, "window is at min size when magazine is empty");
+	magazine_medium_madvise_window_scale_factor = 4;
+	granularity = medium_sliding_madvise_granularity(mag);
+	T_QUIET; T_ASSERT_EQ(granularity, 4ULL * MEDIUM_MADVISE_MIN, "scale factor multiplies min size");
+	magazine_medium_madvise_window_scale_factor = 1;
+
+	// Allocate the majority of the magazine
+	for (size_t i = 0; i < NUM_MEDIUM_BLOCKS / 2; i++) {
+		ptr = medium_malloc_should_clear(&rack, 1, false);
+		T_QUIET; T_ASSERT_NOTNULL(ptr, "allocation");
+	}
+
+	uint64_t num_bytes_allocated = (NUM_MEDIUM_BLOCKS / 2) << SHIFT_MEDIUM_QUANTUM;
+	uint64_t base_window_size = 1 << (64 - __builtin_clzl(num_bytes_allocated >> MEDIUM_MADVISE_SHIFT));
+	T_QUIET; T_ASSERT_GE(base_window_size, (uint64_t) MEDIUM_MADVISE_MIN, "window grows as more bytes are allocated");
+
+	granularity = medium_sliding_madvise_granularity(mag);
+	T_QUIET; T_ASSERT_EQ(granularity, base_window_size, "window grows correctly");
+	magazine_medium_madvise_window_scale_factor = 8;
+	granularity = medium_sliding_madvise_granularity(mag);
+	T_QUIET; T_ASSERT_EQ(granularity, 8 * base_window_size, "larger window also scales up");
+}
+
+T_DECL(medium_free_deallocate, "check medium regions deallocate when empty",
+		T_META_ENABLED(CONFIG_MEDIUM_ALLOCATOR))
+{
+	struct rack_s rack;
+	memset(&rack, 'a', sizeof(rack));
+	rack_init(&rack, RACK_TYPE_MEDIUM, 1, 0);
+	// force recirc to be released when empty
+	recirc_retained_regions = 0;
+
+	magazine_t *mag = &rack.magazines[0];
+	T_ASSERT_EQ(mag->mag_last_region, NULL, "no regions before allocation");
+
+	void *ptr = medium_malloc_should_clear(&rack, 1, false);
+	T_ASSERT_NE(ptr, NULL, "allocate");
+
+	region_t region = MEDIUM_REGION_FOR_PTR(ptr);
+	mag_index_t mag_index = MAGAZINE_INDEX_FOR_MEDIUM_REGION(region);
+	magazine_t *dest_mag = &(rack.magazines[mag_index]);
+	T_ASSERT_GE(mag_index, -1, "assert ptr not in recirc");
+
+	// Is to pass the recirc discriminant in medium_free_try_recirc_to_depot
+	// which normally prevents recirc unless 1.5x of a region has been
+	// allocated in the magazine as a whole.
+	dest_mag->num_bytes_in_magazine = (5 * MEDIUM_REGION_PAYLOAD_BYTES);
+
+	SZONE_MAGAZINE_PTR_LOCK(dest_mag);
+	medium_free_no_lock(&rack, dest_mag, mag_index, region, ptr, 1);
+
+	T_ASSERT_EQ(mag->mag_last_region, NULL, "no regions after last free");
+
+	magazine_t *depot = &rack.magazines[DEPOT_MAGAZINE_INDEX];
+	T_ASSERT_EQ(depot->mag_num_objects, 0, "no objects in depot after last free");
+	T_ASSERT_EQ(depot->num_bytes_in_magazine, 0ul, "no region in depot after last free");
+}
+
+#else // CONFIG_MEDIUM_ALLOCATOR
+
+// binaries are required to contain at least 1 test
+T_DECL(medium_test_skip, "skip medium tests", T_META_TAG_VM_PREFERRED,
+		T_META_TAG_NO_ALLOCATOR_OVERRIDE)
+{
+	T_SKIP("MallocMedium is not compiled on this platform");
+}
+
+#endif // CONFIG_MEDIUM_ALLOCATOR