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"
33 * Target datastore size (in bytes). Realistic sizes are
34 * more like 16 GB (not the default of 16 MB); however,
35 * those take too long to run them in the usual "make check"
36 * sequence. Hence the value used for shipping is tiny.
38 #define MAX_SIZE 1024LL * 1024 * 16 * 1
43 * Number of put operations equivalent to 1/10th of MAX_SIZE
45 #define PUT_10 (MAX_SIZE / 32 / 1024 / ITERATIONS)
47 static char category[256];
49 static unsigned int hits[PUT_10 / 8 + 1];
51 static unsigned long long stored_bytes;
53 static unsigned long long stored_entries;
55 static unsigned long long stored_ops;
57 static const char *plugin_name;
75 struct GNUNET_TIME_Absolute start;
76 struct GNUNET_TIME_Absolute end;
77 const struct GNUNET_CONFIGURATION_Handle *cfg;
78 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, int delta)
101 putValue (struct GNUNET_DATASTORE_PluginFunctions *api, int i, int k)
105 static GNUNET_HashCode key;
110 /* most content is 32k */
112 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
113 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
114 size = size - (size & 7); /* always multiple of 8 */
116 /* generate random key */
117 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value;
118 GNUNET_CRYPTO_hash (&key, sizeof (GNUNET_HashCode), &key);
119 memset (value, i, size);
121 memset (value, i - 255, size / 2);
123 memcpy (&value[4], &i, sizeof (i));
125 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
126 if (GNUNET_OK != api->put (api->cls, &key, size, value, 1 + i % 4 /* type */ ,
127 prio, i % 4 /* anonymity */ ,
128 0 /* replication */ ,
129 GNUNET_TIME_relative_to_absolute
130 (GNUNET_TIME_relative_multiply
131 (GNUNET_TIME_UNIT_MILLISECONDS,
132 60 * 60 * 60 * 1000 +
133 GNUNET_CRYPTO_random_u32
134 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))), &msg))
136 FPRINTF (stderr, "ERROR: `%s'\n", msg);
137 GNUNET_free_non_null (msg);
141 stored_bytes += size;
147 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
151 iterate_zeros (void *cls, const GNUNET_HashCode * key, uint32_t size,
152 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
153 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
156 struct CpsRunContext *crc = cls;
158 const char *cdata = data;
160 GNUNET_assert (key != NULL);
161 GNUNET_assert (size >= 8);
162 memcpy (&i, &cdata[4], sizeof (i));
163 hits[i / 8] |= (1 << (i % 8));
165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
166 "Found result type=%u, priority=%u, size=%u, expire=%llu\n",
167 type, priority, size, (unsigned long long) expiration.abs_value);
169 if (crc->cnt == PUT_10 / 4 - 1)
174 for (i = 0; i < PUT_10; i++)
175 if (0 != (hits[i / 8] & (1 << (i % 8))))
178 crc->end = GNUNET_TIME_absolute_get ();
179 printf ("%s took %llu ms yielding %u/%u items\n",
180 "Select random zero-anonymity item",
181 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
184 GAUGER (category, "Select random zero-anonymity item",
185 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
187 memset (hits, 0, sizeof (hits));
190 crc->start = GNUNET_TIME_absolute_get ();
192 GNUNET_SCHEDULER_add_now (&test, crc);
198 expiration_get (void *cls, const GNUNET_HashCode * key, uint32_t size,
199 const void *data, enum GNUNET_BLOCK_Type type,
200 uint32_t priority, uint32_t anonymity,
201 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
203 struct CpsRunContext *crc = cls;
205 const char *cdata = data;
207 GNUNET_assert (size >= 8);
208 memcpy (&i, &cdata[4], sizeof (i));
209 hits[i / 8] |= (1 << (i % 8));
211 if (PUT_10 <= crc->cnt)
216 for (i = 0; i < PUT_10; i++)
217 if (0 != (hits[i / 8] & (1 << (i % 8))))
220 crc->end = GNUNET_TIME_absolute_get ();
221 printf ("%s took %llu ms yielding %u/%u items\n",
222 "Selecting and deleting by expiration",
223 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
224 bc, (unsigned int) PUT_10);
226 GAUGER (category, "Selecting and deleting by expiration",
227 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
229 memset (hits, 0, sizeof (hits));
230 if (++crc->iter == ITERATIONS)
235 crc->start = GNUNET_TIME_absolute_get ();
237 GNUNET_SCHEDULER_add_now (&test, crc);
243 replication_get (void *cls, const GNUNET_HashCode * key, uint32_t size,
244 const void *data, enum GNUNET_BLOCK_Type type,
245 uint32_t priority, uint32_t anonymity,
246 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
248 struct CpsRunContext *crc = cls;
250 const char *cdata = data;
252 GNUNET_assert (NULL != key);
253 GNUNET_assert (size >= 8);
254 memcpy (&i, &cdata[4], sizeof (i));
255 hits[i / 8] |= (1 << (i % 8));
257 if (PUT_10 <= crc->cnt)
262 for (i = 0; i < PUT_10; i++)
263 if (0 != (hits[i / 8] & (1 << (i % 8))))
266 crc->end = GNUNET_TIME_absolute_get ();
267 printf ("%s took %llu ms yielding %u/%u items\n",
268 "Selecting random item for replication",
269 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
270 bc, (unsigned int) PUT_10);
272 GAUGER (category, "Selecting random item for replication",
273 (crc->end.abs_value - crc->start.abs_value) / crc->cnt,
275 memset (hits, 0, sizeof (hits));
279 crc->start = GNUNET_TIME_absolute_get ();
282 GNUNET_SCHEDULER_add_now (&test, crc);
288 * Function called when the service shuts
289 * down. Unloads our datastore plugin.
291 * @param api api to unload
292 * @param cfg configuration to use
295 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
296 const struct GNUNET_CONFIGURATION_Handle *cfg)
302 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
306 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
310 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
311 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
312 GNUNET_free (libname);
319 * Last task run during shutdown. Disconnects us from
320 * the transport and core.
323 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
325 struct CpsRunContext *crc = cls;
327 unload_plugin (crc->api, crc->cfg);
333 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
335 struct CpsRunContext *crc = cls;
338 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
341 crc->phase = RP_ERROR;
343 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
349 crc->api->drop (crc->api->cls);
351 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
352 &cleaning_task, crc);
355 crc->start = GNUNET_TIME_absolute_get ();
356 for (j = 0; j < PUT_10; j++)
357 putValue (crc->api, j, crc->i);
358 crc->end = GNUNET_TIME_absolute_get ();
360 printf ("%s took %llu ms for %llu items\n", "Storing an item",
361 (unsigned long long) (crc->end.abs_value - crc->start.abs_value),
364 GAUGER (category, "Storing an item",
365 (crc->end.abs_value - crc->start.abs_value) / PUT_10,
369 crc->start = GNUNET_TIME_absolute_get ();
371 GNUNET_SCHEDULER_add_now (&test, crc);
374 crc->api->get_replication (crc->api->cls, &replication_get, crc);
377 crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
378 &iterate_zeros, crc);
381 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
384 crc->api->drop (crc->api->cls);
386 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
387 &cleaning_task, crc);
394 * Load the datastore plugin.
396 static struct GNUNET_DATASTORE_PluginFunctions *
397 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
399 static struct GNUNET_DATASTORE_PluginEnvironment env;
400 struct GNUNET_DATASTORE_PluginFunctions *ret;
405 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
409 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
414 env.duc = &disk_utilization_change_cb;
416 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
418 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
419 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
421 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
424 GNUNET_free (libname);
431 run (void *cls, char *const *args, const char *cfgfile,
432 const struct GNUNET_CONFIGURATION_Handle *c)
434 struct GNUNET_DATASTORE_PluginFunctions *api;
435 struct CpsRunContext *crc;
437 api = load_plugin (c);
441 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
444 crc = GNUNET_malloc (sizeof (struct CpsRunContext));
449 GNUNET_SCHEDULER_add_now (&test, crc);
454 main (int argc, char *argv[])
459 char *const argv[] = {
460 "perf-plugin-datastore",
465 struct GNUNET_GETOPT_CommandLineOption options[] = {
466 GNUNET_GETOPT_OPTION_END
469 /* determine name of plugin to use */
470 plugin_name = argv[0];
471 while (NULL != (pos = strstr (plugin_name, "_")))
472 plugin_name = pos + 1;
473 if (NULL != (pos = strstr (plugin_name, ".")))
476 pos = (char *) plugin_name;
477 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
479 GNUNET_DISK_directory_remove (dir_name);
480 GNUNET_log_setup ("perf-plugin-datastore",
483 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
484 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
485 "perf_plugin_datastore_data_%s.conf", plugin_name);
486 GNUNET_PROGRAM_run ((sizeof (argv) / sizeof (char *)) - 1, argv,
487 "perf-plugin-datastore", "nohelp", options, &run, NULL);
489 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
490 if (pos != plugin_name)
492 GNUNET_DISK_directory_remove (dir_name);
497 /* end of perf_plugin_datastore.c */