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 * 16 * 1
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, int delta)
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, &key, size, value, 1 + i % 4 /* type */ ,
129 prio, i % 4 /* anonymity */ ,
130 0 /* replication */ ,
131 GNUNET_TIME_relative_to_absolute
132 (GNUNET_TIME_relative_multiply
133 (GNUNET_TIME_UNIT_MILLISECONDS,
134 60 * 60 * 60 * 1000 +
135 GNUNET_CRYPTO_random_u32
136 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
138 FPRINTF (stderr, "ERROR: `%s'\n", msg);
139 GNUNET_free_non_null (msg);
143 stored_bytes += size;
149 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
153 iterate_zeros (void *cls, const GNUNET_HashCode * key, uint32_t size,
154 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
155 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
158 struct CpsRunContext *crc = cls;
160 const char *cdata = data;
162 GNUNET_assert (key != NULL);
163 GNUNET_assert (size >= 8);
164 memcpy (&i, &cdata[4], sizeof (i));
165 hits[i / 8] |= (1 << (i % 8));
168 FPRINTF (stderr, "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
169 type, priority, size, (unsigned long long) expiration.abs_value);
172 if (crc->cnt == PUT_10 / 4 - 1)
177 for (i = 0; i < PUT_10; i++)
178 if (0 != (hits[i / 8] & (1 << (i % 8))))
181 crc->end = GNUNET_TIME_absolute_get ();
182 printf ("%s took %llu ms yielding %u/%u items\n",
183 "Select random zero-anonymity item",
184 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
187 GAUGER (category, "Select random zero-anonymity item",
188 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
190 memset (hits, 0, sizeof (hits));
193 crc->start = GNUNET_TIME_absolute_get ();
195 GNUNET_SCHEDULER_add_now (&test, crc);
201 expiration_get (void *cls, const GNUNET_HashCode * key, uint32_t size,
202 const void *data, enum GNUNET_BLOCK_Type type,
203 uint32_t priority, uint32_t anonymity,
204 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
206 struct CpsRunContext *crc = cls;
208 const char *cdata = data;
210 GNUNET_assert (size >= 8);
211 memcpy (&i, &cdata[4], sizeof (i));
212 hits[i / 8] |= (1 << (i % 8));
214 if (PUT_10 <= crc->cnt)
219 for (i = 0; i < PUT_10; i++)
220 if (0 != (hits[i / 8] & (1 << (i % 8))))
223 crc->end = GNUNET_TIME_absolute_get ();
224 printf ("%s took %llu ms yielding %u/%u items\n",
225 "Selecting and deleting by expiration",
226 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
227 bc, (unsigned int) PUT_10);
229 GAUGER (category, "Selecting and deleting by expiration",
230 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
232 memset (hits, 0, sizeof (hits));
233 if (++crc->iter == ITERATIONS)
238 crc->start = GNUNET_TIME_absolute_get ();
240 GNUNET_SCHEDULER_add_now (&test, crc);
246 replication_get (void *cls, const GNUNET_HashCode * key, uint32_t size,
247 const void *data, enum GNUNET_BLOCK_Type type,
248 uint32_t priority, uint32_t anonymity,
249 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
251 struct CpsRunContext *crc = cls;
253 const char *cdata = data;
255 GNUNET_assert (NULL != key);
256 GNUNET_assert (size >= 8);
257 memcpy (&i, &cdata[4], sizeof (i));
258 hits[i / 8] |= (1 << (i % 8));
260 if (PUT_10 <= crc->cnt)
265 for (i = 0; i < PUT_10; i++)
266 if (0 != (hits[i / 8] & (1 << (i % 8))))
269 crc->end = GNUNET_TIME_absolute_get ();
270 printf ("%s took %llu ms yielding %u/%u items\n",
271 "Selecting random item for replication",
272 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
273 bc, (unsigned int) PUT_10);
275 GAUGER (category, "Selecting random item for replication",
276 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
278 memset (hits, 0, sizeof (hits));
282 crc->start = GNUNET_TIME_absolute_get ();
285 GNUNET_SCHEDULER_add_now (&test, crc);
291 * Function called when the service shuts
292 * down. Unloads our datastore plugin.
294 * @param api api to unload
295 * @param cfg configuration to use
298 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
299 const struct GNUNET_CONFIGURATION_Handle *cfg)
305 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
309 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
313 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
314 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
315 GNUNET_free (libname);
322 * Last task run during shutdown. Disconnects us from
323 * the transport and core.
326 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
328 struct CpsRunContext *crc = cls;
330 unload_plugin (crc->api, crc->cfg);
336 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 struct CpsRunContext *crc = cls;
341 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
344 crc->phase = RP_ERROR;
347 FPRINTF (stderr, "In phase %d, iteration %u\n", crc->phase, crc->cnt);
353 crc->api->drop (crc->api->cls);
355 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
356 &cleaning_task, crc);
359 crc->start = GNUNET_TIME_absolute_get ();
360 for (j = 0; j < PUT_10; j++)
361 putValue (crc->api, j, crc->i);
362 crc->end = GNUNET_TIME_absolute_get ();
364 printf ("%s took %llu ms for %llu items\n", "Storing an item",
365 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
368 GAUGER (category, "Storing an item",
369 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
373 crc->start = GNUNET_TIME_absolute_get ();
375 GNUNET_SCHEDULER_add_now (&test, crc);
378 crc->api->get_replication (crc->api->cls, &replication_get, crc);
381 crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
382 &iterate_zeros, crc);
385 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
388 crc->api->drop (crc->api->cls);
390 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
391 &cleaning_task, crc);
398 * Load the datastore plugin.
400 static struct GNUNET_DATASTORE_PluginFunctions *
401 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
403 static struct GNUNET_DATASTORE_PluginEnvironment env;
404 struct GNUNET_DATASTORE_PluginFunctions *ret;
409 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
412 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
413 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
418 env.duc = &disk_utilization_change_cb;
420 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
422 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
423 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
425 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
428 GNUNET_free (libname);
435 run (void *cls, char *const *args, const char *cfgfile,
436 const struct GNUNET_CONFIGURATION_Handle *c)
438 struct GNUNET_DATASTORE_PluginFunctions *api;
439 struct CpsRunContext *crc;
441 api = load_plugin (c);
445 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
448 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
453 GNUNET_SCHEDULER_add_now (&test, crc);
462 char *const argv[] = {
463 "perf-plugin-datastore",
471 struct GNUNET_GETOPT_CommandLineOption options[] = {
472 GNUNET_GETOPT_OPTION_END
475 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
476 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
477 "perf_plugin_datastore_data_%s.conf", plugin_name);
478 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
479 "perf-plugin-datastore", "nohelp", options, &run, NULL);
481 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
487 main (int argc, char *argv[])
494 /* determine name of plugin to use */
495 plugin_name = argv[0];
496 while (NULL != (pos = strstr (plugin_name, "_")))
497 plugin_name = pos + 1;
498 if (NULL != (pos = strstr (plugin_name, ".")))
501 pos = (char *) plugin_name;
503 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
505 GNUNET_DISK_directory_remove (dir_name);
506 GNUNET_log_setup ("perf-plugin-datastore",
514 if (pos != plugin_name)
516 GNUNET_DISK_directory_remove (dir_name);
521 /* end of perf_plugin_datastore.c */