fix #4546
[oweals/gnunet.git] / src / datastore / perf_plugin_datastore.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011 GNUnet e.V.
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /*
21  * @file perf_plugin_datastore.c
22  * @brief Profile database plugin directly, focusing on iterators.
23  * @author Christian Grothoff
24  */
25
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_plugin.h"
30 #include "gnunet_testing_lib.h"
31 #include <gauger.h>
32
33 /**
34  * Target datastore size (in bytes).  Realistic sizes are
35  * more like 16 GB (not the default of 16 MB); however,
36  * those take too long to run them in the usual "make check"
37  * sequence.  Hence the value used for shipping is tiny.
38  */
39 #define MAX_SIZE 1024LL * 1024 * 16 * 1
40
41 #define ITERATIONS 2
42
43 /**
44  * Number of put operations equivalent to 1/10th of MAX_SIZE
45  */
46 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
47
48 static char category[256];
49
50 static unsigned int hits[PUT_10 / 8 + 1];
51
52 static unsigned long long stored_bytes;
53
54 static unsigned long long stored_entries;
55
56 static unsigned long long stored_ops;
57
58 static const char *plugin_name;
59
60 static int ok;
61
62 enum RunPhase
63 {
64   RP_ERROR = 0,
65   RP_PUT,
66   RP_REP_GET,
67   RP_ZA_GET,
68   RP_EXP_GET,
69   RP_DONE
70 };
71
72
73 struct CpsRunContext
74 {
75   unsigned int i;
76   struct GNUNET_TIME_Absolute start;
77   struct GNUNET_TIME_Absolute end;
78   const struct GNUNET_CONFIGURATION_Handle *cfg;
79   struct GNUNET_DATASTORE_PluginFunctions *api;
80   enum RunPhase phase;
81   unsigned int cnt;
82   unsigned int iter;
83   uint64_t offset;
84 };
85
86
87 /**
88  * Function called by plugins to notify us about a
89  * change in their disk utilization.
90  *
91  * @param cls closure (NULL)
92  * @param delta change in disk utilization,
93  *        0 for "reset to empty"
94  */
95 static void
96 disk_utilization_change_cb (void *cls, int delta)
97 {
98 }
99
100
101 static void
102 test (void *cls);
103
104
105 /**
106  * Put continuation.
107  *
108  * @param cls closure
109  * @param key key for the item stored
110  * @param size size of the item stored
111  * @param status #GNUNET_OK or #GNUNET_SYSERROR
112  * @param msg error message on error
113  */
114 static void
115 put_continuation (void *cls,
116                   const struct GNUNET_HashCode *key,
117                   uint32_t size,
118                   int status,
119                   const char *msg)
120 {
121   struct CpsRunContext *crc = cls;
122
123   if (GNUNET_OK != status)
124   {
125     FPRINTF (stderr, "ERROR: `%s'\n", msg);
126   }
127   else
128   {
129     stored_bytes += size;
130     stored_ops++;
131     stored_entries++;
132   }
133   GNUNET_SCHEDULER_add_now (&test, crc);
134 }
135
136
137 static void
138 do_put (struct CpsRunContext *crc)
139 {
140   char value[65536];
141   size_t size;
142   static struct GNUNET_HashCode key;
143   static int i;
144   unsigned int prio;
145
146   if (0 == i)
147     crc->start = GNUNET_TIME_absolute_get ();
148   if (PUT_10 == i)
149   {
150     i = 0;
151     crc->end = GNUNET_TIME_absolute_get ();
152     {
153       printf ("%s took %s for %llu items\n", "Storing an item",
154               GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
155                                                                                            crc->end),
156                                                       GNUNET_YES),
157               PUT_10);
158       if (PUT_10 > 0)
159         GAUGER (category, "Storing an item",
160                 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / PUT_10,
161                 "ms/item");
162     }
163     crc->i++;
164     crc->start = GNUNET_TIME_absolute_get ();
165     crc->phase++;
166     GNUNET_SCHEDULER_add_now (&test, crc);
167     return;
168   }
169   /* most content is 32k */
170   size = 32 * 1024;
171   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)   /* but some of it is less! */
172     size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
173   size = size - (size & 7);     /* always multiple of 8 */
174
175   /* generate random key */
176   key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value_us;
177   GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &key);
178   memset (value, i, size);
179   if (i > 255)
180     memset (value, i - 255, size / 2);
181   value[0] = crc->i;
182   memcpy (&value[4], &i, sizeof (i));
183   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
184   crc->api->put (crc->api->cls, &key, size, value, 1 + i % 4 /* type */ ,
185                  prio, i % 4 /* anonymity */ ,
186                  0 /* replication */ ,
187                  GNUNET_TIME_relative_to_absolute
188                  (GNUNET_TIME_relative_multiply
189                    (GNUNET_TIME_UNIT_MILLISECONDS,
190                     60 * 60 * 60 * 1000 +
191                     GNUNET_CRYPTO_random_u32
192                       (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
193                  put_continuation, crc);
194   i++;
195 }
196
197
198 static int
199 iterate_zeros (void *cls,
200                const struct GNUNET_HashCode *key,
201                uint32_t size,
202                const void *data,
203                enum GNUNET_BLOCK_Type type,
204                uint32_t priority,
205                uint32_t anonymity,
206                struct GNUNET_TIME_Absolute expiration,
207                uint64_t uid)
208 {
209   struct CpsRunContext *crc = cls;
210   int i;
211   const char *cdata = data;
212
213   GNUNET_assert (key != NULL);
214   GNUNET_assert (size >= 8);
215   memcpy (&i, &cdata[4], sizeof (i));
216   hits[i / 8] |= (1 << (i % 8));
217
218   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219               "Found result %d type=%u, priority=%u, size=%u, expire=%s\n",
220               i,
221               type, priority, size,
222               GNUNET_STRINGS_absolute_time_to_string (expiration));
223   crc->cnt++;
224   if (crc->cnt == PUT_10 / 4 - 1)
225   {
226     unsigned int bc;
227
228     bc = 0;
229     for (i = 0; i < PUT_10; i++)
230       if (0 != (hits[i / 8] & (1 << (i % 8))))
231         bc++;
232
233     crc->end = GNUNET_TIME_absolute_get ();
234     printf ("%s took %s yielding %u/%u items\n",
235             "Select random zero-anonymity item",
236             GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
237                                                                                          crc->end),
238                                                     GNUNET_YES),
239             bc, crc->cnt);
240     if (crc->cnt > 0)
241       GAUGER (category, "Select random zero-anonymity item",
242               (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
243               "ms/item");
244     memset (hits, 0, sizeof (hits));
245     crc->phase++;
246     crc->cnt = 0;
247     crc->start = GNUNET_TIME_absolute_get ();
248   }
249   GNUNET_SCHEDULER_add_now (&test, crc);
250   return GNUNET_OK;
251 }
252
253
254 static int
255 expiration_get (void *cls,
256                 const struct GNUNET_HashCode *key,
257                 uint32_t size,
258                 const void *data,
259                 enum GNUNET_BLOCK_Type type,
260                 uint32_t priority,
261                 uint32_t anonymity,
262                 struct GNUNET_TIME_Absolute expiration,
263                 uint64_t uid)
264 {
265   struct CpsRunContext *crc = cls;
266   int i;
267   const char *cdata = data;
268
269   GNUNET_assert (size >= 8);
270   memcpy (&i, &cdata[4], sizeof (i));
271   hits[i / 8] |= (1 << (i % 8));
272   crc->cnt++;
273   if (PUT_10 <= crc->cnt)
274   {
275     unsigned int bc;
276
277     bc = 0;
278     for (i = 0; i < PUT_10; i++)
279       if (0 != (hits[i / 8] & (1 << (i % 8))))
280         bc++;
281
282     crc->end = GNUNET_TIME_absolute_get ();
283     printf ("%s took %s yielding %u/%u items\n",
284             "Selecting and deleting by expiration",
285             GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
286                                                                                          crc->end),
287                                                     GNUNET_YES),
288             bc, (unsigned int) PUT_10);
289     if (crc->cnt > 0)
290       GAUGER (category, "Selecting and deleting by expiration",
291               (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
292               "ms/item");
293     memset (hits, 0, sizeof (hits));
294     if (++crc->iter == ITERATIONS)
295       crc->phase++;
296     else
297       crc->phase = RP_PUT;
298     crc->cnt = 0;
299     crc->start = GNUNET_TIME_absolute_get ();
300   }
301   GNUNET_SCHEDULER_add_now (&test, crc);
302   return GNUNET_NO;
303 }
304
305
306 static int
307 replication_get (void *cls,
308                  const struct GNUNET_HashCode *key,
309                  uint32_t size,
310                  const void *data,
311                  enum GNUNET_BLOCK_Type type,
312                  uint32_t priority,
313                  uint32_t anonymity,
314                  struct GNUNET_TIME_Absolute expiration,
315                  uint64_t uid)
316 {
317   struct CpsRunContext *crc = cls;
318   int i;
319   const char *cdata = data;
320
321   GNUNET_assert (NULL != key);
322   GNUNET_assert (size >= 8);
323   memcpy (&i, &cdata[4], sizeof (i));
324   hits[i / 8] |= (1 << (i % 8));
325   crc->cnt++;
326   if (PUT_10 <= crc->cnt)
327   {
328     unsigned int bc;
329
330     bc = 0;
331     for (i = 0; i < PUT_10; i++)
332       if (0 != (hits[i / 8] & (1 << (i % 8))))
333         bc++;
334
335     crc->end = GNUNET_TIME_absolute_get ();
336     printf ("%s took %s yielding %u/%u items\n",
337             "Selecting random item for replication",
338             GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
339                                                                                          crc->end),
340                                                     GNUNET_YES),
341             bc, (unsigned int) PUT_10);
342     if (crc->cnt > 0)
343       GAUGER (category, "Selecting random item for replication",
344               (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
345               "ms/item");
346     memset (hits, 0, sizeof (hits));
347     crc->phase++;
348     crc->offset = 0;
349     crc->cnt = 0;
350     crc->start = GNUNET_TIME_absolute_get ();
351   }
352
353   GNUNET_SCHEDULER_add_now (&test, crc);
354   return GNUNET_OK;
355 }
356
357
358 /**
359  * Function called when the service shuts
360  * down.  Unloads our datastore plugin.
361  *
362  * @param api api to unload
363  * @param cfg configuration to use
364  */
365 static void
366 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
367                const struct GNUNET_CONFIGURATION_Handle *cfg)
368 {
369   char *name;
370   char *libname;
371
372   if (GNUNET_OK !=
373       GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
374                                              &name))
375   {
376     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377                 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
378                 "DATASTORE");
379     return;
380   }
381   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
382   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
383   GNUNET_free (libname);
384   GNUNET_free (name);
385 }
386
387
388
389 /**
390  * Last task run during shutdown.  Disconnects us from
391  * the transport and core.
392  */
393 static void
394 cleaning_task (void *cls)
395 {
396   struct CpsRunContext *crc = cls;
397
398   unload_plugin (crc->api, crc->cfg);
399   GNUNET_free (crc);
400 }
401
402
403 static void
404 test (void *cls)
405 {
406   struct CpsRunContext *crc = cls;
407
408   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409               "In phase %d, iteration %u\n", crc->phase, crc->cnt);
410   switch (crc->phase)
411   {
412   case RP_ERROR:
413     GNUNET_break (0);
414     crc->api->drop (crc->api->cls);
415     ok = 1;
416     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
417                                         &cleaning_task, crc);
418     break;
419   case RP_PUT:
420     do_put (crc);
421     break;
422   case RP_REP_GET:
423     crc->api->get_replication (crc->api->cls, &replication_get, crc);
424     break;
425   case RP_ZA_GET:
426     crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
427                                   &iterate_zeros, crc);
428     break;
429   case RP_EXP_GET:
430     crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
431     break;
432   case RP_DONE:
433     crc->api->drop (crc->api->cls);
434     ok = 0;
435     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
436                                         &cleaning_task, crc);
437     break;
438   }
439 }
440
441
442 /**
443  * Load the datastore plugin.
444  */
445 static struct GNUNET_DATASTORE_PluginFunctions *
446 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
447 {
448   static struct GNUNET_DATASTORE_PluginEnvironment env;
449   struct GNUNET_DATASTORE_PluginFunctions *ret;
450   char *name;
451   char *libname;
452
453   if (GNUNET_OK !=
454       GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
455                                              &name))
456   {
457     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458                 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
459                 "DATASTORE");
460     return NULL;
461   }
462   env.cfg = cfg;
463   env.duc = &disk_utilization_change_cb;
464   env.cls = NULL;
465   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
466               name);
467   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
468   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
469   {
470     FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
471     GNUNET_free (name);
472     GNUNET_free (libname);
473     return NULL;
474   }
475   GNUNET_free (libname);
476   GNUNET_free (name);
477   return ret;
478 }
479
480
481 static void
482 run (void *cls, char *const *args, const char *cfgfile,
483      const struct GNUNET_CONFIGURATION_Handle *c)
484 {
485   struct GNUNET_DATASTORE_PluginFunctions *api;
486   struct CpsRunContext *crc;
487
488   if (NULL == c)
489   {
490     GNUNET_break (0);
491     return;
492   }
493   api = load_plugin (c);
494   if (api == NULL)
495   {
496     FPRINTF (stderr,
497              "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
498     return;
499   }
500   crc = GNUNET_new (struct CpsRunContext);
501   crc->api = api;
502   crc->cfg = c;
503   crc->phase = RP_PUT;
504   ok = 2;
505   GNUNET_SCHEDULER_add_now (&test, crc);
506 }
507
508
509 int
510 main (int argc, char *argv[])
511 {
512   char dir_name[128];
513   char cfg_name[128];
514   char *const xargv[] = {
515     "perf-plugin-datastore",
516     "-c",
517     cfg_name,
518     NULL
519   };
520   struct GNUNET_GETOPT_CommandLineOption options[] = {
521     GNUNET_GETOPT_OPTION_END
522   };
523
524   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
525   GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
526                    plugin_name);
527   GNUNET_DISK_directory_remove (dir_name);
528   GNUNET_log_setup ("perf-plugin-datastore",
529                     "WARNING",
530                     NULL);
531   GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
532   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
533                    "perf_plugin_datastore_data_%s.conf", plugin_name);
534   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
535                       "perf-plugin-datastore", "nohelp", options, &run, NULL);
536   if (ok != 0)
537     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
538   GNUNET_DISK_directory_remove (dir_name);
539
540   return ok;
541 }
542
543 /* end of perf_plugin_datastore.c */