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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file util/benchmark.c
23 * @brief benchmarking for various operations
24 * @author Florian Dold <flo@dold.me>
28 #include "gnunet_util_lib.h"
29 #include "benchmark.h"
31 #include <sys/syscall.h>
34 * Thread-local storage key for the benchmark data.
36 static pthread_key_t key;
39 * One-time initialization marker for key.
41 static pthread_once_t key_once = PTHREAD_ONCE_INIT;
45 * Write benchmark data to a file.
47 * @param bd the benchmark data
50 write_benchmark_data (struct BenchmarkData *bd)
52 struct GNUNET_DISK_FileHandle *fh;
53 pid_t pid = getpid ();
54 pid_t tid = syscall (SYS_gettid);
58 benchmark_dir = getenv ("GNUNET_BENCHMARK_DIR");
60 if (NULL == benchmark_dir)
63 if (GNUNET_OK != GNUNET_DISK_directory_create (benchmark_dir))
69 GNUNET_asprintf (&s, "%s/gnunet-benchmark-ops-%s-%llu-%llu.txt",
71 (pid == tid) ? "main" : "thread",
72 (unsigned long long) pid,
73 (unsigned long long) tid);
75 fh = GNUNET_DISK_file_open (s,
76 (GNUNET_DISK_OPEN_WRITE
77 | GNUNET_DISK_OPEN_TRUNCATE
78 | GNUNET_DISK_OPEN_CREATE),
79 (GNUNET_DISK_PERM_USER_READ
80 | GNUNET_DISK_PERM_USER_WRITE));
81 GNUNET_assert (NULL != fh);
84 #define WRITE_BENCHMARK_OP(opname) do { \
85 GNUNET_asprintf (&s, "op " #opname " count %llu time_us %llu\n", \
86 (unsigned long long) bd->opname ## _count, \
87 (unsigned long long) bd->opname ## _time.rel_value_us); \
88 GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s, \
94 WRITE_BENCHMARK_OP (ecc_ecdh);
95 WRITE_BENCHMARK_OP (ecdh_eddsa);
96 WRITE_BENCHMARK_OP (ecdhe_key_create);
97 WRITE_BENCHMARK_OP (ecdhe_key_get_public);
98 WRITE_BENCHMARK_OP (ecdsa_ecdh);
99 WRITE_BENCHMARK_OP (ecdsa_key_create);
100 WRITE_BENCHMARK_OP (ecdsa_key_get_public);
101 WRITE_BENCHMARK_OP (ecdsa_sign);
102 WRITE_BENCHMARK_OP (ecdsa_verify);
103 WRITE_BENCHMARK_OP (eddsa_ecdh);
104 WRITE_BENCHMARK_OP (eddsa_key_create);
105 WRITE_BENCHMARK_OP (eddsa_key_get_public);
106 WRITE_BENCHMARK_OP (eddsa_sign);
107 WRITE_BENCHMARK_OP (eddsa_verify);
108 WRITE_BENCHMARK_OP (hash);
109 WRITE_BENCHMARK_OP (hash_context_finish);
110 WRITE_BENCHMARK_OP (hash_context_read);
111 WRITE_BENCHMARK_OP (hash_context_start);
112 WRITE_BENCHMARK_OP (hkdf);
113 WRITE_BENCHMARK_OP (rsa_blind);
114 WRITE_BENCHMARK_OP (rsa_private_key_create);
115 WRITE_BENCHMARK_OP (rsa_private_key_get_public);
116 WRITE_BENCHMARK_OP (rsa_sign_blinded);
117 WRITE_BENCHMARK_OP (rsa_unblind);
118 WRITE_BENCHMARK_OP (rsa_verify);
120 #undef WRITE_BENCHMARK_OP
122 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
124 GNUNET_asprintf (&s, "%s/gnunet-benchmark-urls-%s-%llu-%llu.txt",
126 (pid == tid) ? "main" : "thread",
127 (unsigned long long) pid,
128 (unsigned long long) tid);
130 fh = GNUNET_DISK_file_open (s,
131 (GNUNET_DISK_OPEN_WRITE
132 | GNUNET_DISK_OPEN_TRUNCATE
133 | GNUNET_DISK_OPEN_CREATE),
134 (GNUNET_DISK_PERM_USER_READ
135 | GNUNET_DISK_PERM_USER_WRITE));
136 GNUNET_assert (NULL != fh);
139 for (unsigned int i = 0; i < bd->urd_len; i++)
141 struct UrlRequestData *urd = &bd->urd[i];
143 "url %s status %u count %llu time_us %llu time_us_max %llu bytes_sent %llu bytes_received %llu\n",
146 (unsigned long long) urd->count,
147 (unsigned long long) urd->time.rel_value_us,
148 (unsigned long long) urd->time_max.rel_value_us,
149 (unsigned long long) urd->bytes_sent,
150 (unsigned long long) urd->bytes_received);
151 GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s,
157 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
162 * Called when the main thread exits and benchmark data for it was created.
165 main_thread_destructor ()
167 struct BenchmarkData *bd;
169 bd = pthread_getspecific (key);
171 write_benchmark_data (bd);
176 * Called when a thread exits and benchmark data for it was created.
181 thread_destructor (void *cls)
183 struct BenchmarkData *bd = cls;
185 // main thread will be handled by atexit
186 if (getpid () == (pid_t) syscall (SYS_gettid))
189 GNUNET_assert (NULL != bd);
190 write_benchmark_data (bd);
195 * Initialize the thread-local variable key for benchmark data.
200 (void) pthread_key_create (&key, &thread_destructor);
205 * Acquire the benchmark data for the current thread, allocate if necessary.
206 * Installs handler to collect the benchmark data on thread termination.
208 * @return benchmark data for the current thread
210 struct BenchmarkData *
211 get_benchmark_data (void)
213 struct BenchmarkData *bd;
215 (void) pthread_once (&key_once, &make_key);
217 if (NULL == (bd = pthread_getspecific (key)))
219 bd = GNUNET_new (struct BenchmarkData);
220 (void) pthread_setspecific (key, bd);
221 if (getpid () == (pid_t) syscall (SYS_gettid))
223 // We're the main thread!
224 atexit (main_thread_destructor);
232 * Get benchmark data for a URL. If the URL is too long, it's truncated
233 * before looking up the correspoding benchmark data.
235 * Statistics are bucketed by URL and status code.
237 * @param url url to get request data for
238 * @param status http status code
240 struct UrlRequestData *
241 get_url_benchmark_data (char *url, unsigned int status)
243 char trunc[MAX_BENCHMARK_URL_LEN];
244 struct BenchmarkData *bd;
248 /* Should not happen unless curl barfs */
253 memcpy (trunc, url, MAX_BENCHMARK_URL_LEN);
254 trunc[MAX_BENCHMARK_URL_LEN - 1] = 0;
256 /* We're not interested in what's after the query string */
257 for (size_t i = 0; i < strlen (trunc); i++)
266 bd = get_benchmark_data ();
268 GNUNET_assert (bd->urd_len <= bd->urd_capacity);
270 for (unsigned int i = 0; i < bd->urd_len; i++)
272 if ((0 == strcmp (trunc, bd->urd[i].request_url)) &&
273 (bd->urd[i].status == status))
278 struct UrlRequestData urd = { 0 };
280 memcpy (&urd.request_url, trunc, MAX_BENCHMARK_URL_LEN);
283 if (bd->urd_len == bd->urd_capacity)
285 bd->urd_capacity = 2 * (bd->urd_capacity + 1);
286 bd->urd = GNUNET_realloc (bd->urd, bd->urd_capacity * sizeof(struct
290 bd->urd[bd->urd_len++] = urd;
291 return &bd->urd[bd->urd_len - 1];