2 This file is part of GNUnet.
3 Copyright (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 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
106 put_continuation (void *cls, const struct GNUNET_HashCode *key,
107 uint32_t size, int status, char *msg)
109 struct CpsRunContext *crc = cls;
111 if (GNUNET_OK != status)
113 FPRINTF (stderr, "ERROR: `%s'\n", msg);
117 stored_bytes += size;
121 GNUNET_SCHEDULER_add_now (&test, crc);
125 do_put (struct CpsRunContext *crc)
129 static struct GNUNET_HashCode key;
134 crc->start = GNUNET_TIME_absolute_get ();
138 crc->end = GNUNET_TIME_absolute_get ();
140 printf ("%s took %s for %llu items\n", "Storing an item",
141 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
146 GAUGER (category, "Storing an item",
147 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / PUT_10,
151 crc->start = GNUNET_TIME_absolute_get ();
153 GNUNET_SCHEDULER_add_now (&test, crc);
156 /* most content is 32k */
158 if (GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 16) == 0) /* but some of it is less! */
159 size = 8 + GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 32 * 1024);
160 size = size - (size & 7); /* always multiple of 8 */
162 /* generate random key */
163 key.bits[0] = (unsigned int) GNUNET_TIME_absolute_get ().abs_value_us;
164 GNUNET_CRYPTO_hash (&key, sizeof (struct GNUNET_HashCode), &key);
165 memset (value, i, size);
167 memset (value, i - 255, size / 2);
169 memcpy (&value[4], &i, sizeof (i));
170 prio = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_WEAK, 100);
171 crc->api->put (crc->api->cls, &key, size, value, 1 + i % 4 /* type */ ,
172 prio, i % 4 /* anonymity */ ,
173 0 /* replication */ ,
174 GNUNET_TIME_relative_to_absolute
175 (GNUNET_TIME_relative_multiply
176 (GNUNET_TIME_UNIT_MILLISECONDS,
177 60 * 60 * 60 * 1000 +
178 GNUNET_CRYPTO_random_u32
179 (GNUNET_CRYPTO_QUALITY_WEAK, 1000))),
180 put_continuation, crc);
186 iterate_zeros (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
187 const void *data, enum GNUNET_BLOCK_Type type, uint32_t priority,
188 uint32_t anonymity, struct GNUNET_TIME_Absolute expiration,
191 struct CpsRunContext *crc = cls;
193 const char *cdata = data;
195 GNUNET_assert (key != NULL);
196 GNUNET_assert (size >= 8);
197 memcpy (&i, &cdata[4], sizeof (i));
198 hits[i / 8] |= (1 << (i % 8));
200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
201 "Found result %d type=%u, priority=%u, size=%u, expire=%s\n",
203 type, priority, size,
204 GNUNET_STRINGS_absolute_time_to_string (expiration));
206 if (crc->cnt == PUT_10 / 4 - 1)
211 for (i = 0; i < PUT_10; i++)
212 if (0 != (hits[i / 8] & (1 << (i % 8))))
215 crc->end = GNUNET_TIME_absolute_get ();
216 printf ("%s took %s yielding %u/%u items\n",
217 "Select random zero-anonymity item",
218 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
223 GAUGER (category, "Select random zero-anonymity item",
224 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
226 memset (hits, 0, sizeof (hits));
229 crc->start = GNUNET_TIME_absolute_get ();
231 GNUNET_SCHEDULER_add_now (&test, crc);
237 expiration_get (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
238 const void *data, enum GNUNET_BLOCK_Type type,
239 uint32_t priority, uint32_t anonymity,
240 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
242 struct CpsRunContext *crc = cls;
244 const char *cdata = data;
246 GNUNET_assert (size >= 8);
247 memcpy (&i, &cdata[4], sizeof (i));
248 hits[i / 8] |= (1 << (i % 8));
250 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 printf ("%s took %s yielding %u/%u items\n",
261 "Selecting and deleting by expiration",
262 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
265 bc, (unsigned int) PUT_10);
267 GAUGER (category, "Selecting and deleting by expiration",
268 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
270 memset (hits, 0, sizeof (hits));
271 if (++crc->iter == ITERATIONS)
276 crc->start = GNUNET_TIME_absolute_get ();
278 GNUNET_SCHEDULER_add_now (&test, crc);
284 replication_get (void *cls, const struct GNUNET_HashCode * key, uint32_t size,
285 const void *data, enum GNUNET_BLOCK_Type type,
286 uint32_t priority, uint32_t anonymity,
287 struct GNUNET_TIME_Absolute expiration, uint64_t uid)
289 struct CpsRunContext *crc = cls;
291 const char *cdata = data;
293 GNUNET_assert (NULL != key);
294 GNUNET_assert (size >= 8);
295 memcpy (&i, &cdata[4], sizeof (i));
296 hits[i / 8] |= (1 << (i % 8));
298 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 printf ("%s took %s yielding %u/%u items\n",
309 "Selecting random item for replication",
310 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_difference (crc->start,
313 bc, (unsigned int) PUT_10);
315 GAUGER (category, "Selecting random item for replication",
316 (crc->end.abs_value_us - crc->start.abs_value_us) / 1000LL / crc->cnt,
318 memset (hits, 0, sizeof (hits));
322 crc->start = GNUNET_TIME_absolute_get ();
325 GNUNET_SCHEDULER_add_now (&test, crc);
331 * Function called when the service shuts
332 * down. Unloads our datastore plugin.
334 * @param api api to unload
335 * @param cfg configuration to use
338 unload_plugin (struct GNUNET_DATASTORE_PluginFunctions *api,
339 const struct GNUNET_CONFIGURATION_Handle *cfg)
345 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
349 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
353 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
354 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
355 GNUNET_free (libname);
362 * Last task run during shutdown. Disconnects us from
363 * the transport and core.
366 cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
368 struct CpsRunContext *crc = cls;
370 unload_plugin (crc->api, crc->cfg);
376 test (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
378 struct CpsRunContext *crc = cls;
380 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
383 crc->phase = RP_ERROR;
385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
386 "In phase %d, iteration %u\n", crc->phase, crc->cnt);
391 crc->api->drop (crc->api->cls);
393 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
394 &cleaning_task, crc);
400 crc->api->get_replication (crc->api->cls, &replication_get, crc);
403 crc->api->get_zero_anonymity (crc->api->cls, crc->offset++, 1,
404 &iterate_zeros, crc);
407 crc->api->get_expiration (crc->api->cls, &expiration_get, crc);
410 crc->api->drop (crc->api->cls);
412 GNUNET_SCHEDULER_add_with_priority (GNUNET_SCHEDULER_PRIORITY_IDLE,
413 &cleaning_task, crc);
420 * Load the datastore plugin.
422 static struct GNUNET_DATASTORE_PluginFunctions *
423 load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
425 static struct GNUNET_DATASTORE_PluginEnvironment env;
426 struct GNUNET_DATASTORE_PluginFunctions *ret;
431 GNUNET_CONFIGURATION_get_value_string (cfg, "DATASTORE", "DATABASE",
434 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
435 _("No `%s' specified for `%s' in configuration!\n"), "DATABASE",
440 env.duc = &disk_utilization_change_cb;
442 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Loading `%s' datastore plugin\n"),
444 GNUNET_asprintf (&libname, "libgnunet_plugin_datastore_%s", name);
445 if (NULL == (ret = GNUNET_PLUGIN_load (libname, &env)))
447 FPRINTF (stderr, "Failed to load plugin `%s'!\n", name);
449 GNUNET_free (libname);
452 GNUNET_free (libname);
459 run (void *cls, char *const *args, const char *cfgfile,
460 const struct GNUNET_CONFIGURATION_Handle *c)
462 struct GNUNET_DATASTORE_PluginFunctions *api;
463 struct CpsRunContext *crc;
470 api = load_plugin (c);
474 "%s", "Could not initialize plugin, assuming database not configured. Test not run!\n");
477 crc = GNUNET_new (struct CpsRunContext);
482 GNUNET_SCHEDULER_add_now (&test, crc);
487 main (int argc, char *argv[])
491 char *const xargv[] = {
492 "perf-plugin-datastore",
497 struct GNUNET_GETOPT_CommandLineOption options[] = {
498 GNUNET_GETOPT_OPTION_END
501 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
502 GNUNET_snprintf (dir_name, sizeof (dir_name), "/tmp/perf-gnunet-datastore-%s",
504 GNUNET_DISK_directory_remove (dir_name);
505 GNUNET_log_setup ("perf-plugin-datastore",
508 GNUNET_snprintf (category, sizeof (category), "DATASTORE-%s", plugin_name);
509 GNUNET_snprintf (cfg_name, sizeof (cfg_name),
510 "perf_plugin_datastore_data_%s.conf", plugin_name);
511 GNUNET_PROGRAM_run ((sizeof (xargv) / sizeof (char *)) - 1, xargv,
512 "perf-plugin-datastore", "nohelp", options, &run, NULL);
514 FPRINTF (stderr, "Missed some testcases: %u\n", ok);
515 GNUNET_DISK_directory_remove (dir_name);
520 /* end of perf_plugin_datastore.c */