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;
87 * Function called by plugins to notify us about a
88 * change in their disk utilization.
90 * @param cls closure (NULL)
91 * @param delta change in disk utilization,
92 * 0 for "reset to empty"
95 disk_utilization_change_cb (void *cls,
102 putValue (struct GNUNET_DATASTORE_PluginFunctions * api, int i, int k)
106 static GNUNET_HashCode key;
111 /* most content is 32k */
113 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
114 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
115 size = size - (size & 7); /* always multiple of 8 */
117 /* generate random key */
118 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
119 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
120 memset (value, i, size);
122 memset (value, i - 255, size / 2);
124 memcpy (&value[4], &i, sizeof (i));
126 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
127 if (GNUNET_OK != api->put (api->cls,
131 1 + i % 4 /* type */,
133 i % 4 /* anonymity */,
135 GNUNET_TIME_relative_to_absolute
136 (GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS,
137 60 * 60 * 60 * 1000 +
138 GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
141 fprintf (stderr, "ERROR: `%s'\n", msg);
142 GNUNET_free_non_null (msg);
146 stored_bytes += size;
153 const struct GNUNET_SCHEDULER_TaskContext *tc);
157 iterate_zeros (void *cls,
159 const GNUNET_HashCode * key,
162 enum GNUNET_BLOCK_Type type,
165 struct GNUNET_TIME_Absolute
169 struct CpsRunContext *crc = cls;
171 const char *cdata = data;
179 for (i = 0;i<PUT_10;i++)
180 if (0 != (hits[i/8] & (1 << (i % 8))))
183 crc->end = GNUNET_TIME_absolute_get();
184 GNUNET_snprintf (buf, sizeof (buf),
185 "Iteration over %u zero-anonymity items",
187 printf ("%s took %llu ms yielding %u/%u items\n",
189 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
192 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
193 memset (hits, 0, sizeof (hits));
194 if ( (int) (PUT_10 / 4 - crc->cnt) > 2)
197 "Got %d items, expected %d\n",
198 (int) crc->cnt, (int) PUT_10 / 4);
200 crc->phase = RP_ERROR;
204 crc->start = GNUNET_TIME_absolute_get ();
205 GNUNET_SCHEDULER_add_now (&test, crc);
208 GNUNET_assert (size >= 8);
209 memcpy (&i, &cdata[4], sizeof (i));
210 hits[i/8] |= (1 << (i % 8));
213 fprintf (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
214 type, priority, size,
215 (unsigned long long) expiration.abs_value);
218 crc->api->next_request (next_cls,
225 expiration_get (void *cls,
227 const GNUNET_HashCode * key,
230 enum GNUNET_BLOCK_Type type,
233 struct GNUNET_TIME_Absolute
237 struct CpsRunContext *crc = cls;
239 const char *cdata = data;
241 GNUNET_assert (size >= 8);
242 memcpy (&i, &cdata[4], sizeof (i));
243 hits[i/8] |= (1 << (i % 8));
245 if (PUT_10 == crc->cnt)
251 for (i = 0;i<PUT_10;i++)
252 if (0 != (hits[i/8] & (1 << (i % 8))))
255 crc->end = GNUNET_TIME_absolute_get();
256 GNUNET_snprintf (buf, sizeof (buf),
257 "Execution of %u expiration+deletion-GET requests",
259 printf ("%s took %llu ms yielding %u/%u items\n",
261 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
263 (unsigned int) PUT_10);
264 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
265 memset (hits, 0, sizeof (hits));
268 crc->start = GNUNET_TIME_absolute_get ();
270 GNUNET_SCHEDULER_add_now (&test, crc);
276 replication_get (void *cls,
278 const GNUNET_HashCode * key,
281 enum GNUNET_BLOCK_Type type,
284 struct GNUNET_TIME_Absolute
288 struct CpsRunContext *crc = cls;
290 const char *cdata = data;
292 GNUNET_assert (NULL != key);
293 GNUNET_assert (size >= 8);
294 memcpy (&i, &cdata[4], sizeof (i));
295 hits[i/8] |= (1 << (i % 8));
297 if (PUT_10 == crc->cnt)
303 for (i = 0;i<PUT_10;i++)
304 if (0 != (hits[i/8] & (1 << (i % 8))))
307 crc->end = GNUNET_TIME_absolute_get();
308 GNUNET_snprintf (buf, sizeof (buf),
309 "Execution of %u replication-GET requests",
311 printf ("%s took %llu ms yielding %u/%u items\n",
313 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
315 (unsigned int) PUT_10);
316 GAUGER (category, buf, crc->end.abs_value - crc->start.abs_value, "ms");
317 memset (hits, 0, sizeof (hits));
320 crc->start = GNUNET_TIME_absolute_get ();
323 GNUNET_SCHEDULER_add_now (&test, crc);
329 * Function called when the service shuts
330 * down. Unloads our datastore plugin.
332 * @param api api to unload
333 * @param cfg configuration to use
336 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions * api,
337 const struct GNUNET_CONFIGURATION_Handle *cfg)
343 GNUNET_CONFIGURATION_get_value_string (cfg,
344 "DATASTORE", "DATABASE", &name))
346 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
347 _("No `%s' specified for `%s' in configuration!\n"),
352 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
353 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
354 GNUNET_free (libname);
361 * Last task run during shutdown. Disconnects us from
362 * the transport and core.
365 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
367 struct CpsRunContext *crc = cls;
369 unload_plugin (crc->api, crc->cfg);
376 const struct GNUNET_SCHEDULER_TaskContext *tc)
378 struct CpsRunContext *crc = cls;
381 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
382 crc->phase = RP_ERROR;
387 crc->api->drop (crc->api->cls);
389 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
390 &cleaning_task, crc);
393 crc->start = GNUNET_TIME_absolute_get ();
394 for (j=0;j<PUT_10;j++)
395 putValue (crc->api, j, crc->i);
396 crc->end = GNUNET_TIME_absolute_get ();
400 GNUNET_snprintf (buf, sizeof (buf),
401 "Execution of %u PUT requests",
403 printf ("%s took %llu ms\n",
405 (unsigned long long) (crc->end.abs_value - crc->start.abs_value));
407 buf, crc->end.abs_value - crc->start.abs_value, "ms");
410 crc->start = GNUNET_TIME_absolute_get ();
412 GNUNET_SCHEDULER_add_now (&test, crc);
415 crc->api->replication_get (crc->api->cls,
420 crc->api->iter_zero_anonymity (crc->api->cls, 1,
425 crc->api->expiration_get (crc->api->cls,
431 crc->api->drop (crc->api->cls);
433 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
434 &cleaning_task, crc);
441 * Load the datastore plugin.
443 static struct GNUNET_DATASTORE_PluginFunctions *
444 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
446 static struct GNUNET_DATASTORE_PluginEnvironment env;
447 struct GNUNET_DATASTORE_PluginFunctions * ret;
452 GNUNET_CONFIGURATION_get_value_string (cfg,
453 "DATASTORE", "DATABASE", &name))
455 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
456 _("No `%s' specified for `%s' in configuration!\n"),
462 env.duc = &disk_utilization_change_cb;
464 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
465 _("Loading `%s' datastore plugin\n"), name);
466 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
467 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
470 "Failed to load plugin `%s'!\n",
474 GNUNET_free (libname);
484 const struct GNUNET_CONFIGURATION_Handle *c)
486 struct GNUNET_DATASTORE_PluginFunctions *api;
487 struct CpsRunContext *crc;
489 api = load_plugin (c);
493 "Could not initialize plugin, assuming database not configured. Test not run!\n");
496 crc = GNUNET_malloc(sizeof(struct CpsRunContext));
501 GNUNET_SCHEDULER_add_now (&test, crc);
509 char *const argv[] = {
510 "perf-plugin-datastore",
518 struct GNUNET_GETOPT_CommandLineOption options[] = {
519 GNUNET_GETOPT_OPTION_END
522 GNUNET_snprintf (category, sizeof (category),
525 GNUNET_snprintf (cfg_name,
527 "perf_plugin_datastore_data_%s.conf",
529 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1,
530 argv, "perf-plugin-datastore", "nohelp",
531 options, &run, NULL);
533 fprintf (stderr, "Missed some testcases: %u\n", ok);
539 main (int argc, char *argv[])
545 /* determine name of plugin to use */
546 plugin_name = argv[0];
547 while (NULL != (pos = strstr(plugin_name, "_")))
549 if (NULL != (pos = strstr(plugin_name, ".")))
552 pos = (char *) plugin_name;
554 GNUNET_snprintf (dir_name,
556 "/tmp/perf-gnunet-datastore-%s",
558 GNUNET_DISK_directory_remove (dir_name);
559 GNUNET_log_setup ("perf-plugin-datastore",
567 if (pos != plugin_name)
569 GNUNET_DISK_directory_remove (dir_name);
574 /* end of perf_plugin_datastore.c */