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 * 128
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;
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,
103 putValue (struct GNUNET_DATASTORE_PluginFunctions * api, int i, int k)
107 static GNUNET_HashCode key;
112 /* most content is 32k */
114 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
115 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
116 size = size - (size & 7); /* always multiple of 8 */
118 /* generate random key */
119 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
120 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
121 memset (value, i, size);
123 memset (value, i - 255, size / 2);
125 memcpy (&value[4], &i, sizeof (i));
127 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
128 if (GNUNET_OK != api->put (api->cls,
132 1 + i % 4 /* type */,
134 i % 4 /* anonymity */,
136 GNUNET_TIME_relative_to_absolute
137 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
138 60 * 60 * 60 * 1000 +
139 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
142 fprintf (stderr, "ERROR: `%s'\n", msg);
143 GNUNET_free_non_null (msg);
147 stored_bytes += size;
154 const struct GNUNET_SCHEDULER_TaskContext *tc);
158 iterate_zeros (void *cls,
160 const GNUNET_HashCode * key,
163 enum GNUNET_BLOCK_Type type,
166 struct GNUNET_TIME_Absolute
170 struct CpsRunContext *crc = cls;
172 const char *cdata = data;
180 for (i = 0;i<PUT_10;i++)
181 if (0 != (hits[i/8] & (1 << (i % 8))))
184 crc->end = GNUNET_TIME_absolute_get();
185 GNUNET_snprintf (buf, sizeof (buf),
186 "Iteration over %u zero-anonymity items",
188 printf ("%s took %llu ms yielding %u/%u items\n",
190 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
193 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
194 memset (hits, 0, sizeof (hits));
195 if ( (int) (PUT_10 / 4 - crc->cnt) > 2)
198 "Got %d items, expected %d\n",
199 (int) crc->cnt, (int) PUT_10 / 4);
201 crc->phase = RP_ERROR;
207 crc->start = GNUNET_TIME_absolute_get ();
209 GNUNET_SCHEDULER_add_now (&test, crc);
212 GNUNET_assert (size >= 8);
213 memcpy (&i, &cdata[4], sizeof (i));
214 hits[i/8] |= (1 << (i % 8));
217 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
218 type, priority, size,
219 (unsigned long long) expiration.abs_value);
222 crc->api->next_request (next_cls,
229 expiration_get (void *cls,
231 const GNUNET_HashCode * key,
234 enum GNUNET_BLOCK_Type type,
237 struct GNUNET_TIME_Absolute
241 struct CpsRunContext *crc = cls;
243 const char *cdata = data;
245 GNUNET_assert (size >= 8);
246 memcpy (&i, &cdata[4], sizeof (i));
247 hits[i/8] |= (1 << (i % 8));
249 if (PUT_10 <= crc->cnt)
255 for (i = 0;i<PUT_10;i++)
256 if (0 != (hits[i/8] & (1 << (i % 8))))
259 crc->end = GNUNET_TIME_absolute_get();
260 GNUNET_snprintf (buf, sizeof (buf),
261 "Execution of %u expiration+deletion-GET requests",
263 printf ("%s took %llu ms yielding %u/%u items\n",
265 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
267 (unsigned int) PUT_10);
268 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
269 memset (hits, 0, sizeof (hits));
270 if (++crc->iter == ITERATIONS)
275 crc->start = GNUNET_TIME_absolute_get ();
277 GNUNET_SCHEDULER_add_now (&test, crc);
283 replication_get (void *cls,
285 const GNUNET_HashCode * key,
288 enum GNUNET_BLOCK_Type type,
291 struct GNUNET_TIME_Absolute
295 struct CpsRunContext *crc = cls;
297 const char *cdata = data;
299 GNUNET_assert (NULL != key);
300 GNUNET_assert (size >= 8);
301 memcpy (&i, &cdata[4], sizeof (i));
302 hits[i/8] |= (1 << (i % 8));
304 if (PUT_10 <= crc->cnt)
310 for (i = 0;i<PUT_10;i++)
311 if (0 != (hits[i/8] & (1 << (i % 8))))
314 crc->end = GNUNET_TIME_absolute_get();
315 GNUNET_snprintf (buf, sizeof (buf),
316 "Execution of %u replication-GET requests",
318 printf ("%s took %llu ms yielding %u/%u items\n",
320 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
322 (unsigned int) PUT_10);
323 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
324 memset (hits, 0, sizeof (hits));
327 crc->start = GNUNET_TIME_absolute_get ();
330 GNUNET_SCHEDULER_add_now (&test, crc);
336 * Function called when the service shuts
337 * down. Unloads our datastore plugin.
339 * @param api api to unload
340 * @param cfg configuration to use
343 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
344 const struct GNUNET_CONFIGURATION_Handle *cfg)
350 GNUNET_CONFIGURATION_get_value_string (cfg,
351 "DATASTORE", "DATABASE", &name))
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354 _("No `%s' specified for `%s' in configuration!\n"),
359 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
360 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
361 GNUNET_free (libname);
368 * Last task run during shutdown. Disconnects us from
369 * the transport and core.
372 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
374 struct CpsRunContext *crc = cls;
376 unload_plugin (crc->api, crc->cfg);
383 const struct GNUNET_SCHEDULER_TaskContext *tc)
385 struct CpsRunContext *crc = cls;
388 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
389 crc->phase = RP_ERROR;
394 crc->api->drop (crc->api->cls);
396 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
397 &cleaning_task, crc);
400 crc->start = GNUNET_TIME_absolute_get ();
401 for (j=0;j<PUT_10;j++)
402 putValue (crc->api, j, crc->i);
403 crc->end = GNUNET_TIME_absolute_get ();
407 GNUNET_snprintf (buf, sizeof (buf),
408 "Execution of %u PUT requests",
410 printf ("%s took %llu ms\n",
412 (unsigned long long) (crc->end.abs_value - crc->start.abs_value));
414 buf, crc->end.abs_value - crc->start.abs_value, "ms");
417 crc->start = GNUNET_TIME_absolute_get ();
419 GNUNET_SCHEDULER_add_now (&test, crc);
422 crc->api->replication_get (crc->api->cls,
427 crc->api->iter_zero_anonymity (crc->api->cls, 1,
432 crc->api->expiration_get (crc->api->cls,
438 crc->api->drop (crc->api->cls);
440 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
441 &cleaning_task, crc);
448 * Load the datastore plugin.
450 static struct GNUNET_DATASTORE_PluginFunctions *
451 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
453 static struct GNUNET_DATASTORE_PluginEnvironment env;
454 struct GNUNET_DATASTORE_PluginFunctions * ret;
459 GNUNET_CONFIGURATION_get_value_string (cfg,
460 "DATASTORE", "DATABASE", &name))
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
463 _("No `%s' specified for `%s' in configuration!\n"),
469 env.duc = &disk_utilization_change_cb;
471 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
472 _("Loading `%s' datastore plugin\n"), name);
473 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
474 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
477 "Failed to load plugin `%s'!\n",
481 GNUNET_free (libname);
491 const struct GNUNET_CONFIGURATION_Handle *c)
493 struct GNUNET_DATASTORE_PluginFunctions *api;
494 struct CpsRunContext *crc;
496 api = load_plugin (c);
500 "Could not initialize plugin, assuming database not configured. Test not run!\n");
503 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
508 GNUNET_SCHEDULER_add_now (&test, crc);
516 char *const argv[] = {
517 "perf-plugin-datastore",
525 struct GNUNET_GETOPT_CommandLineOption options[] = {
526 GNUNET_GETOPT_OPTION_END
529 GNUNET_snprintf (category, sizeof (category),
532 GNUNET_snprintf (cfg_name,
534 "perf_plugin_datastore_data_%s.conf",
536 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
537 argv, "perf-plugin-datastore", "nohelp",
538 options, &run, NULL);
540 fprintf (stderr, "Missed some testcases: %u\n", ok);
546 main (int argc, char *argv[])
553 /* determine name of plugin to use */
554 plugin_name = argv[0];
555 while (NULL != (pos = strstr(plugin_name, "_")))
557 if (NULL != (pos = strstr(plugin_name, ".")))
560 pos = (char *) plugin_name;
562 GNUNET_snprintf (dir_name,
564 "/tmp/perf-gnunet-datastore-%s",
566 GNUNET_DISK_directory_remove (dir_name);
567 GNUNET_log_setup ("perf-plugin-datastore",
575 if (pos != plugin_name)
577 GNUNET_DISK_directory_remove (dir_name);
582 /* end of perf_plugin_datastore.c */