Skip to content

Commit c06a2b0

Browse files
authored
[test] Remove some unnecessary usage of TOTAL_MEMORY and INITIAL_MEMORY. NFC (#21303)
1 parent 833448b commit c06a2b0

File tree

2 files changed

+43
-33
lines changed

2 files changed

+43
-33
lines changed

test/pthread/test_pthread_sbrk.cpp

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,28 @@
1414
#define NUM_THREADS 8
1515
#define NUM_ALLOCATIONS 10240
1616
#if ABORTING_MALLOC
17-
#define ALLOCATION_SIZE 1280 // Malloc aborts, so allocate a bit less of memory so all fits
17+
// Malloc aborts, so allocate a bit less of memory so all fits
18+
#define ALLOCATION_SIZE 1280
1819
#else
19-
#define ALLOCATION_SIZE 2560 // Malloc doesn't abort, allocate a bit more memory to test graceful allocation failures
20+
// Malloc doesn't abort, allocate a bit more memory to test graceful allocation
21+
// failures
22+
#define ALLOCATION_SIZE 2560
2023
#endif
2124

2225
#define RESULT_OK 0
2326
#define RESULT_EXPECTED_FAILS 1
2427
#define RESULT_BAD_FAIL 2
2528

26-
// Use barriers to make each thread synchronize their execution points, to maximize the possibility of seeing race conditions
27-
// if those might occur.
29+
// Use barriers to make each thread synchronize their execution points, to
30+
// maximize the possibility of seeing race conditions if those might occur.
2831
static pthread_barrier_t barrierWaitToAlloc;
2932
static pthread_barrier_t barrierWaitToVerify;
3033
static pthread_barrier_t barrierWaitToFree;
3134

3235
// Use a mutex for logging.
3336
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
3437

35-
static void *thread_start(void *arg)
36-
{
38+
static void *thread_start(void *arg) {
3739
#if DEBUG
3840
pthread_mutex_lock( &mutex );
3941
printf("thread started, will try %d allocations of size %d\n", NUM_ALLOCATIONS, ALLOCATION_SIZE);
@@ -48,47 +50,57 @@ static void *thread_start(void *arg)
4850
int some_allocations_failed = 0;
4951
size_t allocated = 0;
5052

51-
pthread_barrier_wait(&barrierWaitToAlloc); // Halt until all threads reach here, then proceed synchronously.
52-
for(int i = 0; i < NUM_ALLOCATIONS; ++i)
53-
{
53+
// Halt until all threads reach here, then proceed synchronously.
54+
pthread_barrier_wait(&barrierWaitToAlloc);
55+
for (int i = 0; i < NUM_ALLOCATIONS; ++i) {
5456
allocated_buffers[i] = (uint8_t*)malloc(ALLOCATION_SIZE);
5557
if (allocated_buffers[i]) {
5658
memset(allocated_buffers[i], id, ALLOCATION_SIZE);
5759
allocated += ALLOCATION_SIZE;
58-
} else
60+
} else {
5961
some_allocations_failed = 1;
62+
}
6063
}
6164
#if DEBUG
6265
pthread_mutex_lock( &mutex );
6366
printf("total allocations: %u (%d of size %d tried), some failed? %d\n", allocated, NUM_ALLOCATIONS, ALLOCATION_SIZE, some_allocations_failed);
6467
pthread_mutex_unlock( &mutex );
6568
#endif
66-
pthread_barrier_wait(&barrierWaitToVerify); // Halt until all threads reach here, then proceed synchronously.
69+
// Halt until all threads reach here, then proceed synchronously.
70+
pthread_barrier_wait(&barrierWaitToVerify);
6771
int reported_once = 0;
68-
for(int i = 0; i < NUM_ALLOCATIONS; ++i)
69-
{
72+
for (int i = 0; i < NUM_ALLOCATIONS; ++i) {
7073
if (!allocated_buffers[i]) continue;
71-
for(int j = 0; j < ALLOCATION_SIZE; ++j)
72-
if (allocated_buffers[i][j] != id)
73-
{
74-
++return_code; // Failed! (but run to completion so that the barriers will all properly proceed without hanging)
74+
for (int j = 0; j < ALLOCATION_SIZE; ++j)
75+
if (allocated_buffers[i][j] != id) {
76+
// Failed! (but run to completion so that the barriers will all properly
77+
// proceed without hanging)
78+
++return_code;
7579
if (!reported_once) {
7680
emscripten_errf("Memory corrupted! mem[i]: %d != %ld, i: %d, j: %d", allocated_buffers[i][j], id, i, j);
77-
reported_once = 1; // Avoid print flood that makes debugging hard.
81+
// Avoid print flood that makes debugging hard.
82+
reported_once = 1;
7883
}
7984
}
8085
}
8186

8287
pthread_barrier_wait(&barrierWaitToFree); // Halt until all threads reach here, then proceed synchronously.
83-
for(int i = 0; i < NUM_ALLOCATIONS; ++i)
88+
for (int i = 0; i < NUM_ALLOCATIONS; ++i) {
8489
free(allocated_buffers[i]);
90+
}
8591

8692
#if ABORTING_MALLOC
87-
if (some_allocations_failed)
88-
return_code = RESULT_BAD_FAIL; // We expect allocations not to fail (if they did, shouldn't reach here, but we should have aborted)
93+
if (some_allocations_failed) {
94+
// We expect allocations not to fail (if they did, shouldn't reach here, but
95+
// we should have aborted)
96+
return_code = RESULT_BAD_FAIL;
97+
}
8998
#else
90-
if (some_allocations_failed)
91-
return_code = RESULT_EXPECTED_FAILS; // We expect to be allocating so much memory that some of the allocations fail.
99+
if (some_allocations_failed) {
100+
// We expect to be allocating so much memory that some of the allocations
101+
// fail.
102+
return_code = RESULT_EXPECTED_FAILS;
103+
}
92104
// Otherwise, the fails might happen in another thread, that's cool.
93105
#endif
94106
#if DEBUG
@@ -99,8 +111,7 @@ static void *thread_start(void *arg)
99111
pthread_exit((void*)return_code);
100112
}
101113

102-
int main()
103-
{
114+
int main() {
104115
printf("starting test, aborting? %d\n", ABORTING_MALLOC);
105116

106117
int ret = pthread_barrier_init(&barrierWaitToAlloc, NULL, NUM_THREADS);
@@ -110,9 +121,8 @@ int main()
110121
ret = pthread_barrier_init(&barrierWaitToFree, NULL, NUM_THREADS);
111122
assert(ret == 0);
112123

113-
pthread_t thr[8/*NUM_THREADS*/];
114-
for(intptr_t i = 0; i < NUM_THREADS; ++i)
115-
{
124+
pthread_t thr[NUM_THREADS];
125+
for (intptr_t i = 0; i < NUM_THREADS; ++i) {
116126
pthread_attr_t attr;
117127
pthread_attr_init(&attr);
118128
pthread_attr_setstacksize(&attr, NUM_ALLOCATIONS*80);
@@ -122,7 +132,7 @@ int main()
122132

123133
int seen_expected_fails = 0;
124134

125-
for(int i = 0; i < NUM_THREADS; ++i) {
135+
for (int i = 0; i < NUM_THREADS; ++i) {
126136
int res = 0;
127137
ret = pthread_join(thr[i], (void**)&res);
128138
assert(ret == 0);

test/test_browser.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3880,17 +3880,17 @@ def prep_no_SAB(self):
38803880
@requires_threads
38813881
def test_pthread_c11_threads(self):
38823882
self.btest_exit('pthread/test_pthread_c11_threads.c',
3883-
args=['-gsource-map', '-std=gnu11', '-pthread', '-sPROXY_TO_PTHREAD', '-sTOTAL_MEMORY=64mb'])
3883+
args=['-gsource-map', '-std=gnu11', '-pthread', '-sPROXY_TO_PTHREAD'])
38843884

38853885
@requires_threads
38863886
def test_pthread_pool_size_strict(self):
38873887
# Check that it doesn't fail with sufficient number of threads in the pool.
38883888
self.btest_exit('pthread/test_pthread_c11_threads.c',
3889-
args=['-g2', '-std=gnu11', '-pthread', '-sPTHREAD_POOL_SIZE=4', '-sPTHREAD_POOL_SIZE_STRICT=2', '-sTOTAL_MEMORY=64mb'])
3889+
args=['-g2', '-std=gnu11', '-pthread', '-sPTHREAD_POOL_SIZE=4', '-sPTHREAD_POOL_SIZE_STRICT=2'])
38903890
# Check that it fails instead of deadlocking on insufficient number of threads in the pool.
38913891
self.btest('pthread/test_pthread_c11_threads.c',
38923892
expected='abort:Assertion failed: thrd_create(&t4, thread_main, NULL) == thrd_success',
3893-
args=['-g2', '-std=gnu11', '-pthread', '-sPTHREAD_POOL_SIZE=3', '-sPTHREAD_POOL_SIZE_STRICT=2', '-sTOTAL_MEMORY=64mb'])
3893+
args=['-g2', '-std=gnu11', '-pthread', '-sPTHREAD_POOL_SIZE=3', '-sPTHREAD_POOL_SIZE_STRICT=2'])
38943894

38953895
@requires_threads
38963896
def test_pthread_in_pthread_pool_size_strict(self):
@@ -3911,7 +3911,7 @@ def test_pthread_atomics(self, args):
39113911
# Test 64-bit atomics.
39123912
@requires_threads
39133913
def test_pthread_64bit_atomics(self):
3914-
self.btest_exit('pthread/test_pthread_64bit_atomics.c', args=['-sINITIAL_MEMORY=64MB', '-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
3914+
self.btest_exit('pthread/test_pthread_64bit_atomics.c', args=['-O3', '-pthread', '-sPTHREAD_POOL_SIZE=8'])
39153915

39163916
# Test 64-bit C++11 atomics.
39173917
@parameterized({

0 commit comments

Comments
 (0)