Skip to content

Commit df81d18

Browse files
jogmenhorman
authored andcommitted
rwlocks: run threads for a constant time
Fixes: openssl/project#1259 Signed-off-by: Norbert Pocs <[email protected]> Reviewed-by: Saša Nedvědický <[email protected]> Reviewed-by: Neil Horman <[email protected]> (Merged from #32)
1 parent b496aed commit df81d18

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

source/rwlocks.c

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,28 @@
2323
#include <openssl/crypto.h>
2424
#include "perflib/perflib.h"
2525

26-
#define NUM_CALLS_PER_RUN 1000000
26+
#define RUN_TIME 5
2727

28-
int threadcount = 0;
28+
size_t threadcount = 0;
2929
int err = 0;
3030
unsigned long *dataval = NULL;
3131
int writers = 0;
3232
int readers = 0;
3333
int write_lock_calls = 0;
3434
int read_lock_calls = 0;
35-
OSSL_TIME reader_end = { 0 };
36-
OSSL_TIME writer_end = { 0 };
3735

3836
CRYPTO_RWLOCK *lock = NULL;
3937

40-
void do_rw_wlock(size_t num)
38+
size_t *counts;
39+
OSSL_TIME max_time;
40+
41+
void do_rw_wlock()
4142
{
42-
int i;
4343
unsigned long *newval, *oldval;
4444
int local_write_lock_calls = 0;
45+
OSSL_TIME time;
4546

46-
for (i = 0; i < NUM_CALLS_PER_RUN / threadcount; i++) {
47+
do {
4748
newval = OPENSSL_malloc(sizeof(int));
4849
CRYPTO_THREAD_write_lock(lock);
4950
if (dataval == NULL)
@@ -55,25 +56,25 @@ void do_rw_wlock(size_t num)
5556
CRYPTO_THREAD_unlock(lock);
5657
local_write_lock_calls += 2; /* lock and unlock */
5758
OPENSSL_free(oldval);
58-
}
59+
time = ossl_time_now();
60+
} while(time.t < max_time.t);
5961

6062
CRYPTO_THREAD_write_lock(lock);
6163
write_lock_calls += local_write_lock_calls;
6264
writers--;
6365
if (writers == 0) {
64-
writer_end = ossl_time_now();
6566
OPENSSL_free(dataval); /* free last allocation */
6667
}
6768
CRYPTO_THREAD_unlock(lock);
6869
}
6970

70-
void do_rw_rlock(size_t num)
71+
void do_rw_rlock()
7172
{
72-
int i;
7373
unsigned long last_val = 0;
7474
int local_read_lock_calls = 0;
75+
OSSL_TIME time;
7576

76-
for (i = 0; i < NUM_CALLS_PER_RUN / threadcount; i++) {
77+
do {
7778
CRYPTO_THREAD_read_lock(lock);
7879
if (dataval != NULL) {
7980
if (last_val != 0 && last_val > *dataval)
@@ -82,34 +83,32 @@ void do_rw_rlock(size_t num)
8283
}
8384
CRYPTO_THREAD_unlock(lock);
8485
local_read_lock_calls += 2; /* lock and unlock */
85-
}
86+
time = ossl_time_now();
87+
} while(time.t < max_time.t);
8688

8789
CRYPTO_THREAD_write_lock(lock);
8890
read_lock_calls += local_read_lock_calls;
8991
readers--;
90-
if (readers == 0)
91-
reader_end = ossl_time_now();
9292
CRYPTO_THREAD_unlock(lock);
9393
}
9494

9595
void do_rwlocks(size_t num)
9696
{
9797
if (num >= threadcount - writers)
98-
do_rw_wlock(num);
98+
do_rw_wlock();
9999
else
100-
do_rw_rlock(num);
100+
do_rw_rlock();
101101
}
102102

103103
int main(int argc, char *argv[])
104104
{
105105
OSSL_TIME duration;
106-
OSSL_TIME start;
107-
uint64_t us;
108106
double avwcalltime;
109107
double avrcalltime;
110108
int terse = 0;
111109
char *writeenv;
112110
int opt;
111+
int writer_threads;
113112

114113
while ((opt = getopt(argc, argv, "t")) != -1) {
115114
switch (opt) {
@@ -141,6 +140,7 @@ int main(int argc, char *argv[])
141140
if (writers == 0)
142141
writers = threadcount / 2;
143142
}
143+
writer_threads = writers;
144144

145145
lock = CRYPTO_THREAD_lock_new();
146146
if (lock == NULL) {
@@ -154,7 +154,7 @@ int main(int argc, char *argv[])
154154
printf("Running rwlock test with %d writers and %d readers\n",
155155
writers, readers);
156156

157-
start = ossl_time_now();
157+
max_time = ossl_time_add(ossl_time_now(), ossl_seconds2time(RUN_TIME));
158158

159159
if (!perflib_run_multi_thread_test(do_rwlocks, threadcount, &duration)) {
160160
printf("Failed to run the test\n");
@@ -166,18 +166,16 @@ int main(int argc, char *argv[])
166166
return EXIT_FAILURE;
167167
}
168168

169-
us = ossl_time2us(ossl_time_subtract(writer_end, start));
170-
avwcalltime = (double)us / (double)write_lock_calls;
169+
avwcalltime = (double)RUN_TIME * 1e6 * writer_threads / write_lock_calls;
171170

172171
if (!terse)
173-
printf("total write lock/unlock calls %d in %lf us\n",
174-
write_lock_calls, (double)us);
172+
printf("total write lock/unlock calls %d in %d s\n",
173+
write_lock_calls, RUN_TIME);
175174

176-
us = ossl_time2us(ossl_time_subtract(reader_end, start));
177-
avrcalltime = (double)us / (double)read_lock_calls;
175+
avrcalltime = (double)RUN_TIME * 1e6 * (threadcount-writer_threads) / read_lock_calls;
178176
if (!terse)
179-
printf("total read lock/unlock calls %d %lf us\n",
180-
read_lock_calls, (double)us);
177+
printf("total read lock/unlock calls %d %d s\n",
178+
read_lock_calls, RUN_TIME);
181179

182180
if (terse) {
183181
printf("%lf %lf\n", avwcalltime, avrcalltime);

0 commit comments

Comments
 (0)