-fix
[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 "gnunet_testing_lib-new.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 putValue (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
103 {
104   char value[65536];
105   size_t size;
106   static GNUNET_HashCode key;
107   static int ic;
108   char *msg;
109   unsigned int prio;
110
111   /* most content is 32k */
112   size = 32 * 1024;
113   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)   /* but some of it is less! */
114     size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
115   size = size - (size & 7);     /* always multiple of 8 */
116
117   /* generate random key */
118   key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
119   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
120   memset (value, i, size);
121   if (i > 255)
122     memset (value, i - 255, size / 2);
123   value[0] = k;
124   memcpy (&value[4], &i, sizeof (i));
125   msg = NULL;
126   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
127   if (GNUNET_OK != api->put (api->cls, &key, size, value, 1 + i % 4 /* type */ ,
128                              prio, i % 4 /* anonymity */ ,
129                              0 /* replication */ ,
130                              GNUNET_TIME_relative_to_absolute
131                              (GNUNET_TIME_relative_multiply
132                               (GNUNET_TIME_UNIT_MILLISECONDS,
133                                60 * 60 * 60 * 1000 +
134                                GNUNET_CRYPTO_random_u32
135                                (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
136   {
137     FPRINTF (stderr, "ERROR: `%s'\n", msg);
138     GNUNET_free_non_null (msg);
139     return;
140   }
141   ic++;
142   stored_bytes += size;
143   stored_ops++;
144   stored_entries++;
145 }
146
147 static void
148 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   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
167               "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
168               type, priority, size, (unsigned long long) expiration.abs_value);
169   crc->cnt++;
170   if (crc->cnt == PUT_10 / 4 - 1)
171   {
172     unsigned int bc;
173
174     bc = 0;
175     for (i = 0; i < PUT_10; i++)
176       if (0 != (hits[i / 8] & (1 << (i % 8))))
177         bc++;
178
179     crc->end = GNUNET_TIME_absolute_get ();
180     printf ("%s took %llu ms yielding %u/%u items\n",
181             "Select random zero-anonymity item",
182             (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
183             bc, crc->cnt);
184     if (crc->cnt > 0)
185       GAUGER (category, "Select random zero-anonymity item",
186               (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
187               "ms/item");
188     memset (hits, 0, sizeof (hits));
189     crc->phase++;
190     crc->cnt = 0;
191     crc->start = GNUNET_TIME_absolute_get ();
192   }
193   GNUNET_SCHEDULER_add_now (&test, crc);
194   return GNUNET_OK;
195 }
196
197
198 static int
199 expiration_get (void *cls, const GNUNET_HashCode * key, uint32_t size,
200                 const void *data, enum GNUNET_BLOCK_Type type,
201                 uint32_t priority, uint32_t anonymity,
202                 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
203 {
204   struct CpsRunContext *crc = cls;
205   int i;
206   const char *cdata = data;
207
208   GNUNET_assert (size >= 8);
209   memcpy (&i, &cdata[4], sizeof (i));
210   hits[i / 8] |= (1 << (i % 8));
211   crc->cnt++;
212   if (PUT_10 <= crc->cnt)
213   {
214     unsigned int bc;
215
216     bc = 0;
217     for (i = 0; i < PUT_10; i++)
218       if (0 != (hits[i / 8] & (1 << (i % 8))))
219         bc++;
220
221     crc->end = GNUNET_TIME_absolute_get ();
222     printf ("%s took %llu ms yielding %u/%u items\n",
223             "Selecting and deleting by expiration",
224             (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
225             bc, (unsigned int) PUT_10);
226     if (crc->cnt > 0)
227       GAUGER (category, "Selecting and deleting by expiration",
228               (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
229               "ms/item");
230     memset (hits, 0, sizeof (hits));
231     if (++crc->iter == ITERATIONS)
232       crc->phase++;
233     else
234       crc->phase = RP_PUT;
235     crc->cnt = 0;
236     crc->start = GNUNET_TIME_absolute_get ();
237   }
238   GNUNET_SCHEDULER_add_now (&test, crc);
239   return GNUNET_NO;
240 }
241
242
243 static int
244 replication_get (void *cls, const GNUNET_HashCode * key, uint32_t size,
245                  const void *data, enum GNUNET_BLOCK_Type type,
246                  uint32_t priority, uint32_t anonymity,
247                  struct GNUNET_TIME_Absolute expiration, uint64_t uid)
248 {
249   struct CpsRunContext *crc = cls;
250   int i;
251   const char *cdata = data;
252
253   GNUNET_assert (NULL != key);
254   GNUNET_assert (size >= 8);
255   memcpy (&i, &cdata[4], sizeof (i));
256   hits[i / 8] |= (1 << (i % 8));
257   crc->cnt++;
258   if (PUT_10 <= crc->cnt)
259   {
260     unsigned int bc;
261
262     bc = 0;
263     for (i = 0; i < PUT_10; i++)
264       if (0 != (hits[i / 8] & (1 << (i % 8))))
265         bc++;
266
267     crc->end = GNUNET_TIME_absolute_get ();
268     printf ("%s took %llu ms yielding %u/%u items\n",
269             "Selecting random item for replication",
270             (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
271             bc, (unsigned int) PUT_10);
272     if (crc->cnt > 0)
273       GAUGER (category, "Selecting random item for replication",
274               (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
275               "ms/item");
276     memset (hits, 0, sizeof (hits));
277     crc->phase++;
278     crc->offset = 0;
279     crc->cnt = 0;
280     crc->start = GNUNET_TIME_absolute_get ();
281   }
282
283   GNUNET_SCHEDULER_add_now (&test, crc);
284   return GNUNET_OK;
285 }
286
287
288 /**
289  * Function called when the service shuts
290  * down.  Unloads our datastore plugin.
291  *
292  * @param api api to unload
293  * @param cfg configuration to use
294  */
295 static void
296 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
297                const struct GNUNET_CONFIGURATION_Handle *cfg)
298 {
299   char *name;
300   char *libname;
301
302   if (GNUNET_OK !=
303       GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
304                                              &name))
305   {
306     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307                 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
308                 "DATASTORE");
309     return;
310   }
311   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
312   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
313   GNUNET_free (libname);
314   GNUNET_free (name);
315 }
316
317
318
319 /**
320  * Last task run during shutdown.  Disconnects us from
321  * the transport and core.
322  */
323 static void
324 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
325 {
326   struct CpsRunContext *crc = cls;
327
328   unload_plugin (crc->api, crc->cfg);
329   GNUNET_free (crc);
330 }
331
332
333 static void
334 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
335 {
336   struct CpsRunContext *crc = cls;
337   int j;
338
339   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
340   {
341     GNUNET_break (0);
342     crc->phase = RP_ERROR;
343   }
344   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
345               "In phase %d, iteration %u\n", crc->phase, crc->cnt);
346   switch (crc->phase)
347   {
348   case RP_ERROR:
349     GNUNET_break (0);
350     crc->api->drop (crc->api->cls);
351     ok = 1;
352     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
353                                         &cleaning_task, crc);
354     break;
355   case RP_PUT:
356     crc->start = GNUNET_TIME_absolute_get ();
357     for (j = 0; j < PUT_10; j++)
358       putValue (crc->api, j, crc->i);
359     crc->end = GNUNET_TIME_absolute_get ();
360     {
361       printf ("%s took %llu ms for %llu items\n", "Storing an item",
362               (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
363               PUT_10);
364       if (PUT_10 > 0)
365         GAUGER (category, "Storing an item",
366                 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
367                 "ms/item");
368     }
369     crc->i++;
370     crc->start = GNUNET_TIME_absolute_get ();
371     crc->phase++;
372     GNUNET_SCHEDULER_add_now (&test, crc);
373     break;
374   case RP_REP_GET:
375     crc->api->get_replication (crc->api->cls, &replication_get, crc);
376     break;
377   case RP_ZA_GET:
378     crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
379                                   &iterate_zeros, crc);
380     break;
381   case RP_EXP_GET:
382     crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
383     break;
384   case RP_DONE:
385     crc->api->drop (crc->api->cls);
386     ok = 0;
387     GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
388                                         &cleaning_task, crc);
389     break;
390   }
391 }
392
393
394 /**
395  * Load the datastore plugin.
396  */
397 static struct GNUNET_DATASTORE_PluginFunctions *
398 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
399 {
400   static struct GNUNET_DATASTORE_PluginEnvironment env;
401   struct GNUNET_DATASTORE_PluginFunctions *ret;
402   char *name;
403   char *libname;
404
405   if (GNUNET_OK !=
406       GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
407                                              &name))
408   {
409     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
410                 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
411                 "DATASTORE");
412     return NULL;
413   }
414   env.cfg = cfg;
415   env.duc = &disk_utilization_change_cb;
416   env.cls = NULL;
417   GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
418               name);
419   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
420   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
421   {
422     FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
423     return NULL;
424   }
425   GNUNET_free (libname);
426   GNUNET_free (name);
427   return ret;
428 }
429
430
431 static void
432 run (void *cls, char *const *args, const char *cfgfile,
433      const struct GNUNET_CONFIGURATION_Handle *c)
434 {
435   struct GNUNET_DATASTORE_PluginFunctions *api;
436   struct CpsRunContext *crc;
437
438   api = load_plugin (c);
439   if (api == NULL)
440   {
441     FPRINTF (stderr,
442              "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
443     return;
444   }
445   crc = GNUNET_malloc (sizeof (struct CpsRunContext));
446   crc->api = api;
447   crc->cfg = c;
448   crc->phase = RP_PUT;
449   ok = 2;
450   GNUNET_SCHEDULER_add_now (&test, crc);
451 }
452
453
454 int
455 main (int argc, char *argv[])
456 {
457   char dir_name[128];
458   char cfg_name[128];
459   char *const xargv[] = {
460     "perf-plugin-datastore",
461     "-c",
462     cfg_name,
463     NULL
464   };
465   struct GNUNET_GETOPT_CommandLineOption options[] = {
466     GNUNET_GETOPT_OPTION_END
467   };
468
469   plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
470   GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
471                    plugin_name);
472   GNUNET_DISK_directory_remove (dir_name);
473   GNUNET_log_setup ("perf-plugin-datastore",
474                     "WARNING",
475                     NULL);
476   GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
477   GNUNET_snprintf (cfg_name, sizeof (cfg_name),
478                    "perf_plugin_datastore_data_%s.conf", plugin_name);
479   GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
480                       "perf-plugin-datastore", "nohelp", options, &run, NULL);
481   if (ok != 0)
482     FPRINTF (stderr, "Missed some testcases: %u\n", ok);
483   GNUNET_DISK_directory_remove (dir_name);
484
485   return ok;
486 }
487
488 /* end of perf_plugin_datastore.c */