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