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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @file perf_plugin_datastore.c
20 * @brief Profile database plugin directly, focusing on iterators.
21 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_protocols.h"
27 #include "gnunet_datastore_plugin.h"
28 #include "gnunet_testing_lib.h"
32 * Target datastore size (in bytes). Realistic sizes are
33 * more like 16 GB (not the default of 16 MB); however,
34 * those take too long to run them in the usual "make check"
35 * sequence. Hence the value used for shipping is tiny.
37 #define MAX_SIZE 1024LL * 1024 * 16 * 1
42 * Number of put operations equivalent to 1/10th of MAX_SIZE
44 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
46 static char category[256];
48 static unsigned int hits[PUT_10 / 8 + 1];
50 static unsigned long long stored_bytes;
52 static unsigned long long stored_entries;
54 static unsigned long long stored_ops;
56 static const char *plugin_name;
74 struct GNUNET_TIME_Absolute start;
75 struct GNUNET_TIME_Absolute end;
76 const struct GNUNET_CONFIGURATION_Handle *cfg;
77 struct GNUNET_DATASTORE_PluginFunctions *api;
86 * Function called by plugins to notify us about a
87 * change in their disk utilization.
89 * @param cls closure (NULL)
90 * @param delta change in disk utilization,
91 * 0 for "reset to empty"
94 disk_utilization_change_cb (void *cls, int delta)
107 * @param key key for the item stored
108 * @param size size of the item stored
109 * @param status #GNUNET_OK or #GNUNET_SYSERROR
110 * @param msg error message on error
113 put_continuation (void *cls,
114 const struct GNUNET_HashCode *key,
119 struct CpsRunContext *crc = cls;
121 if (GNUNET_OK != status)
123 FPRINTF (stderr, "ERROR: `%s'\n", msg);
127 stored_bytes += size;
131 GNUNET_SCHEDULER_add_now (&test, crc);
136 do_put (struct CpsRunContext *crc)
140 static struct GNUNET_HashCode key;
145 crc->start = GNUNET_TIME_absolute_get ();
149 crc->end = GNUNET_TIME_absolute_get ();
151 printf ("%s took %s for %llu items\n", "Storing an item",
152 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
157 GAUGER (category, "Storing an item",
158 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / PUT_10,
162 crc->start = GNUNET_TIME_absolute_get ();
164 GNUNET_SCHEDULER_add_now (&test, crc);
167 /* most content is 32k */
169 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
170 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
171 size = size - (size & 7); /* always multiple of 8 */
173 /* generate random key */
174 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value_us;
175 GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &key);
176 memset (value, i, size);
178 memset (value, i - 255, size / 2);
180 GNUNET_memcpy (&value[4], &i, sizeof (i));
181 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
182 crc->api->put (crc->api->cls,
187 1 + i % 4 /* type */ ,
189 i % 4 /* anonymity */ ,
190 0 /* replication */ ,
191 GNUNET_TIME_relative_to_absolute
192 (GNUNET_TIME_relative_multiply
193 (GNUNET_TIME_UNIT_MILLISECONDS,
194 60 * 60 * 60 * 1000 +
195 GNUNET_CRYPTO_random_u32
196 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
204 iterate_zeros (void *cls,
205 const struct GNUNET_HashCode *key,
208 enum GNUNET_BLOCK_Type type,
211 uint32_t replication,
212 struct GNUNET_TIME_Absolute expiration,
215 struct CpsRunContext *crc = cls;
217 const char *cdata = data;
219 GNUNET_assert (key != NULL);
220 GNUNET_assert (size >= 8);
221 GNUNET_memcpy (&i, &cdata[4], sizeof (i));
222 hits[i / 8] |= (1 << (i % 8));
224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
225 "Found result %d type=%u, priority=%u, size=%u, expire=%s\n",
227 type, priority, size,
228 GNUNET_STRINGS_absolute_time_to_string (expiration));
230 if (crc->cnt == PUT_10 / 4 - 1)
235 for (i = 0; i < PUT_10; i++)
236 if (0 != (hits[i / 8] & (1 << (i % 8))))
239 crc->end = GNUNET_TIME_absolute_get ();
240 printf ("%s took %s yielding %u/%u items\n",
241 "Select random zero-anonymity item",
242 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
247 GAUGER (category, "Select random zero-anonymity item",
248 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
250 memset (hits, 0, sizeof (hits));
253 crc->start = GNUNET_TIME_absolute_get ();
255 GNUNET_SCHEDULER_add_now (&test, crc);
261 expiration_get (void *cls,
262 const struct GNUNET_HashCode *key,
265 enum GNUNET_BLOCK_Type type,
268 uint32_t replication,
269 struct GNUNET_TIME_Absolute expiration,
272 struct CpsRunContext *crc = cls;
274 const char *cdata = data;
276 GNUNET_assert (size >= 8);
277 GNUNET_memcpy (&i, &cdata[4], sizeof (i));
278 hits[i / 8] |= (1 << (i % 8));
280 if (PUT_10 <= crc->cnt)
285 for (i = 0; i < PUT_10; i++)
286 if (0 != (hits[i / 8] & (1 << (i % 8))))
289 crc->end = GNUNET_TIME_absolute_get ();
290 printf ("%s took %s yielding %u/%u items\n",
291 "Selecting and deleting by expiration",
292 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
295 bc, (unsigned int) PUT_10);
297 GAUGER (category, "Selecting and deleting by expiration",
298 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
300 memset (hits, 0, sizeof (hits));
301 if (++crc->iter == ITERATIONS)
306 crc->start = GNUNET_TIME_absolute_get ();
308 GNUNET_SCHEDULER_add_now (&test, crc);
314 replication_get (void *cls,
315 const struct GNUNET_HashCode *key,
318 enum GNUNET_BLOCK_Type type,
321 uint32_t replication,
322 struct GNUNET_TIME_Absolute expiration,
325 struct CpsRunContext *crc = cls;
327 const char *cdata = data;
329 GNUNET_assert (NULL != key);
330 GNUNET_assert (size >= 8);
331 GNUNET_memcpy (&i, &cdata[4], sizeof (i));
332 hits[i / 8] |= (1 << (i % 8));
334 if (PUT_10 <= crc->cnt)
339 for (i = 0; i < PUT_10; i++)
340 if (0 != (hits[i / 8] & (1 << (i % 8))))
343 crc->end = GNUNET_TIME_absolute_get ();
344 printf ("%s took %s yielding %u/%u items\n",
345 "Selecting random item for replication",
346 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
349 bc, (unsigned int) PUT_10);
351 GAUGER (category, "Selecting random item for replication",
352 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
354 memset (hits, 0, sizeof (hits));
358 crc->start = GNUNET_TIME_absolute_get ();
361 GNUNET_SCHEDULER_add_now (&test, crc);
367 * Function called when the service shuts
368 * down. Unloads our datastore plugin.
370 * @param api api to unload
371 * @param cfg configuration to use
374 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
375 const struct GNUNET_CONFIGURATION_Handle *cfg)
381 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
384 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
385 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
389 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
390 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
391 GNUNET_free (libname);
398 * Last task run during shutdown. Disconnects us from
399 * the transport and core.
402 cleaning_task (void *cls)
404 struct CpsRunContext *crc = cls;
406 unload_plugin (crc->api, crc->cfg);
414 struct CpsRunContext *crc = cls;
416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
417 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
422 crc->api->drop (crc->api->cls);
424 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
425 &cleaning_task, crc);
431 crc->api->get_replication (crc->api->cls, &replication_get, crc);
434 crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
435 &iterate_zeros, crc);
438 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
441 crc->api->drop (crc->api->cls);
443 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
444 &cleaning_task, crc);
451 * Load the datastore plugin.
453 static struct GNUNET_DATASTORE_PluginFunctions *
454 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
456 static struct GNUNET_DATASTORE_PluginEnvironment env;
457 struct GNUNET_DATASTORE_PluginFunctions *ret;
462 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
466 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
471 env.duc = &disk_utilization_change_cb;
473 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
475 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
476 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
478 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
480 GNUNET_free (libname);
483 GNUNET_free (libname);
490 run (void *cls, char *const *args, const char *cfgfile,
491 const struct GNUNET_CONFIGURATION_Handle *c)
493 struct GNUNET_DATASTORE_PluginFunctions *api;
494 struct CpsRunContext *crc;
501 api = load_plugin (c);
505 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
508 crc = GNUNET_new (struct CpsRunContext);
513 GNUNET_SCHEDULER_add_now (&test, crc);
518 main (int argc, char *argv[])
522 char *const xargv[] = {
523 "perf-plugin-datastore",
528 struct GNUNET_GETOPT_CommandLineOption options[] = {
529 GNUNET_GETOPT_OPTION_END
532 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
533 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
535 GNUNET_DISK_directory_remove (dir_name);
536 GNUNET_log_setup ("perf-plugin-datastore",
539 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
540 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
541 "perf_plugin_datastore_data_%s.conf", plugin_name);
542 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
543 "perf-plugin-datastore", "nohelp", options, &run, NULL);
545 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
546 GNUNET_DISK_directory_remove (dir_name);
551 /* end of perf_plugin_datastore.c */