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"
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)
102 putValue (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
106 static struct 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 (struct 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, &key, size, value, 1 + i % 4 /* type */ ,
128 prio, i % 4 /* anonymity */ ,
129 0 /* replication */ ,
130 GNUNET_TIME_relative_to_absolute
131 (GNUNET_TIME_relative_multiply
132 (GNUNET_TIME_UNIT_MILLISECONDS,
133 60 * 60 * 60 * 1000 +
134 GNUNET_CRYPTO_random_u32
135 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
137 FPRINTF (stderr, "ERROR: `%s'\n", msg);
138 GNUNET_free_non_null (msg);
142 stored_bytes += size;
148 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
152 iterate_zeros (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
153 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
154 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
157 struct CpsRunContext *crc = cls;
159 const char *cdata = data;
161 GNUNET_assert (key != NULL);
162 GNUNET_assert (size >= 8);
163 memcpy (&i, &cdata[4], sizeof (i));
164 hits[i / 8] |= (1 << (i % 8));
166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
167 "Found result %d type=%u, priority=%u, size=%u, expire=%llu\n",
169 type, priority, size, (unsigned long long) expiration.abs_value);
171 if (crc->cnt == PUT_10 / 4 - 1)
176 for (i = 0; i < PUT_10; i++)
177 if (0 != (hits[i / 8] & (1 << (i % 8))))
180 crc->end = GNUNET_TIME_absolute_get ();
181 printf ("%s took %llu ms yielding %u/%u items\n",
182 "Select random zero-anonymity item",
183 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
186 GAUGER (category, "Select random zero-anonymity item",
187 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
189 memset (hits, 0, sizeof (hits));
192 crc->start = GNUNET_TIME_absolute_get ();
194 GNUNET_SCHEDULER_add_now (&test, crc);
200 expiration_get (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
201 const void *data, enum GNUNET_BLOCK_Type type,
202 uint32_t priority, uint32_t anonymity,
203 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
205 struct CpsRunContext *crc = cls;
207 const char *cdata = data;
209 GNUNET_assert (size >= 8);
210 memcpy (&i, &cdata[4], sizeof (i));
211 hits[i / 8] |= (1 << (i % 8));
213 if (PUT_10 <= crc->cnt)
218 for (i = 0; i < PUT_10; i++)
219 if (0 != (hits[i / 8] & (1 << (i % 8))))
222 crc->end = GNUNET_TIME_absolute_get ();
223 printf ("%s took %llu ms yielding %u/%u items\n",
224 "Selecting and deleting by expiration",
225 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
226 bc, (unsigned int) PUT_10);
228 GAUGER (category, "Selecting and deleting by expiration",
229 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
231 memset (hits, 0, sizeof (hits));
232 if (++crc->iter == ITERATIONS)
237 crc->start = GNUNET_TIME_absolute_get ();
239 GNUNET_SCHEDULER_add_now (&test, crc);
245 replication_get (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
246 const void *data, enum GNUNET_BLOCK_Type type,
247 uint32_t priority, uint32_t anonymity,
248 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
250 struct CpsRunContext *crc = cls;
252 const char *cdata = data;
254 GNUNET_assert (NULL != key);
255 GNUNET_assert (size >= 8);
256 memcpy (&i, &cdata[4], sizeof (i));
257 hits[i / 8] |= (1 << (i % 8));
259 if (PUT_10 <= crc->cnt)
264 for (i = 0; i < PUT_10; i++)
265 if (0 != (hits[i / 8] & (1 << (i % 8))))
268 crc->end = GNUNET_TIME_absolute_get ();
269 printf ("%s took %llu ms yielding %u/%u items\n",
270 "Selecting random item for replication",
271 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
272 bc, (unsigned int) PUT_10);
274 GAUGER (category, "Selecting random item for replication",
275 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
277 memset (hits, 0, sizeof (hits));
281 crc->start = GNUNET_TIME_absolute_get ();
284 GNUNET_SCHEDULER_add_now (&test, crc);
290 * Function called when the service shuts
291 * down. Unloads our datastore plugin.
293 * @param api api to unload
294 * @param cfg configuration to use
297 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
298 const struct GNUNET_CONFIGURATION_Handle *cfg)
304 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
312 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
313 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
314 GNUNET_free (libname);
321 * Last task run during shutdown. Disconnects us from
322 * the transport and core.
325 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
327 struct CpsRunContext *crc = cls;
329 unload_plugin (crc->api, crc->cfg);
335 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
337 struct CpsRunContext *crc = cls;
340 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
343 crc->phase = RP_ERROR;
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
351 crc->api->drop (crc->api->cls);
353 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
354 &cleaning_task, crc);
357 crc->start = GNUNET_TIME_absolute_get ();
358 for (j = 0; j < PUT_10; j++)
359 putValue (crc->api, j, crc->i);
360 crc->end = GNUNET_TIME_absolute_get ();
362 printf ("%s took %llu ms for %llu items\n", "Storing an item",
363 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
366 GAUGER (category, "Storing an item",
367 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
371 crc->start = GNUNET_TIME_absolute_get ();
373 GNUNET_SCHEDULER_add_now (&test, crc);
376 crc->api->get_replication (crc->api->cls, &replication_get, crc);
379 crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
380 &iterate_zeros, crc);
383 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
386 crc->api->drop (crc->api->cls);
388 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
389 &cleaning_task, crc);
396 * Load the datastore plugin.
398 static struct GNUNET_DATASTORE_PluginFunctions *
399 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
401 static struct GNUNET_DATASTORE_PluginEnvironment env;
402 struct GNUNET_DATASTORE_PluginFunctions *ret;
407 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
416 env.duc = &disk_utilization_change_cb;
418 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
420 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
421 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
423 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
426 GNUNET_free (libname);
433 run (void *cls, char *const *args, const char *cfgfile,
434 const struct GNUNET_CONFIGURATION_Handle *c)
436 struct GNUNET_DATASTORE_PluginFunctions *api;
437 struct CpsRunContext *crc;
444 api = load_plugin (c);
448 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
451 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
456 GNUNET_SCHEDULER_add_now (&test, crc);
461 main (int argc, char *argv[])
465 char *const xargv[] = {
466 "perf-plugin-datastore",
471 struct GNUNET_GETOPT_CommandLineOption options[] = {
472 GNUNET_GETOPT_OPTION_END
475 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
476 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
478 GNUNET_DISK_directory_remove (dir_name);
479 GNUNET_log_setup ("perf-plugin-datastore",
482 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
483 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
484 "perf_plugin_datastore_data_%s.conf", plugin_name);
485 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
486 "perf-plugin-datastore", "nohelp", options, &run, NULL);
488 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
489 GNUNET_DISK_directory_remove (dir_name);
494 /* end of perf_plugin_datastore.c */