Merge branch 'master' of ssh://gnunet.org/gnunet
[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   GNUNET_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                uint32_t replication,
207                struct GNUNET_TIME_Absolute expiration,
208                uint64_t uid)
209 {
210   struct CpsRunContext *crc = cls;
211   int i;
212   const char *cdata = data;
213
214   GNUNET_assert (key != NULL);
215   GNUNET_assert (size >= 8);
216   GNUNET_memcpy (&i, &cdata[4], sizeof (i));
217   hits[i / 8] |= (1 << (i % 8));
218
219   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220               "Found result %d type=%u, priority=%u, size=%u, expire=%s\n",
221               i,
222               type, priority, size,
223               GNUNET_STRINGS_absolute_time_to_string (expiration));
224   crc->cnt++;
225   if (crc->cnt == PUT_10 / 4 - 1)
226   {
227     unsigned int bc;
228
229     bc = 0;
230     for (i = 0; i < PUT_10; i++)
231       if (0 != (hits[i / 8] & (1 << (i % 8))))
232         bc++;
233
234     crc->end = GNUNET_TIME_absolute_get ();
235     printf ("%s took %s yielding %u/%u items\n",
236             "Select random zero-anonymity item",
237             GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
238                                                                                          crc->end),
239                                                     GNUNET_YES),
240             bc, crc->cnt);
241     if (crc->cnt > 0)
242       GAUGER (category, "Select random zero-anonymity item",
243               (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
244               "ms/item");
245     memset (hits, 0, sizeof (hits));
246     crc->phase++;
247     crc->cnt = 0;
248     crc->start = GNUNET_TIME_absolute_get ();
249   }
250   GNUNET_SCHEDULER_add_now (&test, crc);
251   return GNUNET_OK;
252 }
253
254
255 static int
256 expiration_get (void *cls,
257                 const struct GNUNET_HashCode *key,
258                 uint32_t size,
259                 const void *data,
260                 enum GNUNET_BLOCK_Type type,
261                 uint32_t priority,
262                 uint32_t anonymity,
263                 uint32_t replication,
264                 struct GNUNET_TIME_Absolute expiration,
265                 uint64_t uid)
266 {
267   struct CpsRunContext *crc = cls;
268   int i;
269   const char *cdata = data;
270
271   GNUNET_assert (size >= 8);
272   GNUNET_memcpy (&i, &cdata[4], sizeof (i));
273   hits[i / 8] |= (1 << (i % 8));
274   crc->cnt++;
275   if (PUT_10 <= crc->cnt)
276   {
277     unsigned int bc;
278
279     bc = 0;
280     for (i = 0; i < PUT_10; i++)
281       if (0 != (hits[i / 8] & (1 << (i % 8))))
282         bc++;
283
284     crc->end = GNUNET_TIME_absolute_get ();
285     printf ("%s took %s yielding %u/%u items\n",
286             "Selecting and deleting by expiration",
287             GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
288                                                                                          crc->end),
289                                                     GNUNET_YES),
290             bc, (unsigned int) PUT_10);
291     if (crc->cnt > 0)
292       GAUGER (category, "Selecting and deleting by expiration",
293               (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
294               "ms/item");
295     memset (hits, 0, sizeof (hits));
296     if (++crc->iter == ITERATIONS)
297       crc->phase++;
298     else
299       crc->phase = RP_PUT;
300     crc->cnt = 0;
301     crc->start = GNUNET_TIME_absolute_get ();
302   }
303   GNUNET_SCHEDULER_add_now (&test, crc);
304   return GNUNET_NO;
305 }
306
307
308 static int
309 replication_get (void *cls,
310                  const struct GNUNET_HashCode *key,
311                  uint32_t size,
312                  const void *data,
313                  enum GNUNET_BLOCK_Type type,
314                  uint32_t priority,
315                  uint32_t anonymity,
316                  uint32_t replication,
317                  struct GNUNET_TIME_Absolute expiration,
318                  uint64_t uid)
319 {
320   struct CpsRunContext *crc = cls;
321   int i;
322   const char *cdata = data;
323
324   GNUNET_assert (NULL != key);
325   GNUNET_assert (size >= 8);
326   GNUNET_memcpy (&i, &cdata[4], sizeof (i));
327   hits[i / 8] |= (1 << (i % 8));
328   crc->cnt++;
329   if (PUT_10 <= crc->cnt)
330   {
331     unsigned int bc;
332
333     bc = 0;
334     for (i = 0; i < PUT_10; i++)
335       if (0 != (hits[i / 8] & (1 << (i % 8))))
336         bc++;
337
338     crc->end = GNUNET_TIME_absolute_get ();
339     printf ("%s took %s yielding %u/%u items\n",
340             "Selecting random item for replication",
341             GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
342                                                                                          crc->end),
343                                                     GNUNET_YES),
344             bc, (unsigned int) PUT_10);
345     if (crc->cnt > 0)
346       GAUGER (category, "Selecting random item for replication",
347               (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
348               "ms/item");
349     memset (hits, 0, sizeof (hits));
350     crc->phase++;
351     crc->offset = 0;
352     crc->cnt = 0;
353     crc->start = GNUNET_TIME_absolute_get ();
354   }
355
356   GNUNET_SCHEDULER_add_now (&test, crc);
357   return GNUNET_OK;
358 }
359
360
361 /**
362  * Function called when the service shuts
363  * down.  Unloads our datastore plugin.
364  *
365  * @param api api to unload
366  * @param cfg configuration to use
367  */
368 static void
369 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
370                const struct GNUNET_CONFIGURATION_Handle *cfg)
371 {
372   char *name;
373   char *libname;
374
375   if (GNUNET_OK !=
376       GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
377                                              &name))
378   {
379     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
380                 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
381                 "DATASTORE");
382     return;
383   }
384   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
385   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
386   GNUNET_free (libname);
387   GNUNET_free (name);
388 }
389
390
391
392 /**
393  * Last task run during shutdown.  Disconnects us from
394  * the transport and core.
395  */
396 static void
397 cleaning_task (void *cls)
398 {
399   struct CpsRunContext *crc = cls;
400
401   unload_plugin (crc->api, crc->cfg);
402   GNUNET_free (crc);
403 }
404
405
406 static void
407 test (void *cls)
408 {
409   struct CpsRunContext *crc = cls;
410
411   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
412               "In phase %d, iteration %u\n", crc->phase, crc->cnt);
413   switch (crc->phase)
414   {
415   case RP_ERROR:
416     GNUNET_break (0);
417     crc->api->drop (crc->api->cls);
418     ok = 1;
419     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
420                                         &cleaning_task, crc);
421     break;
422   case RP_PUT:
423     do_put (crc);
424     break;
425   case RP_REP_GET:
426     crc->api->get_replication (crc->api->cls, &replication_get, crc);
427     break;
428   case RP_ZA_GET:
429     crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
430                                   &iterate_zeros, crc);
431     break;
432   case RP_EXP_GET:
433     crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
434     break;
435   case RP_DONE:
436     crc->api->drop (crc->api->cls);
437     ok = 0;
438     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
439                                         &cleaning_task, crc);
440     break;
441   }
442 }
443
444
445 /**
446  * Load the datastore plugin.
447  */
448 static struct GNUNET_DATASTORE_PluginFunctions *
449 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
450 {
451   static struct GNUNET_DATASTORE_PluginEnvironment env;
452   struct GNUNET_DATASTORE_PluginFunctions *ret;
453   char *name;
454   char *libname;
455
456   if (GNUNET_OK !=
457       GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
458                                              &name))
459   {
460     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
461                 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
462                 "DATASTORE");
463     return NULL;
464   }
465   env.cfg = cfg;
466   env.duc = &disk_utilization_change_cb;
467   env.cls = NULL;
468   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
469               name);
470   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
471   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
472   {
473     FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
474     GNUNET_free (name);
475     GNUNET_free (libname);
476     return NULL;
477   }
478   GNUNET_free (libname);
479   GNUNET_free (name);
480   return ret;
481 }
482
483
484 static void
485 run (void *cls, char *const *args, const char *cfgfile,
486      const struct GNUNET_CONFIGURATION_Handle *c)
487 {
488   struct GNUNET_DATASTORE_PluginFunctions *api;
489   struct CpsRunContext *crc;
490
491   if (NULL == c)
492   {
493     GNUNET_break (0);
494     return;
495   }
496   api = load_plugin (c);
497   if (api == NULL)
498   {
499     FPRINTF (stderr,
500              "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
501     return;
502   }
503   crc = GNUNET_new (struct CpsRunContext);
504   crc->api = api;
505   crc->cfg = c;
506   crc->phase = RP_PUT;
507   ok = 2;
508   GNUNET_SCHEDULER_add_now (&test, crc);
509 }
510
511
512 int
513 main (int argc, char *argv[])
514 {
515   char dir_name[128];
516   char cfg_name[128];
517   char *const xargv[] = {
518     "perf-plugin-datastore",
519     "-c",
520     cfg_name,
521     NULL
522   };
523   struct GNUNET_GETOPT_CommandLineOption options[] = {
524     GNUNET_GETOPT_OPTION_END
525   };
526
527   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
528   GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
529                    plugin_name);
530   GNUNET_DISK_directory_remove (dir_name);
531   GNUNET_log_setup ("perf-plugin-datastore",
532                     "WARNING",
533                     NULL);
534   GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
535   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
536                    "perf_plugin_datastore_data_%s.conf", plugin_name);
537   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
538                       "perf-plugin-datastore", "nohelp", options, &run, NULL);
539   if (ok != 0)
540     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
541   GNUNET_DISK_directory_remove (dir_name);
542
543   return ok;
544 }
545
546 /* end of perf_plugin_datastore.c */