Skip to content

Commit 28ebd57

Browse files
authored
Refactor stress tests to make them runnable in reactor mode (#2614)
1 parent 64baf54 commit 28ebd57

File tree

7 files changed

+86
-48
lines changed

7 files changed

+86
-48
lines changed

.github/workflows/nightly_run.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,11 @@ on:
88
types:
99
- opened
1010
- synchronize
11-
#running nightly pipeline if you're changing it
11+
# running nightly pipeline if you're changing it
12+
# stress tests are run only in nightly at the moment, so running them in they are changed
1213
paths:
1314
- ".github/workflows/nightly_run.yml"
15+
- "core/iwasm/libraries/lib-wasi-threads/stress-test/**"
1416

1517
# midnight UTC
1618
schedule:

core/iwasm/libraries/lib-wasi-threads/stress-test/build.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ for test_c in *.c; do
6060
-Wl,--export=wasi_thread_start \
6161
-Wl,--export=malloc \
6262
-Wl,--export=free \
63+
-Wl,--export=test \
6364
$sysroot_command \
6465
$test_c -o $test_wasm
6566
done

core/iwasm/libraries/lib-wasi-threads/stress-test/errorcheck_mutex_stress_test.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <errno.h>
88
#include "mutex_common.h"
99

10-
int
11-
main()
10+
void
11+
test()
1212
{
1313
pthread_mutex_t mutex;
1414

@@ -25,3 +25,10 @@ main()
2525
fprintf(stderr, "Errorcheck mutex test is completed\n");
2626
pthread_mutex_destroy(&mutex);
2727
}
28+
29+
int
30+
main()
31+
{
32+
test();
33+
return 0;
34+
}

core/iwasm/libraries/lib-wasi-threads/stress-test/normal_mutex_stress_test.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#include <errno.h>
88
#include "mutex_common.h"
99

10-
int
11-
main()
10+
void
11+
test()
1212
{
1313
pthread_mutex_t mutex;
1414
pthread_mutex_init(&mutex, NULL);
@@ -18,3 +18,10 @@ main()
1818
fprintf(stderr, "Normal mutex test is completed\n");
1919
pthread_mutex_destroy(&mutex);
2020
}
21+
22+
int
23+
main()
24+
{
25+
test();
26+
return 0;
27+
}

core/iwasm/libraries/lib-wasi-threads/stress-test/recursive_mutex_stress_test.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ same_thread_multiple_rec_mutex_lock(void *mutex)
3131
return NULL;
3232
}
3333

34-
int
35-
main()
34+
void
35+
test()
3636
{
3737
pthread_mutex_t mutex;
3838

@@ -63,3 +63,10 @@ main()
6363
fprintf(stderr, "Recursive mutex test is completed\n");
6464
pthread_mutex_destroy(&mutex);
6565
}
66+
67+
int
68+
main()
69+
{
70+
test();
71+
return 0;
72+
}

core/iwasm/libraries/lib-wasi-threads/stress-test/spawn_stress_test.c

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,6 @@
1616
#include <stdlib.h>
1717
#include <unistd.h>
1818

19-
enum CONSTANTS {
20-
NUM_ITER = 100000,
21-
NUM_RETRY = 8,
22-
MAX_NUM_THREADS = 12,
23-
RETRY_SLEEP_TIME_US = 2000,
24-
};
25-
2619
unsigned prime_numbers_count = 0;
2720

2821
bool
@@ -49,22 +42,23 @@ check_if_prime(void *value)
4942
}
5043

