2 This file is part of GNUnet.
3 Copyright (C) 2018 GNUnet e.V.
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.
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.
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/>.
20 * @file util/benchmark.c
21 * @brief benchmarking for various operations
22 * @author Florian Dold <flo@dold.me>
26 #include "gnunet_util_lib.h"
27 #include "benchmark.h"
29 #include <sys/syscall.h>
32 * Thread-local storage key for the benchmark data.
34 static pthread_key_t key;
37 * One-time initialization marker for key.
39 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
43 * Write benchmark data to a file.
45 * @param bd the benchmark data
48 write_benchmark_data (struct BenchmarkData *bd)
50 struct GNUNET_DISK_FileHandle *fh;
51 pid_t pid = getpid ();
52 pid_t tid = syscall (SYS_gettid);
56 benchmark_dir = getenv ("GNUNET_BENCHMARK_DIR");
58 if (NULL == benchmark_dir)
61 if (GNUNET_OK != GNUNET_DISK_directory_create (benchmark_dir))
67 GNUNET_asprintf (&s, "%s/gnunet-benchmark-ops-%llu-%llu.txt",
69 (unsigned long long) pid,
70 (unsigned long long) tid);
72 fh = GNUNET_DISK_file_open (s,
73 (GNUNET_DISK_OPEN_WRITE |
74 GNUNET_DISK_OPEN_TRUNCATE |
75 GNUNET_DISK_OPEN_CREATE),
76 (GNUNET_DISK_PERM_USER_READ |
77 GNUNET_DISK_PERM_USER_WRITE));
78 GNUNET_assert (NULL != fh);
81 #define WRITE_BENCHMARK_OP(opname) do { \
82 GNUNET_asprintf (&s, "op " #opname " count %llu time_us %llu\n", \
83 (unsigned long long) bd->opname##_count, \
84 (unsigned long long) bd->opname##_time.rel_value_us); \
85 GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s, strlen (s))); \
89 WRITE_BENCHMARK_OP (ecc_ecdh);
90 WRITE_BENCHMARK_OP (ecdh_eddsa);
91 WRITE_BENCHMARK_OP (ecdhe_key_create);
92 WRITE_BENCHMARK_OP (ecdhe_key_get_public);
93 WRITE_BENCHMARK_OP (ecdsa_ecdh);
94 WRITE_BENCHMARK_OP (ecdsa_key_create);
95 WRITE_BENCHMARK_OP (ecdsa_key_get_public);
96 WRITE_BENCHMARK_OP (ecdsa_sign);
97 WRITE_BENCHMARK_OP (ecdsa_verify);
98 WRITE_BENCHMARK_OP (eddsa_ecdh);
99 WRITE_BENCHMARK_OP (eddsa_key_create);
100 WRITE_BENCHMARK_OP (eddsa_key_get_public);
101 WRITE_BENCHMARK_OP (eddsa_sign);
102 WRITE_BENCHMARK_OP (eddsa_verify);
103 WRITE_BENCHMARK_OP (hash);
104 WRITE_BENCHMARK_OP (hash_context_finish);
105 WRITE_BENCHMARK_OP (hash_context_read);
106 WRITE_BENCHMARK_OP (hash_context_start);
107 WRITE_BENCHMARK_OP (hkdf);
108 WRITE_BENCHMARK_OP (rsa_blind);
109 WRITE_BENCHMARK_OP (rsa_private_key_create);
110 WRITE_BENCHMARK_OP (rsa_private_key_get_public);
111 WRITE_BENCHMARK_OP (rsa_sign_blinded);
112 WRITE_BENCHMARK_OP (rsa_unblind);
113 WRITE_BENCHMARK_OP (rsa_verify);
115 #undef WRITE_BENCHMARK_OP
117 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
119 GNUNET_asprintf (&s, "%s/gnunet-benchmark-urls-%llu-%llu.txt",
121 (unsigned long long) pid,
122 (unsigned long long) tid);
124 fh = GNUNET_DISK_file_open (s,
125 (GNUNET_DISK_OPEN_WRITE |
126 GNUNET_DISK_OPEN_TRUNCATE |
127 GNUNET_DISK_OPEN_CREATE),
128 (GNUNET_DISK_PERM_USER_READ |
129 GNUNET_DISK_PERM_USER_WRITE));
130 GNUNET_assert (NULL != fh);
133 for (unsigned int i = 0; i < bd->urd_len; i++)
135 struct UrlRequestData *urd = &bd->urd[i];
136 GNUNET_asprintf (&s, "url %s status %u count %llu time_us %llu\n",
139 (unsigned long long) urd->count,
140 (unsigned long long) urd->time.rel_value_us);
141 GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s, strlen (s)));
145 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
150 * Called when the main thread exits and benchmark data for it was created.
153 main_thread_destructor ()
155 struct BenchmarkData *bd;
157 bd = pthread_getspecific (key);
159 write_benchmark_data (bd);
164 * Called when a thread exits and benchmark data for it was created.
169 thread_destructor (void *cls)
171 struct BenchmarkData *bd = cls;
173 // main thread will be handled by atexit
174 if (getpid () == (pid_t) syscall (SYS_gettid))
177 GNUNET_assert (NULL != bd);
182 * Initialize the thread-local variable key for benchmark data.
187 (void) pthread_key_create (&key, &thread_destructor);
192 * Acquire the benchmark data for the current thread, allocate if necessary.
193 * Installs handler to collect the benchmark data on thread termination.
195 * @return benchmark data for the current thread
197 struct BenchmarkData *
198 get_benchmark_data (void)
200 struct BenchmarkData *bd;
202 (void) pthread_once (&key_once, &make_key);
204 if (NULL == (bd = pthread_getspecific (key)))
206 bd = GNUNET_new (struct BenchmarkData);
207 (void) pthread_setspecific (key, bd);
208 if (getpid () == (pid_t) syscall (SYS_gettid))
210 // We're the main thread!
211 atexit (main_thread_destructor);
219 * Get benchmark data for a URL. If the URL is too long, it's truncated
220 * before looking up the correspoding benchmark data.
222 * Statistics are bucketed by URL and status code.
224 * @param url url to get request data for
225 * @param status http status code
227 struct UrlRequestData *
228 get_url_benchmark_data (char *url, unsigned int status)
230 char trunc[MAX_BENCHMARK_URL_LEN];
231 struct BenchmarkData *bd;
235 /* Should not happen unless curl barfs */
240 memcpy (trunc, url, MAX_BENCHMARK_URL_LEN);
241 trunc[MAX_BENCHMARK_URL_LEN - 1] = 0;
243 /* We're not interested in what's after the query string */
244 for (size_t i = 0; i < strlen (trunc); i++)
253 bd = get_benchmark_data ();
255 GNUNET_assert (bd->urd_len <= bd->urd_capacity);
257 for (unsigned int i = 0; i < bd->urd_len; i++)
259 if ( (0 == strcmp (trunc, bd->urd[i].request_url)) &&
260 (bd->urd[i].status == status) )
265 struct UrlRequestData urd = { 0 };
267 memcpy (&urd.request_url, trunc, MAX_BENCHMARK_URL_LEN);
270 if (bd->urd_len == bd->urd_capacity)
272 bd->urd_capacity = 2 * (bd->urd_capacity + 1);
273 bd->urd = GNUNET_realloc (bd->urd, bd->urd_capacity * sizeof (struct UrlRequestData));
276 bd->urd[bd->urd_len++] = urd;
277 return &bd->urd[bd->urd_len - 1];