diff options
author | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2011-04-07 23:32:39 +0200 |
---|---|---|
committer | Nikos Mavrogiannopoulos <nmav@gnutls.org> | 2011-04-07 23:32:39 +0200 |
commit | 78afba3179df3ea699a2ab98dd3eafce64404877 (patch) | |
tree | fdb56ca130ab1ab821c0bb31fcbc3c52cdf561e9 /src/benchmark.c | |
parent | 93886329b827485e27e046eae2a8b35531b76e6c (diff) | |
download | gnutls-78afba3179df3ea699a2ab98dd3eafce64404877.tar.gz |
Win32 changes for benchmark. Patch by LRN.
Diffstat (limited to 'src/benchmark.c')
-rw-r--r-- | src/benchmark.c | 65 |
1 files changed, 63 insertions, 2 deletions
diff --git a/src/benchmark.c b/src/benchmark.c index 1bc0dd8687..00d572a946 100644 --- a/src/benchmark.c +++ b/src/benchmark.c @@ -35,11 +35,51 @@ static unsigned char data[64 * 1024]; static int must_finish = 0; +#if !defined(_WIN32) static void alarm_handler (int signo) { must_finish = 1; } +#else +#include <windows.h> +DWORD WINAPI +alarm_handler (LPVOID lpParameter) +{ + HANDLE wtimer = *((HANDLE *) lpParameter); + WaitForSingleObject (wtimer, INFINITE); + must_finish = 1; + return 0; +} + +#define W32_ALARM_VARIABLES HANDLE wtimer = NULL, wthread = NULL; \ + LARGE_INTEGER alarm_timeout = { 0 , 0 } +#define W32_ALARM_TRIGGER(timeout, leave) { \ + wtimer = CreateWaitableTimer (NULL, TRUE, NULL); \ + if (wtimer == NULL) \ + { \ + fprintf (stderr, "error: CreateWaitableTimer %u\n", GetLastError ()); \ + leave; \ + } \ + wthread = CreateThread (NULL, 0, alarm_handler, &wtimer, 0, NULL); \ + if (wthread == NULL) \ + { \ + fprintf (stderr, "error: CreateThread %u\n", GetLastError ()); \ + leave; \ + } \ + alarm_timeout.QuadPart = timeout * 10000000; \ + if (SetWaitableTimer (wtimer, &alarm_timeout, 0, NULL, NULL, FALSE) == 0) \ + { \ + fprintf (stderr, "error: SetWaitableTimer %u\n", GetLastError ()); \ + leave; \ + } \ + } +#define W32_ALARM_CLEANUP { \ + if (wtimer != NULL) \ + CloseHandle (wtimer); \ + if (wthread != NULL) \ + CloseHandle (wthread);} +#endif static void tls_log_func (int level, const char *str) @@ -97,6 +137,9 @@ cipher_mac_bench (int algo, int mac_algo, int size) int keysize = gnutls_cipher_get_key_size (algo); char metric[16]; int step = size*1024; +#if defined(_WIN32) + W32_ALARM_VARIABLES; +#endif _key = malloc (keysize); if (_key == NULL) @@ -119,7 +162,11 @@ cipher_mac_bench (int algo, int mac_algo, int size) fflush (stdout); must_finish = 0; +#if !defined(_WIN32) alarm (5); +#else + W32_ALARM_TRIGGER(5, goto leave); +#endif gettime (&start); @@ -182,6 +229,9 @@ cipher_bench (int algo, int size, int aead) int keysize = gnutls_cipher_get_key_size (algo); char metric[16]; int step = size*1024; +#if defined(_WIN32) + W32_ALARM_VARIABLES; +#endif _key = malloc (keysize); if (_key == NULL) @@ -241,7 +291,9 @@ cipher_bench (int algo, int size, int aead) leave: free (_key); free (_iv); - +#if defined(_WIN32) + W32_ALARM_CLEANUP; +#endif } static void @@ -265,7 +317,11 @@ mac_bench (int algo, int size) fflush (stdout); must_finish = 0; +#if !defined(_WIN32) alarm (5); +#else + W32_ALARM_TRIGGER(5, goto leave); +#endif gettime (&start); @@ -287,7 +343,10 @@ mac_bench (int algo, int size) printf ("Hashed %.2f %s in %.2f secs: ", ddata, metric, secs); printf ("%.2f %s/sec\n", dspeed, metric); - +#if defined(_WIN32) +leave: + W32_ALARM_CLEANUP; +#endif free (_key); } @@ -299,7 +358,9 @@ main (int argc, char **argv) if (argc > 1) debug_level = 2; +#if !defined(_WIN32) signal (SIGALRM, alarm_handler); +#endif gnutls_global_set_log_function (tls_log_func); gnutls_global_set_log_level (debug_level); |