5144
unsigned int
52-
validate()
45+
validate(int iter_num)
5346
{
5447
unsigned int counter = 0;
55-
for (unsigned int i = 2; i <= NUM_ITER; ++i) {
48+
for (unsigned int i = 2; i <= iter_num; ++i) {
5649
counter += is_prime(i);
5750
}
5851

5952
return counter;
6053
}
6154

6255
void
63-
spawn_thread(pthread_t *thread, unsigned int *arg)
56+
spawn_thread(pthread_t *thread, int retry_time_us, int retry_num,
57+
unsigned int *arg)
6458
{
6559
int status_code = -1;
66-
int timeout_us = RETRY_SLEEP_TIME_US;
67-
for (int tries = 0; status_code != 0 && tries < NUM_RETRY; ++tries) {
60+
int timeout_us = retry_time_us;
61+
for (int tries = 0; status_code != 0 && tries < retry_num; ++tries) {
6862
status_code = pthread_create(thread, NULL, &check_if_prime, arg);
6963
assert(status_code == 0 || status_code == EAGAIN);
7064
if (status_code == EAGAIN) {
@@ -76,42 +70,56 @@ spawn_thread(pthread_t *thread, unsigned int *arg)
7670
assert(status_code == 0 && "Thread creation should succeed");
7771
}
7872

79-
int
80-
main(int argc, char **argv)
73+
void
74+
test(int iter_num, int retry_num, int max_threads_num, int retry_time_us)
8175
{
82-
pthread_t threads[MAX_NUM_THREADS];
83-
unsigned int args[MAX_NUM_THREADS];
76+
pthread_t threads[max_threads_num];
77+
unsigned int args[max_threads_num];
8478
double percentage = 0.1;
8579

86-
for (unsigned int factorised_number = 2; factorised_number < NUM_ITER;
80+
for (unsigned int factorised_number = 2; factorised_number < iter_num;
8781
++factorised_number) {
88-
if (factorised_number > NUM_ITER * percentage) {
82+
if (factorised_number > iter_num * percentage) {
8983
fprintf(stderr, "Stress test is %d%% finished\n",
9084
(unsigned int)(percentage * 100));
9185
percentage += 0.1;
9286
}
9387

94-
unsigned int thread_num = factorised_number % MAX_NUM_THREADS;
88+
unsigned int thread_num = factorised_number % max_threads_num;
9589
if (threads[thread_num] != 0) {
9690
assert(pthread_join(threads[thread_num], NULL) == 0);
9791
}
9892

9993
args[thread_num] = factorised_number;
10094

101-
usleep(RETRY_SLEEP_TIME_US);
102-
spawn_thread(&threads[thread_num], &args[thread_num]);
95+
usleep(retry_time_us);
96+
spawn_thread(&threads[thread_num], retry_time_us, retry_num,
97+
&args[thread_num]);
10398
assert(threads[thread_num] != 0);
10499
}
105100

106-
for (int i = 0; i < MAX_NUM_THREADS; ++i) {
101+
for (int i = 0; i < max_threads_num; ++i) {
107102
assert(threads[i] == 0 || pthread_join(threads[i], NULL) == 0);
108103
}
109104

110105
// Check the test results
111106
assert(
112-
prime_numbers_count == validate()
107+
prime_numbers_count == validate(iter_num)
113108
&& "Answer mismatch between tested code and reference implementation");
114109

115110
fprintf(stderr, "Stress test finished successfully\n");
111+
}
112+
113+
enum DEFAULT_PARAMETERS {
114+
ITER_NUM = 20000,
115+
RETRY_NUM = 8,
116+
MAX_THREADS_NUM = 12,
117+
RETRY_SLEEP_TIME_US = 2000,
118+
};
119+
120+
int
121+
main(int argc, char **argv)
122+
{
123+
test(ITER_NUM, RETRY_NUM, MAX_THREADS_NUM, RETRY_SLEEP_TIME_US);
116124
return 0;
117125
}

core/iwasm/libraries/lib-wasi-threads/stress-test/stress_test_threads_creation.c

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,6 @@
99
#include <stdio.h>
1010
#include <unistd.h>
1111

12-
enum CONSTANTS {
13-
NUM_ITER = 200000,
14-
NUM_RETRY = 8,
15-
MAX_NUM_THREADS = 12,
16-
RETRY_SLEEP_TIME_US = 4000,
17-
SECOND = 1000 * 1000 * 1000
18-
};
19-
2012
int threads_executed = 0;
2113
unsigned int threads_creation_tried = 0;
2214
unsigned int threads_in_use = 0;
@@ -31,11 +23,11 @@ thread_func(void *arg)
3123
}
3224

3325
void
34-
spawn_thread(pthread_t *thread)
26+
spawn_thread(pthread_t *thread, int retry_time, int iter_num)
3527
{
3628
int status_code = -1;
37-
int timeout_us = RETRY_SLEEP_TIME_US;
38-
for (int tries = 0; status_code != 0 && tries < NUM_RETRY; ++tries) {
29+
int timeout_us = retry_time;
30+
for (int tries = 0; status_code != 0 && tries < iter_num; ++tries) {
3931
status_code = pthread_create(thread, NULL, &thread_func, NULL);
4032
__atomic_fetch_add(&threads_creation_tried, 1, __ATOMIC_RELAXED);
4133

@@ -49,32 +41,33 @@ spawn_thread(pthread_t *thread)
4941
assert(status_code == 0 && "Thread creation should succeed");
5042
}
5143

52-
int
53-
main(int argc, char **argv)
44+
void
45+
test(int iter_num, int max_threads_num, int retry_num, int retry_time_us)
5446
{
5547
double percentage = 0.1;
48+
int second_us = 1000 * 1000 * 1000; // 1 second in us
5649

57-
for (int iter = 0; iter < NUM_ITER; ++iter) {
58-
if (iter > NUM_ITER * percentage) {
50+
for (int iter = 0; iter < iter_num; ++iter) {
51+
if (iter > iter_num * percentage) {
5952
fprintf(stderr, "Spawning stress test is %d%% finished\n",
6053
(unsigned int)(percentage * 100));
6154
percentage += 0.1;
6255
}
6356
while (__atomic_load_n(&threads_in_use, __ATOMIC_SEQ_CST)
64-
== MAX_NUM_THREADS) {
57+
== max_threads_num) {
6558
usleep(100);
6659
}
6760

6861
__atomic_fetch_add(&threads_in_use, 1, __ATOMIC_SEQ_CST);
6962
pthread_t tmp;
70-
spawn_thread(&tmp);
63+
spawn_thread(&tmp, retry_time_us, iter_num);
7164
pthread_detach(tmp);
7265
}
7366

7467
while ((__atomic_load_n(&threads_in_use, __ATOMIC_SEQ_CST) != 0)) {
7568
// Casting to int* to supress compiler warning
7669
__builtin_wasm_memory_atomic_wait32((int *)(&threads_in_use), 0,
77-
SECOND);
70+
second_us);
7871
}
7972

8073
assert(__atomic_load_n(&threads_in_use, __ATOMIC_SEQ_CST) == 0);
@@ -91,5 +84,18 @@ main(int argc, char **argv)
9184
"with retry ratio %f\n",
9285
threads_creation_tried,
9386
(1. * threads_creation_tried) / threads_executed);
87+
}
88+
89+
enum DEFAULT_PARAMETERS {
90+
ITER_NUM = 50000,
91+
RETRY_NUM = 8,
92+
MAX_NUM_THREADS = 12,
93+
RETRY_SLEEP_TIME_US = 4000,
94+
};
95+
96+
int
97+
main(int argc, char **argv)
98+
{
99+
test(ITER_NUM, MAX_NUM_THREADS, RETRY_NUM, RETRY_SLEEP_TIME_US);
94100
return 0;
95101
}

0 commit comments

Comments
 (0)