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