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