(no commit message)
[oweals/gnunet.git] / src / datastore / perf_plugin_datastore.c
1 /*
2      This file is part of GNUnet.
3      (C) 2004, 2005, 2006, 2007, 2009 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 "plugin_datastore.h"
30
31 #define VERBOSE GNUNET_NO
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 * 128
40
41 #define ITERATIONS 10
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 unsigned long long stored_bytes;
49
50 static unsigned long long stored_entries;
51
52 static unsigned long long stored_ops;
53
54 static const char *plugin_name;
55
56 static int ok;
57
58 enum RunPhase
59   {
60     RP_DONE = 0,
61     RP_PUT,
62     RP_LP_GET,
63     RP_AE_GET,
64     RP_ZA_GET,
65     RP_MO_GET,
66     RP_AN_GET
67   };
68
69
70 struct CpsRunContext
71 {
72   unsigned int i;
73   struct GNUNET_TIME_Absolute start;
74   struct GNUNET_TIME_Absolute end;
75   struct GNUNET_SCHEDULER_Handle *sched;
76   const struct GNUNET_CONFIGURATION_Handle *cfg;
77   struct GNUNET_DATASTORE_PluginFunctions * api;
78   const char *msg;
79   enum RunPhase phase;
80   unsigned int cnt;
81 };
82
83
84 /**
85  * Function called by plugins to notify us about a
86  * change in their disk utilization.
87  *
88  * @param cls closure (NULL)
89  * @param delta change in disk utilization, 
90  *        0 for "reset to empty"
91  */
92 static void
93 disk_utilization_change_cb (void *cls,
94                             int delta)
95 {
96 }
97
98              
99 static void
100 putValue (struct GNUNET_DATASTORE_PluginFunctions * api, int i, int k)
101 {
102   char value[65536];
103   size_t size;
104   static GNUNET_HashCode key;
105   static int ic;
106   char *msg;
107   unsigned int prio;
108
109   /* most content is 32k */
110   size = 32 * 1024;
111
112   if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0)  /* but some of it is less! */
113     size = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
114   size = size - (size & 7);     /* always multiple of 8 */
115
116   /* generate random key */
117   key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
118   GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
119   memset (value, i, size);
120   if (i > 255)
121     memset (value, i - 255, size / 2);
122   value[0] = k;
123   msg = NULL;
124   prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
125   if (GNUNET_OK != api->put (api->cls,
126                              &key, 
127                              size,
128                              value,
129                              i,
130                              prio,
131                              i,
132                              GNUNET_TIME_relative_to_absolute 
133                              (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
134                                                              60 * 60 * 60 * 1000 +
135                                                              GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
136                              &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
149 test (void *cls,
150       const struct GNUNET_SCHEDULER_TaskContext *tc);
151
152
153 static int
154 iterateDummy (void *cls,
155               void *next_cls,
156               const GNUNET_HashCode * key,
157               uint32_t size,
158               const void *data,
159               enum GNUNET_BLOCK_Type type,
160               uint32_t priority,
161               uint32_t anonymity,
162               struct GNUNET_TIME_Absolute
163               expiration, 
164               uint64_t uid)
165 {
166   struct CpsRunContext *crc = cls;
167   
168   if (key == NULL)
169     {
170       crc->end = GNUNET_TIME_absolute_get();
171       printf (crc->msg,
172               crc->i,
173               (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
174               crc->cnt);
175       if (crc->phase != RP_AN_GET)
176         {
177           crc->phase++;
178         }
179       else
180         {
181           if (crc->i == ITERATIONS)
182             crc->phase = RP_DONE;
183           else
184             crc->phase = RP_PUT;
185         }
186       GNUNET_SCHEDULER_add_after (crc->sched,
187                                   GNUNET_SCHEDULER_NO_TASK,
188                                   &test, crc);
189       return GNUNET_OK;
190     }
191 #if VERBOSE
192   fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
193            type, priority, size,
194            (unsigned long long) expiration.abs_value);
195 #endif
196   crc->cnt++;
197   crc->api->next_request (next_cls,
198                           GNUNET_NO);
199   return GNUNET_OK;
200 }
201
202
203
204 /**
205  * Function called when the service shuts
206  * down.  Unloads our datastore plugin.
207  *
208  * @param api api to unload
209  * @param cfg configuration to use
210  */
211 static void
212 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
213                const struct GNUNET_CONFIGURATION_Handle *cfg)
214 {
215   char *name;
216   char *libname;
217
218   if (GNUNET_OK !=
219       GNUNET_CONFIGURATION_get_value_string (cfg,
220                                              "DATASTORE", "DATABASE", &name))
221     {
222       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
223                   _("No `%s' specified for `%s' in configuration!\n"),
224                   "DATABASE",
225                   "DATASTORE");
226       return;
227     }
228   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
229   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
230   GNUNET_free (libname);
231   GNUNET_free (name);
232 }
233
234
235
236 /**
237  * Last task run during shutdown.  Disconnects us from
238  * the transport and core.
239  */
240 static void
241 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
242 {
243   struct CpsRunContext *crc = cls;
244
245   unload_plugin (crc->api, crc->cfg);
246   GNUNET_free (crc);
247 }
248
249
250 static void
251 test (void *cls,
252       const struct GNUNET_SCHEDULER_TaskContext *tc)
253 {  
254   struct CpsRunContext *crc = cls;
255   int j;
256
257   switch (crc->phase)
258     {
259     case RP_PUT:      
260       crc->start = GNUNET_TIME_absolute_get ();
261       for (j=0;j<PUT_10;j++)
262         putValue (crc->api, j, crc->i);
263       crc->end = GNUNET_TIME_absolute_get ();
264       printf ("%3u insertion took                      %20llums for %u\n",
265               crc->i,
266               (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
267               (unsigned int) PUT_10);
268       crc->i++;
269       crc->phase = RP_LP_GET;
270       GNUNET_SCHEDULER_add_after (crc->sched,
271                                   GNUNET_SCHEDULER_NO_TASK,
272                                   &test, crc);
273       break;
274     case RP_LP_GET:
275       crc->cnt = 0;
276       crc->start = GNUNET_TIME_absolute_get ();      
277       crc->msg = "%3u low priority iteration took         %20llums for %u\n";
278       crc->api->iter_low_priority (crc->api->cls, 0, 
279                                    &iterateDummy,
280                                    crc);
281       break;
282     case RP_AE_GET:
283       crc->cnt = 0;
284       crc->start = GNUNET_TIME_absolute_get ();      
285       crc->msg = "%3u ascending expiration iteration took %20llums for %u\n";
286       crc->api->iter_ascending_expiration (crc->api->cls, 0, 
287                                       &iterateDummy,
288                                       crc);
289       break;
290     case RP_ZA_GET:
291       crc->cnt = 0;
292       crc->start = GNUNET_TIME_absolute_get ();      
293       crc->msg = "%3u zero anonymity iteration took       %20llums for %u\n";
294       crc->api->iter_zero_anonymity (crc->api->cls, 0, 
295                                      &iterateDummy,
296                                      crc);
297       break;
298     case RP_MO_GET:
299       crc->cnt = 0;
300       crc->start = GNUNET_TIME_absolute_get ();      
301       crc->msg = "%3u migration order iteration took      %20llums for %u\n";
302       crc->api->iter_migration_order (crc->api->cls, 0, 
303                                       &iterateDummy,
304                                       crc);
305       break;
306     case RP_AN_GET:
307       crc->cnt = 0;
308       crc->start = GNUNET_TIME_absolute_get ();      
309       crc->msg = "%3u all now iteration took              %20llums for %u\n";
310       crc->api->iter_all_now (crc->api->cls, 0,
311                               &iterateDummy,
312                               crc);
313       break;
314     case RP_DONE:
315       crc->api->drop (crc->api->cls);
316       GNUNET_SCHEDULER_add_with_priority (crc->sched,
317                                     GNUNET_SCHEDULER_PRIORITY_IDLE,
318                                     &cleaning_task, crc);
319       break;
320     }
321 }
322
323
324 /**
325  * Load the datastore plugin.
326  */
327 static struct GNUNET_DATASTORE_PluginFunctions *
328 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg,
329              struct GNUNET_SCHEDULER_Handle *sched)
330 {
331   static struct GNUNET_DATASTORE_PluginEnvironment env;
332   struct GNUNET_DATASTORE_PluginFunctions * ret; 
333   char *name;
334   char *libname;
335
336   if (GNUNET_OK !=
337       GNUNET_CONFIGURATION_get_value_string (cfg,
338                                              "DATASTORE", "DATABASE", &name))
339     {
340       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341                   _("No `%s' specified for `%s' in configuration!\n"),
342                   "DATABASE",
343                   "DATASTORE");
344       return NULL;
345     }
346   env.cfg = cfg;
347   env.sched = sched;  
348   env.duc = &disk_utilization_change_cb;
349   env.cls = NULL;
350   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
351               _("Loading `%s' datastore plugin\n"), name);
352   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
353   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
354     {
355       fprintf (stderr,
356                "Failed to load plugin `%s'!\n",
357                name);
358       return NULL;
359     }
360   GNUNET_free (libname);
361   GNUNET_free (name);
362   return ret;
363 }
364
365
366 static void
367 run (void *cls,
368      struct GNUNET_SCHEDULER_Handle *s,
369      char *const *args,
370      const char *cfgfile,
371      const struct GNUNET_CONFIGURATION_Handle *c)
372 {
373   struct GNUNET_DATASTORE_PluginFunctions *api;
374   struct CpsRunContext *crc;
375
376   api = load_plugin (c, s);
377   if (api == NULL)
378     {
379       fprintf (stderr, 
380                "Could not initialize plugin, assuming database not configured. Test not run!\n");
381       return;
382     }
383   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
384   crc->api = api;
385   crc->sched = s;
386   crc->cfg = c;
387   crc->phase = RP_PUT;
388   GNUNET_SCHEDULER_add_now (crc->sched,
389                             &test, crc);
390 }
391
392
393 static int
394 check ()
395 {
396   char cfg_name[128];
397   char *const argv[] = { 
398     "perf-plugin-datastore",
399     "-c",
400     cfg_name,
401 #if VERBOSE
402     "-L", "DEBUG",
403 #endif
404     NULL
405   };
406   struct GNUNET_GETOPT_CommandLineOption options[] = {
407     GNUNET_GETOPT_OPTION_END
408   };
409
410   GNUNET_snprintf (cfg_name,
411                    sizeof (cfg_name),
412                    "perf_plugin_datastore_data_%s.conf",
413                    plugin_name);
414   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
415                       argv, "perf-plugin-datastore", "nohelp",
416                       options, &run, NULL);
417   if (ok != 0)
418     fprintf (stderr, "Missed some testcases: %u\n", ok);
419   return ok;
420 }
421
422
423 int
424 main (int argc, char *argv[])
425 {
426   int ret;
427   const char *pos;
428   char dir_name[128];
429
430   /* determine name of plugin to use */
431   plugin_name = argv[0];
432   while (NULL != (pos = strstr(plugin_name, "_")))
433     plugin_name = pos+1;
434
435   GNUNET_snprintf (dir_name,
436                    sizeof (dir_name),
437                    "/tmp/perf-gnunet-datastore-%s",
438                    plugin_name);
439   GNUNET_DISK_directory_remove (dir_name);
440   GNUNET_log_setup ("perf-plugin-datastore",
441 #if VERBOSE
442                     "DEBUG",
443 #else
444                     "WARNING",
445 #endif
446                     NULL);
447   ret = check ();
448   GNUNET_DISK_directory_remove (dir_name);
449
450   return ret;
451 }
452
453 /* end of perf_plugin_datastore.c */
454
455