Move some common code out of randbytes
authorMatt Caswell <matt@openssl.org>
Wed, 17 May 2023 15:03:06 +0000 (16:03 +0100)
committerMatt Caswell <matt@openssl.org>
Mon, 29 May 2023 10:43:59 +0000 (11:43 +0100)
We move some code out of the randbytes test into perflib so we can later
reuse it.

Reviewed-by: Richard Levitte <levitte@openssl.org>
(Merged from https://github.com/openssl/tools/pull/146)

perf/libperf.a [new file with mode: 0644]
perf/perfhelper.o [new file with mode: 0644]
perf/perflib/perfhelper.c [new file with mode: 0644]
perf/perflib/perflib.h [moved from perf/perflib/threads.h with 63% similarity]
perf/perflib/threads.c
perf/randbytes [new file with mode: 0755]
perf/randbytes.c
perf/threads.o [new file with mode: 0644]
perf/time.o [new file with mode: 0644]

diff --git a/perf/libperf.a b/perf/libperf.a
new file mode 100644 (file)
index 0000000..f4d4fb0
Binary files /dev/null and b/perf/libperf.a differ
diff --git a/perf/perfhelper.o b/perf/perfhelper.o
new file mode 100644 (file)
index 0000000..2913696
Binary files /dev/null and b/perf/perfhelper.o differ
diff --git a/perf/perflib/perfhelper.c b/perf/perflib/perfhelper.c
new file mode 100644 (file)
index 0000000..2a71cbc
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
+#include <openssl/crypto.h>
+#include "perflib/perflib.h"
+
+int perflib_run_multi_thread_test(void (*f)(void), size_t threadcount,
+                                  OSSL_TIME *duration)
+{
+    OSSL_TIME start, end;
+    thread_t *threads;
+    size_t i;
+
+    threads = OPENSSL_malloc(sizeof(*threads) * threadcount);
+    if (threads == NULL)
+        return 0;
+
+    start = ossl_time_now();
+
+    for (i = 0; i < threadcount; i++)
+        perflib_run_thread(&threads[i], f);
+
+    for (i = 0; i < threadcount; i++)
+        perflib_wait_for_thread(threads[i]);
+
+    end = ossl_time_now();
+    OPENSSL_free(threads);
+
+    *duration = ossl_time_subtract(end, start);
+
+    return 1;
+}
similarity index 63%
rename from perf/perflib/threads.h
rename to perf/perflib/perflib.h
index 9005b53a87f645e619f57244610866eecb05d803..0a92c2ff444a4499f43099f0ca8bc37c76d93b17 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
+ * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
  *
  * Licensed under the Apache License 2.0 (the "License").  You may not use
  * this file except in compliance with the License.  You can obtain a copy
@@ -7,10 +7,13 @@
  * https://www.openssl.org/source/license.html
  */
 
-#ifndef OSSL_PERFLIB_THREADS_H
-# define OSSL_PERFLIB_THREADS_H
+#ifndef OSSL_PERFLIB_PERFLIB_H
+# define OSSL_PERFLIB_PERFLIB_H
 # pragma once
 
+#include <stdlib.h>
+#include "perflib/time.h"
+
 # if defined(_WIN32)
 
 #  include <windows.h>
@@ -27,5 +30,7 @@ typedef pthread_t thread_t;
 
 int perflib_run_thread(thread_t *t, void (*f)(void));
 int perflib_wait_for_thread(thread_t thread);
+int perflib_run_multi_thread_test(void (*f)(void), size_t threadcount,
+                                  OSSL_TIME *duration);
 
 #endif
index 74ed8fde382c2053aeaf0ec341642de2e03fbac8..9e46ce81a25e46b96f288d784d7e3e28b0e50738 100644 (file)
@@ -7,7 +7,7 @@
  * https://www.openssl.org/source/license.html
  */
 
-#include "perflib/threads.h"
+#include "perflib/perflib.h"
 
 #if defined(_WIN32)
 
diff --git a/perf/randbytes b/perf/randbytes
new file mode 100755 (executable)
index 0000000..b7d068f
Binary files /dev/null and b/perf/randbytes differ
index 7ad50a3de016e01184d6f3410abd46be7580e943..6578dd7557c7787cf76ffecbcaa8bfa8eeee6cf9 100644 (file)
@@ -1,9 +1,17 @@
+/*
+ * Copyright 2023 The OpenSSL Project Authors. All Rights Reserved.
+ *
+ * Licensed under the Apache License 2.0 (the "License").  You may not use
+ * this file except in compliance with the License.  You can obtain a copy
+ * in the file LICENSE in the source distribution or at
+ * https://www.openssl.org/source/license.html
+ */
+
 #include <stdlib.h>
 #include <stdio.h>
 #include <openssl/rand.h>
 #include <openssl/crypto.h>
-#include "perflib/threads.h"
-#include "perflib/time.h"
+#include "perflib/perflib.h"
 
 #define NUM_CALLS_PER_BLOCK         100
 #define NUM_CALL_BLOCKS_PER_THREAD  100
@@ -23,9 +31,8 @@ void do_randbytes(void)
 
 int main(int argc, char *argv[])
 {
-    int threadcount, i;
-    thread_t *threads;
-    OSSL_TIME start, end;
+    int threadcount;
+    OSSL_TIME duration;
     uint64_t us;
     double avcalltime;
 
@@ -40,30 +47,17 @@ int main(int argc, char *argv[])
         return EXIT_FAILURE;
     }
 
-    threads = OPENSSL_malloc(sizeof(*threads) * threadcount);
-    if (threads == NULL)
-    {
-        printf("malloc failure\n");
+    if (!perflib_run_multi_thread_test(do_randbytes, threadcount, &duration)) {
+        printf("Failed to run the test\n");
         return EXIT_FAILURE;
     }
 
-    start = ossl_time_now();
-
-    for (i = 0; i < threadcount; i++)
-        perflib_run_thread(&threads[i], do_randbytes);
-
-    for (i = 0; i < threadcount; i++)
-        perflib_wait_for_thread(threads[i]);
-
-    end = ossl_time_now();
-    OPENSSL_free(threads);
-
     if (err) {
         printf("Error during test\n");
         return EXIT_FAILURE;
     }
 
-    us = ossl_time2us(ossl_time_subtract(end, start));
+    us = ossl_time2us(duration);
 
     avcalltime = (double)us / (NUM_CALL_BLOCKS_PER_THREAD * threadcount);
 
diff --git a/perf/threads.o b/perf/threads.o
new file mode 100644 (file)
index 0000000..0154848
Binary files /dev/null and b/perf/threads.o differ
diff --git a/perf/time.o b/perf/time.o
new file mode 100644 (file)
index 0000000..8b5b787
Binary files /dev/null and b/perf/time.o differ