removing "unused variable" error
[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,
129                              prio,
130                              i,
131                              GNUNET_TIME_relative_to_absolute 
132                              (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
133                                                              60 * 60 * 60 * 1000 +
134                                                              GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
135                              &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,
149       const struct GNUNET_SCHEDULER_TaskContext *tc);
150
151
152 static int
153 iterateDummy (void *cls,
154               void *next_cls,
155               const GNUNET_HashCode * key,
156               uint32_t size,
157               const void *data,
158               enum GNUNET_BLOCK_Type type,
159               uint32_t priority,
160               uint32_t anonymity,
161               struct GNUNET_TIME_Absolute
162               expiration, 
163               uint64_t uid)
164 {
165   struct CpsRunContext *crc = cls;
166   
167   if (key == NULL)
168     {
169       crc->end = GNUNET_TIME_absolute_get();
170       printf (crc->msg,
171               crc->i,
172               (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
173               crc->cnt);
174       if (crc->phase != RP_AN_GET)
175         {
176           crc->phase++;
177         }
178       else
179         {
180           if (crc->i == ITERATIONS)
181             crc->phase = RP_DONE;
182           else
183             crc->phase = RP_PUT;
184         }
185       GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
186                                   &test, crc);
187       return GNUNET_OK;
188     }
189 #if VERBOSE
190   fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
191            type, priority, size,
192            (unsigned long long) expiration.abs_value);
193 #endif
194   crc->cnt++;
195   crc->api->next_request (next_cls,
196                           GNUNET_NO);
197   return GNUNET_OK;
198 }
199
200
201
202 /**
203  * Function called when the service shuts
204  * down.  Unloads our datastore plugin.
205  *
206  * @param api api to unload
207  * @param cfg configuration to use
208  */
209 static void
210 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
211                const struct GNUNET_CONFIGURATION_Handle *cfg)
212 {
213   char *name;
214   char *libname;
215
216   if (GNUNET_OK !=
217       GNUNET_CONFIGURATION_get_value_string (cfg,
218                                              "DATASTORE", "DATABASE", &name))
219     {
220       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
221                   _("No `%s' specified for `%s' in configuration!\n"),
222                   "DATABASE",
223                   "DATASTORE");
224       return;
225     }
226   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
227   GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
228   GNUNET_free (libname);
229   GNUNET_free (name);
230 }
231
232
233
234 /**
235  * Last task run during shutdown.  Disconnects us from
236  * the transport and core.
237  */
238 static void
239 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
240 {
241   struct CpsRunContext *crc = cls;
242
243   unload_plugin (crc->api, crc->cfg);
244   GNUNET_free (crc);
245 }
246
247
248 static void
249 test (void *cls,
250       const struct GNUNET_SCHEDULER_TaskContext *tc)
251 {  
252   struct CpsRunContext *crc = cls;
253   int j;
254
255   switch (crc->phase)
256     {
257     case RP_PUT:      
258       crc->start = GNUNET_TIME_absolute_get ();
259       for (j=0;j<PUT_10;j++)
260         putValue (crc->api, j, crc->i);
261       crc->end = GNUNET_TIME_absolute_get ();
262       printf ("%3u insertion took                      %20llums for %u\n",
263               crc->i,
264               (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
265               (unsigned int) PUT_10);
266       crc->i++;
267       crc->phase = RP_LP_GET;
268       GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_NO_TASK,
269                                   &test, crc);
270       break;
271     case RP_LP_GET:
272       crc->cnt = 0;
273       crc->start = GNUNET_TIME_absolute_get ();      
274       crc->msg = "%3u low priority iteration took         %20llums for %u\n";
275       crc->api->iter_low_priority (crc->api->cls, 0, 
276                                    &iterateDummy,
277                                    crc);
278       break;
279     case RP_AE_GET:
280       crc->cnt = 0;
281       crc->start = GNUNET_TIME_absolute_get ();      
282       crc->msg = "%3u ascending expiration iteration took %20llums for %u\n";
283       crc->api->iter_ascending_expiration (crc->api->cls, 0, 
284                                       &iterateDummy,
285                                       crc);
286       break;
287     case RP_ZA_GET:
288       crc->cnt = 0;
289       crc->start = GNUNET_TIME_absolute_get ();      
290       crc->msg = "%3u zero anonymity iteration took       %20llums for %u\n";
291       crc->api->iter_zero_anonymity (crc->api->cls, 0, 
292                                      &iterateDummy,
293                                      crc);
294       break;
295     case RP_MO_GET:
296       crc->cnt = 0;
297       crc->start = GNUNET_TIME_absolute_get ();      
298       crc->msg = "%3u migration order iteration took      %20llums for %u\n";
299       crc->api->iter_migration_order (crc->api->cls, 0, 
300                                       &iterateDummy,
301                                       crc);
302       break;
303     case RP_AN_GET:
304       crc->cnt = 0;
305       crc->start = GNUNET_TIME_absolute_get ();      
306       crc->msg = "%3u all now iteration took              %20llums for %u\n";
307       crc->api->iter_all_now (crc->api->cls, 0,
308                               &iterateDummy,
309                               crc);
310       break;
311     case RP_DONE:
312       crc->api->drop (crc->api->cls);
313       GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
314                                     &cleaning_task, crc);
315       break;
316     }
317 }
318
319
320 /**
321  * Load the datastore plugin.
322  */
323 static struct GNUNET_DATASTORE_PluginFunctions *
324 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
325 {
326   static struct GNUNET_DATASTORE_PluginEnvironment env;
327   struct GNUNET_DATASTORE_PluginFunctions * ret; 
328   char *name;
329   char *libname;
330
331   if (GNUNET_OK !=
332       GNUNET_CONFIGURATION_get_value_string (cfg,
333                                              "DATASTORE", "DATABASE", &name))
334     {
335       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336                   _("No `%s' specified for `%s' in configuration!\n"),
337                   "DATABASE",
338                   "DATASTORE");
339       return NULL;
340     }
341   env.cfg = cfg;
342   env.duc = &disk_utilization_change_cb;
343   env.cls = NULL;
344   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
345               _("Loading `%s' datastore plugin\n"), name);
346   GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
347   if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
348     {
349       fprintf (stderr,
350                "Failed to load plugin `%s'!\n",
351                name);
352       return NULL;
353     }
354   GNUNET_free (libname);
355   GNUNET_free (name);
356   return ret;
357 }
358
359
360 static void
361 run (void *cls,
362      char *const *args,
363      const char *cfgfile,
364      const struct GNUNET_CONFIGURATION_Handle *c)
365 {
366   struct GNUNET_DATASTORE_PluginFunctions *api;
367   struct CpsRunContext *crc;
368
369   api = load_plugin (c);
370   if (api == NULL)
371     {
372       fprintf (stderr, 
373                "Could not initialize plugin, assuming database not configured. Test not run!\n");
374       return;
375     }
376   crc = GNUNET_malloc(sizeof(struct CpsRunContext));
377   crc->api = api;
378   crc->cfg = c;
379   crc->phase = RP_PUT;
380   GNUNET_SCHEDULER_add_now (&test, crc);
381 }
382
383
384 static int
385 check ()
386 {
387   char cfg_name[128];
388   char *const argv[] = { 
389     "perf-plugin-datastore",
390     "-c",
391     cfg_name,
392 #if VERBOSE
393     "-L", "DEBUG",
394 #endif
395     NULL
396   };
397   struct GNUNET_GETOPT_CommandLineOption options[] = {
398     GNUNET_GETOPT_OPTION_END
399   };
400
401   GNUNET_snprintf (cfg_name,
402                    sizeof (cfg_name),
403                    "perf_plugin_datastore_data_%s.conf",
404                    plugin_name);
405   GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
406                       argv, "perf-plugin-datastore", "nohelp",
407                       options, &run, NULL);
408   if (ok != 0)
409     fprintf (stderr, "Missed some testcases: %u\n", ok);
410   return ok;
411 }
412
413
414 int
415 main (int argc, char *argv[])
416 {
417   int ret;
418   char *pos;
419   char dir_name[128];
420
421   /* determine name of plugin to use */
422   plugin_name = argv[0];
423   while (NULL != (pos = strstr(plugin_name, "_")))
424     plugin_name = pos+1;
425   if (NULL != (pos = strstr(plugin_name, ".")))
426     pos[0] = 0;
427   else
428     pos = (char *) plugin_name;
429
430   GNUNET_snprintf (dir_name,
431                    sizeof (dir_name),
432                    "/tmp/perf-gnunet-datastore-%s",
433                    plugin_name);
434   GNUNET_DISK_directory_remove (dir_name);
435   GNUNET_log_setup ("perf-plugin-datastore",
436 #if VERBOSE
437                     "DEBUG",
438 #else
439                     "WARNING",
440 #endif
441                     NULL);
442   ret = check ();
443   if (pos != plugin_name)
444     pos[0] = '.';
445   GNUNET_DISK_directory_remove (dir_name);
446
447   return ret;
448 }
449
450 /* end of perf_plugin_datastore.c */
451
452