-
Notifications
You must be signed in to change notification settings - Fork 625
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81f0371
commit 850f2f3
Showing
6 changed files
with
236 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"name": "lib-wasi-threads tests" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"lib-wasi-threads tests": { | ||
"create_threads_until_limit": "Stress tests are incompatible with this test" | ||
} | ||
} |
148 changes: 148 additions & 0 deletions
148
core/iwasm/libraries/lib-wasi-threads/test/spawn_stress_test.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
/* | ||
* Copyright (C) 2023 Amazon.com Inc. or its affiliates. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
*/ | ||
|
||
#ifndef __wasi__ | ||
#error This example only compiles to WASM/WASI target | ||
#endif | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <assert.h> | ||
#include <stdbool.h> | ||
#include <unistd.h> | ||
#include <math.h> | ||
#include <pthread.h> | ||
|
||
|
||
#include "wasi_thread_start.h" | ||
|
||
enum CONSTANTS { | ||
NUM_ITER = 100000, | ||
NUM_RETRY = 5, | ||
SECOND = 1000 * 1000 * 1000, /* 1 second */ | ||
TIMEOUT = 50LL * SECOND, | ||
MAX_NUM_THREADS = 8, | ||
}; | ||
|
||
typedef struct { | ||
start_args_t base; | ||
int th_done; | ||
int thread_id; | ||
int value; | ||
} shared_t; | ||
|
||
unsigned int factorised_number = 1; | ||
unsigned int prime_numbers = 0; | ||
double percentage = 0.1; | ||
|
||
// Inited threads - Deinited threads. Should always be zero in the end. Otherwise it's a leak. | ||
int thread_init_delta = 0; | ||
|
||
shared_t threads[MAX_NUM_THREADS]; | ||
|
||
bool is_prime(unsigned int num) { | ||
for (unsigned int i = 2; i <= (unsigned int)(sqrt(num)); ++i) { | ||
if (num % i == 0) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
void | ||
__wasi_thread_start_C(int thread_id, int *start_arg) | ||
{ | ||
shared_t *data = (shared_t *)start_arg; | ||
__builtin_wasm_memory_atomic_wait32(NULL, 0, SECOND); | ||
|
||
if (is_prime(data->value)) { | ||
__atomic_fetch_add(&prime_numbers, 1, __ATOMIC_SEQ_CST); | ||
} | ||
|
||
__atomic_store_n(&data->th_done, 1, __ATOMIC_SEQ_CST); | ||
__builtin_wasm_memory_atomic_notify(&data->th_done, 1); | ||
} | ||
|
||
unsigned int validate() { | ||
unsigned int counter = 0; | ||
for (unsigned int i = 2; i <= factorised_number; ++i) { | ||
counter += is_prime(i); | ||
} | ||
|
||
return counter; | ||
} | ||
|
||
int | ||
main(int argc, char **argv) | ||
{ | ||
for (int i = 0; i < MAX_NUM_THREADS; ++i) { | ||
// Init a new thread | ||
{ | ||
assert(start_args_init(&threads[i].base)); | ||
factorised_number++; | ||
threads[i].value = factorised_number; | ||
thread_init_delta++; | ||
} | ||
|
||
threads[i].thread_id = __wasi_thread_spawn(&threads[i]); | ||
assert(threads[i].thread_id > 0 && "Thread creation should succeed"); | ||
} | ||
|
||
while (factorised_number < NUM_ITER) { | ||
if (factorised_number > (unsigned int)(NUM_ITER * percentage)) { | ||
printf("Stress test %d %% finished\n", (unsigned int)(percentage * 100)); | ||
percentage += 0.1; | ||
} | ||
|
||
for (int j = 0; j < MAX_NUM_THREADS && factorised_number < NUM_ITER; ++j) { | ||
assert(__builtin_wasm_memory_atomic_wait32(&threads[j].th_done, 0, TIMEOUT) != 2); | ||
// Deinit the expired thread | ||
{ | ||
start_args_deinit(&threads[j].base); | ||
thread_init_delta--; | ||
} | ||
|
||
// Invalidated the expired thread | ||
{ | ||
memset(&threads[j], 0, sizeof(threads[j])); | ||
} | ||
|
||
// Init a new thread | ||
{ | ||
assert(start_args_init(&threads[j].base)); | ||
thread_init_delta++; | ||
} | ||
|
||
factorised_number++; | ||
threads[j].value = factorised_number; | ||
for (int z = 0; factorised_number < NUM_ITER && z < NUM_RETRY && threads[j].thread_id < 0; z++) { | ||
threads[j].thread_id = __wasi_thread_spawn(&threads[j]); | ||
if (threads[j].thread_id < 0) | ||
__builtin_wasm_memory_atomic_wait32(NULL, 0, SECOND); | ||
} | ||
assert((factorised_number == NUM_ITER || threads[j].thread_id > 0) && "Thread creation should succeed"); | ||
} | ||
} | ||
|
||
// Cleanup | ||
for (int i = 0; i < MAX_NUM_THREADS; ++i) { | ||
if (threads[i].thread_id > 0) { | ||
assert(__builtin_wasm_memory_atomic_wait32(&threads[i].th_done, 0, TIMEOUT) != 2); | ||
} | ||
if (threads[i].base.stack != NULL) { | ||
start_args_deinit(&threads[i].base); | ||
thread_init_delta--; | ||
} | ||
} | ||
|
||
|
||
// Check the test results | ||
assert(prime_numbers == validate() && "Answer mismatch between tested code and reference implementation"); | ||
assert(factorised_number == NUM_ITER && "Counter and iteration count mismatch"); | ||
assert(thread_init_delta == 0 && "Init/Deinit threads is not matched. Check the code for memory leaks"); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters