421cafbef116f6defcfd6f521fb778ccfb2eca2b
[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 *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   GNUNET_asprintf (&s, "gnunet-benchmark-urls-%llu-%llu.txt",
76                    (unsigned long long) pid,
77                    (unsigned long long) tid);
78
79   fh = GNUNET_DISK_file_open (s,
80                               (GNUNET_DISK_OPEN_WRITE |
81                                GNUNET_DISK_OPEN_TRUNCATE |
82                                GNUNET_DISK_OPEN_CREATE),
83                               (GNUNET_DISK_PERM_USER_READ |
84                                GNUNET_DISK_PERM_USER_WRITE));
85   GNUNET_assert (NULL != fh);
86   GNUNET_free (s);
87
88   for (unsigned int i = 0; i < bd->urd_len; i++)
89   {
90     struct UrlRequestData *urd = &bd->urd[i];
91     GNUNET_asprintf (&s, "url %s count %lld time_us %lld\n",
92                      urd->request_url,
93                      (unsigned long long) urd->count,
94                      (unsigned long long) urd->time.rel_value_us);
95     GNUNET_assert (GNUNET_SYSERR != GNUNET_DISK_file_write_blocking (fh, s, strlen (s)));
96     GNUNET_free (s);
97   }
98
99
100   GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
101 }
102
103
104 /**
105  * Called when the main thread exits and benchmark data for it was created.
106  */
107 static void
108 main_thread_destructor ()
109 {
110   struct BenchmarkData *bd;
111
112   bd = pthread_getspecific (key);
113   if (NULL != bd)
114     write_benchmark_data (bd);
115 }
116
117
118 /**
119  * Called when a thread exits and benchmark data for it was created.
120  *
121  * @param cls closure
122  */
123 static void
124 thread_destructor (void *cls)
125 {
126   struct BenchmarkData *bd = cls;
127
128   // main thread will be handled by atexit
129   if (getpid () == (pid_t) syscall (SYS_gettid))
130     return;
131   
132   GNUNET_assert (NULL != bd);
133
134 }
135
136
137 /**
138  * Initialize the thread-local variable key for benchmark data.
139  */
140 static void
141 make_key()
142 {
143   (void) pthread_key_create (&key, &thread_destructor);
144 }
145
146
147 /**
148  * Acquire the benchmark data for the current thread, allocate if necessary.
149  * Installs handler to collect the benchmark data on thread termination.
150  *
151  * @return benchmark data for the current thread
152  */
153 struct BenchmarkData *
154 get_benchmark_data (void)
155 {
156   struct BenchmarkData *bd;
157
158   (void) pthread_once (&key_once, &make_key);
159
160   if (NULL == (bd = pthread_getspecific (key)))
161   {
162     bd = GNUNET_new (struct BenchmarkData);
163     (void) pthread_setspecific (key, bd);
164     if (getpid () == (pid_t) syscall (SYS_gettid))
165     {
166       // We're the main thread!
167       atexit (main_thread_destructor);
168     }
169   }
170   return bd;
171 }
172
173
174 /**
175  * Get benchmark data for a URL.  If the URL is too long, it's truncated
176  * before looking up the correspoding benchmark data.
177  *
178  * @param url url to get request data for
179  */
180 struct UrlRequestData *
181 get_url_benchmark_data (char *url)
182 {
183   char trunc[MAX_BENCHMARK_URL_LEN];
184   struct BenchmarkData *bd;
185
186   memcpy (trunc, url, MAX_BENCHMARK_URL_LEN);
187   trunc[MAX_BENCHMARK_URL_LEN - 1] = 0;
188
189   bd = get_benchmark_data ();
190
191   for (unsigned int i = 0; i < bd->urd_len; i++)
192   {
193     if (0 == strcmp (trunc, bd->urd[i].request_url))
194       return &bd->urd[i];
195   }
196
197   {
198     struct UrlRequestData urd = { 0 };
199
200     memcpy (&urd.request_url, trunc, MAX_BENCHMARK_URL_LEN);
201
202     if (bd->urd_len == bd->urd_capacity)
203     {
204       bd->urd_capacity = 2 * (bd->urd_capacity + 1);
205       bd->urd = GNUNET_realloc (bd->urd, bd->urd_capacity * sizeof (struct UrlRequestData));
206     }
207
208     bd->urd[bd->urd_len++] = urd;
209     return &bd->urd[bd->urd_len - 1];
210   }
211 }