benchmark collection awk scripts
[oweals/gnunet.git] / src / util / benchmark.c
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 *benchmark_dir;
54   char *s;
55
56   benchmark_dir = getenv ("GNUNET_BENCHMARK_DIR");
57
58   if (NULL == benchmark_dir)
59     return;
60
61   if (GNUNET_OK != GNUNET_DISK_directory_create (benchmark_dir))
62   {
63     GNUNET_break (0);
64     return;
65   }
66
67   GNUNET_asprintf (&s, "%s/gnunet-benchmark-ops-%llu-%llu.txt",
68                    benchmark_dir,
69                    (unsigned long long) pid,
70                    (unsigned long long) tid);
71
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);
79   GNUNET_free (s);
80
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))); \
86   GNUNET_free (s); \
87 } while (0)
88
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);
114
115 #undef WRITE_BENCHMARK_OP
116
117   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
118
119   GNUNET_asprintf (&s, "%s/gnunet-benchmark-urls-%llu-%llu.txt",
120                    benchmark_dir,
121                    (unsigned long long) pid,
122                    (unsigned long long) tid);
123
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);
131   GNUNET_free (s);
132
133   for (unsigned int i = 0; i < bd->urd_len; i++)
134   {
135     struct UrlRequestData *urd = &bd->urd[i];
136     GNUNET_asprintf (&s, "url %s status %u count %llu time_us %llu\n",
137                      urd->request_url,
138                      urd->status,
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)));
142     GNUNET_free (s);
143   }
144
145   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
146 }
147
148
149 /**
150  * Called when the main thread exits and benchmark data for it was created.
151  */
152 static void
153 main_thread_destructor ()
154 {
155   struct BenchmarkData *bd;
156
157   bd = pthread_getspecific (key);
158   if (NULL != bd)
159     write_benchmark_data (bd);
160 }
161
162
163 /**
164  * Called when a thread exits and benchmark data for it was created.
165  *
166  * @param cls closure
167  */
168 static void
169 thread_destructor (void *cls)
170 {
171   struct BenchmarkData *bd = cls;
172
173   // main thread will be handled by atexit
174   if (getpid () == (pid_t) syscall (SYS_gettid))
175     return;
176   
177   GNUNET_assert (NULL != bd);
178 }
179
180
181 /**
182  * Initialize the thread-local variable key for benchmark data.
183  */
184 static void
185 make_key ()
186 {
187   (void) pthread_key_create (&key, &thread_destructor);
188 }
189
190
191 /**
192  * Acquire the benchmark data for the current thread, allocate if necessary.
193  * Installs handler to collect the benchmark data on thread termination.
194  *
195  * @return benchmark data for the current thread
196  */
197 struct BenchmarkData *
198 get_benchmark_data (void)
199 {
200   struct BenchmarkData *bd;
201
202   (void) pthread_once (&key_once, &make_key);
203
204   if (NULL == (bd = pthread_getspecific (key)))
205   {
206     bd = GNUNET_new (struct BenchmarkData);
207     (void) pthread_setspecific (key, bd);
208     if (getpid () == (pid_t) syscall (SYS_gettid))
209     {
210       // We're the main thread!
211       atexit (main_thread_destructor);
212     }
213   }
214   return bd;
215 }
216
217
218 /**
219  * Get benchmark data for a URL.  If the URL is too long, it's truncated
220  * before looking up the correspoding benchmark data.
221  *
222  * Statistics are bucketed by URL and status code.
223  *
224  * @param url url to get request data for
225  * @param status http status code
226  */
227 struct UrlRequestData *
228 get_url_benchmark_data (char *url, unsigned int status)
229 {
230   char trunc[MAX_BENCHMARK_URL_LEN];
231   struct BenchmarkData *bd;
232
233   if (NULL == url)
234   {
235     /* Should not happen unless curl barfs */
236     GNUNET_break (0);
237     url = "<empty>";
238   }
239
240   memcpy (trunc, url, MAX_BENCHMARK_URL_LEN);
241   trunc[MAX_BENCHMARK_URL_LEN - 1] = 0;
242
243   /* We're not interested in what's after the query string */
244   for (size_t i = 0; i < strlen (trunc); i++)
245   {
246     if (trunc[i] == '?')
247     {
248       trunc[i] = 0;
249       break;
250     }
251   }
252
253   bd = get_benchmark_data ();
254
255   GNUNET_assert (bd->urd_len <= bd->urd_capacity);
256
257   for (unsigned int i = 0; i < bd->urd_len; i++)
258   {
259     if ( (0 == strcmp (trunc, bd->urd[i].request_url)) &&
260          (bd->urd[i].status == status) )
261       return &bd->urd[i];
262   }
263
264   {
265     struct UrlRequestData urd = { 0 };
266
267     memcpy (&urd.request_url, trunc, MAX_BENCHMARK_URL_LEN);
268     urd.status = status;
269
270     if (bd->urd_len == bd->urd_capacity)
271     {
272       bd->urd_capacity = 2 * (bd->urd_capacity + 1);
273       bd->urd = GNUNET_realloc (bd->urd, bd->urd_capacity * sizeof (struct UrlRequestData));
274     }
275
276     bd->urd[bd->urd_len++] = urd;
277     return &bd->urd[bd->urd_len - 1];
278   }
279 }