Loading...
tests/threaded_stress.c libmalloc-792.80.2 libmalloc-521.120.7
--- libmalloc/libmalloc-792.80.2/tests/threaded_stress.c
+++ libmalloc/libmalloc-521.120.7/tests/threaded_stress.c
@@ -3,24 +3,18 @@
 #include <stdatomic.h>
 #include <math.h>
 #include <unistd.h>
+#include <sys/sysctl.h>
+#include <mach/mach.h>
 #include <pthread.h>
 #include <malloc/malloc.h>
 #include <darwintest.h>
 
 #include <../src/internal.h>
 
-#if !MALLOC_TARGET_EXCLAVES
-#include <sys/sysctl.h>
-#include <mach/mach.h>
-typedef unsigned seed_type_t;
-#else
-typedef unsigned long seed_type_t;
-#endif // !MALLOC_TARGET_EXCLAVES
-
 // These tests are based on perf_contended_malloc_free, but intended as
 // functional stress tests rather than performance tests.
 
-T_GLOBAL_META(T_META_TAG_ALL_ALLOCATORS, T_META_TAG_VM_NOT_PREFERRED);
+T_GLOBAL_META(T_META_TAG_XZONE);
 
 // move the darwintest assertion code out of the straight line execution path
 // since it is has non-trivial overhead and codegen impact even if the assertion
@@ -29,8 +23,8 @@
 
 #pragma mark -
 
-static uint64_t
-random_busy_counts(seed_type_t *seed, uint64_t *first, uint64_t *second)
+uint64_t
+random_busy_counts(unsigned int *seed, uint64_t *first, uint64_t *second)
 {
 	uint64_t random = rand_r(seed);
 	*first = 0x4 + (random & (0x10 - 1));
@@ -87,14 +81,7 @@
 	}
 }
 
-#if MALLOC_TARGET_EXCLAVES
-static pthread_cond_t ready_cond;
-static pthread_mutex_t ready_mut;
-static uint32_t num_waiting_threads;
-#else
 static semaphore_t ready_sem, start_sem;
-#endif // MALLOC_TARGET_EXCLAVES
-
 static uint32_t nthreads;
 static _Atomic uint32_t active_thr;
 static _Atomic int64_t todo;
@@ -102,12 +89,6 @@
 static uint32_t
 ncpu(void)
 {
-#if MALLOC_TARGET_EXCLAVES
-	// TODO: Switch to sysctl once liblibc reports multi-cpu. Currently EVE runs
-	// tests on a single thread, but it's good to get some concurrenct tests in,
-	// even if the threads don't run in parallel
-	return 8;
-#else
 	static uint32_t activecpu, physicalcpu;
 	if (!activecpu) {
 		uint32_t n;
@@ -119,7 +100,6 @@
 		physicalcpu = n;
 	}
 	return MIN(activecpu, physicalcpu);
-#endif // MALLOC_TARGET_EXCLAVES
 }
 
 static uint32_t live_allocations;
@@ -136,10 +116,6 @@
 	int batch_size;
 	char *e;
 
