|
|
@@ -16,13 +16,6 @@
|
|
|
#include <stdlib.h>
|
|
|
#include <unistd.h>
|
|
|
|
|
|
-enum CONSTANTS {
|
|
|
- NUM_ITER = 100000,
|
|
|
- NUM_RETRY = 8,
|
|
|
- MAX_NUM_THREADS = 12,
|
|
|
- RETRY_SLEEP_TIME_US = 2000,
|
|
|
-};
|
|
|
-
|
|
|
unsigned prime_numbers_count = 0;
|
|
|
|
|
|
bool
|
|
|
@@ -49,10 +42,10 @@ check_if_prime(void *value)
|
|
|
}
|
|
|
|
|
|
unsigned int
|
|
|
-validate()
|
|
|
+validate(int iter_num)
|
|
|
{
|
|
|
unsigned int counter = 0;
|
|
|
- for (unsigned int i = 2; i <= NUM_ITER; ++i) {
|
|
|
+ for (unsigned int i = 2; i <= iter_num; ++i) {
|
|
|
counter += is_prime(i);
|
|
|
}
|
|
|
|
|
|
@@ -60,11 +53,12 @@ validate()
|
|
|
}
|
|
|
|
|
|
void
|
|
|
-spawn_thread(pthread_t *thread, unsigned int *arg)
|
|
|
+spawn_thread(pthread_t *thread, int retry_time_us, int retry_num,
|
|
|
+ unsigned int *arg)
|
|
|
{
|
|
|
int status_code = -1;
|
|
|
- int timeout_us = RETRY_SLEEP_TIME_US;
|
|
|
- for (int tries = 0; status_code != 0 && tries < NUM_RETRY; ++tries) {
|
|
|
+ int timeout_us = retry_time_us;
|
|
|
+ for (int tries = 0; status_code != 0 && tries < retry_num; ++tries) {
|
|
|
status_code = pthread_create(thread, NULL, &check_if_prime, arg);
|
|
|
assert(status_code == 0 || status_code == EAGAIN);
|
|
|
if (status_code == EAGAIN) {
|
|
|
@@ -76,42 +70,56 @@ spawn_thread(pthread_t *thread, unsigned int *arg)
|
|
|
assert(status_code == 0 && "Thread creation should succeed");
|
|
|
}
|
|
|
|
|
|
-int
|
|
|
-main(int argc, char **argv)
|
|
|
+void
|
|
|
+test(int iter_num, int retry_num, int max_threads_num, int retry_time_us)
|
|
|
{
|
|
|
- pthread_t threads[MAX_NUM_THREADS];
|
|
|
- unsigned int args[MAX_NUM_THREADS];
|
|
|
+ pthread_t threads[max_threads_num];
|
|
|
+ unsigned int args[max_threads_num];
|
|
|
double percentage = 0.1;
|
|
|
|
|
|
- for (unsigned int factorised_number = 2; factorised_number < NUM_ITER;
|
|
|
+ for (unsigned int factorised_number = 2; factorised_number < iter_num;
|
|
|
++factorised_number) {
|
|
|
- if (factorised_number > NUM_ITER * percentage) {
|
|
|
+ if (factorised_number > iter_num * percentage) {
|
|
|
fprintf(stderr, "Stress test is %d%% finished\n",
|
|
|
(unsigned int)(percentage * 100));
|
|
|
percentage += 0.1;
|
|
|
}
|
|
|
|
|
|
- unsigned int thread_num = factorised_number % MAX_NUM_THREADS;
|
|
|
+ unsigned int thread_num = factorised_number % max_threads_num;
|
|
|
if (threads[thread_num] != 0) {
|
|
|
assert(pthread_join(threads[thread_num], NULL) == 0);
|
|
|
}
|
|
|
|
|
|
args[thread_num] = factorised_number;
|
|
|
|
|
|
- usleep(RETRY_SLEEP_TIME_US);
|
|
|
- spawn_thread(&threads[thread_num], &args[thread_num]);
|
|
|
+ usleep(retry_time_us);
|
|
|
+ spawn_thread(&threads[thread_num], retry_time_us, retry_num,
|
|
|
+ &args[thread_num]);
|
|
|
assert(threads[thread_num] != 0);
|
|
|
}
|
|
|
|
|
|
- for (int i = 0; i < MAX_NUM_THREADS; ++i) {
|
|
|
+ for (int i = 0; i < max_threads_num; ++i) {
|
|
|
assert(threads[i] == 0 || pthread_join(threads[i], NULL) == 0);
|
|
|
}
|
|
|
|
|
|
// Check the test results
|
|
|
assert(
|
|
|
- prime_numbers_count == validate()
|
|
|
+ prime_numbers_count == validate(iter_num)
|
|
|
&& "Answer mismatch between tested code and reference implementation");
|
|
|
|
|
|
fprintf(stderr, "Stress test finished successfully\n");
|
|
|
+}
|
|
|
+
|
|
|
+enum DEFAULT_PARAMETERS {
|
|
|
+ ITER_NUM = 20000,
|
|
|
+ RETRY_NUM = 8,
|
|
|
+ MAX_THREADS_NUM = 12,
|
|
|
+ RETRY_SLEEP_TIME_US = 2000,
|
|
|
+};
|
|
|
+
|
|
|
+int
|
|
|
+main(int argc, char **argv)
|
|
|
+{
|
|
|
+ test(ITER_NUM, RETRY_NUM, MAX_THREADS_NUM, RETRY_SLEEP_TIME_US);
|
|
|
return 0;
|
|
|
}
|