2 This file is part of GNUnet.
3 Copyright (C) 2004, 2005, 2006, 2007, 2009, 2011 GNUnet e.V.
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
21 * @file perf_plugin_datastore.c
22 * @brief Profile database plugin directly, focusing on iterators.
23 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_datastore_plugin.h"
30 #include "gnunet_testing_lib.h"
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.
39 #define MAX_SIZE 1024LL * 1024 * 16 * 1
44 * Number of put operations equivalent to 1/10th of MAX_SIZE
46 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
48 static char category[256];
50 static unsigned int hits[PUT_10 / 8 + 1];
52 static unsigned long long stored_bytes;
54 static unsigned long long stored_entries;
56 static unsigned long long stored_ops;
58 static const char *plugin_name;
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;
88 * Function called by plugins to notify us about a
89 * change in their disk utilization.
91 * @param cls closure (NULL)
92 * @param delta change in disk utilization,
93 * 0 for "reset to empty"
96 disk_utilization_change_cb (void *cls, int delta)
109 * @param key key for the item stored
110 * @param size size of the item stored
111 * @param status #GNUNET_OK or #GNUNET_SYSERROR
112 * @param msg error message on error
115 put_continuation (void *cls,
116 const struct GNUNET_HashCode *key,
121 struct CpsRunContext *crc = cls;
123 if (GNUNET_OK != status)
125 FPRINTF (stderr, "ERROR: `%s'\n", msg);
129 stored_bytes += size;
133 GNUNET_SCHEDULER_add_now (&test, crc);
138 do_put (struct CpsRunContext *crc)
142 static struct GNUNET_HashCode key;
147 crc->start = GNUNET_TIME_absolute_get ();
151 crc->end = GNUNET_TIME_absolute_get ();
153 printf ("%s took %s for %llu items\n", "Storing an item",
154 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
159 GAUGER (category, "Storing an item",
160 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / PUT_10,
164 crc->start = GNUNET_TIME_absolute_get ();
166 GNUNET_SCHEDULER_add_now (&test, crc);
169 /* most content is 32k */
171 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
172 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
173 size = size - (size & 7); /* always multiple of 8 */
175 /* generate random key */
176 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value_us;
177 GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &key);
178 memset (value, i, size);
180 memset (value, i - 255, size / 2);
182 memcpy (&value[4], &i, sizeof (i));
183 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
184 crc->api->put (crc->api->cls, &key, size, value, 1 + i % 4 /* type */ ,
185 prio, i % 4 /* anonymity */ ,
186 0 /* replication */ ,
187 GNUNET_TIME_relative_to_absolute
188 (GNUNET_TIME_relative_multiply
189 (GNUNET_TIME_UNIT_MILLISECONDS,
190 60 * 60 * 60 * 1000 +
191 GNUNET_CRYPTO_random_u32
192 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
193 put_continuation, crc);
199 iterate_zeros (void *cls,
200 const struct GNUNET_HashCode *key,
203 enum GNUNET_BLOCK_Type type,
206 struct GNUNET_TIME_Absolute expiration,
209 struct CpsRunContext *crc = cls;
211 const char *cdata = data;
213 GNUNET_assert (key != NULL);
214 GNUNET_assert (size >= 8);
215 memcpy (&i, &cdata[4], sizeof (i));
216 hits[i / 8] |= (1 << (i % 8));
218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
219 "Found result %d type=%u, priority=%u, size=%u, expire=%s\n",
221 type, priority, size,
222 GNUNET_STRINGS_absolute_time_to_string (expiration));
224 if (crc->cnt == PUT_10 / 4 - 1)
229 for (i = 0; i < PUT_10; i++)
230 if (0 != (hits[i / 8] & (1 << (i % 8))))
233 crc->end = GNUNET_TIME_absolute_get ();
234 printf ("%s took %s yielding %u/%u items\n",
235 "Select random zero-anonymity item",
236 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
241 GAUGER (category, "Select random zero-anonymity item",
242 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
244 memset (hits, 0, sizeof (hits));
247 crc->start = GNUNET_TIME_absolute_get ();
249 GNUNET_SCHEDULER_add_now (&test, crc);
255 expiration_get (void *cls,
256 const struct GNUNET_HashCode *key,
259 enum GNUNET_BLOCK_Type type,
262 struct GNUNET_TIME_Absolute expiration,
265 struct CpsRunContext *crc = cls;
267 const char *cdata = data;
269 GNUNET_assert (size >= 8);
270 memcpy (&i, &cdata[4], sizeof (i));
271 hits[i / 8] |= (1 << (i % 8));
273 if (PUT_10 <= crc->cnt)
278 for (i = 0; i < PUT_10; i++)
279 if (0 != (hits[i / 8] & (1 << (i % 8))))
282 crc->end = GNUNET_TIME_absolute_get ();
283 printf ("%s took %s yielding %u/%u items\n",
284 "Selecting and deleting by expiration",
285 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
288 bc, (unsigned int) PUT_10);
290 GAUGER (category, "Selecting and deleting by expiration",
291 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
293 memset (hits, 0, sizeof (hits));
294 if (++crc->iter == ITERATIONS)
299 crc->start = GNUNET_TIME_absolute_get ();
301 GNUNET_SCHEDULER_add_now (&test, crc);
307 replication_get (void *cls,
308 const struct GNUNET_HashCode *key,
311 enum GNUNET_BLOCK_Type type,
314 struct GNUNET_TIME_Absolute expiration,
317 struct CpsRunContext *crc = cls;
319 const char *cdata = data;
321 GNUNET_assert (NULL != key);
322 GNUNET_assert (size >= 8);
323 memcpy (&i, &cdata[4], sizeof (i));
324 hits[i / 8] |= (1 << (i % 8));
326 if (PUT_10 <= crc->cnt)
331 for (i = 0; i < PUT_10; i++)
332 if (0 != (hits[i / 8] & (1 << (i % 8))))
335 crc->end = GNUNET_TIME_absolute_get ();
336 printf ("%s took %s yielding %u/%u items\n",
337 "Selecting random item for replication",
338 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
341 bc, (unsigned int) PUT_10);
343 GAUGER (category, "Selecting random item for replication",
344 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
346 memset (hits, 0, sizeof (hits));
350 crc->start = GNUNET_TIME_absolute_get ();
353 GNUNET_SCHEDULER_add_now (&test, crc);
359 * Function called when the service shuts
360 * down. Unloads our datastore plugin.
362 * @param api api to unload
363 * @param cfg configuration to use
366 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
367 const struct GNUNET_CONFIGURATION_Handle *cfg)
373 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
376 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
377 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
381 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
382 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
383 GNUNET_free (libname);
390 * Last task run during shutdown. Disconnects us from
391 * the transport and core.
394 cleaning_task (void *cls)
396 struct CpsRunContext *crc = cls;
398 unload_plugin (crc->api, crc->cfg);
406 struct CpsRunContext *crc = cls;
408 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
409 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
414 crc->api->drop (crc->api->cls);
416 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
417 &cleaning_task, crc);
423 crc->api->get_replication (crc->api->cls, &replication_get, crc);
426 crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
427 &iterate_zeros, crc);
430 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
433 crc->api->drop (crc->api->cls);
435 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
436 &cleaning_task, crc);
443 * Load the datastore plugin.
445 static struct GNUNET_DATASTORE_PluginFunctions *
446 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
448 static struct GNUNET_DATASTORE_PluginEnvironment env;
449 struct GNUNET_DATASTORE_PluginFunctions *ret;
454 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
457 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
458 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
463 env.duc = &disk_utilization_change_cb;
465 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
467 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
468 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
470 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
472 GNUNET_free (libname);
475 GNUNET_free (libname);
482 run (void *cls, char *const *args, const char *cfgfile,
483 const struct GNUNET_CONFIGURATION_Handle *c)
485 struct GNUNET_DATASTORE_PluginFunctions *api;
486 struct CpsRunContext *crc;
493 api = load_plugin (c);
497 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
500 crc = GNUNET_new (struct CpsRunContext);
505 GNUNET_SCHEDULER_add_now (&test, crc);
510 main (int argc, char *argv[])
514 char *const xargv[] = {
515 "perf-plugin-datastore",
520 struct GNUNET_GETOPT_CommandLineOption options[] = {
521 GNUNET_GETOPT_OPTION_END
524 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
525 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
527 GNUNET_DISK_directory_remove (dir_name);
528 GNUNET_log_setup ("perf-plugin-datastore",
531 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
532 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
533 "perf_plugin_datastore_data_%s.conf", plugin_name);
534 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
535 "perf-plugin-datastore", "nohelp", options, &run, NULL);
537 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
538 GNUNET_DISK_directory_remove (dir_name);
543 /* end of perf_plugin_datastore.c */