-#if MALLOC_TARGET_EXCLAVES
-	nthreads = singlethreaded ? 1 : ncpu();
-	busy_select = 0;
-#else
 	if (singlethreaded) {
 		nthreads = 1;
 	} else {
@@ -154,7 +130,6 @@
 	if ((e = getenv("THREADED_STRESS_CPU_BUSY"))) {
 		busy_select = strtoul(e, NULL, 0);
 	}
-#endif // MALLOC_TARGET_EXCLAVES
 
 	atomic_init(&todo, iterations);
 	atomic_init(&active_thr, nthreads);
@@ -167,47 +142,18 @@
 	max_rand = (to - from) / incr;
 	assert((to - from) % incr == 0);
 
-#if MALLOC_TARGET_EXCLAVES
-	r = pthread_cond_init(&ready_cond, NULL);
-	T_QUIET; T_ASSERT_POSIX_ZERO(r, "condvar create");
-	r = pthread_mutex_init(&ready_mut, NULL);
-	T_QUIET; T_ASSERT_POSIX_ZERO(r, "mutex create");
-	num_waiting_threads = 0;
-#else
 	kr = semaphore_create(mach_task_self(), &ready_sem, SYNC_POLICY_FIFO, 0);
 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_create");
 	kr = semaphore_create(mach_task_self(), &start_sem, SYNC_POLICY_FIFO, 0);
 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_create");
-#endif // MALLOC_TARGET_EXCLAVES
-
-	// Allocate thread array on heap to avoid llvm inserting stack check, which
-	// doesn't compile
-	pthread_t *threads = malloc(sizeof(pthread_t) * nthreads);
+
+	pthread_t threads[nthreads];
 	for (int i = 0; i < nthreads; i++) {
 		r = pthread_create(&threads[i], NULL, thread_fn,
 				(void *)(uintptr_t)(i + 1));
 		T_QUIET; T_ASSERT_POSIX_ZERO(r, "pthread_create");
 	}
 
-#if MALLOC_TARGET_EXCLAVES
-	// Wait for all nthreads to signal that they're ready
-	for (;;) {
-		r = pthread_mutex_lock(&ready_mut);
-		iferr (r) {T_QUIET; T_ASSERT_POSIX_ZERO(r, NULL);}
-		T_ASSERT_POSIX_ZERO(r, "lock mutex");
-		if (num_waiting_threads == nthreads) {
-			r = pthread_cond_broadcast(&ready_cond);
-			T_ASSERT_POSIX_ZERO(r, "ready condvar broadcast");
-			r = pthread_mutex_unlock(&ready_mut);
-			T_ASSERT_POSIX_ZERO(r, "ready mutex unlock");
-			break;
-		} else {
-			r = pthread_mutex_unlock(&ready_mut);
-			T_ASSERT_POSIX_ZERO(r, "ready mutex unlock");
-			yield();
-		}
-	}
-#else
 	for (int i = 0; i < nthreads; i++) {
 		kr = semaphore_wait(ready_sem);
 		iferr (kr) {T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait");}
@@ -215,14 +161,11 @@
 
 	kr = semaphore_signal_all(start_sem);
 	iferr (kr) {T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_signal_all");}
-#endif // MALLOC_TARGET_EXCLAVES
 
 	for (int i = 0; i < nthreads; i++) {
 		r = pthread_join(threads[i], NULL);
-		T_ASSERT_POSIX_ZERO(r, "pthread_join");
-	}
-
-	free(threads);
+		T_QUIET; T_ASSERT_POSIX_ZERO(r, "pthread_join");
+	}
 }
 
 static void *
@@ -230,7 +173,7 @@
 {
 	kern_return_t kr;
 	int r;
-	seed_type_t seed;
+	unsigned int seed;
 	volatile double dummy;
 	uint64_t pos, remaining_frees;
 	void *alloc;
@@ -239,18 +182,8 @@
 	// start threads off in different positions in allocations array
 	pos = (seed - 1) * (live_allocations / nthreads);
 	remaining_frees = live_allocations;
-#if MALLOC_TARGET_EXCLAVES
-	r = pthread_mutex_lock(&ready_mut);
-	T_QUIET; T_ASSERT_POSIX_ZERO(r, NULL);
-	num_waiting_threads++;
-	r = pthread_cond_wait(&ready_cond, &ready_mut);
-	T_QUIET; T_ASSERT_POSIX_ZERO(r, NULL);
-	r = pthread_mutex_unlock(&ready_mut);
-	T_QUIET; T_ASSERT_POSIX_ZERO(r, NULL);
-#else
 	kr = semaphore_wait_signal(start_sem, ready_sem);
 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "semaphore_wait_signal");
-#endif // MALLOC_TARGET_EXCLAVES
 
 	while (1) {
 		uint64_t first, second;
@@ -263,25 +196,18 @@
 			if (!remaining_frees--) break;
 			alloc = NULL;
 		}
-
-		// Size without taking ownership to allow another thread to race to free
-		(void)malloc_size(allocations[pos % live_allocations]);
-
 		alloc = atomic_exchange(
 				(_Atomic(void *) *)&allocations[(pos++)%live_allocations],
 				alloc);
 		if (alloc) {
-			// Size again while definitely allocated
+			// Size once while allocated
 			(void)malloc_size(alloc);
 
 			dummy = busy(second);
 			free(alloc);
 
-			// Calling malloc_size on free pointers isn't safe in exclaves
-#if !MALLOC_TARGET_EXCLAVES
-			// Size again while probably free, but possibly re-allocated
+			// Try again while (possibly) free
 			malloc_size(alloc);
-#endif // !MALLOC_TARGET_EXCLAVES
 		}
 	}
 
@@ -290,8 +216,7 @@
 }
 
 T_DECL(threaded_stress_malloc_size_tiny,
-		"multi-threaded stress test for tiny malloc_size",
-		T_META_ENVVAR("MallocNanoZone=0"))
+		"multi-threaded stress test for tiny malloc_size")
 {
 	uint64_t iterations = 2000000ull;
 #if TARGET_OS_TV || TARGET_OS_WATCH
@@ -302,19 +227,6 @@
 			iterations, malloc_size_stress_thread);
 }
 
-T_DECL(threaded_stress_malloc_size_nano,
-		"multi-threaded stress test for nano malloc_size",
-		T_META_ENVVAR("MallocNanoZone=1"))
-{
-	uint64_t iterations = 2000000ull;
-#if TARGET_OS_TV || TARGET_OS_WATCH
-	iterations = 200000ull;
-#endif // TARGET_OS_TV || TARGET_OS_WATCH
-
-	malloc_threaded_stress(false, 16, 256, 16, 2048,
-			iterations, malloc_size_stress_thread);
-}
-
 T_DECL(threaded_stress_malloc_size_small,
 		"multi-threaded stress test for small malloc_size")
 {
@@ -327,8 +239,6 @@
 			iterations, malloc_size_stress_thread);
 }
 
-#if !MALLOC_TARGET_EXCLAVES
-// Exclaves don't support fork()
 static void *
 malloc_fork_stress_thread(void *arg)
 {
@@ -431,4 +341,3 @@
 	malloc_threaded_stress(false, 2048, 8192, 2048, 64,
 			iterations, malloc_fork_stress_thread);
 }
-#endif // MALLOC_TARGET_EXCLAVES