From 30e91bc6d2b6249da1c8c40582c63601a4838efc Mon Sep 17 00:00:00 2001 From: kinichiro Date: Sun, 14 Jul 2019 18:37:59 +0900 Subject: [PATCH] Enable speed on win32 - Use thread and sleep instead of signal and alarm, on win32 - Disable -multi option on win32 since fork is hard to implement --- CMakeLists.txt | 2 +- apps/openssl/apps_win.c | 66 +++++++++++++++++++++++++++ m4/check-os-options.m4 | 2 +- patches/speed.c.patch | 99 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 167 insertions(+), 2 deletions(-) create mode 100644 patches/speed.c.patch diff --git a/CMakeLists.txt b/CMakeLists.txt index 91a2703..66ac3b0 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -107,7 +107,7 @@ if(WIN32) add_definitions(-D_CRT_DEPRECATED_NO_WARNINGS) add_definitions(-D_REENTRANT -D_POSIX_THREAD_SAFE_FUNCTIONS) add_definitions(-DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0600) - add_definitions(-DCPPFLAGS -DOPENSSL_NO_SPEED -DNO_SYSLOG -DNO_CRYPT) + add_definitions(-DCPPFLAGS -DNO_SYSLOG -DNO_CRYPT) set(PLATFORM_LIBS ${PLATFORM_LIBS} ws2_32) endif() diff --git a/apps/openssl/apps_win.c b/apps/openssl/apps_win.c index 364c033..ba7b1e6 100644 --- a/apps/openssl/apps_win.c +++ b/apps/openssl/apps_win.c @@ -70,3 +70,69 @@ destroy_ui(void) ui_method = NULL; } } + +static void (*speed_alarm_handler)(int); +static HANDLE speed_thread; +static unsigned int speed_lapse; +static volatile unsigned int speed_schlock; + +void +speed_signal(int sigcatch, void (*func)(int sigraised)) +{ + speed_alarm_handler = func; +} + +static DWORD WINAPI +speed_timer(VOID * arg) +{ + speed_schlock = 1; + Sleep(speed_lapse); + (*speed_alarm_handler)(0); + return (0); +} + +unsigned int +speed_alarm(unsigned int seconds) +{ + DWORD err; + + speed_lapse = seconds * 1000; + speed_schlock = 0; + + speed_thread = CreateThread(NULL, 4096, speed_timer, NULL, 0, NULL); + if (speed_thread == NULL) { + err = GetLastError(); + BIO_printf(bio_err, "CreateThread failed (%lu)", err); + ExitProcess(err); + } + + while (!speed_schlock) + Sleep(0); + + return (seconds); +} + +void +speed_alarm_free(int run) +{ + DWORD err; + + if (run) { + if (TerminateThread(speed_thread, 0) == 0) { + err = GetLastError(); + BIO_printf(bio_err, "TerminateThread failed (%lu)", + err); + ExitProcess(err); + } + } + + if (CloseHandle(speed_thread) == 0) { + err = GetLastError(); + BIO_printf(bio_err, "CloseHandle failed (%lu)", err); + ExitProcess(err); + } + + speed_thread = NULL; + speed_lapse = 0; + speed_schlock = 0; +} diff --git a/m4/check-os-options.m4 b/m4/check-os-options.m4 index c88c259..6483c89 100644 --- a/m4/check-os-options.m4 +++ b/m4/check-os-options.m4 @@ -107,7 +107,7 @@ char buf[1]; getentropy(buf, 1); CPPFLAGS="$CPPFLAGS -D_GNU_SOURCE -D_POSIX -D_POSIX_SOURCE -D__USE_MINGW_ANSI_STDIO" CPPFLAGS="$CPPFLAGS -D_REENTRANT -D_POSIX_THREAD_SAFE_FUNCTIONS" CPPFLAGS="$CPPFLAGS -DWIN32_LEAN_AND_MEAN -D_WIN32_WINNT=0x0600" - CPPFLAGS="$CPPFLAGS -DOPENSSL_NO_SPEED" + CPPFLAGS="$CPPFLAGS" AC_SUBST([PLATFORM_LDADD], ['-lws2_32']) ;; *solaris*) diff --git a/patches/speed.c.patch b/patches/speed.c.patch new file mode 100644 index 0000000..28b060b --- /dev/null +++ b/patches/speed.c.patch @@ -0,0 +1,99 @@ +--- apps/openssl/speed.c.orig Tue Jul 2 21:58:28 2019 ++++ apps/openssl/speed.c Sun Jul 14 19:39:44 2019 +@@ -159,7 +159,16 @@ static void + pkey_print_message(const char *str, const char *str2, + long num, int bits, int sec); + static void print_result(int alg, int run_no, int count, double time_used); ++#ifndef _WIN32 + static int do_multi(int multi); ++#else ++void speed_signal(int sigcatch, void (*func)(int sigraised)); ++unsigned int speed_alarm(unsigned int seconds); ++void speed_alarm_free(int run); ++#define SIGALRM 14 ++#define signal(sigcatch, func) speed_signal((sigcatch), (func)) ++#define alarm(seconds) speed_alarm((seconds)) ++#endif + + #define ALGOR_NUM 32 + #define SIZE_NUM 5 +@@ -466,8 +475,10 @@ speed_main(int argc, char **argv) + const EVP_CIPHER *evp_cipher = NULL; + const EVP_MD *evp_md = NULL; + int decrypt = 0; ++#ifndef _WIN32 + int multi = 0; + const char *errstr = NULL; ++#endif + + if (single_execution) { + if (pledge("stdio proc", NULL) == -1) { +@@ -544,6 +555,7 @@ speed_main(int argc, char **argv) + j--; /* Otherwise, -decrypt gets confused with an + * algorithm. */ + } ++#ifndef _WIN32 + else if ((argc > 0) && (strcmp(*argv, "-multi") == 0)) { + argc--; + argv++; +@@ -559,6 +571,7 @@ speed_main(int argc, char **argv) + j--; /* Otherwise, -multi gets confused with an + * algorithm. */ + } ++#endif + else if (argc > 0 && !strcmp(*argv, "-mr")) { + mr = 1; + j--; /* Otherwise, -mr gets confused with an +@@ -921,7 +934,9 @@ speed_main(int argc, char **argv) + BIO_printf(bio_err, "-evp e use EVP e.\n"); + BIO_printf(bio_err, "-decrypt time decryption instead of encryption (only EVP).\n"); + BIO_printf(bio_err, "-mr produce machine readable output.\n"); ++#ifndef _WIN32 + BIO_printf(bio_err, "-multi n run n benchmarks in parallel.\n"); ++#endif + goto end; + } + argc--; +@@ -929,8 +944,10 @@ speed_main(int argc, char **argv) + j++; + } + ++#ifndef _WIN32 + if (multi && do_multi(multi)) + goto show_res; ++#endif + + if (j == 0) { + for (i = 0; i < ALGOR_NUM; i++) { +@@ -1771,7 +1788,9 @@ speed_main(int argc, char **argv) + ecdh_doit[j] = 0; + } + } ++#ifndef _WIN32 + show_res: ++#endif + if (!mr) { + fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_VERSION)); + fprintf(stdout, "%s\n", SSLeay_version(SSLEAY_BUILT_ON)); +@@ -1944,11 +1963,15 @@ pkey_print_message(const char *str, const char *str2, + static void + print_result(int alg, int run_no, int count, double time_used) + { ++#ifdef _WIN32 ++ speed_alarm_free(run); ++#endif + BIO_printf(bio_err, mr ? "+R:%d:%s:%f\n" + : "%d %s's in %.2fs\n", count, names[alg], time_used); + results[alg][run_no] = ((double) count) / time_used * lengths[run_no]; + } + ++#ifndef _WIN32 + static char * + sstrsep(char **string, const char *delim) + { +@@ -2155,4 +2178,5 @@ do_multi(int multi) + free(fds); + return 1; + } ++#endif + #endif