2 This file is part of GNUnet.
3 (C) 2004, 2005, 2006, 2007, 2009, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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"
32 #define VERBOSE GNUNET_NO
35 * Target datastore size (in bytes). Realistic sizes are
36 * more like 16 GB (not the default of 16 MB); however,
37 * those take too long to run them in the usual "make check"
38 * sequence. Hence the value used for shipping is tiny.
40 #define MAX_SIZE 1024LL * 1024 * 32
45 * Number of put operations equivalent to 1/10th of MAX_SIZE
47 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
49 static char category[256];
51 static unsigned int hits[PUT_10 / 8 + 1];
53 static unsigned long long stored_bytes;
55 static unsigned long long stored_entries;
57 static unsigned long long stored_ops;
59 static const char *plugin_name;
77 struct GNUNET_TIME_Absolute start;
78 struct GNUNET_TIME_Absolute end;
79 const struct GNUNET_CONFIGURATION_Handle *cfg;
80 struct GNUNET_DATASTORE_PluginFunctions * api;
89 * Function called by plugins to notify us about a
90 * change in their disk utilization.
92 * @param cls closure (NULL)
93 * @param delta change in disk utilization,
94 * 0 for "reset to empty"
97 disk_utilization_change_cb (void *cls,
104 putValue (struct GNUNET_DATASTORE_PluginFunctions * api,
109 static GNUNET_HashCode key;
114 /* most content is 32k */
116 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
117 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
118 size = size - (size & 7); /* always multiple of 8 */
120 /* generate random key */
121 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
122 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
123 memset (value, i, size);
125 memset (value, i - 255, size / 2);
127 memcpy (&value[4], &i, sizeof (i));
129 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
130 if (GNUNET_OK != api->put (api->cls,
134 1 + i % 4 /* type */,
136 i % 4 /* anonymity */,
138 GNUNET_TIME_relative_to_absolute
139 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
140 60 * 60 * 60 * 1000 +
141 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
144 fprintf (stderr, "ERROR: `%s'\n", msg);
145 GNUNET_free_non_null (msg);
149 stored_bytes += size;
156 const struct GNUNET_SCHEDULER_TaskContext *tc);
160 iterate_zeros (void *cls,
161 const GNUNET_HashCode * key,
164 enum GNUNET_BLOCK_Type type,
167 struct GNUNET_TIME_Absolute
171 struct CpsRunContext *crc = cls;
173 const char *cdata = data;
175 GNUNET_assert (key != NULL);
176 GNUNET_assert (size >= 8);
177 memcpy (&i, &cdata[4], sizeof (i));
178 hits[i/8] |= (1 << (i % 8));
181 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
182 type, priority, size,
183 (unsigned long long) expiration.abs_value);
186 if (crc->cnt == PUT_10 / 4 - 1)
192 for (i = 0;i<PUT_10;i++)
193 if (0 != (hits[i/8] & (1 << (i % 8))))
196 crc->end = GNUNET_TIME_absolute_get();
197 GNUNET_snprintf (buf, sizeof (buf),
198 "Iteration over %u zero-anonymity items",
200 printf ("%s took %llu ms yielding %u/%u items\n",
202 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
205 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
206 memset (hits, 0, sizeof (hits));
209 crc->start = GNUNET_TIME_absolute_get ();
211 GNUNET_SCHEDULER_add_now (&test, crc);
217 expiration_get (void *cls,
218 const GNUNET_HashCode * key,
221 enum GNUNET_BLOCK_Type type,
224 struct GNUNET_TIME_Absolute
228 struct CpsRunContext *crc = cls;
230 const char *cdata = data;
232 GNUNET_assert (size >= 8);
233 memcpy (&i, &cdata[4], sizeof (i));
234 hits[i/8] |= (1 << (i % 8));
236 if (PUT_10 <= crc->cnt)
242 for (i = 0;i<PUT_10;i++)
243 if (0 != (hits[i/8] & (1 << (i % 8))))
246 crc->end = GNUNET_TIME_absolute_get();
247 GNUNET_snprintf (buf, sizeof (buf),
248 "Execution of %u expiration+deletion-GET requests",
250 printf ("%s took %llu ms yielding %u/%u items\n",
252 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
254 (unsigned int) PUT_10);
255 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
256 memset (hits, 0, sizeof (hits));
257 if (++crc->iter == ITERATIONS)
262 crc->start = GNUNET_TIME_absolute_get ();
264 GNUNET_SCHEDULER_add_now (&test, crc);
270 replication_get (void *cls,
271 const GNUNET_HashCode * key,
274 enum GNUNET_BLOCK_Type type,
277 struct GNUNET_TIME_Absolute
281 struct CpsRunContext *crc = cls;
283 const char *cdata = data;
285 GNUNET_assert (NULL != key);
286 GNUNET_assert (size >= 8);
287 memcpy (&i, &cdata[4], sizeof (i));
288 hits[i/8] |= (1 << (i % 8));
290 if (PUT_10 <= crc->cnt)
296 for (i = 0;i<PUT_10;i++)
297 if (0 != (hits[i/8] & (1 << (i % 8))))
300 crc->end = GNUNET_TIME_absolute_get();
301 GNUNET_snprintf (buf, sizeof (buf),
302 "Execution of %u replication-GET requests",
304 printf ("%s took %llu ms yielding %u/%u items\n",
306 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
308 (unsigned int) PUT_10);
309 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
310 memset (hits, 0, sizeof (hits));
314 crc->start = GNUNET_TIME_absolute_get ();
317 GNUNET_SCHEDULER_add_now (&test, crc);
323 * Function called when the service shuts
324 * down. Unloads our datastore plugin.
326 * @param api api to unload
327 * @param cfg configuration to use
330 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
331 const struct GNUNET_CONFIGURATION_Handle *cfg)
337 GNUNET_CONFIGURATION_get_value_string (cfg,
338 "DATASTORE", "DATABASE", &name))
340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
341 _("No `%s' specified for `%s' in configuration!\n"),
346 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
347 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
348 GNUNET_free (libname);
355 * Last task run during shutdown. Disconnects us from
356 * the transport and core.
359 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
361 struct CpsRunContext *crc = cls;
363 unload_plugin (crc->api, crc->cfg);
370 const struct GNUNET_SCHEDULER_TaskContext *tc)
372 struct CpsRunContext *crc = cls;
375 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
378 crc->phase = RP_ERROR;
381 fprintf (stderr, "In phase %d, iteration %u\n",
389 crc->api->drop (crc->api->cls);
391 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
392 &cleaning_task, crc);
395 crc->start = GNUNET_TIME_absolute_get ();
396 for (j=0;j<PUT_10;j++)
397 putValue (crc->api, j, crc->i);
398 crc->end = GNUNET_TIME_absolute_get ();
402 GNUNET_snprintf (buf, sizeof (buf),
403 "Execution of %u PUT requests",
405 printf ("%s took %llu ms\n",
407 (unsigned long long) (crc->end.abs_value - crc->start.abs_value));
409 buf, crc->end.abs_value - crc->start.abs_value, "ms");
412 crc->start = GNUNET_TIME_absolute_get ();
414 GNUNET_SCHEDULER_add_now (&test, crc);
417 crc->api->get_replication (crc->api->cls,
422 crc->api->get_zero_anonymity (crc->api->cls,
429 crc->api->get_expiration (crc->api->cls,
435 crc->api->drop (crc->api->cls);
437 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
438 &cleaning_task, crc);
445 * Load the datastore plugin.
447 static struct GNUNET_DATASTORE_PluginFunctions *
448 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
450 static struct GNUNET_DATASTORE_PluginEnvironment env;
451 struct GNUNET_DATASTORE_PluginFunctions * ret;
456 GNUNET_CONFIGURATION_get_value_string (cfg,
457 "DATASTORE", "DATABASE", &name))
459 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
460 _("No `%s' specified for `%s' in configuration!\n"),
466 env.duc = &disk_utilization_change_cb;
468 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
469 _("Loading `%s' datastore plugin\n"), name);
470 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
471 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
474 "Failed to load plugin `%s'!\n",
478 GNUNET_free (libname);
488 const struct GNUNET_CONFIGURATION_Handle *c)
490 struct GNUNET_DATASTORE_PluginFunctions *api;
491 struct CpsRunContext *crc;
493 api = load_plugin (c);
497 "Could not initialize plugin, assuming database not configured. Test not run!\n");
500 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
505 GNUNET_SCHEDULER_add_now (&test, crc);
513 char *const argv[] = {
514 "perf-plugin-datastore",
522 struct GNUNET_GETOPT_CommandLineOption options[] = {
523 GNUNET_GETOPT_OPTION_END
526 GNUNET_snprintf (category, sizeof (category),
529 GNUNET_snprintf (cfg_name,
531 "perf_plugin_datastore_data_%s.conf",
533 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
534 argv, "perf-plugin-datastore", "nohelp",
535 options, &run, NULL);
537 fprintf (stderr, "Missed some testcases: %u\n", ok);
543 main (int argc, char *argv[])
549 /* determine name of plugin to use */
550 plugin_name = argv[0];
551 while (NULL != (pos = strstr(plugin_name, "_")))
553 if (NULL != (pos = strstr(plugin_name, ".")))
556 pos = (char *) plugin_name;
558 GNUNET_snprintf (dir_name,
560 "/tmp/perf-gnunet-datastore-%s",
562 GNUNET_DISK_directory_remove (dir_name);
563 GNUNET_log_setup ("perf-plugin-datastore",
571 if (pos != plugin_name)
573 GNUNET_DISK_directory_remove (dir_name);
578 /* end of perf_plugin_datastore.c */