diff options
-rw-r--r-- | src/curl/curl.c | 4 | ||||
-rw-r--r-- | src/util/benchmark.c | 144 | ||||
-rw-r--r-- | src/util/benchmark.h | 55 | ||||
-rw-r--r-- | src/util/crypto_ecc.c | 1 |
4 files changed, 203 insertions, 1 deletions
diff --git a/src/curl/curl.c b/src/curl/curl.c index a0a027995..0d9342b60 100644 --- a/src/curl/curl.c +++ b/src/curl/curl.c | |||
@@ -30,6 +30,10 @@ | |||
30 | #include <jansson.h> | 30 | #include <jansson.h> |
31 | #include "gnunet_curl_lib.h" | 31 | #include "gnunet_curl_lib.h" |
32 | 32 | ||
33 | #if ENABLE_BENCHMARK | ||
34 | #include "../util/benchmark.h" | ||
35 | #endif | ||
36 | |||
33 | 37 | ||
34 | /** | 38 | /** |
35 | * Log error related to CURL operations. | 39 | * Log error related to CURL operations. |
diff --git a/src/util/benchmark.c b/src/util/benchmark.c new file mode 100644 index 000000000..4a0c9b7c8 --- /dev/null +++ b/src/util/benchmark.c | |||
@@ -0,0 +1,144 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file util/benchmark.c | ||
21 | * @brief benchmarking for various operations | ||
22 | * @author Florian Dold <flo@dold.me> | ||
23 | */ | ||
24 | |||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "benchmark.h" | ||
28 | #include <pthread.h> | ||
29 | #include <sys/syscall.h> | ||
30 | |||
31 | /** | ||
32 | * Thread-local storage key for the benchmark data. | ||
33 | */ | ||
34 | static pthread_key_t key; | ||
35 | |||
36 | /** | ||
37 | * One-time initialization marker for key. | ||
38 | */ | ||
39 | static pthread_once_t key_once = PTHREAD_ONCE_INIT; | ||
40 | |||
41 | |||
42 | /** | ||
43 | * Write benchmark data to a file. | ||
44 | * | ||
45 | * @param bd the benchmark data | ||
46 | */ | ||
47 | static void | ||
48 | write_benchmark_data (struct BenchmarkData *bd) | ||
49 | { | ||
50 | struct GNUNET_DISK_FileHandle *fh; | ||
51 | pid_t pid = getpid (); | ||
52 | pid_t tid = syscall (SYS_gettid); | ||
53 | char *s; | ||
54 | |||
55 | GNUNET_asprintf (&s, "gnunet-benchmark-%llu-%llu.txt", | ||
56 | (unsigned long long) pid, | ||
57 | (unsigned long long) tid); | ||
58 | |||
59 | fh = GNUNET_DISK_file_open (s, | ||
60 | (GNUNET_DISK_OPEN_WRITE | | ||
61 | GNUNET_DISK_OPEN_TRUNCATE | | ||
62 | GNUNET_DISK_OPEN_CREATE), | ||
63 | (GNUNET_DISK_PERM_USER_READ | | ||
64 | GNUNET_DISK_PERM_USER_WRITE)); | ||
65 | GNUNET_assert (NULL != fh); | ||
66 | GNUNET_free (s); | ||
67 | |||
68 | GNUNET_asprintf (&s, "eddsa_sign_count %llu", | ||
69 | (unsigned long long) bd->eddsa_sign_count); | ||
70 | GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s, strlen (s))); | ||
71 | GNUNET_free (s); | ||
72 | |||
73 | GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh)); | ||
74 | } | ||
75 | |||
76 | |||
77 | /** | ||
78 | * Called when the main thread exits and benchmark data for it was created. | ||
79 | */ | ||
80 | static void | ||
81 | main_thread_destructor () | ||
82 | { | ||
83 | struct BenchmarkData *bd; | ||
84 | |||
85 | bd = pthread_getspecific (key); | ||
86 | if (NULL != bd) | ||
87 | write_benchmark_data (bd); | ||
88 | } | ||
89 | |||
90 | |||
91 | /** | ||
92 | * Called when a thread exits and benchmark data for it was created. | ||
93 | * | ||
94 | * @param cls closure | ||
95 | */ | ||
96 | static void | ||
97 | thread_destructor (void *cls) | ||
98 | { | ||
99 | struct BenchmarkData *bd = cls; | ||
100 | |||
101 | // main thread will be handled by atexit | ||
102 | if (getpid () == (pid_t) syscall (SYS_gettid)) | ||
103 | return; | ||
104 | |||
105 | GNUNET_assert (NULL != bd); | ||
106 | |||
107 | } | ||
108 | |||
109 | |||
110 | /** | ||
111 | * Initialize the thread-local variable key for benchmark data. | ||
112 | */ | ||
113 | static void | ||
114 | make_key() | ||
115 | { | ||
116 | (void) pthread_key_create (&key, &thread_destructor); | ||
117 | } | ||
118 | |||
119 | |||
120 | /** | ||
121 | * Acquire the benchmark data for the current thread, allocate if necessary. | ||
122 | * Installs handler to collect the benchmark data on thread termination. | ||
123 | * | ||
124 | * @return benchmark data for the current thread | ||
125 | */ | ||
126 | struct BenchmarkData * | ||
127 | get_benchmark_data (void) | ||
128 | { | ||
129 | struct BenchmarkData *bd; | ||
130 | |||
131 | (void) pthread_once (&key_once, &make_key); | ||
132 | |||
133 | if (NULL == (bd = pthread_getspecific (key))) | ||
134 | { | ||
135 | bd = GNUNET_new (struct BenchmarkData); | ||
136 | (void) pthread_setspecific (key, bd); | ||
137 | if (getpid () == (pid_t) syscall (SYS_gettid)) | ||
138 | { | ||
139 | // We're the main thread! | ||
140 | atexit (main_thread_destructor); | ||
141 | } | ||
142 | } | ||
143 | return bd; | ||
144 | } | ||
diff --git a/src/util/benchmark.h b/src/util/benchmark.h new file mode 100644 index 000000000..eec9c9c8a --- /dev/null +++ b/src/util/benchmark.h | |||
@@ -0,0 +1,55 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2018 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file util/benchmark.h | ||
21 | * @brief benchmarking for various operations | ||
22 | * @author Florian Dold <flo@dold.me> | ||
23 | */ | ||
24 | |||
25 | #ifndef BENCHMARK_H_ | ||
26 | #define BENCHMARK_H_ | ||
27 | |||
28 | #include "gnunet_time_lib.h" | ||
29 | |||
30 | /** | ||
31 | * Thread-local struct for benchmarking data. | ||
32 | */ | ||
33 | struct BenchmarkData { | ||
34 | /** | ||
35 | * Number of eddsa_sign operations. | ||
36 | */ | ||
37 | uint64_t eddsa_sign_count; | ||
38 | |||
39 | /** | ||
40 | * Time spent in eddsa_sign. | ||
41 | */ | ||
42 | struct GNUNET_TIME_Relative eddsa_sign_time; | ||
43 | }; | ||
44 | |||
45 | |||
46 | /** | ||
47 | * Acquire the benchmark data for the current thread, allocate if necessary. | ||
48 | * Installs handler to collect the benchmark data on thread termination. | ||
49 | * | ||
50 | * @return benchmark data for the current thread | ||
51 | */ | ||
52 | struct BenchmarkData * | ||
53 | get_benchmark_data (void); | ||
54 | |||
55 | #endif /* BENCHMARK_H_ */ | ||
diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c index ed20fa2c8..ca2aa40ad 100644 --- a/src/util/crypto_ecc.c +++ b/src/util/crypto_ecc.c | |||
@@ -877,7 +877,6 @@ GNUNET_CRYPTO_eddsa_sign (const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, | |||
877 | #if ENABLE_BENCHMARK | 877 | #if ENABLE_BENCHMARK |
878 | struct BenchmarkData *bd = get_benchmark_data (); | 878 | struct BenchmarkData *bd = get_benchmark_data (); |
879 | bd->eddsa_sign_count++; | 879 | bd->eddsa_sign_count++; |
880 | printf("crypto eddsa sign\n"); | ||
881 | #endif | 880 | #endif |
882 | 881 | ||
883 | priv_sexp = decode_private_eddsa_key (priv); | 882 | priv_sexp = decode_private_eddsa_key (priv); |