Skip to content

Commit

Permalink
Fix up stylistic errors
Browse files Browse the repository at this point in the history
Reviewed-by: Paul Dale <ppzgs1@gmail.com>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from #168)
  • Loading branch information
nhorman authored and t8m committed Apr 24, 2024
1 parent 3079057 commit 291a580
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
8 changes: 8 additions & 0 deletions perf/README
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ a memory BIO with a private RSA key. It does 100000 repetitions divided evenly
among each thread. The number of threads to use is provided as an argument and
the test reports the average time take to execute a block of 1000
PEM_read_bio_PrivateKey() calls.

rwlocks
-------------
the rwlocks test creates the command line specified number of threads, splitting
them evenly between read and write functions (though this is adjustable via the
LOCK_WRITERS environment variable). Threads then iteratively acquire a shared
rwlock to read or update some shared data. The number of read and write
lock/unlock pairs are reported as a performance measurement
41 changes: 22 additions & 19 deletions perf/rwlocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@
#include <openssl/crypto.h>
#include "perflib/perflib.h"

#define NUM_CALLS_PER_BLOCK 1000
#define NUM_CALL_BLOCKS_PER_THREAD 1000
#define NUM_CALLS_PER_THREAD (NUM_CALLS_PER_BLOCK * NUM_CALL_BLOCKS_PER_THREAD)
#define NUM_CALLS_PER_RUN 1000000

int threadcount = 0;
int err = 0;
Expand All @@ -37,8 +35,8 @@ void do_rw_wlock(size_t num)
unsigned long *newval, *oldval;
int local_write_lock_calls = 0;

for (i = 0; i < NUM_CALLS_PER_THREAD; i++) {
newval = OPENSSL_malloc(sizeof(int));
for (i = 0; i < NUM_CALLS_PER_RUN / threadcount; i++) {
newval = OPENSSL_malloc(sizeof(int));
CRYPTO_THREAD_write_lock(lock);
if (dataval == NULL)
*newval = 1;
Expand All @@ -54,9 +52,11 @@ void do_rw_wlock(size_t num)
CRYPTO_THREAD_write_lock(lock);
write_lock_calls += local_write_lock_calls;
writers--;
if (writers == 0)
if (writers == 0) {
writer_end = ossl_time_now();
CRYPTO_THREAD_unlock(lock);
OPENSSL_free(dataval); /* free last allocation */
}
CRYPTO_THREAD_unlock(lock);
}

void do_rw_rlock(size_t num)
Expand All @@ -65,7 +65,7 @@ void do_rw_rlock(size_t num)
unsigned long last_val = 0;
int local_read_lock_calls = 0;

for (i = 0; i < NUM_CALLS_PER_THREAD; i++) {
for (i = 0; i < NUM_CALLS_PER_RUN / threadcount; i++) {
CRYPTO_THREAD_read_lock(lock);
if (dataval != NULL) {
if (last_val != 0 && last_val > *dataval)
Expand Down Expand Up @@ -104,7 +104,7 @@ int main(int argc, char *argv[])
char *writeenv;

if ((argc != 2 && argc != 3)
|| (argc == 3 && strcmp("--terse", argv[1]) != 0)) {
|| (argc == 3 && strcmp("--terse", argv[1]) != 0)) {
printf("Usage: rwlocks [--terse] threadcount\n");
return EXIT_FAILURE;
}
Expand All @@ -122,11 +122,11 @@ int main(int argc, char *argv[])
return EXIT_FAILURE;
}

writeenv=getenv("LOCK_WRITERS");
writeenv = getenv("LOCK_WRITERS");
if (writeenv == NULL) {
writers = threadcount / 2;
} else {
writers=atoi(writeenv);
writers = atoi(writeenv);
if (writers == 0)
writers = threadcount / 2;
}
Expand All @@ -140,9 +140,10 @@ int main(int argc, char *argv[])
readers = threadcount - writers;

if (!terse)
printf("Running rwlock test with %d writers and %d readers\n", writers, readers);
printf("Running rwlock test with %d writers and %d readers\n",
writers, readers);

start = ossl_time_now();
start = ossl_time_now();

if (!perflib_run_multi_thread_test(do_rwlocks, threadcount, &duration)) {
printf("Failed to run the test\n");
Expand All @@ -158,19 +159,21 @@ int main(int argc, char *argv[])
avwcalltime = (double)us / (double)write_lock_calls;

if (!terse)
printf("total write lock/unlock calls %d in %lf us\n", write_lock_calls, (double)us);
printf("total write lock/unlock calls %d in %lf us\n",
write_lock_calls, (double)us);

us = ossl_time2us(ossl_time_subtract(reader_end, start));
avrcalltime = (double)us / (double)read_lock_calls;
if (!terse)
printf("total read lock/unlock calls %d %lf us\n", read_lock_calls, (double)us);
printf("total read lock/unlock calls %d %lf us\n",
read_lock_calls, (double)us);

if (terse)
if (terse) {
printf("%lf %lf\n", avwcalltime, avrcalltime);
else {
printf("Average time per CRYPTO_THREAD_write_lock/unlock call pair: %lfus\n",
} else {
printf("Average time per write_lock/unlock call pair: %lfus\n",
avwcalltime);
printf("Average time per CRYPTO_THREAD_read_lock/unlock call pair: %lfus\n",
printf("Average time per read_lock/unlock call pair: %lfus\n",
avrcalltime);
}

Expand Down

0 comments on commit 291a580

Please sign in to comment.